[OLINGO-731] Added tests

This commit is contained in:
Christian Amend 2015-07-28 15:35:50 +02:00
parent b146a403bb
commit 57a11aff6b
14 changed files with 650 additions and 69 deletions

View File

@ -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;
}
}

View File

@ -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("</thead>\n<tbody>\n");
for (final String name : entries.keySet()) {
final String value = entries.get(name);
writer.append("<tr><td class=\"name\">").append(name).append("</td>")
.append("<td class=\"value\">");
if (value != null) {
writer.append("<tr><td class=\"name\">").append(name).append("</td>")
.append("<td class=\"value\">")
.append(escapeHtml(value))
.append("</td></tr>\n");
writer.append(escapeHtml(value));
}else{
writer.append("null");
}
writer.append("</td></tr>\n");
}
writer.append("</tbody>\n</table>\n");
}
}

View File

@ -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("<pre class=\"code").append("xml").append("\">\n");

View File

@ -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<String, String> headers;
private final Map<String, List<String>> 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<String, String> wrapHeaders(Map<String, List<String>> allHeaders) {
Map<String, String> localHeaders = new LinkedHashMap<String, String>();
for (Map.Entry<String, List<String>> 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("<h2>Request Method</h2>\n")
.append("<p>").append(method).append("</p>\n")
.append("<h2>Request URI</h2>\n")
.append("<p>").append(DebugResponseHelperImpl.escapeHtml(uri.toString())).append("</p>\n")
.append("<p>").append(DebugResponseHelperImpl.escapeHtml(uri)).append("</p>\n")
.append("<h2>Request Protocol</h2>\n")
.append("<p>").append(protocol).append("</p>\n");
.append("<p>").append(DebugResponseHelperImpl.escapeHtml(protocol)).append("</p>\n");
writer.append("<h2>Request Headers</h2>\n");
DebugResponseHelperImpl.appendHtmlTable(writer, headers);
// .append("<table>\n<thead>\n")
// .append("<tr><th class=\"name\">Name</th><th class=\"value\">Value</th></tr>\n")
// .append("</thead>\n<tbody>\n");
// for (final String name : headers.keySet()) {
// for (final String value : headers.get(name)) {
// if (value != null) {
// writer.append("<tr><td class=\"name\">").append(name).append("</td>")
// .append("<td class=\"value\">").append(DebugResponseHelperImpl.escapeHtml(value))
// .append("</td></tr>\n");
// }
// }
// }
// writer.append("</tbody>\n</table>\n");
writer.append("<table>\n<thead>\n")
.append("<tr><th class=\"name\">Name</th><th class=\"value\">Value</th></tr>\n")
.append("</thead>\n<tbody>\n");
for (final Map.Entry<String, List<String>> entry : headers.entrySet()) {
List<String> headersList = entry.getValue();
if (headersList != null && !headersList.isEmpty()) {
for (String headerValue : headersList) {
writer.append("<tr><td class=\"name\">").append(entry.getKey()).append("</td>")
.append("<td class=\"value\">")
.append(DebugResponseHelperImpl.escapeHtml(headerValue))
.append("</td></tr>\n");
}
}
}
writer.append("</tbody>\n</table>\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<String, List<String>> entry : headers.entrySet()) {
List<String> 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();

View File

@ -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("<h2>Response Headers</h2>\n");
DebugResponseHelperImpl.appendHtmlTable(writer, headers);
writer.append("<h2>Response Body</h2>\n");
if (response.getContent() != null) {
if (response != null && response.getContent() != null) {
new DebugTabBody(response, serviceRoot).appendHtml(writer);
} else {
writer.append("<p>ODataLibrary: no response body</p>");

View File

@ -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("<p>").append(pack.getImplementationTitle())
.append(" Version ").append(pack.getImplementationVersion()).append("</p>\n")
.append("<h2>Server Environment</h2>\n");
DebugResponseHelperImpl.appendHtmlTable(writer, serverEnvironmentVaribles);
if (serverEnvironmentVaribles != null && !serverEnvironmentVaribles.isEmpty()) {
DebugResponseHelperImpl.appendHtmlTable(writer, serverEnvironmentVaribles);
}
}
}

View File

@ -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<String, String> 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,

View File

@ -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;

View File

@ -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));
}
}

View File

@ -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 = "<pre class=\"code\">\n"
+ "ODataLibrary: No body.</pre>\n";
assertEquals("null", createJson(tab));
assertEquals(expectedHtml, createHtml(tab));
}
}

View File

