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.assertTrue;
6 import static org.junit.Assert.fail;
9 import java.util.Arrays;
10 import java.util.Map.Entry;
12 import java.util.TreeSet;
14 import javax.ws.rs.core.UriInfo;
16 import junit.framework.Assert;
18 import org.junit.After;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.controller.sal.core.api.model.SchemaService;
22 import org.opendaylight.controller.sal.rest.doc.swagger.Api;
23 import org.opendaylight.controller.sal.rest.doc.swagger.ApiDeclaration;
24 import org.opendaylight.controller.sal.rest.doc.swagger.Operation;
25 import org.opendaylight.controller.sal.rest.doc.swagger.Resource;
26 import org.opendaylight.controller.sal.rest.doc.swagger.ResourceList;
27 import org.opendaylight.yangtools.yang.model.api.Module;
29 import com.google.common.base.Preconditions;
34 public class ApiDocGeneratorTest {
36 public static final String HTTP_HOST = "http://host";
37 private ApiDocGenerator generator;
38 private DocGenTestHelper helper;
41 public void setUp() throws Exception {
42 generator = new ApiDocGenerator();
43 helper = new DocGenTestHelper();
48 public void after() throws Exception {
52 * Method: getApiDeclaration(String module, String revision, UriInfo
56 public void testGetModuleDoc() throws Exception {
57 Preconditions.checkArgument(helper.getModules() != null, "No modules found");
59 for (Entry<File, Module> m : helper.getModules().entrySet()) {
60 if (m.getKey().getAbsolutePath().endsWith("toaster_short.yang")) {
61 ApiDeclaration doc = generator.getSwaggerDocSpec(m.getValue(),
62 "http://localhost:8080/restconf", "");
64 Assert.assertNotNull(doc);
70 public void testEdgeCases() throws Exception {
71 Preconditions.checkArgument(helper.getModules() != null, "No modules found");
73 for (Entry<File, Module> m : helper.getModules().entrySet()) {
74 if (m.getKey().getAbsolutePath().endsWith("toaster.yang")) {
75 ApiDeclaration doc = generator.getSwaggerDocSpec(m.getValue(),
76 "http://localhost:8080/restconf", "");
77 Assert.assertNotNull(doc);
79 //testing bugs.opendaylight.org bug 1290. UnionType model type.
80 String jsonString = doc.getModels().toString();
82 jsonString.contains( "testUnion\":{\"type\":\"integer or string\",\"required\":false}" ) );
87 private void validateToaster(ApiDeclaration doc) throws Exception {
88 Set<String> expectedUrls = new TreeSet<>(Arrays.asList(new String[] {
89 "/config/toaster2:toaster/", "/operational/toaster2:toaster/",
90 "/operations/toaster2:cancel-toast", "/operations/toaster2:make-toast",
91 "/operations/toaster2:restock-toaster" }));
93 Set<String> actualUrls = new TreeSet<>();
96 for (Api api : doc.getApis()) {
97 actualUrls.add(api.getPath());
98 if (api.getPath().contains("/config/toaster2:toaster/")) {
103 boolean containsAll = actualUrls.containsAll(expectedUrls);
105 expectedUrls.removeAll(actualUrls);
106 fail("Missing expected urls: " + expectedUrls);
109 Set<String> expectedConfigMethods = new TreeSet<>(Arrays.asList(new String[] { "GET",
111 Set<String> actualConfigMethods = new TreeSet<>();
112 for (Operation oper : configApi.getOperations()) {
113 actualConfigMethods.add(oper.getMethod());
116 containsAll = actualConfigMethods.containsAll(expectedConfigMethods);
118 expectedConfigMethods.removeAll(actualConfigMethods);
119 fail("Missing expected method on config API: " + expectedConfigMethods);
122 // TODO: we should really do some more validation of the
125 * Missing validation: Explicit validation of URLs, and their methods
126 * Input / output models.
131 public void testGetResourceListing() throws Exception {
132 UriInfo info = helper.createMockUriInfo(HTTP_HOST);
133 SchemaService mockSchemaService = helper.createMockSchemaService();
135 generator.setSchemaService(mockSchemaService);
137 ResourceList resourceListing = generator.getResourceListing(info);
139 Resource toaster = null;
140 Resource toaster2 = null;
141 for (Resource r : resourceListing.getApis()) {
142 String path = r.getPath();
143 if (path.contains("toaster2")) {
145 } else if (path.contains("toaster")) {
150 assertNotNull(toaster2);
151 assertNotNull(toaster);
153 assertEquals(HTTP_HOST + "/toaster(2009-11-20)", toaster.getPath());
154 assertEquals(HTTP_HOST + "/toaster2(2009-11-20)", toaster2.getPath());