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.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.mdsal.dom.api.DOMSchemaService;
26 import org.opendaylight.netconf.sal.rest.doc.impl.MountPointSwaggerGeneratorDraft02;
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;
49 @SuppressWarnings("resource")
51 public void setUp() throws Exception {
52 this.helper = new DocGenTestHelper();
54 this.schemaContext = this.helper.getSchemaContext();
56 // We are sharing the global schema service and the mount schema service
58 // OK for testing - real thing would have seperate instances.
59 final SchemaContext context = this.helper.createMockSchemaContext();
60 final DOMSchemaService schemaService = this.helper.createMockSchemaService(context);
62 final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
63 when(mountPoint.getSchemaContext()).thenReturn(context);
65 final DOMMountPointService service = mock(DOMMountPointService.class);
66 when(service.getMountPoint(INSTANCE_ID)).thenReturn(Optional.of(mountPoint));
68 MountPointSwaggerGeneratorDraft02 generator = new MountPointSwaggerGeneratorDraft02(schemaService, service);
69 this.swagger = generator.getMountPointSwagger();
73 public void testGetResourceListBadIid() throws Exception {
74 final UriInfo mockInfo = this.helper.createMockUriInfo(HTTP_URL);
76 assertEquals(null, this.swagger.getResourceList(mockInfo, 1L));
80 public void getInstanceIdentifiers() throws Exception {
81 assertEquals(0, this.swagger.getInstanceIdentifiers().size());
82 this.swagger.onMountPointCreated(INSTANCE_ID); // add this ID into the list of
84 assertEquals(1, this.swagger.getInstanceIdentifiers().size());
85 assertEquals((Long) 1L, this.swagger.getInstanceIdentifiers().entrySet().iterator().next()
87 assertEquals(INSTANCE_URL, this.swagger.getInstanceIdentifiers().entrySet().iterator().next()
89 this.swagger.onMountPointRemoved(INSTANCE_ID); // remove ID from list of mount
91 assertEquals(0, this.swagger.getInstanceIdentifiers().size());
95 public void testGetResourceListGoodId() throws Exception {
96 final UriInfo mockInfo = this.helper.createMockUriInfo(HTTP_URL);
97 this.swagger.onMountPointCreated(INSTANCE_ID); // add this ID into the list of
99 final ResourceList resourceList = this.swagger.getResourceList(mockInfo, 1L);
101 Resource dataStoreResource = null;
102 for (final Resource r : resourceList.getApis()) {
103 if (r.getPath().endsWith("/Datastores(-)")) {
104 dataStoreResource = r;
107 assertNotNull("Failed to find data store resource", dataStoreResource);
111 public void testGetDataStoreApi() throws Exception {
112 final UriInfo mockInfo = this.helper.createMockUriInfo(HTTP_URL);
113 this.swagger.onMountPointCreated(INSTANCE_ID); // add this ID into the list of
116 final ApiDeclaration mountPointApi = this.swagger.getMountPointApi(mockInfo, 1L, "Datastores", "-");
117 assertNotNull("failed to find Datastore API", mountPointApi);
118 final List<Api> apis = mountPointApi.getApis();
119 assertEquals("Unexpected api list size", 3, apis.size());
121 final Set<String> actualApis = new TreeSet<>();
122 for (final Api api : apis) {
123 actualApis.add(api.getPath());
124 final List<Operation> operations = api.getOperations();
125 assertEquals("unexpected operation size on " + api.getPath(), 1, operations.size());
126 assertEquals("unexpected operation method " + api.getPath(), "GET", operations.get(0)
128 assertNotNull("expected non-null desc on " + api.getPath(), operations.get(0)
131 final Set<String> expectedApis = new TreeSet<>(Arrays.asList(new String[] {
132 "/restconf/config" + INSTANCE_URL + "yang-ext:mount",
133 "/restconf/operational" + INSTANCE_URL + "yang-ext:mount",
134 "/restconf/operations" + INSTANCE_URL + "yang-ext:mount",}));
135 assertEquals(expectedApis, actualApis);