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