Fix invalid swagger schema
[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 javax.ws.rs.core.UriInfo;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
28 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
29 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
30 import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocServiceImpl.OAversion;
31 import org.opendaylight.netconf.sal.rest.doc.mountpoints.MountPointSwagger;
32 import org.opendaylight.netconf.sal.rest.doc.swagger.OpenApiObject;
33 import org.opendaylight.netconf.sal.rest.doc.swagger.SwaggerObject;
34 import org.opendaylight.yangtools.yang.common.QName;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36
37 public final class MountPointSwaggerTest extends AbstractApiDocTest {
38     private static final String HTTP_URL = "http://localhost/path";
39     private static final YangInstanceIdentifier INSTANCE_ID = YangInstanceIdentifier.builder()
40             .node(QName.create("", "nodes"))
41             .node(QName.create("", "node"))
42             .nodeWithKey(QName.create("", "node"), QName.create("", "id"), "123").build();
43     private static final String INSTANCE_URL = "/nodes/node=123/";
44
45     private MountPointSwagger swagger;
46
47     @Before
48     public void before() {
49         // We are sharing the global schema service and the mount schema service
50         // in our test.
51         // OK for testing - real thing would have separate instances.
52         final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
53         when(mountPoint.getService(DOMSchemaService.class)).thenReturn(Optional.of(SCHEMA_SERVICE));
54
55         final DOMMountPointService service = mock(DOMMountPointService.class);
56         when(service.getMountPoint(INSTANCE_ID)).thenReturn(Optional.of(mountPoint));
57
58         swagger = new MountPointSwaggerGeneratorRFC8040(SCHEMA_SERVICE, service).getMountPointSwagger();
59     }
60
61     @Test()
62     public void getInstanceIdentifiers() {
63         assertEquals(0, swagger.getInstanceIdentifiers().size());
64         swagger.onMountPointCreated(INSTANCE_ID); // add this ID into the list of mount points
65         assertEquals(1, swagger.getInstanceIdentifiers().size());
66         assertEquals((Long) 1L, swagger.getInstanceIdentifiers().entrySet().iterator().next()
67                 .getValue());
68         assertEquals(INSTANCE_URL, swagger.getInstanceIdentifiers().entrySet().iterator().next()
69                 .getKey());
70         swagger.onMountPointRemoved(INSTANCE_ID); // remove ID from list of mount points
71         assertEquals(0, swagger.getInstanceIdentifiers().size());
72     }
73
74     @Test
75     public void testGetDataStoreApi() throws Exception {
76         final UriInfo mockInfo = DocGenTestHelper.createMockUriInfo(HTTP_URL);
77         swagger.onMountPointCreated(INSTANCE_ID); // add this ID into the list of mount points
78
79         final SwaggerObject mountPointApi = (SwaggerObject) swagger.getMountPointApi(mockInfo, 1L, "Datastores", "-",
80             OAversion.V2_0);
81         assertNotNull("failed to find Datastore API", mountPointApi);
82
83         final ObjectNode pathsObject = mountPointApi.getPaths();
84         assertNotNull(pathsObject);
85
86         assertEquals("Unexpected api list size", 2, pathsObject.size());
87
88         final Set<String> actualUrls = new TreeSet<>();
89
90         final Iterator<Map.Entry<String, JsonNode>> fields = pathsObject.fields();
91         while (fields.hasNext()) {
92             final Map.Entry<String, JsonNode> field = fields.next();
93             final String path = field.getKey();
94             final JsonNode operations = field.getValue();
95             actualUrls.add(field.getKey());
96             assertEquals("unexpected operations size on " + path, 1, operations.size());
97
98             final JsonNode getOperation = operations.get("get");
99
100             assertNotNull("unexpected operation method on " + path, getOperation);
101
102             assertNotNull("expected non-null desc on " + path, getOperation.get("description"));
103         }
104
105         assertEquals(Set.of("/rests/data" + INSTANCE_URL + "yang-ext:mount",
106             "/rests/operations" + INSTANCE_URL + "yang-ext:mount"), actualUrls);
107     }
108
109     /**
110      * Test that request parameters are correctly numbered.
111      *
112      * <p>
113      * It means we should have name and name1, etc. when we have the same parameter in path multiple times.
114      */
115     @Test
116     public void testParametersNumberingForMountPointApi() throws Exception {
117         final UriInfo mockInfo = DocGenTestHelper.createMockUriInfo(HTTP_URL);
118         swagger.onMountPointCreated(INSTANCE_ID);
119
120         final OpenApiObject mountPointApi = (OpenApiObject) swagger.getMountPointApi(mockInfo, 1L, Optional.empty(),
121                 OAversion.V3_0);
122         assertNotNull("Failed to find Datastore API", mountPointApi);
123
124         var pathToList1 = "/rests/data/nodes/node=123/yang-ext:mount/path-params-test:cont/list1={name}";
125         assertTrue(mountPointApi.getPaths().has(pathToList1));
126         assertEquals(List.of("name"), getPathParameters(mountPointApi.getPaths(), pathToList1));
127
128         var pathToList2 = "/rests/data/nodes/node=123/yang-ext:mount/path-params-test:cont/list1={name}/list2={name1}";
129         assertTrue(mountPointApi.getPaths().has(pathToList2));
130         assertEquals(List.of("name", "name1"), getPathParameters(mountPointApi.getPaths(), pathToList2));
131
132         var pathToList3 = "/rests/data/nodes/node=123/yang-ext:mount/path-params-test:cont/list3={name}";
133         assertTrue(mountPointApi.getPaths().has(pathToList3));
134         assertEquals(List.of("name"), getPathParameters(mountPointApi.getPaths(), pathToList3));
135     }
136 }