2 * Copyright (c) 2015 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.controller.sal.rest.impl.test.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;
18 import javax.ws.rs.core.MediaType;
19 import javax.ws.rs.core.MultivaluedHashMap;
20 import javax.ws.rs.core.MultivaluedMap;
21 import javax.ws.rs.core.Request;
22 import javax.ws.rs.core.UriInfo;
24 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
25 import org.opendaylight.controller.sal.rest.api.RestconfConstants;
26 import org.opendaylight.controller.sal.rest.impl.AbstractIdentifierAwareJaxRsProvider;
27 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
28 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 * sal-rest-connector org.opendaylight.controller.sal.rest.impl.test.providers
36 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
38 * Created: Mar 7, 2015
40 public abstract class AbstractBodyReaderTest {
42 protected final static ControllerContext controllerContext = ControllerContext
44 protected final MediaType mediaType;
45 private static Field uriField;
46 private static Field requestField;
48 public AbstractBodyReaderTest() throws NoSuchFieldException,
50 uriField = AbstractIdentifierAwareJaxRsProvider.class
51 .getDeclaredField("uriInfo");
52 uriField.setAccessible(true);
53 requestField = AbstractIdentifierAwareJaxRsProvider.class
54 .getDeclaredField("request");
55 requestField.setAccessible(true);
56 mediaType = getMediaType();
59 protected abstract MediaType getMediaType();
61 protected static SchemaContext schemaContextLoader(final String yangPath,
62 final SchemaContext schemaContext) {
63 return TestRestconfUtils.loadSchemaContext(yangPath, schemaContext);
66 protected static <T extends AbstractIdentifierAwareJaxRsProvider> void mockBodyReader(
67 final String identifier, final T normalizedNodeProvider,
68 final boolean isPost) throws NoSuchFieldException,
69 SecurityException, IllegalArgumentException, IllegalAccessException {
70 final UriInfo uriInfoMock = mock(UriInfo.class);
71 final MultivaluedMap<String, String> pathParm = new MultivaluedHashMap<>(
73 pathParm.put(RestconfConstants.IDENTIFIER,
74 Collections.singletonList(identifier));
75 when(uriInfoMock.getPathParameters()).thenReturn(pathParm);
76 when(uriInfoMock.getPathParameters(false)).thenReturn(pathParm);
77 when(uriInfoMock.getPathParameters(true)).thenReturn(pathParm);
78 uriField.set(normalizedNodeProvider, uriInfoMock);
79 final Request request = mock(Request.class);
81 when(request.getMethod()).thenReturn("POST");
83 when(request.getMethod()).thenReturn("PUT");
85 requestField.set(normalizedNodeProvider, request);
88 protected static void checkMountPointNormalizedNodeContext(
89 final NormalizedNodeContext nnContext) {
90 checkNormalizedNodeContext(nnContext);
91 assertNotNull(nnContext.getInstanceIdentifierContext().getMountPoint());
94 protected static void checkNormalizedNodeContext(
95 final NormalizedNodeContext nnContext) {
96 assertNotNull(nnContext.getData());
97 assertNotNull(nnContext.getInstanceIdentifierContext()
98 .getInstanceIdentifier());
99 assertNotNull(nnContext.getInstanceIdentifierContext()
100 .getSchemaContext());
101 assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaNode());