Use SchemaContextHandler non-statically
[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.TestUtils;
27 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMMountPointServiceHandler;
28 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
29 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.spi.AbstractIdentifierAwareJaxRsProvider;
30 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32
33 public abstract class AbstractBodyReaderTest {
34
35     protected static final DOMMountPointServiceHandler MOUNT_POINT_SERVICE_HANDLER =
36             mock(DOMMountPointServiceHandler.class);
37
38     protected final MediaType mediaType;
39     protected final SchemaContextHandler schemaContextHandler;
40
41     protected AbstractBodyReaderTest(SchemaContext schemaContext) throws NoSuchFieldException, IllegalAccessException {
42         mediaType = getMediaType();
43
44         final Field mountPointServiceHandlerField =
45                 RestConnectorProvider.class.getDeclaredField("mountPointServiceHandler");
46         mountPointServiceHandlerField.setAccessible(true);
47         mountPointServiceHandlerField.set(RestConnectorProvider.class, MOUNT_POINT_SERVICE_HANDLER);
48
49         schemaContextHandler = TestUtils.newSchemaContextHandler(schemaContext);
50     }
51
52     protected abstract MediaType getMediaType();
53
54     protected static SchemaContext schemaContextLoader(final String yangPath,
55             final SchemaContext schemaContext) {
56         return TestRestconfUtils.loadSchemaContext(yangPath, schemaContext);
57     }
58
59     protected static <T extends AbstractIdentifierAwareJaxRsProvider> void mockBodyReader(
60             final String identifier, final T normalizedNodeProvider,
61             final boolean isPost) throws NoSuchFieldException,
62             SecurityException, IllegalArgumentException, IllegalAccessException {
63         final UriInfo uriInfoMock = mock(UriInfo.class);
64         final MultivaluedMap<String, String> pathParm = new MultivaluedHashMap<>(1);
65
66         if (!identifier.isEmpty()) {
67             pathParm.put(RestconfConstants.IDENTIFIER, Collections.singletonList(identifier));
68         }
69
70         when(uriInfoMock.getPathParameters()).thenReturn(pathParm);
71         when(uriInfoMock.getPathParameters(false)).thenReturn(pathParm);
72         when(uriInfoMock.getPathParameters(true)).thenReturn(pathParm);
73         normalizedNodeProvider.setUriInfo(uriInfoMock);
74
75         final Request request = mock(Request.class);
76         if (isPost) {
77             when(request.getMethod()).thenReturn("POST");
78         } else {
79             when(request.getMethod()).thenReturn("PUT");
80         }
81
82         normalizedNodeProvider.setRequest(request);
83     }
84
85     protected static void checkMountPointNormalizedNodeContext(
86             final NormalizedNodeContext nnContext) {
87         checkNormalizedNodeContext(nnContext);
88         assertNotNull(nnContext.getInstanceIdentifierContext().getMountPoint());
89     }
90
91     protected static void checkNormalizedNodeContext(
92             final NormalizedNodeContext nnContext) {
93         assertNotNull(nnContext.getData());
94         assertNotNull(nnContext.getInstanceIdentifierContext()
95                 .getInstanceIdentifier());
96         assertNotNull(nnContext.getInstanceIdentifierContext()
97                 .getSchemaContext());
98         assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaNode());
99     }
100
101     protected static void checkPatchContext(final PatchContext patchContext) {
102         assertNotNull(patchContext.getData());
103         assertNotNull(patchContext.getInstanceIdentifierContext().getInstanceIdentifier());
104         assertNotNull(patchContext.getInstanceIdentifierContext().getSchemaContext());
105         assertNotNull(patchContext.getInstanceIdentifierContext().getSchemaNode());
106     }
107
108     protected static void checkPatchContextMountPoint(final PatchContext patchContext) {
109         checkPatchContext(patchContext);
110         assertNotNull(patchContext.getInstanceIdentifierContext().getMountPoint());
111         assertNotNull(patchContext.getInstanceIdentifierContext().getMountPoint().getSchemaContext());
112     }
113 }