Update MRI projects for Aluminium
[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.doCallRealMethod;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
16
17 import java.util.Collections;
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.mdsal.dom.api.DOMMountPoint;
25 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
26 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
27 import org.opendaylight.restconf.common.patch.PatchContext;
28 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
29 import org.opendaylight.restconf.nb.rfc8040.TestUtils;
30 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMMountPointServiceHandler;
31 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
32 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.spi.AbstractIdentifierAwareJaxRsProvider;
33 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
36
37 public abstract class AbstractBodyReaderTest {
38
39     protected final MediaType mediaType;
40     protected final SchemaContextHandler schemaContextHandler;
41     protected final DOMMountPointServiceHandler mountPointServiceHandler;
42
43     protected AbstractBodyReaderTest(final EffectiveModelContext schemaContext) throws NoSuchFieldException,
44             IllegalAccessException {
45         mediaType = getMediaType();
46
47         schemaContextHandler = TestUtils.newSchemaContextHandler(schemaContext);
48
49         final DOMMountPointService mountPointService = mock(DOMMountPointService.class);
50         final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
51         doReturn(Optional.of(mountPoint)).when(mountPointService).getMountPoint(any(YangInstanceIdentifier.class));
52         doReturn(schemaContext).when(mountPoint).getEffectiveModelContext();
53         doCallRealMethod().when(mountPoint).getSchemaContext();
54
55         mountPointServiceHandler = DOMMountPointServiceHandler.newInstance(mountPointService);
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,
67             final boolean isPost) throws NoSuchFieldException,
68             SecurityException, IllegalArgumentException, IllegalAccessException {
69         final UriInfo uriInfoMock = mock(UriInfo.class);
70         final MultivaluedMap<String, String> pathParm = new MultivaluedHashMap<>(1);
71
72         if (!identifier.isEmpty()) {
73             pathParm.put(RestconfConstants.IDENTIFIER, Collections.singletonList(identifier));
74         }
75
76         when(uriInfoMock.getPathParameters()).thenReturn(pathParm);
77         when(uriInfoMock.getPathParameters(false)).thenReturn(pathParm);
78         when(uriInfoMock.getPathParameters(true)).thenReturn(pathParm);
79         normalizedNodeProvider.setUriInfo(uriInfoMock);
80
81         final Request request = mock(Request.class);
82         if (isPost) {
83             when(request.getMethod()).thenReturn("POST");
84         } else {
85             when(request.getMethod()).thenReturn("PUT");
86         }
87
88         normalizedNodeProvider.setRequest(request);
89     }
90
91     protected static void checkMountPointNormalizedNodeContext(
92             final NormalizedNodeContext nnContext) {
93         checkNormalizedNodeContext(nnContext);
94         assertNotNull(nnContext.getInstanceIdentifierContext().getMountPoint());
95     }
96
97     protected static void checkNormalizedNodeContext(
98             final NormalizedNodeContext nnContext) {
99         assertNotNull(nnContext.getData());
100         assertNotNull(nnContext.getInstanceIdentifierContext()
101                 .getInstanceIdentifier());
102         assertNotNull(nnContext.getInstanceIdentifierContext()
103                 .getSchemaContext());
104         assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaNode());
105     }
106
107     protected static void checkPatchContext(final PatchContext patchContext) {
108         assertNotNull(patchContext.getData());
109         assertNotNull(patchContext.getInstanceIdentifierContext().getInstanceIdentifier());
110         assertNotNull(patchContext.getInstanceIdentifierContext().getSchemaContext());
111         assertNotNull(patchContext.getInstanceIdentifierContext().getSchemaNode());
112     }
113
114     protected static void checkPatchContextMountPoint(final PatchContext patchContext) {
115         checkPatchContext(patchContext);
116         assertNotNull(patchContext.getInstanceIdentifierContext().getMountPoint());
117         assertNotNull(patchContext.getInstanceIdentifierContext().getMountPoint().getSchemaContext());
118     }
119 }