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 com.google.common.base.Optional;
16 import java.net.URISyntaxException;
17 import java.util.Arrays;
18 import java.util.HashSet;
19 import java.util.List;
21 import java.util.TreeSet;
22 import javax.ws.rs.core.UriInfo;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
26 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
27 import org.opendaylight.controller.sal.core.api.model.SchemaService;
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.Module;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
40 public class MountPointSwaggerTest {
42 private static final String HTTP_URL = "http://localhost/path";
43 private static final YangInstanceIdentifier instanceId = YangInstanceIdentifier.builder()
44 .node(QName.create("nodes"))
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;
52 public void setUp() throws Exception {
53 swagger = new MountPointSwagger();
54 helper = new DocGenTestHelper();
56 schemaContext = new YangParserImpl().resolveSchemaContext(new HashSet<Module>(helper.getModules().values()));
60 public void testGetResourceListBadIid() throws Exception {
61 UriInfo mockInfo = helper.createMockUriInfo(HTTP_URL);
63 assertEquals(null, swagger.getResourceList(mockInfo, 1L));
67 public void getInstanceIdentifiers() throws Exception {
68 UriInfo mockInfo = setUpSwaggerForDocGeneration();
70 assertEquals(0, swagger.getInstanceIdentifiers().size());
71 swagger.onMountPointCreated(instanceId); // add this ID into the list of
73 assertEquals(1, swagger.getInstanceIdentifiers().size());
74 assertEquals((Long) 1L, swagger.getInstanceIdentifiers().entrySet().iterator().next()
76 assertEquals(INSTANCE_URL, swagger.getInstanceIdentifiers().entrySet().iterator().next()
78 swagger.onMountPointRemoved(instanceId); // remove ID from list of mount
80 assertEquals(0, swagger.getInstanceIdentifiers().size());
84 public void testGetResourceListGoodId() throws Exception {
85 UriInfo mockInfo = setUpSwaggerForDocGeneration();
86 swagger.onMountPointCreated(instanceId); // add this ID into the list of
88 ResourceList resourceList = swagger.getResourceList(mockInfo, 1L);
90 Resource dataStoreResource = null;
91 for (Resource r : resourceList.getApis()) {
92 if (r.getPath().endsWith("/Datastores(-)")) {
93 dataStoreResource = r;
96 assertNotNull("Failed to find data store resource", dataStoreResource);
100 public void testGetDataStoreApi() throws Exception {
101 UriInfo mockInfo = setUpSwaggerForDocGeneration();
102 swagger.onMountPointCreated(instanceId); // add this ID into the list of
104 ApiDeclaration mountPointApi = swagger.getMountPointApi(mockInfo, 1L, "Datastores", "-");
105 assertNotNull("failed to find Datastore API", mountPointApi);
106 List<Api> apis = mountPointApi.getApis();
107 assertEquals("Unexpected api list size", 3, apis.size());
109 Set<String> actualApis = new TreeSet<>();
110 for (Api api : apis) {
111 actualApis.add(api.getPath());
112 List<Operation> operations = api.getOperations();
113 assertEquals("unexpected operation size on " + api.getPath(), 1, operations.size());
114 assertEquals("unexpected operation method " + api.getPath(), "GET", operations.get(0)
116 assertNotNull("expected non-null desc on " + api.getPath(), operations.get(0)
119 Set<String> expectedApis = new TreeSet<>(Arrays.asList(new String[] {
120 "/config/" + INSTANCE_URL + "yang-ext:mount/",
121 "/operational/" + INSTANCE_URL + "yang-ext:mount/",
122 "/operations/" + INSTANCE_URL + "yang-ext:mount/", }));
123 assertEquals(expectedApis, actualApis);
126 protected UriInfo setUpSwaggerForDocGeneration() throws URISyntaxException {
127 UriInfo mockInfo = helper.createMockUriInfo(HTTP_URL);
128 // We are sharing the global schema service and the mount schema service
130 // OK for testing - real thing would have seperate instances.
131 SchemaContext context = helper.createMockSchemaContext();
132 SchemaService schemaService = helper.createMockSchemaService(context);
134 DOMMountPoint mountPoint = mock(DOMMountPoint.class);
135 when(mountPoint.getSchemaContext()).thenReturn(context);
137 DOMMountPointService service = mock(DOMMountPointService.class);
138 when(service.getMountPoint(instanceId)).thenReturn(Optional.of(mountPoint));
139 swagger.setMountService(service);
140 swagger.setGlobalSchema(schemaService);