Add profile list support 67/34667/2
authorKanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Tue, 6 Mar 2018 09:14:24 +0000 (14:44 +0530)
committerKanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Thu, 8 Mar 2018 05:47:58 +0000 (11:17 +0530)
Issue-ID: CLI-95

Change-Id: Ic50132693fcd8a49c7e0b1bfb38e02f0948f764d
Signed-off-by: Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
framework/src/main/java/org/onap/cli/fw/conf/OnapCommandConstants.java
framework/src/main/java/org/onap/cli/fw/input/cache/OnapCommandParameterCache.java
framework/src/main/java/org/onap/cli/fw/registrar/OnapCommandRegistrar.java
framework/src/test/java/org/onap/cli/fw/input/OnapCommandParameterCacheTest.java
framework/src/test/java/org/onap/cli/fw/input/cache/OnapCommandParameterCacheTest.java [new file with mode: 0644]
framework/src/test/java/org/onap/cli/fw/registrar/OnapCommandRegistrarTest.java
framework/src/test/resources/data/test-profile.json [new file with mode: 0644]
framework/src/test/resources/data/test2-profile.json [new file with mode: 0644]
main/src/main/java/org/onap/cli/main/OnapCli.java
main/src/test/java/org/onap/cli/main/OnapCliMainTest.java

