1 package org.opendaylight.controller.sal.rest.doc.impl;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.fail;
8 import java.util.Arrays;
9 import java.util.Map.Entry;
11 import java.util.TreeSet;
13 import javax.ws.rs.core.UriInfo;
15 import junit.framework.Assert;
17 import org.junit.After;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.controller.sal.core.api.model.SchemaService;
21 import org.opendaylight.controller.sal.rest.doc.swagger.Api;
22 import org.opendaylight.controller.sal.rest.doc.swagger.ApiDeclaration;
23 import org.opendaylight.controller.sal.rest.doc.swagger.Operation;
24 import org.opendaylight.controller.sal.rest.doc.swagger.Resource;
25 import org.opendaylight.controller.sal.rest.doc.swagger.ResourceList;
26 import org.opendaylight.yangtools.yang.model.api.Module;
28 import com.google.common.base.Preconditions;
33 public class ApiDocGeneratorTest {
35 public static final String HTTP_HOST = "http://host";
36 private ApiDocGenerator generator;
37 private DocGenTestHelper helper;
40 public void setUp() throws Exception {
41 generator = new ApiDocGenerator();
42 helper = new DocGenTestHelper();
47 public void after() throws Exception {
51 * Method: getApiDeclaration(String module, String revision, UriInfo
55 public void testGetModuleDoc() throws Exception {
56 Preconditions.checkArgument(helper.getModules() != null, "No modules found");
58 for (Entry<File, Module> m : helper.getModules().entrySet()) {
59 if (m.getKey().getAbsolutePath().endsWith("toaster_short.yang")) {
60 ApiDeclaration doc = generator.getSwaggerDocSpec(m.getValue(),
61 "http://localhost:8080/restconf", "");
63 Assert.assertNotNull(doc);
68 private void validateToaster(ApiDeclaration doc) throws Exception {
69 Set<String> expectedUrls = new TreeSet<>(Arrays.asList(new String[] {
70 "/config/toaster2:toaster/", "/operational/toaster2:toaster/",
71 "/operations/toaster2:cancel-toast", "/operations/toaster2:make-toast",
72 "/operations/toaster2:restock-toaster" }));
74 Set<String> actualUrls = new TreeSet<>();
77 for (Api api : doc.getApis()) {
78 actualUrls.add(api.getPath());
79 if (api.getPath().contains("/config/toaster2:toaster/")) {
84 boolean containsAll = actualUrls.containsAll(expectedUrls);
86 expectedUrls.removeAll(actualUrls);
87 fail("Missing expected urls: " + expectedUrls);
90 Set<String> expectedConfigMethods = new TreeSet<>(Arrays.asList(new String[] { "GET",
92 Set<String> actualConfigMethods = new TreeSet<>();
93 for (Operation oper : configApi.getOperations()) {
94 actualConfigMethods.add(oper.getMethod());
97 containsAll = actualConfigMethods.containsAll(expectedConfigMethods);
99 expectedConfigMethods.removeAll(actualConfigMethods);
100 fail("Missing expected method on config API: " + expectedConfigMethods);
102 // TODO: we should really do some more validation of the
105 * Missing validation: Explicit validation of URLs, and their methods
106 * Input / output models.
111 public void testGetResourceListing() throws Exception {
112 UriInfo info = helper.createMockUriInfo(HTTP_HOST);
113 SchemaService mockSchemaService = helper.createMockSchemaService();
115 generator.setSchemaService(mockSchemaService);
117 ResourceList resourceListing = generator.getResourceListing(info);
119 Resource toaster = null;
120 Resource toaster2 = null;
121 for (Resource r : resourceListing.getApis()) {
122 String path = r.getPath();
123 if (path.contains("toaster2")) {
125 } else if (path.contains("toaster")) {
130 assertNotNull(toaster2);
131 assertNotNull(toaster);
133 assertEquals(HTTP_HOST + "/toaster(2009-11-20)", toaster.getPath());
134 assertEquals(HTTP_HOST + "/toaster2(2009-11-20)", toaster2.getPath());