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