2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.test;
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
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;
33 public abstract class AbstractBodyReaderTest {
35 protected static final DOMMountPointServiceHandler MOUNT_POINT_SERVICE_HANDLER =
36 mock(DOMMountPointServiceHandler.class);
38 protected final MediaType mediaType;
39 protected final SchemaContextHandler schemaContextHandler;
41 protected AbstractBodyReaderTest(SchemaContext schemaContext) throws NoSuchFieldException, IllegalAccessException {
42 mediaType = getMediaType();
44 final Field mountPointServiceHandlerField =
45 RestConnectorProvider.class.getDeclaredField("mountPointServiceHandler");
46 mountPointServiceHandlerField.setAccessible(true);
47 mountPointServiceHandlerField.set(RestConnectorProvider.class, MOUNT_POINT_SERVICE_HANDLER);
49 schemaContextHandler = TestUtils.newSchemaContextHandler(schemaContext);
52 protected abstract MediaType getMediaType();
54 protected static SchemaContext schemaContextLoader(final String yangPath,
55 final SchemaContext schemaContext) {
56 return TestRestconfUtils.loadSchemaContext(yangPath, schemaContext);
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);
66 if (!identifier.isEmpty()) {
67 pathParm.put(RestconfConstants.IDENTIFIER, Collections.singletonList(identifier));
70 when(uriInfoMock.getPathParameters()).thenReturn(pathParm);
71 when(uriInfoMock.getPathParameters(false)).thenReturn(pathParm);
72 when(uriInfoMock.getPathParameters(true)).thenReturn(pathParm);
73 normalizedNodeProvider.setUriInfo(uriInfoMock);
75 final Request request = mock(Request.class);
77 when(request.getMethod()).thenReturn("POST");
79 when(request.getMethod()).thenReturn("PUT");
82 normalizedNodeProvider.setRequest(request);
85 protected static void checkMountPointNormalizedNodeContext(
86 final NormalizedNodeContext nnContext) {
87 checkNormalizedNodeContext(nnContext);
88 assertNotNull(nnContext.getInstanceIdentifierContext().getMountPoint());
91 protected static void checkNormalizedNodeContext(
92 final NormalizedNodeContext nnContext) {
93 assertNotNull(nnContext.getData());
94 assertNotNull(nnContext.getInstanceIdentifierContext()
95 .getInstanceIdentifier());
96 assertNotNull(nnContext.getInstanceIdentifierContext()
98 assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaNode());
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());
108 protected static void checkPatchContextMountPoint(final PatchContext patchContext) {
109 checkPatchContext(patchContext);
110 assertNotNull(patchContext.getInstanceIdentifierContext().getMountPoint());
111 assertNotNull(patchContext.getInstanceIdentifierContext().getMountPoint().getSchemaContext());