BUG 907 distinguish augmented nodes
[controller.git] / opendaylight / md-sal / sal-rest-docgen / src / test / java / org / opendaylight / controller / sal / rest / doc / impl / ApiDocGeneratorTest.java
index 07c9378439d2f66551672d8a08ec20785a0a074b..19f82b53867b603af20576759485999ca1fe28c4 100644 (file)
@@ -5,16 +5,17 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
+import com.google.common.base.Preconditions;
 import java.io.File;
 import java.util.Arrays;
+import java.util.HashSet;
 import java.util.Map.Entry;
 import java.util.Set;
 import java.util.TreeSet;
-
 import javax.ws.rs.core.UriInfo;
-
 import junit.framework.Assert;
-
+import org.json.JSONException;
+import org.json.JSONObject;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -25,8 +26,8 @@ import org.opendaylight.controller.sal.rest.doc.swagger.Operation;
 import org.opendaylight.controller.sal.rest.doc.swagger.Resource;
 import org.opendaylight.controller.sal.rest.doc.swagger.ResourceList;
 import org.opendaylight.yangtools.yang.model.api.Module;
-
-import com.google.common.base.Preconditions;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
 
 /**
  *
@@ -36,12 +37,14 @@ public class ApiDocGeneratorTest {
     public static final String HTTP_HOST = "http://host";
     private ApiDocGenerator generator;
     private DocGenTestHelper helper;
+    private SchemaContext schemaContext;
 
     @Before
     public void setUp() throws Exception {
         generator = new ApiDocGenerator();
         helper = new DocGenTestHelper();
         helper.setUp();
+        schemaContext = new YangParserImpl().resolveSchemaContext(new HashSet<Module>(helper.getModules().values()));
     }
 
     @After
@@ -59,8 +62,9 @@ public class ApiDocGeneratorTest {
         for (Entry<File, Module> m : helper.getModules().entrySet()) {
             if (m.getKey().getAbsolutePath().endsWith("toaster_short.yang")) {
                 ApiDeclaration doc = generator.getSwaggerDocSpec(m.getValue(),
-                        "http://localhost:8080/restconf", "");
+                        "http://localhost:8080/restconf", "",schemaContext);
                 validateToaster(doc);
+                validateTosterDocContainsModulePrefixes(doc);
                 Assert.assertNotNull(doc);
             }
         }
@@ -73,7 +77,7 @@ public class ApiDocGeneratorTest {
         for (Entry<File, Module> m : helper.getModules().entrySet()) {
             if (m.getKey().getAbsolutePath().endsWith("toaster.yang")) {
                 ApiDeclaration doc = generator.getSwaggerDocSpec(m.getValue(),
-                        "http://localhost:8080/restconf", "");
+                        "http://localhost:8080/restconf", "",schemaContext);
                 Assert.assertNotNull(doc);
 
                 //testing bugs.opendaylight.org bug 1290. UnionType model type.
@@ -84,11 +88,21 @@ public class ApiDocGeneratorTest {
         }
     }
 
+    /**
+     * Tests whether from yang files are generated all required paths for HTTP operations (GET, DELETE, PUT, POST)
+     *
+     * If container | list is augmented then in path there should be specified module name followed with collon (e. g.
+     * "/config/module1:element1/element2/module2:element3")
+     *
+     * @param doc
+     * @throws Exception
+     */
     private void validateToaster(ApiDeclaration doc) throws Exception {
         Set<String> expectedUrls = new TreeSet<>(Arrays.asList(new String[] {
                 "/config/toaster2:toaster/", "/operational/toaster2:toaster/",
                 "/operations/toaster2:cancel-toast", "/operations/toaster2:make-toast",
-                "/operations/toaster2:restock-toaster" }));
+                "/operations/toaster2:restock-toaster",
+                "/config/toaster2:toaster/toasterSlot/{slotId}/toaster-augmented:slotInfo/" }));
 
         Set<String> actualUrls = new TreeSet<>();
 
@@ -130,7 +144,7 @@ public class ApiDocGeneratorTest {
     @Test
     public void testGetResourceListing() throws Exception {
         UriInfo info = helper.createMockUriInfo(HTTP_HOST);
-        SchemaService mockSchemaService = helper.createMockSchemaService();
+        SchemaService mockSchemaService = helper.createMockSchemaService(schemaContext);
 
         generator.setSchemaService(mockSchemaService);
 
@@ -154,4 +168,30 @@ public class ApiDocGeneratorTest {
         assertEquals(HTTP_HOST + "/toaster2(2009-11-20)", toaster2.getPath());
     }
 
+    private void validateTosterDocContainsModulePrefixes(ApiDeclaration doc) {
+        JSONObject topLevelJson = doc.getModels();
+        try {
+            JSONObject configToaster = topLevelJson.getJSONObject("(config)toaster");
+            assertNotNull("(config)toaster JSON object missing", configToaster);
+            //without module prefix
+            containsProperties(configToaster, "toasterSlot");
+
+            JSONObject toasterSlot = topLevelJson.getJSONObject("(config)toasterSlot");
+            assertNotNull("(config)toasterSlot JSON object missing", toasterSlot);
+            //with module prefix
+            containsProperties(toasterSlot, "toaster-augmented:slotInfo");
+
+        } catch (JSONException e) {
+            fail("Json exception while reading JSON object. Original message "+e.getMessage());
+        }
+    }
+
+    private void containsProperties(final JSONObject jsonObject,final String...properties) throws JSONException {
+        for (String property : properties) {
+            JSONObject propertiesObject = jsonObject.getJSONObject("properties");
+            assertNotNull("Properties object missing in ", propertiesObject);
+            JSONObject concretePropertyObject = propertiesObject.getJSONObject(property);
+            assertNotNull(property + " is missing",concretePropertyObject);
+        }
+    }
 }