Merge changes from topic 'rem_web_xml'
[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
9 package org.opendaylight.controller.sal.rest.impl.test.providers;
10
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
14
15 import java.lang.reflect.Field;
16 import java.net.URI;
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;
31
32 public abstract class AbstractBodyReaderTest {
33     private static Field uriField;
34     private static Field requestField;
35
36     static {
37         try {
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);
46         }
47     }
48
49     protected final ControllerContext controllerContext;
50     protected final MediaType mediaType;
51
52     protected AbstractBodyReaderTest(SchemaContext schemaContext, DOMMountPoint mountInstance) {
53         this.mediaType = getMediaType();
54
55         controllerContext = TestRestconfUtils.newControllerContext(schemaContext, mountInstance);
56     }
57
58     protected abstract MediaType getMediaType();
59
60     protected static SchemaContext schemaContextLoader(final String yangPath,
61             final SchemaContext schemaContext) {
62         return TestRestconfUtils.loadSchemaContext(yangPath, schemaContext);
63     }
64
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);
69
70         if (!identifier.isEmpty()) {
71             pathParm.put(RestconfConstants.IDENTIFIER, Collections.singletonList(identifier));
72         }
73
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);
79
80         final Request request = mock(Request.class);
81         if (isPost) {
82             when(request.getMethod()).thenReturn("POST");
83         } else {
84             when(request.getMethod()).thenReturn("PUT");
85         }
86
87         requestField.set(normalizedNodeProvider, request);
88     }
89
90     protected static void checkMountPointNormalizedNodeContext(
91             final NormalizedNodeContext nnContext) {
92         checkNormalizedNodeContext(nnContext);
93         assertNotNull(nnContext.getInstanceIdentifierContext().getMountPoint());
94     }
95
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());
104     }
105
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());
111     }
112
113     protected static void checkPatchContextMountPoint(final PatchContext patchContext) {
114         checkPatchContext(patchContext);
115         assertNotNull(patchContext.getInstanceIdentifierContext().getMountPoint());
116         assertNotNull(patchContext.getInstanceIdentifierContext().getMountPoint().getSchemaContext());
117     }
118 }