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 .node(QName.create("node"))
46 .nodeWithKey(QName.create("node"), QName.create("id"), "123").build();
47 private static final String INSTANCE_URL = "nodes/node/123/";
48 private MountPointSwagger swagger;
49 private DocGenTestHelper helper;
50 private SchemaContext schemaContext;
53 public void setUp() throws Exception {
54 swagger = new MountPointSwagger();
55 helper = new DocGenTestHelper();
57 schemaContext = new YangParserImpl().resolveSchemaContext(new HashSet<Module>(helper.getModules().values()));
61 public void testGetResourceListBadIid() throws Exception {
62 UriInfo mockInfo = helper.createMockUriInfo(HTTP_URL);
64 assertEquals(null, swagger.getResourceList(mockInfo, 1L));
68 public void getInstanceIdentifiers() throws Exception {
69 UriInfo mockInfo = setUpSwaggerForDocGeneration();
71 assertEquals(0, swagger.getInstanceIdentifiers().size());
72 swagger.onMountPointCreated(instanceId); // add this ID into the list of
74 assertEquals(1, swagger.getInstanceIdentifiers().size());
75 assertEquals((Long) 1L, swagger.getInstanceIdentifiers().entrySet().iterator().next()
77 assertEquals(INSTANCE_URL, swagger.getInstanceIdentifiers().entrySet().iterator().next()
79 swagger.onMountPointRemoved(instanceId); // remove ID from list of mount
81 assertEquals(0, swagger.getInstanceIdentifiers().size());
85 public void testGetResourceListGoodId() throws Exception {
86 UriInfo mockInfo = setUpSwaggerForDocGeneration();
87 swagger.onMountPointCreated(instanceId); // add this ID into the list of
89 ResourceList resourceList = swagger.getResourceList(mockInfo, 1L);
91 Resource dataStoreResource = null;
92 for (Resource r : resourceList.getApis()) {
93 if (r.getPath().endsWith("/Datastores(-)")) {
94 dataStoreResource = r;
97 assertNotNull("Failed to find data store resource", dataStoreResource);
101 public void testGetDataStoreApi() throws Exception {
102 UriInfo mockInfo = setUpSwaggerForDocGeneration();
103 swagger.onMountPointCreated(instanceId); // add this ID into the list of
105 ApiDeclaration mountPointApi = swagger.getMountPointApi(mockInfo, 1L, "Datastores", "-");
106 assertNotNull("failed to find Datastore API", mountPointApi);
107 List<Api> apis = mountPointApi.getApis();
108 assertEquals("Unexpected api list size", 3, apis.size());
110 Set<String> actualApis = new TreeSet<>();
111 for (Api api : apis) {
112 actualApis.add(api.getPath());
113 List<Operation> operations = api.getOperations();
114 assertEquals("unexpected operation size on " + api.getPath(), 1, operations.size());
115 assertEquals("unexpected operation method " + api.getPath(), "GET", operations.get(0)
117 assertNotNull("expected non-null desc on " + api.getPath(), operations.get(0)
120 Set<String> expectedApis = new TreeSet<>(Arrays.asList(new String[] {
121 "/config/" + INSTANCE_URL + "yang-ext:mount/",
122 "/operational/" + INSTANCE_URL + "yang-ext:mount/",
123 "/operations/" + INSTANCE_URL + "yang-ext:mount/", }));
124 assertEquals(expectedApis, actualApis);
127 protected UriInfo setUpSwaggerForDocGeneration() throws URISyntaxException {
128 UriInfo mockInfo = helper.createMockUriInfo(HTTP_URL);
129 // We are sharing the global schema service and the mount schema service
131 // OK for testing - real thing would have seperate instances.
132 SchemaContext context = helper.createMockSchemaContext();
133 SchemaService schemaService = helper.createMockSchemaService(context);
135 DOMMountPoint mountPoint = mock(DOMMountPoint.class);
136 when(mountPoint.getSchemaContext()).thenReturn(context);
138 DOMMountPointService service = mock(DOMMountPointService.class);
139 when(service.getMountPoint(instanceId)).thenReturn(Optional.of(mountPoint));
140 swagger.setMountService(service);
141 swagger.setGlobalSchema(schemaService);