Merge "Fixed for bug 1197"
[controller.git] / opendaylight / md-sal / 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 java.net.URISyntaxException;
16 import java.util.Arrays;
17 import java.util.List;
18 import java.util.Set;
19 import java.util.TreeSet;
20
21 import javax.ws.rs.core.UriInfo;
22
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.controller.sal.core.api.model.SchemaService;
26 import org.opendaylight.controller.sal.core.api.mount.MountProvisionInstance;
27 import org.opendaylight.controller.sal.core.api.mount.MountProvisionService;
28 import org.opendaylight.controller.sal.rest.doc.mountpoints.MountPointSwagger;
29 import org.opendaylight.controller.sal.rest.doc.swagger.Api;
30 import org.opendaylight.controller.sal.rest.doc.swagger.ApiDeclaration;
31 import org.opendaylight.controller.sal.rest.doc.swagger.Operation;
32 import org.opendaylight.controller.sal.rest.doc.swagger.Resource;
33 import org.opendaylight.controller.sal.rest.doc.swagger.ResourceList;
34 import org.opendaylight.yangtools.yang.common.QName;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37
38 public class MountPointSwaggerTest {
39
40     private static final String HTTP_URL = "http://localhost/path";
41     private static final YangInstanceIdentifier instanceId = YangInstanceIdentifier.builder()
42             .node(QName.create("nodes"))
43             .nodeWithKey(QName.create("node"), QName.create("id"), "123").build();
44     private static final String INSTANCE_URL = "nodes/node/123/";
45     private MountPointSwagger swagger;
46     private DocGenTestHelper helper;
47
48     @Before
49     public void setUp() throws Exception {
50         swagger = new MountPointSwagger();
51         helper = new DocGenTestHelper();
52         helper.setUp();
53     }
54
55     @Test()
56     public void testGetResourceListBadIid() throws Exception {
57         UriInfo mockInfo = helper.createMockUriInfo(HTTP_URL);
58
59         assertEquals(null, swagger.getResourceList(mockInfo, 1L));
60     }
61
62     @Test()
63     public void getInstanceIdentifiers() throws Exception {
64         UriInfo mockInfo = setUpSwaggerForDocGeneration();
65
66         assertEquals(0, swagger.getInstanceIdentifiers().size());
67         swagger.onMountPointCreated(instanceId); // add this ID into the list of
68                                                  // mount points
69         assertEquals(1, swagger.getInstanceIdentifiers().size());
70         assertEquals((Long) 1L, swagger.getInstanceIdentifiers().entrySet().iterator().next()
71                 .getValue());
72         assertEquals(INSTANCE_URL, swagger.getInstanceIdentifiers().entrySet().iterator().next()
73                 .getKey());
74         swagger.onMountPointRemoved(instanceId); // remove ID from list of mount
75                                                  // points
76         assertEquals(0, swagger.getInstanceIdentifiers().size());
77     }
78
79     @Test
80     public void testGetResourceListGoodId() throws Exception {
81         UriInfo mockInfo = setUpSwaggerForDocGeneration();
82         swagger.onMountPointCreated(instanceId); // add this ID into the list of
83                                                  // mount points
84         ResourceList resourceList = swagger.getResourceList(mockInfo, 1L);
85
86         Resource dataStoreResource = null;
87         for (Resource r : resourceList.getApis()) {
88             if (r.getPath().endsWith("/Datastores(-)")) {
89                 dataStoreResource = r;
90             }
91         }
92         assertNotNull("Failed to find data store resource", dataStoreResource);
93     }
94
95     @Test
96     public void testGetDataStoreApi() throws Exception {
97         UriInfo mockInfo = setUpSwaggerForDocGeneration();
98         swagger.onMountPointCreated(instanceId); // add this ID into the list of
99                                                  // mount points
100         ApiDeclaration mountPointApi = swagger.getMountPointApi(mockInfo, 1L, "Datastores", "-");
101         assertNotNull("failed to find Datastore API", mountPointApi);
102         List<Api> apis = mountPointApi.getApis();
103         assertEquals("Unexpected api list size", 3, apis.size());
104
105         Set<String> actualApis = new TreeSet<>();
106         for (Api api : apis) {
107             actualApis.add(api.getPath());
108             List<Operation> operations = api.getOperations();
109             assertEquals("unexpected operation size on " + api.getPath(), 1, operations.size());
110             assertEquals("unexpected operation method " + api.getPath(), "GET", operations.get(0)
111                     .getMethod());
112             assertNotNull("expected non-null desc on " + api.getPath(), operations.get(0)
113                     .getNotes());
114         }
115         Set<String> expectedApis = new TreeSet<>(Arrays.asList(new String[] {
116                 "/config/" + INSTANCE_URL + "yang-ext:mount/",
117                 "/operational/" + INSTANCE_URL + "yang-ext:mount/",
118                 "/operations/" + INSTANCE_URL + "yang-ext:mount/", }));
119         assertEquals(expectedApis, actualApis);
120     }
121
122     protected UriInfo setUpSwaggerForDocGeneration() throws URISyntaxException {
123         UriInfo mockInfo = helper.createMockUriInfo(HTTP_URL);
124         // We are sharing the global schema service and the mount schema service
125         // in our test.
126         // OK for testing - real thing would have seperate instances.
127         SchemaContext context = helper.createMockSchemaContext();
128         SchemaService schemaService = helper.createMockSchemaService(context);
129
130         MountProvisionInstance mountPoint = mock(MountProvisionInstance.class);
131         when(mountPoint.getSchemaContext()).thenReturn(context);
132
133         MountProvisionService service = mock(MountProvisionService.class);
134         when(service.getMountPoint(instanceId)).thenReturn(mountPoint);
135         swagger.setMountService(service);
136         swagger.setGlobalSchema(schemaService);
137
138         return mockInfo;
139     }
140
141 }