2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.asdctool.main;
22 import static org.junit.Assert.assertEquals;
24 import java.nio.file.NoSuchFileException;
25 import java.security.Permission;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.contrib.java.lang.system.ExpectedSystemExit;
29 import org.junit.contrib.java.lang.system.SystemOutRule;
31 public class ExportImportMenuTest{
33 private static final String EXPORT_USAGE = "Usage: export <janusgraph.properties> <output directory>\n";
34 private static final String EXPORT_AS_GRAPH_ML_USAGE = "Usage: export-as-graph-ml <janusgraph.properties> <output directory>\n";
35 private static final String IMPORT_USAGE = "Usage: import <janusgraph.properties> <graph file location>\n";
36 private static final String EXPORT_USERS_USAGE = "Usage: exportusers <janusgraph.properties> <output directory>\n";
37 private static final String EXPORT_WITH_REPORT_USAGE = "Usage: export-as-graph-ml-with-data-report <janusgraph.properties> <output directory>\n";
38 private static final String DATA_REPORT_USAGE = "Usage: get-data-report-from-graph-ml <full path of .graphml file>\n";
39 private static final String VALIDATE_JSON_USAGE = "Usage: validate-json <export graph path>\n";
40 private static final String FIND_PROBLEM_USAGE = "Usage: findproblem <janusgraph.properties> <graph file location>\n";
41 private static final String USAGE = DATA_REPORT_USAGE + EXPORT_USAGE + EXPORT_AS_GRAPH_ML_USAGE + EXPORT_USERS_USAGE
42 + EXPORT_WITH_REPORT_USAGE + FIND_PROBLEM_USAGE + IMPORT_USAGE + VALIDATE_JSON_USAGE;
43 private static final String PARAM_3 = "param3";
44 private static final String PARAM_2 = "param2";
45 private static final String EXPORT = "export";
46 private static final String EXPORT_AS_GRAPH_ML = "export-as-graph-ml";
47 private static final String NONEXISTENT = "nonexistent";
48 private static final String IMPORT = "import";
49 private static final String EXPORT_USERS = "exportusers";
50 private static final String DATA_REPORT = "get-data-report-from-graph-ml";
51 private static final String FIND_PROBLEM = "findproblem";
52 private static final String VALIDATE_JSON = "validate-json";
53 private static final String EXPORT_WITH_REPORT = "export-as-graph-ml-with-data-report";
56 public final SystemOutRule systemOutRule = new SystemOutRule().enableLog();
59 public final ExpectedSystemExit exit = ExpectedSystemExit.none();
62 public void testOfMainWithInvalidLengthOfArgs() throws Exception {
64 exit.expectSystemExitWithStatus(1);
65 ExportImportMenu.main(args);
69 public void testOutputOfMainWithInvalidLengthOfArgs() {
71 callMainWithoutSystemExit(args);
72 String log = systemOutRule.getLog();
73 assertEquals(log, USAGE);
77 public void testOfMainWithDefaultOperation() throws Exception {
78 String [] args = {NONEXISTENT};
79 exit.expectSystemExitWithStatus(1);
80 ExportImportMenu.main(args);
84 public void testOutputOfOfMainWithDefaultOperation() {
85 String [] args = {NONEXISTENT};
86 callMainWithoutSystemExit(args);
87 String log = systemOutRule.getLog();
88 assertEquals(log, USAGE);
92 public void testOfMainWithExportOperationAndInvalidNoArgs() throws Exception {
93 String [] args = {EXPORT};
94 exit.expectSystemExitWithStatus(1);
95 ExportImportMenu.main(args);
99 public void testOutputOfMainWithExportOperationAndInvalidNoArgs(){
100 String [] args = {EXPORT};
101 callMainWithoutSystemExit(args);
102 String log = systemOutRule.getLog();
103 assertEquals(log, EXPORT_USAGE);
107 public void testOfMainWithExportOperationAndValidNoArgs() throws Exception {
108 String [] args = {EXPORT, PARAM_2, PARAM_3};
109 exit.expectSystemExitWithStatus(2);
110 ExportImportMenu.main(args);
114 public void testOutputOfMainWithExportUsersOperationAndInvalidNoArgs(){
115 String [] args = {EXPORT_USERS};
116 callMainWithoutSystemExit(args);
117 String log = systemOutRule.getLog();
118 assertEquals(log, EXPORT_USERS_USAGE);
122 public void testOfMainWithExportUsersOperationAndValidNoArgs() throws Exception {
123 String [] args = {EXPORT_USERS, PARAM_2, PARAM_3};
124 exit.expectSystemExitWithStatus(2);
125 ExportImportMenu.main(args);
129 public void testOutputOfMainWithImportOperationAndInvalidNoArgs(){
130 String [] args = {IMPORT};
131 callMainWithoutSystemExit(args);
132 String log = systemOutRule.getLog();
133 assertEquals(log, IMPORT_USAGE);
137 public void testOfMainWithImportOperationAndValidNoArgs() throws Exception {
138 String [] args = {IMPORT, PARAM_2, PARAM_3};
139 exit.expectSystemExitWithStatus(2);
140 ExportImportMenu.main(args);
144 public void testOutputOfMainWithDataReportOperationAndInvalidNoArgs(){
145 String [] args = {DATA_REPORT};
146 callMainWithoutSystemExit(args);
147 String log = systemOutRule.getLog();
148 assertEquals(log, DATA_REPORT_USAGE);
152 public void testOfMainWithDataReportOperationAndValidNoArgs() throws Exception {
153 String [] args = {DATA_REPORT, PARAM_2};
154 exit.expectSystemExitWithStatus(2);
155 ExportImportMenu.main(args);
159 public void testOutputOfMainWithExportAsGraphMLOperationAndInvalidNoArgs(){
160 String [] args = {EXPORT_AS_GRAPH_ML};
161 callMainWithoutSystemExit(args);
162 String log = systemOutRule.getLog();
163 assertEquals(log, EXPORT_AS_GRAPH_ML_USAGE);
167 public void testMainWithExportAsGraphMLOperationAndInvalidNoArgs() throws Exception {
168 String [] args = {EXPORT_AS_GRAPH_ML};
169 exit.expectSystemExitWithStatus(1);
170 ExportImportMenu.main(args);
174 public void testOfMainWithExportAsGraphMLOperationAndValidNoArgs() throws Exception {
175 String [] args = {EXPORT_AS_GRAPH_ML, PARAM_2, PARAM_3};
176 exit.expectSystemExitWithStatus(2);
177 ExportImportMenu.main(args);
181 public void testOutputOfMainWithFindProblemOperationAndInvalidNoArgs(){
182 String [] args = {FIND_PROBLEM};
183 callMainWithoutSystemExit(args);
184 String log = systemOutRule.getLog();
185 assertEquals(log, FIND_PROBLEM_USAGE);
189 public void testMainWithFindProblemOperationAndInvalidNoArgs() throws Exception {
190 String [] args = {FIND_PROBLEM};
191 exit.expectSystemExitWithStatus(1);
192 ExportImportMenu.main(args);
196 public void testOfMainWithFindProblemOperationAndValidNoArgs() throws Exception {
197 String [] args = {FIND_PROBLEM, PARAM_2, PARAM_3};
198 exit.expectSystemExitWithStatus(2);
199 ExportImportMenu.main(args);
203 public void testOutputOfMainWithExportWithReportOperationAndInvalidNoArgs(){
204 String [] args = {EXPORT_WITH_REPORT};
205 callMainWithoutSystemExit(args);
206 String log = systemOutRule.getLog();
207 assertEquals(log, EXPORT_WITH_REPORT_USAGE);
211 public void testMainWithExportWithReportOperationAndInvalidNoArgs() throws Exception {
212 String [] args = {EXPORT_WITH_REPORT};
213 exit.expectSystemExitWithStatus(1);
214 ExportImportMenu.main(args);
218 public void testOfMainWithExportWithReportOperationAndValidNoArgs() throws Exception {
219 String [] args = {EXPORT_WITH_REPORT, PARAM_2, PARAM_3};
220 exit.expectSystemExitWithStatus(2);
221 ExportImportMenu.main(args);
225 public void testOutputOfMainWithValidateJsonOperationAndInvalidNoArgs(){
226 String [] args = {VALIDATE_JSON};
227 callMainWithoutSystemExit(args);
228 String log = systemOutRule.getLog();
229 assertEquals(log, VALIDATE_JSON_USAGE);
233 public void testMainWithValidateJsonOperationAndInvalidNoArgs() throws Exception {
234 String [] args = {VALIDATE_JSON};
235 exit.expectSystemExitWithStatus(1);
236 ExportImportMenu.main(args);
239 @Test(expected = NoSuchFileException.class)
240 public void testOfMainWithValidateJsonOperationAndValidNoArgs() throws Exception {
241 String [] args = {VALIDATE_JSON, PARAM_2, PARAM_3};
242 ExportImportMenu.main(args);
245 private void callMainWithoutSystemExit(String[] params) {
247 class NoExitException extends RuntimeException {}
249 SecurityManager securityManager = System.getSecurityManager();
250 System.setSecurityManager(new SecurityManager(){
253 public void checkPermission(Permission permission) {
257 public void checkPermission(Permission permission, Object o) {
261 public void checkExit(int status) {
262 super.checkExit(status);
263 throw new NoExitException();
267 ExportImportMenu.main(params);
268 }catch (Exception ignore){}
269 System.setSecurityManager(securityManager);