2 * Copyright (c) 2015 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.controller.sal.rest.impl.test.providers;
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertNull;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
15 import java.lang.reflect.Field;
17 import java.util.List;
18 import java.util.Optional;
19 import javax.ws.rs.core.MediaType;
20 import javax.ws.rs.core.MultivaluedHashMap;
21 import javax.ws.rs.core.MultivaluedMap;
22 import javax.ws.rs.core.Request;
23 import javax.ws.rs.core.UriInfo;
24 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
25 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
26 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
27 import org.opendaylight.netconf.sal.rest.api.RestconfConstants;
28 import org.opendaylight.netconf.sal.rest.impl.AbstractIdentifierAwareJaxRsProvider;
29 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext;
30 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
31 import org.opendaylight.restconf.common.patch.PatchContext;
32 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
34 public abstract class AbstractBodyReaderTest {
35 private static Field uriField;
36 private static Field requestField;
40 uriField = AbstractIdentifierAwareJaxRsProvider.class.getDeclaredField("uriInfo");
41 uriField.setAccessible(true);
42 requestField = AbstractIdentifierAwareJaxRsProvider.class.getDeclaredField("request");
43 requestField.setAccessible(true);
44 } catch (NoSuchFieldException e) {
45 throw new RuntimeException(e);
49 protected final ControllerContext controllerContext;
50 protected final MediaType mediaType;
52 protected AbstractBodyReaderTest(final EffectiveModelContext schemaContext, final DOMMountPoint mountInstance) {
53 mediaType = getMediaType();
55 controllerContext = TestRestconfUtils.newControllerContext(schemaContext, mountInstance);
58 protected abstract MediaType getMediaType();
60 protected static EffectiveModelContext schemaContextLoader(final String yangPath,
61 final EffectiveModelContext schemaContext) {
62 return TestRestconfUtils.loadSchemaContext(yangPath, schemaContext);
65 protected static <T extends AbstractIdentifierAwareJaxRsProvider> void mockBodyReader(
66 final String identifier, final T normalizedNodeProvider, final boolean isPost) throws Exception {
67 final UriInfo uriInfoMock = mock(UriInfo.class);
68 final MultivaluedMap<String, String> pathParm = new MultivaluedHashMap<>(1);
70 if (!identifier.isEmpty()) {
71 pathParm.put(RestconfConstants.IDENTIFIER, List.of(identifier));
74 when(uriInfoMock.getPathParameters()).thenReturn(pathParm);
75 when(uriInfoMock.getPathParameters(false)).thenReturn(pathParm);
76 when(uriInfoMock.getPathParameters(true)).thenReturn(pathParm);
77 when(uriInfoMock.getAbsolutePath()).thenReturn(URI.create("restconf"));
78 uriField.set(normalizedNodeProvider, uriInfoMock);
80 final Request request = mock(Request.class);
82 when(request.getMethod()).thenReturn("POST");
84 when(request.getMethod()).thenReturn("PUT");
87 requestField.set(normalizedNodeProvider, request);
90 protected static void checkMountPointNormalizedNodeContext(final NormalizedNodeContext nnContext) {
91 checkNormalizedNodeContext(nnContext);
92 assertNotNull(nnContext.getInstanceIdentifierContext().getMountPoint());
95 protected static void checkNormalizedNodeContext(final NormalizedNodeContext nnContext) {
96 assertNotNull(nnContext.getData());
97 assertNotNull(nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
98 assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaContext());
99 assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaNode());
102 protected static void checkNormalizedNodeContextRpc(final NormalizedNodeContext nnContext) {
103 assertNotNull(nnContext.getData());
104 assertNull(nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
105 assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaContext());
106 assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaNode());
109 protected static void checkPatchContext(final PatchContext patchContext) {
110 assertNotNull(patchContext.getData());
111 assertNotNull(patchContext.getInstanceIdentifierContext().getInstanceIdentifier());
112 assertNotNull(patchContext.getInstanceIdentifierContext().getSchemaContext());
113 assertNotNull(patchContext.getInstanceIdentifierContext().getSchemaNode());
116 protected static void checkPatchContextMountPoint(final PatchContext patchContext) {
117 checkPatchContext(patchContext);
118 assertNotNull(patchContext.getInstanceIdentifierContext().getMountPoint());
121 protected static EffectiveModelContext modelContext(final DOMMountPoint mountPoint) {
122 return mountPoint.getService(DOMSchemaService.class)
123 .flatMap(svc -> Optional.ofNullable(svc.getGlobalContext()))