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