diff options
author | Tomasz Golabek <tomasz.golabek@nokia.com> | 2019-06-07 09:28:23 +0200 |
---|---|---|
committer | Ofir Sonsino <ofir.sonsino@intl.att.com> | 2019-06-23 11:22:37 +0000 |
commit | aa18a9537994b8637d40516aab71fa82cf55c2d3 (patch) | |
tree | 944ac0fd22981f82fccb8146dd397336ae582037 | |
parent | 4c2766df0ffa61835c90d7eb869e4fb285af23f0 (diff) |
ExportImportMenu tests
Main class tested and refactored
Change-Id: I379a18c289b613cdc7ecbb1617618af96ffa275b
Issue-ID: SDC-2326
Signed-off-by: Tomasz Golabek <tomasz.golabek@nokia.com>
3 files changed, 435 insertions, 152 deletions
diff --git a/asdctool/pom.xml b/asdctool/pom.xml index bfc1f00d43..d754deced7 100644 --- a/asdctool/pom.xml +++ b/asdctool/pom.xml @@ -466,12 +466,20 @@ <scope>test</scope> </dependency> - <dependency> - <groupId>org.assertj</groupId> - <artifactId>assertj-core</artifactId> - <scope>test</scope> - </dependency> - <!-- testing end --> + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>com.github.stefanbirkner</groupId> + <artifactId>system-rules</artifactId> + <version>1.19.0</version> + <scope>test</scope> + </dependency> + + <!-- testing end --> <dependency> <groupId>io.netty</groupId> 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 <janusgraph.properties> <graph file location>"); - } - - private static void validateJsonUsage() { - System.out.println("Usage: validate-json <export graph path>"); - } - - private static void exportUsage() { - System.out.println("Usage: export <janusgraph.properties> <output directory>"); - } - - private static void dataReportUsage() { - System.out.println("Usage: get-data-report-from-graph-ml <full path of .graphml file>"); - } - - private static void exportUsersUsage() { - System.out.println("Usage: exportusers <janusgraph.properties> <output directory>"); - } - - 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 <full path of .graphml file>", "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 <janusgraph.properties> <output directory>", "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 <janusgraph.properties> <output directory>", "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 <janusgraph.properties> <output directory>", "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 <janusgraph.properties> <output directory>", "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 <janusgraph.properties> <graph file location>", "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 <janusgraph.properties> <graph file location>", "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 <export graph path>", "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<ExportImportEnum> 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; } } diff --git a/asdctool/src/test/java/org/openecomp/sdc/asdctool/main/ExportImportMenuTest.java b/asdctool/src/test/java/org/openecomp/sdc/asdctool/main/ExportImportMenuTest.java new file mode 100644 index 0000000000..497e116a57 --- /dev/null +++ b/asdctool/src/test/java/org/openecomp/sdc/asdctool/main/ExportImportMenuTest.java @@ -0,0 +1,272 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ +package org.openecomp.sdc.asdctool.main; + +import static org.junit.Assert.assertEquals; + +import java.nio.file.NoSuchFileException; +import java.security.Permission; +import org.junit.Rule; +import org.junit.Test; +import org.junit.contrib.java.lang.system.ExpectedSystemExit; +import org.junit.contrib.java.lang.system.SystemOutRule; + +public class ExportImportMenuTest{ + + private static final String EXPORT_USAGE = "Usage: export <janusgraph.properties> <output directory>\n"; + private static final String EXPORT_AS_GRAPH_ML_USAGE = "Usage: export-as-graph-ml <janusgraph.properties> <output directory>\n"; + private static final String IMPORT_USAGE = "Usage: import <janusgraph.properties> <graph file location>\n"; + private static final String EXPORT_USERS_USAGE = "Usage: exportusers <janusgraph.properties> <output directory>\n"; + private static final String EXPORT_WITH_REPORT_USAGE = "Usage: export-as-graph-ml-with-data-report <janusgraph.properties> <output directory>\n"; + private static final String DATA_REPORT_USAGE = "Usage: get-data-report-from-graph-ml <full path of .graphml file>\n"; + private static final String VALIDATE_JSON_USAGE = "Usage: validate-json <export graph path>\n"; + private static final String FIND_PROBLEM_USAGE = "Usage: findproblem <janusgraph.properties> <graph file location>\n"; + private static final String USAGE = DATA_REPORT_USAGE + EXPORT_USAGE + EXPORT_AS_GRAPH_ML_USAGE + EXPORT_USERS_USAGE + + EXPORT_WITH_REPORT_USAGE + FIND_PROBLEM_USAGE + IMPORT_USAGE + VALIDATE_JSON_USAGE; + private static final String PARAM_3 = "param3"; + private static final String PARAM_2 = "param2"; + private static final String EXPORT = "export"; + private static final String EXPORT_AS_GRAPH_ML = "export-as-graph-ml"; + private static final String NONEXISTENT = "nonexistent"; + private static final String IMPORT = "import"; + private static final String EXPORT_USERS = "exportusers"; + private static final String DATA_REPORT = "get-data-report-from-graph-ml"; + private static final String FIND_PROBLEM = "findproblem"; + private static final String VALIDATE_JSON = "validate-json"; + private static final String EXPORT_WITH_REPORT = "export-as-graph-ml-with-data-report"; + + @Rule + public final SystemOutRule systemOutRule = new SystemOutRule().enableLog(); + + @Rule + public final ExpectedSystemExit exit = ExpectedSystemExit.none(); + + @Test + public void testOfMainWithInvalidLengthOfArgs() throws Exception { + String [] args = {}; + exit.expectSystemExitWithStatus(1); + ExportImportMenu.main(args); + } + + @Test + public void testOutputOfMainWithInvalidLengthOfArgs() { + String [] args = {}; + callMainWithoutSystemExit(args); + String log = systemOutRule.getLog(); + assertEquals(log, USAGE); + } + + @Test + public void testOfMainWithDefaultOperation() throws Exception { + String [] args = {NONEXISTENT}; + exit.expectSystemExitWithStatus(1); + ExportImportMenu.main(args); + } + + @Test + public void testOutputOfOfMainWithDefaultOperation() { + String [] args = {NONEXISTENT}; + callMainWithoutSystemExit(args); + String log = systemOutRule.getLog(); + assertEquals(log, USAGE); + } + + @Test + public void testOfMainWithExportOperationAndInvalidNoArgs() throws Exception { + String [] args = {EXPORT}; + exit.expectSystemExitWithStatus(1); + ExportImportMenu.main(args); + } + + @Test + public void testOutputOfMainWithExportOperationAndInvalidNoArgs(){ + String [] args = {EXPORT}; + callMainWithoutSystemExit(args); + String log = systemOutRule.getLog(); + assertEquals(log, EXPORT_USAGE); + } + + @Test + public void testOfMainWithExportOperationAndValidNoArgs() throws Exception { + String [] args = {EXPORT, PARAM_2, PARAM_3}; + exit.expectSystemExitWithStatus(2); + ExportImportMenu.main(args); + } + + @Test + public void testOutputOfMainWithExportUsersOperationAndInvalidNoArgs(){ + String [] args = {EXPORT_USERS}; + callMainWithoutSystemExit(args); + String log = systemOutRule.getLog(); + assertEquals(log, EXPORT_USERS_USAGE); + } + + @Test + public void testOfMainWithExportUsersOperationAndValidNoArgs() throws Exception { + String [] args = {EXPORT_USERS, PARAM_2, PARAM_3}; + exit.expectSystemExitWithStatus(2); + ExportImportMenu.main(args); + } + + @Test + public void testOutputOfMainWithImportOperationAndInvalidNoArgs(){ + String [] args = {IMPORT}; + callMainWithoutSystemExit(args); + String log = systemOutRule.getLog(); + assertEquals(log, IMPORT_USAGE); + } + + @Test + public void testOfMainWithImportOperationAndValidNoArgs() throws Exception { + String [] args = {IMPORT, PARAM_2, PARAM_3}; + exit.expectSystemExitWithStatus(2); + ExportImportMenu.main(args); + } + + @Test + public void testOutputOfMainWithDataReportOperationAndInvalidNoArgs(){ + String [] args = {DATA_REPORT}; + callMainWithoutSystemExit(args); + String log = systemOutRule.getLog(); + assertEquals(log, DATA_REPORT_USAGE); + } + + @Test + public void testOfMainWithDataReportOperationAndValidNoArgs() throws Exception { + String [] args = {DATA_REPORT, PARAM_2}; + exit.expectSystemExitWithStatus(2); + ExportImportMenu.main(args); + } + + @Test + public void testOutputOfMainWithExportAsGraphMLOperationAndInvalidNoArgs(){ + String [] args = {EXPORT_AS_GRAPH_ML}; + callMainWithoutSystemExit(args); + String log = systemOutRule.getLog(); + assertEquals(log, EXPORT_AS_GRAPH_ML_USAGE); + } + + @Test + public void testMainWithExportAsGraphMLOperationAndInvalidNoArgs() throws Exception { + String [] args = {EXPORT_AS_GRAPH_ML}; + exit.expectSystemExitWithStatus(1); + ExportImportMenu.main(args); + } + + @Test + public void testOfMainWithExportAsGraphMLOperationAndValidNoArgs() throws Exception { + String [] args = {EXPORT_AS_GRAPH_ML, PARAM_2, PARAM_3}; + exit.expectSystemExitWithStatus(2); + ExportImportMenu.main(args); + } + + @Test + public void testOutputOfMainWithFindProblemOperationAndInvalidNoArgs(){ + String [] args = {FIND_PROBLEM}; + callMainWithoutSystemExit(args); + String log = systemOutRule.getLog(); + assertEquals(log, FIND_PROBLEM_USAGE); + } + + @Test + public void testMainWithFindProblemOperationAndInvalidNoArgs() throws Exception { + String [] args = {FIND_PROBLEM}; + exit.expectSystemExitWithStatus(1); + ExportImportMenu.main(args); + } + + @Test + public void testOfMainWithFindProblemOperationAndValidNoArgs() throws Exception { + String [] args = {FIND_PROBLEM, PARAM_2, PARAM_3}; + exit.expectSystemExitWithStatus(2); + ExportImportMenu.main(args); + } + + @Test + public void testOutputOfMainWithExportWithReportOperationAndInvalidNoArgs(){ + String [] args = {EXPORT_WITH_REPORT}; + callMainWithoutSystemExit(args); + String log = systemOutRule.getLog(); + assertEquals(log, EXPORT_WITH_REPORT_USAGE); + } + + @Test + public void testMainWithExportWithReportOperationAndInvalidNoArgs() throws Exception { + String [] args = {EXPORT_WITH_REPORT}; + exit.expectSystemExitWithStatus(1); + ExportImportMenu.main(args); + } + + @Test + public void testOfMainWithExportWithReportOperationAndValidNoArgs() throws Exception { + String [] args = {EXPORT_WITH_REPORT, PARAM_2, PARAM_3}; + exit.expectSystemExitWithStatus(2); + ExportImportMenu.main(args); + } + + @Test + public void testOutputOfMainWithValidateJsonOperationAndInvalidNoArgs(){ + String [] args = {VALIDATE_JSON}; + callMainWithoutSystemExit(args); + String log = systemOutRule.getLog(); + assertEquals(log, VALIDATE_JSON_USAGE); + } + + @Test + public void testMainWithValidateJsonOperationAndInvalidNoArgs() throws Exception { + String [] args = {VALIDATE_JSON}; + exit.expectSystemExitWithStatus(1); + ExportImportMenu.main(args); + } + + @Test(expected = NoSuchFileException.class) + public void testOfMainWithValidateJsonOperationAndValidNoArgs() throws Exception { + String [] args = {VALIDATE_JSON, PARAM_2, PARAM_3}; + ExportImportMenu.main(args); + } + + private void callMainWithoutSystemExit(String[] params) { + + class NoExitException extends RuntimeException {} + + SecurityManager securityManager = System.getSecurityManager(); + System.setSecurityManager(new SecurityManager(){ + + @Override + public void checkPermission(Permission permission) { + } + + @Override + public void checkPermission(Permission permission, Object o) { + } + + @Override + public void checkExit(int status) { + super.checkExit(status); + throw new NoExitException(); + } + }); + try { + ExportImportMenu.main(params); + }catch (Exception ignore){} + System.setSecurityManager(securityManager); + } + +}
\ No newline at end of file |