11package com .checkmarx .ast ;
22
3+ import org .jetbrains .annotations .NotNull ;
34import org .junit .Before ;
45import org .junit .Test ;
6+ import org .junit .runner .RunWith ;
7+ import org .junit .runners .JUnit4 ;
58import org .slf4j .Logger ;
69import org .slf4j .LoggerFactory ;
710
811import java .io .IOException ;
912import java .net .URISyntaxException ;
10- import java .util .ArrayList ;
1113import java .util .HashMap ;
1214import java .util .List ;
1315import java .util .Map ;
1416
15- import static org .junit .Assert .*;
17+ import static org .junit .Assert .assertEquals ;
18+ import static org .junit .Assert .assertTrue ;
1619
20+ @ RunWith (JUnit4 .class )
1721public 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 ;
2429
2530 @ Before
2631 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" );
4833
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 <>();
4950 params .put (CxParamType .PROJECT_NAME , "TestCaseWrapper" );
5051 params .put (CxParamType .SCAN_TYPES , "sast" );
5152 params .put (CxParamType .S , "." );
5253 params .put (CxParamType .FILTER , "*.java" );
53- auth = new CxAuth (config , log );
54+
55+ return params ;
5456 }
5557
5658 @ Test
59+ public void cxScanShow () throws InterruptedException , IOException {
60+ List <CxScan > scanList = auth .cxAstScanList ();
61+
62+ assertTrue (scanList .get (0 ) instanceof CxScan );
63+ }
5764
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 ();
6668
69+ assertEquals (VALID_RETURN_CODE , validate .intValue ());
6770 }
6871
6972 @ 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+
7376 assertTrue (scanList .size () > 0 );
7477 }
7578
7679 @ Test
77- public void cxScanCreationWrongPreset () throws InterruptedException , IOException , URISyntaxException {
78- init ();
80+ public void cxScanCreationWrongPreset () throws InterruptedException , IOException {
81+ Map < CxParamType , String > params = createParams ();
7982 params .put (CxParamType .SAST_PRESET_NAME , "Checkmarx Default Jay" );
83+
8084 CxScan scanResult = auth .cxScanCreate (params );
81- assertTrue (auth .cxScanShow (scanResult .getID ()).getStatus ().equalsIgnoreCase ("failed" ));
8285
86+ assertTrue (auth .cxScanShow (scanResult .getID ()).getStatus ().equalsIgnoreCase (FAILED ));
8387 }
8488
8589 @ Test
86- public void cxScanCreationSuccess () throws InterruptedException , IOException , URISyntaxException {
87- init ();
90+ public void cxScanCreationSuccess () throws InterruptedException , IOException {
91+ Map < CxParamType , String > params = createParams ();
8892 params .put (CxParamType .SAST_PRESET_NAME , "Checkmarx Default" );
93+
8994 CxScan scanResult = auth .cxScanCreate (params );
90- assertTrue (auth .cxScanShow (scanResult .getID ()).getStatus ().equalsIgnoreCase ("completed" ));
95+ assertTrue (auth .cxScanShow (scanResult .getID ()).getStatus ().equalsIgnoreCase (COMPLETED ));
9196 }
9297}
0 commit comments