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.List;
19 import java.util.TreeSet;
21 import javax.ws.rs.core.UriInfo;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.controller.sal.core.api.model.SchemaService;
26 import org.opendaylight.controller.sal.core.api.mount.MountProvisionInstance;
27 import org.opendaylight.controller.sal.core.api.mount.MountProvisionService;
28 import org.opendaylight.controller.sal.rest.doc.mountpoints.MountPointSwagger;
29 import org.opendaylight.controller.sal.rest.doc.swagger.Api;
30 import org.opendaylight.controller.sal.rest.doc.swagger.ApiDeclaration;
31 import org.opendaylight.controller.sal.rest.doc.swagger.Operation;
32 import org.opendaylight.controller.sal.rest.doc.swagger.Resource;
33 import org.opendaylight.controller.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.SchemaContext;
38 public class MountPointSwaggerTest {
40 private static final String HTTP_URL = "http://localhost/path";
41 private static final YangInstanceIdentifier instanceId = YangInstanceIdentifier.builder()
42 .node(QName.create("nodes"))
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;
49 public void setUp() throws Exception {
50 swagger = new MountPointSwagger();
51 helper = new DocGenTestHelper();
56 public void testGetResourceListBadIid() throws Exception {
57 UriInfo mockInfo = helper.createMockUriInfo(HTTP_URL);
59 assertEquals(null, swagger.getResourceList(mockInfo, 1L));
63 public void getInstanceIdentifiers() throws Exception {
64 UriInfo mockInfo = setUpSwaggerForDocGeneration();
66 assertEquals(0, swagger.getInstanceIdentifiers().size());
67 swagger.onMountPointCreated(instanceId); // add this ID into the list of
69 assertEquals(1, swagger.getInstanceIdentifiers().size());
70 assertEquals((Long) 1L, swagger.getInstanceIdentifiers().entrySet().iterator().next()
72 assertEquals(INSTANCE_URL, swagger.getInstanceIdentifiers().entrySet().iterator().next()
74 swagger.onMountPointRemoved(instanceId); // remove ID from list of mount
76 assertEquals(0, swagger.getInstanceIdentifiers().size());
80 public void testGetResourceListGoodId() throws Exception {
81 UriInfo mockInfo = setUpSwaggerForDocGeneration();
82 swagger.onMountPointCreated(instanceId); // add this ID into the list of
84 ResourceList resourceList = swagger.getResourceList(mockInfo, 1L);
86 Resource dataStoreResource = null;
87 for (Resource r : resourceList.getApis()) {
88 if (r.getPath().endsWith("/Datastores(-)")) {
89 dataStoreResource = r;
92 assertNotNull("Failed to find data store resource", dataStoreResource);
96 public void testGetDataStoreApi() throws Exception {
97 UriInfo mockInfo = setUpSwaggerForDocGeneration();
98 swagger.onMountPointCreated(instanceId); // add this ID into the list of
100 ApiDeclaration mountPointApi = swagger.getMountPointApi(mockInfo, 1L, "Datastores", "-");
101 assertNotNull("failed to find Datastore API", mountPointApi);
102 List<Api> apis = mountPointApi.getApis();
103 assertEquals("Unexpected api list size", 3, apis.size());
105 Set<String> actualApis = new TreeSet<>();
106 for (Api api : apis) {
107 actualApis.add(api.getPath());
108 List<Operation> operations = api.getOperations();
109 assertEquals("unexpected operation size on " + api.getPath(), 1, operations.size());
110 assertEquals("unexpected operation method " + api.getPath(), "GET", operations.get(0)
112 assertNotNull("expected non-null desc on " + api.getPath(), operations.get(0)
115 Set<String> expectedApis = new TreeSet<>(Arrays.asList(new String[] {
116 "/config/" + INSTANCE_URL + "yang-ext:mount/",
117 "/operational/" + INSTANCE_URL + "yang-ext:mount/",
118 "/operations/" + INSTANCE_URL + "yang-ext:mount/", }));
119 assertEquals(expectedApis, actualApis);
122 protected UriInfo setUpSwaggerForDocGeneration() throws URISyntaxException {
123 UriInfo mockInfo = helper.createMockUriInfo(HTTP_URL);
124 // We are sharing the global schema service and the mount schema service
126 // OK for testing - real thing would have seperate instances.
127 SchemaContext context = helper.createMockSchemaContext();
128 SchemaService schemaService = helper.createMockSchemaService(context);
130 MountProvisionInstance mountPoint = mock(MountProvisionInstance.class);
131 when(mountPoint.getSchemaContext()).thenReturn(context);
133 MountProvisionService service = mock(MountProvisionService.class);
134 when(service.getMountPoint(instanceId)).thenReturn(mountPoint);
135 swagger.setMountService(service);
136 swagger.setGlobalSchema(schemaService);