CSIT Fix for SDC-2585
[sdc.git] / asdctool / src / test / java / org / openecomp / sdc / asdctool / main / ExportImportMenuTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.openecomp.sdc.asdctool.main;
21
22 import static org.junit.Assert.assertEquals;
23
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;
30
31 public class ExportImportMenuTest{
32
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";
54
55     @Rule
56     public final SystemOutRule systemOutRule = new SystemOutRule().enableLog();
57
58     @Rule
59     public final ExpectedSystemExit exit = ExpectedSystemExit.none();
60
61     @Test
62     public void testOfMainWithInvalidLengthOfArgs() throws Exception {
63         String [] args = {};
64         exit.expectSystemExitWithStatus(1);
65         ExportImportMenu.main(args);
66     }
67
68     @Test
69     public void testOutputOfMainWithInvalidLengthOfArgs() {
70         String [] args = {};
71         callMainWithoutSystemExit(args);
72         String log = systemOutRule.getLog();
73         assertEquals(log, USAGE);
74     }
75
76     @Test
77     public void testOfMainWithDefaultOperation() throws Exception {
78         String [] args = {NONEXISTENT};
79         exit.expectSystemExitWithStatus(1);
80         ExportImportMenu.main(args);
81     }
82
83     @Test
84     public void testOutputOfOfMainWithDefaultOperation() {
85         String [] args = {NONEXISTENT};
86         callMainWithoutSystemExit(args);
87         String log = systemOutRule.getLog();
88         assertEquals(log, USAGE);
89     }
90
91     @Test
92     public void testOfMainWithExportOperationAndInvalidNoArgs() throws Exception {
93         String [] args = {EXPORT};
94         exit.expectSystemExitWithStatus(1);
95         ExportImportMenu.main(args);
96     }
97
98     @Test
99     public void testOutputOfMainWithExportOperationAndInvalidNoArgs(){
100         String [] args = {EXPORT};
101         callMainWithoutSystemExit(args);
102         String log = systemOutRule.getLog();
103         assertEquals(log, EXPORT_USAGE);
104     }
105
106     @Test
107     public void testOfMainWithExportOperationAndValidNoArgs() throws Exception {
108         String [] args = {EXPORT, PARAM_2, PARAM_3};
109         exit.expectSystemExitWithStatus(2);
110         ExportImportMenu.main(args);
111     }
112
113     @Test
114     public void testOutputOfMainWithExportUsersOperationAndInvalidNoArgs(){
115         String [] args = {EXPORT_USERS};
116         callMainWithoutSystemExit(args);
117         String log = systemOutRule.getLog();
118         assertEquals(log, EXPORT_USERS_USAGE);
119     }
120
121     @Test
122     public void testOfMainWithExportUsersOperationAndValidNoArgs() throws Exception {
123         String [] args = {EXPORT_USERS, PARAM_2, PARAM_3};
124         exit.expectSystemExitWithStatus(2);
125         ExportImportMenu.main(args);
126     }
127
128     @Test
129     public void testOutputOfMainWithImportOperationAndInvalidNoArgs(){
130         String [] args = {IMPORT};
131         callMainWithoutSystemExit(args);
132         String log = systemOutRule.getLog();
133         assertEquals(log, IMPORT_USAGE);
134     }
135
136     @Test
137     public void testOfMainWithImportOperationAndValidNoArgs() throws Exception {
138         String [] args = {IMPORT, PARAM_2, PARAM_3};
139         exit.expectSystemExitWithStatus(2);
140         ExportImportMenu.main(args);
141     }
142
143     @Test
144     public void testOutputOfMainWithDataReportOperationAndInvalidNoArgs(){
145         String [] args = {DATA_REPORT};
146         callMainWithoutSystemExit(args);
147         String log = systemOutRule.getLog();
148         assertEquals(log, DATA_REPORT_USAGE);
149     }
150
151     @Test
152     public void testOfMainWithDataReportOperationAndValidNoArgs() throws Exception {
153         String [] args = {DATA_REPORT, PARAM_2};
154         exit.expectSystemExitWithStatus(2);
155         ExportImportMenu.main(args);
156     }
157
158     @Test
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);
164     }
165
166     @Test
167     public void testMainWithExportAsGraphMLOperationAndInvalidNoArgs() throws Exception {
168         String [] args = {EXPORT_AS_GRAPH_ML};
169         exit.expectSystemExitWithStatus(1);
170         ExportImportMenu.main(args);
171     }
172
173     @Test
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);
178     }
179
180     @Test
181     public void testOutputOfMainWithFindProblemOperationAndInvalidNoArgs(){
182         String [] args = {FIND_PROBLEM};
183         callMainWithoutSystemExit(args);
184         String log = systemOutRule.getLog();
185         assertEquals(log, FIND_PROBLEM_USAGE);
186     }
187
188     @Test
189     public void testMainWithFindProblemOperationAndInvalidNoArgs() throws Exception {
190         String [] args = {FIND_PROBLEM};
191         exit.expectSystemExitWithStatus(1);
192         ExportImportMenu.main(args);
193     }
194
195     @Test
196     public void testOfMainWithFindProblemOperationAndValidNoArgs() throws Exception {
197         String [] args = {FIND_PROBLEM, PARAM_2, PARAM_3};
198         exit.expectSystemExitWithStatus(2);
199         ExportImportMenu.main(args);
200     }
201
202     @Test
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);
208     }
209
210     @Test
211     public void testMainWithExportWithReportOperationAndInvalidNoArgs() throws Exception {
212         String [] args = {EXPORT_WITH_REPORT};
213         exit.expectSystemExitWithStatus(1);
214         ExportImportMenu.main(args);
215     }
216
217     @Test
218     public void testOfMainWithExportWithReportOperationAndValidNoArgs() throws Exception {
219         String [] args = {EXPORT_WITH_REPORT, PARAM_2, PARAM_3};
220         exit.expectSystemExitWithStatus(2);
221         ExportImportMenu.main(args);
222     }
223
224     @Test
225     public void testOutputOfMainWithValidateJsonOperationAndInvalidNoArgs(){
226         String [] args = {VALIDATE_JSON};
227         callMainWithoutSystemExit(args);
228         String log = systemOutRule.getLog();
229         assertEquals(log, VALIDATE_JSON_USAGE);
230     }
231
232     @Test
233     public void testMainWithValidateJsonOperationAndInvalidNoArgs() throws Exception {
234         String [] args = {VALIDATE_JSON};
235         exit.expectSystemExitWithStatus(1);
236         ExportImportMenu.main(args);
237     }
238
239     @Test(expected = NoSuchFileException.class)
240     public void testOfMainWithValidateJsonOperationAndValidNoArgs() throws Exception {
241         String [] args = {VALIDATE_JSON, PARAM_2, PARAM_3};
242         ExportImportMenu.main(args);
243     }
244
245     private void callMainWithoutSystemExit(String[] params) {
246
247         class NoExitException extends RuntimeException {}
248
249         SecurityManager securityManager = System.getSecurityManager();
250         System.setSecurityManager(new SecurityManager(){
251
252             @Override
253             public void checkPermission(Permission permission) {
254             }
255
256             @Override
257             public void checkPermission(Permission permission, Object o) {
258             }
259
260             @Override
261             public void checkExit(int status) {
262                 super.checkExit(status);
263                 throw new NoExitException();
264             }
265         });
266         try {
267             ExportImportMenu.main(params);
268         }catch (Exception ignore){}
269         System.setSecurityManager(securityManager);
270     }
271
272 }