Split Restconf implementations (draft02 and RFC) - providers tests
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / test / AbstractBodyReaderTest.java
1 /*
2  * Copyright (c) 2016 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.restconf.nb.rfc8040.jersey.providers.test;
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.Request;
21 import javax.ws.rs.core.UriInfo;
22 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
23 import org.opendaylight.restconf.common.patch.PatchContext;
24 import org.opendaylight.restconf.nb.rfc8040.RestConnectorProvider;
25 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
26 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMMountPointServiceHandler;
27 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.spi.AbstractIdentifierAwareJaxRsProvider;
28 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30
31 public abstract class AbstractBodyReaderTest {
32
33     protected static final DOMMountPointServiceHandler MOUNT_POINT_SERVICE_HANDLER =
34             mock(DOMMountPointServiceHandler.class);
35
36     protected final MediaType mediaType;
37
38     protected AbstractBodyReaderTest() throws NoSuchFieldException, IllegalAccessException {
39         mediaType = getMediaType();
40
41         final Field mountPointServiceHandlerField =
42                 RestConnectorProvider.class.getDeclaredField("mountPointServiceHandler");
43         mountPointServiceHandlerField.setAccessible(true);
44         mountPointServiceHandlerField.set(RestConnectorProvider.class, MOUNT_POINT_SERVICE_HANDLER);
45     }
46
47     protected abstract MediaType getMediaType();
48
49     protected static SchemaContext schemaContextLoader(final String yangPath,
50             final SchemaContext schemaContext) {
51         return TestRestconfUtils.loadSchemaContext(yangPath, schemaContext);
52     }
53
54     protected static <T extends AbstractIdentifierAwareJaxRsProvider> void mockBodyReader(
55             final String identifier, final T normalizedNodeProvider,
56             final boolean isPost) throws NoSuchFieldException,
57             SecurityException, IllegalArgumentException, IllegalAccessException {
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         normalizedNodeProvider.setUriInfo(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         normalizedNodeProvider.setRequest(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 }