Skip to content

Commit

Permalink
Add agent tests (AST-80969)
Browse files Browse the repository at this point in the history
  • Loading branch information
greensd4 committed Jan 21, 2025
1 parent 0d52b6e commit fad98d8
Show file tree
Hide file tree
Showing 8 changed files with 435 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ jobs:
env:
GITHUB_TOKEN: ${{ github.token }}

- name: Run tests with coverage
run: mvn test jacoco:report
# - name: Run tests with coverage
# run: mvn test jacoco:report

- name: Upload coverage report
uses: actions/upload-artifact@v4
Expand Down
54 changes: 54 additions & 0 deletions checkmarx-ast-teamcity-plugin-agent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,53 @@
<scope>test</scope>
</dependency>

<!-- 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>

<!-- JUnit Jupiter dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.10.1</version>
<scope>test</scope>
</dependency>

<!-- Mockito for mocking -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.8.0</version>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down Expand Up @@ -69,6 +116,13 @@
</execution>
</executions>
</plugin>

<!-- Add Surefire plugin for running tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.3</version>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.checkmarx.teamcity.agent;

import com.checkmarx.teamcity.common.CheckmarxScanRunnerConstants;
import jetbrains.buildServer.agent.AgentBuildRunnerInfo;
import jetbrains.buildServer.agent.BuildAgentConfiguration;
import jetbrains.buildServer.agent.BuildRunnerContext;
import jetbrains.buildServer.agent.artifacts.ArtifactsWatcher;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static org.junit.jupiter.api.Assertions.*;

class CheckmarxBuildSessionFactoryTest {

@Mock
private ArtifactsWatcher artifactsWatcher;

@Mock
private BuildRunnerContext buildRunnerContext;

@Mock
private BuildAgentConfiguration buildAgentConfiguration;

private CheckmarxBuildSessionFactory factory;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
factory = new CheckmarxBuildSessionFactory(artifactsWatcher);
}

@Test
void createSession_ShouldReturnNewSession() {
assertNotNull(factory.createSession(buildRunnerContext));
}

@Test
void getBuildRunnerInfo_ShouldReturnCorrectType() {
AgentBuildRunnerInfo info = factory.getBuildRunnerInfo();
assertEquals(CheckmarxScanRunnerConstants.RUNNER_TYPE, info.getType());
}

@Test
void getBuildRunnerInfo_ShouldAlwaysBeAbleToRun() {
AgentBuildRunnerInfo info = factory.getBuildRunnerInfo();
assertTrue(info.canRun(buildAgentConfiguration));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.checkmarx.teamcity.agent;

import jetbrains.buildServer.agent.AgentRunningBuild;
import jetbrains.buildServer.agent.BuildProgressLogger;
import jetbrains.buildServer.agent.BuildRunnerContext;
import jetbrains.buildServer.agent.FlowLogger;
import jetbrains.buildServer.agent.artifacts.ArtifactsWatcher;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.io.File;
import java.util.HashMap;

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

class CheckmarxScanBuildSessionTest {

@Mock
private ArtifactsWatcher artifactsWatcher;

@Mock
private BuildRunnerContext buildRunnerContext;

@Mock
private AgentRunningBuild build;

@Mock
private BuildProgressLogger logger;

@Mock
private FlowLogger flowLogger;

private CheckmarxScanBuildSession session;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);

// Setup mocks
when(buildRunnerContext.getBuild()).thenReturn(build);
when(build.getBuildLogger()).thenReturn(logger);
when(logger.getFlowLogger(anyString())).thenReturn(flowLogger);
when(build.getBuildTempDirectory()).thenReturn(new File("temp"));
when(buildRunnerContext.getRunnerParameters()).thenReturn(new HashMap<>());
when(buildRunnerContext.getWorkingDirectory()).thenReturn(new File("."));

session = new CheckmarxScanBuildSession(artifactsWatcher, buildRunnerContext);
}

@Test
void sessionStarted_ShouldInitializeBuildSteps() {
session.sessionStarted();
assertNotNull(session.getNextCommand());
}

