Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update custom habit implementation change #6520

Merged
merged 5 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions service/src/main/java/greencity/service/HabitServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import greencity.dto.habit.CustomHabitDtoRequest;
import greencity.dto.habit.CustomHabitDtoResponse;
import greencity.dto.habit.HabitDto;
import greencity.dto.habittranslation.HabitTranslationDto;
import greencity.dto.shoppinglistitem.ShoppingListItemDto;
import greencity.dto.user.UserProfilePictureDto;
import greencity.dto.user.UserVO;
Expand Down Expand Up @@ -382,12 +383,11 @@ private void enhanceHabitWithNewData(Habit toUpdate, CustomHabitDtoRequest habit
toUpdate.setDefaultDuration(habitDto.getDefaultDuration());
}
if (isNotEmpty(habitDto.getHabitTranslations())) {
habitTranslationRepo.deleteAllByHabit(toUpdate);
saveHabitTranslationListsToHabitTranslationRepo(habitDto, toUpdate);
updateHabitTranslationsForCustomHabit(habitDto, toUpdate);
}
if (isNotEmpty(habitDto.getCustomShoppingListItemDto())) {
customShoppingListItemRepo.deleteCustomShoppingListItemsByHabitId(toUpdate.getId());
setCustomShoppingListItemToHabit(habitDto, toUpdate, user);
saveNewCustomShoppingListItemsToUpdate(habitDto, toUpdate, user);
updateExistingCustomShoppingListItems(habitDto, toUpdate, user);
}
if (StringUtils.isNotBlank(habitDto.getImage())) {
image = fileService.convertToMultipartImage(habitDto.getImage());
Expand All @@ -400,6 +400,48 @@ private void enhanceHabitWithNewData(Habit toUpdate, CustomHabitDtoRequest habit
}
}

private void saveNewCustomShoppingListItemsToUpdate(CustomHabitDtoRequest habitDto, Habit habit, User user) {
List<CustomShoppingListItem> customShoppingListItems = customShoppingListMapper
.mapAllToList(habitDto.getCustomShoppingListItemDto());

customShoppingListItems.stream()
.filter(item -> Objects.isNull(item.getId()))
.forEach(customShoppingListItem -> {
customShoppingListItem.setHabit(habit);
customShoppingListItem.setUser(user);
customShoppingListItemRepo.save(customShoppingListItem);
});
}

private void updateExistingCustomShoppingListItems(CustomHabitDtoRequest habitDto, Habit habit, User user) {
List<CustomShoppingListItem> customShoppingListItems = customShoppingListItemRepo
.findAllByUserIdAndHabitId(user.getId(), habit.getId());

customShoppingListItems.stream()
.forEach(item -> habitDto.getCustomShoppingListItemDto().stream()
.filter(itemToUpdate -> item.getId().equals(itemToUpdate.getId()))
.forEach(itemToUpdate -> {
item.setStatus(itemToUpdate.getStatus());
item.setText(itemToUpdate.getText());
}));

customShoppingListItemRepo.deleteAll(customShoppingListItems.stream()
.filter(item -> habitDto.getCustomShoppingListItemDto().stream()
.noneMatch(itemToUpdate -> item.getId().equals(itemToUpdate.getId())))
.collect(Collectors.toList()));
}

private void updateHabitTranslationsForCustomHabit(CustomHabitDtoRequest habitDto, Habit habit) {
Optional<HabitTranslationDto> habitTranslationDtoOptional = habitDto.getHabitTranslations().stream()
.findFirst();
habitTranslationDtoOptional.ifPresent(habitTranslationDto -> habitTranslationRepo.findAllByHabit(habit)
.forEach(habitTranslation -> {
habitTranslation.setName(habitTranslationDto.getName());
habitTranslation.setDescription(habitTranslationDto.getDescription());
habitTranslation.setHabitItem(habitTranslationDto.getHabitItem());
}));
}

