a41f35270d6eacad3b535484059a4c7b95132434
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / restconf / jersey / providers / AbstractBodyReaderTest.java
1 /*
2  * Copyright (c) 2016 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.restconf.jersey.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.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.netconf.sal.rest.api.RestconfConstants;
24 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
25 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
26 import org.opendaylight.netconf.sal.restconf.impl.PATCHContext;
27 import org.opendaylight.restconf.RestConnectorProvider;
28 import org.opendaylight.restconf.handlers.DOMMountPointServiceHandler;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30
31 abstract class AbstractBodyReaderTest {
32
33     protected final static ControllerContext controllerContext = ControllerContext.getInstance();
34     protected final MediaType mediaType;
35     private static Field uriField;
36     private static Field requestField;
37     protected final static DOMMountPointServiceHandler mountPointServiceHandler = mock(
38             DOMMountPointServiceHandler.class);
39
40     AbstractBodyReaderTest() throws NoSuchFieldException, IllegalAccessException {
41         uriField = AbstractIdentifierAwareJaxRsProvider.class
42                 .getDeclaredField("uriInfo");
43         uriField.setAccessible(true);
44
45         requestField = AbstractIdentifierAwareJaxRsProvider.class
46                 .getDeclaredField("request");
47         requestField.setAccessible(true);
48
49         mediaType = getMediaType();
50
51         final Field mountPointServiceHandlerField = RestConnectorProvider.class.
52                 getDeclaredField("mountPointServiceHandler");
53         mountPointServiceHandlerField.setAccessible(true);
54         mountPointServiceHandlerField.set(RestConnectorProvider.class, mountPointServiceHandler);
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,
66             final boolean isPost) throws NoSuchFieldException,
67             SecurityException, IllegalArgumentException, IllegalAccessException {
68         final UriInfo uriInfoMock = mock(UriInfo.class);
69         final MultivaluedMap<String, String> pathParm = new MultivaluedHashMap<>(1);
70
71         if (!identifier.isEmpty()) {
72             pathParm.put(RestconfConstants.IDENTIFIER, Collections.singletonList(identifier));
73         }
74
75         when(uriInfoMock.getPathParameters()).thenReturn(pathParm);
76         when(uriInfoMock.getPathParameters(false)).thenReturn(pathParm);
77         when(uriInfoMock.getPathParameters(true)).thenReturn(pathParm);
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 checkNormalizedNodeContext(
91             final NormalizedNodeContext nnContext) {
92         assertNotNull(nnContext.getData());
93         assertNotNull(nnContext.getInstanceIdentifierContext()
94                 .getInstanceIdentifier());
95         assertNotNull(nnContext.getInstanceIdentifierContext()
96                 .getSchemaContext());
97         assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaNode());
98     }
99
100     protected static void checkPATCHContext(final PATCHContext patchContext) {
101         assertNotNull(patchContext.getData());
102         assertNotNull(patchContext.getInstanceIdentifierContext().getInstanceIdentifier());
103         assertNotNull(patchContext.getInstanceIdentifierContext().getSchemaContext());
104         assertNotNull(patchContext.getInstanceIdentifierContext().getSchemaNode());
105     }
106
107     protected static void checkPATCHContextMountPoint(final PATCHContext patchContext) {
108         checkPATCHContext(patchContext);
109         assertNotNull(patchContext.getInstanceIdentifierContext().getMountPoint());
110         assertNotNull(patchContext.getInstanceIdentifierContext().getMountPoint().getSchemaContext());
111     }
112 }