2 * Copyright © 2019 FRINX s.r.o. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.errors;
10 import static org.mockito.Mockito.doReturn;
11 import static org.mockito.Mockito.mock;
13 import com.google.common.collect.Lists;
15 import java.util.Arrays;
16 import java.util.Collections;
17 import java.util.List;
18 import javax.ws.rs.core.HttpHeaders;
19 import javax.ws.rs.core.MediaType;
20 import javax.ws.rs.core.Response;
21 import javax.ws.rs.core.Response.Status;
22 import org.json.JSONObject;
24 import org.junit.Assert;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.junit.runners.Parameterized;
29 import org.junit.runners.Parameterized.Parameter;
30 import org.junit.runners.Parameterized.Parameters;
31 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
32 import org.opendaylight.restconf.common.errors.RestconfError;
33 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
34 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
35 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
36 import org.opendaylight.yangtools.yang.common.QName;
37 import org.opendaylight.yangtools.yang.common.QNameModule;
38 import org.opendaylight.yangtools.yang.common.Revision;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
42 import org.skyscreamer.jsonassert.JSONAssert;
44 @RunWith(Parameterized.class)
45 public class RestconfDocumentedExceptionMapperTest {
47 private static final String EMPTY_XML = "<errors xmlns=\"urn:ietf:params:xml:ns:yang:ietf-restconf\"></errors>";
48 private static final String EMPTY_JSON = "{}";
49 private static QNameModule MONITORING_MODULE_INFO = QNameModule.create(
50 URI.create("instance:identifier:patch:module"), Revision.of("2015-11-21"));
52 private static RestconfDocumentedExceptionMapper exceptionMapper;
55 public static void setupExceptionMapper() {
56 final SchemaContext schemaContext = YangParserTestUtils.parseYangResources(
57 RestconfDocumentedExceptionMapperTest.class, "/restconf/impl/ietf-restconf@2017-01-26.yang",
58 "/instanceidentifier/yang/instance-identifier-patch-module.yang");
59 final SchemaContextHandler schemaContextHandler = mock(SchemaContextHandler.class);
60 doReturn(schemaContext).when(schemaContextHandler).get();
62 exceptionMapper = new RestconfDocumentedExceptionMapper(schemaContextHandler);
66 * Testing entries 0 - 6: testing of media types and empty responses.
67 * Testing entries 7 - 8: testing of deriving of status codes from error entries.
68 * Testing entries 9 - 10: testing of serialization of different optional fields of error entries (JSON/XML).
70 * @return Testing data for parametrized test.
72 @Parameters(name = "{index}: {0}: {1}")
73 public static Iterable<Object[]> data() {
74 final RestconfDocumentedException sampleComplexError
75 = new RestconfDocumentedException("general message", new IllegalStateException("cause"),
77 new RestconfError(ErrorType.APPLICATION, ErrorTag.BAD_ATTRIBUTE,
78 "message 1", "app tag #1"),
79 new RestconfError(ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED,
80 "message 2", "app tag #2", "my info"),
81 new RestconfError(ErrorType.RPC, ErrorTag.DATA_MISSING,
82 "message 3", " app tag #3", "my error info", YangInstanceIdentifier.builder()
83 .node(QName.create(MONITORING_MODULE_INFO, "patch-cont"))
84 .node(QName.create(MONITORING_MODULE_INFO, "my-list1"))
85 .nodeWithKey(QName.create(MONITORING_MODULE_INFO, "my-list1"),
86 QName.create(MONITORING_MODULE_INFO, "name"), "sample")
87 .node(QName.create(MONITORING_MODULE_INFO, "my-leaf12"))
90 return Arrays.asList(new Object[][]{
92 "Mapping of the exception without any errors and XML output derived from content type",
93 new RestconfDocumentedException(Status.BAD_REQUEST),
94 mockHttpHeaders(MediaType.APPLICATION_XML_TYPE, Collections.emptyList()),
95 Response.status(Status.BAD_REQUEST)
96 .type(RestconfDocumentedExceptionMapper.YANG_DATA_XML_TYPE)
101 "Mapping of the exception without any errors and JSON output derived from unsupported content type",
102 new RestconfDocumentedException(Status.INTERNAL_SERVER_ERROR),
103 mockHttpHeaders(MediaType.APPLICATION_FORM_URLENCODED_TYPE, Collections.emptyList()),
104 Response.status(Status.INTERNAL_SERVER_ERROR)
105 .type(RestconfDocumentedExceptionMapper.YANG_DATA_JSON_TYPE)
110 "Mapping of the exception without any errors and JSON output derived from missing content type "
111 + "and accepted media types",
112 new RestconfDocumentedException(Status.NOT_IMPLEMENTED),
113 mockHttpHeaders(null, Collections.emptyList()),
114 Response.status(Status.NOT_IMPLEMENTED)
115 .type(RestconfDocumentedExceptionMapper.YANG_DATA_JSON_TYPE)
120 "Mapping of the exception without any errors and JSON output derived from expected types - both JSON"
121 + "and XML types are accepted, but server should prefer JSON format",
122 new RestconfDocumentedException(Status.INTERNAL_SERVER_ERROR),
123 mockHttpHeaders(MediaType.APPLICATION_JSON_TYPE, Lists.newArrayList(
124 MediaType.APPLICATION_FORM_URLENCODED_TYPE, MediaType.APPLICATION_XML_TYPE,
125 MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_OCTET_STREAM_TYPE)),
126 Response.status(Status.INTERNAL_SERVER_ERROR)
127 .type(RestconfDocumentedExceptionMapper.YANG_DATA_JSON_TYPE)
132 "Mapping of the exception without any errors and JSON output derived from expected types - there"
133 + "is only a wildcard type that should be mapped to default type",
134 new RestconfDocumentedException(Status.NOT_FOUND),
135 mockHttpHeaders(null, Lists.newArrayList(MediaType.WILDCARD_TYPE)),
136 Response.status(Status.NOT_FOUND)
137 .type(RestconfDocumentedExceptionMapper.YANG_DATA_JSON_TYPE)
142 "Mapping of the exception without any errors and XML output derived from expected types - "
143 + "we should choose the most specific and supported type",
144 new RestconfDocumentedException(Status.NOT_FOUND),
145 mockHttpHeaders(null, Lists.newArrayList(MediaType.valueOf("*/yang-data+json"),
146 MediaType.valueOf("application/yang-data+xml"), MediaType.WILDCARD_TYPE)),
147 Response.status(Status.NOT_FOUND)
148 .type(RestconfDocumentedExceptionMapper.YANG_DATA_XML_TYPE)
153 "Mapping of the exception without any errors and XML output derived from expected types - "
154 + "we should choose the most specific and supported type",
155 new RestconfDocumentedException(Status.NOT_FOUND),
156 mockHttpHeaders(null, Lists.newArrayList(MediaType.valueOf("*/unsupported"),
157 MediaType.valueOf("application/*"), MediaType.WILDCARD_TYPE)),
158 Response.status(Status.NOT_FOUND)
159 .type(RestconfDocumentedExceptionMapper.YANG_DATA_JSON_TYPE)
164 "Mapping of the exception with one error entry but null status code. This status code should"
165 + "be derived from single error entry; JSON output",
166 new RestconfDocumentedException("Sample error message"),
167 mockHttpHeaders(MediaType.APPLICATION_JSON_TYPE, Collections.singletonList(
168 RestconfDocumentedExceptionMapper.YANG_PATCH_JSON_TYPE)),
169 Response.status(Status.INTERNAL_SERVER_ERROR)
170 .type(RestconfDocumentedExceptionMapper.YANG_DATA_JSON_TYPE)
175 + " \"error-message\": \"Sample error message\",\n"
176 + " \"error-tag\": \"operation-failed\",\n"
177 + " \"error-type\": \"application\"\n"
185 "Mapping of the exception with two error entries but null status code. This status code should"
186 + "be derived from the first error entry that is specified; XML output",
187 new RestconfDocumentedException("general message", new IllegalStateException("cause"),
189 new RestconfError(ErrorType.APPLICATION, ErrorTag.BAD_ATTRIBUTE, "message 1"),
190 new RestconfError(ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, "message 2"))),
191 mockHttpHeaders(MediaType.APPLICATION_JSON_TYPE, Collections.singletonList(
192 RestconfDocumentedExceptionMapper.YANG_PATCH_XML_TYPE)),
193 Response.status(Status.BAD_REQUEST)
194 .type(RestconfDocumentedExceptionMapper.YANG_DATA_XML_TYPE)
195 .entity("<errors xmlns=\"urn:ietf:params:xml:ns:yang:ietf-restconf\">\n"
197 + "<error-message>message 1</error-message>\n"
198 + "<error-tag>bad-attribute</error-tag>\n"
199 + "<error-type>application</error-type>\n"
202 + "<error-message>message 2</error-message>\n"
203 + "<error-tag>operation-failed</error-tag>\n"
204 + "<error-type>application</error-type>\n"
210 "Mapping of the exception with three entries and optional entries set: error app tag (the first error),"
211 + " error info (the second error), and error path (the last error); JSON output",
212 sampleComplexError, mockHttpHeaders(MediaType.APPLICATION_JSON_TYPE, Collections.singletonList(
213 MediaType.APPLICATION_JSON_TYPE)),
214 Response.status(Status.BAD_REQUEST)
215 .type(RestconfDocumentedExceptionMapper.YANG_DATA_JSON_TYPE)
220 + " \"error-type\": \"application\",\n"
221 + " \"error-message\": \"message 1\",\n"
222 + " \"error-tag\": \"bad-attribute\",\n"
223 + " \"error-app-tag\": \"app tag #1\"\n"
226 + " \"error-type\": \"application\",\n"
227 + " \"error-message\": \"message 2\",\n"
228 + " \"error-tag\": \"operation-failed\",\n"
229 + " \"error-app-tag\": \"app tag #2\",\n"
230 + " \"error-info\": \"my info\"\n"
233 + " \"error-type\": \"rpc\",\n"
234 + " \"error-path\": \"/instance-identifier-patch-module:patch-cont/"
235 + "my-list1[name='sample']/my-leaf12\",\n"
236 + " \"error-message\": \"message 3\",\n"
237 + " \"error-tag\": \"data-missing\",\n"
238 + " \"error-app-tag\": \" app tag #3\",\n"
239 + " \"error-info\": \"my error info\"\n"
247 "Mapping of the exception with three entries and optional entries set: error app tag (the first error),"
248 + " error info (the second error), and error path (the last error); XML output",
249 sampleComplexError, mockHttpHeaders(RestconfDocumentedExceptionMapper.YANG_PATCH_JSON_TYPE,
250 Collections.singletonList(RestconfDocumentedExceptionMapper.YANG_DATA_XML_TYPE)),
251 Response.status(Status.BAD_REQUEST)
252 .type(RestconfDocumentedExceptionMapper.YANG_DATA_XML_TYPE)
253 .entity("<errors xmlns=\"urn:ietf:params:xml:ns:yang:ietf-restconf\">\n"
255 + "<error-type>application</error-type>\n"
256 + "<error-message>message 1</error-message>\n"
257 + "<error-tag>bad-attribute</error-tag>\n"
258 + "<error-app-tag>app tag #1</error-app-tag>\n"
261 + "<error-type>application</error-type>\n"
262 + "<error-message>message 2</error-message>\n"
263 + "<error-tag>operation-failed</error-tag>\n"
264 + "<error-app-tag>app tag #2</error-app-tag>\n"
265 + "<error-info>my info</error-info></error>\n"
267 + "<error-type>rpc</error-type>\n"
268 + "<error-path xmlns:a=\"instance:identifier:patch:module\">/a:patch-cont/"
269 + "a:my-list1[a:name='sample']/a:my-leaf12</error-path>\n"
270 + "<error-message>message 3</error-message>\n"
271 + "<error-tag>data-missing</error-tag>\n"
272 + "<error-app-tag> app tag #3</error-app-tag>\n"
273 + "<error-info>my error info</error-info>\n"
282 public String testDescription;
284 public RestconfDocumentedException thrownException;
286 public HttpHeaders httpHeaders;
288 public Response expectedResponse;
291 public void testMappingOfExceptionToResponse() {
292 exceptionMapper.setHttpHeaders(httpHeaders);
293 final Response response = exceptionMapper.toResponse(thrownException);
294 compareResponseWithExpectation(expectedResponse, response);
297 private static HttpHeaders mockHttpHeaders(final MediaType contentType, final List<MediaType> acceptedTypes) {
298 final HttpHeaders httpHeaders = mock(HttpHeaders.class);
299 doReturn(contentType).when(httpHeaders).getMediaType();
300 doReturn(acceptedTypes).when(httpHeaders).getAcceptableMediaTypes();
304 private static void compareResponseWithExpectation(final Response expectedResponse, final Response actualResponse) {
305 final String errorMessage = String.format("Actual response %s doesn't equal to expected response %s",
306 actualResponse, expectedResponse);
307 Assert.assertEquals(errorMessage, expectedResponse.getStatus(), actualResponse.getStatus());
308 Assert.assertEquals(errorMessage, expectedResponse.getMediaType(), actualResponse.getMediaType());
309 if (RestconfDocumentedExceptionMapper.YANG_DATA_JSON_TYPE.equals(expectedResponse.getMediaType())) {
310 JSONAssert.assertEquals(expectedResponse.getEntity().toString(),
311 actualResponse.getEntity().toString(), true);
313 final JSONObject expectedResponseInJson = XML.toJSONObject(expectedResponse.getEntity().toString());
314 final JSONObject actualResponseInJson = XML.toJSONObject(actualResponse.getEntity().toString());
315 JSONAssert.assertEquals(expectedResponseInJson, actualResponseInJson, true);