f9a31955a59986ec533ce69ab18fb13bb1a7205c
[netconf.git] / opendaylight / restconf / sal-rest-connector / 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.util.Collections;
17
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
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.netconf.sal.restconf.impl.NormalizedNodeContext;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30
31 /**
32  * sal-rest-connector org.opendaylight.controller.sal.rest.impl.test.providers
33  *
34  *
35  *
36  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
37  *
38  *         Created: Mar 7, 2015
39  */
40 public abstract class AbstractBodyReaderTest {
41
42     protected final static ControllerContext controllerContext = ControllerContext
43             .getInstance();
44     protected final MediaType mediaType;
45     private static Field uriField;
46     private static Field requestField;
47
48     public AbstractBodyReaderTest() throws NoSuchFieldException,
49             SecurityException {
50         uriField = AbstractIdentifierAwareJaxRsProvider.class
51                 .getDeclaredField("uriInfo");
52         uriField.setAccessible(true);
53         requestField = AbstractIdentifierAwareJaxRsProvider.class
54                 .getDeclaredField("request");
55         requestField.setAccessible(true);
56         mediaType = getMediaType();
57     }
58
59     protected abstract MediaType getMediaType();
60
61     protected static SchemaContext schemaContextLoader(final String yangPath,
62             final SchemaContext schemaContext) {
63         return TestRestconfUtils.loadSchemaContext(yangPath, schemaContext);
64     }
65
66     protected static <T extends AbstractIdentifierAwareJaxRsProvider> void mockBodyReader(
67             final String identifier, final T normalizedNodeProvider,
68             final boolean isPost) throws NoSuchFieldException,
69             SecurityException, IllegalArgumentException, IllegalAccessException {
70         final UriInfo uriInfoMock = mock(UriInfo.class);
71         final MultivaluedMap<String, String> pathParm = new MultivaluedHashMap<>(
72                 1);
73         pathParm.put(RestconfConstants.IDENTIFIER,
74                 Collections.singletonList(identifier));
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         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         requestField.set(normalizedNodeProvider, request);
86     }
87
88     protected static void checkMountPointNormalizedNodeContext(
89             final NormalizedNodeContext nnContext) {
90         checkNormalizedNodeContext(nnContext);
91         assertNotNull(nnContext.getInstanceIdentifierContext().getMountPoint());
92     }
93
94     protected static void checkNormalizedNodeContext(
95             final NormalizedNodeContext nnContext) {
96         assertNotNull(nnContext.getData());
97         assertNotNull(nnContext.getInstanceIdentifierContext()
98                 .getInstanceIdentifier());
99         assertNotNull(nnContext.getInstanceIdentifierContext()
100                 .getSchemaContext());
101         assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaNode());
102     }
103 }