Skip to content

Commit ee12757

Browse files
author
elchananarb
committed
Update TestPreferences.java
1 parent df938ca commit ee12757

File tree

1 file changed

+74
-22
lines changed
  • checkmarx-ast-eclipse-plugin-tests/src/test/java/checkmarx/ast/eclipse/plugin/tests/ui

1 file changed

+74
-22
lines changed
Lines changed: 74 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,124 @@
11
package checkmarx.ast.eclipse.plugin.tests.ui;
22

3-
import static org.junit.Assert.assertTrue;
43
import static org.junit.Assert.assertFalse;
4+
import static org.junit.Assert.assertTrue;
5+
import static org.junit.Assert.assertEquals;
56

67
import java.util.concurrent.TimeoutException;
78

89
import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
10+
import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
911
import org.junit.Test;
1012
import org.junit.runner.RunWith;
1113

1214
import com.checkmarx.eclipse.utils.PluginConstants;
1315

16+
import checkmarx.ast.eclipse.plugin.tests.common.Environment;
17+
1418
@RunWith(SWTBotJunit4ClassRunner.class)
15-
public class ApiKeyTest extends BaseUITest {
19+
public class TestPreferences extends BaseUITest {
20+
21+
private static final String ASSERT_API_KEY_EMPTY = "API Key field must not be empty after setting";
22+
private static final String ASSERT_CONNECTION_FAILED = "Connection should fail with an invalid API key";
23+
private static final String ASSERT_CONNECTION_SUCCESS = "Connection should succeed with a valid API key";
1624

1725
@Test
1826
public void testValidApiKeyConnection() throws TimeoutException {
19-
// Open the Preferences window
27+
preventWidgetWasNullInCIEnvironment();
28+
29+
// Open Preferences
2030
_bot.menu(TAB_WINDOW).menu(ITEM_PREFERENCES).click();
2131
_bot.shell(ITEM_PREFERENCES).activate();
2232
_bot.tree().select(ITEM_CHECKMARX_AST);
2333

24-
// Enter a valid API Key
25-
String validApiKey = "your-valid-api-key"; // Replace with a valid API key
26-
_bot.textWithLabel(PluginConstants.PREFERENCES_API_KEY).setText(validApiKey);
34+
// Set valid API key
35+
_bot.textWithLabel(PluginConstants.PREFERENCES_API_KEY).setText(Environment.API_KEY);
2736
_bot.button(BTN_APPLY).click();
2837
_bot.button(BTN_TEST_CONNECTION).click();
2938

30-
// Wait for a response
39+
// Wait for successful connection message
3140
waitForConnectionResponse();
3241

33-
// Validate: the connection should succeed
34-
assertTrue("Connection should succeed with a valid API key",
42+
// Validate success message
43+
assertTrue(ASSERT_CONNECTION_SUCCESS,
3544
_bot.text(3).getText().contains("Successfully authenticated"));
3645

37-
// Close the Preferences window
3846
_bot.button(BTN_APPLY_AND_CLOSE).click();
3947
}
4048

4149
@Test
4250
public void testInvalidApiKeyConnection() throws TimeoutException {
43-
// Open the Preferences window
51+
preventWidgetWasNullInCIEnvironment();
52+
53+
// Open Preferences
4454
_bot.menu(TAB_WINDOW).menu(ITEM_PREFERENCES).click();
4555
_bot.shell(ITEM_PREFERENCES).activate();
4656
_bot.tree().select(ITEM_CHECKMARX_AST);
4757

48-
// Enter an invalid API Key
49-
String invalidApiKey = "invalid-api-key";
50-
_bot.textWithLabel(PluginConstants.PREFERENCES_API_KEY).setText(invalidApiKey);
58+
// Set invalid API key
59+
_bot.textWithLabel(PluginConstants.PREFERENCES_API_KEY).setText("invalid-key");
5160
_bot.button(BTN_APPLY).click();
5261
_bot.button(BTN_TEST_CONNECTION).click();
5362

54-
// Wait for a response
55-
waitForConnectionResponse();
63+
// Wait for error response
64+
SWTBotPreferences.TIMEOUT = 5000; // Adjusted timeout
65+
_bot.sleep(5000); // Simulating waiting time for the response
5666

57-
// Validate: the connection should fail
58-
assertFalse("Connection should fail with an invalid API key",
67+
// Validate error message
68+
assertFalse(ASSERT_CONNECTION_FAILED,
5969
_bot.text(3).getText().contains("Successfully authenticated"));
6070

61-
// Close the Preferences window
71+
_bot.button(BTN_APPLY_AND_CLOSE).click();
72+
}
73+
74+
@Test
75+
public void testApiKeyFieldNotEmpty() {
76+
preventWidgetWasNullInCIEnvironment();
77+
78+
// Open Preferences
79+
_bot.menu(TAB_WINDOW).menu(ITEM_PREFERENCES).click();
80+
_bot.shell(ITEM_PREFERENCES).activate();
81+
_bot.tree().select(ITEM_CHECKMARX_AST);
82+
83+
// Set API Key
84+
String apiKey = "dummy-api-key";
85+
_bot.textWithLabel(PluginConstants.PREFERENCES_API_KEY).setText(apiKey);
86+
_bot.button(BTN_APPLY).click();
87+
88+
// Validate the API Key field is not empty
89+
String currentKey = _bot.textWithLabel(PluginConstants.PREFERENCES_API_KEY).getText();
90+
assertEquals(ASSERT_API_KEY_EMPTY, apiKey, currentKey);
91+
92+
_bot.button(BTN_APPLY_AND_CLOSE).click();
93+
}
94+
95+
@Test
96+
public void testEmptyApiKeyFieldAfterClear() {
97+
preventWidgetWasNullInCIEnvironment();
98+
99+
// Open Preferences
100+
_bot.menu(TAB_WINDOW).menu(ITEM_PREFERENCES).click();
101+
_bot.shell(ITEM_PREFERENCES).activate();
102+
_bot.tree().select(ITEM_CHECKMARX_AST);
103+
104+
// Clear API Key
105+
_bot.textWithLabel(PluginConstants.PREFERENCES_API_KEY).setText("");
106+
_bot.button(BTN_APPLY).click();
107+
108+
// Validate the API Key field is empty
109+
String currentKey = _bot.textWithLabel(PluginConstants.PREFERENCES_API_KEY).getText();
110+
assertEquals("API Key field should be empty after clearing", "", currentKey);
111+
62112
_bot.button(BTN_APPLY_AND_CLOSE).click();
63113
}
64114

65115
private void waitForConnectionResponse() throws TimeoutException {
66116
int retryIdx = 0;
67-
while (!_bot.text(3).getText().contains("Successfully authenticated") &&
68-
retryIdx++ < 10) {
69-
_bot.sleep(1000); // Wait for a short interval
117+
while (!_bot.text(3).getText().contains("Successfully authenticated")) {
118+
if (retryIdx++ == 10) {
119+
throw new TimeoutException("Connection validation timeout after 10000ms.");
120+
}
121+
_bot.sleep(1000);
70122
}
71123
}
72124
}

0 commit comments

Comments
 (0)