Skip to content

Commit

Permalink
formatting changes and remove useless methode
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel7373 committed Jan 13, 2025
1 parent 690db09 commit 8453811
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package ch.puzzle.okr.repository;

import ch.puzzle.okr.models.Completed;
import org.springframework.data.repository.CrudRepository;

import java.util.Optional;
import org.springframework.data.repository.CrudRepository;

public interface CompletedRepository extends CrudRepository<Completed, Long> {
Optional<Completed> findByObjectiveId(Long objectiveId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public String getModelName() {
}

public Completed getCompletedByObjectiveId(Long objectiveId) {
return getRepository().findByObjectiveId(objectiveId).orElseThrow(() -> createEntityNotFoundException(objectiveId));
return getRepository()
.findByObjectiveId(objectiveId)
.orElseThrow(() -> createEntityNotFoundException(objectiveId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ void shouldThrowExceptionWhenNotAuthorizedToGetCompletedByObjectiveId() {
.hasRoleReadByObjectiveId(objectiveId, authorizationUser);

ResponseStatusException exception = assertThrows(ResponseStatusException.class,
() -> completedAuthorizationService
.getCompletedByObjectiveId(objectiveId));
() -> completedAuthorizationService
.getCompletedByObjectiveId(objectiveId));
assertEquals(UNAUTHORIZED, exception.getStatusCode());
assertEquals(reason, exception.getReason());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,12 @@ void getCompletedShouldGetCompletedByObjectiveId() {
@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));
() -> completedPersistenceService
.getCompletedByObjectiveId(-1L));

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

assertEquals(NOT_FOUND, exception.getStatusCode());
assertThat(expectedErrors).hasSameElementsAs(exception.getErrors());
Expand All @@ -154,21 +155,15 @@ void deleteByIdShouldDeleteExistingCompletedByObjectiveId() {
@DisplayName("Should throw exception on findById() when id does not exist")
@Test
void deleteCompletedShouldThrowExceptionWhenCompletedNotFound() {
long noExistentId = getNonExistentId();

OkrResponseStatusException exception = assertThrows(OkrResponseStatusException.class,
() -> completedPersistenceService.findById(noExistentId));
() -> completedPersistenceService.findById(-1L));

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

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

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

0 comments on commit 8453811

Please sign in to comment.