5bbd161130db7938c58e3a90bff0d800ec67f7f6
[netconf.git] / restconf / restconf-nb-bierman02 / 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 package org.opendaylight.controller.sal.rest.impl.test.providers;
9
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertNull;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
14
15 import java.lang.reflect.Field;
16 import java.net.URI;
17 import java.util.List;
18 import java.util.Optional;
19 import javax.ws.rs.core.MediaType;
20 import javax.ws.rs.core.MultivaluedHashMap;
21 import javax.ws.rs.core.MultivaluedMap;
22 import javax.ws.rs.core.Request;
23 import javax.ws.rs.core.UriInfo;
24 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
25 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
26 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
27 import org.opendaylight.netconf.sal.rest.api.RestconfConstants;
28 import org.opendaylight.netconf.sal.rest.impl.AbstractIdentifierAwareJaxRsProvider;
29 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext;
30 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
31 import org.opendaylight.restconf.common.patch.PatchContext;
32 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
33
34 public abstract class AbstractBodyReaderTest {
35     private static Field uriField;
36     private static Field requestField;
37
38     static {
39         try {
40             uriField = AbstractIdentifierAwareJaxRsProvider.class.getDeclaredField("uriInfo");
41             uriField.setAccessible(true);
42             requestField = AbstractIdentifierAwareJaxRsProvider.class.getDeclaredField("request");
43             requestField.setAccessible(true);
44         } catch (NoSuchFieldException e) {
45             throw new RuntimeException(e);
46         }
47     }
48
49     protected final ControllerContext controllerContext;
50     protected final MediaType mediaType;
51
52     protected AbstractBodyReaderTest(final EffectiveModelContext schemaContext, final DOMMountPoint mountInstance) {
53         mediaType = getMediaType();
54
55         controllerContext = TestRestconfUtils.newControllerContext(schemaContext, mountInstance);
56     }
57
58     protected abstract MediaType getMediaType();
59
60     protected static EffectiveModelContext schemaContextLoader(final String yangPath,
61             final EffectiveModelContext schemaContext) {
62         return TestRestconfUtils.loadSchemaContext(yangPath, schemaContext);
63     }
64
65     protected static <T extends AbstractIdentifierAwareJaxRsProvider> void mockBodyReader(
66             final String identifier, final T normalizedNodeProvider, final boolean isPost) throws Exception {
67         final UriInfo uriInfoMock = mock(UriInfo.class);
68         final MultivaluedMap<String, String> pathParm = new MultivaluedHashMap<>(1);
69
70         if (!identifier.isEmpty()) {
71             pathParm.put(RestconfConstants.IDENTIFIER, List.of(identifier));
72         }
73
74         when(uriInfoMock.getPathParameters()).thenReturn(pathParm);
75         when(uriInfoMock.getPathParameters(false)).thenReturn(pathParm);
76         when(uriInfoMock.getPathParameters(true)).thenReturn(pathParm);
77         when(uriInfoMock.getAbsolutePath()).thenReturn(URI.create("restconf"));
78         uriField.set(normalizedNodeProvider, uriInfoMock);
79
80         final Request request = mock(Request.class);
81         if (isPost) {
82             when(request.getMethod()).thenReturn("POST");
83         } else {
84             when(request.getMethod()).thenReturn("PUT");
85         }
86
87         requestField.set(normalizedNodeProvider, request);
88     }
89
90     protected static void checkMountPointNormalizedNodeContext(final NormalizedNodeContext nnContext) {
91         checkNormalizedNodeContext(nnContext);
92         assertNotNull(nnContext.getInstanceIdentifierContext().getMountPoint());
93     }
94
95     protected static void checkNormalizedNodeContext(final NormalizedNodeContext nnContext) {
96         assertNotNull(nnContext.getData());
97         assertNotNull(nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
98         assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaContext());
99         assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaNode());
100     }
101
102     protected static void checkNormalizedNodeContextRpc(final NormalizedNodeContext nnContext) {
103         assertNotNull(nnContext.getData());
104         assertNull(nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
105         assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaContext());
106         assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaNode());
107     }
108
109     protected static void checkPatchContext(final PatchContext patchContext) {
110         assertNotNull(patchContext.getData());
111         assertNotNull(patchContext.getInstanceIdentifierContext().getInstanceIdentifier());
112         assertNotNull(patchContext.getInstanceIdentifierContext().getSchemaContext());
113         assertNotNull(patchContext.getInstanceIdentifierContext().getSchemaNode());
114     }
115
116     protected static void checkPatchContextMountPoint(final PatchContext patchContext) {
117         checkPatchContext(patchContext);
118         assertNotNull(patchContext.getInstanceIdentifierContext().getMountPoint());
119     }
120
121     protected static EffectiveModelContext modelContext(final DOMMountPoint mountPoint) {
122         return mountPoint.getService(DOMSchemaService.class)
123             .flatMap(svc -> Optional.ofNullable(svc.getGlobalContext()))
124             .orElse(null);
125     }
126
127 }