Skip to content

Commit

Permalink
aui hei Täuer aber niemer isch am ässe
Browse files Browse the repository at this point in the history
  • Loading branch information
kcinay055679 committed Feb 15, 2025
1 parent 013984e commit baa8860
Showing 1 changed file with 137 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,42 +104,42 @@ void tearDown() {
TenantContext.setCurrentTenant(null);
}

// @DisplayName("Should save new key result on save()")
// @Test
// void saveKeyResultShouldSaveNewKeyResult() {
// KeyResult keyResult = createKeyResultMetric(null);
//
// createdKeyResult = keyResultPersistenceService.save(keyResult);
//
// assertNotNull(createdKeyResult.getId());
// assertEquals(keyResult.getModifiedOn(), createdKeyResult.getModifiedOn());
// assertEquals(keyResult.getTitle(), createdKeyResult.getTitle());
// assertEquals(keyResult.getObjective(), createdKeyResult.getObjective());
// assertEquals(keyResult.getOwner(), createdKeyResult.getOwner());
// assertEquals(keyResult.getDescription(), createdKeyResult.getDescription());
// }
//
// @DisplayName("Should return correct key result on findById()")
// @Test
// void shouldFindKeyResultById() {
// KeyResult keyResult = keyResultPersistenceService.findById(3L);
//
// assertEquals(3L, keyResult.getId());
// assertEquals("Steigern der URS um 25%", keyResult.getTitle());
// }

// @DisplayName("Should throw exception on findById() when id does not exist")
// @Test
// void getKeyResultByIdShouldThrowExceptionWhenKeyResultNotFound() {
// OkrResponseStatusException exception = assertThrows(OkrResponseStatusException.class,
// () -> keyResultPersistenceService.findById(321L));
//
// List<ErrorDto> expectedErrors = List.of(new ErrorDto(MODEL_NOT_FOUND, List.of(KEY_RESULT, "321")));
//
// assertEquals(NOT_FOUND, exception.getStatusCode());
// assertThat(expectedErrors).hasSameElementsAs(exception.getErrors());
// assertTrue(TestHelper.getAllErrorKeys(expectedErrors).contains(exception.getReason()));
// }
@DisplayName("Should save new key result on save()")
@Test
void saveKeyResultShouldSaveNewKeyResult() {
KeyResult keyResult = createKeyResultMetric(null);

createdKeyResult = keyResultPersistenceService.save(keyResult);

assertNotNull(createdKeyResult.getId());
assertEquals(keyResult.getModifiedOn(), createdKeyResult.getModifiedOn());
assertEquals(keyResult.getTitle(), createdKeyResult.getTitle());
assertEquals(keyResult.getObjective(), createdKeyResult.getObjective());
assertEquals(keyResult.getOwner(), createdKeyResult.getOwner());
assertEquals(keyResult.getDescription(), createdKeyResult.getDescription());
}

@DisplayName("Should return correct key result on findById()")
@Test
void shouldFindKeyResultById() {
KeyResult keyResult = keyResultPersistenceService.findById(3L);

assertEquals(3L, keyResult.getId());
assertEquals("Steigern der URS um 25%", keyResult.getTitle());
}

