diff --git a/backend/src/test/java/ch/puzzle/okr/controller/ObjectiveControllerIT.java b/backend/src/test/java/ch/puzzle/okr/controller/ObjectiveControllerIT.java index 15b9ce5838..4d3f4fc738 100644 --- a/backend/src/test/java/ch/puzzle/okr/controller/ObjectiveControllerIT.java +++ b/backend/src/test/java/ch/puzzle/okr/controller/ObjectiveControllerIT.java @@ -2,11 +2,14 @@ import ch.puzzle.okr.dto.ObjectiveDto; import ch.puzzle.okr.mapper.ObjectiveMapper; +import ch.puzzle.okr.mapper.keyresult.KeyResultMapper; import ch.puzzle.okr.models.*; -import ch.puzzle.okr.models.keyresult.KeyResult; import ch.puzzle.okr.models.keyresult.KeyResultMetric; +import ch.puzzle.okr.models.keyresult.KeyResultOrdinal; +import ch.puzzle.okr.service.authorization.ActionAuthorizationService; import ch.puzzle.okr.service.authorization.AuthorizationService; import ch.puzzle.okr.service.authorization.ObjectiveAuthorizationService; +import org.apache.coyote.Request; import org.hamcrest.core.Is; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -17,19 +20,20 @@ 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.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors; import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import org.springframework.web.server.ResponseStatusException; +import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.List; -import static ch.puzzle.okr.test.KeyResultTestHelpers.metricKeyResult; +import static ch.puzzle.okr.test.KeyResultTestHelpers.*; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.Mockito.*; @@ -48,6 +52,7 @@ class ObjectiveControllerIT { private static final String URL_BASE_OBJECTIVE = "/api/v2/objectives"; private static final String URL_OBJECTIVE_5 = "/api/v2/objectives/5"; private static final String URL_OBJECTIVE_10 = "/api/v2/objectives/10"; + private static final String URL_DUPLICATE_OBJECTIVE_5 = "/api/v2/objectives/5"; private static final String JSON = """ { "title": "FullObjective", "ownerId": 1, "ownerFirstname": "Bob", "ownerLastname": "Kaufmann", @@ -55,6 +60,13 @@ class ObjectiveControllerIT { "description": "This is our description", "progress": 33.3 } """; + private static final String DUPLICATE_OBJECTIVE = """ + { + "objective": %s, + "keyResults": [%s,%s] + } + """.formatted(JSON, KEY_RESULT_METRIC_JSON, KEY_RESULT_ORDINAL_JSON); + private static final String CREATE_NEW_OBJECTIVE = """ { "title": "FullObjective", "ownerId": 1, "teamId": 1, "teamName": "Team1", @@ -94,7 +106,11 @@ class ObjectiveControllerIT { @Mock private AuthorizationService authorizationService; @MockBean + private ActionAuthorizationService actionAuthorizationService; + @MockBean private ObjectiveMapper objectiveMapper; + @MockBean + private KeyResultMapper keyResultMapper; @BeforeEach void setUp() { @@ -217,21 +233,25 @@ void throwExceptionWhenObjectiveWithIdCantBeFoundWhileDeleting() throws Exceptio @Test void shouldReturnIsCreatedWhenObjectiveWasDuplicated() throws Exception { - List keyResults = new ArrayList<>(); - keyResults.add(metricKeyResult); - - BDDMockito.given(objectiveAuthorizationService.duplicateEntity(anyLong(), any(), keyResults)) + BDDMockito.given(objectiveAuthorizationService.duplicateEntity(anyLong(), any(), anyList())) .willReturn(objective1); + BDDMockito.given(keyResultMapper.toDto(any(KeyResultMetric.class), any())).willReturn(keyResultMetricDto); + BDDMockito.given(keyResultMapper.toDto(any(KeyResultOrdinal.class), any())).willReturn(keyResultOrdinalDto); BDDMockito.given(objectiveAuthorizationService.getAuthorizationService()).willReturn(authorizationService); BDDMockito.given(objectiveMapper.toDto(objective1)).willReturn(objective1Dto); - - mvc.perform(post("/api/v2/objectives/{id}", objective1.getId()).contentType(MediaType.APPLICATION_JSON) - .content(JSON).with(SecurityMockMvcRequestPostProcessors.csrf())) - .andExpect(MockMvcResultMatchers.status().isCreated()) - .andExpect(jsonPath("$.id", Is.is(objective1Dto.id().intValue()))) - .andExpect(jsonPath("$.description", Is.is(objective1Dto.description()))) - .andExpect(jsonPath(JSON_PATH_TITLE, Is.is(objective1Dto.title()))); - + System.out.println(DUPLICATE_OBJECTIVE); + // mvc.perform(post("/api/v2/objectives/5") + // .with(SecurityMockMvcRequestPostProcessors.csrf()).content("").contentType(MediaType.APPLICATION_JSON)) + // + // .andExpect(jsonPath("$.id", Is.is(objective1Dto.id().intValue()))) + // .andExpect(jsonPath("$.description", Is.is(objective1Dto.description()))) + // .andExpect(jsonPath(JSON_PATH_TITLE, Is.is(objective1Dto.title()))); + + MvcResult a = mvc + .perform(post(URL_DUPLICATE_OBJECTIVE_5).contentType(MediaType.APPLICATION_JSON) + .with(SecurityMockMvcRequestPostProcessors.csrf()).content(DUPLICATE_OBJECTIVE)) + .andExpect(MockMvcResultMatchers.status().isCreated()).andReturn(); + System.out.println(a.getRequest().getContentType()); verify(objectiveMapper, times(1)).toObjective(any()); verify(objectiveMapper, times(1)).toDto(any()); }