Repository navigation
AIML-343: Fix get_scan_project returning empty data #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
src/main/java/com/contrast/labs/ai/mcp/contrast/data/sast/ScanProject.java
This file contains hidden or 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 @@ | ||
| /* | ||
| * Copyright 2025 Contrast Security | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.contrast.labs.ai.mcp.contrast.data.sast; | ||
|
|
||
| import com.contrastsecurity.sdk.scan.Project; | ||
| import java.time.Instant; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Serializable representation of a Contrast Scan (SAST) project. | ||
| * | ||
| * <p>This record wraps the SDK {@link Project} interface to provide Jackson-compatible | ||
| * serialization. The SDK interface uses method-style accessors (e.g., {@code id()}) which Jackson | ||
| * cannot serialize by default. | ||
| * | ||
| * @param id Unique project identifier (UUID) | ||
| * @param name Project name as displayed in Contrast UI | ||
| * @param organizationId Organization UUID that owns this project | ||
| * @param archived Whether the project has been archived | ||
| * @param language Programming language (JAVA, JAVASCRIPT, PYTHON, etc.) | ||
| * @param critical Count of critical severity vulnerabilities in last scan | ||
| * @param high Count of high severity vulnerabilities in last scan | ||
| * @param medium Count of medium severity vulnerabilities in last scan | ||
| * @param low Count of low severity vulnerabilities in last scan | ||
| * @param note Count of note severity vulnerabilities in last scan | ||
| * @param lastScanId ID of most recent completed scan (null if no scans) | ||
| * @param lastScanTime Timestamp of most recent completed scan (null if no scans) | ||
| * @param completedScans Total count of successfully completed scans | ||
| * @param includeNamespaceFilters Namespaces to include in scans | ||
| * @param excludeNamespaceFilters Namespaces to exclude from scans | ||
| */ | ||
| public record ScanProject( | ||
| String id, | ||
| String name, | ||
| String organizationId, | ||
| boolean archived, | ||
| String language, | ||
| int critical, | ||
| int high, | ||
| int medium, | ||
| int low, | ||
| int note, | ||
| String lastScanId, | ||
| Instant lastScanTime, | ||
| int completedScans, | ||
| List<String> includeNamespaceFilters, | ||
| List<String> excludeNamespaceFilters) { | ||
|
|
||
| /** | ||
| * Creates a ScanProject from an SDK Project interface. | ||
| * | ||
| * @param project SDK Project to convert | ||
| * @return new ScanProject with all fields mapped | ||
| * @throws NullPointerException if project is null | ||
| */ | ||
| public static ScanProject from(Project project) { | ||
| return new ScanProject( | ||
| project.id(), | ||
| project.name(), | ||
| project.organizationId(), | ||
| project.archived(), | ||
| project.language(), | ||
| project.critical(), | ||
| project.high(), | ||
| project.medium(), | ||
| project.low(), | ||
| project.note(), | ||
| project.lastScanId(), | ||
| project.lastScanTime(), | ||
| project.completedScans(), | ||
| project.includeNamespaceFilters() != null | ||
| ? List.copyOf(project.includeNamespaceFilters()) | ||
| : List.of(), | ||
| project.excludeNamespaceFilters() != null | ||
| ? List.copyOf(project.excludeNamespaceFilters()) | ||
| : List.of()); | ||
| } | ||
| } |
This file contains hidden or 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
141 changes: 141 additions & 0 deletions
141
src/test/java/com/contrast/labs/ai/mcp/contrast/data/sast/ScanProjectTest.java
This file contains hidden or 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,141 @@ | ||
| /* | ||
| * Copyright 2025 Contrast Security | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.contrast.labs.ai.mcp.contrast.data.sast; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import com.contrastsecurity.sdk.scan.Project; | ||
| import java.time.Instant; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** Unit tests for ScanProject record and its factory method. */ | ||
| class ScanProjectTest { | ||
|
|
||
| @Test | ||
| void from_should_map_all_fields_correctly() { | ||
| // Given | ||
| Project mockProject = mock(); | ||
| when(mockProject.id()).thenReturn("proj-123"); | ||
| when(mockProject.name()).thenReturn("test-project"); | ||
| when(mockProject.organizationId()).thenReturn("org-456"); | ||
| when(mockProject.archived()).thenReturn(false); | ||
| when(mockProject.language()).thenReturn("JAVA"); | ||
| when(mockProject.critical()).thenReturn(2); | ||
| when(mockProject.high()).thenReturn(5); | ||
| when(mockProject.medium()).thenReturn(10); | ||
| when(mockProject.low()).thenReturn(3); | ||
| when(mockProject.note()).thenReturn(1); | ||
| when(mockProject.lastScanId()).thenReturn("scan-789"); | ||
| when(mockProject.lastScanTime()).thenReturn(Instant.parse("2025-01-01T12:00:00Z")); | ||
| when(mockProject.completedScans()).thenReturn(15); | ||
| when(mockProject.includeNamespaceFilters()).thenReturn(List.of("com.example")); | ||
| when(mockProject.excludeNamespaceFilters()).thenReturn(List.of("com.example.test")); | ||
|
|
||
| // When | ||
| ScanProject result = ScanProject.from(mockProject); | ||
|
|
||
| // Then | ||
| assertThat(result.id()).isEqualTo("proj-123"); | ||
| assertThat(result.name()).isEqualTo("test-project"); | ||
| assertThat(result.organizationId()).isEqualTo("org-456"); | ||
| assertThat(result.archived()).isFalse(); | ||
| assertThat(result.language()).isEqualTo("JAVA"); | ||
| assertThat(result.critical()).isEqualTo(2); | ||
| assertThat(result.high()).isEqualTo(5); | ||
| assertThat(result.medium()).isEqualTo(10); | ||
| assertThat(result.low()).isEqualTo(3); | ||
| assertThat(result.note()).isEqualTo(1); | ||
| assertThat(result.lastScanId()).isEqualTo("scan-789"); | ||
| assertThat(result.lastScanTime()).isEqualTo(Instant.parse("2025-01-01T12:00:00Z")); | ||
| assertThat(result.completedScans()).isEqualTo(15); | ||
| assertThat(result.includeNamespaceFilters()).containsExactly("com.example"); | ||
| assertThat(result.excludeNamespaceFilters()).containsExactly("com.example.test"); | ||
| } | ||
|
|
||
| @Test | ||
| void from_should_handle_null_lastScanId_and_lastScanTime() { | ||
| // Given - project with no completed scans | ||
| Project mockProject = createMinimalMockProject(); | ||
| when(mockProject.lastScanId()).thenReturn(null); | ||
| when(mockProject.lastScanTime()).thenReturn(null); | ||
| when(mockProject.completedScans()).thenReturn(0); | ||
|
|
||
| // When | ||
| ScanProject result = ScanProject.from(mockProject); | ||
|
|
||
| // Then | ||
| assertThat(result.lastScanId()).isNull(); | ||
| assertThat(result.lastScanTime()).isNull(); | ||
| assertThat(result.completedScans()).isZero(); | ||
| } | ||
|
|
||
| @Test | ||
| void from_should_handle_null_namespace_filters() { | ||
| // Given - SDK may return null for empty filters | ||
| Project mockProject = createMinimalMockProject(); | ||
| when(mockProject.includeNamespaceFilters()).thenReturn(null); | ||
| when(mockProject.excludeNamespaceFilters()).thenReturn(null); | ||
|
|
||
| // When | ||
| ScanProject result = ScanProject.from(mockProject); | ||
|
|
||
| // Then - should be empty list, not null | ||
| assertThat(result.includeNamespaceFilters()).isEmpty(); | ||
| assertThat(result.excludeNamespaceFilters()).isEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| void from_should_create_defensive_copy_of_namespace_filters() { | ||
| // Given | ||
| var includeFilters = new ArrayList<>(List.of("com.example")); | ||
| var excludeFilters = new ArrayList<>(List.of("com.test")); | ||
|
ChrisEdwards marked this conversation as resolved.
Resolved
|
||
|
|
||
| Project mockProject = createMinimalMockProject(); | ||
| when(mockProject.includeNamespaceFilters()).thenReturn(includeFilters); | ||
| when(mockProject.excludeNamespaceFilters()).thenReturn(excludeFilters); | ||
|
|
||
| // When | ||
| ScanProject result = ScanProject.from(mockProject); | ||
|
|
||
| // Then - modifying original should not affect result | ||
| includeFilters.add("modified"); | ||
| excludeFilters.add("modified"); | ||
|
|
||
| assertThat(result.includeNamespaceFilters()).containsExactly("com.example"); | ||
| assertThat(result.excludeNamespaceFilters()).containsExactly("com.test"); | ||
| } | ||
|
|
||
| @Test | ||
| void from_should_throw_on_null_project() { | ||
| assertThatThrownBy(() -> ScanProject.from(null)).isInstanceOf(NullPointerException.class); | ||
| } | ||
|
|
||
| private Project createMinimalMockProject() { | ||
| Project mock = mock(); | ||
| when(mock.id()).thenReturn("id"); | ||
| when(mock.name()).thenReturn("name"); | ||
| when(mock.organizationId()).thenReturn("org"); | ||
| when(mock.language()).thenReturn("JAVA"); | ||
| when(mock.includeNamespaceFilters()).thenReturn(List.of()); | ||
| when(mock.excludeNamespaceFilters()).thenReturn(List.of()); | ||
| return mock; | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.