Ignore invalid schema while discovering schema 73/24373/7
authorKanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Sat, 7 Oct 2017 17:53:05 +0000 (23:23 +0530)
committerKanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Mon, 20 Nov 2017 08:21:04 +0000 (13:51 +0530)
Issue-Id: CLI-66

Change-Id: I17a367c9277a264b2d3828007af9b42592484ff1
Signed-off-by: Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
framework/src/main/java/org/onap/cli/fw/OnapCommandRegistrar.java
framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java
framework/src/test/java/org/onap/cli/fw/utils/OnapCommandUtilsTest.java
validate/validation/src/test/java/org/onap/cli/moco/OnapCommandHttpMocoServer.java

index dcd9a50..b3905de 100644 (file)
@@ -213,6 +213,7 @@ public class OnapCommandRegistrar {
             } else {
                 schemaName = this.getSchemaFileName(cls);
             }
+
             cmd.initializeSchema(schemaName);
         } catch (OnapCommandException | NoSuchMethodException | SecurityException | InstantiationException
                 | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
index 579c4af..d03ee10 100644 (file)
@@ -190,7 +190,7 @@ public class OnapCommandUtils {
         InputStream inputStream = OnapCommandUtils.class.getClassLoader().getResourceAsStream(schemaName);
 
         try {
-            Resource resource = getExternalResource(schemaName, SCHEMA_PATH_PATERN);
+            Resource resource = findResource(schemaName, SCHEMA_PATH_PATERN);
 
             if (resource != null) {
                 inputStream = resource.getInputStream();
@@ -1604,12 +1604,17 @@ public class OnapCommandUtils {
     public static List<SchemaInfo> discoverSchemas() throws OnapCommandException {
         List<SchemaInfo> extSchemas = new ArrayList<>();
         try {
-            Resource[] res = getExternalResources(SCHEMA_PATH_PATERN);
+            Resource[] res = findResources(SCHEMA_PATH_PATERN);
             if (res != null && res.length > 0) {
                 Map<String, ?> resourceMap;
 
                 for (Resource resource : res) {
-                    resourceMap = loadSchema(resource);
+                    try {
+                        resourceMap = loadSchema(resource);
+                    } catch (OnapCommandException e) {
+                        LOG.error("Invalid schema " + resource.getURI().toString(), e);
+                        continue;
+                    }
 
                     if (resourceMap != null && resourceMap.size() > 0) {
                         SchemaInfo schema = new SchemaInfo();
@@ -1666,7 +1671,7 @@ public class OnapCommandUtils {
      * @throws IOException
      *             exception
      */
-    public static Resource[] getExternalResources(String pattern) throws IOException {
+    public static Resource[] findResources(String pattern) throws IOException {
         ClassLoader cl = OnapCommandUtils.class.getClassLoader();
         ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
         return resolver.getResources("classpath*:" + pattern);
@@ -1681,8 +1686,8 @@ public class OnapCommandUtils {
      * @throws IOException
      *             exception
      */
-    public static Resource getExternalResource(String fileName, String pattern) throws IOException {
-        Resource[] resources = getExternalResources(pattern);
+    public static Resource findResource(String fileName, String pattern) throws IOException {
+        Resource[] resources = findResources(pattern);
         if (resources != null && resources.length > 0) {
             for (Resource res : resources) {
                 if (res.getFilename().equals(fileName)) {
@@ -1724,7 +1729,7 @@ public class OnapCommandUtils {
     public static void persistSchemaInfo(List<SchemaInfo> schemas) throws OnapCommandDiscoveryFailed {
         if (schemas != null) {
             try {
-                Resource[] resources = getExternalResources(DATA_DIRECTORY);
+                Resource[] resources = findResources(DATA_DIRECTORY);
                 if (resources != null && resources.length == 1) {
                     String path = resources[0].getURI().getPath();
                     File file = new File(path + File.separator + DISCOVERY_FILE);
@@ -1741,7 +1746,7 @@ public class OnapCommandUtils {
     public static void persistProfile(List<Param> params, String profileName) throws OnapCommandPersistProfileFailed {
         if (params != null) {
             try {
-                Resource[] resources = getExternalResources(DATA_DIRECTORY);
+                Resource[] resources = findResources(DATA_DIRECTORY);
                 if (resources != null && resources.length == 1) {
                     String path = resources[0].getURI().getPath();
                     File file = new File(path + File.separator + profileName + ".json");
@@ -1764,7 +1769,7 @@ public class OnapCommandUtils {
     public static boolean isAlreadyDiscovered() throws OnapCommandDiscoveryFailed {
         Resource resource = null;
         try {
-            resource = getExternalResource(DISCOVERY_FILE,
+            resource = findResource(DISCOVERY_FILE,
                     DATA_PATH_JSON_PATTERN);
             if (resource != null) {
                 return true;
@@ -1795,7 +1800,7 @@ public class OnapCommandUtils {
             }
         } else {
             try {
-                Resource resource = getExternalResource(DISCOVERY_FILE,
+                Resource resource = findResource(DISCOVERY_FILE,
                         DATA_PATH_JSON_PATTERN);
                 if (resource != null) {
                     File file = new File(resource.getURI().getPath());
@@ -1816,7 +1821,7 @@ public class OnapCommandUtils {
         List<Param> params = new ArrayList<>();
 
         try {
-            Resource resource = getExternalResource(profileName + ".json",
+            Resource resource = findResource(profileName + ".json",
                     DATA_PATH_JSON_PATTERN);
             if (resource != null) {
                 File file = new File(resource.getURI().getPath());
index 43883ce..e874dc9 100644 (file)
@@ -380,47 +380,6 @@ public class OnapCommandUtilsTest {
         input1 = OnapCommandUtils.populateOutputs(params, output);
     }
 
-    @Test(expected = OnapCommandException.class)
-    public void zendExceptionTest1() throws OnapCommandException {
-
-        mockExternalResources();
-        OnapCommandUtils.loadSchema(new OnapSwaggerBasedCommandSample(), "schemaName");
-    }
-
-    @Test(expected = OnapCommandException.class)
-    public void zendExceptionTest2() throws OnapCommandException {
-
-        mockExternalResources();
-        OnapCommandUtils.loadSchema(new OnapHttpCommandSample(), "schemaName", false, false);
-    }
-
-    @Test(expected = OnapCommandException.class)
-    public void zendExceptionTest3() throws OnapCommandException {
-
-        mockExternalResources();
-        OnapCommandUtils.validateSchemaVersion("schemaName", "version");
-    }
-
-    @Test(expected = OnapCommandException.class)
-    public void zendExceptionTest4() throws OnapCommandException {
-
-        mockExternalResources();
-        OnapCommandUtils.discoverOrLoadSchemas();
-    }
-
-    @Test(expected = OnapCommandException.class)
-    public void zendExceptionTest5() throws OnapCommandException {
-
-        mockExternalResources();
-        OnapCommandUtils.discoverSchemas();
-    }
-
-    @Test(expected = OnapCommandException.class)
-    public void zendExceptionTest6() throws OnapCommandException {
-
-        mockExternalResources();
-        OnapCommandUtils.persistSchemaInfo(new ArrayList<SchemaInfo>());
-    }
 
     @Test(expected = OnapCommandHelpFailed.class)
     public void zendExceptionHelpTest1() throws OnapCommandException {
index 488c619..cef6af1 100644 (file)
@@ -117,7 +117,7 @@ public class OnapCommandHttpMocoServer {
     private List<Resource> dicoverSampleYamls() {
         Resource[] resources = new Resource [] {};
         try {
-            resources = OnapCommandUtils.getExternalResources(SAMPLE_PATTERN + this.samplesToTest);
+            resources = OnapCommandUtils.findResources(SAMPLE_PATTERN + this.samplesToTest);
         } catch (IOException e) {
             LOG.error("Failed to discover the samples", e);
         }