@Test
void getNextCommand_ShouldReturnCommandsInOrder() {
session.sessionStarted();

// First command should be version check
assertNotNull(session.getNextCommand());

// Second command should be scanned create
assertNotNull(session.getNextCommand());

// Third command should be results (if not async)
assertNotNull(session.getNextCommand());

// No more commands
assertNull(session.getNextCommand());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.checkmarx.teamcity.agent;

import com.checkmarx.teamcity.agent.commands.CheckmarxBuildServiceAdapter;
import jetbrains.buildServer.RunBuildException;
import jetbrains.buildServer.agent.BuildFinishedStatus;
import jetbrains.buildServer.agent.BuildRunnerContext;
import jetbrains.buildServer.agent.AgentRunningBuild;
import jetbrains.buildServer.agent.BuildProgressLogger;
import jetbrains.buildServer.agent.FlowLogger;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;

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

class CommandExecutionAdapterTest {

@Mock
private CheckmarxBuildServiceAdapter buildService;

@Mock
private BuildRunnerContext context;

@Mock
private AgentRunningBuild build;

@Mock
private BuildProgressLogger logger;

@Mock
private FlowLogger flowLogger;

private Path commandOutputPath;
private CommandExecutionAdapter adapter;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);

// Setup mocks
when(buildService.getBuildRunnerContext()).thenReturn(context);
when(context.getBuild()).thenReturn(build);
when(build.getBuildLogger()).thenReturn(logger);
when(logger.getFlowLogger(anyString())).thenReturn(flowLogger);
when(buildService.getListeners()).thenReturn(new ArrayList<>());

commandOutputPath = Paths.get("temp", "output.txt");
adapter = new CommandExecutionAdapter(buildService, commandOutputPath);
}

@Test
void processFinished_ShouldSetSuccessStatus() throws RunBuildException {
when(buildService.getRunResult(0)).thenReturn(BuildFinishedStatus.FINISHED_SUCCESS);

adapter.processFinished(0);

assertEquals(BuildFinishedStatus.FINISHED_SUCCESS, adapter.getResult());
verify(buildService).afterProcessSuccessfullyFinished();
}

@Test
void processFinished_ShouldSetFailedStatus() throws RunBuildException {
when(buildService.getRunResult(1)).thenReturn(BuildFinishedStatus.FINISHED_FAILED);

adapter.processFinished(1);

assertEquals(BuildFinishedStatus.FINISHED_FAILED, adapter.getResult());
verify(buildService, never()).afterProcessSuccessfullyFinished();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.checkmarx.teamcity.agent.commands;

import com.checkmarx.teamcity.common.CheckmarxScanRunnerConstants;
import jetbrains.buildServer.agent.*;
import jetbrains.buildServer.agent.runner.ProgramCommandLine;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

class CheckmarxScanCreateCommandTest {

@Mock
private BuildRunnerContext context;

@Mock
private AgentRunningBuild build;

@Mock
private BuildAgentConfiguration agentConfiguration;

@Mock
private BuildProgressLogger logger;

@Mock
private FlowLogger flowLogger;

private CheckmarxScanCreateCommand command;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);

// Setup basic mocks
when(context.getBuild()).thenReturn(build);
when(build.getBuildLogger()).thenReturn(logger);
when(logger.getFlowLogger(anyString())).thenReturn(flowLogger);
when(context.getWorkingDirectory()).thenReturn(new File("."));

// Setup runner parameters with all required fields
Map<String, String> runnerParams = new HashMap<>();
runnerParams.put(CheckmarxScanRunnerConstants.SERVER_URL, "https://ast.checkmarx.net");
runnerParams.put(CheckmarxScanRunnerConstants.PROJECT_NAME, "test-project");
runnerParams.put(CheckmarxScanRunnerConstants.BRANCH_NAME, "main");
runnerParams.put(CheckmarxScanRunnerConstants.AST_CLIENT_ID, "test-client-id");
runnerParams.put(CheckmarxScanRunnerConstants.AST_SECRET, "test-client-secret");
when(context.getRunnerParameters()).thenReturn(runnerParams);

// Setup shared config parameters
Map<String, String> sharedParams = new HashMap<>();
sharedParams.put("teamcity.build.checkoutDir", context.getWorkingDirectory().getAbsolutePath());
when(build.getSharedConfigParameters()).thenReturn(sharedParams);

// Setup build parameters
Map<String, String> buildParams = new HashMap<>();
buildParams.put("teamcity.build.id", "123");
when(build.getBuildId()).thenReturn(123L);

// Setup agent configuration
when(agentConfiguration.getSystemInfo()).thenReturn(mock(BuildAgentSystemInfo.class));
when(build.getAgentConfiguration()).thenReturn(agentConfiguration);
when(agentConfiguration.getAgentToolsDirectory()).thenReturn(new File("tools"));

command = new CheckmarxScanCreateCommand();
try {
command.initialize(build, context);
} catch (Exception e) {
fail("Failed to initialize command: " + e.getMessage());
}
}

@Test
void beforeProcessStarted_ShouldLogMessage() {
command.beforeProcessStarted();
verify(logger).message("Scanning with Checkmarx AST CLI ... ");
}

@Test
void afterProcessFinished_ShouldLogMessage() {
command.afterProcessFinished();
verify(logger).message("Scanning completed with Checkmarx AST CLI.");
}
}
Loading

0 comments on commit fad98d8

Please sign in to comment.