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