Ajay Yadav
Initial deployment of da-admin-service-dev
d6afd6c
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());
}
}