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