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.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.md.sal.dom.api.DOMMountPoint;
25 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
26 import org.opendaylight.controller.sal.core.api.model.SchemaService;
27 import org.opendaylight.netconf.sal.rest.doc.mountpoints.MountPointSwagger;
28 import org.opendaylight.netconf.sal.rest.doc.swagger.Api;
29 import org.opendaylight.netconf.sal.rest.doc.swagger.ApiDeclaration;
30 import org.opendaylight.netconf.sal.rest.doc.swagger.Operation;
31 import org.opendaylight.netconf.sal.rest.doc.swagger.Resource;
32 import org.opendaylight.netconf.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.SchemaContext;
37 public class MountPointSwaggerTest {
39 private static final String HTTP_URL = "http://localhost/path";
40 private static final YangInstanceIdentifier INSTANCE_ID = YangInstanceIdentifier.builder()
41 .node(QName.create("nodes"))
42 .node(QName.create("node"))
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;
47 private SchemaContext schemaContext;
50 public void setUp() throws Exception {
51 this.swagger = new MountPointSwagger();
52 this.helper = new DocGenTestHelper();
54 this.schemaContext = this.helper.getSchemaContext();
58 public void testGetResourceListBadIid() throws Exception {
59 final UriInfo mockInfo = this.helper.createMockUriInfo(HTTP_URL);
61 assertEquals(null, this.swagger.getResourceList(mockInfo, 1L));
65 public void getInstanceIdentifiers() throws Exception {
66 final UriInfo mockInfo = setUpSwaggerForDocGeneration();
68 assertEquals(0, this.swagger.getInstanceIdentifiers().size());
69 this.swagger.onMountPointCreated(INSTANCE_ID); // add this ID into the list of
71 assertEquals(1, this.swagger.getInstanceIdentifiers().size());
72 assertEquals((Long) 1L, this.swagger.getInstanceIdentifiers().entrySet().iterator().next()
74 assertEquals(INSTANCE_URL, this.swagger.getInstanceIdentifiers().entrySet().iterator().next()
76 this.swagger.onMountPointRemoved(INSTANCE_ID); // remove ID from list of mount
78 assertEquals(0, this.swagger.getInstanceIdentifiers().size());
82 public void testGetResourceListGoodId() throws Exception {
83 final UriInfo mockInfo = setUpSwaggerForDocGeneration();
84 this.swagger.onMountPointCreated(INSTANCE_ID); // add this ID into the list of
86 final ResourceList resourceList = this.swagger.getResourceList(mockInfo, 1L);
88 Resource dataStoreResource = null;
89 for (final Resource r : resourceList.getApis()) {
90 if (r.getPath().endsWith("/Datastores(-)")) {
91 dataStoreResource = r;
94 assertNotNull("Failed to find data store resource", dataStoreResource);
98 public void testGetDataStoreApi() throws Exception {
99 final UriInfo mockInfo = setUpSwaggerForDocGeneration();
100 this.swagger.onMountPointCreated(INSTANCE_ID); // add this ID into the list of
103 final ApiDeclaration mountPointApi = this.swagger.getMountPointApi(mockInfo, 1L, "Datastores", "-");
104 assertNotNull("failed to find Datastore API", mountPointApi);
105 final List<Api> apis = mountPointApi.getApis();
106 assertEquals("Unexpected api list size", 3, apis.size());
108 final Set<String> actualApis = new TreeSet<>();
109 for (final Api api : apis) {
110 actualApis.add(api.getPath());
111 final 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 final 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 final UriInfo mockInfo = this.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 final SchemaContext context = this.helper.createMockSchemaContext();
131 final SchemaService schemaService = this.helper.createMockSchemaService(context);
133 final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
134 when(mountPoint.getSchemaContext()).thenReturn(context);
136 final DOMMountPointService service = mock(DOMMountPointService.class);
137 when(service.getMountPoint(INSTANCE_ID)).thenReturn(Optional.of(mountPoint));
138 this.swagger.setMountService(service);
139 this.swagger.setGlobalSchema(schemaService);