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 final static ControllerContext controllerContext = ControllerContext.getInstance();
34 protected final MediaType mediaType;
35 protected final static DOMMountPointServiceHandler mountPointServiceHandler = mock(
36 DOMMountPointServiceHandler.class);
38 AbstractBodyReaderTest() throws NoSuchFieldException, IllegalAccessException {
39 mediaType = getMediaType();
41 final Field mountPointServiceHandlerField = RestConnectorProvider.class.
42 getDeclaredField("mountPointServiceHandler");
43 mountPointServiceHandlerField.setAccessible(true);
44 mountPointServiceHandlerField.set(RestConnectorProvider.class, mountPointServiceHandler);
47 protected abstract MediaType getMediaType();
49 protected static SchemaContext schemaContextLoader(final String yangPath,
50 final SchemaContext schemaContext) {
51 return TestRestconfUtils.loadSchemaContext(yangPath, schemaContext);
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);
61 if (!identifier.isEmpty()) {
62 pathParm.put(RestconfConstants.IDENTIFIER, Collections.singletonList(identifier));
65 when(uriInfoMock.getPathParameters()).thenReturn(pathParm);
66 when(uriInfoMock.getPathParameters(false)).thenReturn(pathParm);
67 when(uriInfoMock.getPathParameters(true)).thenReturn(pathParm);
68 normalizedNodeProvider.setUriInfo(uriInfoMock);
70 final Request request = mock(Request.class);
72 when(request.getMethod()).thenReturn("POST");
74 when(request.getMethod()).thenReturn("PUT");
77 normalizedNodeProvider.setRequest(request);
80 protected static void checkNormalizedNodeContext(
81 final NormalizedNodeContext nnContext) {
82 assertNotNull(nnContext.getData());
83 assertNotNull(nnContext.getInstanceIdentifierContext()
84 .getInstanceIdentifier());
85 assertNotNull(nnContext.getInstanceIdentifierContext()
87 assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaNode());
90 protected static void checkPATCHContext(final PATCHContext patchContext) {
91 assertNotNull(patchContext.getData());
92 assertNotNull(patchContext.getInstanceIdentifierContext().getInstanceIdentifier());
93 assertNotNull(patchContext.getInstanceIdentifierContext().getSchemaContext());
94 assertNotNull(patchContext.getInstanceIdentifierContext().getSchemaNode());
97 protected static void checkPATCHContextMountPoint(final PATCHContext patchContext) {
98 checkPATCHContext(patchContext);
99 assertNotNull(patchContext.getInstanceIdentifierContext().getMountPoint());
100 assertNotNull(patchContext.getInstanceIdentifierContext().getMountPoint().getSchemaContext());