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.mockito.Mockito.mock;
12 import static org.mockito.Mockito.when;
14 import java.lang.reflect.Field;
16 import java.util.Collections;
17 import java.util.Optional;
18 import javax.ws.rs.core.MediaType;
19 import javax.ws.rs.core.MultivaluedHashMap;
20 import javax.ws.rs.core.MultivaluedMap;
21 import javax.ws.rs.core.Request;
22 import javax.ws.rs.core.UriInfo;
23 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
24 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
25 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
26 import org.opendaylight.netconf.sal.rest.api.RestconfConstants;
27 import org.opendaylight.netconf.sal.rest.impl.AbstractIdentifierAwareJaxRsProvider;
28 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
29 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
30 import org.opendaylight.restconf.common.patch.PatchContext;
31 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
33 public abstract class AbstractBodyReaderTest {
34 private static Field uriField;
35 private static Field requestField;
39 uriField = AbstractIdentifierAwareJaxRsProvider.class
40 .getDeclaredField("uriInfo");
41 uriField.setAccessible(true);
42 requestField = AbstractIdentifierAwareJaxRsProvider.class
43 .getDeclaredField("request");
44 requestField.setAccessible(true);
45 } catch (NoSuchFieldException e) {
46 throw new RuntimeException(e);
50 protected final ControllerContext controllerContext;
51 protected final MediaType mediaType;
53 protected AbstractBodyReaderTest(final EffectiveModelContext schemaContext, final DOMMountPoint mountInstance) {
54 this.mediaType = getMediaType();
56 controllerContext = TestRestconfUtils.newControllerContext(schemaContext, mountInstance);
59 protected abstract MediaType getMediaType();
61 protected static EffectiveModelContext schemaContextLoader(final String yangPath,
62 final EffectiveModelContext schemaContext) {
63 return TestRestconfUtils.loadSchemaContext(yangPath, schemaContext);
66 protected static <T extends AbstractIdentifierAwareJaxRsProvider> void mockBodyReader(
67 final String identifier, final T normalizedNodeProvider, final boolean isPost) throws Exception {
68 final UriInfo uriInfoMock = mock(UriInfo.class);
69 final MultivaluedMap<String, String> pathParm = new MultivaluedHashMap<>(1);
71 if (!identifier.isEmpty()) {
72 pathParm.put(RestconfConstants.IDENTIFIER, Collections.singletonList(identifier));
75 when(uriInfoMock.getPathParameters()).thenReturn(pathParm);
76 when(uriInfoMock.getPathParameters(false)).thenReturn(pathParm);
77 when(uriInfoMock.getPathParameters(true)).thenReturn(pathParm);
78 when(uriInfoMock.getAbsolutePath()).thenReturn(new URI("restconf"));
79 uriField.set(normalizedNodeProvider, uriInfoMock);
81 final Request request = mock(Request.class);
83 when(request.getMethod()).thenReturn("POST");
85 when(request.getMethod()).thenReturn("PUT");
88 requestField.set(normalizedNodeProvider, request);
91 protected static void checkMountPointNormalizedNodeContext(
92 final NormalizedNodeContext nnContext) {
93 checkNormalizedNodeContext(nnContext);
94 assertNotNull(nnContext.getInstanceIdentifierContext().getMountPoint());
97 protected static void checkNormalizedNodeContext(
98 final NormalizedNodeContext nnContext) {
99 assertNotNull(nnContext.getData());
100 assertNotNull(nnContext.getInstanceIdentifierContext()
101 .getInstanceIdentifier());
102 assertNotNull(nnContext.getInstanceIdentifierContext()
103 .getSchemaContext());
104 assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaNode());
107 protected static void checkPatchContext(final PatchContext patchContext) {
108 assertNotNull(patchContext.getData());
109 assertNotNull(patchContext.getInstanceIdentifierContext().getInstanceIdentifier());
110 assertNotNull(patchContext.getInstanceIdentifierContext().getSchemaContext());
111 assertNotNull(patchContext.getInstanceIdentifierContext().getSchemaNode());
114 protected static void checkPatchContextMountPoint(final PatchContext patchContext) {
115 checkPatchContext(patchContext);
116 assertNotNull(patchContext.getInstanceIdentifierContext().getMountPoint());
119 protected static EffectiveModelContext modelContext(final DOMMountPoint mountPoint) {
120 return mountPoint.getService(DOMSchemaService.class)
121 .flatMap(svc -> Optional.ofNullable(svc.getGlobalContext()))