File size: 4,797 Bytes
d6afd6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package com.dalab.adminservice.controller;

import static org.mockito.ArgumentMatchers.*;
import static org.mockito.BDDMockito.*;
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.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.ServiceConfigDTO;
import com.dalab.adminservice.service.IServiceConfigService;
import com.fasterxml.jackson.databind.ObjectMapper;

@WebMvcTest(ServiceConfigController.class)
@Import(TestSecurityConfiguration.class)
class ServiceConfigControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private IServiceConfigService serviceConfigService;

    @Autowired
    private ObjectMapper objectMapper;

    private ServiceConfigDTO serviceConfigDTO;

    @BeforeEach
    void setUp() {
        serviceConfigDTO = ServiceConfigDTO.builder()
                .serviceId("test-service")
                .displayName("Test Service")
                .endpoint("http://localhost:1234")
                .enabled(true)
                .build();
    }

    @Test
    @WithMockUser(roles = "ADMIN")
    void getAllServiceConfigs_shouldReturnListOfConfigs() throws Exception {
        given(serviceConfigService.getAllServiceConfigs()).willReturn(Collections.singletonList(serviceConfigDTO));

        mockMvc.perform(get("/api/v1/admin/config/services"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$[0].serviceId").value("test-service"));
    }

    @Test
    @WithMockUser(roles = "ADMIN")
    void getServiceConfigById_whenExists_shouldReturnConfig() throws Exception {
        given(serviceConfigService.getServiceConfigById("test-service")).willReturn(Optional.of(serviceConfigDTO));

        mockMvc.perform(get("/api/v1/admin/config/services/test-service"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.displayName").value("Test Service"));
    }

    @Test
    @WithMockUser(roles = "ADMIN")
    void getServiceConfigById_whenNotExists_shouldReturnNotFound() throws Exception {
        given(serviceConfigService.getServiceConfigById("unknown-service")).willReturn(Optional.empty());

        mockMvc.perform(get("/api/v1/admin/config/services/unknown-service"))
                .andExpect(status().isNotFound());
    }

    @Test
    @WithMockUser(roles = "ADMIN")
    void createServiceConfig_shouldReturnCreatedConfig() throws Exception {
        given(serviceConfigService.createServiceConfig(any(ServiceConfigDTO.class))).willReturn(serviceConfigDTO);

        mockMvc.perform(post("/api/v1/admin/config/services").with(csrf())
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(serviceConfigDTO)))
                .andExpect(status().isCreated())
                .andExpect(jsonPath("$.serviceId").value("test-service"));
    }

    @Test
    @WithMockUser(roles = "ADMIN")
    void updateServiceConfig_shouldReturnUpdatedConfig() throws Exception {
        ServiceConfigDTO updatedDTO = ServiceConfigDTO.builder().serviceId("test-service").displayName("Updated Name").build();
        given(serviceConfigService.updateServiceConfig(eq("test-service"), any(ServiceConfigDTO.class))).willReturn(updatedDTO);

        mockMvc.perform(put("/api/v1/admin/config/services/test-service").with(csrf())
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(updatedDTO)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.displayName").value("Updated Name"));
    }
    
    @Test
    @WithMockUser(roles = "USER") // Non-admin user
    void createServiceConfig_whenUnauthorized_shouldReturnForbidden() throws Exception {
         mockMvc.perform(post("/api/v1/admin/config/services").with(csrf())
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(serviceConfigDTO)))
                .andExpect(status().isForbidden());
    }
}