Bug 931, Bug 910 - Enhance Restconf Swagger Documentation
[controller.git] / opendaylight / md-sal / sal-rest-docgen / src / test / java / org / opendaylight / controller / sal / rest / doc / impl / ApiDocGeneratorTest.java
1 package org.opendaylight.controller.sal.rest.doc.impl;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.fail;
6
7 import java.io.File;
8 import java.util.Arrays;
9 import java.util.Map.Entry;
10 import java.util.Set;
11 import java.util.TreeSet;
12
13 import javax.ws.rs.core.UriInfo;
14
15 import junit.framework.Assert;
16
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;
27
28 import com.google.common.base.Preconditions;
29
30 /**
31  *
32  */
33 public class ApiDocGeneratorTest {
34
35     public static final String HTTP_HOST = "http://host";
36     private ApiDocGenerator generator;
37     private DocGenTestHelper helper;
38
39     @Before
40     public void setUp() throws Exception {
41         generator = new ApiDocGenerator();
42         helper = new DocGenTestHelper();
43         helper.setUp();
44     }
45
46     @After
47     public void after() throws Exception {
48     }
49
50     /**
51      * Method: getApiDeclaration(String module, String revision, UriInfo
52      * uriInfo)
53      */
54     @Test
55     public void testGetModuleDoc() throws Exception {
56         Preconditions.checkArgument(helper.getModules() != null, "No modules found");
57
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", "");
62                 validateToaster(doc);
63                 Assert.assertNotNull(doc);
64             }
65         }
66     }
67
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" }));
73
74         Set<String> actualUrls = new TreeSet<>();
75
76         Api configApi = null;
77         for (Api api : doc.getApis()) {
78             actualUrls.add(api.getPath());
79             if (api.getPath().contains("/config/toaster2:toaster/")) {
80                 configApi = api;
81             }
82         }
83
84         boolean containsAll = actualUrls.containsAll(expectedUrls);
85         if (!containsAll) {
86             expectedUrls.removeAll(actualUrls);
87             fail("Missing expected urls: " + expectedUrls);
88         }
89
90         Set<String> expectedConfigMethods = new TreeSet<>(Arrays.asList(new String[] { "GET",
91                 "PUT", "DELETE" }));
92         Set<String> actualConfigMethods = new TreeSet<>();
93         for (Operation oper : configApi.getOperations()) {
94             actualConfigMethods.add(oper.getMethod());
95         }
96
97         containsAll = actualConfigMethods.containsAll(expectedConfigMethods);
98         if (!containsAll) {
99             expectedConfigMethods.removeAll(actualConfigMethods);
100             fail("Missing expected method on config API: " + expectedConfigMethods);
101         }
102         // TODO: we should really do some more validation of the
103         // documentation...
104         /**
105          * Missing validation: Explicit validation of URLs, and their methods
106          * Input / output models.
107          */
108     }
109
110     @Test
111     public void testGetResourceListing() throws Exception {
112         UriInfo info = helper.createMockUriInfo(HTTP_HOST);
113         SchemaService mockSchemaService = helper.createMockSchemaService();
114
115         generator.setSchemaService(mockSchemaService);
116
117         ResourceList resourceListing = generator.getResourceListing(info);
118
119         Resource toaster = null;
120         Resource toaster2 = null;
121         for (Resource r : resourceListing.getApis()) {
122             String path = r.getPath();
123             if (path.contains("toaster2")) {
124                 toaster2 = r;
125             } else if (path.contains("toaster")) {
126                 toaster = r;
127             }
128         }
129
130         assertNotNull(toaster2);
131         assertNotNull(toaster);
132
133         assertEquals(HTTP_HOST + "/toaster(2009-11-20)", toaster.getPath());
134         assertEquals(HTTP_HOST + "/toaster2(2009-11-20)", toaster2.getPath());
135     }
136
137 }