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;
14 import com.google.common.base.Optional;
15 import java.net.URISyntaxException;
16 import java.util.Arrays;
17 import java.util.List;
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.controller.md.sal.dom.api.DOMMountPoint;
24 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
25 import org.opendaylight.controller.sal.core.api.model.SchemaService;
26 import org.opendaylight.netconf.sal.rest.doc.mountpoints.MountPointSwagger;
27 import org.opendaylight.netconf.sal.rest.doc.swagger.Api;
28 import org.opendaylight.netconf.sal.rest.doc.swagger.ApiDeclaration;
29 import org.opendaylight.netconf.sal.rest.doc.swagger.Operation;
30 import org.opendaylight.netconf.sal.rest.doc.swagger.Resource;
31 import org.opendaylight.netconf.sal.rest.doc.swagger.ResourceList;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 public class MountPointSwaggerTest {
38 private static final String HTTP_URL = "http://localhost/path";
39 private static final YangInstanceIdentifier instanceId = YangInstanceIdentifier.builder()
40 .node(QName.create("nodes"))
41 .node(QName.create("node"))
42 .nodeWithKey(QName.create("node"), QName.create("id"), "123").build();
43 private static final String INSTANCE_URL = "nodes/node/123/";
44 private MountPointSwagger swagger;
45 private DocGenTestHelper helper;
46 private SchemaContext schemaContext;
49 public void setUp() throws Exception {
50 this.swagger = new MountPointSwagger();
51 this.helper = new DocGenTestHelper();
53 this.schemaContext = this.helper.getSchemaContext();
57 public void testGetResourceListBadIid() throws Exception {
58 final UriInfo mockInfo = this.helper.createMockUriInfo(HTTP_URL);
60 assertEquals(null, this.swagger.getResourceList(mockInfo, 1L));
64 public void getInstanceIdentifiers() throws Exception {
65 final UriInfo mockInfo = setUpSwaggerForDocGeneration();
67 assertEquals(0, this.swagger.getInstanceIdentifiers().size());
68 this.swagger.onMountPointCreated(instanceId); // add this ID into the list of
70 assertEquals(1, this.swagger.getInstanceIdentifiers().size());
71 assertEquals((Long) 1L, this.swagger.getInstanceIdentifiers().entrySet().iterator().next()
73 assertEquals(INSTANCE_URL, this.swagger.getInstanceIdentifiers().entrySet().iterator().next()
75 this.swagger.onMountPointRemoved(instanceId); // remove ID from list of mount
77 assertEquals(0, this.swagger.getInstanceIdentifiers().size());
81 public void testGetResourceListGoodId() throws Exception {
82 final UriInfo mockInfo = setUpSwaggerForDocGeneration();
83 this.swagger.onMountPointCreated(instanceId); // add this ID into the list of
85 final ResourceList resourceList = this.swagger.getResourceList(mockInfo, 1L);
87 Resource dataStoreResource = null;
88 for (final Resource r : resourceList.getApis()) {
89 if (r.getPath().endsWith("/Datastores(-)")) {
90 dataStoreResource = r;
93 assertNotNull("Failed to find data store resource", dataStoreResource);
97 public void testGetDataStoreApi() throws Exception {
98 final UriInfo mockInfo = setUpSwaggerForDocGeneration();
99 this.swagger.onMountPointCreated(instanceId); // add this ID into the list of
101 final ApiDeclaration mountPointApi = this.swagger.getMountPointApi(mockInfo, 1L, "Datastores", "-");
102 assertNotNull("failed to find Datastore API", mountPointApi);
103 final List<Api> apis = mountPointApi.getApis();
104 assertEquals("Unexpected api list size", 3, apis.size());
106 final Set<String> actualApis = new TreeSet<>();
107 for (final Api api : apis) {
108 actualApis.add(api.getPath());
109 final List<Operation> operations = api.getOperations();
110 assertEquals("unexpected operation size on " + api.getPath(), 1, operations.size());
111 assertEquals("unexpected operation method " + api.getPath(), "GET", operations.get(0)
113 assertNotNull("expected non-null desc on " + api.getPath(), operations.get(0)
116 final Set<String> expectedApis = new TreeSet<>(Arrays.asList(new String[] {
117 "/config/" + INSTANCE_URL + "yang-ext:mount/",
118 "/operational/" + INSTANCE_URL + "yang-ext:mount/",
119 "/operations/" + INSTANCE_URL + "yang-ext:mount/", }));
120 assertEquals(expectedApis, actualApis);
123 protected UriInfo setUpSwaggerForDocGeneration() throws URISyntaxException {
124 final UriInfo mockInfo = this.helper.createMockUriInfo(HTTP_URL);
125 // We are sharing the global schema service and the mount schema service
127 // OK for testing - real thing would have seperate instances.
128 final SchemaContext context = this.helper.createMockSchemaContext();
129 final SchemaService schemaService = this.helper.createMockSchemaService(context);
131 final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
132 when(mountPoint.getSchemaContext()).thenReturn(context);
134 final DOMMountPointService service = mock(DOMMountPointService.class);
135 when(service.getMountPoint(instanceId)).thenReturn(Optional.of(mountPoint));
136 this.swagger.setMountService(service);
137 this.swagger.setGlobalSchema(schemaService);