Skip to content

Commit

Permalink
Add common tests (AST-80969)
Browse files Browse the repository at this point in the history
  • Loading branch information
greensd4 committed Jan 19, 2025
1 parent 80613ab commit 3f322dd
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release-drafter.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Create draft release
name: Create draft release

on:
push:
Expand Down
23 changes: 22 additions & 1 deletion checkmarx-ast-teamcity-plugin-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,28 @@
<scope>test</scope>
</dependency>

</dependencies>
<!-- Test Dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>


</dependencies>

<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package com.checkmarx.teamcity.server;

import com.checkmarx.teamcity.common.CheckmarxParams;
import com.checkmarx.teamcity.common.CheckmarxScanRunnerConstants;
import jetbrains.buildServer.serverSide.InvalidProperty;
import jetbrains.buildServer.serverSide.PropertiesProcessor;
import jetbrains.buildServer.serverSide.RunTypeRegistry;
import jetbrains.buildServer.web.openapi.PluginDescriptor;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class CheckmarxScanRunTypeTest {

@Mock
private PluginDescriptor pluginDescriptor;

@Mock
private RunTypeRegistry runTypeRegistry;

private CheckmarxScanRunType scanRunType;

@BeforeEach
void setUp() {
scanRunType = new CheckmarxScanRunType(runTypeRegistry, pluginDescriptor);
verify(runTypeRegistry).registerRunType(scanRunType);
}

@Test
void testRunTypeBasicProperties() {
when(pluginDescriptor.getPluginResourcesPath("editCheckmarxScanRunnerParameters.jsp"))
.thenReturn("checkmarx/editScan.jsp");
when(pluginDescriptor.getPluginResourcesPath("viewCheckmarxScanRunnerParameters.jsp"))
.thenReturn("checkmarx/viewScan.jsp");

assertEquals(CheckmarxScanRunnerConstants.RUNNER_TYPE, scanRunType.getType());
assertEquals(CheckmarxScanRunnerConstants.RUNNER_DISPLAY_NAME, scanRunType.getDisplayName());
assertEquals(CheckmarxScanRunnerConstants.RUNNER_DESCRIPTION, scanRunType.getDescription());
assertEquals("checkmarx/editScan.jsp", scanRunType.getEditRunnerParamsJspFilePath());
assertEquals("checkmarx/viewScan.jsp", scanRunType.getViewRunnerParamsJspFilePath());
}

@Test
void testGetDefaultRunnerProperties() {
Map<String, String> defaults = scanRunType.getDefaultRunnerProperties();
assertNotNull(defaults);
assertTrue(defaults.isEmpty());
}

@Test
void testPropertiesProcessorWithNullProperties() {
PropertiesProcessor processor = scanRunType.getRunnerPropertiesProcessor();
Collection<InvalidProperty> errors = processor.process(null);
assertTrue(errors.isEmpty());
}

@Test
void testPropertiesProcessorWithValidCustomServerProperties() {
PropertiesProcessor processor = scanRunType.getRunnerPropertiesProcessor();

Map<String, String> properties = new HashMap<>();
properties.put(CheckmarxParams.USE_DEFAULT_SERVER, "false");
properties.put(CheckmarxParams.SERVER_URL, "https://test.checkmarx.com");
properties.put(CheckmarxParams.BRANCH_NAME, "main");

Collection<InvalidProperty> errors = processor.process(properties);
assertTrue(errors.isEmpty());
}

@Test
void testPropertiesProcessorWithMissingServerUrl() {
PropertiesProcessor processor = scanRunType.getRunnerPropertiesProcessor();

Map<String, String> properties = new HashMap<>();
properties.put(CheckmarxParams.USE_DEFAULT_SERVER, "false");
properties.put(CheckmarxParams.BRANCH_NAME, "main");

Collection<InvalidProperty> errors = processor.process(properties);
assertFalse(errors.isEmpty());
assertTrue(errors.stream()
.anyMatch(error -> error.getPropertyName().equals(CheckmarxParams.SERVER_URL)));
}

@Test
void testPropertiesProcessorWithMissingBranchName() {
PropertiesProcessor processor = scanRunType.getRunnerPropertiesProcessor();

Map<String, String> properties = new HashMap<>();
properties.put(CheckmarxParams.USE_DEFAULT_SERVER, "false");
properties.put(CheckmarxParams.SERVER_URL, "https://test.checkmarx.com");

Collection<InvalidProperty> errors = processor.process(properties);
assertFalse(errors.isEmpty());
assertTrue(errors.stream()
.anyMatch(error -> error.getPropertyName().equals(CheckmarxParams.BRANCH_NAME)));
}

@Test
void testPropertiesProcessorWithDefaultServer() {
PropertiesProcessor processor = scanRunType.getRunnerPropertiesProcessor();

Map<String, String> properties = new HashMap<>();
properties.put(CheckmarxParams.USE_DEFAULT_SERVER, CheckmarxScanRunnerConstants.TRUE);
properties.put(CheckmarxParams.BRANCH_NAME, "main");

Collection<InvalidProperty> errors = processor.process(properties);
assertTrue(errors.isEmpty());
}

@Test
void testPropertiesProcessorWithDefaultServerMissingBranch() {
PropertiesProcessor processor = scanRunType.getRunnerPropertiesProcessor();

Map<String, String> properties = new HashMap<>();
properties.put(CheckmarxParams.USE_DEFAULT_SERVER, CheckmarxScanRunnerConstants.TRUE);

Collection<InvalidProperty> errors = processor.process(properties);
assertFalse(errors.isEmpty());
assertTrue(errors.stream()
.anyMatch(error -> error.getPropertyName().equals(CheckmarxParams.BRANCH_NAME)));
}
}

0 comments on commit 3f322dd

Please sign in to comment.