2 * Copyright (c) 2016 Cisco Systems, Inc. and others. 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.test;
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;
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.data.api.YangInstanceIdentifier;
42 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
44 public abstract class AbstractBodyReaderTest {
45 protected final MediaType mediaType;
46 protected final DatabindProvider databindProvider;
47 protected final DOMMountPointService mountPointService;
49 protected AbstractBodyReaderTest(final EffectiveModelContext schemaContext) throws NoSuchFieldException,
50 IllegalAccessException {
51 mediaType = getMediaType();
53 final var databindContext = DatabindContext.ofModel(schemaContext);
54 databindProvider = () -> databindContext;
56 mountPointService = mock(DOMMountPointService.class);
57 final var mountPoint = mock(DOMMountPoint.class);
58 doReturn(Optional.of(mountPoint)).when(mountPointService).getMountPoint(any(YangInstanceIdentifier.class));
59 doReturn(Optional.of(FixedDOMSchemaService.of(schemaContext))).when(mountPoint)
60 .getService(DOMSchemaService.class);
63 protected abstract MediaType getMediaType();
65 protected static EffectiveModelContext schemaContextLoader(final String yangPath,
66 final EffectiveModelContext schemaContext) {
67 return TestRestconfUtils.loadSchemaContext(yangPath, schemaContext);
70 protected static <T extends AbstractIdentifierAwareJaxRsProvider<?>> void mockBodyReader(
71 final String identifier, final T normalizedNodeProvider, final boolean isPost) {
72 final UriInfo uriInfoMock = mock(UriInfo.class);
73 final MultivaluedMap<String, String> pathParm = new MultivaluedHashMap<>(1);
75 if (!identifier.isEmpty()) {
76 pathParm.put("identifier", List.of(identifier));
79 when(uriInfoMock.getPathParameters()).thenReturn(pathParm);
80 when(uriInfoMock.getPathParameters(false)).thenReturn(pathParm);
81 when(uriInfoMock.getPathParameters(true)).thenReturn(pathParm);
82 normalizedNodeProvider.setUriInfo(uriInfoMock);
84 final Request request = mock(Request.class);
86 when(request.getMethod()).thenReturn("POST");
88 when(request.getMethod()).thenReturn("PUT");
91 normalizedNodeProvider.setRequest(request);
94 protected static void checkMountPointNormalizedNodePayload(final NormalizedNodePayload nnContext) {
95 checkNormalizedNodePayload(nnContext);
96 assertNotNull(nnContext.getInstanceIdentifierContext().getMountPoint());
99 protected static void checkNormalizedNodePayload(final NormalizedNodePayload nnContext) {
100 assertNotNull(nnContext.getData());
101 assertNotNull(nnContext.getInstanceIdentifierContext()
102 .getInstanceIdentifier());
103 assertNotNull(nnContext.getInstanceIdentifierContext()
104 .getSchemaContext());
105 assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaNode());
108 protected static void checkPatchContext(final PatchContext patchContext) {
109 assertNotNull(patchContext.getData());
110 assertNotNull(patchContext.getInstanceIdentifierContext().getInstanceIdentifier());
111 assertNotNull(patchContext.getInstanceIdentifierContext().getSchemaContext());
112 assertNotNull(patchContext.getInstanceIdentifierContext().getSchemaNode());
115 protected static void checkPatchContextMountPoint(final PatchContext patchContext) {
116 checkPatchContext(patchContext);
117 assertNotNull(patchContext.getInstanceIdentifierContext().getMountPoint());
120 protected static EffectiveModelContext modelContext(final DOMMountPoint mountPoint) {
121 return mountPoint.getService(DOMSchemaService.class)
122 .flatMap(svc -> Optional.ofNullable(svc.getGlobalContext()))
126 protected static void assertRangeViolation(final ThrowingRunnable runnable) {
127 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class, runnable);
128 assertEquals(Status.BAD_REQUEST, ex.getResponse().getStatusInfo());
130 final List<RestconfError> errors = ex.getErrors();
131 assertEquals(1, errors.size());
133 final RestconfError error = errors.get(0);
134 assertEquals(ErrorType.APPLICATION, error.getErrorType());
135 assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
136 assertEquals("bar error app tag", error.getErrorAppTag());
137 assertEquals("bar error message", error.getErrorMessage());