Merge "Migrate restconf to MD-SAL APIs"
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / controller / sal / rest / impl / test / providers / AbstractBodyReaderTest.java
1 /*
2  * Copyright (c) 2015 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.controller.sal.rest.impl.test.providers;
9
10 import static org.junit.Assert.assertNotNull;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.when;
13
14 import java.lang.reflect.Field;
15 import java.net.URI;
16 import java.util.Collections;
17 import javax.ws.rs.core.MediaType;
18 import javax.ws.rs.core.MultivaluedHashMap;
19 import javax.ws.rs.core.MultivaluedMap;
20 import javax.ws.rs.core.Request;
21 import javax.ws.rs.core.UriInfo;
22 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
23 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
24 import org.opendaylight.netconf.sal.rest.api.RestconfConstants;
25 import org.opendaylight.netconf.sal.rest.impl.AbstractIdentifierAwareJaxRsProvider;
26 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
27 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
28 import org.opendaylight.restconf.common.patch.PatchContext;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30
31 public abstract class AbstractBodyReaderTest {
32     private static Field uriField;
33     private static Field requestField;
34
35     static {
36         try {
37             uriField = AbstractIdentifierAwareJaxRsProvider.class
38                     .getDeclaredField("uriInfo");
39             uriField.setAccessible(true);
40             requestField = AbstractIdentifierAwareJaxRsProvider.class
41                     .getDeclaredField("request");
42             requestField.setAccessible(true);
43         } catch (NoSuchFieldException e) {
44             throw new RuntimeException(e);
45         }
46     }
47
48     protected final ControllerContext controllerContext;
49     protected final MediaType mediaType;
50
51     protected AbstractBodyReaderTest(final SchemaContext schemaContext, final DOMMountPoint mountInstance) {
52         this.mediaType = getMediaType();
53
54         controllerContext = TestRestconfUtils.newControllerContext(schemaContext, mountInstance);
55     }
56
57     protected abstract MediaType getMediaType();
58
59     protected static SchemaContext schemaContextLoader(final String yangPath,
60             final SchemaContext schemaContext) {
61         return TestRestconfUtils.loadSchemaContext(yangPath, schemaContext);
62     }
63
64     protected static <T extends AbstractIdentifierAwareJaxRsProvider> void mockBodyReader(
65             final String identifier, final T normalizedNodeProvider, final boolean isPost) throws Exception {
66         final UriInfo uriInfoMock = mock(UriInfo.class);
67         final MultivaluedMap<String, String> pathParm = new MultivaluedHashMap<>(1);
68
69         if (!identifier.isEmpty()) {
70             pathParm.put(RestconfConstants.IDENTIFIER, Collections.singletonList(identifier));
71         }
72
73         when(uriInfoMock.getPathParameters()).thenReturn(pathParm);
74         when(uriInfoMock.getPathParameters(false)).thenReturn(pathParm);
75         when(uriInfoMock.getPathParameters(true)).thenReturn(pathParm);
76         when(uriInfoMock.getAbsolutePath()).thenReturn(new URI("restconf"));
77         uriField.set(normalizedNodeProvider, uriInfoMock);
78
79         final Request request = mock(Request.class);
80         if (isPost) {
81             when(request.getMethod()).thenReturn("POST");
82         } else {
83             when(request.getMethod()).thenReturn("PUT");
84         }
85
86         requestField.set(normalizedNodeProvider, request);
87     }
88
89     protected static void checkMountPointNormalizedNodeContext(
90             final NormalizedNodeContext nnContext) {
91         checkNormalizedNodeContext(nnContext);
92         assertNotNull(nnContext.getInstanceIdentifierContext().getMountPoint());
93     }
94
95     protected static void checkNormalizedNodeContext(
96             final NormalizedNodeContext nnContext) {
97         assertNotNull(nnContext.getData());
98         assertNotNull(nnContext.getInstanceIdentifierContext()
99                 .getInstanceIdentifier());
100         assertNotNull(nnContext.getInstanceIdentifierContext()
101                 .getSchemaContext());
102         assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaNode());
103     }
104
105     protected static void checkPatchContext(final PatchContext patchContext) {
106         assertNotNull(patchContext.getData());
107         assertNotNull(patchContext.getInstanceIdentifierContext().getInstanceIdentifier());
108         assertNotNull(patchContext.getInstanceIdentifierContext().getSchemaContext());
109         assertNotNull(patchContext.getInstanceIdentifierContext().getSchemaNode());
110     }
111
112     protected static void checkPatchContextMountPoint(final PatchContext patchContext) {
113         checkPatchContext(patchContext);
114         assertNotNull(patchContext.getInstanceIdentifierContext().getMountPoint());
115         assertNotNull(patchContext.getInstanceIdentifierContext().getMountPoint().getSchemaContext());
116     }
117 }