Merge "Migrate netconf to mockito ArgumentMatchers"
[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
9 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.test;
10
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
16
17 import com.google.common.base.Optional;
18 import java.util.Collections;
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.dom.api.DOMMountPoint;
25 import org.opendaylight.controller.md.sal.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.SchemaContext;
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(SchemaContext schemaContext) throws NoSuchFieldException, IllegalAccessException {
44         mediaType = getMediaType();
45
46         schemaContextHandler = TestUtils.newSchemaContextHandler(schemaContext);
47
48         final DOMMountPointService mountPointService = mock(DOMMountPointService.class);
49         final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
50         doReturn(Optional.of(mountPoint)).when(mountPointService).getMountPoint(any(YangInstanceIdentifier.class));
51         doReturn(schemaContext).when(mountPoint).getSchemaContext();
52
53         mountPointServiceHandler = DOMMountPointServiceHandler.newInstance(mountPointService);
54     }
55
56     protected abstract MediaType getMediaType();
57
58     protected static SchemaContext schemaContextLoader(final String yangPath,
59             final SchemaContext schemaContext) {
60         return TestRestconfUtils.loadSchemaContext(yangPath, schemaContext);
61     }
62
63     protected static <T extends AbstractIdentifierAwareJaxRsProvider> void mockBodyReader(
64             final String identifier, final T normalizedNodeProvider,
65             final boolean isPost) throws NoSuchFieldException,
66             SecurityException, IllegalArgumentException, IllegalAccessException {
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, Collections.singletonList(identifier));
72         }
73
74         when(uriInfoMock.getPathParameters()).thenReturn(pathParm);
75         when(uriInfoMock.getPathParameters(false)).thenReturn(pathParm);
76         when(uriInfoMock.getPathParameters(true)).thenReturn(pathParm);
77         normalizedNodeProvider.setUriInfo(uriInfoMock);
78
79         final Request request = mock(Request.class);
80         if (isPost) {
81             when(request.getMethod()).thenReturn("POST");
82         } else {
83             when(request.getMethod()).thenReturn("PUT");
84         }
85
86         normalizedNodeProvider.setRequest(request);
87     }
88
89     protected static void checkMountPointNormalizedNodeContext(
90             final NormalizedNodeContext nnContext) {
91         checkNormalizedNodeContext(nnContext);
92         assertNotNull(nnContext.getInstanceIdentifierContext().getMountPoint());
93     }
94
95     protected static void checkNormalizedNodeContext(
96             final NormalizedNodeContext nnContext) {
97         assertNotNull(nnContext.getData());
98         assertNotNull(nnContext.getInstanceIdentifierContext()
99                 .getInstanceIdentifier());
100         assertNotNull(nnContext.getInstanceIdentifierContext()
101                 .getSchemaContext());
102         assertNotNull(nnContext.getInstanceIdentifierContext().getSchemaNode());
103     }
104
105     protected static void checkPatchContext(final PatchContext patchContext) {
106         assertNotNull(patchContext.getData());
107         assertNotNull(patchContext.getInstanceIdentifierContext().getInstanceIdentifier());
108         assertNotNull(patchContext.getInstanceIdentifierContext().getSchemaContext());
109         assertNotNull(patchContext.getInstanceIdentifierContext().getSchemaNode());
110     }
111
112     protected static void checkPatchContextMountPoint(final PatchContext patchContext) {
113         checkPatchContext(patchContext);
114         assertNotNull(patchContext.getInstanceIdentifierContext().getMountPoint());
115         assertNotNull(patchContext.getInstanceIdentifierContext().getMountPoint().getSchemaContext());
116     }
117 }