1
1
package com .checkmarx .ast ;
2
2
3
+ import org .jetbrains .annotations .NotNull ;
3
4
import org .junit .Before ;
4
5
import org .junit .Test ;
6
+ import org .junit .runner .RunWith ;
7
+ import org .junit .runners .JUnit4 ;
5
8
import org .slf4j .Logger ;
6
9
import org .slf4j .LoggerFactory ;
7
10
8
11
import java .io .IOException ;
9
12
import java .net .URISyntaxException ;
10
- import java .util .ArrayList ;
11
13
import java .util .HashMap ;
12
14
import java .util .List ;
13
15
import java .util .Map ;
14
16
15
- import static org .junit .Assert .*;
17
+ import static org .junit .Assert .assertEquals ;
18
+ import static org .junit .Assert .assertTrue ;
16
19
20
+ @ RunWith (JUnit4 .class )
17
21
public class CxAuthTest {
18
- CxAuth auth = null ;
19
- private Logger log = LoggerFactory .getLogger (CxAuthTest .class .getName ());
20
- CxScanConfig config = new CxScanConfig ();
21
- List <CxScan > scanList = new ArrayList <>();
22
- Map <CxParamType , String > params = new HashMap <>();
23
- Map <String , String > environmentVariables = System .getenv ();
22
+ private static final Logger log = LoggerFactory .getLogger (CxAuthTest .class .getName ());
23
+
24
+ private static final int VALID_RETURN_CODE = 0 ;
25
+ private static final String FAILED = "failed" ;
26
+ private static final String COMPLETED = "completed" ;
27
+
28
+ private CxAuth auth ;
24
29
25
30
@ Before
26
31
public void init () throws IOException , URISyntaxException {
27
- if (environmentVariables .containsKey ("CX_CLIENT_ID" )) {
28
- config .setClientId (environmentVariables .get ("CX_CLIENT_ID" ));
29
- }
30
- if (environmentVariables .containsKey ("CX_CLIENT_SECRET" )) {
31
- config .setClientSecret (environmentVariables .get ("CX_CLIENT_SECRET" ));
32
- }
33
- if (environmentVariables .containsKey ("CX_APIKEY" )) {
34
- config .setApiKey (environmentVariables .get ("CX_APIKEY" ));
35
- }
36
- if (environmentVariables .containsKey ("CX_BASE_URI" )) {
37
- config .setBaseUri (environmentVariables .get ("CX_BASE_URI" ));
38
- }
39
- if (environmentVariables .containsKey ("CX_BASE_AUTH_URI" )) {
40
- config .setBaseAuthUri (environmentVariables .get ("CX_BASE_AUTH_URI" ));
41
- }
42
- if (environmentVariables .containsKey ("CX_TENANT" )) {
43
- config .setTenant (environmentVariables .get ("CX_TENANT" ));
44
- }
45
- if (environmentVariables .containsKey ("PATH_TO_EXECUTABLE" )) {
46
- config .setPathToExecutable (environmentVariables .get ("PATH_TO_EXECUTABLE" ));
47
- }
32
+ log .info ("Init test" );
48
33
34
+ Map <String , String > environmentVariables = System .getenv ();
35
+ CxScanConfig config = new CxScanConfig ();
36
+ config .setClientId (environmentVariables .getOrDefault ("CX_CLIENT_ID" , null ));
37
+ config .setClientSecret (environmentVariables .getOrDefault ("CX_CLIENT_SECRET" , null ));
38
+ config .setApiKey (environmentVariables .getOrDefault ("CX_APIKEY" , null ));
39
+ config .setBaseUri (environmentVariables .getOrDefault ("CX_BASE_URI" , null ));
40
+ config .setBaseAuthUri (environmentVariables .getOrDefault ("CX_BASE_AUTH_URI" , null ));
41
+ config .setTenant (environmentVariables .getOrDefault ("CX_TENANT" , null ));
42
+ config .setPathToExecutable (environmentVariables .getOrDefault ("PATH_TO_EXECUTABLE" , null ));
43
+
44
+ auth = new CxAuth (config , log );
45
+ }
46
+
47
+ @ NotNull
48
+ private Map <CxParamType , String > createParams () {
49
+ Map <CxParamType , String > params = new HashMap <>();
49
50
params .put (CxParamType .PROJECT_NAME , "TestCaseWrapper" );
50
51
params .put (CxParamType .SCAN_TYPES , "sast" );
51
52
params .put (CxParamType .S , "." );
52
53
params .put (CxParamType .FILTER , "*.java" );
53
- auth = new CxAuth (config , log );
54
+
55
+ return params ;
54
56
}
55
57
56
58
@ Test
59
+ public void cxScanShow () throws InterruptedException , IOException {
60
+ List <CxScan > scanList = auth .cxAstScanList ();
61
+
62
+ assertTrue (scanList .get (0 ) instanceof CxScan );
63
+ }
57
64
58
- public void cxScanShow () throws InterruptedException , IOException , URISyntaxException {
59
- init ();
60
- if (scanList == null || scanList .size () == 0 ) {
61
- cxAstScanList ();
62
- }
63
- if (scanList .size () > 0 ) {
64
- assertTrue (scanList .get (0 ) instanceof CxScan );
65
- }
65
+ @ Test
66
+ public void cxAstAuthValidate () throws IOException , InterruptedException {
67
+ Integer validate = auth .cxAuthValidate ();
66
68
69
+ assertEquals (VALID_RETURN_CODE , validate .intValue ());
67
70
}
68
71
69
72
@ Test
70
- public void cxAstScanList () throws IOException , InterruptedException , URISyntaxException {
71
- init ();
72
- scanList = auth . cxAstScanList ();
73
+ public void cxAstScanList () throws IOException , InterruptedException {
74
+ List < CxScan > scanList = auth . cxAstScanList ();
75
+
73
76
assertTrue (scanList .size () > 0 );
74
77
}
75
78
76
79
@ Test
77
- public void cxScanCreationWrongPreset () throws InterruptedException , IOException , URISyntaxException {
78
- init ();
80
+ public void cxScanCreationWrongPreset () throws InterruptedException , IOException {
81
+ Map < CxParamType , String > params = createParams ();
79
82
params .put (CxParamType .SAST_PRESET_NAME , "Checkmarx Default Jay" );
83
+
80
84
CxScan scanResult = auth .cxScanCreate (params );
81
- assertTrue (auth .cxScanShow (scanResult .getID ()).getStatus ().equalsIgnoreCase ("failed" ));
82
85
86
+ assertTrue (auth .cxScanShow (scanResult .getID ()).getStatus ().equalsIgnoreCase (FAILED ));
83
87
}
84
88
85
89
@ Test
86
- public void cxScanCreationSuccess () throws InterruptedException , IOException , URISyntaxException {
87
- init ();
90
+ public void cxScanCreationSuccess () throws InterruptedException , IOException {
91
+ Map < CxParamType , String > params = createParams ();
88
92
params .put (CxParamType .SAST_PRESET_NAME , "Checkmarx Default" );
93
+
89
94
CxScan scanResult = auth .cxScanCreate (params );
90
- assertTrue (auth .cxScanShow (scanResult .getID ()).getStatus ().equalsIgnoreCase ("completed" ));
95
+ assertTrue (auth .cxScanShow (scanResult .getID ()).getStatus ().equalsIgnoreCase (COMPLETED ));
91
96
}
92
97
}
0 commit comments