Skip to content

Commit

Permalink
made some update and added some test
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrieleMaiocchiFilippo committed Jun 14, 2024
1 parent 7983d21 commit 7a6a06b
Show file tree
Hide file tree
Showing 12 changed files with 200 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public User toEntityInsertionWithProfiles(UserInsertionWithProfilesDTO userInser
user.setUserId(userInsertionWithProfilesDTO.getUserId());
user.setName(userInsertionWithProfilesDTO.getName());
user.setSurname(userInsertionWithProfilesDTO.getSurname());
userInsertionWithProfilesDTO.getProfileIds().stream().forEach(x -> userProfilesList.add(new UserProfiles(new UserProfilesPK(user.getUserId(), x))));
userInsertionWithProfilesDTO.getProfileIds().forEach(x -> userProfilesList.add(new UserProfiles(new UserProfilesPK(user.getUserId(), x))));
user.setUserProfiles(userProfilesList);
return user;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

@ApplicationScoped
public class UserRepository implements PanacheRepositoryBase<User, String> {
public Uni<User> findById(String userId) {
return find("select u from User u left join fetch u.userProfiles where u.userId = :userId",
Parameters.with("userId", userId)).firstResult();
public Uni<User> findByIdCustom(String userId) {
return find("select u from User u left join fetch u.userProfiles where u.userId = :userId",
Parameters.with("userId", userId)).firstResult();
}

public PanacheQuery<User> findAll() {
public PanacheQuery<User> findAllCustom() {
return find("select u from User u left join fetch u.userProfiles");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public Uni<List<UserProfilesDTO>> update(@RequestBody(required = true) @Valid Us
@Produces(MediaType.APPLICATION_JSON)
public Uni<UserProfilesDTO> getById(@PathParam("userId") String userId,
@PathParam("profileId") int profileId) {
return this.userProfilesService.findById(userId, profileId)
return this.userProfilesService.getById(userId, profileId)
.onItem()
.transform(user -> userProfilesMapper.toDTO(user));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import it.gov.pagopa.atmlayer.service.model.dto.UserInsertionWithProfilesDTO;
import it.gov.pagopa.atmlayer.service.model.dto.UserWithProfilesDTO;
import it.gov.pagopa.atmlayer.service.model.mapper.UserMapper;
import it.gov.pagopa.atmlayer.service.model.repository.UserRepository;
import it.gov.pagopa.atmlayer.service.model.service.UserService;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
Expand All @@ -30,6 +31,9 @@ public class UserResource {
@Inject
UserService userService;

@Inject
UserRepository userRepository;

@POST
@Path("/insert")
@Consumes(MediaType.APPLICATION_JSON)
Expand Down Expand Up @@ -69,7 +73,7 @@ public Uni<UserWithProfilesDTO> update(@RequestBody(required = true) @Valid User
public Uni<UserWithProfilesDTO> insertWithProfiles(@RequestBody(required = true) @Valid UserInsertionWithProfilesDTO userInsertionWithProfilesDTO) {
return this.userService.insertUserWithProfiles(userInsertionWithProfilesDTO)
.onItem()
.transformToUni(insertedProfiles -> userService.findUser(userInsertionWithProfilesDTO.getUserId()))
.transformToUni(insertedProfiles -> userRepository.findByIdCustom(userInsertionWithProfilesDTO.getUserId()))
.onItem()
.transformToUni(insertedUser -> Uni.createFrom().item(this.userMapper.toProfilesDTO(insertedUser)));
}
Expand All @@ -90,7 +94,7 @@ public Uni<Void> delete(@PathParam("userId") String userId) {
@Path("/{userId}")
@Produces(MediaType.APPLICATION_JSON)
public Uni<UserWithProfilesDTO> getByIdWithProfiles(@PathParam("userId") String userId) {
return this.userService.findById(userId)
return this.userService.getById(userId)
.onItem()
.transform(foundUser -> userMapper.toProfilesDTO(foundUser));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface UserProfilesService {

Uni<List<UserProfiles>> insertUserProfiles(UserProfilesInsertionDTO userProfilesInsertionDTO);

Uni<UserProfiles> findById(String userId, int profileId);
Uni<UserProfiles> getById(String userId, int profileId);

Uni<Void> deleteUserProfiles(UserProfilesPK userProfilesIDs);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.smallrye.mutiny.Uni;
import it.gov.pagopa.atmlayer.service.model.dto.UserInsertionDTO;
import it.gov.pagopa.atmlayer.service.model.dto.UserInsertionWithProfilesDTO;
import it.gov.pagopa.atmlayer.service.model.dto.UserWithProfilesDTO;
import it.gov.pagopa.atmlayer.service.model.entity.User;

import java.util.List;
Expand All @@ -19,7 +20,7 @@ public interface UserService {

Uni<Boolean> deleteUser(String userId);

Uni<User> findById(String userId);
Uni<User> getById(String userId);

Uni<List<User>> getAllUsers();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,16 @@ protected Uni<Boolean> isUserProfileExisting(UserProfiles userProfiles) {

@Override
@WithSession
public Uni<UserProfiles> findById(String userId, int profileId) {
public Uni<UserProfiles> getById(String userId, int profileId) {
UserProfilesPK userProfilesPK = new UserProfilesPK(userId, profileId);
return userProfilesRepository.findById(userProfilesPK)
.onItem()
.transformToUni(userProfile -> Uni.createFrom().item(userProfile));
.transform(userProfile -> {
if (userProfile == null) {
throw new AtmLayerException(Response.Status.NOT_FOUND, AppErrorCodeEnum.NO_ASSOCIATION_FOUND);
}
return userProfile;
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
import io.smallrye.mutiny.unchecked.Unchecked;
import it.gov.pagopa.atmlayer.service.model.dto.UserInsertionDTO;
import it.gov.pagopa.atmlayer.service.model.dto.UserInsertionWithProfilesDTO;
import it.gov.pagopa.atmlayer.service.model.dto.UserWithProfilesDTO;
import it.gov.pagopa.atmlayer.service.model.entity.User;
import it.gov.pagopa.atmlayer.service.model.entity.UserProfiles;
import it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum;
import it.gov.pagopa.atmlayer.service.model.exception.AtmLayerException;
import it.gov.pagopa.atmlayer.service.model.mapper.UserMapper;
import it.gov.pagopa.atmlayer.service.model.repository.UserRepository;
import it.gov.pagopa.atmlayer.service.model.service.UserProfilesService;
import it.gov.pagopa.atmlayer.service.model.service.UserService;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
Expand Down Expand Up @@ -51,8 +52,8 @@ public Uni<User> insertUser(UserInsertionDTO userInsertionDTO) {

@Override
@WithSession
public Uni<User> findUser (String userId) {
return this.userRepository.findById(userId);
public Uni<User> findUser(String userId) {
return this.userRepository.findById(userId);
}

@Override
Expand All @@ -75,7 +76,7 @@ public Uni<User> insertUserWithProfiles(UserInsertionWithProfilesDTO userInserti
public Uni<User> updateUser(UserInsertionDTO userInsertionDTO) {
String userId = userInsertionDTO.getUserId();
log.info("Updating user with userId : {}", userId);
return this.findById(userInsertionDTO.getUserId())
return this.getById(userInsertionDTO.getUserId())
.onItem()
.transformToUni(Unchecked.function(userFound -> {
if (userInsertionDTO.getName().isBlank() && userInsertionDTO.getSurname().isBlank()) {
Expand All @@ -97,15 +98,15 @@ public Uni<User> updateUser(UserInsertionDTO userInsertionDTO) {
@WithTransaction
public Uni<Boolean> deleteUser(String userId) {
log.info("Deleting user with userId : {}", userId);
return this.findById(userId)
return this.getById(userId)
.onItem()
.transformToUni(x -> this.userRepository.deleteById(userId));
}

@Override
@WithSession
public Uni<List<User>> getAllUsers() {
return this.userRepository.findAll().list();
return this.userRepository.findAllCustom().list();
}

@Override
Expand All @@ -115,8 +116,8 @@ public Uni<Long> countUsers() {

@Override
@WithSession
public Uni<User> findById(String userId) {
return this.userRepository.findById(userId)
public Uni<User> getById(String userId) {
return this.userRepository.findByIdCustom(userId)
.onItem()
.ifNull()
.switchTo(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ void testGetById() {
int profileId = 1;
UserProfiles userProfiles = new UserProfiles();

when(userProfilesService.findById(userId, profileId)).thenReturn(
when(userProfilesService.getById(userId, profileId)).thenReturn(
Uni.createFrom().item(userProfiles));
UserProfilesDTO dto = new UserProfilesDTO();
when(userProfilesMapper.toDTO(userProfiles)).thenReturn(dto);
Expand All @@ -142,7 +142,7 @@ void testGetById() {
.then()
.statusCode(200);

verify(userProfilesService, times(1)).findById(userId, profileId);
verify(userProfilesService, times(1)).getById(userId, profileId);
verify(userProfilesMapper, times(1)).toDTO(userProfiles);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import it.gov.pagopa.atmlayer.service.model.entity.UserProfilesPK;
import it.gov.pagopa.atmlayer.service.model.mapper.UserMapper;
import it.gov.pagopa.atmlayer.service.model.model.ProfileDTO;
import it.gov.pagopa.atmlayer.service.model.repository.UserRepository;
import it.gov.pagopa.atmlayer.service.model.service.UserService;
import jakarta.ws.rs.core.MediaType;
import org.junit.jupiter.api.Test;
Expand All @@ -28,7 +29,8 @@ class UserResourceTest {

@InjectMock
UserMapper userMapper;

@InjectMock
UserRepository userRepository;
@InjectMock
UserService userService;

Expand Down Expand Up @@ -118,6 +120,9 @@ void testInsertWithProfiles() {
List<UserProfiles> userProfilesList = new ArrayList<>();
userProfilesList.add(userProfiles);

List<Integer> profileIds = new ArrayList<>();
profileIds.add(1);

user.setUserId("[email protected]");
user.setName("prova");
user.setSurname("test");
Expand All @@ -127,12 +132,12 @@ void testInsertWithProfiles() {
userInsertionWithProfilesDTO.setUserId("[email protected]");
userInsertionWithProfilesDTO.setName("prova");
userInsertionWithProfilesDTO.setSurname("test");
userInsertionWithProfilesDTO.setProfileIds(List.of(1));
userInsertionWithProfilesDTO.setProfileIds(profileIds);

UserWithProfilesDTO userWithProfilesDTO = new UserWithProfilesDTO();

when(userMapper.toEntityInsertionWithProfiles(userInsertionWithProfilesDTO)).thenReturn(user);
when(userService.findUser(userInsertionWithProfilesDTO.getUserId())).thenReturn(Uni.createFrom().item(user));
when(userService.insertUserWithProfiles(userInsertionWithProfilesDTO)).thenReturn(Uni.createFrom().item(user));
when(userRepository.findByIdCustom(userInsertionWithProfilesDTO.getUserId())).thenReturn(Uni.createFrom().item(user));
when(userMapper.toProfilesDTO(user)).thenReturn(userWithProfilesDTO);

UserWithProfilesDTO result = given()
Expand Down Expand Up @@ -213,7 +218,7 @@ void testGetByIdWithProfiles() {
User user = new User();
UserWithProfilesDTO userWithProfilesDTO = new UserWithProfilesDTO();

when(userService.findById(any(String.class))).thenReturn(Uni.createFrom().item(user));
when(userService.getById(any(String.class))).thenReturn(Uni.createFrom().item(user));
when(userMapper.toProfilesDTO(any(User.class))).thenReturn(userWithProfilesDTO);

UserWithProfilesDTO result = given()
Expand All @@ -226,7 +231,7 @@ void testGetByIdWithProfiles() {
.as(UserWithProfilesDTO.class);

assertEquals(userWithProfilesDTO, result);
verify(userService, times(1)).findById(any(String.class));
verify(userService, times(1)).getById(any(String.class));
verify(userMapper, times(1)).toProfilesDTO(user);
}

Expand Down
Loading

0 comments on commit 7a6a06b

Please sign in to comment.