876dbfa0029176906832e60458c28f76d4d17e30
[netconf.git] / restconf / restconf-nb / 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.errors.RestconfDocumentedException;
32 import org.opendaylight.restconf.common.errors.RestconfError;
33 import org.opendaylight.restconf.common.patch.PatchContext;
34 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
35 import org.opendaylight.restconf.nb.rfc8040.databind.DatabindContext;
36 import org.opendaylight.restconf.nb.rfc8040.databind.DatabindProvider;
37 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.spi.AbstractIdentifierAwareJaxRsProvider;
38 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
39 import org.opendaylight.yangtools.yang.common.ErrorTag;
40 import org.opendaylight.yangtools.yang.common.ErrorType;
41 import org.opendaylight.yangtools.yang.common.QName;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
43 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
44
45 public abstract class AbstractBodyReaderTest {
46     protected static final QName MAP_CONT_QNAME = QName.create("map:ns", "my-map").intern();
47     protected static final QName KEY_LEAF_QNAME = QName.create("map:ns", "key-leaf").intern();
48     protected static final QName DATA_LEAF_QNAME = QName.create("map:ns", "data-leaf").intern();
49     protected static final QName LEAF_SET_QNAME = QName.create("set:ns", "my-set").intern();
50     protected static final QName LIST_QNAME = QName.create("list:ns", "unkeyed-list").intern();
51     protected static final QName LIST_LEAF1_QNAME = QName.create("list:ns", "leaf1").intern();
52     protected static final QName LIST_LEAF2_QNAME = QName.create("list:ns", "leaf2").intern();
53     protected static final QName CHOICE_CONT_QNAME = QName.create("choice:ns", "case-cont1").intern();
54     protected static final QName CASE_LEAF1_QNAME = QName.create("choice:ns", "case-leaf1").intern();
55
56     protected final MediaType mediaType;
57     protected final DatabindProvider databindProvider;
58     protected final DOMMountPointService mountPointService;
59
60     protected AbstractBodyReaderTest(final EffectiveModelContext schemaContext) throws NoSuchFieldException,
61             IllegalAccessException {
62         mediaType = getMediaType();
63
64         final var databindContext = DatabindContext.ofModel(schemaContext);
65         databindProvider = () -> databindContext;
66
67         mountPointService = mock(DOMMountPointService.class);
68         final var mountPoint = mock(DOMMountPoint.class);
69         doReturn(Optional.of(mountPoint)).when(mountPointService).getMountPoint(any(YangInstanceIdentifier.class));
70         doReturn(Optional.of(FixedDOMSchemaService.of(schemaContext))).when(mountPoint)
71             .getService(DOMSchemaService.class);
72     }
73
74     protected abstract MediaType getMediaType();
75
76     protected static EffectiveModelContext schemaContextLoader(final String yangPath,
77             final EffectiveModelContext schemaContext) {
78         return TestRestconfUtils.loadSchemaContext(yangPath, schemaContext);
79     }
80
81     protected static <T extends AbstractIdentifierAwareJaxRsProvider<?>> void mockBodyReader(
82             final String identifier, final T normalizedNodeProvider, final boolean isPost) {
83         final UriInfo uriInfoMock = mock(UriInfo.class);
84         final MultivaluedMap<String, String> pathParm = new MultivaluedHashMap<>(1);
85
86         if (!identifier.isEmpty()) {
87             pathParm.put("identifier", List.of(identifier));
88         }
89
90         when(uriInfoMock.getPathParameters()).thenReturn(pathParm);
91         when(uriInfoMock.getPathParameters(false)).thenReturn(pathParm);
92         when(uriInfoMock.getPathParameters(true)).thenReturn(pathParm);
93         normalizedNodeProvider.setUriInfo(uriInfoMock);
94
95         final Request request = mock(Request.class);
96         if (isPost) {
97             when(request.getMethod()).thenReturn("POST");
98         } else {
99             when(request.getMethod()).thenReturn("PUT");
100         }
101
102         normalizedNodeProvider.setRequest(request);
103     }
104
105     protected static void checkMountPointNormalizedNodePayload(final NormalizedNodePayload nnContext) {
106         checkNormalizedNodePayload(nnContext);
107         assertNotNull(nnContext.getInstanceIdentifierContext().getMountPoint());
108     }
109
110     protected static void checkNormalizedNodePayload(final NormalizedNodePayload nnContext) {
111         assertNotNull(nnContext.getData());
112         assertNotNull(nnContext.getInstanceIdentifierContext()
113                 .getInstanceIdentifier());
114         assertNotNull(nnContext.getInstanceIdentifierContext()
115                 .getSchemaContext());
116         assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaNode());
117     }
118
119     protected static void checkPatchContext(final PatchContext patchContext) {
120         assertNotNull(patchContext.getData());
121         assertNotNull(patchContext.getInstanceIdentifierContext().getInstanceIdentifier());
122         assertNotNull(patchContext.getInstanceIdentifierContext().getSchemaContext());
123         assertNotNull(patchContext.getInstanceIdentifierContext().getSchemaNode());
124     }
125
126     protected static void checkPatchContextMountPoint(final PatchContext patchContext) {
127         checkPatchContext(patchContext);
128         assertNotNull(patchContext.getInstanceIdentifierContext().getMountPoint());
129     }
130
131     protected static EffectiveModelContext modelContext(final DOMMountPoint mountPoint) {
132         return mountPoint.getService(DOMSchemaService.class)
133             .flatMap(svc -> Optional.ofNullable(svc.getGlobalContext()))
134             .orElse(null);
135     }
136
137     protected static void assertRangeViolation(final ThrowingRunnable runnable) {
138         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class, runnable);
139         assertEquals(Status.BAD_REQUEST, ex.getResponse().getStatusInfo());
140
141         final List<RestconfError> errors = ex.getErrors();
142         assertEquals(1, errors.size());
143
144         final RestconfError error = errors.get(0);
145         assertEquals(ErrorType.APPLICATION, error.getErrorType());
146         assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
147         assertEquals("bar error app tag", error.getErrorAppTag());
148         assertEquals("bar error message", error.getErrorMessage());
149     }
150 }