File size: 2,249 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
package com.dalab.adminservice.controller;

import static org.mockito.BDDMockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import java.util.Collections;

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.AggregatedJobStatusDTO;
import com.dalab.adminservice.service.IJobStatusService;
import com.fasterxml.jackson.databind.ObjectMapper;

@WebMvcTest(JobStatusController.class)
@Import(TestSecurityConfiguration.class)
class JobStatusControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private IJobStatusService jobStatusService;

    @Autowired
    private ObjectMapper objectMapper;

    @Test
    @WithMockUser(roles = {"ADMIN", "VIEWER"})
    void getAggregatedJobStatuses_shouldReturnAggregatedStatuses() throws Exception {
        AggregatedJobStatusDTO aggregatedStatus = AggregatedJobStatusDTO.builder()
                .jobs(Collections.emptyList())
                .totalJobs(0)
                .build();

        given(jobStatusService.getAggregatedJobStatuses()).willReturn(aggregatedStatus);

        mockMvc.perform(get("/api/v1/admin/job-statuses")
                .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.totalJobs").value(0));
    }

    @Test
    @WithMockUser(roles = "USER") // A user without ADMIN or VIEWER role
    void getAggregatedJobStatuses_whenUnauthorized_shouldReturnForbidden() throws Exception {
        mockMvc.perform(get("/api/v1/admin/job-statuses")
                .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isForbidden());
    }
}