package com.example.demo.controller; import com.example.demo.service.DeveloperService; 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.test.web.servlet.MockMvc; import java.util.List; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @WebMvcTest(DeveloperController.class) class DeveloperControllerTest { @Autowired private MockMvc mockMvc; @MockBean private DeveloperService developerService; @Test void getAllRoles_returnsRoleList() throws Exception { when(developerService.getAllRoles()).thenReturn(List.of("developer", "viewer", "admin")); mockMvc.perform(get("/api/roles")) .andExpect(status().isOk()) .andExpect(jsonPath("$").isArray()) .andExpect(jsonPath("$.length()").value(3)) .andExpect(jsonPath("$[0]").value("developer")); } @Test void getRolePermission_returnsPermission() throws Exception { when(developerService.getRolePermission("developer")).thenReturn("read-write"); mockMvc.perform(get("/api/roles/developer")) .andExpect(status().isOk()) .andExpect(jsonPath("$.role").value("developer")) .andExpect(jsonPath("$.permission").value("read-write")); } @Test void checkPermission_allowed_returnsTrue() throws Exception { when(developerService.hasPermission("developer", "read")).thenReturn(true); mockMvc.perform(get("/api/roles/developer/check").param("action", "read")) .andExpect(status().isOk()) .andExpect(jsonPath("$.action").value("read")) .andExpect(jsonPath("$.allowed").value(true)); } @Test void checkPermission_denied_returnsFalse() throws Exception { when(developerService.hasPermission("developer", "delete")).thenReturn(false); mockMvc.perform(get("/api/roles/developer/check").param("action", "delete")) .andExpect(status().isOk()) .andExpect(jsonPath("$.action").value("delete")) .andExpect(jsonPath("$.allowed").value(false)); } }