Merge "BUG 2805 - Fixed Restconf RPC calls and POST to create data"
[controller.git] / opendaylight / md-sal / 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 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.UriInfo;
21 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
22 import org.opendaylight.controller.sal.rest.api.RestconfConstants;
23 import org.opendaylight.controller.sal.rest.impl.AbstractIdentifierAwareJaxRsProvider;
24 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
25 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27
28 /**
29  * sal-rest-connector
30  * org.opendaylight.controller.sal.rest.impl.test.providers
31  *
32  *
33  *
34  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
35  *
36  * Created: Mar 7, 2015
37  */
38 public abstract class AbstractBodyReaderTest {
39
40     protected final static ControllerContext controllerContext = ControllerContext.getInstance();
41     protected final MediaType mediaType;
42     private static Field uriField;
43
44     public AbstractBodyReaderTest () throws NoSuchFieldException, SecurityException {
45         uriField = AbstractIdentifierAwareJaxRsProvider.class.getDeclaredField("uriInfo");
46         uriField.setAccessible(true);
47         mediaType = getMediaType();
48     }
49
50     abstract MediaType getMediaType();
51
52     protected static SchemaContext schemaContextLoader(final String yangPath, final SchemaContext schemaContext) {
53         return TestRestconfUtils.loadSchemaContext(yangPath, schemaContext);
54     }
55
56     protected static <T extends AbstractIdentifierAwareJaxRsProvider> void mockBodyReader(
57             final String identifier, final T normalizedNodeProvider) throws NoSuchFieldException,
58             SecurityException, IllegalArgumentException, IllegalAccessException {
59         final UriInfo uriInfoMock = mock(UriInfo.class);
60         final MultivaluedMap<String, String> pathParm = new MultivaluedHashMap<>(1);
61         pathParm.put(RestconfConstants.IDENTIFIER, Collections.singletonList(identifier));
62         when(uriInfoMock.getPathParameters()).thenReturn(pathParm);
63         when(uriInfoMock.getPathParameters(false)).thenReturn(pathParm);
64         when(uriInfoMock.getPathParameters(true)).thenReturn(pathParm);
65         uriField.set(normalizedNodeProvider, uriInfoMock);
66     }
67
68     protected static void checkMountPointNormalizedNodeContext(final NormalizedNodeContext nnContext) {
69         checkNormalizedNodeContext(nnContext);
70         assertNotNull(nnContext.getInstanceIdentifierContext().getMountPoint());
71     }
72
73     protected static void checkNormalizedNodeContext(final NormalizedNodeContext nnContext) {
74         assertNotNull(nnContext);
75         assertNotNull(nnContext.getData());
76         assertNotNull(nnContext.getInstanceIdentifierContext());
77         assertNotNull(nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
78         assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaContext());
79         assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaNode());
80     }
81 }