1f241da3dd59121dd54e3c4f8947256d4e5b49be
[netconf.git] / restconf / sal-rest-docgen / src / test / java / org / opendaylight / controller / 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.controller.sal.rest.doc.impl;
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 com.fasterxml.jackson.databind.node.ObjectNode;
17 import java.util.Iterator;
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.impl.ApiDocServiceImpl.OAversion;
29 import org.opendaylight.netconf.sal.rest.doc.impl.MountPointSwaggerGeneratorRFC8040;
30 import org.opendaylight.netconf.sal.rest.doc.mountpoints.MountPointSwagger;
31 import org.opendaylight.netconf.sal.rest.doc.swagger.SwaggerObject;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34
35 public final class MountPointSwaggerTest extends AbstractApiDocTest {
36     private static final String HTTP_URL = "http://localhost/path";
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() throws Exception {
74         final UriInfo mockInfo = DocGenTestHelper.createMockUriInfo(HTTP_URL);
75         swagger.onMountPointCreated(INSTANCE_ID); // add this ID into the list of mount points
76
77         final SwaggerObject mountPointApi = (SwaggerObject) swagger.getMountPointApi(mockInfo, 1L, "Datastores", "-",
78             OAversion.V2_0);
79         assertNotNull("failed to find Datastore API", mountPointApi);
80
81         final ObjectNode pathsObject = mountPointApi.getPaths();
82         assertNotNull(pathsObject);
83
84         assertEquals("Unexpected api list size", 2, pathsObject.size());
85
86         final Set<String> actualUrls = new TreeSet<>();
87
88         final Iterator<Map.Entry<String, JsonNode>> fields = pathsObject.fields();
89         while (fields.hasNext()) {
90             final Map.Entry<String, JsonNode> field = fields.next();
91             final String path = field.getKey();
92             final JsonNode operations = field.getValue();
93             actualUrls.add(field.getKey());
94             assertEquals("unexpected operations size on " + path, 1, operations.size());
95
96             final JsonNode getOperation = operations.get("get");
97
98             assertNotNull("unexpected operation method on " + path, getOperation);
99
100             assertNotNull("expected non-null desc on " + path, getOperation.get("description"));
101         }
102
103         assertEquals(Set.of("/rests/data" + INSTANCE_URL + "yang-ext:mount",
104             "/rests/operations" + INSTANCE_URL + "yang-ext:mount"), actualUrls);
105     }
106 }