Skip to content

Commit

Permalink
make all objectiveTests work again except duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
nevio18324 committed Dec 3, 2024
1 parent d29ba72 commit 99d3a0e
Showing 1 changed file with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.*;
Expand All @@ -48,13 +52,21 @@ 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",
"teamId": 1, "teamName": "Team1", "quarterId": 1, "quarterNumber": 3, "quarterYear": 2020,
"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",
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -217,21 +233,25 @@ void throwExceptionWhenObjectiveWithIdCantBeFoundWhileDeleting() throws Exceptio

@Test
void shouldReturnIsCreatedWhenObjectiveWasDuplicated() throws Exception {
List<KeyResult> 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());
}
Expand Down

0 comments on commit 99d3a0e

Please sign in to comment.