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
9 package org.opendaylight.controller.sal.rest.impl.test.providers;
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
15 import java.lang.reflect.Field;
17 import java.util.Collections;
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.dom.api.DOMMountPoint;
24 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
25 import org.opendaylight.netconf.sal.rest.api.RestconfConstants;
26 import org.opendaylight.netconf.sal.rest.impl.AbstractIdentifierAwareJaxRsProvider;
27 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
28 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
29 import org.opendaylight.restconf.common.patch.PatchContext;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 public abstract class AbstractBodyReaderTest {
33 private static Field uriField;
34 private static Field requestField;
38 uriField = AbstractIdentifierAwareJaxRsProvider.class
39 .getDeclaredField("uriInfo");
40 uriField.setAccessible(true);
41 requestField = AbstractIdentifierAwareJaxRsProvider.class
42 .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(SchemaContext schemaContext, DOMMountPoint mountInstance) {
53 this.mediaType = getMediaType();
55 controllerContext = TestRestconfUtils.newControllerContext(schemaContext, mountInstance);
58 protected abstract MediaType getMediaType();
60 protected static SchemaContext schemaContextLoader(final String yangPath,
61 final SchemaContext 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, Collections.singletonList(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(new URI("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(
91 final NormalizedNodeContext nnContext) {
92 checkNormalizedNodeContext(nnContext);
93 assertNotNull(nnContext.getInstanceIdentifierContext().getMountPoint());
96 protected static void checkNormalizedNodeContext(
97 final NormalizedNodeContext nnContext) {
98 assertNotNull(nnContext.getData());
99 assertNotNull(nnContext.getInstanceIdentifierContext()
100 .getInstanceIdentifier());
101 assertNotNull(nnContext.getInstanceIdentifierContext()
102 .getSchemaContext());
103 assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaNode());
106 protected static void checkPatchContext(final PatchContext patchContext) {
107 assertNotNull(patchContext.getData());
108 assertNotNull(patchContext.getInstanceIdentifierContext().getInstanceIdentifier());
109 assertNotNull(patchContext.getInstanceIdentifierContext().getSchemaContext());
110 assertNotNull(patchContext.getInstanceIdentifierContext().getSchemaNode());
113 protected static void checkPatchContextMountPoint(final PatchContext patchContext) {
114 checkPatchContext(patchContext);
115 assertNotNull(patchContext.getInstanceIdentifierContext().getMountPoint());
116 assertNotNull(patchContext.getInstanceIdentifierContext().getMountPoint().getSchemaContext());