From 57996d1bd38cf17ee03c1e7ba0ee1462218344fe Mon Sep 17 00:00:00 2001 From: Chernenko Vitaliy Date: Mon, 9 Dec 2024 15:54:00 +0200 Subject: [PATCH 1/4] Fixed functionality of saving HabitTranslation in saveHabitTranslationListsToHabitTranslationRepo method in HabitServiceImpl; Fixed necessary tests in HabitServiceImplTest; Added getHabitTranslationDtoEnAndUa method in ModelUtils; Added @NotBlank annotation in HabitTranslationDto for description, languageCode and name fields; Added @Valid annotation for validation HabitTranslationDto into CustomHabitDtoRequest; Added convertUa() and mapAllToList() methods into HabitTranslationMapper; Added necessary nests in HabitTranslationMapperTests; --- .../dto/habit/CustomHabitDtoRequest.java | 3 ++ .../habittranslation/HabitTranslationDto.java | 4 ++ .../mapping/HabitTranslationMapper.java | 49 +++++++++++++++++++ .../greencity/service/HabitServiceImpl.java | 17 ++++--- .../src/test/java/greencity/ModelUtils.java | 15 ++++++ .../mapping/HabitTranslationMapperTests.java | 44 +++++++++++++++++ .../service/HabitServiceImplTest.java | 37 +++++++++----- 7 files changed, 152 insertions(+), 17 deletions(-) diff --git a/service-api/src/main/java/greencity/dto/habit/CustomHabitDtoRequest.java b/service-api/src/main/java/greencity/dto/habit/CustomHabitDtoRequest.java index 957c3e62bc..318606d81e 100644 --- a/service-api/src/main/java/greencity/dto/habit/CustomHabitDtoRequest.java +++ b/service-api/src/main/java/greencity/dto/habit/CustomHabitDtoRequest.java @@ -14,9 +14,11 @@ import jakarta.validation.constraints.Min; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Size; +import org.springframework.validation.annotation.Validated; import java.util.List; import java.util.Set; +@Validated @NoArgsConstructor @AllArgsConstructor @Builder @@ -29,6 +31,7 @@ public class CustomHabitDtoRequest { @NotNull(message = ServiceValidationConstants.HABIT_COMPLEXITY) private Integer complexity; private Integer defaultDuration; + @Valid private List habitTranslations; private String image; private List customToDoListItemDto; diff --git a/service-api/src/main/java/greencity/dto/habittranslation/HabitTranslationDto.java b/service-api/src/main/java/greencity/dto/habittranslation/HabitTranslationDto.java index d5c7b10795..e606ae687b 100644 --- a/service-api/src/main/java/greencity/dto/habittranslation/HabitTranslationDto.java +++ b/service-api/src/main/java/greencity/dto/habittranslation/HabitTranslationDto.java @@ -1,6 +1,7 @@ package greencity.dto.habittranslation; import java.io.Serializable; +import jakarta.validation.constraints.NotBlank; import lombok.NoArgsConstructor; import lombok.AllArgsConstructor; import lombok.EqualsAndHashCode; @@ -15,9 +16,12 @@ @EqualsAndHashCode @Builder public class HabitTranslationDto implements Serializable { + @NotBlank private String description; private String habitItem; + @NotBlank private String languageCode; + @NotBlank private String name; private String descriptionUa; private String nameUa; diff --git a/service/src/main/java/greencity/mapping/HabitTranslationMapper.java b/service/src/main/java/greencity/mapping/HabitTranslationMapper.java index d92781fa8b..336da9181b 100644 --- a/service/src/main/java/greencity/mapping/HabitTranslationMapper.java +++ b/service/src/main/java/greencity/mapping/HabitTranslationMapper.java @@ -18,6 +18,37 @@ protected HabitTranslation convert(HabitTranslationDto habitTranslationDto) { .build(); } + /** + * Additional method that build {@link HabitTranslation} from + * {@link HabitTranslationDto} but from nameUa, descriptionUa, habitItemUa + * fields if they not null. + * + * @param habitTranslationDto {@link HabitTranslationDto} + * @return {@link HabitTranslation} + * + * @author Chernenko Vitaliy + */ + public HabitTranslation convertUa(HabitTranslationDto habitTranslationDto) { + HabitTranslation habitTranslation = new HabitTranslation(); + if (habitTranslationDto.getNameUa() == null) { + habitTranslation.setName(habitTranslationDto.getName()); + } else { + habitTranslation.setName(habitTranslationDto.getNameUa()); + } + if (habitTranslationDto.getDescriptionUa() == null) { + habitTranslation.setDescription(habitTranslationDto.getDescription()); + } else { + habitTranslation.setDescription(habitTranslationDto.getDescriptionUa()); + } + if (habitTranslationDto.getHabitItemUa() == null) { + habitTranslation.setHabitItem(habitTranslationDto.getHabitItem()); + } else { + habitTranslation.setHabitItem(habitTranslationDto.getHabitItemUa()); + } + + return habitTranslation; + } + /** * Method that build {@link List} of {@link HabitTranslation} from {@link List} * of {@link HabitTranslationDto}. @@ -29,4 +60,22 @@ protected HabitTranslation convert(HabitTranslationDto habitTranslationDto) { public List mapAllToList(List dtoList) { return dtoList.stream().map(this::convert).collect(Collectors.toList()); } + + /** + * Method that build {@link List} of {@link HabitTranslation} from {@link List} + * of {@link HabitTranslationDto} and {@link String} language. + * + * @param dtoList {@link List} of {@link HabitTranslationDto} + * @param language {@link String} + * + * @return {@link List} of {@link HabitTranslation} + * + * @author Chernenko Vitaliy + */ + public List mapAllToList(List dtoList, String language) { + if (language.equals("ua")) { + return dtoList.stream().map(this::convertUa).collect(Collectors.toList()); + } + return dtoList.stream().map(this::convert).collect(Collectors.toList()); + } } diff --git a/service/src/main/java/greencity/service/HabitServiceImpl.java b/service/src/main/java/greencity/service/HabitServiceImpl.java index f31d82fa6e..fbd4057c66 100644 --- a/service/src/main/java/greencity/service/HabitServiceImpl.java +++ b/service/src/main/java/greencity/service/HabitServiceImpl.java @@ -491,21 +491,26 @@ private void updateHabitTranslationsForCustomHabit(CustomHabitDtoRequest habitDt } private void saveHabitTranslationListsToHabitTranslationRepo(CustomHabitDtoRequest habitDto, Habit habit) { - List habitTranslationListForUa = mapHabitTranslationFromAddCustomHabitDtoRequest(habitDto); + final String UA = "ua"; + final String EN = "en"; + List habitTranslationListForUa = + mapHabitTranslationFromAddCustomHabitDtoRequest(habitDto, UA); habitTranslationListForUa.forEach(habitTranslation -> habitTranslation.setHabit(habit)); habitTranslationListForUa.forEach(habitTranslation -> habitTranslation.setLanguage( - languageRepo.findByCode("ua").orElseThrow(NoSuchElementException::new))); + languageRepo.findByCode(UA).orElseThrow(NoSuchElementException::new))); habitTranslationRepo.saveAll(habitTranslationListForUa); - List habitTranslationListForEn = mapHabitTranslationFromAddCustomHabitDtoRequest(habitDto); + List habitTranslationListForEn = + mapHabitTranslationFromAddCustomHabitDtoRequest(habitDto, EN); habitTranslationListForEn.forEach(habitTranslation -> habitTranslation.setHabit(habit)); habitTranslationListForEn.forEach(habitTranslation -> habitTranslation.setLanguage( - languageRepo.findByCode("en").orElseThrow(NoSuchElementException::new))); + languageRepo.findByCode(EN).orElseThrow(NoSuchElementException::new))); habitTranslationRepo.saveAll(habitTranslationListForEn); } - private List mapHabitTranslationFromAddCustomHabitDtoRequest(CustomHabitDtoRequest habitDto) { - return habitTranslationMapper.mapAllToList(habitDto.getHabitTranslations()); + private List mapHabitTranslationFromAddCustomHabitDtoRequest(CustomHabitDtoRequest habitDto, + String language) { + return habitTranslationMapper.mapAllToList(habitDto.getHabitTranslations(), language); } private void setTagsIdsToHabit(CustomHabitDtoRequest habitDto, Habit habit) { diff --git a/service/src/test/java/greencity/ModelUtils.java b/service/src/test/java/greencity/ModelUtils.java index 46541de3c3..8ceeb13896 100644 --- a/service/src/test/java/greencity/ModelUtils.java +++ b/service/src/test/java/greencity/ModelUtils.java @@ -276,9 +276,12 @@ public class ModelUtils { public static ZonedDateTime zonedDateTime = ZonedDateTime.now(); public static LocalDateTime localDateTime = LocalDateTime.now(); public static String habitTranslationName = "use shopper"; + public static String habitTranslationNameUa = "Назва звички українською"; public static String habitTranslationDescription = "Description"; + public static String habitTranslationDescriptionUa = "Опис звички українською"; public static String toDoListText = "buy a shopper"; public static String habitItem = "Item"; + public static String habitItemUa = "Айтем звички українською"; public static String habitDefaultImage = "img/habit-default.png"; public static AddEventDtoRequest addEventDtoRequest = AddEventDtoRequest.builder() .datesLocations(List.of(EventDateLocationDto.builder() @@ -2577,6 +2580,18 @@ public static HabitTranslationDto getHabitTranslationDto() { .build(); } + public static HabitTranslationDto getHabitTranslationDtoEnAndUa() { + return HabitTranslationDto.builder() + .description(habitTranslationDescription) + .habitItem(habitItem) + .name(habitTranslationName) + .languageCode("en") + .nameUa(habitTranslationNameUa) + .descriptionUa(habitTranslationDescriptionUa) + .habitItemUa(habitItemUa) + .build(); + } + public static HabitTranslation getHabitTranslationForServiceTest() { return HabitTranslation.builder() .id(1L) diff --git a/service/src/test/java/greencity/mapping/HabitTranslationMapperTests.java b/service/src/test/java/greencity/mapping/HabitTranslationMapperTests.java index f0b2ba0eb3..d1b0bb8b1b 100644 --- a/service/src/test/java/greencity/mapping/HabitTranslationMapperTests.java +++ b/service/src/test/java/greencity/mapping/HabitTranslationMapperTests.java @@ -14,6 +14,8 @@ @ExtendWith(MockitoExtension.class) class HabitTranslationMapperTests { + private final String UA = "ua"; + private final String EN = "en"; @InjectMocks private HabitTranslationMapper habitTranslationMapper; @@ -29,6 +31,18 @@ void convertTest() { assertEquals(expected, habitTranslationMapper.convert(habitTranslationDto)); } + @Test + void convertUaWithValidHabitTranslationDtoSucceeds() { + HabitTranslationDto habitTranslationDto = ModelUtils.getHabitTranslationDtoEnAndUa(); + + HabitTranslation expected = HabitTranslation.builder() + .description(habitTranslationDto.getDescriptionUa()) + .habitItem(habitTranslationDto.getHabitItemUa()) + .name(habitTranslationDto.getNameUa()) + .build(); + assertEquals(expected, habitTranslationMapper.convertUa(habitTranslationDto)); + } + @Test void mapAllToListTest() { HabitTranslationDto habitTranslationDto = ModelUtils.getHabitTranslationDto(); @@ -42,4 +56,34 @@ void mapAllToListTest() { List expectedList = List.of(expected); assertEquals(expectedList, habitTranslationMapper.mapAllToList(habitTranslationDtoList)); } + + @Test + void mapAllToListWithEnLanguageReturnsList() { + HabitTranslationDto habitTranslationDto = ModelUtils.getHabitTranslationDtoEnAndUa(); + List habitTranslationDtoList = List.of(habitTranslationDto); + + HabitTranslation expected = HabitTranslation.builder() + .description(habitTranslationDto.getDescription()) + .habitItem(habitTranslationDto.getHabitItem()) + .name(habitTranslationDto.getName()) + .build(); + List expectedList = List.of(expected); + + assertEquals(expectedList, habitTranslationMapper.mapAllToList(habitTranslationDtoList, EN)); + } + + @Test + void mapAllToListWithUaLanguageReturnsList() { + HabitTranslationDto habitTranslationDto = ModelUtils.getHabitTranslationDtoEnAndUa(); + List habitTranslationDtoList = List.of(habitTranslationDto); + + HabitTranslation expected = HabitTranslation.builder() + .description(habitTranslationDto.getDescriptionUa()) + .habitItem(habitTranslationDto.getHabitItemUa()) + .name(habitTranslationDto.getNameUa()) + .build(); + List expectedList = List.of(expected); + + assertEquals(expectedList, habitTranslationMapper.mapAllToList(habitTranslationDtoList, UA)); + } } diff --git a/service/src/test/java/greencity/service/HabitServiceImplTest.java b/service/src/test/java/greencity/service/HabitServiceImplTest.java index 9cf143597e..ab2da79927 100644 --- a/service/src/test/java/greencity/service/HabitServiceImplTest.java +++ b/service/src/test/java/greencity/service/HabitServiceImplTest.java @@ -619,7 +619,9 @@ void addCustomHabitTestWithImagePathInDto() throws IOException { when(userRepo.findByEmail(user.getEmail())).thenReturn(Optional.of(user)); when(habitRepo.save(customHabitMapper.convert(addCustomHabitDtoRequest))).thenReturn(habit); when(tagsRepo.findById(20L)).thenReturn(Optional.of(tag)); - when(habitTranslationMapper.mapAllToList(List.of(habitTranslationDto))) + when(habitTranslationMapper.mapAllToList(List.of(habitTranslationDto), "ua")) + .thenReturn(List.of(habitTranslationUa)); + when(habitTranslationMapper.mapAllToList(List.of(habitTranslationDto), "en")) .thenReturn(List.of(habitTranslationUa)); when(languageRepo.findByCode("ua")).thenReturn(Optional.of(languageUa)); when(languageRepo.findByCode("en")).thenReturn(Optional.of(languageEn)); @@ -641,7 +643,8 @@ void addCustomHabitTestWithImagePathInDto() throws IOException { verify(habitRepo).save(customHabitMapper.convert(addCustomHabitDtoRequest)); verify(customHabitMapper, times(3)).convert(addCustomHabitDtoRequest); verify(tagsRepo).findById(20L); - verify(habitTranslationMapper, times(2)).mapAllToList(List.of(habitTranslationDto)); + verify(habitTranslationMapper, times(1)).mapAllToList(List.of(habitTranslationDto), "ua"); + verify(habitTranslationMapper, times(1)).mapAllToList(List.of(habitTranslationDto), "en"); verify(languageRepo, times(2)).findByCode(anyString()); verify(customToDoListItemRepo).findAllByUserIdAndHabitId(1L, 1L); verify(customToDoListMapper).mapAllToList(anyList()); @@ -692,8 +695,10 @@ void addCustomHabitTestWithImageFile() throws IOException { when(languageRepo.findByCode("ua")).thenReturn(Optional.of(languageUa)); when(languageRepo.findByCode("en")).thenReturn(Optional.of(languageEn)); when(customToDoListItemRepo.findAllByUserIdAndHabitId(1L, 1L)).thenReturn(List.of(customToDoListItem)); - when(customToDoListMapper.mapAllToList(List.of(customToDoListItemResponseDto))) - .thenReturn(List.of(customToDoListItem)); + when(habitTranslationMapper.mapAllToList(List.of(habitTranslationDto), "ua")) + .thenReturn(List.of(habitTranslationUa)); + when(habitTranslationMapper.mapAllToList(List.of(habitTranslationDto), "en")) + .thenReturn(List.of(habitTranslationUa)); when(modelMapper.map(habit, CustomHabitDtoResponse.class)).thenReturn(addCustomHabitDtoResponse); when(customToDoListResponseDtoMapper.mapAllToList(List.of(customToDoListItem))) .thenReturn(List.of(customToDoListItemResponseDto)); @@ -709,7 +714,8 @@ void addCustomHabitTestWithImageFile() throws IOException { verify(habitRepo).save(customHabitMapper.convert(addCustomHabitDtoRequest)); verify(customHabitMapper, times(3)).convert(addCustomHabitDtoRequest); verify(tagsRepo).findById(20L); - verify(habitTranslationMapper, times(2)).mapAllToList(List.of(habitTranslationDto)); + verify(habitTranslationMapper, times(1)).mapAllToList(List.of(habitTranslationDto), "ua"); + verify(habitTranslationMapper, times(1)).mapAllToList(List.of(habitTranslationDto), "en"); verify(languageRepo, times(2)).findByCode(anyString()); verify(customToDoListItemRepo).findAllByUserIdAndHabitId(1L, 1L); verify(customToDoListMapper).mapAllToList(anyList()); @@ -755,7 +761,9 @@ void addCustomHabitTest2() throws IOException { when(userRepo.findByEmail(user.getEmail())).thenReturn(Optional.of(user)); when(habitRepo.save(customHabitMapper.convert(addCustomHabitDtoRequest))).thenReturn(habit); when(tagsRepo.findById(20L)).thenReturn(Optional.of(tag)); - when(habitTranslationMapper.mapAllToList(List.of(habitTranslationDto))) + when(habitTranslationMapper.mapAllToList(List.of(habitTranslationDto), "ua")) + .thenReturn(List.of(habitTranslationUa)); + when(habitTranslationMapper.mapAllToList(List.of(habitTranslationDto), "en")) .thenReturn(List.of(habitTranslationUa)); when(languageRepo.findByCode("ua")).thenReturn(Optional.of(languageUa)); when(languageRepo.findByCode("en")).thenReturn(Optional.of(languageEn)); @@ -777,7 +785,8 @@ void addCustomHabitTest2() throws IOException { verify(habitRepo).save(customHabitMapper.convert(addCustomHabitDtoRequest)); verify(customHabitMapper, times(3)).convert(addCustomHabitDtoRequest); verify(tagsRepo).findById(20L); - verify(habitTranslationMapper, times(2)).mapAllToList(List.of(habitTranslationDto)); + verify(habitTranslationMapper, times(1)).mapAllToList(List.of(habitTranslationDto), "ua"); + verify(habitTranslationMapper, times(1)).mapAllToList(List.of(habitTranslationDto), "en"); verify(languageRepo, times(2)).findByCode(anyString()); verify(customToDoListItemRepo).findAllByUserIdAndHabitId(1L, 1L); verify(customToDoListMapper).mapAllToList(anyList()); @@ -807,7 +816,9 @@ void addCustomHabitNoSuchElementExceptionWithNotExistingLanguageCodeTestUa() thr when(userRepo.findByEmail(user.getEmail())).thenReturn(Optional.of(user)); when(habitRepo.save(customHabitMapper.convert(addCustomHabitDtoRequest))).thenReturn(habit); when(tagsRepo.findById(20L)).thenReturn(Optional.of(tag)); - when(habitTranslationMapper.mapAllToList(List.of(habitTranslationDto))) + when(habitTranslationMapper.mapAllToList(addCustomHabitDtoRequest.getHabitTranslations(), "ua")) + .thenReturn(List.of(habitTranslation)); + when(habitTranslationMapper.mapAllToList(addCustomHabitDtoRequest.getHabitTranslations(), "en")) .thenReturn(List.of(habitTranslation)); when(languageRepo.findByCode("ua")).thenReturn(Optional.empty()); @@ -818,7 +829,8 @@ void addCustomHabitNoSuchElementExceptionWithNotExistingLanguageCodeTestUa() thr verify(habitRepo).save(customHabitMapper.convert(addCustomHabitDtoRequest)); verify(customHabitMapper, times(3)).convert(addCustomHabitDtoRequest); verify(tagsRepo).findById(20L); - verify(habitTranslationMapper).mapAllToList(addCustomHabitDtoRequest.getHabitTranslations()); + verify(habitTranslationMapper, times(1)).mapAllToList(List.of(habitTranslationDto), "ua"); + verify(habitTranslationMapper, times(0)).mapAllToList(List.of(habitTranslationDto), "en"); verify(languageRepo).findByCode(anyString()); } @@ -842,7 +854,9 @@ void addCustomHabitNoSuchElementExceptionWithNotExistingLanguageCodeEn() throws when(userRepo.findByEmail(user.getEmail())).thenReturn(Optional.of(user)); when(habitRepo.save(customHabitMapper.convert(addCustomHabitDtoRequest))).thenReturn(habit); when(tagsRepo.findById(20L)).thenReturn(Optional.of(tag)); - when(habitTranslationMapper.mapAllToList(List.of(habitTranslationDto))) + when(habitTranslationMapper.mapAllToList(addCustomHabitDtoRequest.getHabitTranslations(), "ua")) + .thenReturn(List.of(habitTranslationUa)); + when(habitTranslationMapper.mapAllToList(addCustomHabitDtoRequest.getHabitTranslations(), "en")) .thenReturn(List.of(habitTranslationUa)); when(languageRepo.findByCode("ua")).thenReturn(Optional.of(languageUa)); when(languageRepo.findByCode("en")).thenReturn(Optional.empty()); @@ -855,7 +869,8 @@ void addCustomHabitNoSuchElementExceptionWithNotExistingLanguageCodeEn() throws verify(customHabitMapper, times(3)).convert(addCustomHabitDtoRequest); verify(tagsRepo).findById(20L); - verify(habitTranslationMapper, times(2)).mapAllToList(addCustomHabitDtoRequest.getHabitTranslations()); + verify(habitTranslationMapper, times(1)).mapAllToList(List.of(habitTranslationDto), "ua"); + verify(habitTranslationMapper, times(1)).mapAllToList(List.of(habitTranslationDto), "en"); verify(languageRepo, times(2)).findByCode(anyString()); } From 54325f23b9af4ac25bf0315f0d31cf4fcfa50d7d Mon Sep 17 00:00:00 2001 From: Chernenko Vitaliy Date: Mon, 9 Dec 2024 16:25:03 +0200 Subject: [PATCH 2/4] Fixed sonar issues in HabitTranslationMapperTests; --- .../java/greencity/mapping/HabitTranslationMapperTests.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/service/src/test/java/greencity/mapping/HabitTranslationMapperTests.java b/service/src/test/java/greencity/mapping/HabitTranslationMapperTests.java index d1b0bb8b1b..9d241dfa48 100644 --- a/service/src/test/java/greencity/mapping/HabitTranslationMapperTests.java +++ b/service/src/test/java/greencity/mapping/HabitTranslationMapperTests.java @@ -14,8 +14,6 @@ @ExtendWith(MockitoExtension.class) class HabitTranslationMapperTests { - private final String UA = "ua"; - private final String EN = "en"; @InjectMocks private HabitTranslationMapper habitTranslationMapper; @@ -69,7 +67,7 @@ void mapAllToListWithEnLanguageReturnsList() { .build(); List expectedList = List.of(expected); - assertEquals(expectedList, habitTranslationMapper.mapAllToList(habitTranslationDtoList, EN)); + assertEquals(expectedList, habitTranslationMapper.mapAllToList(habitTranslationDtoList, "en")); } @Test @@ -84,6 +82,6 @@ void mapAllToListWithUaLanguageReturnsList() { .build(); List expectedList = List.of(expected); - assertEquals(expectedList, habitTranslationMapper.mapAllToList(habitTranslationDtoList, UA)); + assertEquals(expectedList, habitTranslationMapper.mapAllToList(habitTranslationDtoList, "ua")); } } From 343e565329235b2b10b42e284ada29e76099b1d8 Mon Sep 17 00:00:00 2001 From: Chernenko Vitaliy Date: Wed, 11 Dec 2024 15:51:35 +0200 Subject: [PATCH 3/4] Moved strings for language codes in AppConstant; Replced if/else to ObjectUtils.defaultNull; Added test for HabitTranslationMapperTests to improve coverage; --- .../java/greencity/constant/AppConstant.java | 1 + .../mapping/HabitTranslationMapper.java | 25 +++++++---------- .../greencity/service/HabitServiceImpl.java | 10 +++---- .../mapping/HabitTranslationMapperTests.java | 27 ++++++++++++++----- 4 files changed, 35 insertions(+), 28 deletions(-) diff --git a/service-api/src/main/java/greencity/constant/AppConstant.java b/service-api/src/main/java/greencity/constant/AppConstant.java index 9280d26f70..c4311afc76 100644 --- a/service-api/src/main/java/greencity/constant/AppConstant.java +++ b/service-api/src/main/java/greencity/constant/AppConstant.java @@ -14,6 +14,7 @@ public class AppConstant { public static final String AUTHORIZATION = "Authorization"; public static final String ROLE = "role"; public static final String DEFAULT_LANGUAGE_CODE = "en"; + public static final String LANGUAGE_CODE_UA = "ua"; public static final String DEFAULT_SOCIAL_NETWORK_IMAGE_HOST_PATH = "img/default_social_network_icon.png"; public static final Integer MAX_NUMBER_OF_HABIT_ASSIGNS_FOR_USER = 6; public static final int MIN_DAYS_DURATION = 7; diff --git a/service/src/main/java/greencity/mapping/HabitTranslationMapper.java b/service/src/main/java/greencity/mapping/HabitTranslationMapper.java index 336da9181b..73e2a7d972 100644 --- a/service/src/main/java/greencity/mapping/HabitTranslationMapper.java +++ b/service/src/main/java/greencity/mapping/HabitTranslationMapper.java @@ -1,7 +1,9 @@ package greencity.mapping; +import greencity.constant.AppConstant; import greencity.dto.habittranslation.HabitTranslationDto; import greencity.entity.HabitTranslation; +import org.apache.commons.lang3.ObjectUtils; import org.modelmapper.AbstractConverter; import org.springframework.stereotype.Component; import java.util.List; @@ -30,21 +32,12 @@ protected HabitTranslation convert(HabitTranslationDto habitTranslationDto) { */ public HabitTranslation convertUa(HabitTranslationDto habitTranslationDto) { HabitTranslation habitTranslation = new HabitTranslation(); - if (habitTranslationDto.getNameUa() == null) { - habitTranslation.setName(habitTranslationDto.getName()); - } else { - habitTranslation.setName(habitTranslationDto.getNameUa()); - } - if (habitTranslationDto.getDescriptionUa() == null) { - habitTranslation.setDescription(habitTranslationDto.getDescription()); - } else { - habitTranslation.setDescription(habitTranslationDto.getDescriptionUa()); - } - if (habitTranslationDto.getHabitItemUa() == null) { - habitTranslation.setHabitItem(habitTranslationDto.getHabitItem()); - } else { - habitTranslation.setHabitItem(habitTranslationDto.getHabitItemUa()); - } + habitTranslation + .setName(ObjectUtils.defaultIfNull(habitTranslationDto.getNameUa(), habitTranslationDto.getName())); + habitTranslation.setDescription( + ObjectUtils.defaultIfNull(habitTranslationDto.getDescriptionUa(), habitTranslationDto.getDescription())); + habitTranslation.setHabitItem( + ObjectUtils.defaultIfNull(habitTranslationDto.getHabitItemUa(), habitTranslationDto.getHabitItem())); return habitTranslation; } @@ -73,7 +66,7 @@ public List mapAllToList(List dtoList) { * @author Chernenko Vitaliy */ public List mapAllToList(List dtoList, String language) { - if (language.equals("ua")) { + if (AppConstant.LANGUAGE_CODE_UA.equals(language)) { return dtoList.stream().map(this::convertUa).collect(Collectors.toList()); } return dtoList.stream().map(this::convert).collect(Collectors.toList()); diff --git a/service/src/main/java/greencity/service/HabitServiceImpl.java b/service/src/main/java/greencity/service/HabitServiceImpl.java index fbd4057c66..fa34149b6f 100644 --- a/service/src/main/java/greencity/service/HabitServiceImpl.java +++ b/service/src/main/java/greencity/service/HabitServiceImpl.java @@ -491,20 +491,18 @@ private void updateHabitTranslationsForCustomHabit(CustomHabitDtoRequest habitDt } private void saveHabitTranslationListsToHabitTranslationRepo(CustomHabitDtoRequest habitDto, Habit habit) { - final String UA = "ua"; - final String EN = "en"; List habitTranslationListForUa = - mapHabitTranslationFromAddCustomHabitDtoRequest(habitDto, UA); + mapHabitTranslationFromAddCustomHabitDtoRequest(habitDto, AppConstant.LANGUAGE_CODE_UA); habitTranslationListForUa.forEach(habitTranslation -> habitTranslation.setHabit(habit)); habitTranslationListForUa.forEach(habitTranslation -> habitTranslation.setLanguage( - languageRepo.findByCode(UA).orElseThrow(NoSuchElementException::new))); + languageRepo.findByCode(AppConstant.LANGUAGE_CODE_UA).orElseThrow(NoSuchElementException::new))); habitTranslationRepo.saveAll(habitTranslationListForUa); List habitTranslationListForEn = - mapHabitTranslationFromAddCustomHabitDtoRequest(habitDto, EN); + mapHabitTranslationFromAddCustomHabitDtoRequest(habitDto, AppConstant.DEFAULT_LANGUAGE_CODE); habitTranslationListForEn.forEach(habitTranslation -> habitTranslation.setHabit(habit)); habitTranslationListForEn.forEach(habitTranslation -> habitTranslation.setLanguage( - languageRepo.findByCode(EN).orElseThrow(NoSuchElementException::new))); + languageRepo.findByCode(AppConstant.DEFAULT_LANGUAGE_CODE).orElseThrow(NoSuchElementException::new))); habitTranslationRepo.saveAll(habitTranslationListForEn); } diff --git a/service/src/test/java/greencity/mapping/HabitTranslationMapperTests.java b/service/src/test/java/greencity/mapping/HabitTranslationMapperTests.java index 9d241dfa48..925a65f6a7 100644 --- a/service/src/test/java/greencity/mapping/HabitTranslationMapperTests.java +++ b/service/src/test/java/greencity/mapping/HabitTranslationMapperTests.java @@ -1,13 +1,13 @@ package greencity.mapping; import greencity.ModelUtils; +import greencity.constant.AppConstant; import greencity.dto.habittranslation.HabitTranslationDto; import greencity.entity.HabitTranslation; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.junit.jupiter.MockitoExtension; - import java.util.List; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -30,7 +30,7 @@ void convertTest() { } @Test - void convertUaWithValidHabitTranslationDtoSucceeds() { + void convertUaWithValidHabitTranslationDtoSucceedsTest() { HabitTranslationDto habitTranslationDto = ModelUtils.getHabitTranslationDtoEnAndUa(); HabitTranslation expected = HabitTranslation.builder() @@ -56,7 +56,7 @@ void mapAllToListTest() { } @Test - void mapAllToListWithEnLanguageReturnsList() { + void mapAllToListWithEnLanguageReturnsListTest() { HabitTranslationDto habitTranslationDto = ModelUtils.getHabitTranslationDtoEnAndUa(); List habitTranslationDtoList = List.of(habitTranslationDto); @@ -67,11 +67,11 @@ void mapAllToListWithEnLanguageReturnsList() { .build(); List expectedList = List.of(expected); - assertEquals(expectedList, habitTranslationMapper.mapAllToList(habitTranslationDtoList, "en")); + assertEquals(expectedList, habitTranslationMapper.mapAllToList(habitTranslationDtoList, AppConstant.DEFAULT_LANGUAGE_CODE)); } @Test - void mapAllToListWithUaLanguageReturnsList() { + void mapAllToListWithUaLanguageReturnsListTest() { HabitTranslationDto habitTranslationDto = ModelUtils.getHabitTranslationDtoEnAndUa(); List habitTranslationDtoList = List.of(habitTranslationDto); @@ -82,6 +82,21 @@ void mapAllToListWithUaLanguageReturnsList() { .build(); List expectedList = List.of(expected); - assertEquals(expectedList, habitTranslationMapper.mapAllToList(habitTranslationDtoList, "ua")); + assertEquals(expectedList, habitTranslationMapper.mapAllToList(habitTranslationDtoList, AppConstant.LANGUAGE_CODE_UA)); + } + + @Test + void mapAllToListWithUaCodeButEmptyUaFieldsReturnListTest() { + HabitTranslationDto habitTranslationDto = ModelUtils.getHabitTranslationDto(); + List habitTranslationDtoList = List.of(habitTranslationDto); + + HabitTranslation expected = HabitTranslation.builder() + .description(habitTranslationDto.getDescription()) + .habitItem(habitTranslationDto.getHabitItem()) + .name(habitTranslationDto.getName()) + .build(); + List expectedList = List.of(expected); + + assertEquals(expectedList, habitTranslationMapper.mapAllToList(habitTranslationDtoList, AppConstant.LANGUAGE_CODE_UA)); } } From 1dc3b17fbf421467a6afad70c4acb6a144e789c3 Mon Sep 17 00:00:00 2001 From: Chernenko Vitaliy Date: Wed, 11 Dec 2024 16:03:52 +0200 Subject: [PATCH 4/4] Formatted code in HabitTranslationMapperTests with formatter-maven-plagin; --- .../mapping/HabitTranslationMapperTests.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/service/src/test/java/greencity/mapping/HabitTranslationMapperTests.java b/service/src/test/java/greencity/mapping/HabitTranslationMapperTests.java index 925a65f6a7..e76875cac2 100644 --- a/service/src/test/java/greencity/mapping/HabitTranslationMapperTests.java +++ b/service/src/test/java/greencity/mapping/HabitTranslationMapperTests.java @@ -67,7 +67,8 @@ void mapAllToListWithEnLanguageReturnsListTest() { .build(); List expectedList = List.of(expected); - assertEquals(expectedList, habitTranslationMapper.mapAllToList(habitTranslationDtoList, AppConstant.DEFAULT_LANGUAGE_CODE)); + assertEquals(expectedList, + habitTranslationMapper.mapAllToList(habitTranslationDtoList, AppConstant.DEFAULT_LANGUAGE_CODE)); } @Test @@ -82,7 +83,8 @@ void mapAllToListWithUaLanguageReturnsListTest() { .build(); List expectedList = List.of(expected); - assertEquals(expectedList, habitTranslationMapper.mapAllToList(habitTranslationDtoList, AppConstant.LANGUAGE_CODE_UA)); + assertEquals(expectedList, + habitTranslationMapper.mapAllToList(habitTranslationDtoList, AppConstant.LANGUAGE_CODE_UA)); } @Test @@ -91,12 +93,13 @@ void mapAllToListWithUaCodeButEmptyUaFieldsReturnListTest() { List habitTranslationDtoList = List.of(habitTranslationDto); HabitTranslation expected = HabitTranslation.builder() - .description(habitTranslationDto.getDescription()) - .habitItem(habitTranslationDto.getHabitItem()) - .name(habitTranslationDto.getName()) - .build(); + .description(habitTranslationDto.getDescription()) + .habitItem(habitTranslationDto.getHabitItem()) + .name(habitTranslationDto.getName()) + .build(); List expectedList = List.of(expected); - assertEquals(expectedList, habitTranslationMapper.mapAllToList(habitTranslationDtoList, AppConstant.LANGUAGE_CODE_UA)); + assertEquals(expectedList, + habitTranslationMapper.mapAllToList(habitTranslationDtoList, AppConstant.LANGUAGE_CODE_UA)); } }