@DisplayName("Should throw exception on findById() when id does not exist")
@Test
void getKeyResultByIdShouldThrowExceptionWhenKeyResultNotFound() {
OkrResponseStatusException exception = assertThrows(OkrResponseStatusException.class,
() -> keyResultPersistenceService.findById(321L));

List<ErrorDto> expectedErrors = List.of(new ErrorDto(MODEL_NOT_FOUND, List.of(KEY_RESULT, "321")));

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

@DisplayName("Should throw exception on findById() when id is null")
@Test
Expand Down Expand Up @@ -210,116 +210,109 @@ void recreateEntityShouldUpdateKeyResultWithTypeChange() {
KeyResult byId = keyResultPersistenceService.findById(keyResultId);
assertTrue(byId.isDeleted());

// List<ErrorDto> expectedErrors = List.of(ErrorDto.of(MODEL_NOT_FOUND, List.of(KEY_RESULT, keyResultId)));
//
// assertEquals(NOT_FOUND, exception.getStatusCode());
// assertThat(expectedErrors).hasSameElementsAs(exception.getErrors());
// assertTrue(TestHelper.getAllErrorKeys(expectedErrors).contains(exception.getReason()));

// delete re-created key result in tearDown()
createdKeyResult = recreatedKeyResult;
}
//
// @DisplayName("Should update key result on updateEntity()")
// @Test
// void updateEntityShouldUpdateKeyResult() {
// KeyResult keyResult = createKeyResultOrdinal(null);
// createdKeyResult = keyResultPersistenceService.save(keyResult);
// KeyResult updateKeyResult = createKeyResultOrdinal(createdKeyResult.getId(), createdKeyResult.getVersion());
// updateKeyResult.setTitle(KEY_RESULT_TITLE);
// updateKeyResult.setDescription(KEY_RESULT_DESCRIPTION);
//
// KeyResult updatedKeyResult = keyResultPersistenceService.updateEntity(updateKeyResult);
//
// assertEquals(createdKeyResult.getId(), updatedKeyResult.getId());
// assertEquals(createdKeyResult.getVersion() + 1, updatedKeyResult.getVersion());
// assertEquals(KEY_RESULT_TITLE, updatedKeyResult.getTitle());
// assertEquals(KEY_RESULT_DESCRIPTION, updatedKeyResult.getDescription());
// assertEquals(createdKeyResult.getOwner().getId(), updatedKeyResult.getOwner().getId());
// assertEquals(createdKeyResult.getObjective().getId(), updatedKeyResult.getObjective().getId());
// assertEquals(createdKeyResult.getModifiedOn(), updatedKeyResult.getModifiedOn());
// }
//
// @DisplayName("Should throw exception on updateEntity() when entity was already updated in the mean time")
// @Test
// void updateEntityShouldThrowExceptionWhenAlreadyUpdated() {
// KeyResult keyResult = createKeyResultOrdinal(null);
// createdKeyResult = keyResultPersistenceService.save(keyResult);
// KeyResult updateKeyResult = createKeyResultOrdinal(createdKeyResult.getId(), 0);
// updateKeyResult.setTitle(KEY_RESULT_TITLE);
// updateKeyResult.setDescription(KEY_RESULT_DESCRIPTION);
//
// OkrResponseStatusException exception = assertThrows(OkrResponseStatusException.class,
// () -> keyResultPersistenceService
// .updateEntity(updateKeyResult));
// List<ErrorDto> expectedErrors = List.of(new ErrorDto("DATA_HAS_BEEN_UPDATED", List.of(KEY_RESULT)));
//
// assertEquals(UNPROCESSABLE_ENTITY, exception.getStatusCode());
// assertThat(expectedErrors).hasSameElementsAs(exception.getErrors());
// assertTrue(TestHelper.getAllErrorKeys(expectedErrors).contains(exception.getReason()));
// }
//
// @DisplayName("Should return list of key results on getKeyResultsByObjective()")
// @Test
// void getKeyResultsByObjectiveShouldReturnListOfKeyResults() {
// List<KeyResult> keyResultsByObjective = keyResultPersistenceService.getKeyResultsByObjective(3L);
//
// assertEquals(3, keyResultsByObjective.size());
// }
//
// @DisplayName("Should delete entity on deleteById()")
// @Test
// void deleteKeyResultByIdShouldDeleteExistingKeyResult() {
// KeyResult keyResult = createKeyResultMetric(null);
// createdKeyResult = keyResultPersistenceService.save(keyResult);
// keyResultPersistenceService.deleteById(createdKeyResult.getId(), new HardDelete<>());
//
// Long keyResultId = createdKeyResult.getId();
// OkrResponseStatusException exception = assertThrows(OkrResponseStatusException.class,
// () -> keyResultPersistenceService.findById(keyResultId));
//
// List<ErrorDto> expectedErrors = List.of(ErrorDto.of(MODEL_NOT_FOUND, List.of(KEY_RESULT, keyResultId)));
//
// assertEquals(NOT_FOUND, exception.getStatusCode());
// assertThat(expectedErrors).hasSameElementsAs(exception.getErrors());
// assertTrue(TestHelper.getAllErrorKeys(expectedErrors).contains(exception.getReason()));
// }
//
// @DisplayName("Should mark as deleted on deleteById() per default")
// @Test
// void shouldDeleteCheckInById() {
// KeyResult keyResult = createKeyResultMetric(null);
//
// var newEntity = keyResultPersistenceService.save(keyResult);
//
// long entityId = newEntity.getId();
//
// // act
// keyResultPersistenceService.deleteById(entityId);
//
// // assert
// assertTrue(keyResultPersistenceService.findById(entityId).isDeleted());
//
// Mockito.verify(keyResultRepository, Mockito.times(1)).markAsDeleted(entityId);
// keyResultPersistenceService.deleteById(entityId, new HardDelete<>());
//
// }
//
// @DisplayName("Should throw exception on deleteById() when id does not exist")
// @Test
// void deleteKeyResultShouldThrowExceptionWhenKeyResultNotFound() {
// long nonExistentId = getNonExistentId();
//
// OkrResponseStatusException exception = assertThrows(OkrResponseStatusException.class,
// () -> keyResultPersistenceService.findById(nonExistentId));
//
// List<ErrorDto> expectedErrors = List.of(ErrorDto.of(MODEL_NOT_FOUND, List.of(KEY_RESULT, nonExistentId)));
//
// assertEquals(NOT_FOUND, exception.getStatusCode());
// assertThat(expectedErrors).hasSameElementsAs(exception.getErrors());
// assertTrue(TestHelper.getAllErrorKeys(expectedErrors).contains(exception.getReason()));
// }
//

@DisplayName("Should update key result on updateEntity()")
@Test
void updateEntityShouldUpdateKeyResult() {
KeyResult keyResult = createKeyResultOrdinal(null);
createdKeyResult = keyResultPersistenceService.save(keyResult);
KeyResult updateKeyResult = createKeyResultOrdinal(createdKeyResult.getId(), createdKeyResult.getVersion());
updateKeyResult.setTitle(KEY_RESULT_TITLE);
updateKeyResult.setDescription(KEY_RESULT_DESCRIPTION);

KeyResult updatedKeyResult = keyResultPersistenceService.updateEntity(updateKeyResult);

assertEquals(createdKeyResult.getId(), updatedKeyResult.getId());
assertEquals(createdKeyResult.getVersion() + 1, updatedKeyResult.getVersion());
assertEquals(KEY_RESULT_TITLE, updatedKeyResult.getTitle());
assertEquals(KEY_RESULT_DESCRIPTION, updatedKeyResult.getDescription());
assertEquals(createdKeyResult.getOwner().getId(), updatedKeyResult.getOwner().getId());
assertEquals(createdKeyResult.getObjective().getId(), updatedKeyResult.getObjective().getId());
assertEquals(createdKeyResult.getModifiedOn(), updatedKeyResult.getModifiedOn());
}

@DisplayName("Should throw exception on updateEntity() when entity was already updated in the mean time")
@Test
void updateEntityShouldThrowExceptionWhenAlreadyUpdated() {
KeyResult keyResult = createKeyResultOrdinal(null);
createdKeyResult = keyResultPersistenceService.save(keyResult);
KeyResult updateKeyResult = createKeyResultOrdinal(createdKeyResult.getId(), 0);
updateKeyResult.setTitle(KEY_RESULT_TITLE);
updateKeyResult.setDescription(KEY_RESULT_DESCRIPTION);

OkrResponseStatusException exception = assertThrows(OkrResponseStatusException.class,
() -> keyResultPersistenceService
.updateEntity(updateKeyResult));
List<ErrorDto> expectedErrors = List.of(new ErrorDto("DATA_HAS_BEEN_UPDATED", List.of(KEY_RESULT)));

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

@DisplayName("Should return list of key results on getKeyResultsByObjective()")
@Test
void getKeyResultsByObjectiveShouldReturnListOfKeyResults() {
List<KeyResult> keyResultsByObjective = keyResultPersistenceService.getKeyResultsByObjective(3L);

assertEquals(3, keyResultsByObjective.size());
}

@DisplayName("Should delete entity on deleteById()")
@Test
void deleteKeyResultByIdShouldDeleteExistingKeyResult() {
KeyResult keyResult = createKeyResultMetric(null);
createdKeyResult = keyResultPersistenceService.save(keyResult);
keyResultPersistenceService.deleteById(createdKeyResult.getId(), new HardDelete<>());

Long keyResultId = createdKeyResult.getId();
OkrResponseStatusException exception = assertThrows(OkrResponseStatusException.class,
() -> keyResultPersistenceService.findById(keyResultId));

List<ErrorDto> expectedErrors = List.of(ErrorDto.of(MODEL_NOT_FOUND, List.of(KEY_RESULT, keyResultId)));

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

@DisplayName("Should mark as deleted on deleteById() per default")
@Test
void shouldDeleteCheckInById() {
KeyResult keyResult = createKeyResultMetric(null);

var newEntity = keyResultPersistenceService.save(keyResult);

long entityId = newEntity.getId();

// act
keyResultPersistenceService.deleteById(entityId);

// assert
assertTrue(keyResultPersistenceService.findById(entityId).isDeleted());

Mockito.verify(keyResultRepository, Mockito.times(1)).markAsDeleted(entityId);
keyResultPersistenceService.deleteById(entityId, new HardDelete<>());

}

@DisplayName("Should throw exception on deleteById() when id does not exist")
@Test
void deleteKeyResultShouldThrowExceptionWhenKeyResultNotFound() {
long nonExistentId = getNonExistentId();

OkrResponseStatusException exception = assertThrows(OkrResponseStatusException.class,
() -> keyResultPersistenceService.findById(nonExistentId));

List<ErrorDto> expectedErrors = List.of(ErrorDto.of(MODEL_NOT_FOUND, List.of(KEY_RESULT, nonExistentId)));

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

private long getNonExistentId() {
long id = keyResultPersistenceService.findAll().stream().mapToLong(KeyResult::getId).max().orElse(10L);

Expand Down

0 comments on commit baa8860

Please sign in to comment.