-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
435 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...in-agent/src/test/java/com/checkmarx/teamcity/agent/CheckmarxBuildSessionFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...lugin-agent/src/test/java/com/checkmarx/teamcity/agent/CheckmarxScanBuildSessionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
...-plugin-agent/src/test/java/com/checkmarx/teamcity/agent/CommandExecutionAdapterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
...t/src/test/java/com/checkmarx/teamcity/agent/commands/CheckmarxScanCreateCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} |
Oops, something went wrong.