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

Fix bug update score #6420

Merged
merged 16 commits into from
Oct 4, 2023
28 changes: 0 additions & 28 deletions service-api/src/main/java/greencity/client/RestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -505,34 +505,6 @@ public void sendHabitNotification(SendHabitNotification sendHabitNotification) {
.getBody();
}

/**
* Method that allow you to save new {@link UserVO}.
*
* @param userVO for save User.
* @author Orest Mamchuk
*/
public void save(UserVO userVO) {
HttpEntity<UserVO> entity = new HttpEntity<>(userVO, setHeader());
restTemplate.exchange(greenCityUserServerAddress
+ RestTemplateLinks.USER, HttpMethod.POST, entity, Object.class)
.getBody();
}

/**
* Method that allow you to save new {@link UserVO}.
*
* @param userVO for save User.
* @author Orest Mamchuk
*/
public void save(UserVO userVO, String accessToken) {
HttpHeaders headers = new HttpHeaders();
headers.set(AUTHORIZATION, accessToken);
HttpEntity<UserVO> entity = new HttpEntity<>(userVO, headers);
restTemplate.exchange(greenCityUserServerAddress
+ RestTemplateLinks.USER, HttpMethod.POST, entity, Object.class)
.getBody();
}

/**
* Method that allow you to search users by several values
* {@link UserManagementViewDto}.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package greencity.constant;

public final class ErrorMessage {
public static final String RATING_CALCULATION_ENUM_NOT_FOUND_BY_NAME =
"RatingCalculationEnum with such name does not exist: ";
public static final String CUSTOM_SHOPPING_LIST_ITEM_NOT_FOUND_BY_ID =
"Custom shopping list item with such id does not exist.";
public static final String DUPLICATED_CUSTOM_SHOPPING_LIST_ITEM = "CustomShoppingListItems should be unique";
Expand Down
10 changes: 10 additions & 0 deletions service-api/src/main/java/greencity/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
import java.util.Optional;

public interface UserService {
/**
* Saves the provided UserVO object into the repository after mapping it to a
* User entity. This method uses ModelMapper to convert the UserVO object into a
* User entity and then saves it using the User repository.
*
* @param userVO The user value object containing the user details to be saved.
* @see UserVO
*/
void save(UserVO userVO);

