Skip to content

Commit

Permalink
fix formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
kcinay055679 committed Jan 23, 2025
1 parent b7bd5e8 commit 69e7b18
Showing 1 changed file with 37 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package ch.puzzle.okr.controller;

import static ch.puzzle.okr.test.TestHelper.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;

import ch.puzzle.okr.dto.UnitDto;
import ch.puzzle.okr.mapper.UnitMapper;
import ch.puzzle.okr.models.Unit;
import ch.puzzle.okr.multitenancy.TenantContext;
import ch.puzzle.okr.service.authorization.AuthorizationService;
import ch.puzzle.okr.service.authorization.UnitAuthorizationService;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
import org.hamcrest.Matchers;
import org.hamcrest.core.Is;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -24,24 +31,20 @@
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.web.server.ResponseStatusException;

import java.util.List;

import static ch.puzzle.okr.test.TestHelper.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;

@WithMockUser(value = "spring")
@ExtendWith(MockitoExtension.class)
@WebMvcTest(UnitController.class)
class UnitControllerITIT {
private final String URL_BASE = "/api/v2/units";
private final ObjectMapper objectMapper = new ObjectMapper();
@Autowired private MockMvc mvc;
@MockitoBean private UnitAuthorizationService unitAuthorizationService;
@MockitoBean private AuthorizationService authorizationService;
@MockitoBean private UnitMapper unitMapper;
@Autowired
private MockMvc mvc;
@MockitoBean
private UnitAuthorizationService unitAuthorizationService;
@MockitoBean
private AuthorizationService authorizationService;
@MockitoBean
private UnitMapper unitMapper;

@BeforeEach
void setUp() {
Expand Down Expand Up @@ -87,9 +90,9 @@ void shouldReturnNewUnitWithCurrentUserAsOwner() throws Exception {

mvc
.perform(post(URL_BASE)
.content(unitJson)
.with(SecurityMockMvcRequestPostProcessors.jwt().jwt(glJwtToken()))
.contentType(MediaType.APPLICATION_JSON))
.content(unitJson)
.with(SecurityMockMvcRequestPostProcessors.jwt().jwt(glJwtToken()))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(jsonPath("$.owner.email", Is.is("[email protected]")))
.andExpect(jsonPath("$.unitName", Is.is("FTE")));
Expand All @@ -99,27 +102,28 @@ void shouldReturnNewUnitWithCurrentUserAsOwner() throws Exception {
void shouldReturn401ForInvalidUserWhenCreatingUnit() throws Exception {
UnitDto unitDTO = new UnitDto(null, "TestUnit", null);
String unitJson = objectMapper.writeValueAsString(unitDTO);
when(unitAuthorizationService.createUnit(any(Unit.class))).thenThrow(new ResponseStatusException(HttpStatus.UNAUTHORIZED));
when(unitAuthorizationService.createUnit(any(Unit.class)))
.thenThrow(new ResponseStatusException(HttpStatus.UNAUTHORIZED));
mvc
.perform(post(URL_BASE)
.with(SecurityMockMvcRequestPostProcessors.csrf())
.content(unitJson)
.contentType(MediaType.APPLICATION_JSON))
.with(SecurityMockMvcRequestPostProcessors.csrf())
.content(unitJson)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isUnauthorized());
}

@Test
void shouldReturn200ForUserWhenUpdatingUnit() throws Exception {
UnitDto unitDTO = new UnitDto(null, "TestUnit", null);
String unitJson = objectMapper.writeValueAsString(unitDTO);
when(unitAuthorizationService.updateUnit(any(),any(Unit.class))).thenReturn(FTE_UNIT);
when(unitAuthorizationService.updateUnit(any(), any(Unit.class))).thenReturn(FTE_UNIT);

mvc
.perform(put(URL_BASE + "/100")
.with(SecurityMockMvcRequestPostProcessors.csrf())
.with(SecurityMockMvcRequestPostProcessors.jwt().jwt(glJwtToken()))
.content(unitJson)
.contentType(MediaType.APPLICATION_JSON))
.with(SecurityMockMvcRequestPostProcessors.csrf())
.with(SecurityMockMvcRequestPostProcessors.jwt().jwt(glJwtToken()))
.content(unitJson)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(jsonPath("$.unitName", Is.is("FTE")));
}
Expand All @@ -128,13 +132,14 @@ void shouldReturn200ForUserWhenUpdatingUnit() throws Exception {
void shouldReturn403ForWrongUserWhenUpdatingUnit() throws Exception {
UnitDto unitDTO = new UnitDto(null, "TestUnit", null);
String unitJson = objectMapper.writeValueAsString(unitDTO);
when(unitAuthorizationService.updateUnit(any(),any(Unit.class))).thenThrow(new ResponseStatusException(HttpStatus.FORBIDDEN));
when(unitAuthorizationService.updateUnit(any(), any(Unit.class)))
.thenThrow(new ResponseStatusException(HttpStatus.FORBIDDEN));
mvc
.perform(put(URL_BASE + "/100")
.with(SecurityMockMvcRequestPostProcessors.csrf())
.with(SecurityMockMvcRequestPostProcessors.jwt().jwt(bbtJwtToken()))
.content(unitJson)
.contentType(MediaType.APPLICATION_JSON))
.with(SecurityMockMvcRequestPostProcessors.csrf())
.with(SecurityMockMvcRequestPostProcessors.jwt().jwt(bbtJwtToken()))
.content(unitJson)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isForbidden());
}

Expand All @@ -153,9 +158,9 @@ void shouldReturn403ForWrongUserWhenDeletingUnit() throws Exception {
void shouldReturn200ForRightUserWhenDeletingUnit() throws Exception {
mvc
.perform(delete(URL_BASE + "/101")
.with(SecurityMockMvcRequestPostProcessors.csrf())
.with(SecurityMockMvcRequestPostProcessors.jwt().jwt(glJwtToken()))
.contentType(MediaType.APPLICATION_JSON))
.with(SecurityMockMvcRequestPostProcessors.csrf())
.with(SecurityMockMvcRequestPostProcessors.jwt().jwt(glJwtToken()))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk());
verify(unitAuthorizationService, times(1)).deleteUnitById(101L);
}
Expand Down

0 comments on commit 69e7b18

Please sign in to comment.