From 57a11aff6b1658e66424290423a4cadb104661f5 Mon Sep 17 00:00:00 2001 From: Christian Amend Date: Tue, 28 Jul 2015 15:35:50 +0200 Subject: [PATCH] [OLINGO-731] Added tests --- .../olingo/server/core/ODataHandler.java | 16 +- .../core/debug/DebugResponseHelperImpl.java | 15 +- .../server/core/debug/DebugTabBody.java | 19 ++- .../server/core/debug/DebugTabRequest.java | 97 ++++++----- .../server/core/debug/DebugTabResponse.java | 20 ++- .../server/core/debug/DebugTabServer.java | 10 +- .../server/core/debug/ServerCoreDebugger.java | 7 +- .../serializer/BatchResponseSerializer.java | 1 - .../core/debug/AbstractDebugTabTest.java | 58 +++++++ .../server/core/debug/DebugTabBodyTest.java | 37 +++++ .../core/debug/DebugTabRequestTest.java | 155 ++++++++++++++++++ .../core/debug/DebugTabResponseTest.java | 79 +++++++++ .../server/core/debug/DebugTabServerTest.java | 84 ++++++++++ .../core/debug/ServerCoreDebuggerTest.java | 121 ++++++++++++++ 14 files changed, 650 insertions(+), 69 deletions(-) create mode 100644 lib/server-core/src/test/java/org/apache/olingo/server/core/debug/AbstractDebugTabTest.java create mode 100644 lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabBodyTest.java create mode 100644 lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabRequestTest.java create mode 100644 lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabResponseTest.java create mode 100644 lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabServerTest.java create mode 100644 lib/server-core/src/test/java/org/apache/olingo/server/core/debug/ServerCoreDebuggerTest.java diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java index 1d1b270fd..bfbc5ac3d 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java @@ -74,10 +74,9 @@ public class ODataHandler { public ODataResponse process(final ODataRequest request) { ODataResponse response = new ODataResponse(); + int measurementHandel = debugger.startRuntimeMeasurement("ODataHandler", "processInternal"); try { - processInternal(request, response); - } catch (final UriValidationException e) { ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e, null); handleException(request, response, serverError, e); @@ -112,6 +111,7 @@ public class ODataHandler { ODataServerError serverError = ODataExceptionHelper.createServerErrorObject(e); handleException(request, response, serverError, e); } + debugger.stopRuntimeMeasurement(measurementHandel); return response; } @@ -119,13 +119,19 @@ public class ODataHandler { throws ODataApplicationException, ODataLibraryException { validateODataVersion(request, response); + int measurementUriParser = debugger.startRuntimeMeasurement("UriParser", "parseUri"); uriInfo = new Parser().parseUri(request.getRawODataPath(), request.getRawQueryPath(), null, serviceMetadata.getEdm()); + debugger.stopRuntimeMeasurement(measurementUriParser); + int measurementUriValidator = debugger.startRuntimeMeasurement("UriValidator", "validate"); final HttpMethod method = request.getMethod(); new UriValidator().validate(uriInfo, method); + debugger.stopRuntimeMeasurement(measurementUriValidator); + int measurementDispatcher = debugger.startRuntimeMeasurement("Dispatcher", "dispatch"); new ODataDispatcher(method, uriInfo, this).dispatch(request, response); + debugger.stopRuntimeMeasurement(measurementDispatcher); } public void handleException(final ODataRequest request, final ODataResponse response, @@ -146,7 +152,9 @@ public class ODataHandler { } catch (final ContentNegotiatorException e) { requestedContentType = ContentType.JSON; } + int measurementHandle = debugger.startRuntimeMeasurement("ErrorProcessor", "processError"); exceptionProcessor.processError(request, response, serverError, requestedContentType); + debugger.stopRuntimeMeasurement(measurementHandle); } private void validateODataVersion(final ODataRequest request, final ODataResponse response) @@ -196,8 +204,8 @@ public class ODataHandler { public Exception getLastThrownException() { return lastThrownException; } - - public UriInfo getUriInfo(){ + + public UriInfo getUriInfo() { return uriInfo; } } diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugResponseHelperImpl.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugResponseHelperImpl.java index d99dd4c16..94ab544ab 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugResponseHelperImpl.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugResponseHelperImpl.java @@ -76,7 +76,7 @@ public class DebugResponseHelperImpl implements DebugResponseHelper { + new Date().toString().replace(' ', '_').replace(':', '.') + ".html"); // Download is the same as html except for the above header case HTML: - String title = debugInfo.getRequest() == null ? + String title = debugInfo.getRequest() == null ? "V4 Service" : "V4 Service: " + debugInfo.getRequest().getRawODataPath(); body = wrapInHtml(parts, title); contentTypeString = ContentType.TEXT_HTML.toContentTypeString(); @@ -164,7 +164,7 @@ public class DebugResponseHelperImpl implements DebugResponseHelper { gen.writeEndObject(); gen.close(); - csb.close(); + csb.closeWrite(); return csb.getInputStream(); } @@ -258,14 +258,15 @@ public class DebugResponseHelperImpl implements DebugResponseHelper { .append("\n\n"); for (final String name : entries.keySet()) { final String value = entries.get(name); + writer.append("").append(name).append("") + .append(""); if (value != null) { - writer.append("").append(name).append("") - .append("") - .append(escapeHtml(value)) - .append("\n"); + writer.append(escapeHtml(value)); + }else{ + writer.append("null"); } + writer.append("\n"); } writer.append("\n\n"); } - } diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabBody.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabBody.java index 43e9ba60e..5608d584a 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabBody.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabBody.java @@ -50,11 +50,15 @@ public class DebugTabBody implements DebugTab { public DebugTabBody(final ODataResponse response, final String serviceRoot) { this.response = response; - this.serviceRoot = serviceRoot; - final String contentType = response.getHeaders().get(HttpHeader.CONTENT_TYPE); - // TODO: Differentiate better - if (contentType != null) { - responseContent = ResponseContent.JSON; + this.serviceRoot = serviceRoot == null ? "/" : serviceRoot; + if (response != null) { + final String contentType = response.getHeaders().get(HttpHeader.CONTENT_TYPE); + // TODO: Differentiate better + if (contentType != null) { + responseContent = ResponseContent.JSON; + } else { + responseContent = ResponseContent.TEXT; + } } else { responseContent = ResponseContent.TEXT; } @@ -74,7 +78,7 @@ public class DebugTabBody implements DebugTab { // @Override public void appendJson(final JsonGenerator gen) throws IOException { - if (response.getContent() == null) { + if (response == null || response.getContent() == null) { gen.writeNull(); } else { gen.writeString(getContentString()); @@ -106,7 +110,8 @@ public class DebugTabBody implements DebugTab { @Override public void appendHtml(final Writer writer) throws IOException { - final String body = response.getContent() == null ? "ODataLibrary: null body." : getContentString(); + final String body = + response == null || response.getContent() == null ? "ODataLibrary: No body." : getContentString(); switch (responseContent) { case XML: writer.append("
\n");
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabRequest.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabRequest.java
index 8eba537bc..be38f681a 100644
--- a/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabRequest.java
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabRequest.java
@@ -20,7 +20,7 @@ package org.apache.olingo.server.core.debug;
 
 import java.io.IOException;
 import java.io.Writer;
-import java.util.LinkedHashMap;
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 
@@ -36,33 +36,20 @@ public class DebugTabRequest implements DebugTab {
   private final String method;
   private final String uri;
   private final String protocol;
-  private final Map headers;
+  private final Map> headers;
 
   public DebugTabRequest(ODataRequest request) {
-    method = request.getMethod() == null ? "unkown" : request.getMethod().toString();
-    uri = request.getRawRequestUri() == null ? "unkown" : request.getRawRequestUri();
-    protocol = request.getProtocol() == null ? "unkown" : request.getProtocol();
-    // TODO: Should we really wrap the headers here or keep the original structure?
-    headers = wrapHeaders(request.getAllHeaders());
-  }
-
-  private Map wrapHeaders(Map> allHeaders) {
-    Map localHeaders = new LinkedHashMap();
-    for (Map.Entry> entry : allHeaders.entrySet()) {
-      String value = null;
-      if (entry.getValue() != null) {
-        value = "";
-        boolean first = true;
-        for (String valuePart : entry.getValue()) {
-          if (!first) {
-            value = value + ", ";
-          }
-          value = value + valuePart;
-        }
-      }
-      localHeaders.put(entry.getKey(), value);
+    if (request != null) {
+      method = request.getMethod() == null ? "unkown" : request.getMethod().toString();
+      uri = request.getRawRequestUri() == null ? "unkown" : request.getRawRequestUri();
+      protocol = request.getProtocol() == null ? "unkown" : request.getProtocol();
+      headers = request.getAllHeaders();
+    } else {
+      method = "unkown";
+      uri = "unkown";
+      protocol = "unkown";
+      headers = Collections.emptyMap();
     }
-    return localHeaders;
   }
 
   @Override
@@ -70,25 +57,26 @@ public class DebugTabRequest implements DebugTab {
     writer.append("

Request Method

\n") .append("

").append(method).append("

\n") .append("

Request URI

\n") - .append("

").append(DebugResponseHelperImpl.escapeHtml(uri.toString())).append("

\n") + .append("

").append(DebugResponseHelperImpl.escapeHtml(uri)).append("

\n") .append("

Request Protocol

\n") - .append("

").append(protocol).append("

\n"); + .append("

").append(DebugResponseHelperImpl.escapeHtml(protocol)).append("

\n"); writer.append("

Request Headers

\n"); - DebugResponseHelperImpl.appendHtmlTable(writer, headers); -// .append("\n\n") -// .append("\n") -// .append("\n\n"); -// for (final String name : headers.keySet()) { -// for (final String value : headers.get(name)) { -// if (value != null) { -// writer.append("") -// .append("\n"); -// } -// } -// } -// writer.append("\n
NameValue
").append(name).append("").append(DebugResponseHelperImpl.escapeHtml(value)) -// .append("
\n"); + writer.append("\n\n") + .append("\n") + .append("\n\n"); + for (final Map.Entry> entry : headers.entrySet()) { + List headersList = entry.getValue(); + if (headersList != null && !headersList.isEmpty()) { + for (String headerValue : headersList) { + writer.append("") + .append("\n"); + } + } + } + writer.append("\n
NameValue
").append(entry.getKey()).append("") + .append(DebugResponseHelperImpl.escapeHtml(headerValue)) + .append("
\n"); } @Override @@ -107,7 +95,32 @@ public class DebugTabRequest implements DebugTab { if (!headers.isEmpty()) { gen.writeFieldName("headers"); - DebugResponseHelperImpl.appendJsonTable(gen, headers); + + gen.writeStartObject(); + + for (Map.Entry> entry : headers.entrySet()) { + List headersList = entry.getValue(); + if (headersList != null && !headersList.isEmpty()) { + if (headersList.size() == 1) { + gen.writeStringField(entry.getKey(), headersList.get(0)); + } else { + gen.writeFieldName(entry.getKey()); + gen.writeStartArray(); + for (String headerValue : headersList) { + if (headerValue != null) { + gen.writeString(headerValue); + } else { + gen.writeNull(); + } + } + gen.writeEndArray(); + } + } else { + gen.writeNullField(entry.getKey()); + } + } + + gen.writeEndObject(); } gen.writeEndObject(); diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabResponse.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabResponse.java index 5cb153ca9..528cf6160 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabResponse.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabResponse.java @@ -20,6 +20,7 @@ package org.apache.olingo.server.core.debug; import java.io.IOException; import java.io.Writer; +import java.util.Collections; import java.util.Map; import org.apache.olingo.commons.api.http.HttpStatusCode; @@ -39,9 +40,14 @@ public class DebugTabResponse implements DebugTab { public DebugTabResponse(final ODataResponse applicationResponse, final String serviceRoot) { this.response = applicationResponse; - this.serviceRoot = serviceRoot; - status = HttpStatusCode.fromStatusCode(response.getStatusCode()); - headers = response.getHeaders(); + if (response != null) { + status = HttpStatusCode.fromStatusCode(response.getStatusCode()); + headers = response.getHeaders(); + } else { + status = HttpStatusCode.INTERNAL_SERVER_ERROR; + headers = Collections.emptyMap(); + } + this.serviceRoot = serviceRoot == null ? "/" : serviceRoot; } @Override @@ -67,7 +73,11 @@ public class DebugTabResponse implements DebugTab { } gen.writeFieldName("body"); - new DebugTabBody(response, serviceRoot).appendJson(gen); + if (response != null && response.getContent() != null) { + new DebugTabBody(response, serviceRoot).appendJson(gen); + } else { + gen.writeNull(); + } gen.writeEndObject(); } @@ -80,7 +90,7 @@ public class DebugTabResponse implements DebugTab { .append("

Response Headers

\n"); DebugResponseHelperImpl.appendHtmlTable(writer, headers); writer.append("

Response Body

\n"); - if (response.getContent() != null) { + if (response != null && response.getContent() != null) { new DebugTabBody(response, serviceRoot).appendHtml(writer); } else { writer.append("

ODataLibrary: no response body

"); diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabServer.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabServer.java index 4eb95ba59..2faae3e11 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabServer.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/DebugTabServer.java @@ -42,7 +42,11 @@ public class DebugTabServer implements DebugTab { @Override public void appendJson(JsonGenerator gen) throws IOException { - DebugResponseHelperImpl.appendJsonTable(gen, serverEnvironmentVaribles); + if (serverEnvironmentVaribles != null && !serverEnvironmentVaribles.isEmpty()) { + DebugResponseHelperImpl.appendJsonTable(gen, serverEnvironmentVaribles); + } else { + gen.writeNull(); + } } @Override @@ -52,6 +56,8 @@ public class DebugTabServer implements DebugTab { .append("

").append(pack.getImplementationTitle()) .append(" Version ").append(pack.getImplementationVersion()).append("

\n") .append("

Server Environment

\n"); - DebugResponseHelperImpl.appendHtmlTable(writer, serverEnvironmentVaribles); + if (serverEnvironmentVaribles != null && !serverEnvironmentVaribles.isEmpty()) { + DebugResponseHelperImpl.appendHtmlTable(writer, serverEnvironmentVaribles); + } } } diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/ServerCoreDebugger.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/ServerCoreDebugger.java index fcf611df6..8bdbe3915 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/ServerCoreDebugger.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/debug/ServerCoreDebugger.java @@ -64,6 +64,11 @@ public class ServerCoreDebugger { public ODataResponse createDebugResponse(final HttpServletRequest request, final Exception exception, final ODataRequest odRequest, final ODataResponse odResponse, UriInfo uriInfo, Map serverEnvironmentVaribles) { + //Failsafe so we do not generate unauthorized debug messages + if(!isDebugMode){ + return odResponse; + } + try { DebugInformation debugInfo = createDebugInformation(request, exception, odRequest, odResponse, uriInfo, serverEnvironmentVaribles); @@ -80,7 +85,7 @@ public class ServerCoreDebugger { odResponse.setHeader(HttpHeader.CONTENT_TYPE, ContentType.TEXT_PLAIN.toContentTypeString()); InputStream content = new ByteArrayInputStream("ODataLibrary: Could not assemble debug response.".getBytes()); odResponse.setContent(content); - return null; + return odResponse; } private DebugInformation createDebugInformation(final HttpServletRequest request, final Exception exception, diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/BatchResponseSerializer.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/BatchResponseSerializer.java index 377c5e1d6..c907b8f15 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/BatchResponseSerializer.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/BatchResponseSerializer.java @@ -22,7 +22,6 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import java.nio.ByteBuffer; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/AbstractDebugTabTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/AbstractDebugTabTest.java new file mode 100644 index 000000000..43b1aaaaf --- /dev/null +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/AbstractDebugTabTest.java @@ -0,0 +1,58 @@ +/* + * 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.server.core.debug; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.StringWriter; + +import org.apache.commons.io.IOUtils; +import org.apache.olingo.server.core.serializer.utils.CircleStreamBuffer; + +import com.fasterxml.jackson.core.JsonEncoding; +import com.fasterxml.jackson.core.JsonFactory; +import com.fasterxml.jackson.core.JsonGenerator; + +public abstract class AbstractDebugTabTest { + + protected String createHtml(DebugTab tab) throws Exception { + StringWriter writer = new StringWriter(); + tab.appendHtml(writer); + writer.flush(); + byte[] bytes = writer.toString().getBytes("UTF-8"); + return IOUtils.toString(new ByteArrayInputStream(bytes)); + } + + protected String createJson(DebugTab requestTab) throws IOException { + CircleStreamBuffer csb = new CircleStreamBuffer(); + JsonGenerator gen = new JsonFactory().createGenerator(csb.getOutputStream(), JsonEncoding.UTF8); + requestTab.appendJson(gen); + gen.flush(); + gen.close(); + csb.closeWrite(); + return IOUtils.toString(csb.getInputStream()); + } + + protected void print(DebugTab tab) throws Exception{ + System.out.println(createJson(tab)); + System.out.println("---------------------------------------------------------"); + System.out.println(createHtml(tab)); + } + +} diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabBodyTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabBodyTest.java new file mode 100644 index 000000000..7d3a58e43 --- /dev/null +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabBodyTest.java @@ -0,0 +1,37 @@ +/* + * 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.server.core.debug; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class DebugTabBodyTest extends AbstractDebugTabTest { + + @Test + public void nullResponseMustNotLeadToException() throws Exception { + DebugTabBody tab = new DebugTabBody(null, null); + + String expectedHtml = "
\n"
+        + "ODataLibrary: No body.
\n"; + + assertEquals("null", createJson(tab)); + assertEquals(expectedHtml, createHtml(tab)); + } +} diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabRequestTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabRequestTest.java new file mode 100644 index 000000000..368930152 --- /dev/null +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabRequestTest.java @@ -0,0 +1,155 @@ +/* + * 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.server.core.debug; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.olingo.commons.api.http.HttpMethod; +import org.apache.olingo.server.api.ODataRequest; +import org.junit.Test; + +public class DebugTabRequestTest extends AbstractDebugTabTest { + + @Test + public void initialRequestMustNotleadToException() throws Exception { + String expectedJson = "{\"method\":\"unkown\",\"uri\":\"unkown\",\"protocol\":\"unkown\"}"; + String expectedHtml = "

Request Method

\n" + + "

unkown

\n" + + "

Request URI

\n" + + "

unkown

\n" + + "

Request Protocol

\n" + + "

unkown

\n" + + "

Request Headers

\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "
NameValue
\n"; + + DebugTabRequest requestTab = new DebugTabRequest(null); + assertEquals(expectedJson, createJson(requestTab)); + assertEquals(expectedHtml, createHtml(requestTab)); + + requestTab = new DebugTabRequest(new ODataRequest()); + assertEquals(expectedJson, createJson(requestTab)); + assertEquals(expectedHtml, createHtml(requestTab)); + } + + @Test + public void onlyProtocolNotSet() throws Exception { + String expectedJson = "{\"method\":\"GET\",\"uri\":\"def&\",\"protocol\":\"unkown\"}"; + String expectedHtml = "

Request Method

\n" + + "

GET

\n" + + "

Request URI

\n" + + "

def&

\n" + + "

Request Protocol

\n" + + "

unkown

\n" + + "

Request Headers

\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "
NameValue
\n"; + + ODataRequest oDataRequest = new ODataRequest(); + oDataRequest.setMethod(HttpMethod.GET); + oDataRequest.setRawRequestUri("def&"); + + DebugTabRequest requestTab = new DebugTabRequest(oDataRequest); + assertEquals(expectedJson, createJson(requestTab)); + assertEquals(expectedHtml, createHtml(requestTab)); + } + + @Test + public void singleHeaderValue() throws Exception { + String expectedJson = + "{\"method\":\"GET\",\"uri\":\"def&\",\"protocol\":\"def&\",\"headers\":{\"HEADERNAME\":\"Value1\"}}"; + String expectedHtml = "

Request Method

\n" + + "

GET

\n" + + "

Request URI

\n" + + "

def&

\n" + + "

Request Protocol

\n" + + "

def&

\n" + + "

Request Headers

\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "
NameValue
HEADERNAMEValue1
\n"; + + ODataRequest oDataRequest = new ODataRequest(); + oDataRequest.setMethod(HttpMethod.GET); + oDataRequest.setRawRequestUri("def&"); + oDataRequest.setProtocol("def&"); + List headerValues = new ArrayList(); + headerValues.add("Value1"); + oDataRequest.addHeader("HeaderName", headerValues); + + DebugTabRequest requestTab = new DebugTabRequest(oDataRequest); + System.out.println(createHtml(requestTab)); + assertEquals(expectedJson, createJson(requestTab)); + assertEquals(expectedHtml, createHtml(requestTab)); + } + + @Test + public void multiHeaderValueResultsInMap() throws Exception { + String expectedJson = "{\"method\":\"GET\",\"uri\":\"def&\",\"protocol\":\"def&\"," + + "\"headers\":{\"HEADERNAME\":[\"Value1\",\"Value2\"]}}"; + String expectedHtml = "

Request Method

\n" + + "

GET

\n" + + "

Request URI

\n" + + "

def&

\n" + + "

Request Protocol

\n" + + "

def&

\n" + + "

Request Headers

\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "
NameValue
HEADERNAMEValue1
HEADERNAMEValue2
\n"; + + ODataRequest oDataRequest = new ODataRequest(); + oDataRequest.setMethod(HttpMethod.GET); + oDataRequest.setRawRequestUri("def&"); + oDataRequest.setProtocol("def&"); + List headerValues = new ArrayList(); + headerValues.add("Value1"); + headerValues.add("Value2"); + oDataRequest.addHeader("HeaderName", headerValues); + + DebugTabRequest requestTab = new DebugTabRequest(oDataRequest); + assertEquals(expectedJson, createJson(requestTab)); + assertEquals(expectedHtml, createHtml(requestTab)); + } + +} diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabResponseTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabResponseTest.java new file mode 100644 index 000000000..af55a4086 --- /dev/null +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabResponseTest.java @@ -0,0 +1,79 @@ +/* + * 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.server.core.debug; + +import static org.junit.Assert.assertEquals; + +import org.apache.olingo.commons.api.http.HttpStatusCode; +import org.apache.olingo.server.api.ODataResponse; +import org.junit.Test; + +public class DebugTabResponseTest extends AbstractDebugTabTest { + + @Test + public void nullResponseMustNotLeadToException() throws Exception { + DebugTabResponse tab = new DebugTabResponse(null, null); + + String expectedJson = "{\"status\":{\"code\":\"500\",\"info\":\"Internal Server Error\"},\"body\":null}"; + String expectedHtml = "

Status Code

\n" + + "

500 Internal Server Error

\n" + + "

Response Headers

\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "
NameValue
\n" + + "

Response Body

\n" + + "

ODataLibrary: no response body

"; + + assertEquals(expectedJson, createJson(tab)); + assertEquals(expectedHtml, createHtml(tab)); + } + + @Test + public void withInformationNoBody() throws Exception { + ODataResponse response = new ODataResponse(); + response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode()); + response.setHeader("headername", "headervalue"); + response.setHeader("headername2", "headervalue2"); + DebugTabResponse tab = new DebugTabResponse(response, null); + + String expectedJson = "{\"status\":{\"code\":\"204\",\"info\":\"No Content\"}," + + "\"headers\":{\"headername\":\"headervalue\",\"headername2\":\"headervalue2\"},\"body\":null}"; + String expectedHtml = "

Status Code

\n" + + "

204 No Content

\n" + + "

Response Headers

\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "
NameValue
headernameheadervalue
headername2headervalue2
\n" + + "

Response Body

\n" + + "

ODataLibrary: no response body

"; + assertEquals(expectedJson, createJson(tab)); + assertEquals(expectedHtml, createHtml(tab)); + } + +} diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabServerTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabServerTest.java new file mode 100644 index 000000000..dc4748da3 --- /dev/null +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/DebugTabServerTest.java @@ -0,0 +1,84 @@ +/* + * 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.server.core.debug; + +import static org.junit.Assert.*; + +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.junit.Test; + +public class DebugTabServerTest extends AbstractDebugTabTest { + + @Test + public void nullServerInformationMustNotleadToException() throws Exception { + DebugTabServer serverTab = new DebugTabServer(null); + + assertEquals("null", createJson(serverTab)); + String html = createHtml(serverTab); + assertTrue(html.startsWith("

Library Version

")); + assertTrue(html.endsWith("

Server Environment

\n")); + } + + @Test + public void initialServerInformationMustNotleadToException() throws Exception { + Map env = Collections.emptyMap(); + DebugTabServer serverTab = new DebugTabServer(env); + + assertEquals("null", createJson(serverTab)); + String html = createHtml(serverTab); + assertTrue(html.startsWith("

Library Version

")); + assertTrue(html.endsWith("

Server Environment

\n")); + } + + @Test + public void twoParametersNoNull() throws Exception { + Map env = new LinkedHashMap(); + env.put("key1", "value1"); + env.put("key2", "value2"); + DebugTabServer serverTab = new DebugTabServer(env); + + String expectedJson = "{\"key1\":\"value1\",\"key2\":\"value2\"}"; + + assertEquals(expectedJson, createJson(serverTab)); + String html = createHtml(serverTab); + assertTrue(html.contains("key1value1")); + assertTrue(html.contains("key2value2")); + assertTrue(html.endsWith("\n")); + } + + @Test + public void twoParametersWithNull() throws Exception { + Map env = new LinkedHashMap(); + env.put("key1", null); + env.put("key2", null); + DebugTabServer serverTab = new DebugTabServer(env); + + String expectedJson = "{\"key1\":null,\"key2\":null}"; + + assertEquals(expectedJson, createJson(serverTab)); + String html = createHtml(serverTab); + assertTrue(html.contains("key1null")); + assertTrue(html.contains("key2null")); + assertTrue(html.endsWith("\n")); + } + +} diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/ServerCoreDebuggerTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/ServerCoreDebuggerTest.java new file mode 100644 index 000000000..32ec1954f --- /dev/null +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/debug/ServerCoreDebuggerTest.java @@ -0,0 +1,121 @@ +/* + * 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.server.core.debug; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.io.IOException; + +import javax.servlet.http.HttpServletRequest; + +import org.apache.commons.io.IOUtils; +import org.apache.olingo.commons.api.ODataRuntimeException; +import org.apache.olingo.server.api.OData; +import org.apache.olingo.server.api.ODataResponse; +import org.apache.olingo.server.api.debug.DebugInformation; +import org.apache.olingo.server.api.debug.DebugSupport; +import org.junit.Before; +import org.junit.Test; + +public class ServerCoreDebuggerTest { + + private ServerCoreDebugger debugger; + + @Before + public void setupDebugger() { + debugger = new ServerCoreDebugger(OData.newInstance()); + debugger.setDebugSupportProcessor(new LocalDebugProcessor()); + } + + @Test + public void standardIsDebugModeIsFlase() { + assertFalse(debugger.isDebugMode()); + } + + @Test + public void resolveDebugModeNoDebugSupportProcessor() { + HttpServletRequest request = mock(HttpServletRequest.class); + when(request.getParameter(DebugSupport.ODATA_DEBUG_QUERY_PARAMETER)).thenReturn(DebugSupport.ODATA_DEBUG_JSON); + + ServerCoreDebugger localDebugger = new ServerCoreDebugger(OData.newInstance()); + localDebugger.resolveDebugMode(request); + assertFalse(debugger.isDebugMode()); + } + + @Test + public void resolveDebugModeNullParameter() { + HttpServletRequest request = mock(HttpServletRequest.class); + when(request.getParameter(DebugSupport.ODATA_DEBUG_QUERY_PARAMETER)).thenReturn(null); + debugger.resolveDebugMode(request); + assertFalse(debugger.isDebugMode()); + } + + @Test + public void resolveDebugModeJsonNotAuthorized() { + HttpServletRequest request = mock(HttpServletRequest.class); + when(request.getParameter(DebugSupport.ODATA_DEBUG_QUERY_PARAMETER)).thenReturn(DebugSupport.ODATA_DEBUG_JSON); + + DebugSupport debugSupportMock = mock(DebugSupport.class); + when(debugSupportMock.isUserAuthorized()).thenReturn(false); + + ServerCoreDebugger localDebugger = new ServerCoreDebugger(OData.newInstance()); + localDebugger.setDebugSupportProcessor(debugSupportMock); + + localDebugger.resolveDebugMode(request); + assertFalse(debugger.isDebugMode()); + } + + @Test + public void testFailResponse() throws IOException { + HttpServletRequest request = mock(HttpServletRequest.class); + when(request.getParameter(DebugSupport.ODATA_DEBUG_QUERY_PARAMETER)).thenReturn(DebugSupport.ODATA_DEBUG_JSON); + debugger.resolveDebugMode(request); + ODataResponse debugResponse = debugger.createDebugResponse(null, null, null, null, null, null); + assertEquals(500, debugResponse.getStatusCode()); + assertEquals("ODataLibrary: Could not assemble debug response.", IOUtils.toString(debugResponse.getContent())); + } + + @Test + public void noDebugModeCreateDebugResponseCallMustDoNothing() { + ODataResponse odResponse = new ODataResponse(); + ODataResponse debugResponse = debugger.createDebugResponse(null, null, null, odResponse, null, null); + + assertTrue(odResponse == debugResponse); + } + + public class LocalDebugProcessor implements DebugSupport { + + @Override + public void init(OData odata) {} + + @Override + public boolean isUserAuthorized() { + return true; + } + + @Override + public ODataResponse createDebugResponse(String debugFormat, DebugInformation debugInfo) { + throw new ODataRuntimeException("Test"); + } + } +}