index 408f4f6..3920423 100644 (file)
@@ -114,6 +114,8 @@ public class OnapCommandConstants {
     public static final String DATA_DIRECTORY = "data";
     public static final String DISCOVERY_FILE = "cli-schema.json";
     public static final String DATA_PATH_JSON_PATTERN = DATA_DIRECTORY + JSON_PATTERN;
+    public static final String DATA_PATH_PROFILE_JSON = "-profile.json";
+    public static final String DATA_PATH_PROFILE_JSON_PATTERN = DATA_DIRECTORY + "/**/*" + DATA_PATH_PROFILE_JSON;
     public static final String DISCOVER_ALWAYS = "discover_always";
     public static final String PARAM_CACHE_FILE_NAME = "global-profile";
 
index 5583316..ee6dbed 100644 (file)
@@ -18,6 +18,8 @@ package org.onap.cli.fw.input.cache;
 
 import static org.onap.cli.fw.conf.OnapCommandConstants.DATA_DIRECTORY;
 import static org.onap.cli.fw.conf.OnapCommandConstants.DATA_PATH_JSON_PATTERN;
+import static org.onap.cli.fw.conf.OnapCommandConstants.DATA_PATH_PROFILE_JSON;
+import static org.onap.cli.fw.conf.OnapCommandConstants.DATA_PATH_PROFILE_JSON_PATTERN;
 
 import java.io.File;
 import java.io.IOException;
@@ -132,7 +134,7 @@ public class OnapCommandParameterCache {
                 Resource[] resources = OnapCommandDiscoveryUtils.findResources(DATA_DIRECTORY);
                 if (resources != null && resources.length == 1) {
                     String path = resources[0].getURI().getPath();
-                    File file = new File(path + File.separator + profileName + ".json");
+                    File file = new File(path + File.separator + profileName + DATA_PATH_PROFILE_JSON);
                     ObjectMapper mapper = new ObjectMapper();
                     mapper.writerWithDefaultPrettyPrinter().writeValue(file, params);
                 }
@@ -146,7 +148,7 @@ public class OnapCommandParameterCache {
         List<OnapCommandParamEntity> params = new ArrayList<>();
 
         try {
-            Resource resource = OnapCommandDiscoveryUtils.findResource(profileName + ".json",
+            Resource resource = OnapCommandDiscoveryUtils.findResource(profileName + DATA_PATH_PROFILE_JSON,
                     DATA_PATH_JSON_PATTERN);
             if (resource != null) {
                 File file = new File(resource.getURI().getPath());
@@ -160,4 +162,24 @@ public class OnapCommandParameterCache {
 
         return params;
     }
+
+    public List<String> getProfiles() {
+        List<String> profiles = new ArrayList<>();
+
+        Resource[] resources;
+        try {
+            resources = OnapCommandDiscoveryUtils.findResources(DATA_PATH_PROFILE_JSON_PATTERN);
+        } catch (IOException e) {
+             throw new RuntimeException(e);   // NOSONAR
+        }
+
+        if (resources != null && resources.length > 0) {
+            for (Resource res : resources) {
+                String profile = res.getFilename().substring(0, res.getFilename().indexOf(DATA_PATH_PROFILE_JSON));
+                profiles.add(profile);
+            }
+        }
+
+        return profiles;
+    }
 }
index 854b55b..8ba215e 100644 (file)
@@ -94,6 +94,10 @@ public class OnapCommandRegistrar {
         this.paramCache.setProfile(profileName);
     }
 
+    public List<String> getUserProfiles() {
+        return paramCache.getProfiles();
+    }
+
     private static OnapCommandRegistrar registrar = null;
 
     /**
index ea06ea1..302d280 100644 (file)
@@ -27,7 +27,7 @@ public class OnapCommandParameterCacheTest {
 
         cache.add("1.0", "a", "b");
         cache.remove("1.0", "a");
-        cache.setProfile("test");
+        cache.setProfile("test123");
         cache.getParams("1.0");
     }
 
diff --git a/framework/src/test/java/org/onap/cli/fw/input/cache/OnapCommandParameterCacheTest.java b/framework/src/test/java/org/onap/cli/fw/input/cache/OnapCommandParameterCacheTest.java
new file mode 100644 (file)
index 0000000..be53994
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2017 Huawei Technologies Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onap.cli.fw.input.cache;
+
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+
+public class OnapCommandParameterCacheTest {
+    @Test
+    public void paramTypeGetTest() {
+
+        assertTrue(OnapCommandParameterCache.getInstance().getProfiles().contains("test"));
+
+
+    }
+
+}
index 539ec24..124b956 100644 (file)
@@ -90,7 +90,7 @@ public class OnapCommandRegistrarTest {
     @Test
     public void testProfile() throws OnapCommandException {
         try {
-                OnapCommandRegistrar.getRegistrar().setProfile("test");
+                OnapCommandRegistrar.getRegistrar().setProfile("test12312");
                 OnapCommandRegistrar.getRegistrar().addParamCache("a", "b");
                 OnapCommandRegistrar.getRegistrar().getParamCache();
                 OnapCommandRegistrar.getRegistrar().removeParamCache("a");
diff --git a/framework/src/test/resources/data/test-profile.json b/framework/src/test/resources/data/test-profile.json
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/framework/src/test/resources/data/test2-profile.json b/framework/src/test/resources/data/test2-profile.json
new file mode 100644 (file)
index 0000000..e69de29
index 3f37fab..ca3c365 100644 (file)
@@ -243,7 +243,9 @@ public class OnapCli {
                         handleVersion();
                     } else if (!args.isEmpty() && this.args.get(0).equals(OnapCliConstants.PARAM_INTERACTIVE_PROFILE)) {
                         if (args.size() == 1) {
-                            this.print("Please use it in the form of 'profile <profile-name>'");
+                            this.print("Please use it in the form of 'profile <profile-name>'\n");
+                            this.print("Profiles:");
+                            this.print(OnapCommandRegistrar.getRegistrar().getUserProfiles().toString());
                         } else {
                             this.args = Arrays.asList(new String [] {
                                     this.getLongOption(OnapCliConstants.PARAM_PROFILE_LONG),
index f740190..8fd3441 100644 (file)
@@ -151,6 +151,13 @@ public class OnapCliMainTest {
         } catch (Exception e) {
         }
 
+        cli = new OnapCli(new String[] {});
+        mockConsole("profile");
+        try {
+            cli.handleInteractive();
+        } catch (Exception e) {
+        }
+
         cli = new OnapCli(new String[] {});
         mockConsole("version");
         try {