From aa18a9537994b8637d40516aab71fa82cf55c2d3 Mon Sep 17 00:00:00 2001 From: Tomasz Golabek Date: Fri, 7 Jun 2019 09:28:23 +0200 Subject: ExportImportMenu tests Main class tested and refactored Change-Id: I379a18c289b613cdc7ecbb1617618af96ffa275b Issue-ID: SDC-2326 Signed-off-by: Tomasz Golabek --- .../sdc/asdctool/main/ExportImportMenu.java | 295 +++++++++++---------- 1 file changed, 149 insertions(+), 146 deletions(-) (limited to 'asdctool/src/main') diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/ExportImportMenu.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/ExportImportMenu.java index 844ae1ec6e..9c8ca992aa 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/ExportImportMenu.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/ExportImportMenu.java @@ -16,178 +16,181 @@ * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= + * Modifications copyright (c) 2019 Nokia + * ================================================================================ */ package org.openecomp.sdc.asdctool.main; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; import org.openecomp.sdc.asdctool.impl.GraphJsonValidator; import org.openecomp.sdc.asdctool.impl.GraphMLConverter; import org.openecomp.sdc.asdctool.impl.GraphMLDataAnalyzer; public class ExportImportMenu { - private static void usageAndExit() { - exportUsage(); - importUsage(); - exportUsersUsage(); - validateJsonUsage(); - - System.exit(1); - } - - private static void importUsage() { - System.out.println("Usage: import "); - } - - private static void validateJsonUsage() { - System.out.println("Usage: validate-json "); - } - - private static void exportUsage() { - System.out.println("Usage: export "); - } - - private static void dataReportUsage() { - System.out.println("Usage: get-data-report-from-graph-ml "); - } - - private static void exportUsersUsage() { - System.out.println("Usage: exportusers "); - } - - public static void main(String[] args) throws Exception { - - if (args == null || args.length < 1) { - usageAndExit(); - } - - String operation = args[0]; - GraphMLConverter graphMLConverter = new GraphMLConverter(); - switch (operation.toLowerCase()) { - - case "export": - boolean isValid = verifyParamsLength(args, 3); - if (false == isValid) { - exportUsage(); + enum ExportImportEnum { + DATA_REPORT("Usage: get-data-report-from-graph-ml ", "get-data-report-from-graph-ml"){ + @Override + void handle(String[] args) { + if (verifyParamsLength(args, 2)) { + usage(); + System.exit(1); + } + String[] dataArgs = new String[] { args[1] }; + if (new GraphMLDataAnalyzer().analyzeGraphMLData(dataArgs) == null) { + System.exit(2); + } + } + }, + EXPORT("Usage: export ", "export"){ + @Override + void handle(String[] args) { + if (verifyParamsLength(args, 3)) { + usage(); + System.exit(1); + } + + if (!GRAPH_ML_CONVERTER.exportGraph(args)) { + System.exit(2); + } + } + },EXPORT_AS_GRAPH("Usage: export-as-graph-ml ", "export-as-graph-ml"){ + @Override + void handle(String[] args) { + if (verifyParamsLength(args, 3)) { + usage(); + System.exit(1); + } + if (GRAPH_ML_CONVERTER.exportGraphMl(args) == null) { + System.exit(2); + } + } + },EXPORT_USERS("Usage: exportusers ", "exportusers"){ + @Override + void handle(String[] args) { + if (verifyParamsLength(args, 3)) { + usage(); + System.exit(1); + } + if (!GRAPH_ML_CONVERTER.exportUsers(args)) { + System.exit(2); + } + } + },EXPORT_WITH_REPORT("Usage: export-as-graph-ml-with-data-report ", "export-as-graph-ml-with-data-report"){ + @Override + void handle(String[] args) { + if (verifyParamsLength(args, 3)) { + usage(); + System.exit(1); + } + if (GRAPH_ML_CONVERTER.exportGraphMl(args) == null) { + System.exit(2); + } + String[] dataArgs = new String[] {GRAPH_ML_CONVERTER.exportGraphMl(args)}; + if (new GraphMLDataAnalyzer().analyzeGraphMLData(dataArgs) == null) { + System.exit(2); + } + } + },FIND_PROBLEM("Usage: findproblem ", "findproblem"){ + @Override + void handle(String[] args) { + if (verifyParamsLength(args, 3)) { + usage(); + System.exit(1); + } + if (!GRAPH_ML_CONVERTER.findErrorInJsonGraph(args)) { + System.exit(2); + } + } + },IMPORT("Usage: import ", "import"){ + @Override + void handle(String[] args) { + if (verifyParamsLength(args, 3)) { + usage(); + System.exit(1); + } + if (!GRAPH_ML_CONVERTER.importGraph(args)) { + System.exit(2); + } + } + },VALIDATE_JSON("Usage: validate-json ", "validate-json"){ + @Override + void handle(String[] args) throws IOException { + if (verifyParamsLength(args, 2)) { + usage(); + System.exit(1); + } + String jsonFilePath = args[1]; + GraphJsonValidator graphJsonValidator = new GraphJsonValidator(); + if (graphJsonValidator.verifyJanusGraphJson(jsonFilePath)) { + System.exit(2); + } + } + },NONE{ + @Override + void handle(String[] args) { + usage(); System.exit(1); } - boolean result = graphMLConverter.exportGraph(args); - if (result == false) { - System.exit(2); + void usage(){ + Arrays.stream(ExportImportEnum.values()).filter(type -> type != NONE).forEach(ExportImportEnum::usage); } + }; - break; - case "import": - isValid = verifyParamsLength(args, 3); - if (false == isValid) { - importUsage(); - System.exit(1); - } - result = graphMLConverter.importGraph(args); - if (result == false) { - System.exit(2); - } - break; + private static final GraphMLConverter GRAPH_ML_CONVERTER = new GraphMLConverter(); + private String usage; + private String keyword; - case "exportusers": - isValid = verifyParamsLength(args, 3); - if (false == isValid) { - importUsage(); - System.exit(1); - } - result = graphMLConverter.exportUsers(args); - if (result == false) { - System.exit(2); - } - break; + ExportImportEnum(String usage, String keyword) { + this.usage = usage; + this.keyword = keyword; + } - case "findproblem": - isValid = verifyParamsLength(args, 3); - if (false == isValid) { - importUsage(); - System.exit(1); - } - result = graphMLConverter.findErrorInJsonGraph(args); - if (result == false) { - System.exit(2); - } - break; - case "validate-json": - String jsonFilePath = validateAndGetJsonFilePath(args); - GraphJsonValidator graphJsonValidator = new GraphJsonValidator(); - if (graphJsonValidator.verifyJanusGraphJson(jsonFilePath)) { - System.exit(2); - } - break; + ExportImportEnum() {} - case "export-as-graph-ml": - isValid = verifyParamsLength(args, 3); - if (false == isValid) { - exportUsage(); - System.exit(1); - } - String mlFile = graphMLConverter.exportGraphMl(args); - if (mlFile == null) { - System.exit(2); - } - break; - case "export-as-graph-ml-with-data-report": - isValid = verifyParamsLength(args, 3); - if (false == isValid) { - exportUsage(); - System.exit(1); - } - mlFile = graphMLConverter.exportGraphMl(args); - if (mlFile == null) { - System.exit(2); - } - String[] dataArgs = new String[] { mlFile }; - mlFile = new GraphMLDataAnalyzer().analyzeGraphMLData(dataArgs); - if (mlFile == null) { - System.exit(2); - } - break; - case "get-data-report-from-graph-ml": - isValid = verifyParamsLength(args, 2); - if (false == isValid) { - dataReportUsage(); - System.exit(1); - } - dataArgs = new String[] { args[1] }; - mlFile = new GraphMLDataAnalyzer().analyzeGraphMLData(dataArgs); - if (mlFile == null) { - System.exit(2); - } - break; - default: - usageAndExit(); + void usage(){ + System.out.println(usage); } - } + static ExportImportEnum getByKeyword(String keyword) { + List collected = Arrays.stream(ExportImportEnum.values()) + .filter(type -> type != NONE) + .filter(type -> type.keyword.equals(keyword)) + .collect(Collectors.toList()); + return collected.isEmpty() ? NONE : collected.get(0); + } - private static String validateAndGetJsonFilePath(String[] args) { - boolean isValid; - isValid = verifyParamsLength(args, 2); - if (!isValid) { - validateJsonUsage(); - System.exit(1); - } - return args[1]; - } + abstract void handle(String[] args) throws IOException; - private static boolean verifyParamsLength(String[] args, int i) { - if (args == null) { - if (i > 0) { - return false; + private static boolean verifyParamsLength(String[] args, int i) { + if (args == null) { + return i > 0; } - return true; + return args.length < i; } + } + + public static void main(String[] args) throws Exception { + ExportImportEnum type; + if (args == null || args.length < 1) { + type = ExportImportEnum.NONE; + }else{ + type = ExportImportEnum.getByKeyword(getOperation(args).toLowerCase()); + } + type.handle(args); + } - if (args.length >= i) { - return true; + private static String getOperation(String[] args) { + String operation = null; + if (args != null) { + operation = args[0]; } - return false; + return operation; } } -- cgit 1.2.3-korg