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;
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.UriInfo;
21 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
22 import org.opendaylight.controller.sal.rest.api.RestconfConstants;
23 import org.opendaylight.controller.sal.rest.impl.AbstractIdentifierAwareJaxRsProvider;
24 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
25 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 * org.opendaylight.controller.sal.rest.impl.test.providers
34 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
36 * Created: Mar 7, 2015
38 public abstract class AbstractBodyReaderTest {
40 protected final static ControllerContext controllerContext = ControllerContext.getInstance();
41 protected final MediaType mediaType;
42 private static Field uriField;
44 public AbstractBodyReaderTest () throws NoSuchFieldException, SecurityException {
45 uriField = AbstractIdentifierAwareJaxRsProvider.class.getDeclaredField("uriInfo");
46 uriField.setAccessible(true);
47 mediaType = getMediaType();
50 abstract MediaType getMediaType();
52 protected static SchemaContext schemaContextLoader(final String yangPath, final SchemaContext schemaContext) {
53 return TestRestconfUtils.loadSchemaContext(yangPath, schemaContext);
56 protected static <T extends AbstractIdentifierAwareJaxRsProvider> void mockBodyReader(
57 final String identifier, final T normalizedNodeProvider) throws NoSuchFieldException,
58 SecurityException, IllegalArgumentException, IllegalAccessException {
59 final UriInfo uriInfoMock = mock(UriInfo.class);
60 final MultivaluedMap<String, String> pathParm = new MultivaluedHashMap<>(1);
61 pathParm.put(RestconfConstants.IDENTIFIER, Collections.singletonList(identifier));
62 when(uriInfoMock.getPathParameters()).thenReturn(pathParm);
63 when(uriInfoMock.getPathParameters(false)).thenReturn(pathParm);
64 when(uriInfoMock.getPathParameters(true)).thenReturn(pathParm);
65 uriField.set(normalizedNodeProvider, uriInfoMock);
68 protected static void checkMountPointNormalizedNodeContext(final NormalizedNodeContext nnContext) {
69 checkNormalizedNodeContext(nnContext);
70 assertNotNull(nnContext.getInstanceIdentifierContext().getMountPoint());
73 protected static void checkNormalizedNodeContext(final NormalizedNodeContext nnContext) {
74 assertNotNull(nnContext);
75 assertNotNull(nnContext.getData());
76 assertNotNull(nnContext.getInstanceIdentifierContext());
77 assertNotNull(nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
78 assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaContext());
79 assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaNode());