a366ce29b0329cdc26cb809e860bd155befd4d03
[netconf.git] / 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.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.rest.common.TestRestconfUtils;
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.netconf.sal.restconf.impl.NormalizedNodeContext;
28 import org.opendaylight.netconf.sal.restconf.impl.PATCHContext;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30
31 public 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
38     public AbstractBodyReaderTest() throws NoSuchFieldException,
39             SecurityException {
40         uriField = AbstractIdentifierAwareJaxRsProvider.class
41                 .getDeclaredField("uriInfo");
42         uriField.setAccessible(true);
43         requestField = AbstractIdentifierAwareJaxRsProvider.class
44                 .getDeclaredField("request");
45         requestField.setAccessible(true);
46         this.mediaType = getMediaType();
47     }
48
49     protected abstract MediaType getMediaType();
50
51     protected static SchemaContext schemaContextLoader(final String yangPath,
52             final SchemaContext schemaContext) {
53         return TestRestconfUtils.loadSchemaContext(yangPath, schemaContext);
54     }
55
56     protected static <T extends AbstractIdentifierAwareJaxRsProvider> void mockBodyReader(final String identifier,
57             final T normalizedNodeProvider, final boolean isPost) throws Exception {
58         final UriInfo uriInfoMock = mock(UriInfo.class);
59         final MultivaluedMap<String, String> pathParm = new MultivaluedHashMap<>(1);
60
61         if (!identifier.isEmpty()) {
62             pathParm.put(RestconfConstants.IDENTIFIER, Collections.singletonList(identifier));
63         }
64
65         when(uriInfoMock.getPathParameters()).thenReturn(pathParm);
66         when(uriInfoMock.getPathParameters(false)).thenReturn(pathParm);
67         when(uriInfoMock.getPathParameters(true)).thenReturn(pathParm);
68         when(uriInfoMock.getAbsolutePath()).thenReturn(new URI("restconf"));
69         uriField.set(normalizedNodeProvider, uriInfoMock);
70
71         final Request request = mock(Request.class);
72         if (isPost) {
73             when(request.getMethod()).thenReturn("POST");
74         } else {
75             when(request.getMethod()).thenReturn("PUT");
76         }
77
78         requestField.set(normalizedNodeProvider, request);
79     }
80
81     protected static void checkMountPointNormalizedNodeContext(
82             final NormalizedNodeContext nnContext) {
83         checkNormalizedNodeContext(nnContext);
84         assertNotNull(nnContext.getInstanceIdentifierContext().getMountPoint());
85     }
86
87     protected static void checkNormalizedNodeContext(
88             final NormalizedNodeContext nnContext) {
89         assertNotNull(nnContext.getData());
90         assertNotNull(nnContext.getInstanceIdentifierContext()
91                 .getInstanceIdentifier());
92         assertNotNull(nnContext.getInstanceIdentifierContext()
93                 .getSchemaContext());
94         assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaNode());
95     }
96
97     protected static void checkPATCHContext(final PATCHContext patchContext) {
98         assertNotNull(patchContext.getData());
99         assertNotNull(patchContext.getInstanceIdentifierContext().getInstanceIdentifier());
100         assertNotNull(patchContext.getInstanceIdentifierContext().getSchemaContext());
101         assertNotNull(patchContext.getInstanceIdentifierContext().getSchemaNode());
102     }
103
104     protected static void checkPATCHContextMountPoint(final PATCHContext patchContext) {
105         checkPATCHContext(patchContext);
106         assertNotNull(patchContext.getInstanceIdentifierContext().getMountPoint());
107         assertNotNull(patchContext.getInstanceIdentifierContext().getMountPoint().getSchemaContext());
108     }
109 }