/**
* Method that allow you to find not 'DEACTIVATED' {@link UserVO} by email.
*
Expand Down
32 changes: 0 additions & 32 deletions service-api/src/test/java/greencity/client/RestClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -452,38 +452,6 @@ void sendHabitNotification() {
+ RestTemplateLinks.SEND_HABIT_NOTIFICATION, HttpMethod.POST, entity, Object.class);
}

@Test
void save() {
UserVO userVO = ModelUtils.getUserVO();
String accessToken = "accessToken";
HttpHeaders headers = new HttpHeaders();
headers.set(AUTHORIZATION, accessToken);
HttpEntity<UserVO> entity = new HttpEntity<>(userVO, headers);
when(httpServletRequest.getHeader(AUTHORIZATION)).thenReturn(accessToken);
when(restTemplate.exchange(GREEN_CITY_USER_ADDRESS
+ RestTemplateLinks.USER, HttpMethod.POST, entity, Object.class)).thenReturn(ResponseEntity.ok(Object));
restClient.save(userVO);

verify(restTemplate).exchange(GREEN_CITY_USER_ADDRESS
+ RestTemplateLinks.USER, HttpMethod.POST, entity, Object.class);
}

@Test
void saveTest() {
UserVO userVO = ModelUtils.getUserVO();
String accessToken = "accessToken";
HttpHeaders headers = new HttpHeaders();
headers.set(AUTHORIZATION, accessToken);
HttpEntity<UserVO> entity = new HttpEntity<>(userVO, headers);
when(restTemplate.exchange(GREEN_CITY_USER_ADDRESS
+ RestTemplateLinks.USER, HttpMethod.POST, entity, Object.class)).thenReturn(ResponseEntity.ok(Object));
restClient.save(userVO, accessToken);

verify(restTemplate).exchange(GREEN_CITY_USER_ADDRESS
+ RestTemplateLinks.USER, HttpMethod.POST, entity, Object.class);

}

@Test
void findUserForManagementByPage() {
String accessToken = "accessToken";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package greencity.filters;

import greencity.constant.ErrorMessage;
import greencity.entity.RatingStatistics;
import greencity.entity.RatingStatistics_;
import greencity.entity.User;
import greencity.entity.User_;
import greencity.enums.RatingCalculationEnum;
import greencity.exception.exceptions.NotFoundException;
import lombok.NoArgsConstructor;
import javax.persistence.criteria.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@NoArgsConstructor
public class RatingStatisticsSpecification implements MySpecification<RatingStatistics> {
Expand Down Expand Up @@ -62,15 +64,14 @@ public Predicate toPredicate(Root<RatingStatistics> root, CriteriaQuery<?> crite
private Predicate getEventNamePredicate(Root<RatingStatistics> root, CriteriaBuilder criteriaBuilder,
SearchCriteria searchCriteria) {
List<RatingCalculationEnum> enumValues = Arrays.asList(RatingCalculationEnum.values());
List<RatingCalculationEnum> selectedEnums = enumValues.stream()
.filter(x -> x.toString().toLowerCase().contains(((String) searchCriteria.getValue()).toLowerCase()))
.collect(Collectors.toList());

RatingCalculationEnum ratingCalculationEnum = enumValues.stream()
.filter(x -> x.toString().equalsIgnoreCase(((String) searchCriteria.getValue())))
ospodaryk marked this conversation as resolved.
Show resolved Hide resolved
.findFirst().orElseThrow(() -> new NotFoundException(
ErrorMessage.RATING_CALCULATION_ENUM_NOT_FOUND_BY_NAME + searchCriteria.getValue()));
Predicate predicate = criteriaBuilder.disjunction();
for (RatingCalculationEnum ratingCalculationEnum : selectedEnums) {
predicate = criteriaBuilder.or(predicate,
criteriaBuilder.equal(root.get(searchCriteria.getKey()), ratingCalculationEnum));
}
predicate = criteriaBuilder.or(predicate,
criteriaBuilder.equal(root.get(searchCriteria.getKey()), ratingCalculationEnum));

return predicate;
}

Expand Down
12 changes: 3 additions & 9 deletions service/src/main/java/greencity/rating/RatingCalculation.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
package greencity.rating;

import greencity.client.RestClient;
import greencity.dto.ratingstatistics.RatingStatisticsVO;
import greencity.dto.user.UserVO;
import greencity.entity.RatingStatistics;
import greencity.entity.User;
import greencity.enums.RatingCalculationEnum;
import greencity.service.RatingStatisticsService;
import greencity.service.UserService;
import lombok.AllArgsConstructor;
import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

import static greencity.constant.AppConstant.AUTHORIZATION;

@Component
@AllArgsConstructor
public class RatingCalculation {
private RestClient restClient;
private RatingStatisticsService ratingStatisticsService;
private final ModelMapper modelMapper;
private final HttpServletRequest httpServletRequest;
private UserService userService;

/**
* Method that calculates the user rating.
Expand All @@ -30,10 +25,9 @@ public class RatingCalculation {
* @param userVo of {@link UserVO}
*/
public void ratingCalculation(RatingCalculationEnum rating, UserVO userVo) {
String accessToken = httpServletRequest.getHeader(AUTHORIZATION);
User user = modelMapper.map(userVo, User.class);
userVo.setRating(userVo.getRating() + rating.getRatingPoints());
restClient.save(userVo, accessToken);
ospodaryk marked this conversation as resolved.
Show resolved Hide resolved
userService.save(userVo);
ospodaryk marked this conversation as resolved.
Show resolved Hide resolved
RatingStatistics ratingStatistics = RatingStatistics
.builder()
.rating(userVo.getRating())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class AchievementServiceImpl implements AchievementService {
private final AchievementCategoryService achievementCategoryService;
private final UserActionService userActionService;
private AchievementCalculation achievementCalculation;
private final UserService userService;

/**
* {@inheritDoc}
Expand Down Expand Up @@ -75,7 +76,7 @@ public AchievementVO save(AchievementPostDto achievementPostDto) {
}
userVO.getUserAchievements().add(userAchievementVO);
userAchievementVO.setUser(userVO);
restClient.save(userVO);
userService.save(userVO);
ospodaryk marked this conversation as resolved.
Show resolved Hide resolved
});
return achievementVO;
}
Expand Down
8 changes: 8 additions & 0 deletions service/src/main/java/greencity/service/UserServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ public class UserServiceImpl implements UserService {
@Value("300000")
private long timeAfterLastActivity;

/**
* {@inheritDoc}
*/
@Override
public void save(UserVO userVO) {
userRepo.save(modelMapper.map(userVO, User.class));
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package greencity.filters;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;

import greencity.enums.RatingCalculationEnum;
import greencity.dto.ratingstatistics.RatingStatisticsViewDto;
import greencity.entity.RatingStatistics;
import greencity.entity.RatingStatistics_;
import greencity.entity.User;
import greencity.entity.User_;
import greencity.exception.exceptions.NotFoundException;

import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.criteria.*;
import javax.persistence.metamodel.SingularAttribute;
import org.junit.jupiter.api.BeforeEach;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
Expand Down Expand Up @@ -77,13 +81,17 @@ class RatingStatisticsSpecificationTest {

private RatingStatisticsSpecification ratingStatisticsSpecification;

private RatingStatisticsViewDto ratingStatisticsViewDto;

private List<SearchCriteria> criteriaList;

@BeforeEach
void setUp() {
RatingStatisticsViewDto ratingStatisticsViewDto =
new RatingStatisticsViewDto("2", "UNLIKE_COMMENT_OR_REPLY", "1", "", "2021-01-12", "2021-01-13", "", "50");
void initRatingStatisticsViewDto(String id, String eventName, String userId, String userEmail, String startDate,
String endDate, String pointsChanged, String curentRating) {
ratingStatisticsViewDto = new RatingStatisticsViewDto(id, eventName, userId, userEmail, startDate, endDate,
pointsChanged, curentRating);
}

void init() {
criteriaList = new ArrayList<>();
criteriaList.add(
SearchCriteria.builder()
Expand Down Expand Up @@ -124,7 +132,8 @@ void setUp() {

@Test
void toPredicate() {

initRatingStatisticsViewDto("2", "UNLIKE_COMMENT_OR_REPLY", "1", "", "2021-01-12", "2021-01-13", "", "50");
init();
when(criteriaBuilderMock.conjunction()).thenReturn(predicateMock);

when(ratingStatisticsRootMock.get("id")).thenReturn(pathRatingStatisticsIdMock);
Expand Down Expand Up @@ -180,4 +189,26 @@ void toPredicate() {
verify(criteriaBuilderMock).and(andDataRangePredicate, andCurrentRatingPredicate);

}

@Test
void toPredicate_exceptionTest() {
initRatingStatisticsViewDto("2", "NOTREAL", "1", "", "2021-01-12", "2021-01-13", "", "50");
init();
ospodaryk marked this conversation as resolved.
Show resolved Hide resolved
when(criteriaBuilderMock.conjunction()).thenReturn(predicateMock);

when(ratingStatisticsRootMock.get("id")).thenReturn(pathRatingStatisticsIdMock);

when(criteriaBuilderMock.equal(pathRatingStatisticsIdMock, criteriaList.get(0).getValue()))
.thenReturn(andIdNumericPredicate);

when(criteriaBuilderMock.and(predicateMock, andIdNumericPredicate)).thenReturn(andIdNumericPredicate);
ospodaryk marked this conversation as resolved.
Show resolved Hide resolved

assertThrows(NotFoundException.class, () -> ratingStatisticsSpecification.toPredicate(ratingStatisticsRootMock,
criteriaQueryMock, criteriaBuilderMock));
verify(criteriaBuilderMock).and(predicateMock, andIdNumericPredicate);
verify(criteriaBuilderMock).equal(pathRatingStatisticsIdMock, criteriaList.get(0).getValue());
verify(ratingStatisticsRootMock).get("id");
verify(criteriaBuilderMock).conjunction();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import greencity.entity.User;
import greencity.service.RatingStatisticsService;
import java.time.ZonedDateTime;

import greencity.service.UserService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
Expand All @@ -22,17 +24,15 @@

@ExtendWith(MockitoExtension.class)
class RatingCalculationTest {
@Mock
private RestClient restClient;
@Mock
private RatingStatisticsService ratingStatisticsService;
@Mock
private ModelMapper modelMapper;
@Mock
private HttpServletRequest httpServletRequest;

@InjectMocks
private RatingCalculation ratingCalculation;
@Mock
private UserService userService;

@Test
void ratingCalculation() {
Expand Down Expand Up @@ -60,11 +60,11 @@ void ratingCalculation() {
.pointsChanged(rating.getRatingPoints())
.build();
when(modelMapper.map(userVO, User.class)).thenReturn(user);
doNothing().when(restClient).save(userVO, null);
ospodaryk marked this conversation as resolved.
Show resolved Hide resolved
doNothing().when(userService).save(userVO);
ospodaryk marked this conversation as resolved.
Show resolved Hide resolved
when(modelMapper.map(ratingStatistics, RatingStatisticsVO.class)).thenReturn(ratingStatisticsVO);
when(ratingStatisticsService.save(ratingStatisticsVO)).thenReturn(ratingStatisticsVO);
ratingCalculation.ratingCalculation(RatingCalculationEnum.COMMENT_OR_REPLY, userVO);
verify(ratingStatisticsService).save(ratingStatisticsVO);

verify(userService).save(userVO);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ void saveTest() {

AchievementVO expected = achievementService.save(achievementPostDto);
assertEquals(expected, achievementVO);
verify(userService).save(userVO);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ void findByIdTest() {
verify(userRepo, times(1)).findById(id);
}

@Test
void saveTest() {
userService.save(userVO);
verify(modelMapper).map(userVO, User.class);
verify(userRepo).save(any());
}

@Test
void checkIfTheUserIsOnlineExceptionTest() {
assertThrows(WrongIdException.class, () -> userService.checkIfTheUserIsOnline(null));
Expand Down