Skip to content

Commit aca6bea

Browse files
authored
Merge pull request #179 from Checkmarx/feature/elchananarb/add-integration-tests
Add integration test
2 parents 624705c + efee2e5 commit aca6bea

7 files changed

Lines changed: 253 additions & 82 deletions

File tree

.github/workflows/ci.yml

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,55 @@
1-
name: Checkmarx One Eclipse Plugin
2-
3-
on: [pull_request]
1+
name: Integration Tests
2+
on:
3+
pull_request:
4+
branches:
5+
- main
46

57
jobs:
8+
9+
ui-tests:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v3.5.2
13+
- uses: actions/setup-java@v3
14+
with:
15+
distribution: temurin
16+
java-version: 8
17+
- uses: actions/cache@v3
18+
with:
19+
path: ~/.m2/repository
20+
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
21+
restore-keys: ${{ runner.os }}-maven-
22+
- name: Run UI Tests
23+
env:
24+
CX_BASE_URI: ${{ secrets.CX_BASE_URI }}
25+
CX_TENANT: ${{ secrets.CX_TENANT }}
26+
CX_APIKEY: ${{ secrets.CX_APIKEY }}
27+
CX_TEST_SCAN: ${{ secrets.CX_TEST_SCAN }}
28+
DISPLAY: :99.0
29+
run: |
30+
Xvfb -ac :99 -screen 0 1920x1080x16 &
31+
mvn verify -Dtest.includes="**/ui/*.java"
32+
633
integration-tests:
734
runs-on: ubuntu-latest
835
steps:
9-
- name: Checkout
10-
uses: actions/checkout@v3.5.2
11-
12-
- name: Set up JDK 8
13-
uses: actions/setup-java@v3
36+
- uses: actions/checkout@v3.5.2
37+
- uses: actions/setup-java@v3
1438
with:
1539
distribution: temurin
1640
java-version: 8
17-
18-
- name: Cache local Maven repository
19-
uses: actions/cache@v3
41+
- uses: actions/cache@v3
2042
with:
2143
path: ~/.m2/repository
2244
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
23-
restore-keys: |
24-
${{ runner.os }}-maven-
25-
26-
- name: Run tests with Maven
45+
restore-keys: ${{ runner.os }}-maven-
46+
- name: Run Integration Tests
2747
env:
2848
CX_BASE_URI: ${{ secrets.CX_BASE_URI }}
2949
CX_TENANT: ${{ secrets.CX_TENANT }}
3050
CX_APIKEY: ${{ secrets.CX_APIKEY }}
3151
CX_TEST_SCAN: ${{ secrets.CX_TEST_SCAN }}
32-
CX_NOT_MATCH_TEST_PROJECT: ${{ secrets.CX_NOT_MATCH_TEST_PROJECT }}
52+
DISPLAY: :99.0
3353
run: |
34-
export DISPLAY=:99.0
35-
Xvfb -ac :99 -screen 0 1920x1080x16 & mvn clean install
54+
Xvfb -ac :99 -screen 0 1920x1080x16 &
55+
mvn verify -Dtest.includes="**/integration/*Test.java"
Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,44 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4-
<modelVersion>4.0.0</modelVersion>
5-
<groupId>com.checkmarx.ast.eclipse.tests</groupId>
6-
<artifactId>com.checkmarx.ast.eclipse.tests</artifactId>
7-
<description>Checkmarx Eclipse Plugin Unit Tests Project</description>
8-
<packaging>eclipse-test-plugin</packaging>
9-
<parent>
10-
<groupId>com.checkmarx.ast.eclipse</groupId>
11-
<artifactId>checkmarx-eclipse-plugin</artifactId>
12-
<version>1.0.0-SNAPSHOT</version>
13-
</parent>
14-
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.checkmarx.ast.eclipse.tests</groupId>
6+
<artifactId>com.checkmarx.ast.eclipse.tests</artifactId>
7+
<description>Checkmarx Eclipse Plugin Integration Tests Project</description>
8+
<packaging>eclipse-test-plugin</packaging>
9+
<parent>
10+
<groupId>com.checkmarx.ast.eclipse</groupId>
11+
<artifactId>checkmarx-eclipse-plugin</artifactId>
12+
<version>1.0.0-SNAPSHOT</version>
13+
</parent>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>junit</groupId>
18+
<artifactId>junit</artifactId>
19+
<version>4.13.2</version>
20+
<scope>test</scope>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.mockito</groupId>
24+
<artifactId>mockito-core</artifactId>
25+
<version>5.6.0</version>
26+
<scope>test</scope>
27+
</dependency>
28+
</dependencies>
29+
30+
<build>
31+
<plugins>
32+
<plugin>
33+
<groupId>org.eclipse.tycho</groupId>
34+
<artifactId>tycho-surefire-plugin</artifactId>
35+
<version>${tycho.version}</version>
36+
<configuration>
37+
<includes>
38+
<include>${test.includes}</include>
39+
</includes>
40+
</configuration>
41+
</plugin>
42+
</plugins>
43+
</build>
1544
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package checkmarx.ast.eclipse.plugin.tests.integration;
2+
3+
import static org.junit.Assert.*;
4+
import org.junit.Test;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import com.checkmarx.eclipse.runner.Authenticator;
8+
import java.io.File;
9+
10+
public class AuthenticatorIntegrationTest extends BaseIntegrationTest {
11+
12+
private static final Logger logger = LoggerFactory.getLogger(AuthenticatorIntegrationTest.class);
13+
14+
@Test
15+
public void testSuccessfulAuthentication() {
16+
System.out.println("\n=== Starting Authentication Test ===");
17+
System.out.println("Current directory: " + new File(".").getAbsolutePath());
18+
System.out.println("API Key available: " + (VALID_API_KEY != null));
19+
String result = authenticator.doAuthentication(VALID_API_KEY, "");
20+
System.out.println("Authentication result: " + result);
21+
assertNotNull("Authentication result should not be null", result);
22+
assertFalse("Authentication result should not contain error", result.toLowerCase().contains("error"));
23+
System.out.println("=== Authentication Test Completed ===\n");
24+
}
25+
26+
@Test
27+
public void testInvalidApiKeyAuthentication() {
28+
System.out.println("\n=== Starting Invalid API Key Test ===");
29+
String invalidApiKey = "invalid-api-key";
30+
String result = authenticator.doAuthentication(invalidApiKey, "");
31+
System.out.println("Authentication result with invalid API key: " + result);
32+
assertNotNull("Result should not be null for invalid API key", result);
33+
assertTrue("Result should contain error for invalid API key", result.toLowerCase().contains("error"));
34+
System.out.println("=== Invalid API Key Test Completed ===\n");
35+
}
36+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package checkmarx.ast.eclipse.plugin.tests.integration;
2+
3+
import org.junit.Before;
4+
import org.junit.BeforeClass;
5+
import org.mockito.Mock;
6+
import org.mockito.MockitoAnnotations;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
10+
import com.checkmarx.ast.wrapper.CxConfig;
11+
import com.checkmarx.ast.wrapper.CxWrapper;
12+
import com.checkmarx.eclipse.runner.Authenticator;
13+
14+
import checkmarx.ast.eclipse.plugin.tests.common.Environment;
15+
16+
public abstract class BaseIntegrationTest {
17+
18+
private static final Logger logger = LoggerFactory.getLogger(BaseIntegrationTest.class);
19+
20+
protected static final String VALID_SCAN_ID = Environment.SCAN_ID;
21+
protected static final String INVALID_SCAN_ID = "invalid-scan-id";
22+
protected static final String VALID_API_KEY = Environment.API_KEY;
23+
24+
@Mock
25+
protected Logger mockLogger;
26+
27+
protected Authenticator authenticator;
28+
protected CxWrapper cxWrapper;
29+
protected static boolean initialized = false;
30+
31+
@Before
32+
public void setUp() throws Exception {
33+
logger.info("Setting up test: {}", this.getClass().getSimpleName());
34+
logger.info("API Key available: {}", VALID_API_KEY != null);
35+
MockitoAnnotations.initMocks(this);
36+
authenticator = new Authenticator(mockLogger);
37+
38+
if (!initialized) {
39+
initializeCxWrapper();
40+
initialized = true;
41+
}
42+
}
43+
44+
protected void initializeCxWrapper() throws Exception {
45+
logger.info("Initializing CxWrapper");
46+
CxConfig config = CxConfig.builder()
47+
.apiKey(VALID_API_KEY)
48+
.build();
49+
cxWrapper = new CxWrapper(config, mockLogger);
50+
logger.info("CxWrapper initialized successfully");
51+
}
52+
53+
protected void reinitializeCxWrapper(String apiKey) throws Exception {
54+
logger.info("Reinitializing CxWrapper with new API key");
55+
CxConfig config = CxConfig.builder()
56+
.apiKey(apiKey)
57+
.build();
58+
cxWrapper = new CxWrapper(config, mockLogger);
59+
logger.info("CxWrapper reinitialized successfully");
60+
}
61+
}

checkmarx-ast-eclipse-plugin-tests/src/test/java/checkmarx/ast/eclipse/plugin/tests/ui/TestScan.java

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,32 +45,54 @@ public void testScanButtonsDisabledWhenMissingProjectOrBranch() throws TimeoutEx
4545

4646
@Test
4747
public void testScanProjectDoesNotMatch() throws TimeoutException {
48-
// Used to wait for scan to finish
49-
SWTBotPreferences.TIMEOUT = 300000; // 5minutes
50-
51-
testSuccessfulConnection(false);
48+
// Set SWTBot timeout to 5 minutes to wait for slow operations
49+
SWTBotPreferences.TIMEOUT = 300000; // 5 minutes
5250

53-
addCheckmarxPlugin(true);
54-
55-
preventWidgetWasNullInCIEnvironment();
56-
57-
_bot.comboBox(2).setText(Environment.SCAN_ID_PROJECT_DOES_NOT_MATCH);
58-
_bot.comboBox(2).pressShortcut(Keystrokes.LF);
59-
60-
waitUntilBranchComboIsEnabled();
61-
62-
_bot.waitUntil(startScanButtonEnabled);
63-
64-
SWTBotToolbarButton startBtn = _bot.viewByTitle(VIEW_CHECKMARX_AST_SCAN).getToolbarButtons().stream().filter(btn -> btn.getToolTipText().equals(PluginConstants.CX_START_SCAN)).findFirst().get();
65-
startBtn.click();
66-
67-
SWTBotShell shell = _bot.shell(PluginConstants.CX_PROJECT_MISMATCH);
68-
shell.activate();
69-
70-
_bot.button(BTN_NO).click();
71-
72-
SWTBotPreferences.TIMEOUT = 5000;
51+
// Ensure successful connection to the server
52+
testSuccessfulConnection(false);
53+
54+
// Add the Checkmarx plugin to the environment
55+
addCheckmarxPlugin(true);
56+
57+
// Handle potential widget null issues in CI environment
58+
preventWidgetWasNullInCIEnvironment();
59+
60+
// Set the project ID in the combo box
61+
_bot.comboBox(2).setText("9a9c75a5-2fb1-460c-80f7-f902790422b0");
62+
_bot.comboBox(2).pressShortcut(Keystrokes.LF);
63+
64+
// Wait until the branch combo box is enabled
65+
waitUntilBranchComboIsEnabled();
66+
67+
// Wait until the Start Scan button is enabled
68+
_bot.waitUntil(startScanButtonEnabled);
69+
70+
// Add a 30-second delay
71+
try {
72+
Thread.sleep(30000); // 30 seconds
73+
} catch (InterruptedException e) {
74+
e.printStackTrace(); // Handle interruption
75+
}
76+
77+
// Find and click the Start Scan button
78+
SWTBotToolbarButton startBtn = _bot.viewByTitle(VIEW_CHECKMARX_AST_SCAN)
79+
.getToolbarButtons().stream()
80+
.filter(btn -> btn.getToolTipText().equals(PluginConstants.CX_START_SCAN))
81+
.findFirst()
82+
.orElseThrow(() -> new RuntimeException("Start Scan button not found"));
83+
startBtn.click();
84+
85+
// Wait for and activate the project mismatch dialog
86+
SWTBotShell shell = _bot.shell(PluginConstants.CX_PROJECT_MISMATCH);
87+
shell.activate();
88+
89+
// Click the "No" button in the dialog
90+
_bot.button(BTN_NO).click();
91+
92+
// Reset SWTBot timeout to 5 seconds
93+
SWTBotPreferences.TIMEOUT = 5000;
7394
}
95+
7496

7597
@Test
7698
public void testCancelScan() throws TimeoutException {

checkmarx-ast-eclipse-plugin/.classpath

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@
1919
<classpathentry kind="src" path="src"/>
2020
<classpathentry kind="lib" path="lib/slf4j-reload4j-1.7.36.jar"/>
2121
<classpathentry kind="output" path="target/classes"/>
22-
</classpath>
22+
</classpath>

checkmarx-ast-eclipse-plugin/src/com/checkmarx/eclipse/runner/Authenticator.java

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,33 @@
1212
import com.checkmarx.eclipse.utils.PluginConstants;
1313

1414
public class Authenticator {
15-
16-
17-
protected static final String AUTH_STATUS = "Authentication Status: ";
18-
19-
public static final Authenticator INSTANCE = new Authenticator();
20-
21-
public String doAuthentication(String apiKey,
22-
String additionalParams) {
23-
24-
CxConfig config = CxConfig.builder().apiKey(apiKey)
25-
.additionalParameters(additionalParams).build();
26-
27-
Logger log = LoggerFactory.getLogger(Authenticator.class.getName());
28-
29-
CxWrapper wrapper;
30-
try {
31-
wrapper = new CxWrapper(config, log);
32-
String cxValidateOutput = wrapper.authValidate();
33-
CxLogger.info(AUTH_STATUS + cxValidateOutput);
34-
return cxValidateOutput;
35-
} catch (IOException | InterruptedException | CxException e) {
36-
CxLogger.error(String.format(PluginConstants.ERROR_AUTHENTICATING_AST, e.getMessage()), e);
37-
return e.getMessage();
38-
}
39-
}
40-
41-
}
15+
private final Logger log;
16+
17+
private Authenticator() {
18+
this.log = LoggerFactory.getLogger(Authenticator.class);
19+
}
20+
21+
// for test only
22+
public Authenticator(Logger logger) {
23+
this.log = logger;
24+
}
25+
26+
protected static final String AUTH_STATUS = "Authentication Status: ";
27+
public static final Authenticator INSTANCE = new Authenticator();
28+
29+
public String doAuthentication(String apiKey, String additionalParams) {
30+
CxConfig config = CxConfig.builder()
31+
.apiKey(apiKey)
32+
.additionalParameters(additionalParams)
33+
.build();
34+
try {
35+
CxWrapper wrapper = new CxWrapper(config, log);
36+
String cxValidateOutput = wrapper.authValidate();
37+
log.info(AUTH_STATUS + cxValidateOutput);
38+
return cxValidateOutput;
39+
} catch (IOException | InterruptedException | CxException e) {
40+
log.error(String.format(PluginConstants.ERROR_AUTHENTICATING_AST, e.getMessage()), e);
41+
return e.getMessage();
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)