Repository: olingo-odata2 Updated Branches: refs/heads/master 7c4eae45f -> 1d8b7b916
[OLINGO-646] Start fixing debug message Project: http://git-wip-us.apache.org/repos/asf/olingo-odata2/repo Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata2/commit/1d8b7b91 Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata2/tree/1d8b7b91 Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata2/diff/1d8b7b91 Branch: refs/heads/master Commit: 1d8b7b916d30380c26198ebed97f236b896fbbe0 Parents: 7c4eae4 Author: Christian Amend <[email protected]> Authored: Thu Apr 30 14:09:12 2015 +0200 Committer: Christian Amend <[email protected]> Committed: Thu Apr 30 14:09:12 2015 +0200 ---------------------------------------------------------------------- .../olingo/odata2/core/ODataContextImpl.java | 13 +- .../odata2/fit/basic/DebugDisabledTest.java | 145 +++++++++++++++++++ .../odata2/testutil/fit/AbstractFitTest.java | 17 +++ .../testutil/fit/DebugCallbackFactoryFlase.java | 41 ++++++ .../testutil/fit/DebugCallbackFactoryTrue.java | 41 ++++++ .../odata2/testutil/server/TestServer.java | 25 +--- 6 files changed, 256 insertions(+), 26 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/1d8b7b91/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ODataContextImpl.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ODataContextImpl.java b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ODataContextImpl.java index d38664e..54679ac 100644 --- a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ODataContextImpl.java +++ b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ODataContextImpl.java @@ -33,7 +33,6 @@ import org.apache.olingo.odata2.api.exception.ODataException; import org.apache.olingo.odata2.api.processor.ODataContext; import org.apache.olingo.odata2.api.processor.ODataRequest; import org.apache.olingo.odata2.api.uri.PathInfo; -import org.apache.olingo.odata2.core.debug.ODataDebugResponseWrapper; /** * Context. @@ -285,14 +284,10 @@ public class ODataContextImpl implements ODataContext { private boolean checkDebugMode(final Map<String, String> queryParameters) { final ODataDebugCallback callback = getServiceFactory().getCallback(ODataDebugCallback.class); - return callback == null ? getQueryDebugValue(queryParameters) != null : callback.isDebugEnabled(); - } - - private static String getQueryDebugValue(final Map<String, String> queryParameters) { - final String debugValue = queryParameters.get(ODataDebugResponseWrapper.ODATA_DEBUG_QUERY_PARAMETER); - return ODataDebugResponseWrapper.ODATA_DEBUG_JSON.equals(debugValue) - || ODataDebugResponseWrapper.ODATA_DEBUG_HTML.equals(debugValue) - || ODataDebugResponseWrapper.ODATA_DEBUG_DOWNLOAD.equals(debugValue) ? debugValue : null; + if(callback != null){ + return callback.isDebugEnabled(); + } + return false; } public void setBatchParentContext(final ODataContext ctx) { http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/1d8b7b91/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/basic/DebugDisabledTest.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/basic/DebugDisabledTest.java b/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/basic/DebugDisabledTest.java new file mode 100644 index 0000000..2d5011f --- /dev/null +++ b/odata2-lib/odata-fit/src/test/java/org/apache/olingo/odata2/fit/basic/DebugDisabledTest.java @@ -0,0 +1,145 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + ******************************************************************************/ +package org.apache.olingo.odata2.fit.basic; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.apache.http.HttpResponse; +import org.apache.olingo.odata2.api.commons.HttpStatusCodes; +import org.apache.olingo.odata2.api.exception.ODataException; +import org.apache.olingo.odata2.api.processor.ODataResponse; +import org.apache.olingo.odata2.api.processor.ODataSingleProcessor; +import org.apache.olingo.odata2.api.processor.part.ServiceDocumentProcessor; +import org.apache.olingo.odata2.api.uri.info.GetServiceDocumentUriInfo; +import org.apache.olingo.odata2.testutil.fit.DebugCallbackFactoryFlase; +import org.apache.olingo.odata2.testutil.fit.DebugCallbackFactoryTrue; +import org.apache.olingo.odata2.testutil.fit.FitStaticServiceFactory; +import org.apache.olingo.odata2.testutil.helper.StringHelper; +import org.apache.olingo.odata2.testutil.server.ServletType; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +@RunWith(Parameterized.class) +public class DebugDisabledTest extends AbstractBasicTest { + + public DebugDisabledTest(final ServletType servletType) { + super(servletType); + } + + @Test + public void checkNoDebugCallbackMustResultInNoDebugResponse() throws Exception { + startCustomServer(FitStaticServiceFactory.class); + HttpResponse response = executeGetRequest("/?odata-debug=json"); + assertEquals(HttpStatusCodes.OK.getStatusCode(), response.getStatusLine().getStatusCode()); + String payload = StringHelper.inputStreamToString(response.getEntity().getContent()); + assertEquals("service document", payload); + + response = executeGetRequest("/?odata-debug=html"); + assertEquals(HttpStatusCodes.OK.getStatusCode(), response.getStatusLine().getStatusCode()); + payload = StringHelper.inputStreamToString(response.getEntity().getContent()); + assertEquals("service document", payload); + + response = executeGetRequest("/?odata-debug=download"); + assertEquals(HttpStatusCodes.OK.getStatusCode(), response.getStatusLine().getStatusCode()); + payload = StringHelper.inputStreamToString(response.getEntity().getContent()); + assertEquals("service document", payload); + + stopCustomServer(); + } + + @Test + public void checkDebugCallbackFalseMustResultInNoDebugResponse() throws Exception { + startCustomServer(DebugCallbackFactoryFlase.class); + HttpResponse response = executeGetRequest("/?odata-debug=json"); + assertEquals(HttpStatusCodes.OK.getStatusCode(), response.getStatusLine().getStatusCode()); + String payload = StringHelper.inputStreamToString(response.getEntity().getContent()); + assertEquals("service document", payload); + + response = executeGetRequest("/?odata-debug=html"); + assertEquals(HttpStatusCodes.OK.getStatusCode(), response.getStatusLine().getStatusCode()); + payload = StringHelper.inputStreamToString(response.getEntity().getContent()); + assertEquals("service document", payload); + + response = executeGetRequest("/?odata-debug=download"); + assertEquals(HttpStatusCodes.OK.getStatusCode(), response.getStatusLine().getStatusCode()); + payload = StringHelper.inputStreamToString(response.getEntity().getContent()); + assertEquals("service document", payload); + + stopCustomServer(); + } + + @Test + public void checkDebugCallbackTrueMustResultInDebugResponse() throws Exception { + startCustomServer(DebugCallbackFactoryTrue.class); + HttpResponse response = null; + String payload = null; + response = executeGetRequest("/?odata-debug=json"); + assertEquals(HttpStatusCodes.OK.getStatusCode(), response.getStatusLine().getStatusCode()); + payload = StringHelper.inputStreamToString(response.getEntity().getContent()); + assertFalse("service document".equals(payload)); + assertTrue(payload.startsWith("{\"request\":{\"method\":\"GET\"")); + assertTrue(payload.contains("service document")); + + response = executeGetRequest("/?odata-debug=html"); + assertEquals(HttpStatusCodes.OK.getStatusCode(), response.getStatusLine().getStatusCode()); + payload = StringHelper.inputStreamToString(response.getEntity().getContent()); + assertFalse("service document".equals(payload)); + assertTrue(payload.startsWith("<!DOCTYPE html")); + assertTrue(payload.contains("service document")); + + response = executeGetRequest("/?odata-debug=download"); + assertEquals(HttpStatusCodes.OK.getStatusCode(), response.getStatusLine().getStatusCode()); + payload = StringHelper.inputStreamToString(response.getEntity().getContent()); + assertFalse("service document".equals(payload)); + assertTrue(payload.contains("service document")); + + stopCustomServer(); + } + + @Override + @Before + public void before() { + // Do nothing here to stop default server from starting + } + + @Override + @After + public void after() { + // Do nothing here to stop default server from ending + } + + @Override + protected ODataSingleProcessor createProcessor() throws ODataException { + final ODataSingleProcessor processor = mock(ODataSingleProcessor.class); + when( + ((ServiceDocumentProcessor) processor).readServiceDocument(any(GetServiceDocumentUriInfo.class), + any(String.class))) + .thenReturn(ODataResponse.entity("service document").status(HttpStatusCodes.OK).build()); + return processor; + } + +} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/1d8b7b91/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/fit/AbstractFitTest.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/fit/AbstractFitTest.java b/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/fit/AbstractFitTest.java index 6134fe8..9e85f72 100644 --- a/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/fit/AbstractFitTest.java +++ b/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/fit/AbstractFitTest.java @@ -77,6 +77,23 @@ public abstract class AbstractFitTest extends BaseTest { return service; } + protected void startCustomServer(Class<? extends FitStaticServiceFactory> factoryClass){ + try { + service = createService(); + server.startServer(service, factoryClass); + } catch (final ODataException e) { + throw new TestUtilRuntimeException(e); + } + } + + protected void stopCustomServer(){ + try { + server.stopServer(); + } catch (final ServerRuntimeException e) { + throw new TestUtilRuntimeException(e); + } + } + protected abstract ODataService createService() throws ODataException; @Before http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/1d8b7b91/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/fit/DebugCallbackFactoryFlase.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/fit/DebugCallbackFactoryFlase.java b/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/fit/DebugCallbackFactoryFlase.java new file mode 100644 index 0000000..9e7cdc6 --- /dev/null +++ b/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/fit/DebugCallbackFactoryFlase.java @@ -0,0 +1,41 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + ******************************************************************************/ +package org.apache.olingo.odata2.testutil.fit; + +import org.apache.olingo.odata2.api.ODataCallback; +import org.apache.olingo.odata2.api.ODataDebugCallback; + +public class DebugCallbackFactoryFlase extends FitStaticServiceFactory { + + @SuppressWarnings("unchecked") + @Override + public <T extends ODataCallback> T getCallback(final Class<? extends ODataCallback> callbackInterface) { + if (callbackInterface.isAssignableFrom(FitErrorCallback.class)) { + return (T) new FitErrorCallback(); + } else if (callbackInterface.isAssignableFrom(ODataDebugCallback.class)) { + return (T) new ODataDebugCallback() { + @Override + public boolean isDebugEnabled() { + return false; + } + }; + } + return null; + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/1d8b7b91/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/fit/DebugCallbackFactoryTrue.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/fit/DebugCallbackFactoryTrue.java b/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/fit/DebugCallbackFactoryTrue.java new file mode 100644 index 0000000..66f483d --- /dev/null +++ b/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/fit/DebugCallbackFactoryTrue.java @@ -0,0 +1,41 @@ +/******************************************************************************* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + ******************************************************************************/ +package org.apache.olingo.odata2.testutil.fit; + +import org.apache.olingo.odata2.api.ODataCallback; +import org.apache.olingo.odata2.api.ODataDebugCallback; + +public class DebugCallbackFactoryTrue extends FitStaticServiceFactory { + + @SuppressWarnings("unchecked") + @Override + public <T extends ODataCallback> T getCallback(final Class<? extends ODataCallback> callbackInterface) { + if (callbackInterface.isAssignableFrom(FitErrorCallback.class)) { + return (T) new FitErrorCallback(); + } else if (callbackInterface.isAssignableFrom(ODataDebugCallback.class)) { + return (T) new ODataDebugCallback() { + @Override + public boolean isDebugEnabled() { + return true; + } + }; + } + return null; + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/1d8b7b91/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/server/TestServer.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/server/TestServer.java b/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/server/TestServer.java index 3e2c097..780cdb4 100644 --- a/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/server/TestServer.java +++ b/odata2-lib/odata-testutil/src/main/java/org/apache/olingo/odata2/testutil/server/TestServer.java @@ -154,23 +154,6 @@ public class TestServer { return contextHandler; } - // private ServletContextHandler createODataContextHandler(final Class<? extends ODataServiceFactory> factoryClass) - // throws InstantiationException, IllegalAccessException, ClassNotFoundException { - // String odataServlet = "org.apache.olingo.odata2.core.servlet.ODataServlet"; - // final HttpServlet httpServlet = (HttpServlet) Class.forName(odataServlet).newInstance(); - // final ServletHolder odataServletHolder = new ServletHolder(httpServlet); - // odataServletHolder.setInitParameter(ODataServiceFactory.FACTORY_LABEL, factoryClass.getCanonicalName()); - // - // if (pathSplit > 0) { - // odataServletHolder.setInitParameter(ODataServiceFactory.PATH_SPLIT_LABEL, Integer.toString(pathSplit)); - // } - // - // final ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS); - // contextHandler.setContextPath("/abc"); - // contextHandler.addServlet(odataServletHolder, path + "/*"); - // return contextHandler; - // } - public void startServer(final ODataService service) { startServer(FitStaticServiceFactory.class); @@ -179,6 +162,14 @@ public class TestServer { } } + public void startServer(final ODataService service, Class<? extends FitStaticServiceFactory> clazz) { + startServer(clazz); + + if ((server != null) && server.isStarted()) { + FitStaticServiceFactory.bindService(this, service); + } + } + public void stopServer() { try { if (server != null) {
