Skip to content

Commit

Permalink
change backend and add tests for completed services
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel7373 committed Jan 13, 2025
1 parent 3552bbf commit 0a5e4b2
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import ch.puzzle.okr.models.Completed;
import org.springframework.data.repository.CrudRepository;

import java.util.Optional;

public interface CompletedRepository extends CrudRepository<Completed, Long> {
Completed findByObjectiveId(Long objectiveId);
Optional<Completed> findByObjectiveId(Long objectiveId);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package ch.puzzle.okr.service.business;

import ch.puzzle.okr.ErrorKey;
import ch.puzzle.okr.exception.OkrResponseStatusException;
import ch.puzzle.okr.models.Completed;
import ch.puzzle.okr.models.Objective;
import ch.puzzle.okr.service.persistence.CompletedPersistenceService;
import ch.puzzle.okr.service.validation.CompletedValidationService;
import jakarta.transaction.Transactional;
import org.springframework.stereotype.Service;

import java.util.List;

import static org.springframework.http.HttpStatus.NOT_FOUND;

@Service
public class CompletedBusinessService {
private final CompletedPersistenceService completedPersistenceService;
Expand Down Expand Up @@ -33,6 +40,7 @@ public void deleteCompletedByObjectiveId(Long objectiveId) {
}

public Completed getCompletedByObjectiveId(Long objectiveId) {
validator.validateOnGet(objectiveId);
return completedPersistenceService.getCompletedByObjectiveId(objectiveId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public String getModelName() {
}

public Completed getCompletedByObjectiveId(Long objectiveId) {
return getRepository().findByObjectiveId(objectiveId);
return getRepository().findByObjectiveId(objectiveId).orElseThrow(() -> createEntityNotFoundException(objectiveId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,20 @@ void shouldThrowExceptionWhenNotAuthorizedToDeleteCompletedByObjectiveId() {
assertEquals(UNAUTHORIZED, exception.getStatusCode());
assertEquals(reason, exception.getReason());
}

@DisplayName("Should throw an exception when the user is not authorized to get completed object by objective ID")
@Test
void shouldThrowExceptionWhenNotAuthorizedToGetCompletedByObjectiveId() {
String reason = "junit test reason";
when(authorizationService.updateOrAddAuthorizationUser()).thenReturn(authorizationUser);
doThrow(new ResponseStatusException(HttpStatus.UNAUTHORIZED, reason))
.when(authorizationService)
.hasRoleReadByObjectiveId(objectiveId, authorizationUser);

ResponseStatusException exception = assertThrows(ResponseStatusException.class,
() -> completedAuthorizationService
.getCompletedByObjectiveId(objectiveId));
assertEquals(UNAUTHORIZED, exception.getStatusCode());
assertEquals(reason, exception.getReason());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,15 @@ void shouldDoNothingIfCompletedIsNull() {

verify(validator, never()).validateOnDelete(anyLong());
}

@DisplayName("Should get completed by objective id")
@Test
void shouldGetCompleted() {
when(completedPersistenceService.getCompletedByObjectiveId(anyLong())).thenReturn(successfulCompleted);

this.completedBusinessService.getCompletedByObjectiveId(1L);

verify(this.completedPersistenceService, times(1)).getCompletedByObjectiveId(1L);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.server.ResponseStatusException;

@SpringIntegrationTest
class CompletedPersistenceServiceIT {
private static final Logger log = LoggerFactory.getLogger(CompletedPersistenceServiceIT.class);
@Autowired
private CompletedPersistenceService completedPersistenceService;
private Completed createdCompleted;
Expand Down Expand Up @@ -118,6 +121,20 @@ void getCompletedShouldGetCompletedByObjectiveId() {
savedCompleted.getObjective().getTitle());
}

@DisplayName("Should throw exception on getCompletedByObjectiveId() when id does not exist")
@Test
void getCompletedShouldThrowExceptionWhenCompletedNotFound() {
long noExistentId = getNonExistentId();
OkrResponseStatusException exception = assertThrows(OkrResponseStatusException.class,
() -> completedPersistenceService.getCompletedByObjectiveId(noExistentId));

List<ErrorDto> expectedErrors = List.of(new ErrorDto("MODEL_WITH_ID_NOT_FOUND",List.of(COMPLETED, String.valueOf(noExistentId))));

assertEquals(NOT_FOUND, exception.getStatusCode());
assertThat(expectedErrors).hasSameElementsAs(exception.getErrors());
assertTrue(TestHelper.getAllErrorKeys(expectedErrors).contains(exception.getReason()));
}

@DisplayName("Should delete entity on deleteById()")
@Test
void deleteByIdShouldDeleteExistingCompletedByObjectiveId() {
Expand Down Expand Up @@ -152,6 +169,6 @@ void deleteCompletedShouldThrowExceptionWhenCompletedNotFound() {

private long getNonExistentId() {
long id = completedPersistenceService.findAll().stream().mapToLong(Completed::getId).max().orElse(10L);
return id + 1;
return id + 10;
}
}

0 comments on commit 0a5e4b2

Please sign in to comment.