Skip to content

Commit

Permalink
Bugfix #7319 - Fixed incorrect saving for HabitTranslation for ua fie…
Browse files Browse the repository at this point in the history
…lds. (#7898)

* 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;

* Fixed sonar issues in HabitTranslationMapperTests;

* Moved strings for language codes in AppConstant;
Replced if/else to ObjectUtils.defaultNull;
Added test for HabitTranslationMapperTests to improve coverage;

* Formatted code in HabitTranslationMapperTests with formatter-maven-plagin;
  • Loading branch information
ChernenkoVitaliy authored Dec 13, 2024
1 parent c4c7afd commit b5324f3
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,6 +31,7 @@ public class CustomHabitDtoRequest {
@NotNull(message = ServiceValidationConstants.HABIT_COMPLEXITY)
private Integer complexity;
private Integer defaultDuration;
@Valid
private List<HabitTranslationDto> habitTranslations;
private String image;
private List<CustomToDoListItemResponseDto> customToDoListItemDto;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,6 +20,28 @@ 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();
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;
}

/**
* Method that build {@link List} of {@link HabitTranslation} from {@link List}
* of {@link HabitTranslationDto}.
Expand All @@ -29,4 +53,22 @@ protected HabitTranslation convert(HabitTranslationDto habitTranslationDto) {
public List<HabitTranslation> mapAllToList(List<HabitTranslationDto> 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<HabitTranslation> mapAllToList(List<HabitTranslationDto> dtoList, String language) {
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());
}
}
15 changes: 9 additions & 6 deletions service/src/main/java/greencity/service/HabitServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -497,21 +497,24 @@ private void updateHabitTranslationsForCustomHabit(CustomHabitDtoRequest habitDt
}

private void saveHabitTranslationListsToHabitTranslationRepo(CustomHabitDtoRequest habitDto, Habit habit) {
List<HabitTranslation> habitTranslationListForUa = mapHabitTranslationFromAddCustomHabitDtoRequest(habitDto);
List<HabitTranslation> habitTranslationListForUa =
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<HabitTranslation> habitTranslationListForEn = mapHabitTranslationFromAddCustomHabitDtoRequest(habitDto);
List<HabitTranslation> habitTranslationListForEn =
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);
}

private List<HabitTranslation> mapHabitTranslationFromAddCustomHabitDtoRequest(CustomHabitDtoRequest habitDto) {
return habitTranslationMapper.mapAllToList(habitDto.getHabitTranslations());
private List<HabitTranslation> mapHabitTranslationFromAddCustomHabitDtoRequest(CustomHabitDtoRequest habitDto,
String language) {
return habitTranslationMapper.mapAllToList(habitDto.getHabitTranslations(), language);
}

private void setTagsIdsToHabit(CustomHabitDtoRequest habitDto, Habit habit) {
Expand Down
15 changes: 15 additions & 0 deletions service/src/test/java/greencity/ModelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -29,6 +29,18 @@ void convertTest() {
assertEquals(expected, habitTranslationMapper.convert(habitTranslationDto));
}

@Test
void convertUaWithValidHabitTranslationDtoSucceedsTest() {
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();
Expand All @@ -42,4 +54,52 @@ void mapAllToListTest() {
List<HabitTranslation> expectedList = List.of(expected);
assertEquals(expectedList, habitTranslationMapper.mapAllToList(habitTranslationDtoList));
}

@Test
void mapAllToListWithEnLanguageReturnsListTest() {
HabitTranslationDto habitTranslationDto = ModelUtils.getHabitTranslationDtoEnAndUa();
List<HabitTranslationDto> habitTranslationDtoList = List.of(habitTranslationDto);

HabitTranslation expected = HabitTranslation.builder()
.description(habitTranslationDto.getDescription())
.habitItem(habitTranslationDto.getHabitItem())
.name(habitTranslationDto.getName())
.build();
List<HabitTranslation> expectedList = List.of(expected);

assertEquals(expectedList,
habitTranslationMapper.mapAllToList(habitTranslationDtoList, AppConstant.DEFAULT_LANGUAGE_CODE));
}

@Test
void mapAllToListWithUaLanguageReturnsListTest() {
HabitTranslationDto habitTranslationDto = ModelUtils.getHabitTranslationDtoEnAndUa();
List<HabitTranslationDto> habitTranslationDtoList = List.of(habitTranslationDto);

HabitTranslation expected = HabitTranslation.builder()
.description(habitTranslationDto.getDescriptionUa())
.habitItem(habitTranslationDto.getHabitItemUa())
.name(habitTranslationDto.getNameUa())
.build();
List<HabitTranslation> expectedList = List.of(expected);

assertEquals(expectedList,
habitTranslationMapper.mapAllToList(habitTranslationDtoList, AppConstant.LANGUAGE_CODE_UA));
}

@Test
void mapAllToListWithUaCodeButEmptyUaFieldsReturnListTest() {
HabitTranslationDto habitTranslationDto = ModelUtils.getHabitTranslationDto();
List<HabitTranslationDto> habitTranslationDtoList = List.of(habitTranslationDto);

HabitTranslation expected = HabitTranslation.builder()
.description(habitTranslationDto.getDescription())
.habitItem(habitTranslationDto.getHabitItem())
.name(habitTranslationDto.getName())
.build();
List<HabitTranslation> expectedList = List.of(expected);

assertEquals(expectedList,
habitTranslationMapper.mapAllToList(habitTranslationDtoList, AppConstant.LANGUAGE_CODE_UA));
}
}
37 changes: 26 additions & 11 deletions service/src/test/java/greencity/service/HabitServiceImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,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));
Expand All @@ -655,7 +657,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());
Expand Down Expand Up @@ -706,8 +709,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));
Expand All @@ -723,7 +728,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());
Expand Down Expand Up @@ -769,7 +775,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));
Expand All @@ -791,7 +799,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());
Expand Down Expand Up @@ -821,7 +830,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());

Expand All @@ -832,7 +843,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());
}

Expand All @@ -856,7 +868,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());
Expand All @@ -869,7 +883,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());
}

Expand Down

0 comments on commit b5324f3

Please sign in to comment.