Bump upstreams for Silicon
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / test / AbstractBodyReaderTest.java
1 /*
2  * Copyright (c) 2016 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.restconf.nb.rfc8040.jersey.providers.test;
9
10 import static org.junit.Assert.assertNotNull;
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.when;
15
16 import java.util.Collections;
17 import java.util.Optional;
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;
23 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
24 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
25 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
26 import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
27 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
28 import org.opendaylight.restconf.common.patch.PatchContext;
29 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
30 import org.opendaylight.restconf.nb.rfc8040.TestUtils;
31 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMMountPointServiceHandler;
32 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
33 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.spi.AbstractIdentifierAwareJaxRsProvider;
34 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
37
38 public abstract class AbstractBodyReaderTest {
39
40     protected final MediaType mediaType;
41     protected final SchemaContextHandler schemaContextHandler;
42     protected final DOMMountPointServiceHandler mountPointServiceHandler;
43
44     protected AbstractBodyReaderTest(final EffectiveModelContext schemaContext) throws NoSuchFieldException,
45             IllegalAccessException {
46         mediaType = getMediaType();
47
48         schemaContextHandler = TestUtils.newSchemaContextHandler(schemaContext);
49
50         final DOMMountPointService mountPointService = mock(DOMMountPointService.class);
51         final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
52         doReturn(Optional.of(mountPoint)).when(mountPointService).getMountPoint(any(YangInstanceIdentifier.class));
53         doReturn(Optional.of(FixedDOMSchemaService.of(schemaContext))).when(mountPoint)
54             .getService(DOMSchemaService.class);
55
56         mountPointServiceHandler = new DOMMountPointServiceHandler(mountPointService);
57     }
58
59     protected abstract MediaType getMediaType();
60
61     protected static EffectiveModelContext schemaContextLoader(final String yangPath,
62             final EffectiveModelContext schemaContext) {
63         return TestRestconfUtils.loadSchemaContext(yangPath, schemaContext);
64     }
65
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<>(1);
72
73         if (!identifier.isEmpty()) {
74             pathParm.put(RestconfConstants.IDENTIFIER, Collections.singletonList(identifier));
75         }
76
77         when(uriInfoMock.getPathParameters()).thenReturn(pathParm);
78         when(uriInfoMock.getPathParameters(false)).thenReturn(pathParm);
79         when(uriInfoMock.getPathParameters(true)).thenReturn(pathParm);
80         normalizedNodeProvider.setUriInfo(uriInfoMock);
81
82         final Request request = mock(Request.class);
83         if (isPost) {
84             when(request.getMethod()).thenReturn("POST");
85         } else {
86             when(request.getMethod()).thenReturn("PUT");
87         }
88
89         normalizedNodeProvider.setRequest(request);
90     }
91
92     protected static void checkMountPointNormalizedNodeContext(
93             final NormalizedNodeContext nnContext) {
94         checkNormalizedNodeContext(nnContext);
95         assertNotNull(nnContext.getInstanceIdentifierContext().getMountPoint());
96     }
97
98     protected static void checkNormalizedNodeContext(
99             final NormalizedNodeContext nnContext) {
100         assertNotNull(nnContext.getData());
101         assertNotNull(nnContext.getInstanceIdentifierContext()
102                 .getInstanceIdentifier());
103         assertNotNull(nnContext.getInstanceIdentifierContext()
104                 .getSchemaContext());
105         assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaNode());
106     }
107
108     protected static void checkPatchContext(final PatchContext patchContext) {
109         assertNotNull(patchContext.getData());
110         assertNotNull(patchContext.getInstanceIdentifierContext().getInstanceIdentifier());
111         assertNotNull(patchContext.getInstanceIdentifierContext().getSchemaContext());
112         assertNotNull(patchContext.getInstanceIdentifierContext().getSchemaNode());
113     }
114
115     protected static void checkPatchContextMountPoint(final PatchContext patchContext) {
116         checkPatchContext(patchContext);
117         assertNotNull(patchContext.getInstanceIdentifierContext().getMountPoint());
118     }
119
120     protected static EffectiveModelContext modelContext(final DOMMountPoint mountPoint) {
121         return mountPoint.getService(DOMSchemaService.class)
122             .flatMap(svc -> Optional.ofNullable(svc.getGlobalContext()))
123             .orElse(null);
124     }
125
126 }