@ -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 = "<h2>Request Method</h2>\n"
+ "<p>unkown</p>\n"
+ "<h2>Request URI</h2>\n"
+ "<p>unkown</p>\n"
+ "<h2>Request Protocol</h2>\n"
+ "<p>unkown</p>\n"
+ "<h2>Request Headers</h2>\n"
+ "<table>\n"
+ "<thead>\n"
+ "<tr><th class=\"name\">Name</th><th class=\"value\">Value</th></tr>\n"
+ "</thead>\n"
+ "<tbody>\n"
+ "</tbody>\n"
+ "</table>\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 = "<h2>Request Method</h2>\n"
+ "<p>GET</p>\n"
+ "<h2>Request URI</h2>\n"
+ "<p>def&amp;</p>\n"
+ "<h2>Request Protocol</h2>\n"
+ "<p>unkown</p>\n"
+ "<h2>Request Headers</h2>\n"
+ "<table>\n"
+ "<thead>\n"
+ "<tr><th class=\"name\">Name</th><th class=\"value\">Value</th></tr>\n"
+ "</thead>\n"
+ "<tbody>\n"
+ "</tbody>\n"
+ "</table>\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 = "<h2>Request Method</h2>\n"
+ "<p>GET</p>\n"
+ "<h2>Request URI</h2>\n"
+ "<p>def&amp;</p>\n"
+ "<h2>Request Protocol</h2>\n"
+ "<p>def&amp;</p>\n"
+ "<h2>Request Headers</h2>\n"
+ "<table>\n"
+ "<thead>\n"
+ "<tr><th class=\"name\">Name</th><th class=\"value\">Value</th></tr>\n"
+ "</thead>\n"
+ "<tbody>\n"
+ "<tr><td class=\"name\">HEADERNAME</td><td class=\"value\">Value1</td></tr>\n"
+ "</tbody>\n"
+ "</table>\n";
ODataRequest oDataRequest = new ODataRequest();
oDataRequest.setMethod(HttpMethod.GET);
oDataRequest.setRawRequestUri("def&");
oDataRequest.setProtocol("def&");
List<String> headerValues = new ArrayList<String>();
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 = "<h2>Request Method</h2>\n"
+ "<p>GET</p>\n"
+ "<h2>Request URI</h2>\n"
+ "<p>def&amp;</p>\n"
+ "<h2>Request Protocol</h2>\n"
+ "<p>def&amp;</p>\n"
+ "<h2>Request Headers</h2>\n"
+ "<table>\n"
+ "<thead>\n"
+ "<tr><th class=\"name\">Name</th><th class=\"value\">Value</th></tr>\n"
+ "</thead>\n"
+ "<tbody>\n"
+ "<tr><td class=\"name\">HEADERNAME</td><td class=\"value\">Value1</td></tr>\n"
+ "<tr><td class=\"name\">HEADERNAME</td><td class=\"value\">Value2</td></tr>\n"
+ "</tbody>\n"
+ "</table>\n";
ODataRequest oDataRequest = new ODataRequest();
oDataRequest.setMethod(HttpMethod.GET);
oDataRequest.setRawRequestUri("def&");
oDataRequest.setProtocol("def&");
List<String> headerValues = new ArrayList<String>();
headerValues.add("Value1");
headerValues.add("Value2");
oDataRequest.addHeader("HeaderName", headerValues);
DebugTabRequest requestTab = new DebugTabRequest(oDataRequest);
assertEquals(expectedJson, createJson(requestTab));
assertEquals(expectedHtml, createHtml(requestTab));
}
}

View File

@ -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 = "<h2>Status Code</h2>\n"
+ "<p>500 Internal Server Error</p>\n"
+ "<h2>Response Headers</h2>\n"
+ "<table>\n"
+ "<thead>\n"
+ "<tr><th class=\"name\">Name</th><th class=\"value\">Value</th></tr>\n"
+ "</thead>\n"
+ "<tbody>\n"
+ "</tbody>\n"
+ "</table>\n"
+ "<h2>Response Body</h2>\n"
+ "<p>ODataLibrary: no response body</p>";
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 = "<h2>Status Code</h2>\n"
+ "<p>204 No Content</p>\n"
+ "<h2>Response Headers</h2>\n"
+ "<table>\n"
+ "<thead>\n"
+ "<tr><th class=\"name\">Name</th><th class=\"value\">Value</th></tr>\n"
+ "</thead>\n"
+ "<tbody>\n"
+ "<tr><td class=\"name\">headername</td><td class=\"value\">headervalue</td></tr>\n"
+ "<tr><td class=\"name\">headername2</td><td class=\"value\">headervalue2</td></tr>\n"
+ "</tbody>\n"
+ "</table>\n"
+ "<h2>Response Body</h2>\n"
+ "<p>ODataLibrary: no response body</p>";
assertEquals(expectedJson, createJson(tab));
assertEquals(expectedHtml, createHtml(tab));
}
}

View File

@ -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("<h2>Library Version</h2>"));
assertTrue(html.endsWith("<h2>Server Environment</h2>\n"));
}
@Test
public void initialServerInformationMustNotleadToException() throws Exception {
Map<String, String> env = Collections.emptyMap();
DebugTabServer serverTab = new DebugTabServer(env);
assertEquals("null", createJson(serverTab));
String html = createHtml(serverTab);
assertTrue(html.startsWith("<h2>Library Version</h2>"));
assertTrue(html.endsWith("<h2>Server Environment</h2>\n"));
}
@Test
public void twoParametersNoNull() throws Exception {
Map<String, String> env = new LinkedHashMap<String, String>();
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("<tr><td class=\"name\">key1</td><td class=\"value\">value1</td></tr>"));
assertTrue(html.contains("<tr><td class=\"name\">key2</td><td class=\"value\">value2</td></tr>"));
assertTrue(html.endsWith("</table>\n"));
}
@Test
public void twoParametersWithNull() throws Exception {
Map<String, String> env = new LinkedHashMap<String, String>();
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("<tr><td class=\"name\">key1</td><td class=\"value\">null</td></tr>"));
assertTrue(html.contains("<tr><td class=\"name\">key2</td><td class=\"value\">null</td></tr>"));
assertTrue(html.endsWith("</table>\n"));
}
}

View File

@ -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");
}
}
}