|
1 | 1 | package checkmarx.ast.eclipse.plugin.tests.ui; |
2 | 2 |
|
3 | | -import static org.junit.Assert.assertTrue; |
4 | 3 | import static org.junit.Assert.assertFalse; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | +import static org.junit.Assert.assertEquals; |
5 | 6 |
|
6 | 7 | import java.util.concurrent.TimeoutException; |
7 | 8 |
|
8 | 9 | import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner; |
| 10 | +import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences; |
9 | 11 | import org.junit.Test; |
10 | 12 | import org.junit.runner.RunWith; |
11 | 13 |
|
12 | 14 | import com.checkmarx.eclipse.utils.PluginConstants; |
13 | 15 |
|
| 16 | +import checkmarx.ast.eclipse.plugin.tests.common.Environment; |
| 17 | + |
14 | 18 | @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"; |
16 | 24 |
|
17 | 25 | @Test |
18 | 26 | public void testValidApiKeyConnection() throws TimeoutException { |
19 | | - // Open the Preferences window |
| 27 | + preventWidgetWasNullInCIEnvironment(); |
| 28 | + |
| 29 | + // Open Preferences |
20 | 30 | _bot.menu(TAB_WINDOW).menu(ITEM_PREFERENCES).click(); |
21 | 31 | _bot.shell(ITEM_PREFERENCES).activate(); |
22 | 32 | _bot.tree().select(ITEM_CHECKMARX_AST); |
23 | 33 |
|
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); |
27 | 36 | _bot.button(BTN_APPLY).click(); |
28 | 37 | _bot.button(BTN_TEST_CONNECTION).click(); |
29 | 38 |
|
30 | | - // Wait for a response |
| 39 | + // Wait for successful connection message |
31 | 40 | waitForConnectionResponse(); |
32 | 41 |
|
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, |
35 | 44 | _bot.text(3).getText().contains("Successfully authenticated")); |
36 | 45 |
|
37 | | - // Close the Preferences window |
38 | 46 | _bot.button(BTN_APPLY_AND_CLOSE).click(); |
39 | 47 | } |
40 | 48 |
|
41 | 49 | @Test |
42 | 50 | public void testInvalidApiKeyConnection() throws TimeoutException { |
43 | | - // Open the Preferences window |
| 51 | + preventWidgetWasNullInCIEnvironment(); |
| 52 | + |
| 53 | + // Open Preferences |
44 | 54 | _bot.menu(TAB_WINDOW).menu(ITEM_PREFERENCES).click(); |
45 | 55 | _bot.shell(ITEM_PREFERENCES).activate(); |
46 | 56 | _bot.tree().select(ITEM_CHECKMARX_AST); |
47 | 57 |
|
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"); |
51 | 60 | _bot.button(BTN_APPLY).click(); |
52 | 61 | _bot.button(BTN_TEST_CONNECTION).click(); |
53 | 62 |
|
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 |
56 | 66 |
|
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, |
59 | 69 | _bot.text(3).getText().contains("Successfully authenticated")); |
60 | 70 |
|
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 | + |
62 | 112 | _bot.button(BTN_APPLY_AND_CLOSE).click(); |
63 | 113 | } |
64 | 114 |
|
65 | 115 | private void waitForConnectionResponse() throws TimeoutException { |
66 | 116 | 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); |
70 | 122 | } |
71 | 123 | } |
72 | 124 | } |
0 commit comments