Spaces:
Build error
Build error
da-admin-service-dev
/
src
/test
/java
/com
/dalab
/adminservice
/controller
/CloudConnectionControllerTest.java
| package com.dalab.adminservice.controller; | |
| import static org.hamcrest.Matchers.*; | |
| import static org.mockito.ArgumentMatchers.*; | |
| import static org.mockito.ArgumentMatchers.any; | |
| import static org.mockito.BDDMockito.*; | |
| import static org.mockito.Mockito.*; | |
| import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*; | |
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; | |
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | |
| import java.util.Collections; | |
| import java.util.Map; | |
| 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.test.autoconfigure.web.servlet.WebMvcTest; | |
| import org.springframework.boot.test.mock.mockito.MockBean; | |
| import org.springframework.context.annotation.Import; | |
| import org.springframework.http.MediaType; | |
| import org.springframework.security.test.context.support.WithMockUser; | |
| import org.springframework.test.web.servlet.MockMvc; | |
| import com.dalab.adminservice.config.TestSecurityConfiguration; | |
| import com.dalab.adminservice.dto.CloudConnectionDTO; | |
| import com.dalab.adminservice.dto.CloudConnectionTestResultDTO; | |
| import com.dalab.adminservice.model.enums.CloudProviderType; | |
| import com.dalab.adminservice.service.ICloudConnectionService; | |
| import com.fasterxml.jackson.databind.ObjectMapper; | |
| class CloudConnectionControllerTest { | |
| private MockMvc mockMvc; | |
| private ICloudConnectionService cloudConnectionService; | |
| private ObjectMapper objectMapper; | |
| private CloudConnectionDTO cloudConnectionDTO; | |
| private String connectionId; | |
| void setUp() { | |
| connectionId = "test-connection-id"; | |
| cloudConnectionDTO = CloudConnectionDTO.builder() | |
| .id(connectionId) | |
| .name("Test GCP Connection") | |
| .providerType(CloudProviderType.GCP) | |
| .connectionParameters(Map.of("projectId", "test-project")) | |
| .sensitiveCredentials("{\"type\": \"service_account\"}") // Example, this won't be returned | |
| .enabled(true) | |
| .build(); | |
| } | |
| void getAllCloudConnections_shouldReturnListOfConnections() throws Exception { | |
| given(cloudConnectionService.getAllCloudConnections()).willReturn(Collections.singletonList(cloudConnectionDTO)); | |
| mockMvc.perform(get("/api/v1/admin/cloud-connections")) | |
| .andExpect(status().isOk()) | |
| .andExpect(jsonPath("$", hasSize(1))) | |
| .andExpect(jsonPath("$[0].name").value("Test GCP Connection")); | |
| } | |
| void getCloudConnectionById_whenExists_shouldReturnConnection() throws Exception { | |
| given(cloudConnectionService.getCloudConnectionById(connectionId)).willReturn(Optional.of(cloudConnectionDTO)); | |
| mockMvc.perform(get("/api/v1/admin/cloud-connections/" + connectionId)) | |
| .andExpect(status().isOk()) | |
| .andExpect(jsonPath("$.name").value("Test GCP Connection")); | |
| } | |
| void getCloudConnectionById_whenNotExists_shouldReturnNotFound() throws Exception { | |
| given(cloudConnectionService.getCloudConnectionById("unknown-id")).willReturn(Optional.empty()); | |
| mockMvc.perform(get("/api/v1/admin/cloud-connections/unknown-id")) | |
| .andExpect(status().isNotFound()); | |
| } | |
| void createCloudConnection_shouldReturnCreatedConnection() throws Exception { | |
| given(cloudConnectionService.createCloudConnection(any(CloudConnectionDTO.class))).willReturn(cloudConnectionDTO); | |
| mockMvc.perform(post("/api/v1/admin/cloud-connections").with(csrf()) | |
| .contentType(MediaType.APPLICATION_JSON) | |
| .content(objectMapper.writeValueAsString(cloudConnectionDTO))) | |
| .andExpect(status().isCreated()) | |
| .andExpect(jsonPath("$.id").value(connectionId)); | |
| } | |
| void updateCloudConnection_shouldReturnUpdatedConnection() throws Exception { | |
| CloudConnectionDTO updatedDto = CloudConnectionDTO.builder() | |
| .id(connectionId) | |
| .name("Updated GCP Connection") | |
| .providerType(CloudProviderType.GCP) | |
| .connectionParameters(Map.of("projectId", "updated-project")) | |
| .enabled(true) | |
| .build(); | |
| given(cloudConnectionService.updateCloudConnection(eq(connectionId), any(CloudConnectionDTO.class))).willReturn(updatedDto); | |
| CloudConnectionDTO requestDto = CloudConnectionDTO.builder() | |
| .name("Updated GCP Connection") | |
| .providerType(CloudProviderType.GCP) | |
| .build(); | |
| mockMvc.perform(put("/api/v1/admin/cloud-connections/" + connectionId).with(csrf()) | |
| .contentType(MediaType.APPLICATION_JSON) | |
| .content(objectMapper.writeValueAsString(requestDto))) | |
| .andExpect(status().isOk()) | |
| .andExpect(jsonPath("$.name").value("Updated GCP Connection")); | |
| } | |
| void deleteCloudConnection_shouldReturnNoContent() throws Exception { | |
| doNothing().when(cloudConnectionService).deleteCloudConnection(connectionId); | |
| mockMvc.perform(delete("/api/v1/admin/cloud-connections/" + connectionId).with(csrf())) | |
| .andExpect(status().isNoContent()); | |
| } | |
| void testCloudConnection_shouldReturnTestResult() throws Exception { | |
| CloudConnectionTestResultDTO testResult = CloudConnectionTestResultDTO.builder() | |
| .success(true) | |
| .message("Connection successful") | |
| .build(); | |
| given(cloudConnectionService.testCloudConnection(connectionId)).willReturn(testResult); | |
| mockMvc.perform(post("/api/v1/admin/cloud-connections/" + connectionId + "/test").with(csrf())) | |
| .andExpect(status().isOk()) | |
| .andExpect(jsonPath("$.success").value(true)) | |
| .andExpect(jsonPath("$.message").value("Connection successful")); | |
| } | |
| // Non-admin | |
| void createCloudConnection_whenUnauthorized_shouldReturnForbidden() throws Exception { | |
| mockMvc.perform(post("/api/v1/admin/cloud-connections").with(csrf()) | |
| .contentType(MediaType.APPLICATION_JSON) | |
| .content(objectMapper.writeValueAsString(cloudConnectionDTO))) | |
| .andExpect(status().isForbidden()); | |
| } | |
| } |