c3ee927658abffd5b1b10abb770f8f209c14efdb
[netconf.git] / restconf / sal-rest-docgen / src / test / java / org / opendaylight / netconf / sal / rest / doc / mountpoints / MountPointOpenApiTest.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.mountpoints;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
14
15 import com.fasterxml.jackson.databind.JsonNode;
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Optional;
20 import java.util.Set;
21 import java.util.TreeSet;
22 import javax.ws.rs.core.UriInfo;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
26 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
27 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
28 import org.opendaylight.netconf.sal.rest.doc.AbstractApiDocTest;
29 import org.opendaylight.netconf.sal.rest.doc.DocGenTestHelper;
30 import org.opendaylight.netconf.sal.rest.doc.impl.MountPointOpenApiGeneratorRFC8040;
31 import org.opendaylight.netconf.sal.rest.doc.openapi.OpenApiObject;
32 import org.opendaylight.netconf.sal.rest.doc.openapi.Path;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35
36 public final class MountPointOpenApiTest extends AbstractApiDocTest {
37     private static final String HTTP_URL = "http://localhost/path";
38     private static final YangInstanceIdentifier INSTANCE_ID = YangInstanceIdentifier.builder()
39             .node(QName.create("", "nodes"))
40             .node(QName.create("", "node"))
41             .nodeWithKey(QName.create("", "node"), QName.create("", "id"), "123").build();
42     private static final String INSTANCE_URL = "/nodes/node=123/";
43
44     private MountPointOpenApi openApi;
45
46     @Before
47     public void before() {
48         // We are sharing the global schema service and the mount schema service
49         // in our test.
50         // OK for testing - real thing would have separate instances.
51         final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
52         when(mountPoint.getService(DOMSchemaService.class)).thenReturn(Optional.of(SCHEMA_SERVICE));
53
54         final DOMMountPointService service = mock(DOMMountPointService.class);
55         when(service.getMountPoint(INSTANCE_ID)).thenReturn(Optional.of(mountPoint));
56
57         openApi = new MountPointOpenApiGeneratorRFC8040(SCHEMA_SERVICE, service).getMountPointOpenApi();
58     }
59
60     @Test()
61     public void getInstanceIdentifiers() {
62         assertEquals(0, openApi.getInstanceIdentifiers().size());
63         openApi.onMountPointCreated(INSTANCE_ID); // add this ID into the list of mount points
64         assertEquals(1, openApi.getInstanceIdentifiers().size());
65         assertEquals((Long) 1L, openApi.getInstanceIdentifiers().entrySet().iterator().next()
66                 .getValue());
67         assertEquals(INSTANCE_URL, openApi.getInstanceIdentifiers().entrySet().iterator().next()
68                 .getKey());
69         openApi.onMountPointRemoved(INSTANCE_ID); // remove ID from list of mount points
70         assertEquals(0, openApi.getInstanceIdentifiers().size());
71     }
72
73     @Test
74     public void testGetDataStoreApi() throws Exception {
75         final UriInfo mockInfo = DocGenTestHelper.createMockUriInfo(HTTP_URL);
76         openApi.onMountPointCreated(INSTANCE_ID); // add this ID into the list of mount points
77
78         final OpenApiObject mountPointApi = openApi.getMountPointApi(mockInfo, 1L, "Datastores", "-");
79         assertNotNull("Failed to find Datastore API", mountPointApi);
80
81         final Map<String, Path> paths = mountPointApi.getPaths();
82         assertNotNull(paths);
83
84         assertEquals("Unexpected api list size", 2, paths.size());
85
86         final Set<String> actualUrls = new TreeSet<>();
87
88         for (final Map.Entry<String, Path> path : paths.entrySet()) {
89             actualUrls.add(path.getKey());
90             final JsonNode getOperation = path.getValue().getGet();
91             assertNotNull("Unexpected operation method on " + path, getOperation);
92             assertNotNull("Expected non-null desc on " + path, getOperation.get("description"));
93         }
94
95         assertEquals(Set.of("/rests/data" + INSTANCE_URL + "yang-ext:mount",
96             "/rests/operations" + INSTANCE_URL + "yang-ext:mount"), actualUrls);
97     }
98
99     /**
100      * Test that creates mount point api with all models from yang folder and checks operation paths for these models.
101      */
102     @Test
103     public void testGetMountPointApi() throws Exception {
104         final UriInfo mockInfo = DocGenTestHelper.createMockUriInfo(HTTP_URL);
105         openApi.onMountPointCreated(INSTANCE_ID);
106
107         final OpenApiObject mountPointApi = openApi.getMountPointApi(mockInfo, 1L, Optional.empty());
108         assertNotNull("Failed to find Datastore API", mountPointApi);
109
110         final Map<String, Path> paths = mountPointApi.getPaths();
111         assertNotNull(paths);
112
113         assertEquals("Unexpected api list size", 26, paths.size());
114
115         final List<JsonNode> getOperations = new ArrayList<>();
116         final List<JsonNode> postOperations = new ArrayList<>();
117         final List<JsonNode> putOperations = new ArrayList<>();
118         final List<JsonNode> patchOperations = new ArrayList<>();
119         final List<JsonNode> deleteOperations = new ArrayList<>();
120
121         for (final Map.Entry<String, Path> path : paths.entrySet()) {
122             Optional.ofNullable(path.getValue().getGet()).ifPresent(getOperations::add);
123             Optional.ofNullable(path.getValue().getPost()).ifPresent(postOperations::add);
124             Optional.ofNullable(path.getValue().getPut()).ifPresent(putOperations::add);
125             Optional.ofNullable(path.getValue().getPatch()).ifPresent(patchOperations::add);
126             Optional.ofNullable(path.getValue().getDelete()).ifPresent(deleteOperations::add);
127         }
128
129         assertEquals("Unexpected GET paths size", 18, getOperations.size());
130         assertEquals("Unexpected POST paths size", 24, postOperations.size());
131         assertEquals("Unexpected PUT paths size", 16, putOperations.size());
132         assertEquals("Unexpected PATCH paths size", 16, patchOperations.size());
133         assertEquals("Unexpected DELETE paths size", 16, deleteOperations.size());
134     }
135 }