Merge "Reduce/enhance logging in AbstractLeader"
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / rest / impl / test / providers / AbstractBodyReaderTest.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.sal.rest.impl.test.providers;
10
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
14 import java.lang.reflect.Field;
15 import java.util.Collections;
16 import javax.ws.rs.core.MediaType;
17 import javax.ws.rs.core.MultivaluedHashMap;
18 import javax.ws.rs.core.MultivaluedMap;
19 import javax.ws.rs.core.Request;
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;
27
28 /**
29  * sal-rest-connector
30  * org.opendaylight.controller.sal.rest.impl.test.providers
31  *
32  *
33  *
34  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
35  *
36  * Created: Mar 7, 2015
37  */
38 public abstract class AbstractBodyReaderTest {
39
40     protected final static ControllerContext controllerContext = ControllerContext.getInstance();
41     protected final MediaType mediaType;
42     private static Field uriField;
43     private static Field requestField;
44
45     public AbstractBodyReaderTest () throws NoSuchFieldException, SecurityException {
46         uriField = AbstractIdentifierAwareJaxRsProvider.class.getDeclaredField("uriInfo");
47         uriField.setAccessible(true);
48         requestField = AbstractIdentifierAwareJaxRsProvider.class.getDeclaredField("request");
49         requestField.setAccessible(true);
50         mediaType = getMediaType();
51     }
52
53     abstract MediaType getMediaType();
54
55     protected static SchemaContext schemaContextLoader(final String yangPath, final SchemaContext schemaContext) {
56         return TestRestconfUtils.loadSchemaContext(yangPath, schemaContext);
57     }
58
59     protected static <T extends AbstractIdentifierAwareJaxRsProvider> void mockBodyReader(
60             final String identifier, final T normalizedNodeProvider, final boolean isPost) throws NoSuchFieldException,
61             SecurityException, IllegalArgumentException, IllegalAccessException {
62         final UriInfo uriInfoMock = mock(UriInfo.class);
63         final MultivaluedMap<String, String> pathParm = new MultivaluedHashMap<>(1);
64         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         uriField.set(normalizedNodeProvider, uriInfoMock);
69         final Request request = mock(Request.class);
70         if (isPost) {
71             when(request.getMethod()).thenReturn("POST");
72         } else {
73             when(request.getMethod()).thenReturn("PUT");
74         }
75         requestField.set(normalizedNodeProvider, request);
76     }
77
78     protected static void checkMountPointNormalizedNodeContext(final NormalizedNodeContext nnContext) {
79         checkNormalizedNodeContext(nnContext);
80         assertNotNull(nnContext.getInstanceIdentifierContext().getMountPoint());
81     }
82
83     protected static void checkNormalizedNodeContext(final NormalizedNodeContext nnContext) {
84         assertNotNull(nnContext);
85         assertNotNull(nnContext.getData());
86         assertNotNull(nnContext.getInstanceIdentifierContext());
87         assertNotNull(nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
88         assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaContext());
89         assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaNode());
90     }
91 }