2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.rest.doc.impl;
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;
15 import java.net.URISyntaxException;
16 import java.util.Arrays;
17 import java.util.HashSet;
18 import java.util.List;
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.sal.core.api.model.SchemaService;
25 import org.opendaylight.controller.sal.core.api.mount.MountProvisionInstance;
26 import org.opendaylight.controller.sal.core.api.mount.MountProvisionService;
27 import org.opendaylight.controller.sal.rest.doc.mountpoints.MountPointSwagger;
28 import org.opendaylight.controller.sal.rest.doc.swagger.Api;
29 import org.opendaylight.controller.sal.rest.doc.swagger.ApiDeclaration;
30 import org.opendaylight.controller.sal.rest.doc.swagger.Operation;
31 import org.opendaylight.controller.sal.rest.doc.swagger.Resource;
32 import org.opendaylight.controller.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.Module;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
39 public class MountPointSwaggerTest {
41 private static final String HTTP_URL = "http://localhost/path";
42 private static final YangInstanceIdentifier instanceId = YangInstanceIdentifier.builder()
43 .node(QName.create("nodes"))
44 .nodeWithKey(QName.create("node"), QName.create("id"), "123").build();
45 private static final String INSTANCE_URL = "nodes/node/123/";
46 private MountPointSwagger swagger;
47 private DocGenTestHelper helper;
48 private SchemaContext schemaContext;
51 public void setUp() throws Exception {
52 swagger = new MountPointSwagger();
53 helper = new DocGenTestHelper();
55 schemaContext = new YangParserImpl().resolveSchemaContext(new HashSet<Module>(helper.getModules().values()));
59 public void testGetResourceListBadIid() throws Exception {
60 UriInfo mockInfo = helper.createMockUriInfo(HTTP_URL);
62 assertEquals(null, swagger.getResourceList(mockInfo, 1L));
66 public void getInstanceIdentifiers() throws Exception {
67 UriInfo mockInfo = setUpSwaggerForDocGeneration();
69 assertEquals(0, swagger.getInstanceIdentifiers().size());
70 swagger.onMountPointCreated(instanceId); // add this ID into the list of
72 assertEquals(1, swagger.getInstanceIdentifiers().size());
73 assertEquals((Long) 1L, swagger.getInstanceIdentifiers().entrySet().iterator().next()
75 assertEquals(INSTANCE_URL, swagger.getInstanceIdentifiers().entrySet().iterator().next()
77 swagger.onMountPointRemoved(instanceId); // remove ID from list of mount
79 assertEquals(0, swagger.getInstanceIdentifiers().size());
83 public void testGetResourceListGoodId() throws Exception {
84 UriInfo mockInfo = setUpSwaggerForDocGeneration();
85 swagger.onMountPointCreated(instanceId); // add this ID into the list of
87 ResourceList resourceList = swagger.getResourceList(mockInfo, 1L);
89 Resource dataStoreResource = null;
90 for (Resource r : resourceList.getApis()) {
91 if (r.getPath().endsWith("/Datastores(-)")) {
92 dataStoreResource = r;
95 assertNotNull("Failed to find data store resource", dataStoreResource);
99 public void testGetDataStoreApi() throws Exception {
100 UriInfo mockInfo = setUpSwaggerForDocGeneration();
101 swagger.onMountPointCreated(instanceId); // add this ID into the list of
103 ApiDeclaration mountPointApi = swagger.getMountPointApi(mockInfo, 1L, "Datastores", "-");
104 assertNotNull("failed to find Datastore API", mountPointApi);
105 List<Api> apis = mountPointApi.getApis();
106 assertEquals("Unexpected api list size", 3, apis.size());
108 Set<String> actualApis = new TreeSet<>();
109 for (Api api : apis) {
110 actualApis.add(api.getPath());
111 List<Operation> operations = api.getOperations();
112 assertEquals("unexpected operation size on " + api.getPath(), 1, operations.size());
113 assertEquals("unexpected operation method " + api.getPath(), "GET", operations.get(0)
115 assertNotNull("expected non-null desc on " + api.getPath(), operations.get(0)
118 Set<String> expectedApis = new TreeSet<>(Arrays.asList(new String[] {
119 "/config/" + INSTANCE_URL + "yang-ext:mount/",
120 "/operational/" + INSTANCE_URL + "yang-ext:mount/",
121 "/operations/" + INSTANCE_URL + "yang-ext:mount/", }));
122 assertEquals(expectedApis, actualApis);
125 protected UriInfo setUpSwaggerForDocGeneration() throws URISyntaxException {
126 UriInfo mockInfo = helper.createMockUriInfo(HTTP_URL);
127 // We are sharing the global schema service and the mount schema service
129 // OK for testing - real thing would have seperate instances.
130 SchemaContext context = helper.createMockSchemaContext();
131 SchemaService schemaService = helper.createMockSchemaService(context);
133 MountProvisionInstance mountPoint = mock(MountProvisionInstance.class);
134 when(mountPoint.getSchemaContext()).thenReturn(context);
136 MountProvisionService service = mock(MountProvisionService.class);
137 when(service.getMountPoint(instanceId)).thenReturn(mountPoint);
138 swagger.setMountService(service);
139 swagger.setGlobalSchema(schemaService);