Eliminate use of SchemaService
[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.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.mdsal.dom.api.DOMSchemaService;
26 import org.opendaylight.netconf.sal.rest.doc.impl.MountPointSwaggerGeneratorDraft02;
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 INSTANCE_ID = 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     @SuppressWarnings("resource")
50     @Before
51     public void setUp() throws Exception {
52         this.helper = new DocGenTestHelper();
53         this.helper.setUp();
54         this.schemaContext = this.helper.getSchemaContext();
55
56         // We are sharing the global schema service and the mount schema service
57         // in our test.
58         // OK for testing - real thing would have seperate instances.
59         final SchemaContext context = this.helper.createMockSchemaContext();
60         final DOMSchemaService schemaService = this.helper.createMockSchemaService(context);
61
62         final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
63         when(mountPoint.getSchemaContext()).thenReturn(context);
64
65         final DOMMountPointService service = mock(DOMMountPointService.class);
66         when(service.getMountPoint(INSTANCE_ID)).thenReturn(Optional.of(mountPoint));
67
68         MountPointSwaggerGeneratorDraft02 generator = new MountPointSwaggerGeneratorDraft02(schemaService, service);
69         this.swagger = generator.getMountPointSwagger();
70     }
71
72     @Test()
73     public void testGetResourceListBadIid() throws Exception {
74         final UriInfo mockInfo = this.helper.createMockUriInfo(HTTP_URL);
75
76         assertEquals(null, this.swagger.getResourceList(mockInfo, 1L));
77     }
78
79     @Test()
80     public void getInstanceIdentifiers() throws Exception {
81         assertEquals(0, this.swagger.getInstanceIdentifiers().size());
82         this.swagger.onMountPointCreated(INSTANCE_ID); // add this ID into the list of
83                                                  // mount points
84         assertEquals(1, this.swagger.getInstanceIdentifiers().size());
85         assertEquals((Long) 1L, this.swagger.getInstanceIdentifiers().entrySet().iterator().next()
86                 .getValue());
87         assertEquals(INSTANCE_URL, this.swagger.getInstanceIdentifiers().entrySet().iterator().next()
88                 .getKey());
89         this.swagger.onMountPointRemoved(INSTANCE_ID); // remove ID from list of mount
90                                                  // points
91         assertEquals(0, this.swagger.getInstanceIdentifiers().size());
92     }
93
94     @Test
95     public void testGetResourceListGoodId() throws Exception {
96         final UriInfo mockInfo = this.helper.createMockUriInfo(HTTP_URL);
97         this.swagger.onMountPointCreated(INSTANCE_ID); // add this ID into the list of
98                                                  // mount points
99         final ResourceList resourceList = this.swagger.getResourceList(mockInfo, 1L);
100
101         Resource dataStoreResource = null;
102         for (final Resource r : resourceList.getApis()) {
103             if (r.getPath().endsWith("/Datastores(-)")) {
104                 dataStoreResource = r;
105             }
106         }
107         assertNotNull("Failed to find data store resource", dataStoreResource);
108     }
109
110     @Test
111     public void testGetDataStoreApi() throws Exception {
112         final UriInfo mockInfo = this.helper.createMockUriInfo(HTTP_URL);
113         this.swagger.onMountPointCreated(INSTANCE_ID); // add this ID into the list of
114                                                  // mount points
115
116         final ApiDeclaration mountPointApi = this.swagger.getMountPointApi(mockInfo, 1L, "Datastores", "-");
117         assertNotNull("failed to find Datastore API", mountPointApi);
118         final List<Api> apis = mountPointApi.getApis();
119         assertEquals("Unexpected api list size", 3, apis.size());
120
121         final Set<String> actualApis = new TreeSet<>();
122         for (final Api api : apis) {
123             actualApis.add(api.getPath());
124             final List<Operation> operations = api.getOperations();
125             assertEquals("unexpected operation size on " + api.getPath(), 1, operations.size());
126             assertEquals("unexpected operation method " + api.getPath(), "GET", operations.get(0)
127                     .getMethod());
128             assertNotNull("expected non-null desc on " + api.getPath(), operations.get(0)
129                     .getNotes());
130         }
131         final Set<String> expectedApis = new TreeSet<>(Arrays.asList(new String[] {
132             "/restconf/config" + INSTANCE_URL + "yang-ext:mount",
133             "/restconf/operational" + INSTANCE_URL + "yang-ext:mount",
134             "/restconf/operations" + INSTANCE_URL + "yang-ext:mount",}));
135         assertEquals(expectedApis, actualApis);
136     }
137 }