File size: 6,201 Bytes
442299c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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;

@WebMvcTest(
    controllers = DiscoveryConfigController.class,
    excludeAutoConfiguration = {
        OAuth2ClientAutoConfiguration.class,
        OAuth2ResourceServerAutoConfiguration.class 
    }
)
@AutoConfigureMockMvc
@ActiveProfiles("test")
class DiscoveryConfigControllerTest {

    @SpringBootConfiguration
    @Import({TestWebSecurityConfiguration.class, DiscoveryConfigController.class}) // Added DiscoveryConfigController.class
    static class TestApp {}

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private IDiscoveryConfigService configService;

    @Autowired
    private ObjectMapper objectMapper;

    private GlobalDiscoveryConfigDTO globalConfigDTO;
    private ConnectionDiscoveryConfigDTO connectionConfigDTO;
    private final String testConnectionId = "conn-123";

    @BeforeEach
    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
    @Test
    @WithMockUser(authorities = "ROLE_ADMIN") // 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));
    }

    @Test
    @WithMockUser(authorities = "ROLE_USER") // Restored
    void getGlobalConfig_AsUser_ShouldBeForbidden() throws Exception {
        mockMvc.perform(get("/api/v1/discovery/config/global"))
                .andExpect(status().isForbidden()); // Restored original expectation
    }

    @Test
    @WithMockUser(authorities = "ROLE_ADMIN") // 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
    @Test
    @WithMockUser(authorities = "ROLE_ADMIN") // 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));
    }

    @Test
    @WithMockUser(authorities = "ROLE_USER") // Restored
    void getConnectionConfig_AsUser_ShouldBeForbidden() throws Exception {
        mockMvc.perform(get("/api/v1/discovery/config/connections/{connectionId}", testConnectionId))
                 .andExpect(status().isForbidden()); // Restored original expectation
    }

    @Test
    @WithMockUser(authorities = "ROLE_ADMIN") // 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());
    }
}