|
| 1 | +package org.frankframework.insights.vulnerability; |
| 2 | + |
| 3 | +import static org.mockito.Mockito.*; |
| 4 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 5 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; |
| 6 | + |
| 7 | +import java.util.Collections; |
| 8 | +import java.util.Set; |
| 9 | +import org.frankframework.insights.common.enums.VulnerabilitySeverity; |
| 10 | +import org.junit.jupiter.api.BeforeEach; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.springframework.beans.factory.annotation.Autowired; |
| 13 | +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
| 14 | +import org.springframework.boot.test.context.TestConfiguration; |
| 15 | +import org.springframework.context.annotation.Bean; |
| 16 | +import org.springframework.http.MediaType; |
| 17 | +import org.springframework.test.web.servlet.MockMvc; |
| 18 | + |
| 19 | +@WebMvcTest(VulnerabilityController.class) |
| 20 | +public class VulnerabilityControllerTest { |
| 21 | + |
| 22 | + @Autowired |
| 23 | + private MockMvc mockMvc; |
| 24 | + |
| 25 | + @Autowired |
| 26 | + private VulnerabilityService vulnerabilityService; |
| 27 | + |
| 28 | + @TestConfiguration |
| 29 | + public static class TestConfig { |
| 30 | + @Bean |
| 31 | + public VulnerabilityService vulnerabilityService() { |
| 32 | + return mock(VulnerabilityService.class); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + @BeforeEach |
| 37 | + public void resetMocks() { |
| 38 | + reset(vulnerabilityService); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void getVulnerabilitiesByReleaseId_returnsOkWithVulnerabilities() throws Exception { |
| 43 | + String releaseId = "release-123"; |
| 44 | + VulnerabilityResponse vuln1 = new VulnerabilityResponse( |
| 45 | + "CVE-2023-1234", |
| 46 | + VulnerabilitySeverity.HIGH, |
| 47 | + 7.5, |
| 48 | + "SQL Injection vulnerability", |
| 49 | + Set.of("CWE-89") |
| 50 | + ); |
| 51 | + VulnerabilityResponse vuln2 = new VulnerabilityResponse( |
| 52 | + "CVE-2023-5678", |
| 53 | + VulnerabilitySeverity.CRITICAL, |
| 54 | + 9.8, |
| 55 | + "Remote Code Execution", |
| 56 | + Set.of("CWE-78", "CWE-502") |
| 57 | + ); |
| 58 | + Set<VulnerabilityResponse> vulnerabilities = Set.of(vuln1, vuln2); |
| 59 | + |
| 60 | + when(vulnerabilityService.getVulnerabilitiesByReleaseId(releaseId)).thenReturn(vulnerabilities); |
| 61 | + |
| 62 | + mockMvc.perform(get("/api/vulnerabilities/release/{releaseId}", releaseId)) |
| 63 | + .andExpect(status().isOk()) |
| 64 | + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) |
| 65 | + .andExpect(jsonPath("$").isArray()) |
| 66 | + .andExpect(jsonPath("$.length()").value(2)); |
| 67 | + |
| 68 | + verify(vulnerabilityService, times(1)).getVulnerabilitiesByReleaseId(releaseId); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void getVulnerabilitiesByReleaseId_returnsEmptySet() throws Exception { |
| 73 | + String releaseId = "release-no-vulns"; |
| 74 | + when(vulnerabilityService.getVulnerabilitiesByReleaseId(releaseId)).thenReturn(Collections.emptySet()); |
| 75 | + |
| 76 | + mockMvc.perform(get("/api/vulnerabilities/release/{releaseId}", releaseId)) |
| 77 | + .andExpect(status().isOk()) |
| 78 | + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) |
| 79 | + .andExpect(jsonPath("$").isArray()) |
| 80 | + .andExpect(jsonPath("$.length()").value(0)); |
| 81 | + |
| 82 | + verify(vulnerabilityService, times(1)).getVulnerabilitiesByReleaseId(releaseId); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void getVulnerabilitiesByReleaseId_serviceReturnsNull_treatedAsEmptySet() throws Exception { |
| 87 | + String releaseId = "release-null"; |
| 88 | + when(vulnerabilityService.getVulnerabilitiesByReleaseId(releaseId)).thenReturn(null); |
| 89 | + |
| 90 | + mockMvc.perform(get("/api/vulnerabilities/release/{releaseId}", releaseId)) |
| 91 | + .andExpect(status().isOk()) |
| 92 | + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) |
| 93 | + .andExpect(jsonPath("$").isArray()) |
| 94 | + .andExpect(jsonPath("$.length()").value(0)); |
| 95 | + |
| 96 | + verify(vulnerabilityService, times(1)).getVulnerabilitiesByReleaseId(releaseId); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void getVulnerabilitiesByReleaseId_withSingleVulnerability() throws Exception { |
| 101 | + String releaseId = "release-single"; |
| 102 | + VulnerabilityResponse vuln = new VulnerabilityResponse( |
| 103 | + "CVE-2024-0001", |
| 104 | + VulnerabilitySeverity.MEDIUM, |
| 105 | + 5.3, |
| 106 | + "Information Disclosure", |
| 107 | + Set.of("CWE-200") |
| 108 | + ); |
| 109 | + Set<VulnerabilityResponse> vulnerabilities = Set.of(vuln); |
| 110 | + |
| 111 | + when(vulnerabilityService.getVulnerabilitiesByReleaseId(releaseId)).thenReturn(vulnerabilities); |
| 112 | + |
| 113 | + mockMvc.perform(get("/api/vulnerabilities/release/{releaseId}", releaseId)) |
| 114 | + .andExpect(status().isOk()) |
| 115 | + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) |
| 116 | + .andExpect(jsonPath("$").isArray()) |
| 117 | + .andExpect(jsonPath("$.length()").value(1)) |
| 118 | + .andExpect(jsonPath("$[0].cveId").value("CVE-2024-0001")) |
| 119 | + .andExpect(jsonPath("$[0].severity").value("MEDIUM")) |
| 120 | + .andExpect(jsonPath("$[0].cvssScore").value(5.3)) |
| 121 | + .andExpect(jsonPath("$[0].description").value("Information Disclosure")); |
| 122 | + |
| 123 | + verify(vulnerabilityService, times(1)).getVulnerabilitiesByReleaseId(releaseId); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void getVulnerabilitiesByReleaseId_withoutReleaseIdParam_returnsNotFound() throws Exception { |
| 128 | + mockMvc.perform(get("/api/vulnerabilities/release/")) |
| 129 | + .andExpect(status().isNotFound()); |
| 130 | + |
| 131 | + verify(vulnerabilityService, never()).getVulnerabilitiesByReleaseId(any()); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void getVulnerabilitiesByReleaseId_withVulnerabilitiesOfDifferentSeverities() throws Exception { |
| 136 | + String releaseId = "release-mixed-severity"; |
| 137 | + VulnerabilityResponse low = new VulnerabilityResponse( |
| 138 | + "CVE-2024-0010", |
| 139 | + VulnerabilitySeverity.LOW, |
| 140 | + 3.1, |
| 141 | + "Low severity issue", |
| 142 | + Set.of("CWE-1") |
| 143 | + ); |
| 144 | + VulnerabilityResponse medium = new VulnerabilityResponse( |
| 145 | + "CVE-2024-0020", |
| 146 | + VulnerabilitySeverity.MEDIUM, |
| 147 | + 5.5, |
| 148 | + "Medium severity issue", |
| 149 | + Set.of("CWE-2") |
| 150 | + ); |
| 151 | + VulnerabilityResponse high = new VulnerabilityResponse( |
| 152 | + "CVE-2024-0030", |
| 153 | + VulnerabilitySeverity.HIGH, |
| 154 | + 7.8, |
| 155 | + "High severity issue", |
| 156 | + Set.of("CWE-3") |
| 157 | + ); |
| 158 | + VulnerabilityResponse critical = new VulnerabilityResponse( |
| 159 | + "CVE-2024-0040", |
| 160 | + VulnerabilitySeverity.CRITICAL, |
| 161 | + 9.9, |
| 162 | + "Critical severity issue", |
| 163 | + Set.of("CWE-4") |
| 164 | + ); |
| 165 | + Set<VulnerabilityResponse> vulnerabilities = Set.of(low, medium, high, critical); |
| 166 | + |
| 167 | + when(vulnerabilityService.getVulnerabilitiesByReleaseId(releaseId)).thenReturn(vulnerabilities); |
| 168 | + |
| 169 | + mockMvc.perform(get("/api/vulnerabilities/release/{releaseId}", releaseId)) |
| 170 | + .andExpect(status().isOk()) |
| 171 | + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) |
| 172 | + .andExpect(jsonPath("$").isArray()) |
| 173 | + .andExpect(jsonPath("$.length()").value(4)); |
| 174 | + |
| 175 | + verify(vulnerabilityService, times(1)).getVulnerabilitiesByReleaseId(releaseId); |
| 176 | + } |
| 177 | + |
| 178 | + @Test |
| 179 | + public void getVulnerabilitiesByReleaseId_withVulnerabilityWithMultipleCwes() throws Exception { |
| 180 | + String releaseId = "release-multi-cwe"; |
| 181 | + VulnerabilityResponse vuln = new VulnerabilityResponse( |
| 182 | + "CVE-2024-9999", |
| 183 | + VulnerabilitySeverity.HIGH, |
| 184 | + 8.2, |
| 185 | + "Multiple weakness types", |
| 186 | + Set.of("CWE-79", "CWE-89", "CWE-352", "CWE-434") |
| 187 | + ); |
| 188 | + Set<VulnerabilityResponse> vulnerabilities = Set.of(vuln); |
| 189 | + |
| 190 | + when(vulnerabilityService.getVulnerabilitiesByReleaseId(releaseId)).thenReturn(vulnerabilities); |
| 191 | + |
| 192 | + mockMvc.perform(get("/api/vulnerabilities/release/{releaseId}", releaseId)) |
| 193 | + .andExpect(status().isOk()) |
| 194 | + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) |
| 195 | + .andExpect(jsonPath("$").isArray()) |
| 196 | + .andExpect(jsonPath("$.length()").value(1)) |
| 197 | + .andExpect(jsonPath("$[0].cwes").isArray()) |
| 198 | + .andExpect(jsonPath("$[0].cwes.length()").value(4)); |
| 199 | + |
| 200 | + verify(vulnerabilityService, times(1)).getVulnerabilitiesByReleaseId(releaseId); |
| 201 | + } |
| 202 | + |
| 203 | + @Test |
| 204 | + public void getVulnerabilitiesByReleaseId_withVulnerabilityWithNoCwes() throws Exception { |
| 205 | + String releaseId = "release-no-cwe"; |
| 206 | + VulnerabilityResponse vuln = new VulnerabilityResponse( |
| 207 | + "CVE-2024-1111", |
| 208 | + VulnerabilitySeverity.LOW, |
| 209 | + 2.1, |
| 210 | + "Vulnerability without CWE classification", |
| 211 | + Collections.emptySet() |
| 212 | + ); |
| 213 | + Set<VulnerabilityResponse> vulnerabilities = Set.of(vuln); |
| 214 | + |
| 215 | + when(vulnerabilityService.getVulnerabilitiesByReleaseId(releaseId)).thenReturn(vulnerabilities); |
| 216 | + |
| 217 | + mockMvc.perform(get("/api/vulnerabilities/release/{releaseId}", releaseId)) |
| 218 | + .andExpect(status().isOk()) |
| 219 | + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) |
| 220 | + .andExpect(jsonPath("$").isArray()) |
| 221 | + .andExpect(jsonPath("$.length()").value(1)) |
| 222 | + .andExpect(jsonPath("$[0].cwes").isArray()) |
| 223 | + .andExpect(jsonPath("$[0].cwes.length()").value(0)); |
| 224 | + |
| 225 | + verify(vulnerabilityService, times(1)).getVulnerabilitiesByReleaseId(releaseId); |
| 226 | + } |
| 227 | +} |
0 commit comments