Spaces:
Build error
Build error
| package com.dalab.discovery.client.rest; | |
| import static org.mockito.ArgumentMatchers.*; | |
| import static org.mockito.Mockito.*; | |
| import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*; // Restored | |
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; | |
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | |
| import java.util.Optional; | |
| import org.junit.jupiter.api.BeforeEach; | |
| import org.junit.jupiter.api.Test; | |
| import org.springframework.beans.factory.annotation.Autowired; | |
| import org.springframework.boot.SpringBootConfiguration; | |
| import org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientAutoConfiguration; | |
| import org.springframework.boot.autoconfigure.security.oauth2.resource.servlet.OAuth2ResourceServerAutoConfiguration; | |
| import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | |
| import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | |
| import org.springframework.boot.test.mock.mockito.MockBean; | |
| import org.springframework.context.annotation.Import; // Restored for TestApp | |
| import org.springframework.http.MediaType; | |
| import org.springframework.security.test.context.support.WithMockUser; // Restored | |
| import org.springframework.test.context.ActiveProfiles; | |
| import org.springframework.test.web.servlet.MockMvc; | |
| import com.dalab.discovery.config.dto.ConnectionDiscoveryConfigDTO; | |
| import com.dalab.discovery.config.dto.GlobalDiscoveryConfigDTO; | |
| import com.dalab.discovery.config.service.IDiscoveryConfigService; | |
| import com.fasterxml.jackson.databind.ObjectMapper; | |
| class DiscoveryConfigControllerTest { | |
| // Added DiscoveryConfigController.class | |
| static class TestApp {} | |
| private MockMvc mockMvc; | |
| private IDiscoveryConfigService configService; | |
| private ObjectMapper objectMapper; | |
| private GlobalDiscoveryConfigDTO globalConfigDTO; | |
| private ConnectionDiscoveryConfigDTO connectionConfigDTO; | |
| private final String testConnectionId = "conn-123"; | |
| void setUp() { | |
| globalConfigDTO = new GlobalDiscoveryConfigDTO(); | |
| globalConfigDTO.setDefaultScanIntervalMinutes(60); | |
| globalConfigDTO.setEnableAutoRemediation(false); | |
| // globalConfigDTO.setDefaultResourceTypesToExclude(List.of("AWS::IAM::Role")); // Example if needed | |
| // globalConfigDTO.setGlobalCrawlerProperties(Map.of("key", "value")); // Example if needed | |
| connectionConfigDTO = new ConnectionDiscoveryConfigDTO(); | |
| connectionConfigDTO.setCloudConnectionId(testConnectionId); | |
| connectionConfigDTO.setIsEnabled(true); | |
| connectionConfigDTO.setScanIntervalHours(2); | |
| // connectionConfigDTO.setResourceTypesToInclude(List.of("AWS::S3::Bucket")); // Example if needed | |
| } | |
| // Global Config Tests | |
| // Restored | |
| void getGlobalConfig_AsAdmin_ShouldReturnConfig() throws Exception { | |
| when(configService.getGlobalDiscoveryConfig()).thenReturn(globalConfigDTO); | |
| mockMvc.perform(get("/api/v1/discovery/config/global")) | |
| .andExpect(status().isOk()) | |
| .andExpect(jsonPath("$.defaultScanIntervalMinutes").value(60)) | |
| .andExpect(jsonPath("$.enableAutoRemediation").value(false)); | |
| } | |
| // Restored | |
| void getGlobalConfig_AsUser_ShouldBeForbidden() throws Exception { | |
| mockMvc.perform(get("/api/v1/discovery/config/global")) | |
| .andExpect(status().isForbidden()); // Restored original expectation | |
| } | |
| // Restored | |
| void updateGlobalConfig_AsAdmin_ShouldSucceed() throws Exception { | |
| doNothing().when(configService).saveGlobalDiscoveryConfig(any(GlobalDiscoveryConfigDTO.class)); | |
| mockMvc.perform(put("/api/v1/discovery/config/global") | |
| .with(csrf()) // Restored csrf | |
| .contentType(MediaType.APPLICATION_JSON) | |
| .content(objectMapper.writeValueAsString(globalConfigDTO))) | |
| .andExpect(status().isOk()); | |
| } | |
| // Connection Config Tests | |
| // Restored | |
| void getConnectionConfig_AsAdmin_ShouldReturnConfig() throws Exception { | |
| when(configService.getConnectionDiscoveryConfig(testConnectionId)).thenReturn(Optional.of(connectionConfigDTO)); | |
| mockMvc.perform(get("/api/v1/discovery/config/connections/{connectionId}", testConnectionId)) | |
| .andExpect(status().isOk()) | |
| .andExpect(jsonPath("$.isEnabled").value(true)) | |
| .andExpect(jsonPath("$.scanIntervalHours").value(2)); | |
| } | |
| // Restored | |
| void getConnectionConfig_AsUser_ShouldBeForbidden() throws Exception { | |
| mockMvc.perform(get("/api/v1/discovery/config/connections/{connectionId}", testConnectionId)) | |
| .andExpect(status().isForbidden()); // Restored original expectation | |
| } | |
| // Restored | |
| void updateConnectionConfig_AsAdmin_ShouldSucceed() throws Exception { | |
| when(configService.saveConnectionDiscoveryConfig(eq(testConnectionId), any(ConnectionDiscoveryConfigDTO.class))) | |
| .thenReturn(connectionConfigDTO); | |
| mockMvc.perform(put("/api/v1/discovery/config/connections/{connectionId}", testConnectionId) | |
| .with(csrf()) // Restored csrf | |
| .contentType(MediaType.APPLICATION_JSON) | |
| .content(objectMapper.writeValueAsString(connectionConfigDTO))) | |
| .andExpect(status().isOk()); | |
| } | |
| } |