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