private void saveHabitTranslationListsToHabitTranslationRepo(CustomHabitDtoRequest habitDto, Habit habit) {
List<HabitTranslation> habitTranslationListForUa = mapHabitTranslationFromAddCustomHabitDtoRequest(habitDto);
habitTranslationListForUa.forEach(habitTranslation -> habitTranslation.setHabit(habit));
Expand Down
16 changes: 15 additions & 1 deletion service/src/test/java/greencity/ModelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2838,12 +2838,26 @@ public static CustomHabitDtoRequest getAddCustomHabitDtoRequestForServiceTest()
.build();
}

public static CustomHabitDtoRequest getСustomHabitDtoRequestWithTagsForServiceTest() {
public static CustomHabitDtoRequest getСustomHabitDtoRequestWithNewCustomShoppingListItem() {
return CustomHabitDtoRequest.builder()
.customShoppingListItemDto(List.of(
CustomShoppingListItemResponseDto.builder()
.id(null)
.status(ShoppingListItemStatus.ACTIVE)
.text(SHOPPING_LIST_TEXT)
.build()))
.tagIds(Set.of(20L))
.build();
}

public static CustomShoppingListItem getCustomShoppingListItemForUpdate() {
return CustomShoppingListItem.builder()
.id(1L)
.status(ShoppingListItemStatus.ACTIVE)
.text(SHOPPING_LIST_TEXT)
.build();
}

public static CustomHabitDtoRequest getСustomHabitDtoRequestWithComplexityAndDuration() {
return CustomHabitDtoRequest.builder()
.complexity(2)
Expand Down
61 changes: 12 additions & 49 deletions service/src/test/java/greencity/service/HabitServiceImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -816,10 +816,6 @@ void updateCustomHabitTest() throws IOException {
when(habitRepo.findById(1L)).thenReturn(Optional.of(habit));
when(habitRepo.save(customHabitMapper.convert(customHabitDtoRequest))).thenReturn(habit);
when(tagsRepo.findById(20L)).thenReturn(Optional.of(tag));
when(habitTranslationMapper.mapAllToList(List.of(habitTranslationDto)))
.thenReturn(List.of(habitTranslationUa));
when(languageRepo.findByCode("ua")).thenReturn(Optional.of(languageUa));
when(languageRepo.findByCode("en")).thenReturn(Optional.of(languageEn));
when(customShoppingListItemRepo.findAllByUserIdAndHabitId(anyLong(), anyLong()))
.thenReturn(List.of(customShoppingListItem));
when(customShoppingListMapper.mapAllToList(List.of(customShoppingListItemResponseDto)))
Expand All @@ -838,13 +834,11 @@ void updateCustomHabitTest() throws IOException {
verify(habitRepo).save(any());
verify(customHabitMapper).convert(customHabitDtoRequest);
verify(tagsRepo).findById(20L);
verify(habitTranslationMapper, times(2)).mapAllToList(List.of(habitTranslationDto));
verify(languageRepo, times(2)).findByCode(anyString());
verify(customShoppingListItemRepo).findAllByUserIdAndHabitId(anyLong(), anyLong());
verify(customShoppingListItemRepo, times(2)).findAllByUserIdAndHabitId(anyLong(), anyLong());
verify(customShoppingListMapper).mapAllToList(anyList());
verify(modelMapper).map(habit, CustomHabitDtoResponse.class);
verify(customShoppingListResponseDtoMapper).mapAllToList(List.of(customShoppingListItem));
verify(habitTranslationRepo).findAllByHabit(habit);
verify(habitTranslationRepo, times(2)).findAllByHabit(habit);
verify(habitTranslationDtoMapper).mapAllToList(habitTranslationList);
}

Expand All @@ -862,44 +856,6 @@ void updateCustomHabitThrowsUserNotFoundException() {
verify(customHabitMapper).convert(customHabitDtoRequest);
}

@Test
void updateCustomHabitNoSuchElementExceptionWithNotExistingLanguageCodes() throws IOException {
User user = ModelUtils.getUser();
user.setRole(Role.ROLE_MODERATOR);
Tag tag = ModelUtils.getTagHabitForServiceTest();
Language languageUa = ModelUtils.getLanguageUa();
Habit habit = ModelUtils.getCustomHabitForServiceTest();
MultipartFile image = ModelUtils.getFile();
String imageToEncode = Base64.getEncoder().encodeToString(image.getBytes());
habit.setTags(Set.of(tag));
habit.setUserId(1L);
habit.setImage(imageToEncode);

CustomHabitDtoRequest customHabitDtoRequest =
ModelUtils.getAddCustomHabitDtoRequestForServiceTest();
customHabitDtoRequest.setImage(ModelUtils.HABIT_DEFAULT_IMAGE);

HabitTranslationDto habitTranslationDto = ModelUtils.getHabitTranslationDto();
habitTranslationDto.setLanguageCode("ua");
HabitTranslation habitTranslationUa = ModelUtils.getHabitTranslationForServiceTest();

when(habitRepo.findById(1L)).thenReturn(Optional.of(habit));
when(userRepo.findByEmail(user.getEmail())).thenReturn(Optional.of(user));
when(habitTranslationMapper.mapAllToList(List.of(habitTranslationDto)))
.thenReturn(List.of(habitTranslationUa));
when(languageRepo.findByCode("ua")).thenReturn(Optional.of(languageUa));
when(languageRepo.findByCode("en")).thenReturn(Optional.empty());

assertThrows(NoSuchElementException.class, () -> habitService.updateCustomHabit(
customHabitDtoRequest, 1L, "[email protected]", image));

verify(habitRepo).findById(anyLong());
verify(userRepo).findByEmail(user.getEmail());
verify(habitTranslationMapper, times(2))
.mapAllToList(customHabitDtoRequest.getHabitTranslations());
verify(languageRepo, times(2)).findByCode(anyString());
}

@Test
void updateCustomHabitThrowsUserHasNoPermissionToAccessException() {
CustomHabitDtoRequest customHabitDtoRequest =
Expand All @@ -921,7 +877,7 @@ void updateCustomHabitThrowsUserHasNoPermissionToAccessException() {
}

@Test
void updateCustomHabitWithOneParameterToUpdateTest() throws IOException {
void updateCustomHabitWithNewCustomShoppingListItemToUpdateTest() throws IOException {
User user = ModelUtils.getTestUser();
user.setRole(Role.ROLE_ADMIN);
Tag tag = ModelUtils.getTagHabitForServiceTest();
Expand All @@ -930,10 +886,15 @@ void updateCustomHabitWithOneParameterToUpdateTest() throws IOException {
String imageToEncode = Base64.getEncoder().encodeToString(image.getBytes());
habit.setUserId(1L);
habit.setImage(imageToEncode);
CustomShoppingListItem newItem = ModelUtils.getCustomShoppingListItemForUpdate();
newItem.setId(null);

CustomHabitDtoRequest customHabitDtoRequest = ModelUtils.getСustomHabitDtoRequestWithTagsForServiceTest();
CustomHabitDtoRequest customHabitDtoRequest = ModelUtils
.getСustomHabitDtoRequestWithNewCustomShoppingListItem();
CustomHabitDtoResponse customHabitDtoResponse = ModelUtils.getAddCustomHabitDtoResponse();

when(customShoppingListMapper.mapAllToList(any()))
.thenReturn(List.of(newItem));
when(customShoppingListItemRepo.save(any())).thenReturn(ModelUtils.getCustomShoppingListItemForUpdate());
when(userRepo.findByEmail(user.getEmail())).thenReturn(Optional.of(user));
when(habitRepo.findById(1L)).thenReturn(Optional.of(habit));
when(tagsRepo.findById(20L)).thenReturn(Optional.of(tag));
Expand All @@ -944,6 +905,8 @@ void updateCustomHabitWithOneParameterToUpdateTest() throws IOException {
assertEquals(customHabitDtoResponse,
habitService.updateCustomHabit(customHabitDtoRequest, 1L, "[email protected]", image));

verify(customShoppingListItemRepo, times(2)).findAllByUserIdAndHabitId(2L, 1L);
verify(customShoppingListItemRepo).save(any());
verify(habitRepo).findById(anyLong());
verify(userRepo).findByEmail(user.getEmail());
verify(habitRepo).save(any());
Expand Down
Loading