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