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