Merge "Complete implementation of DataChangeListenerProxy"
[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.assertTrue;
6 import static org.junit.Assert.fail;
7
8 import java.io.File;
9 import java.util.Arrays;
10 import java.util.Map.Entry;
11 import java.util.Set;
12 import java.util.TreeSet;
13
14 import javax.ws.rs.core.UriInfo;
15
16 import junit.framework.Assert;
17
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;
28
29 import com.google.common.base.Preconditions;
30
31 /**
32  *
33  */
34 public class ApiDocGeneratorTest {
35
36     public static final String HTTP_HOST = "http://host";
37     private ApiDocGenerator generator;
38     private DocGenTestHelper helper;
39
40     @Before
41     public void setUp() throws Exception {
42         generator = new ApiDocGenerator();
43         helper = new DocGenTestHelper();
44         helper.setUp();
45     }
46
47     @After
48     public void after() throws Exception {
49     }
50
51     /**
52      * Method: getApiDeclaration(String module, String revision, UriInfo
53      * uriInfo)
54      */
55     @Test
56     public void testGetModuleDoc() throws Exception {
57         Preconditions.checkArgument(helper.getModules() != null, "No modules found");
58
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", "");
63                 validateToaster(doc);
64                 Assert.assertNotNull(doc);
65             }
66         }
67     }
68
69     @Test
70     public void testEdgeCases() throws Exception {
71         Preconditions.checkArgument(helper.getModules() != null, "No modules found");
72
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);
78
79                 //testing bugs.opendaylight.org bug 1290. UnionType model type.
80                 String jsonString = doc.getModels().toString();
81                 assertTrue(
82                         jsonString.contains( "testUnion\":{\"type\":\"integer or string\",\"required\":false}" ) );
83             }
84         }
85     }
86
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" }));
92
93         Set<String> actualUrls = new TreeSet<>();
94
95         Api configApi = null;
96         for (Api api : doc.getApis()) {
97             actualUrls.add(api.getPath());
98             if (api.getPath().contains("/config/toaster2:toaster/")) {
99                 configApi = api;
100             }
101         }
102
103         boolean containsAll = actualUrls.containsAll(expectedUrls);
104         if (!containsAll) {
105             expectedUrls.removeAll(actualUrls);
106             fail("Missing expected urls: " + expectedUrls);
107         }
108
109         Set<String> expectedConfigMethods = new TreeSet<>(Arrays.asList(new String[] { "GET",
110                 "PUT", "DELETE" }));
111         Set<String> actualConfigMethods = new TreeSet<>();
112         for (Operation oper : configApi.getOperations()) {
113             actualConfigMethods.add(oper.getMethod());
114         }
115
116         containsAll = actualConfigMethods.containsAll(expectedConfigMethods);
117         if (!containsAll) {
118             expectedConfigMethods.removeAll(actualConfigMethods);
119             fail("Missing expected method on config API: " + expectedConfigMethods);
120         }
121
122         // TODO: we should really do some more validation of the
123         // documentation...
124         /**
125          * Missing validation: Explicit validation of URLs, and their methods
126          * Input / output models.
127          */
128     }
129
130     @Test
131     public void testGetResourceListing() throws Exception {
132         UriInfo info = helper.createMockUriInfo(HTTP_HOST);
133         SchemaService mockSchemaService = helper.createMockSchemaService();
134
135         generator.setSchemaService(mockSchemaService);
136
137         ResourceList resourceListing = generator.getResourceListing(info);
138
139         Resource toaster = null;
140         Resource toaster2 = null;
141         for (Resource r : resourceListing.getApis()) {
142             String path = r.getPath();
143             if (path.contains("toaster2")) {
144                 toaster2 = r;
145             } else if (path.contains("toaster")) {
146                 toaster = r;
147             }
148         }
149
150         assertNotNull(toaster2);
151         assertNotNull(toaster);
152
153         assertEquals(HTTP_HOST + "/toaster(2009-11-20)", toaster.getPath());
154         assertEquals(HTTP_HOST + "/toaster2(2009-11-20)", toaster2.getPath());
155     }
156
157 }