Update RESTCONF error mapping
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / test / AbstractBodyReaderTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.test;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertThrows;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.when;
17
18 import java.util.Collections;
19 import java.util.List;
20 import java.util.Optional;
21 import javax.ws.rs.core.MediaType;
22 import javax.ws.rs.core.MultivaluedHashMap;
23 import javax.ws.rs.core.MultivaluedMap;
24 import javax.ws.rs.core.Request;
25 import javax.ws.rs.core.Response.Status;
26 import javax.ws.rs.core.UriInfo;
27 import org.junit.function.ThrowingRunnable;
28 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
29 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
30 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
31 import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
32 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
33 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
34 import org.opendaylight.restconf.common.errors.RestconfError;
35 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
36 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
37 import org.opendaylight.restconf.common.patch.PatchContext;
38 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
39 import org.opendaylight.restconf.nb.rfc8040.TestUtils;
40 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
41 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.spi.AbstractIdentifierAwareJaxRsProvider;
42 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
44 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
45
46 public abstract class AbstractBodyReaderTest {
47     protected final MediaType mediaType;
48     protected final SchemaContextHandler schemaContextHandler;
49     protected final DOMMountPointService mountPointService;
50
51     protected AbstractBodyReaderTest(final EffectiveModelContext schemaContext) throws NoSuchFieldException,
52             IllegalAccessException {
53         mediaType = getMediaType();
54
55         schemaContextHandler = TestUtils.newSchemaContextHandler(schemaContext);
56
57         mountPointService = mock(DOMMountPointService.class);
58         final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
59         doReturn(Optional.of(mountPoint)).when(mountPointService).getMountPoint(any(YangInstanceIdentifier.class));
60         doReturn(Optional.of(FixedDOMSchemaService.of(schemaContext))).when(mountPoint)
61             .getService(DOMSchemaService.class);
62     }
63
64     protected abstract MediaType getMediaType();
65
66     protected static EffectiveModelContext schemaContextLoader(final String yangPath,
67             final EffectiveModelContext schemaContext) {
68         return TestRestconfUtils.loadSchemaContext(yangPath, schemaContext);
69     }
70
71     protected static <T extends AbstractIdentifierAwareJaxRsProvider<?>> void mockBodyReader(
72             final String identifier, final T normalizedNodeProvider,
73             final boolean isPost) throws NoSuchFieldException,
74             SecurityException, IllegalArgumentException, IllegalAccessException {
75         final UriInfo uriInfoMock = mock(UriInfo.class);
76         final MultivaluedMap<String, String> pathParm = new MultivaluedHashMap<>(1);
77
78         if (!identifier.isEmpty()) {
79             pathParm.put(RestconfConstants.IDENTIFIER, Collections.singletonList(identifier));
80         }
81
82         when(uriInfoMock.getPathParameters()).thenReturn(pathParm);
83         when(uriInfoMock.getPathParameters(false)).thenReturn(pathParm);
84         when(uriInfoMock.getPathParameters(true)).thenReturn(pathParm);
85         normalizedNodeProvider.setUriInfo(uriInfoMock);
86
87         final Request request = mock(Request.class);
88         if (isPost) {
89             when(request.getMethod()).thenReturn("POST");
90         } else {
91             when(request.getMethod()).thenReturn("PUT");
92         }
93
94         normalizedNodeProvider.setRequest(request);
95     }
96
97     protected static void checkMountPointNormalizedNodeContext(
98             final NormalizedNodeContext nnContext) {
99         checkNormalizedNodeContext(nnContext);
100         assertNotNull(nnContext.getInstanceIdentifierContext().getMountPoint());
101     }
102
103     protected static void checkNormalizedNodeContext(
104             final NormalizedNodeContext nnContext) {
105         assertNotNull(nnContext.getData());
106         assertNotNull(nnContext.getInstanceIdentifierContext()
107                 .getInstanceIdentifier());
108         assertNotNull(nnContext.getInstanceIdentifierContext()
109                 .getSchemaContext());
110         assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaNode());
111     }
112
113     protected static void checkPatchContext(final PatchContext patchContext) {
114         assertNotNull(patchContext.getData());
115         assertNotNull(patchContext.getInstanceIdentifierContext().getInstanceIdentifier());
116         assertNotNull(patchContext.getInstanceIdentifierContext().getSchemaContext());
117         assertNotNull(patchContext.getInstanceIdentifierContext().getSchemaNode());
118     }
119
120     protected static void checkPatchContextMountPoint(final PatchContext patchContext) {
121         checkPatchContext(patchContext);
122         assertNotNull(patchContext.getInstanceIdentifierContext().getMountPoint());
123     }
124
125     protected static EffectiveModelContext modelContext(final DOMMountPoint mountPoint) {
126         return mountPoint.getService(DOMSchemaService.class)
127             .flatMap(svc -> Optional.ofNullable(svc.getGlobalContext()))
128             .orElse(null);
129     }
130
131     protected static void assertRangeViolation(final ThrowingRunnable runnable) {
132         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class, runnable);
133         assertEquals(Status.BAD_REQUEST, ex.getResponse().getStatusInfo());
134
135         final List<RestconfError> errors = ex.getErrors();
136         assertEquals(1, errors.size());
137
138         final RestconfError error = errors.get(0);
139         assertEquals(ErrorType.PROTOCOL, error.getErrorType());
140         assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
141         assertEquals("bar error app tag", error.getErrorAppTag());
142         assertEquals("bar error message", error.getErrorMessage());
143     }
144 }