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.jersey.providers;
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.controller.md.sal.rest.common.TestRestconfUtils;
23 import org.opendaylight.netconf.sal.rest.api.RestconfConstants;
24 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
25 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
26 import org.opendaylight.netconf.sal.restconf.impl.PatchContext;
27 import org.opendaylight.restconf.RestConnectorProvider;
28 import org.opendaylight.restconf.handlers.DOMMountPointServiceHandler;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 abstract class AbstractBodyReaderTest {
33 protected static final ControllerContext CONTROLLER_CONTEXT = ControllerContext.getInstance();
34 protected static final DOMMountPointServiceHandler MOUNT_POINT_SERVICE_HANDLER =
35 mock(DOMMountPointServiceHandler.class);
37 protected final MediaType mediaType;
39 AbstractBodyReaderTest() throws NoSuchFieldException, IllegalAccessException {
40 mediaType = getMediaType();
42 final Field mountPointServiceHandlerField =
43 RestConnectorProvider.class.getDeclaredField("mountPointServiceHandler");
44 mountPointServiceHandlerField.setAccessible(true);
45 mountPointServiceHandlerField.set(RestConnectorProvider.class, MOUNT_POINT_SERVICE_HANDLER);
48 protected abstract MediaType getMediaType();
50 protected static SchemaContext schemaContextLoader(final String yangPath,
51 final SchemaContext schemaContext) {
52 return TestRestconfUtils.loadSchemaContext(yangPath, schemaContext);
55 protected static <T extends AbstractIdentifierAwareJaxRsProvider> void mockBodyReader(
56 final String identifier, final T normalizedNodeProvider,
57 final boolean isPost) throws NoSuchFieldException,
58 SecurityException, IllegalArgumentException, IllegalAccessException {
59 final UriInfo uriInfoMock = mock(UriInfo.class);
60 final MultivaluedMap<String, String> pathParm = new MultivaluedHashMap<>(1);
62 if (!identifier.isEmpty()) {
63 pathParm.put(RestconfConstants.IDENTIFIER, Collections.singletonList(identifier));
66 when(uriInfoMock.getPathParameters()).thenReturn(pathParm);
67 when(uriInfoMock.getPathParameters(false)).thenReturn(pathParm);
68 when(uriInfoMock.getPathParameters(true)).thenReturn(pathParm);
69 normalizedNodeProvider.setUriInfo(uriInfoMock);
71 final Request request = mock(Request.class);
73 when(request.getMethod()).thenReturn("POST");
75 when(request.getMethod()).thenReturn("PUT");
78 normalizedNodeProvider.setRequest(request);
81 protected static void checkMountPointNormalizedNodeContext(
82 final NormalizedNodeContext nnContext) {
83 checkNormalizedNodeContext(nnContext);
84 assertNotNull(nnContext.getInstanceIdentifierContext().getMountPoint());
87 protected static void checkNormalizedNodeContext(
88 final NormalizedNodeContext nnContext) {
89 assertNotNull(nnContext.getData());
90 assertNotNull(nnContext.getInstanceIdentifierContext()
91 .getInstanceIdentifier());
92 assertNotNull(nnContext.getInstanceIdentifierContext()
94 assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaNode());
97 protected static void checkPatchContext(final PatchContext patchContext) {
98 assertNotNull(patchContext.getData());
99 assertNotNull(patchContext.getInstanceIdentifierContext().getInstanceIdentifier());
100 assertNotNull(patchContext.getInstanceIdentifierContext().getSchemaContext());
101 assertNotNull(patchContext.getInstanceIdentifierContext().getSchemaNode());
104 protected static void checkPatchContextMountPoint(final PatchContext patchContext) {
105 checkPatchContext(patchContext);
106 assertNotNull(patchContext.getInstanceIdentifierContext().getMountPoint());
107 assertNotNull(patchContext.getInstanceIdentifierContext().getMountPoint().getSchemaContext());