Fix deviceName with specific module request
[netconf.git] / restconf / sal-rest-docgen / src / test / java / org / opendaylight / netconf / sal / rest / doc / impl / MountPointSwaggerTest.java
1 /*
2  * Copyright (c) 2014 Brocade Communications 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.netconf.sal.rest.doc.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.when;
15
16 import com.fasterxml.jackson.databind.JsonNode;
17 import com.fasterxml.jackson.databind.node.ObjectNode;
18 import java.util.Iterator;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Optional;
22 import java.util.Set;
23 import java.util.TreeSet;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
27 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
28 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
29 import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocServiceImpl.OAversion;
30 import org.opendaylight.netconf.sal.rest.doc.mountpoints.MountPointSwagger;
31 import org.opendaylight.netconf.sal.rest.doc.swagger.OpenApiObject;
32 import org.opendaylight.netconf.sal.rest.doc.swagger.SwaggerObject;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35
36 public final class MountPointSwaggerTest extends AbstractApiDocTest {
37     private static final YangInstanceIdentifier INSTANCE_ID = YangInstanceIdentifier.builder()
38             .node(QName.create("", "nodes"))
39             .node(QName.create("", "node"))
40             .nodeWithKey(QName.create("", "node"), QName.create("", "id"), "123").build();
41     private static final String INSTANCE_URL = "/nodes/node=123/";
42
43     private MountPointSwagger swagger;
44
45     @Before
46     public void before() {
47         // We are sharing the global schema service and the mount schema service
48         // in our test.
49         // OK for testing - real thing would have separate instances.
50         final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
51         when(mountPoint.getService(DOMSchemaService.class)).thenReturn(Optional.of(SCHEMA_SERVICE));
52
53         final DOMMountPointService service = mock(DOMMountPointService.class);
54         when(service.getMountPoint(INSTANCE_ID)).thenReturn(Optional.of(mountPoint));
55
56         swagger = new MountPointSwaggerGeneratorRFC8040(SCHEMA_SERVICE, service).getMountPointSwagger();
57     }
58
59     @Test()
60     public void getInstanceIdentifiers() {
61         assertEquals(0, swagger.getInstanceIdentifiers().size());
62         swagger.onMountPointCreated(INSTANCE_ID); // add this ID into the list of mount points
63         assertEquals(1, swagger.getInstanceIdentifiers().size());
64         assertEquals((Long) 1L, swagger.getInstanceIdentifiers().entrySet().iterator().next()
65                 .getValue());
66         assertEquals(INSTANCE_URL, swagger.getInstanceIdentifiers().entrySet().iterator().next()
67                 .getKey());
68         swagger.onMountPointRemoved(INSTANCE_ID); // remove ID from list of mount points
69         assertEquals(0, swagger.getInstanceIdentifiers().size());
70     }
71
72     @Test
73     public void testGetDataStoreApi() {
74         swagger.onMountPointCreated(INSTANCE_ID); // add this ID into the list of mount points
75
76         final SwaggerObject mountPointApi = (SwaggerObject) swagger.getMountPointApi(URI_INFO, 1L, "Datastores", "-",
77             OAversion.V2_0);
78         assertNotNull("failed to find Datastore API", mountPointApi);
79
80         final ObjectNode pathsObject = mountPointApi.getPaths();
81         assertNotNull(pathsObject);
82
83         assertEquals("Unexpected api list size", 2, pathsObject.size());
84
85         final Set<String> actualUrls = new TreeSet<>();
86
87         final Iterator<Map.Entry<String, JsonNode>> fields = pathsObject.fields();
88         while (fields.hasNext()) {
89             final Map.Entry<String, JsonNode> field = fields.next();
90             final String path = field.getKey();
91             final JsonNode operations = field.getValue();
92             actualUrls.add(field.getKey());
93             assertEquals("unexpected operations size on " + path, 1, operations.size());
94
95             final JsonNode getOperation = operations.get("get");
96
97             assertNotNull("unexpected operation method on " + path, getOperation);
98
99             assertNotNull("expected non-null desc on " + path, getOperation.get("description"));
100         }
101
102         assertEquals(Set.of("/rests/data" + INSTANCE_URL + "yang-ext:mount",
103             "/rests/operations" + INSTANCE_URL + "yang-ext:mount"), actualUrls);
104     }
105
106     /**
107      * Test that request parameters are correctly numbered.
108      *
109      * <p>
110      * It means we should have name and name1, etc. when we have the same parameter in path multiple times.
111      */
112     @Test
113     public void testParametersNumberingForMountPointApi() {
114         swagger.onMountPointCreated(INSTANCE_ID);
115
116         final OpenApiObject mountPointApi = (OpenApiObject) swagger.getMountPointApi(URI_INFO, 1L, Optional.empty(),
117                 OAversion.V3_0);
118         assertNotNull("Failed to find Datastore API", mountPointApi);
119
120         var pathToList1 = "/rests/data/nodes/node=123/yang-ext:mount/path-params-test:cont/list1={name}";
121         assertTrue(mountPointApi.getPaths().has(pathToList1));
122         assertEquals(List.of("name"), getPathParameters(mountPointApi.getPaths(), pathToList1));
123
124         var pathToList2 = "/rests/data/nodes/node=123/yang-ext:mount/path-params-test:cont/list1={name}/list2={name1}";
125         assertTrue(mountPointApi.getPaths().has(pathToList2));
126         assertEquals(List.of("name", "name1"), getPathParameters(mountPointApi.getPaths(), pathToList2));
127
128         var pathToList3 = "/rests/data/nodes/node=123/yang-ext:mount/path-params-test:cont/list3={name}";
129         assertTrue(mountPointApi.getPaths().has(pathToList3));
130         assertEquals(List.of("name"), getPathParameters(mountPointApi.getPaths(), pathToList3));
131
132         var pathToList4 = "/rests/data/nodes/node=123/yang-ext:mount/path-params-test:cont/list1={name}/list4={name1}";
133         assertTrue(mountPointApi.getPaths().has(pathToList4));
134         assertEquals(List.of("name", "name1"), getPathParameters(mountPointApi.getPaths(), pathToList4));
135
136         var pathToList5 = "/rests/data/nodes/node=123/yang-ext:mount/path-params-test:cont/list1={name}/cont2";
137         assertTrue(mountPointApi.getPaths().has(pathToList5));
138         assertEquals(List.of("name"), getPathParameters(mountPointApi.getPaths(), pathToList5));
139     }
140
141     /**
142      * Test that request for actions is correct and has parameters.
143      */
144     @Test
145     public void testActionPathsParamsForMountPointApi() {
146         swagger.onMountPointCreated(INSTANCE_ID);
147
148         final var mountPointApi = (OpenApiObject) swagger.getMountPointApi(URI_INFO, 1L, Optional.empty(),
149             OAversion.V3_0);
150         assertNotNull("Failed to find Datastore API", mountPointApi);
151
152         final var pathWithParameters =
153             "/rests/operations/nodes/node=123/yang-ext:mount/action-types:list={name}/list-action";
154         assertTrue(mountPointApi.getPaths().has(pathWithParameters));
155         assertEquals(List.of("name"), getPathParameters(mountPointApi.getPaths(), pathWithParameters));
156
157         final var pathWithoutParameters =
158             "/rests/operations/nodes/node=123/yang-ext:mount/action-types:multi-container/inner-container/action";
159         assertTrue(mountPointApi.getPaths().has(pathWithoutParameters));
160         assertEquals(List.of(), getPathParameters(mountPointApi.getPaths(), pathWithoutParameters));
161     }
162 }