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.util.Arrays;
16 import java.util.List;
17 import java.util.Optional;
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.mdsal.dom.api.DOMMountPoint;
24 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
25 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
26 import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocServiceImpl.URIType;
27 import org.opendaylight.netconf.sal.rest.doc.impl.MountPointSwaggerGeneratorDraft02;
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.EffectiveModelContext;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 public class MountPointSwaggerTest {
41 private static final String HTTP_URL = "http://localhost/path";
42 private static final YangInstanceIdentifier INSTANCE_ID = YangInstanceIdentifier.builder()
43 .node(QName.create("", "nodes"))
44 .node(QName.create("", "node"))
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;
51 @SuppressWarnings("resource")
53 public void setUp() throws Exception {
54 this.helper = new DocGenTestHelper();
56 this.schemaContext = this.helper.getSchemaContext();
58 // We are sharing the global schema service and the mount schema service
60 // OK for testing - real thing would have seperate instances.
61 final EffectiveModelContext context = this.helper.createMockSchemaContext();
62 final DOMSchemaService schemaService = this.helper.createMockSchemaService(context);
64 final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
65 when(mountPoint.getSchemaContext()).thenReturn(context);
67 final DOMMountPointService service = mock(DOMMountPointService.class);
68 when(service.getMountPoint(INSTANCE_ID)).thenReturn(Optional.of(mountPoint));
70 MountPointSwaggerGeneratorDraft02 generator = new MountPointSwaggerGeneratorDraft02(schemaService, service);
71 this.swagger = generator.getMountPointSwagger();
75 public void testGetResourceListBadIid() throws Exception {
76 final UriInfo mockInfo = this.helper.createMockUriInfo(HTTP_URL);
78 assertEquals(null, this.swagger.getResourceList(mockInfo, 1L, URIType.DRAFT02));
82 public void getInstanceIdentifiers() throws Exception {
83 assertEquals(0, this.swagger.getInstanceIdentifiers().size());
84 this.swagger.onMountPointCreated(INSTANCE_ID); // add this ID into the list of
86 assertEquals(1, this.swagger.getInstanceIdentifiers().size());
87 assertEquals((Long) 1L, this.swagger.getInstanceIdentifiers().entrySet().iterator().next()
89 assertEquals(INSTANCE_URL, this.swagger.getInstanceIdentifiers().entrySet().iterator().next()
91 this.swagger.onMountPointRemoved(INSTANCE_ID); // remove ID from list of mount
93 assertEquals(0, this.swagger.getInstanceIdentifiers().size());
97 public void testGetResourceListGoodId() throws Exception {
98 final UriInfo mockInfo = this.helper.createMockUriInfo(HTTP_URL);
99 this.swagger.onMountPointCreated(INSTANCE_ID); // add this ID into the list of
101 final ResourceList resourceList = this.swagger.getResourceList(mockInfo, 1L, URIType.DRAFT02);
103 Resource dataStoreResource = null;
104 for (final Resource r : resourceList.getApis()) {
105 if (r.getPath().endsWith("/Datastores(-)")) {
106 dataStoreResource = r;
109 assertNotNull("Failed to find data store resource", dataStoreResource);
113 public void testGetDataStoreApi() throws Exception {
114 final UriInfo mockInfo = this.helper.createMockUriInfo(HTTP_URL);
115 this.swagger.onMountPointCreated(INSTANCE_ID); // add this ID into the list of
118 final ApiDeclaration mountPointApi = this.swagger.getMountPointApi(mockInfo, 1L, "Datastores", "-",
120 assertNotNull("failed to find Datastore API", mountPointApi);
121 final List<Api> apis = mountPointApi.getApis();
122 assertEquals("Unexpected api list size", 3, apis.size());
124 final Set<String> actualApis = new TreeSet<>();
125 for (final Api api : apis) {
126 actualApis.add(api.getPath());
127 final List<Operation> operations = api.getOperations();
128 assertEquals("unexpected operation size on " + api.getPath(), 1, operations.size());
129 assertEquals("unexpected operation method " + api.getPath(), "GET", operations.get(0)
131 assertNotNull("expected non-null desc on " + api.getPath(), operations.get(0)
134 final Set<String> expectedApis = new TreeSet<>(Arrays.asList(new String[] {
135 "/restconf/config" + INSTANCE_URL + "yang-ext:mount",
136 "/restconf/operational" + INSTANCE_URL + "yang-ext:mount",
137 "/restconf/operations" + INSTANCE_URL + "yang-ext:mount",}));
138 assertEquals(expectedApis, actualApis);