From fd5460292aa0c0f4c9d21b1e70257f4edaea37cf Mon Sep 17 00:00:00 2001 From: stefano Date: Mon, 29 Jan 2024 12:08:24 +0100 Subject: [PATCH 01/10] add new field "description" --- .../atmlayer/service/model/entity/ResourceEntity.java | 2 ++ .../pagopa/atmlayer/service/model/model/ResourceDTO.java | 1 + .../atmlayer/service/model/entity/ResourceEntityTest.java | 8 ++++++++ 3 files changed, 11 insertions(+) diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/entity/ResourceEntity.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/entity/ResourceEntity.java index e371705c..099e5f5f 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/entity/ResourceEntity.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/entity/ResourceEntity.java @@ -49,6 +49,8 @@ public class ResourceEntity extends PanacheEntityBase implements Serializable { @Transient @Getter(AccessLevel.NONE) private String cdnUrl; + @Column(name = "description") + private String description; @PrePersist public void generateUUID() { diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/model/ResourceDTO.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/model/ResourceDTO.java index 5f0ba24a..b92d2398 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/model/ResourceDTO.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/model/ResourceDTO.java @@ -19,4 +19,5 @@ public class ResourceDTO { private String lastUpdatedBy; private String cdnUrl; private ResourceFileDTO resourceFile; + private String description; } diff --git a/src/test/java/it/gov/pagopa/atmlayer/service/model/entity/ResourceEntityTest.java b/src/test/java/it/gov/pagopa/atmlayer/service/model/entity/ResourceEntityTest.java index c3440b7a..754b38e2 100644 --- a/src/test/java/it/gov/pagopa/atmlayer/service/model/entity/ResourceEntityTest.java +++ b/src/test/java/it/gov/pagopa/atmlayer/service/model/entity/ResourceEntityTest.java @@ -76,5 +76,13 @@ void testCdnUrl() { cdnBaseUrl + "/" + resourceFile.getStorageKey().substring(cdnOffsetPath.length() + 1); assertNotEquals(expectedCdnUrl, resourceEntity.getCdnUrl()); } + + @Test + void testDescriptionGetterGetter() { + ResourceEntity resourceEntity = new ResourceEntity(); + String description = "Sample description"; + resourceEntity.setDescription(description); + assertEquals(description, resourceEntity.getDescription()); + } } From 7e7a5a17eec61c48cdcc4f5d6336262d76bd4627 Mon Sep 17 00:00:00 2001 From: stefano Date: Thu, 1 Feb 2024 12:49:57 +0100 Subject: [PATCH 02/10] added new field "description" with tests --- .../atmlayer/service/model/dto/ResourceCreationDto.java | 2 ++ .../atmlayer/service/model/mapper/ResourceEntityMapper.java | 1 + .../service/model/resource/ResourceEntityResource.java | 2 +- .../atmlayer/service/model/service/ResourceEntityService.java | 2 +- .../service/model/service/impl/ResourceEntityServiceImpl.java | 2 +- .../service/model/resource/ResourceEntityResourceTest.java | 4 ++-- .../model/service/impl/ResourceEntityServiceImplTest.java | 4 ++-- 7 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/dto/ResourceCreationDto.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/dto/ResourceCreationDto.java index 600725b7..49a65aa4 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/dto/ResourceCreationDto.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/dto/ResourceCreationDto.java @@ -37,4 +37,6 @@ public class ResourceCreationDto { @Schema(description = "Description of the path parameter: example/path", pattern = "(^$)|(^(?!/)[a-zA-Z0-9/]+(? createResource( throws NoSuchAlgorithmException, IOException { ResourceEntity resourceEntity = resourceEntityMapper.toEntityCreation(resourceCreationDto); return resourceEntityService.createResource(resourceEntity, resourceCreationDto.getFile(), - resourceCreationDto.getFilename(), resourceCreationDto.getPath()) + resourceCreationDto.getFilename(), resourceCreationDto.getPath(), resourceCreationDto.getDescription()) .onItem() .transformToUni(resource -> Uni.createFrom().item(resourceEntityMapper.toDTO(resource))); } diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/service/ResourceEntityService.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/service/ResourceEntityService.java index b1b4c4b9..f5292b97 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/service/ResourceEntityService.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/service/ResourceEntityService.java @@ -22,7 +22,7 @@ Uni saveAndUpload(ResourceEntity resourceEntity, File file, Stri Uni upload(ResourceEntity resourceEntity, File file, String filename, String path); - Uni createResource(ResourceEntity resourceEntity, File file, String filename, String path); + Uni createResource(ResourceEntity resourceEntity, File file, String filename, String path, String description); Uni updateResource(UUID uuid, File file); diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/service/impl/ResourceEntityServiceImpl.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/service/impl/ResourceEntityServiceImpl.java index 5aa20950..fc8a7cba 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/service/impl/ResourceEntityServiceImpl.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/service/impl/ResourceEntityServiceImpl.java @@ -95,7 +95,7 @@ public Uni upload(ResourceEntity resourceEntity, File file, @Override public Uni createResource(ResourceEntity resourceEntity, File file, - String filename, String path) { + String filename, String path, String description) { return findBySHA256(resourceEntity.getSha256()) .onItem().transformToUni(Unchecked.function(x -> { if (x.isPresent()) { diff --git a/src/test/java/it/gov/pagopa/atmlayer/service/model/resource/ResourceEntityResourceTest.java b/src/test/java/it/gov/pagopa/atmlayer/service/model/resource/ResourceEntityResourceTest.java index 56e86be4..c17f40bd 100644 --- a/src/test/java/it/gov/pagopa/atmlayer/service/model/resource/ResourceEntityResourceTest.java +++ b/src/test/java/it/gov/pagopa/atmlayer/service/model/resource/ResourceEntityResourceTest.java @@ -46,7 +46,7 @@ void testCreateResource() throws NoSuchAlgorithmException, IOException { when(resourceEntityMapper.toEntityCreation(any(ResourceCreationDto.class))).thenReturn( resourceEntity); when(resourceEntityService.createResource(any(ResourceEntity.class), any(File.class), - any(String.class), any(String.class))) + any(String.class), any(String.class), any(String.class))) .thenReturn(Uni.createFrom().item(resourceEntity)); when(resourceEntityMapper.toDTO(any(ResourceEntity.class))).thenReturn(resourceDTO); @@ -55,7 +55,7 @@ void testCreateResource() throws NoSuchAlgorithmException, IOException { .multiPart("file", new File("src/test/resources/TestMalformed.bpmn")) .formParam("filename", "name.bpmn") .formParam("resourceType", "OTHER") - .formParam("path", "") + .formParam("path", "").formParam("description", "") .when().post("/api/v1/model/resources") .then() .statusCode(200) diff --git a/src/test/java/it/gov/pagopa/atmlayer/service/model/service/impl/ResourceEntityServiceImplTest.java b/src/test/java/it/gov/pagopa/atmlayer/service/model/service/impl/ResourceEntityServiceImplTest.java index e189a19a..ea793cf0 100644 --- a/src/test/java/it/gov/pagopa/atmlayer/service/model/service/impl/ResourceEntityServiceImplTest.java +++ b/src/test/java/it/gov/pagopa/atmlayer/service/model/service/impl/ResourceEntityServiceImplTest.java @@ -61,7 +61,7 @@ void testCreateAlreadyExists() { resourceEntity.setStorageKey("storageKey"); when(resourceEntityRepository.findBySHA256(any(String.class))).thenReturn(Uni.createFrom().nullItem()); when(resourceFileService.findByStorageKey(any(String.class))).thenReturn(Uni.createFrom().item(Optional.of(resourceFile))); - resourceEntityService.createResource(resourceEntity, file, "filename", "path") + resourceEntityService.createResource(resourceEntity, file, "filename", "path", "description") .subscribe().withSubscriber(UniAssertSubscriber.create()) .assertFailedWith(AtmLayerException.class, "Cannot upload storageKey: resource with same file name and path already exists"); } @@ -77,7 +77,7 @@ void testCreateSyncError() { when(resourceEntityRepository.persist(any(ResourceEntity.class))).thenReturn(Uni.createFrom().item(resourceEntity)); when(resourceEntityStorageService.saveFile(any(ResourceEntity.class), any(File.class), any(String.class), any(String.class))).thenReturn(Uni.createFrom().item(resourceFile)); when(resourceEntityRepository.findById(any(UUID.class))).thenReturn(Uni.createFrom().nullItem()); - resourceEntityService.createResource(resourceEntity, file, "filename.xml", "path") + resourceEntityService.createResource(resourceEntity, file, "filename.xml", "path", "description") .subscribe().withSubscriber(UniAssertSubscriber.create()) .assertFailedWith(AtmLayerException.class, "Sync problem on resource creation"); } From 4df85742fd1f66731d8a4f9c1b0762def73903ab Mon Sep 17 00:00:00 2001 From: stefano Date: Mon, 5 Feb 2024 15:34:19 +0100 Subject: [PATCH 03/10] add user profile managing, with creation date and last update date --- .../model/dto/UserProfileCreationDto.java | 21 ++++ .../service/model/dto/UserProfileDto.java | 21 ++++ .../service/model/entity/UserProfile.java | 31 ++++++ .../model/enumeration/AppErrorCodeEnum.java | 5 +- .../model/enumeration/AppErrorType.java | 4 +- .../model/enumeration/UserProfileEnum.java | 28 ++++++ .../model/mapper/UserProfileMapper.java | 44 +++++++++ .../repository/UserProfileRepository.java | 13 +++ .../model/resource/UserProfileResource.java | 94 ++++++++++++++++++ .../model/service/UserProfileService.java | 19 ++++ .../service/impl/UserProfileServiceImpl.java | 95 +++++++++++++++++++ .../validators/UserProfileValidator.java | 20 ++++ 12 files changed, 393 insertions(+), 2 deletions(-) create mode 100644 src/main/java/it/gov/pagopa/atmlayer/service/model/dto/UserProfileCreationDto.java create mode 100644 src/main/java/it/gov/pagopa/atmlayer/service/model/dto/UserProfileDto.java create mode 100644 src/main/java/it/gov/pagopa/atmlayer/service/model/entity/UserProfile.java create mode 100644 src/main/java/it/gov/pagopa/atmlayer/service/model/enumeration/UserProfileEnum.java create mode 100644 src/main/java/it/gov/pagopa/atmlayer/service/model/mapper/UserProfileMapper.java create mode 100644 src/main/java/it/gov/pagopa/atmlayer/service/model/repository/UserProfileRepository.java create mode 100644 src/main/java/it/gov/pagopa/atmlayer/service/model/resource/UserProfileResource.java create mode 100644 src/main/java/it/gov/pagopa/atmlayer/service/model/service/UserProfileService.java create mode 100644 src/main/java/it/gov/pagopa/atmlayer/service/model/service/impl/UserProfileServiceImpl.java create mode 100644 src/main/java/it/gov/pagopa/atmlayer/service/model/validators/UserProfileValidator.java diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/dto/UserProfileCreationDto.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/dto/UserProfileCreationDto.java new file mode 100644 index 00000000..d4964cad --- /dev/null +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/dto/UserProfileCreationDto.java @@ -0,0 +1,21 @@ +package it.gov.pagopa.atmlayer.service.model.dto; + +import jakarta.validation.constraints.Email; +import jakarta.validation.constraints.NotNull; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.sql.Timestamp; + +@NoArgsConstructor +@Data +public class UserProfileCreationDto { + + @NotNull(message = "is required") + @Email(message = "must be an email address in the correct format") + private String userId; + @NotNull(message = "is required") + private Integer profile; + private Timestamp createdAt; + private Timestamp lastUpdatedAt; +} diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/dto/UserProfileDto.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/dto/UserProfileDto.java new file mode 100644 index 00000000..31fe26a1 --- /dev/null +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/dto/UserProfileDto.java @@ -0,0 +1,21 @@ +package it.gov.pagopa.atmlayer.service.model.dto; + +import com.fasterxml.jackson.annotation.JsonInclude; +import it.gov.pagopa.atmlayer.service.model.enumeration.UserProfileEnum; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.sql.Timestamp; + +@NoArgsConstructor +@Data +@JsonInclude(JsonInclude.Include.NON_NULL) +public class UserProfileDto { + private String userId; + private UserProfileEnum profile; + private Boolean visible; + private Boolean editable; + private Boolean admin; + private Timestamp createdAt; + private Timestamp lastUpdatedAt; +} diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/entity/UserProfile.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/entity/UserProfile.java new file mode 100644 index 00000000..2ffb949d --- /dev/null +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/entity/UserProfile.java @@ -0,0 +1,31 @@ +package it.gov.pagopa.atmlayer.service.model.entity; + +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import org.hibernate.annotations.CreationTimestamp; +import org.hibernate.annotations.UpdateTimestamp; + +import java.sql.Timestamp; + +@Entity +@Setter +@Getter +@AllArgsConstructor +@NoArgsConstructor +@Table(name = "user_profile") +public class UserProfile { + @Id + @Column(name = "user_id") + private String userId; + @Column(name = "profile") + private int profile; + @CreationTimestamp + @Column(name = "created_at") + private Timestamp createdAt; + @UpdateTimestamp + @Column(name = "last_updated_at") + private Timestamp lastUpdatedAt; +} diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/enumeration/AppErrorCodeEnum.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/enumeration/AppErrorCodeEnum.java index 71819fa4..019e4199 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/enumeration/AppErrorCodeEnum.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/enumeration/AppErrorCodeEnum.java @@ -52,7 +52,10 @@ public enum AppErrorCodeEnum { BPMN_ALREADY_DISABLED("ATMLM_4000039", "The referenced BPMN file is already disabled", CANNOT_DISABLE), DUPLICATE_ASSOCIATION_CONFIGS("ATMLM_4000040","Cannot save associations: duplicate configurations in input",CANNOT_ASSOCIATE), PAGE_SIZE_WRONG_VALUE("ATMLM_4000041", "Page and Size must not be null or empty, and Size greater than zero", INVALID_ARGUMENT), - ILLEGALE_CONFIGURATION_TRIPLET("ATMLM_4000042","AcquirerId must be specified for BranchId, and BranchId must be specified for TerminalId",INVALID_ARGUMENT); + ILLEGALE_CONFIGURATION_TRIPLET("ATMLM_4000042","AcquirerId must be specified for BranchId, and BranchId must be specified for TerminalId",INVALID_ARGUMENT), + USER_PROFILE_WITH_SAME_ID_ALREADY_EXIST("ATMLM_4000043", "A User Profile with the same id already exists", CONSTRAINT_VIOLATION), + NO_USER_PROFILE_FOUND_FOR_ID("ATMLM_4000044", "No User found for id", NOT_EXISTING_USER_ID), + NO_USER_PROFILE_FOUND_FOR_PROFILE("ATMLM_4000045", "No User profile found", NOT_EXISTING_USER_PROFILE); private final String errorCode; private final String errorMessage; private final AppErrorType type; diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/enumeration/AppErrorType.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/enumeration/AppErrorType.java index 2573cf55..b0646681 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/enumeration/AppErrorType.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/enumeration/AppErrorType.java @@ -23,5 +23,7 @@ public enum AppErrorType { NOT_UPLOADABLE, CANNOT_ROLLBACK, CANNOT_DISABLE, - CANNOT_ASSOCIATE + CANNOT_ASSOCIATE, + NOT_EXISTING_USER_ID, + NOT_EXISTING_USER_PROFILE, } \ No newline at end of file diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/enumeration/UserProfileEnum.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/enumeration/UserProfileEnum.java new file mode 100644 index 00000000..239e962e --- /dev/null +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/enumeration/UserProfileEnum.java @@ -0,0 +1,28 @@ +package it.gov.pagopa.atmlayer.service.model.enumeration; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.util.HashMap; +import java.util.Map; + +@AllArgsConstructor +@Getter +public enum UserProfileEnum { + GUEST(1), + OPERATOR(2), + ADMIN(3); + + private final int value; + private static final Map map = new HashMap<>(); + + static { + for (UserProfileEnum profileEnum : UserProfileEnum.values()) { + map.put(profileEnum.value, profileEnum); + } + } + + public static UserProfileEnum valueOf(int profile) { + return map.get(profile); + } +} diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/mapper/UserProfileMapper.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/mapper/UserProfileMapper.java new file mode 100644 index 00000000..80f80c2d --- /dev/null +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/mapper/UserProfileMapper.java @@ -0,0 +1,44 @@ +package it.gov.pagopa.atmlayer.service.model.mapper; + +import it.gov.pagopa.atmlayer.service.model.dto.UserProfileDto; +import it.gov.pagopa.atmlayer.service.model.entity.UserProfile; +import it.gov.pagopa.atmlayer.service.model.enumeration.UserProfileEnum; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.NullValuePropertyMappingStrategy; + +@Mapper(componentModel = "cdi", nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE) +public abstract class UserProfileMapper { + + @Mapping(source = "userProfile.userId", target = "userId") + @Mapping(expression = "java(getEnumValue(userProfile.getProfile()))", target = "profile") + @Mapping(source = "userProfile.createdAt", target = "createdAt") + @Mapping(source = "userProfile.lastUpdatedAt", target = "lastUpdatedAt") + public abstract UserProfileDto toUserProfileDto(UserProfile userProfile); + + UserProfileEnum getEnumValue(int source) { + return UserProfileEnum.valueOf(source); + } + + public UserProfileDto toUserProfileDtoWithProfileMapping(UserProfile userProfile) { + UserProfileDto userProfileDTO = this.toUserProfileDto(userProfile); + switch(UserProfileEnum.valueOf(userProfile.getProfile())){ + case GUEST -> { + userProfileDTO.setVisible(true); + userProfileDTO.setEditable(false); + userProfileDTO.setAdmin(false); + } + case OPERATOR -> { + userProfileDTO.setVisible(true); + userProfileDTO.setEditable(true); + userProfileDTO.setAdmin(false); + } + case ADMIN -> { + userProfileDTO.setVisible(true); + userProfileDTO.setEditable(true); + userProfileDTO.setAdmin(true); + } + } + return userProfileDTO; + } +} diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/repository/UserProfileRepository.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/repository/UserProfileRepository.java new file mode 100644 index 00000000..73e3a2ad --- /dev/null +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/repository/UserProfileRepository.java @@ -0,0 +1,13 @@ +package it.gov.pagopa.atmlayer.service.model.repository; + +import io.quarkus.hibernate.reactive.panache.PanacheRepositoryBase; +import io.smallrye.mutiny.Uni; +import it.gov.pagopa.atmlayer.service.model.entity.UserProfile; +import jakarta.enterprise.context.ApplicationScoped; + +@ApplicationScoped +public class UserProfileRepository implements PanacheRepositoryBase { + public Uni findUserId(String userId) { + return find("userId", userId).firstResult(); + } +} diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/resource/UserProfileResource.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/resource/UserProfileResource.java new file mode 100644 index 00000000..9b0ade90 --- /dev/null +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/resource/UserProfileResource.java @@ -0,0 +1,94 @@ +package it.gov.pagopa.atmlayer.service.model.resource; + +import io.smallrye.mutiny.Uni; +import io.smallrye.mutiny.unchecked.Unchecked; +import it.gov.pagopa.atmlayer.service.model.dto.UserProfileCreationDto; +import it.gov.pagopa.atmlayer.service.model.dto.UserProfileDto; +import it.gov.pagopa.atmlayer.service.model.entity.UserProfile; +import it.gov.pagopa.atmlayer.service.model.mapper.UserProfileMapper; +import it.gov.pagopa.atmlayer.service.model.service.UserProfileService; +import it.gov.pagopa.atmlayer.service.model.validators.UserProfileValidator; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; +import jakarta.validation.Valid; +import jakarta.validation.constraints.NotNull; +import jakarta.ws.rs.*; +import jakarta.ws.rs.core.MediaType; +import lombok.extern.slf4j.Slf4j; +import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody; +import org.eclipse.microprofile.openapi.annotations.tags.Tag; + +import java.io.IOException; +import java.security.NoSuchAlgorithmException; +import java.util.List; + +@ApplicationScoped +@Path("/users") +@Tag(name = "USERS", description = "USERS operations") +@Slf4j +public class UserProfileResource { + + @Inject + UserProfileService userProfileService; + @Inject + UserProfileMapper userProfileMapper; + @Inject + UserProfileValidator userProfileValidator; + + @GET + @Path("/search") + @Produces(MediaType.APPLICATION_JSON) + public Uni findByUserId(@NotNull @QueryParam("userId") String userId) { + return this.userProfileService.findByUserId(userId) + .onItem() + .transformToUni(Unchecked.function( x -> { + UserProfileDto userProfileDto = this.userProfileMapper.toUserProfileDtoWithProfileMapping(x); + return Uni.createFrom().item(userProfileDto); + })); + } + + @GET + @Produces(MediaType.APPLICATION_JSON) + public Uni> getAll(){ + return this.userProfileService.getAll() + .onItem() + .transform(Unchecked.function(list -> { + if (list.isEmpty()) { + log.info("No User profiles saved in database"); + } + return list.stream() + .map( x -> this.userProfileMapper.toUserProfileDto(x)) + .toList(); + })); + } + + @POST + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public Uni create( + @RequestBody(required = true) @Valid UserProfileCreationDto user) throws IOException { + return this.userProfileValidator.validateExistenceProfileType(user.getProfile()) + .onItem() + .transformToUni((x) -> this.userProfileService.save(user) + .onItem() + .transformToUni(Unchecked.function(u -> Uni.createFrom().item(this.userProfileMapper.toUserProfileDto(u))))); + } + + @DELETE + @Path("/search") + public Uni delete(@NotNull @QueryParam("userId") String userId) { + return this.userProfileService.delete(userId); + } + + @PUT + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public Uni update( + @RequestBody(required = true) @Valid UserProfileCreationDto user) throws NoSuchAlgorithmException, IOException { + return this.userProfileValidator.validateExistenceProfileType(user.getProfile()) + .onItem() + .transformToUni((x) -> this.userProfileService.update(user) + .onItem() + .transformToUni(Unchecked.function(u -> Uni.createFrom().item(this.userProfileMapper.toUserProfileDto(u))))); + } +} diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/service/UserProfileService.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/service/UserProfileService.java new file mode 100644 index 00000000..d9004122 --- /dev/null +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/service/UserProfileService.java @@ -0,0 +1,19 @@ +package it.gov.pagopa.atmlayer.service.model.service; + +import io.smallrye.mutiny.Uni; +import it.gov.pagopa.atmlayer.service.model.dto.UserProfileCreationDto; +import it.gov.pagopa.atmlayer.service.model.entity.ResourceEntity; +import it.gov.pagopa.atmlayer.service.model.entity.UserProfile; + +import java.util.List; +import java.util.Optional; +import java.util.UUID; + +public interface UserProfileService { + + Uni save(UserProfileCreationDto userProfile); + Uni findByUserId(String userId); + Uni> getAll(); + Uni delete(String userId); + Uni update(UserProfileCreationDto userProfile); +} diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/service/impl/UserProfileServiceImpl.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/service/impl/UserProfileServiceImpl.java new file mode 100644 index 00000000..d4163692 --- /dev/null +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/service/impl/UserProfileServiceImpl.java @@ -0,0 +1,95 @@ +package it.gov.pagopa.atmlayer.service.model.service.impl; + +import io.quarkus.hibernate.reactive.panache.common.WithSession; +import io.quarkus.hibernate.reactive.panache.common.WithTransaction; +import io.smallrye.mutiny.Uni; +import io.smallrye.mutiny.unchecked.Unchecked; +import it.gov.pagopa.atmlayer.service.model.dto.UserProfileCreationDto; +import it.gov.pagopa.atmlayer.service.model.entity.UserProfile; +import it.gov.pagopa.atmlayer.service.model.enumeration.UserProfileEnum; +import it.gov.pagopa.atmlayer.service.model.exception.AtmLayerException; +import it.gov.pagopa.atmlayer.service.model.repository.UserProfileRepository; +import it.gov.pagopa.atmlayer.service.model.service.UserProfileService; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; +import jakarta.ws.rs.core.Response; +import lombok.extern.slf4j.Slf4j; + +import java.sql.Timestamp; +import java.util.List; + +import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.NO_USER_PROFILE_FOUND_FOR_ID; +import static it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum.USER_PROFILE_WITH_SAME_ID_ALREADY_EXIST; + +@ApplicationScoped +@Slf4j +public class UserProfileServiceImpl implements UserProfileService { + @Inject + UserProfileRepository userProfileRepository; + + @Override + @WithSession + @WithTransaction + public Uni save(UserProfileCreationDto userProfile) { + log.info("Create user {}", userProfile.getUserId()); + UserProfile localUserProfile = new UserProfile(userProfile.getUserId(), + userProfile.getProfile(), + new Timestamp(System.currentTimeMillis()), + new Timestamp(System.currentTimeMillis())); + return this.userProfileRepository.findUserId(localUserProfile.getUserId()) + .onItem().transformToUni(Unchecked.function(x -> { + if(x != null){ + throw new AtmLayerException("A user with the same id already exists", + Response.Status.BAD_REQUEST, + USER_PROFILE_WITH_SAME_ID_ALREADY_EXIST); + } + return this.userProfileRepository.persist(localUserProfile); + } + )); + } + + @Override + @WithSession + public Uni findByUserId(String userId) { + return this.userProfileRepository.findUserId(userId) + .onItem().transformToUni(Unchecked.function(x -> { + if(x == null){ + throw new AtmLayerException(String.format("A user with the id %s not exists", userId), + Response.Status.NOT_FOUND, + NO_USER_PROFILE_FOUND_FOR_ID); + } + return Uni.createFrom().item(x); + } + )); + } + + @Override + @WithSession + public Uni> getAll() { + return this.userProfileRepository.findAll().list(); + } + + @Override + @WithSession + @WithTransaction + public Uni delete(String userId) { + log.info("Delete user {} from database", userId); + return this.findByUserId(userId) + .onItem().transformToUni(Unchecked.function(x -> userProfileRepository.delete(x) + )); + } + + @Override + @WithSession + @WithTransaction + public Uni update(UserProfileCreationDto userProfile) { + log.info("Update user {}", userProfile.getUserId()); + return this.findByUserId(userProfile.getUserId()) + .onItem().transformToUni(Unchecked.function(x -> { + x.setProfile(userProfile.getProfile()); + x.setLastUpdatedAt(new Timestamp(System.currentTimeMillis())); + return userProfileRepository.persist(x); + } + )); + } +} diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/validators/UserProfileValidator.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/validators/UserProfileValidator.java new file mode 100644 index 00000000..fc292f08 --- /dev/null +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/validators/UserProfileValidator.java @@ -0,0 +1,20 @@ +package it.gov.pagopa.atmlayer.service.model.validators; + +import io.smallrye.mutiny.Uni; +import it.gov.pagopa.atmlayer.service.model.enumeration.AppErrorCodeEnum; +import it.gov.pagopa.atmlayer.service.model.enumeration.UserProfileEnum; +import it.gov.pagopa.atmlayer.service.model.exception.AtmLayerException; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.ws.rs.core.Response; + +@ApplicationScoped +public class UserProfileValidator { + + public Uni validateExistenceProfileType (int profile){ + if(UserProfileEnum.valueOf(profile) == null){ + String errorMessage = String.format("Profile with value %s not exists", profile); + throw new AtmLayerException(errorMessage, Response.Status.BAD_REQUEST, AppErrorCodeEnum.NO_USER_PROFILE_FOUND_FOR_PROFILE); + } + return Uni.createFrom().nullItem(); + } +} From 8fc17a93f3bad74663c3f6565e82bc54ab028d9d Mon Sep 17 00:00:00 2001 From: stefano Date: Mon, 5 Feb 2024 15:56:01 +0100 Subject: [PATCH 04/10] added UserProfile to mapper --- .../service/model/mapper/UserProfileMapper.java | 6 ++++++ .../model/service/impl/UserProfileServiceImpl.java | 10 +++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/mapper/UserProfileMapper.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/mapper/UserProfileMapper.java index 80f80c2d..f3b73203 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/mapper/UserProfileMapper.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/mapper/UserProfileMapper.java @@ -1,5 +1,6 @@ package it.gov.pagopa.atmlayer.service.model.mapper; +import it.gov.pagopa.atmlayer.service.model.dto.UserProfileCreationDto; import it.gov.pagopa.atmlayer.service.model.dto.UserProfileDto; import it.gov.pagopa.atmlayer.service.model.entity.UserProfile; import it.gov.pagopa.atmlayer.service.model.enumeration.UserProfileEnum; @@ -15,6 +16,11 @@ public abstract class UserProfileMapper { @Mapping(source = "userProfile.createdAt", target = "createdAt") @Mapping(source = "userProfile.lastUpdatedAt", target = "lastUpdatedAt") public abstract UserProfileDto toUserProfileDto(UserProfile userProfile); + @Mapping(source = "userProfile.userId", target = "userId") + @Mapping(source = "userProfile.profile", target = "profile") + @Mapping(source = "userProfile.createdAt", target = "createdAt") + @Mapping(source = "userProfile.lastUpdatedAt", target = "lastUpdatedAt") + public abstract UserProfile toUserProfile(UserProfileCreationDto userProfile); UserProfileEnum getEnumValue(int source) { return UserProfileEnum.valueOf(source); diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/service/impl/UserProfileServiceImpl.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/service/impl/UserProfileServiceImpl.java index d4163692..8c84134b 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/service/impl/UserProfileServiceImpl.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/service/impl/UserProfileServiceImpl.java @@ -6,8 +6,8 @@ import io.smallrye.mutiny.unchecked.Unchecked; import it.gov.pagopa.atmlayer.service.model.dto.UserProfileCreationDto; import it.gov.pagopa.atmlayer.service.model.entity.UserProfile; -import it.gov.pagopa.atmlayer.service.model.enumeration.UserProfileEnum; import it.gov.pagopa.atmlayer.service.model.exception.AtmLayerException; +import it.gov.pagopa.atmlayer.service.model.mapper.UserProfileMapper; import it.gov.pagopa.atmlayer.service.model.repository.UserProfileRepository; import it.gov.pagopa.atmlayer.service.model.service.UserProfileService; import jakarta.enterprise.context.ApplicationScoped; @@ -27,15 +27,15 @@ public class UserProfileServiceImpl implements UserProfileService { @Inject UserProfileRepository userProfileRepository; + @Inject + UserProfileMapper userProfileMapper; + @Override @WithSession @WithTransaction public Uni save(UserProfileCreationDto userProfile) { log.info("Create user {}", userProfile.getUserId()); - UserProfile localUserProfile = new UserProfile(userProfile.getUserId(), - userProfile.getProfile(), - new Timestamp(System.currentTimeMillis()), - new Timestamp(System.currentTimeMillis())); + UserProfile localUserProfile = this.userProfileMapper.toUserProfile(userProfile); return this.userProfileRepository.findUserId(localUserProfile.getUserId()) .onItem().transformToUni(Unchecked.function(x -> { if(x != null){ From 29c4846795265213d642b90b040805a25edb954c Mon Sep 17 00:00:00 2001 From: stefano Date: Tue, 6 Feb 2024 09:14:25 +0100 Subject: [PATCH 05/10] renaming business methods --- .../model/resource/UserProfileResource.java | 17 ++++++++--------- .../model/service/UserProfileService.java | 8 ++++---- .../service/impl/UserProfileServiceImpl.java | 8 ++++---- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/resource/UserProfileResource.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/resource/UserProfileResource.java index 9b0ade90..11f27008 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/resource/UserProfileResource.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/resource/UserProfileResource.java @@ -4,7 +4,6 @@ import io.smallrye.mutiny.unchecked.Unchecked; import it.gov.pagopa.atmlayer.service.model.dto.UserProfileCreationDto; import it.gov.pagopa.atmlayer.service.model.dto.UserProfileDto; -import it.gov.pagopa.atmlayer.service.model.entity.UserProfile; import it.gov.pagopa.atmlayer.service.model.mapper.UserProfileMapper; import it.gov.pagopa.atmlayer.service.model.service.UserProfileService; import it.gov.pagopa.atmlayer.service.model.validators.UserProfileValidator; @@ -49,8 +48,8 @@ public Uni findByUserId(@NotNull @QueryParam("userId") String us @GET @Produces(MediaType.APPLICATION_JSON) - public Uni> getAll(){ - return this.userProfileService.getAll() + public Uni> getUsers(){ + return this.userProfileService.getUsers() .onItem() .transform(Unchecked.function(list -> { if (list.isEmpty()) { @@ -65,19 +64,19 @@ public Uni> getAll(){ @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) - public Uni create( - @RequestBody(required = true) @Valid UserProfileCreationDto user) throws IOException { + public Uni createUser( + @RequestBody(required = true) @Valid UserProfileCreationDto user){ return this.userProfileValidator.validateExistenceProfileType(user.getProfile()) .onItem() - .transformToUni((x) -> this.userProfileService.save(user) + .transformToUni((x) -> this.userProfileService.createUser(user) .onItem() .transformToUni(Unchecked.function(u -> Uni.createFrom().item(this.userProfileMapper.toUserProfileDto(u))))); } @DELETE @Path("/search") - public Uni delete(@NotNull @QueryParam("userId") String userId) { - return this.userProfileService.delete(userId); + public Uni deleteUser(@NotNull @QueryParam("userId") String userId) { + return this.userProfileService.deleteUser(userId); } @PUT @@ -87,7 +86,7 @@ public Uni update( @RequestBody(required = true) @Valid UserProfileCreationDto user) throws NoSuchAlgorithmException, IOException { return this.userProfileValidator.validateExistenceProfileType(user.getProfile()) .onItem() - .transformToUni((x) -> this.userProfileService.update(user) + .transformToUni((x) -> this.userProfileService.updateUser(user) .onItem() .transformToUni(Unchecked.function(u -> Uni.createFrom().item(this.userProfileMapper.toUserProfileDto(u))))); } diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/service/UserProfileService.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/service/UserProfileService.java index d9004122..bfc02d5a 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/service/UserProfileService.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/service/UserProfileService.java @@ -11,9 +11,9 @@ public interface UserProfileService { - Uni save(UserProfileCreationDto userProfile); + Uni createUser(UserProfileCreationDto userProfile); Uni findByUserId(String userId); - Uni> getAll(); - Uni delete(String userId); - Uni update(UserProfileCreationDto userProfile); + Uni> getUsers(); + Uni deleteUser(String userId); + Uni updateUser(UserProfileCreationDto userProfile); } diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/service/impl/UserProfileServiceImpl.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/service/impl/UserProfileServiceImpl.java index 8c84134b..8e6dff77 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/service/impl/UserProfileServiceImpl.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/service/impl/UserProfileServiceImpl.java @@ -33,7 +33,7 @@ public class UserProfileServiceImpl implements UserProfileService { @Override @WithSession @WithTransaction - public Uni save(UserProfileCreationDto userProfile) { + public Uni createUser(UserProfileCreationDto userProfile) { log.info("Create user {}", userProfile.getUserId()); UserProfile localUserProfile = this.userProfileMapper.toUserProfile(userProfile); return this.userProfileRepository.findUserId(localUserProfile.getUserId()) @@ -65,14 +65,14 @@ public Uni findByUserId(String userId) { @Override @WithSession - public Uni> getAll() { + public Uni> getUsers() { return this.userProfileRepository.findAll().list(); } @Override @WithSession @WithTransaction - public Uni delete(String userId) { + public Uni deleteUser(String userId) { log.info("Delete user {} from database", userId); return this.findByUserId(userId) .onItem().transformToUni(Unchecked.function(x -> userProfileRepository.delete(x) @@ -82,7 +82,7 @@ public Uni delete(String userId) { @Override @WithSession @WithTransaction - public Uni update(UserProfileCreationDto userProfile) { + public Uni updateUser(UserProfileCreationDto userProfile) { log.info("Update user {}", userProfile.getUserId()); return this.findByUserId(userProfile.getUserId()) .onItem().transformToUni(Unchecked.function(x -> { From 8fb0a21a5451f9d8bed4d3cd2193691c42396867 Mon Sep 17 00:00:00 2001 From: stefano Date: Tue, 6 Feb 2024 11:00:58 +0100 Subject: [PATCH 06/10] refactoring user profile mapper and resource with toDtoList method --- .../atmlayer/service/model/mapper/UserProfileMapper.java | 6 ++++++ .../service/model/resource/UserProfileResource.java | 4 +--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/mapper/UserProfileMapper.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/mapper/UserProfileMapper.java index f3b73203..e392d616 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/mapper/UserProfileMapper.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/mapper/UserProfileMapper.java @@ -8,6 +8,8 @@ import org.mapstruct.Mapping; import org.mapstruct.NullValuePropertyMappingStrategy; +import java.util.List; + @Mapper(componentModel = "cdi", nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE) public abstract class UserProfileMapper { @@ -47,4 +49,8 @@ public UserProfileDto toUserProfileDtoWithProfileMapping(UserProfile userProfile } return userProfileDTO; } + + public List toDtoList(List list){ + return list.stream().map( x -> this.toUserProfileDto(x)).toList(); + } } diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/resource/UserProfileResource.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/resource/UserProfileResource.java index 11f27008..1142cdfe 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/resource/UserProfileResource.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/resource/UserProfileResource.java @@ -55,9 +55,7 @@ public Uni> getUsers(){ if (list.isEmpty()) { log.info("No User profiles saved in database"); } - return list.stream() - .map( x -> this.userProfileMapper.toUserProfileDto(x)) - .toList(); + return userProfileMapper.toDtoList(list); })); } From 231935917905d7e5b276b2932a7b5ccf849f57a9 Mon Sep 17 00:00:00 2001 From: stefano Date: Tue, 6 Feb 2024 14:22:35 +0100 Subject: [PATCH 07/10] renaming update method --- .../service/model/resource/UserProfileResource.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/resource/UserProfileResource.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/resource/UserProfileResource.java index 1142cdfe..d6ccfdb4 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/resource/UserProfileResource.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/resource/UserProfileResource.java @@ -1,5 +1,6 @@ package it.gov.pagopa.atmlayer.service.model.resource; +import io.smallrye.common.annotation.NonBlocking; import io.smallrye.mutiny.Uni; import io.smallrye.mutiny.unchecked.Unchecked; import it.gov.pagopa.atmlayer.service.model.dto.UserProfileCreationDto; @@ -17,8 +18,6 @@ import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody; import org.eclipse.microprofile.openapi.annotations.tags.Tag; -import java.io.IOException; -import java.security.NoSuchAlgorithmException; import java.util.List; @ApplicationScoped @@ -62,6 +61,7 @@ public Uni> getUsers(){ @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) + @NonBlocking public Uni createUser( @RequestBody(required = true) @Valid UserProfileCreationDto user){ return this.userProfileValidator.validateExistenceProfileType(user.getProfile()) @@ -80,8 +80,8 @@ public Uni deleteUser(@NotNull @QueryParam("userId") String userId) { @PUT @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) - public Uni update( - @RequestBody(required = true) @Valid UserProfileCreationDto user) throws NoSuchAlgorithmException, IOException { + public Uni updateUser( + @RequestBody(required = true) @Valid UserProfileCreationDto user) { return this.userProfileValidator.validateExistenceProfileType(user.getProfile()) .onItem() .transformToUni((x) -> this.userProfileService.updateUser(user) From a2a981af7807ef79dfcff10dbd5d6706d820ce60 Mon Sep 17 00:00:00 2001 From: stefano Date: Wed, 7 Feb 2024 10:24:55 +0100 Subject: [PATCH 08/10] add test cases for UserProfile --- .../model/dto/UserProfileCreationDtoTest.java | 33 ++++ .../service/model/dto/UserProfileDtoTest.java | 39 +++++ .../service/model/entity/UserProfileTest.java | 40 +++++ .../enumeration/UserProfileEnumTest.java | 24 +++ .../model/mapper/UserProfileMapperTest.java | 107 ++++++++++++ .../repository/UserProfileRepositoryTest.java | 35 ++++ .../resource/UserProfileResourceTest.java | 165 ++++++++++++++++++ .../impl/UserProfileServiceImplTest.java | 64 +++++++ .../validators/UserProfileValidatorTest.java | 34 ++++ 9 files changed, 541 insertions(+) create mode 100644 src/test/java/it/gov/pagopa/atmlayer/service/model/dto/UserProfileCreationDtoTest.java create mode 100644 src/test/java/it/gov/pagopa/atmlayer/service/model/dto/UserProfileDtoTest.java create mode 100644 src/test/java/it/gov/pagopa/atmlayer/service/model/entity/UserProfileTest.java create mode 100644 src/test/java/it/gov/pagopa/atmlayer/service/model/enumeration/UserProfileEnumTest.java create mode 100644 src/test/java/it/gov/pagopa/atmlayer/service/model/mapper/UserProfileMapperTest.java create mode 100644 src/test/java/it/gov/pagopa/atmlayer/service/model/repository/UserProfileRepositoryTest.java create mode 100644 src/test/java/it/gov/pagopa/atmlayer/service/model/resource/UserProfileResourceTest.java create mode 100644 src/test/java/it/gov/pagopa/atmlayer/service/model/service/impl/UserProfileServiceImplTest.java create mode 100644 src/test/java/it/gov/pagopa/atmlayer/service/model/validators/UserProfileValidatorTest.java diff --git a/src/test/java/it/gov/pagopa/atmlayer/service/model/dto/UserProfileCreationDtoTest.java b/src/test/java/it/gov/pagopa/atmlayer/service/model/dto/UserProfileCreationDtoTest.java new file mode 100644 index 00000000..d05a5e7c --- /dev/null +++ b/src/test/java/it/gov/pagopa/atmlayer/service/model/dto/UserProfileCreationDtoTest.java @@ -0,0 +1,33 @@ +package it.gov.pagopa.atmlayer.service.model.dto; + +import io.quarkus.test.junit.QuarkusTest; +import it.gov.pagopa.atmlayer.service.model.enumeration.UserProfileEnum; +import org.junit.jupiter.api.Test; + +import java.sql.Timestamp; + +import static org.junit.jupiter.api.Assertions.*; + +@QuarkusTest +class UserProfileCreationDtoTest { + + @Test + void testConstructor(){ + UserProfileCreationDto dto = new UserProfileCreationDto(); + dto.setUserId("email@domain.com"); + dto.setProfile(3); + dto.setCreatedAt(new Timestamp(System.currentTimeMillis())); + dto.setLastUpdatedAt(new Timestamp(System.currentTimeMillis())); + assertEquals("email@domain.com", dto.getUserId()); + assertEquals(UserProfileEnum.ADMIN.getValue(), dto.getProfile()); + } + + @Test + void testEquals() { + UserProfileCreationDto dto1 = new UserProfileCreationDto(); + UserProfileCreationDto dto2 = new UserProfileCreationDto(); + assertEquals(dto1, dto2); + int expectedHashCodeResult = dto1.hashCode(); + assertEquals(expectedHashCodeResult, dto2.hashCode()); + } +} \ No newline at end of file diff --git a/src/test/java/it/gov/pagopa/atmlayer/service/model/dto/UserProfileDtoTest.java b/src/test/java/it/gov/pagopa/atmlayer/service/model/dto/UserProfileDtoTest.java new file mode 100644 index 00000000..9673a73c --- /dev/null +++ b/src/test/java/it/gov/pagopa/atmlayer/service/model/dto/UserProfileDtoTest.java @@ -0,0 +1,39 @@ +package it.gov.pagopa.atmlayer.service.model.dto; + +import io.quarkus.test.junit.QuarkusTest; +import it.gov.pagopa.atmlayer.service.model.enumeration.UserProfileEnum; +import org.junit.jupiter.api.Test; + +import java.sql.Timestamp; + +import static org.junit.jupiter.api.Assertions.*; + +@QuarkusTest +class UserProfileDtoTest { + + @Test + void testConstructor() { + UserProfileDto dto = new UserProfileDto(); + dto.setUserId("user@domain.com"); + dto.setProfile(UserProfileEnum.GUEST); + dto.setVisible(true); + dto.setAdmin(false); + dto.setEditable(false); + dto.setCreatedAt(new Timestamp(System.currentTimeMillis())); + dto.setLastUpdatedAt(new Timestamp(System.currentTimeMillis())); + assertEquals("user@domain.com", dto.getUserId()); + assertEquals(UserProfileEnum.GUEST, dto.getProfile()); + assertEquals(true, dto.getVisible()); + assertEquals(false, dto.getAdmin()); + assertEquals(false, dto.getEditable()); + } + + @Test + void testEquals(){ + UserProfileDto dto1 = new UserProfileDto(); + UserProfileDto dto2 = new UserProfileDto(); + assertEquals(dto1, dto2); + int expectedHashCodeResult = dto1.hashCode(); + assertEquals(expectedHashCodeResult, dto2.hashCode()); + } +} \ No newline at end of file diff --git a/src/test/java/it/gov/pagopa/atmlayer/service/model/entity/UserProfileTest.java b/src/test/java/it/gov/pagopa/atmlayer/service/model/entity/UserProfileTest.java new file mode 100644 index 00000000..8815113a --- /dev/null +++ b/src/test/java/it/gov/pagopa/atmlayer/service/model/entity/UserProfileTest.java @@ -0,0 +1,40 @@ +package it.gov.pagopa.atmlayer.service.model.entity; + +import it.gov.pagopa.atmlayer.service.model.enumeration.UserProfileEnum; +import org.junit.jupiter.api.Test; + +import java.sql.Timestamp; + +import static org.junit.jupiter.api.Assertions.*; + +class UserProfileTest { + + @Test + void testSetGetUserId() { + UserProfile user = new UserProfile(); + user.setUserId("user@domain.com"); + assertEquals("user@domain.com", user.getUserId()); + assertEquals("user@domain.com", user.getUserId()); + } + + @Test + void testSetGetProfile() { + UserProfile user = new UserProfile(); + user.setProfile(1); + assertEquals(UserProfileEnum.GUEST.getValue(), user.getProfile()); + } + + @Test + void testSetGetCreatedAt(){ + UserProfile user = new UserProfile(); + user.setCreatedAt(new Timestamp(System.currentTimeMillis())); + assertEquals(new Timestamp(System.currentTimeMillis()), user.getCreatedAt()); + } + + @Test + void testSetGetLastUpdatedAt(){ + UserProfile user = new UserProfile(); + user.setLastUpdatedAt(new Timestamp(System.currentTimeMillis())); + assertEquals(new Timestamp(System.currentTimeMillis()), user.getLastUpdatedAt()); + } +} \ No newline at end of file diff --git a/src/test/java/it/gov/pagopa/atmlayer/service/model/enumeration/UserProfileEnumTest.java b/src/test/java/it/gov/pagopa/atmlayer/service/model/enumeration/UserProfileEnumTest.java new file mode 100644 index 00000000..94eabbdc --- /dev/null +++ b/src/test/java/it/gov/pagopa/atmlayer/service/model/enumeration/UserProfileEnumTest.java @@ -0,0 +1,24 @@ +package it.gov.pagopa.atmlayer.service.model.enumeration; + +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +@QuarkusTest +class UserProfileEnumTest { + + @Test + void testGetValue() { + assertEquals(1, UserProfileEnum.GUEST.getValue()); + assertEquals(2, UserProfileEnum.OPERATOR.getValue()); + assertEquals(3, UserProfileEnum.ADMIN.getValue()); + } + + @Test + void testValueOf() { + assertEquals(UserProfileEnum.valueOf(1), UserProfileEnum.GUEST); + assertEquals(UserProfileEnum.valueOf(2), UserProfileEnum.OPERATOR); + assertEquals(UserProfileEnum.valueOf(3), UserProfileEnum.ADMIN); + } +} \ No newline at end of file diff --git a/src/test/java/it/gov/pagopa/atmlayer/service/model/mapper/UserProfileMapperTest.java b/src/test/java/it/gov/pagopa/atmlayer/service/model/mapper/UserProfileMapperTest.java new file mode 100644 index 00000000..1a296485 --- /dev/null +++ b/src/test/java/it/gov/pagopa/atmlayer/service/model/mapper/UserProfileMapperTest.java @@ -0,0 +1,107 @@ +package it.gov.pagopa.atmlayer.service.model.mapper; + +import io.quarkus.test.junit.QuarkusTest; +import it.gov.pagopa.atmlayer.service.model.dto.UserProfileCreationDto; +import it.gov.pagopa.atmlayer.service.model.dto.UserProfileDto; +import it.gov.pagopa.atmlayer.service.model.entity.UserProfile; +import it.gov.pagopa.atmlayer.service.model.enumeration.UserProfileEnum; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +@QuarkusTest +class UserProfileMapperTest { + private UserProfileMapper userProfileMapper; + + @BeforeEach + public void setUp() { + userProfileMapper = new UserProfileMapperImpl(); + } + + @Test + void testToUserProfileDto() { + UserProfile userProfile = mock(UserProfile.class); + Timestamp create = new Timestamp(System.currentTimeMillis()); + Timestamp update = new Timestamp(System.currentTimeMillis()); + + when(userProfile.getUserId()).thenReturn("email@domain.com"); + when(userProfile.getProfile()).thenReturn(UserProfileEnum.GUEST.getValue()); + when(userProfile.getCreatedAt()).thenReturn(create); + when(userProfile.getLastUpdatedAt()).thenReturn(update); + UserProfileDto userProfileDto = userProfileMapper.toUserProfileDto(userProfile); + + assertNotNull(userProfileDto); + assertEquals("email@domain.com", userProfileDto.getUserId()); + assertEquals(UserProfileEnum.GUEST, userProfileDto.getProfile()); + assertEquals(create, userProfileDto.getCreatedAt()); + assertEquals(update, userProfileDto.getLastUpdatedAt()); + } + + @Test + void testToUserProfile() { + UserProfileCreationDto userProfileDto = mock(UserProfileCreationDto.class); + + Timestamp create = new Timestamp(System.currentTimeMillis()); + Timestamp update = new Timestamp(System.currentTimeMillis()); + + when(userProfileDto.getUserId()).thenReturn("email@domain.com"); + when(userProfileDto.getProfile()).thenReturn(UserProfileEnum.GUEST.getValue()); + when(userProfileDto.getCreatedAt()).thenReturn(create); + when(userProfileDto.getLastUpdatedAt()).thenReturn(update); + UserProfile userProfile = userProfileMapper.toUserProfile(userProfileDto); + + assertNotNull(userProfile); + assertEquals("email@domain.com", userProfile.getUserId()); + assertEquals(UserProfileEnum.GUEST.getValue(), userProfile.getProfile()); + assertEquals(create, userProfile.getCreatedAt()); + assertEquals(update, userProfile.getLastUpdatedAt()); + } + + @Test + void testGetEnumValue() { + assertEquals(UserProfileEnum.valueOf(1), userProfileMapper.getEnumValue(1)); + assertEquals(UserProfileEnum.valueOf(2), userProfileMapper.getEnumValue(2)); + assertEquals(UserProfileEnum.valueOf(3), userProfileMapper.getEnumValue(3)); + } + + @Test + void testToUserProfileDtoWithProfileMapping() { + UserProfile userProfile = new UserProfile(); + UserProfileDto userProfileDto; + + userProfile.setProfile(UserProfileEnum.ADMIN.getValue()); + userProfileDto = userProfileMapper.toUserProfileDtoWithProfileMapping(userProfile); + assertEquals(true, userProfileDto.getAdmin()); + assertEquals(true, userProfileDto.getEditable()); + assertEquals(true, userProfileDto.getVisible()); + + userProfile.setProfile(UserProfileEnum.OPERATOR.getValue()); + userProfileDto = userProfileMapper.toUserProfileDtoWithProfileMapping(userProfile); + assertEquals(false, userProfileDto.getAdmin()); + assertEquals(true, userProfileDto.getEditable()); + assertEquals(true, userProfileDto.getVisible()); + + userProfile.setProfile(UserProfileEnum.GUEST.getValue()); + userProfileDto = userProfileMapper.toUserProfileDtoWithProfileMapping(userProfile); + assertEquals(false, userProfileDto.getAdmin()); + assertEquals(false, userProfileDto.getEditable()); + assertEquals(true, userProfileDto.getVisible()); + } + + @Test + void testToDtoList() { + UserProfile userProfile = mock(UserProfile.class); + List list = new ArrayList<>(); + list.add(userProfile); + List listDto = userProfileMapper.toDtoList(list); + assertNotNull(listDto); + assertEquals(list.size(), listDto.size()); + + } +} \ No newline at end of file diff --git a/src/test/java/it/gov/pagopa/atmlayer/service/model/repository/UserProfileRepositoryTest.java b/src/test/java/it/gov/pagopa/atmlayer/service/model/repository/UserProfileRepositoryTest.java new file mode 100644 index 00000000..6bf58f43 --- /dev/null +++ b/src/test/java/it/gov/pagopa/atmlayer/service/model/repository/UserProfileRepositoryTest.java @@ -0,0 +1,35 @@ +package it.gov.pagopa.atmlayer.service.model.repository; + +import io.quarkus.test.junit.QuarkusTest; +import io.smallrye.mutiny.Uni; +import it.gov.pagopa.atmlayer.service.model.entity.UserProfile; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.util.Collections; +import java.util.List; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; + +@QuarkusTest +class UserProfileRepositoryTest { + + @Test + void testFindUserId() { + + UserProfileRepository repository = Mockito.mock(UserProfileRepository.class); + UserProfile userProfile = new UserProfile(); + userProfile.setUserId("email@domain.com"); + userProfile.setProfile(1); + List mockResult = Collections.singletonList(userProfile); + + Mockito.when(repository.findUserId("email@domain.com")) + .thenReturn(Uni.createFrom().item(mockResult.get(0))); + + UserProfile result = repository.findUserId("email@domain.com").await().indefinitely(); + + assertThat(result.getUserId(), is(equalTo("email@domain.com"))); + } +} \ No newline at end of file diff --git a/src/test/java/it/gov/pagopa/atmlayer/service/model/resource/UserProfileResourceTest.java b/src/test/java/it/gov/pagopa/atmlayer/service/model/resource/UserProfileResourceTest.java new file mode 100644 index 00000000..573cfccd --- /dev/null +++ b/src/test/java/it/gov/pagopa/atmlayer/service/model/resource/UserProfileResourceTest.java @@ -0,0 +1,165 @@ +package it.gov.pagopa.atmlayer.service.model.resource; + +import io.quarkus.test.InjectMock; +import io.quarkus.test.junit.QuarkusTest; +import io.smallrye.mutiny.Uni; +import it.gov.pagopa.atmlayer.service.model.dto.UserProfileCreationDto; +import it.gov.pagopa.atmlayer.service.model.dto.UserProfileDto; +import it.gov.pagopa.atmlayer.service.model.entity.UserProfile; +import it.gov.pagopa.atmlayer.service.model.mapper.UserProfileMapper; +import it.gov.pagopa.atmlayer.service.model.service.UserProfileService; +import it.gov.pagopa.atmlayer.service.model.validators.UserProfileValidator; +import jakarta.ws.rs.core.MediaType; +import org.junit.jupiter.api.Test; + +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.List; + +import static io.restassured.RestAssured.given; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +@QuarkusTest +class UserProfileResourceTest { + + @InjectMock + UserProfileService userProfileService; + @InjectMock + UserProfileMapper userProfileMapper; + @InjectMock + UserProfileValidator userProfileValidator; + + @Test + void testFindByUserId() { + String userId = "email@domain.com"; + + UserProfile userProfile = new UserProfile(); + + when(userProfileService.findByUserId(any(String.class))).thenReturn( + Uni.createFrom().item(userProfile)); + UserProfileDto userDto = new UserProfileDto(); + when(userProfileMapper.toUserProfileDtoWithProfileMapping(any(UserProfile.class))).thenReturn(userDto); + + given() + .queryParam("userId", userId) + .when() + .get("/api/v1/model/users/search") + .then() + .statusCode(200); + + verify(userProfileService, times(1)).findByUserId(any(String.class)); + verify(userProfileMapper, times(1)).toUserProfileDtoWithProfileMapping(userProfile); + } + + @Test + void testGetAllUsersEmptyList() { + List list = new ArrayList<>(); + + when(userProfileService.getUsers()).thenReturn(Uni.createFrom().item(list)); + + given() + .when().get("/api/v1/model/users") + .then() + .statusCode(200); + + verify(userProfileService, times(1)).getUsers(); + } + + @Test + void testGetAllUsersList() { + List userProfileList = new ArrayList<>(); + UserProfile userProfile = new UserProfile(); + userProfileList.add(userProfile); + + List userProfileDtoList = new ArrayList<>(); + UserProfileDto userProfileDto = new UserProfileDto(); + userProfileDtoList.add(userProfileDto); + when(userProfileService.getUsers()).thenReturn(Uni.createFrom().item(userProfileList)); + when(userProfileMapper.toDtoList(any(ArrayList.class))).thenReturn(userProfileDtoList); + ArrayList result = given() + .when().get("/api/v1/model/users") + .then() + .statusCode(200) + .extract() + .body() + .as(ArrayList.class); + assertEquals(1, result.size()); + verify(userProfileService, times(1)).getUsers(); + verify(userProfileMapper, times(1)).toDtoList(userProfileList); + } + + @Test + void testCreateUser() { + UserProfile userProfile = new UserProfile(); + UserProfileDto userProfileDto = new UserProfileDto(); + UserProfileCreationDto userProfileCreationDto = new UserProfileCreationDto(); + userProfileCreationDto.setUserId("email@domain.com"); + userProfileCreationDto.setProfile(1); + userProfileCreationDto.setCreatedAt(new Timestamp(System.currentTimeMillis())); + userProfileCreationDto.setLastUpdatedAt(new Timestamp(System.currentTimeMillis())); + + when(userProfileMapper.toUserProfile(any(UserProfileCreationDto.class))) + .thenReturn(userProfile); + when(userProfileService.createUser(any(UserProfileCreationDto.class))) + .thenReturn(Uni.createFrom().item(userProfile)); + when(userProfileMapper.toUserProfileDto(any(UserProfile.class))) + .thenReturn(userProfileDto); + when(userProfileValidator.validateExistenceProfileType(1)) + .thenReturn(Uni.createFrom().voidItem()); + + UserProfileDto result = given() + .contentType(MediaType.APPLICATION_JSON) + .body(userProfileCreationDto) + .when().post("/api/v1/model/users") + .then() + .statusCode(200) + .extract().as(UserProfileDto.class); + + assertEquals(userProfileDto, result); + + } + + @Test + void testUpdateUser() { + UserProfile userProfile = new UserProfile(); + UserProfileDto userProfileDto = new UserProfileDto(); + UserProfileCreationDto userProfileCreationDto = new UserProfileCreationDto(); + userProfileCreationDto.setUserId("email@domain.com"); + userProfileCreationDto.setProfile(2); + userProfileCreationDto.setCreatedAt(new Timestamp(System.currentTimeMillis())); + userProfileCreationDto.setLastUpdatedAt(new Timestamp(System.currentTimeMillis())); + + when(userProfileService.updateUser(any(UserProfileCreationDto.class))) + .thenReturn(Uni.createFrom().item(userProfile)); + when(userProfileMapper.toUserProfileDto(any(UserProfile.class))).thenReturn(userProfileDto); + when(userProfileValidator.validateExistenceProfileType(1)) + .thenReturn(Uni.createFrom().voidItem()); + + UserProfileDto result = given() + .contentType(MediaType.APPLICATION_JSON) + .body(userProfileCreationDto) + .when().put("/api/v1/model/users") + .then() + .statusCode(200) + .extract().as(UserProfileDto.class); + + assertEquals(userProfileDto, result); + } + + @Test + void testDeleteUser() { + String userId = "email@domain.com"; + + when(userProfileService.deleteUser(any(String.class))) + .thenReturn(Uni.createFrom().voidItem()); + + given() + .contentType(MediaType.APPLICATION_JSON) + .queryParam("userId", userId) + .when().delete("/api/v1/model/users/search") + .then() + .statusCode(204); + } +} \ No newline at end of file diff --git a/src/test/java/it/gov/pagopa/atmlayer/service/model/service/impl/UserProfileServiceImplTest.java b/src/test/java/it/gov/pagopa/atmlayer/service/model/service/impl/UserProfileServiceImplTest.java new file mode 100644 index 00000000..55b65784 --- /dev/null +++ b/src/test/java/it/gov/pagopa/atmlayer/service/model/service/impl/UserProfileServiceImplTest.java @@ -0,0 +1,64 @@ +package it.gov.pagopa.atmlayer.service.model.service.impl; + +import io.quarkus.test.InjectMock; +import io.quarkus.test.junit.QuarkusTest; +import io.smallrye.mutiny.Uni; +import io.smallrye.mutiny.helpers.test.UniAssertSubscriber; +import it.gov.pagopa.atmlayer.service.model.dto.UserProfileCreationDto; +import it.gov.pagopa.atmlayer.service.model.entity.UserProfile; +import it.gov.pagopa.atmlayer.service.model.exception.AtmLayerException; +import it.gov.pagopa.atmlayer.service.model.mapper.UserProfileMapper; +import it.gov.pagopa.atmlayer.service.model.repository.UserProfileRepository; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +@QuarkusTest +class UserProfileServiceImplTest { + + @InjectMocks + UserProfileServiceImpl userProfileService; + @Mock + UserProfileRepository userProfileRepository; + + @InjectMock + UserProfileMapper userProfileMapper; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + void testCreateUserAlreadyExist() { + String userId = "email@domain.com"; + + UserProfile userProfile = new UserProfile(); + userProfile.setUserId(userId); + UserProfileCreationDto userProfileDto = new UserProfileCreationDto(); + userProfileDto.setUserId(userId); + + when(userProfileMapper.toUserProfile(any(UserProfileCreationDto.class))).thenReturn(userProfile); + when(userProfileRepository.findUserId(any(String.class))).thenReturn(Uni.createFrom().item(userProfile)); + when(userProfileService.findByUserId(any(String.class))).thenReturn(Uni.createFrom().item(userProfile)); + userProfileService.createUser(userProfileDto) + .subscribe().withSubscriber(UniAssertSubscriber.create()) + .assertFailedWith(AtmLayerException.class, "A user with the same id already exists"); + } + + @Test + void testDeleteUserNotExist() { + String userId = "email@domain.com"; + UserProfile userProfile = new UserProfile(); + + when(userProfileRepository.findUserId(any(String.class))).thenReturn(Uni.createFrom().nullItem()); + userProfileService.deleteUser(userId) + .subscribe().withSubscriber(UniAssertSubscriber.create()) + .assertFailedWith(AtmLayerException.class, "A user with the id email@domain.com not exists"); + } +} \ No newline at end of file diff --git a/src/test/java/it/gov/pagopa/atmlayer/service/model/validators/UserProfileValidatorTest.java b/src/test/java/it/gov/pagopa/atmlayer/service/model/validators/UserProfileValidatorTest.java new file mode 100644 index 00000000..b0abf1d3 --- /dev/null +++ b/src/test/java/it/gov/pagopa/atmlayer/service/model/validators/UserProfileValidatorTest.java @@ -0,0 +1,34 @@ +package it.gov.pagopa.atmlayer.service.model.validators; + +import io.quarkus.test.junit.QuarkusTest; +import io.smallrye.mutiny.Uni; +import it.gov.pagopa.atmlayer.service.model.enumeration.UserProfileEnum; +import it.gov.pagopa.atmlayer.service.model.exception.AtmLayerException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.MockitoAnnotations; + +import static org.junit.jupiter.api.Assertions.*; + +@QuarkusTest +class UserProfileValidatorTest { + @InjectMocks + private UserProfileValidator validator; + + @BeforeEach + void init() { + MockitoAnnotations.openMocks(this); + } + + @Test + void testValidateExistenceProfileType() { + Uni resultGuest = validator.validateExistenceProfileType(UserProfileEnum.GUEST.getValue()); + assertDoesNotThrow(() -> resultGuest.await().indefinitely()); + Uni resultOperator = validator.validateExistenceProfileType(UserProfileEnum.OPERATOR.getValue()); + assertDoesNotThrow(() -> resultOperator.await().indefinitely()); + Uni resultAdmin = validator.validateExistenceProfileType(UserProfileEnum.OPERATOR.getValue()); + assertDoesNotThrow(() -> resultAdmin.await().indefinitely()); + Throwable exception = assertThrowsExactly(AtmLayerException.class, () -> validator.validateExistenceProfileType(4)); + } +} \ No newline at end of file From bd4d1bfb4de35ac8a6d833230df23b7ae0824e3e Mon Sep 17 00:00:00 2001 From: stefano Date: Wed, 7 Feb 2024 17:30:19 +0100 Subject: [PATCH 09/10] refactoring --- .../service/model/dto/UserProfileAllDto.java | 22 +++++++++ .../model/dto/UserProfileCreationDto.java | 6 +++ .../service/model/dto/UserProfileDto.java | 4 ++ .../model/mapper/ResourceFileMapper.java | 4 -- .../model/mapper/UserProfileMapper.java | 12 +++++ .../model/resource/UserProfileResource.java | 45 ++++++++++++++++--- .../model/dto/UserProfileCreationDtoTest.java | 4 +- .../model/mapper/UserProfileMapperTest.java | 1 + .../resource/UserProfileResourceTest.java | 27 +++++------ 9 files changed, 99 insertions(+), 26 deletions(-) create mode 100644 src/main/java/it/gov/pagopa/atmlayer/service/model/dto/UserProfileAllDto.java diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/dto/UserProfileAllDto.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/dto/UserProfileAllDto.java new file mode 100644 index 00000000..2662445f --- /dev/null +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/dto/UserProfileAllDto.java @@ -0,0 +1,22 @@ +package it.gov.pagopa.atmlayer.service.model.dto; + +import com.fasterxml.jackson.annotation.JsonInclude; +import it.gov.pagopa.atmlayer.service.model.enumeration.UserProfileEnum; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.eclipse.microprofile.openapi.annotations.media.Schema; + +import java.sql.Timestamp; + +@NoArgsConstructor +@Data +@JsonInclude(JsonInclude.Include.NON_NULL) +public class UserProfileAllDto { + @Schema(example = "email@domain.com") + private String userId; + private UserProfileEnum profile; + @Schema(example = "2024-02-07T11:38:58.445+00:00") + private Timestamp createdAt; + @Schema(example = "2024-02-07T11:38:58.445+00:00") + private Timestamp lastUpdatedAt; +} diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/dto/UserProfileCreationDto.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/dto/UserProfileCreationDto.java index d4964cad..77facde7 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/dto/UserProfileCreationDto.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/dto/UserProfileCreationDto.java @@ -1,9 +1,11 @@ package it.gov.pagopa.atmlayer.service.model.dto; +import it.gov.pagopa.atmlayer.service.model.enumeration.UserProfileEnum; import jakarta.validation.constraints.Email; import jakarta.validation.constraints.NotNull; import lombok.Data; import lombok.NoArgsConstructor; +import org.eclipse.microprofile.openapi.annotations.media.Schema; import java.sql.Timestamp; @@ -13,9 +15,13 @@ public class UserProfileCreationDto { @NotNull(message = "is required") @Email(message = "must be an email address in the correct format") + @Schema(required = true, example = "email@domain.com") private String userId; @NotNull(message = "is required") + @Schema(required = true, description = "1 = GUEST, 2 = OPERATOR, 3 = ADMIN") private Integer profile; + @Schema(hidden = true) private Timestamp createdAt; + @Schema(hidden = true) private Timestamp lastUpdatedAt; } diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/dto/UserProfileDto.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/dto/UserProfileDto.java index 31fe26a1..303ac308 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/dto/UserProfileDto.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/dto/UserProfileDto.java @@ -4,6 +4,7 @@ import it.gov.pagopa.atmlayer.service.model.enumeration.UserProfileEnum; import lombok.Data; import lombok.NoArgsConstructor; +import org.eclipse.microprofile.openapi.annotations.media.Schema; import java.sql.Timestamp; @@ -11,11 +12,14 @@ @Data @JsonInclude(JsonInclude.Include.NON_NULL) public class UserProfileDto { + @Schema(example = "email@domain.com") private String userId; private UserProfileEnum profile; private Boolean visible; private Boolean editable; private Boolean admin; + @Schema(example = "2024-02-07T11:38:58.445+00:00") private Timestamp createdAt; + @Schema(example = "2024-02-07T11:38:58.445+00:00") private Timestamp lastUpdatedAt; } diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/mapper/ResourceFileMapper.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/mapper/ResourceFileMapper.java index c48bda25..fb5cad2c 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/mapper/ResourceFileMapper.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/mapper/ResourceFileMapper.java @@ -2,13 +2,9 @@ import it.gov.pagopa.atmlayer.service.model.entity.ResourceFile; import it.gov.pagopa.atmlayer.service.model.model.ResourceFileDTO; -import it.gov.pagopa.atmlayer.service.model.model.ResourceFrontEndDTO; -import org.mapstruct.IterableMapping; import org.mapstruct.Mapper; import org.mapstruct.Mapping; -import org.mapstruct.Named; -import java.util.List; @Mapper(componentModel = "cdi") public interface ResourceFileMapper { diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/mapper/UserProfileMapper.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/mapper/UserProfileMapper.java index e392d616..08fd7474 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/mapper/UserProfileMapper.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/mapper/UserProfileMapper.java @@ -1,5 +1,6 @@ package it.gov.pagopa.atmlayer.service.model.mapper; +import it.gov.pagopa.atmlayer.service.model.dto.UserProfileAllDto; import it.gov.pagopa.atmlayer.service.model.dto.UserProfileCreationDto; import it.gov.pagopa.atmlayer.service.model.dto.UserProfileDto; import it.gov.pagopa.atmlayer.service.model.entity.UserProfile; @@ -18,6 +19,13 @@ public abstract class UserProfileMapper { @Mapping(source = "userProfile.createdAt", target = "createdAt") @Mapping(source = "userProfile.lastUpdatedAt", target = "lastUpdatedAt") public abstract UserProfileDto toUserProfileDto(UserProfile userProfile); + + @Mapping(source = "userProfile.userId", target = "userId") + @Mapping(expression = "java(getEnumValue(userProfile.getProfile()))", target = "profile") + @Mapping(source = "userProfile.createdAt", target = "createdAt") + @Mapping(source = "userProfile.lastUpdatedAt", target = "lastUpdatedAt") + public abstract UserProfileAllDto toUserProfileAllDto(UserProfile userProfile); + @Mapping(source = "userProfile.userId", target = "userId") @Mapping(source = "userProfile.profile", target = "profile") @Mapping(source = "userProfile.createdAt", target = "createdAt") @@ -53,4 +61,8 @@ public UserProfileDto toUserProfileDtoWithProfileMapping(UserProfile userProfile public List toDtoList(List list){ return list.stream().map( x -> this.toUserProfileDto(x)).toList(); } + + public List toDtoAllList(List list){ + return list.stream().map( x -> this.toUserProfileAllDto(x)).toList(); + } } diff --git a/src/main/java/it/gov/pagopa/atmlayer/service/model/resource/UserProfileResource.java b/src/main/java/it/gov/pagopa/atmlayer/service/model/resource/UserProfileResource.java index d6ccfdb4..79066c04 100644 --- a/src/main/java/it/gov/pagopa/atmlayer/service/model/resource/UserProfileResource.java +++ b/src/main/java/it/gov/pagopa/atmlayer/service/model/resource/UserProfileResource.java @@ -3,6 +3,7 @@ import io.smallrye.common.annotation.NonBlocking; import io.smallrye.mutiny.Uni; import io.smallrye.mutiny.unchecked.Unchecked; +import it.gov.pagopa.atmlayer.service.model.dto.UserProfileAllDto; import it.gov.pagopa.atmlayer.service.model.dto.UserProfileCreationDto; import it.gov.pagopa.atmlayer.service.model.dto.UserProfileDto; import it.gov.pagopa.atmlayer.service.model.mapper.UserProfileMapper; @@ -15,7 +16,13 @@ import jakarta.ws.rs.*; import jakarta.ws.rs.core.MediaType; import lombok.extern.slf4j.Slf4j; +import org.eclipse.microprofile.openapi.annotations.Operation; +import org.eclipse.microprofile.openapi.annotations.media.Content; +import org.eclipse.microprofile.openapi.annotations.media.Schema; import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody; +import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; +import org.eclipse.microprofile.openapi.annotations.responses.APIResponseSchema; +import org.eclipse.microprofile.openapi.annotations.responses.APIResponses; import org.eclipse.microprofile.openapi.annotations.tags.Tag; import java.util.List; @@ -36,6 +43,11 @@ public class UserProfileResource { @GET @Path("/search") @Produces(MediaType.APPLICATION_JSON) + @APIResponseSchema(value = UserProfileDto.class) + @APIResponses(value = { + @APIResponse(responseCode = "200", description = "Successfully retrieved"), + @APIResponse(responseCode = "404", description = "Not found") + }) public Uni findByUserId(@NotNull @QueryParam("userId") String userId) { return this.userProfileService.findByUserId(userId) .onItem() @@ -47,14 +59,18 @@ public Uni findByUserId(@NotNull @QueryParam("userId") String us @GET @Produces(MediaType.APPLICATION_JSON) - public Uni> getUsers(){ + @APIResponses(value = { + @APIResponse(responseCode = "200", description = "Successfully retrieved"), + @APIResponse(responseCode = "404", description = "Not found") + }) + public Uni> getUsers(){ return this.userProfileService.getUsers() .onItem() .transform(Unchecked.function(list -> { if (list.isEmpty()) { log.info("No User profiles saved in database"); } - return userProfileMapper.toDtoList(list); + return userProfileMapper.toDtoAllList(list); })); } @@ -62,17 +78,28 @@ public Uni> getUsers(){ @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @NonBlocking - public Uni createUser( - @RequestBody(required = true) @Valid UserProfileCreationDto user){ + @APIResponses(value = { + @APIResponse(responseCode = "200", description = "Successfully created"), + @APIResponse(responseCode = "400", description = "User already exist") + }) + public Uni createUser( + @RequestBody(required = true, content = { + @Content(schema = @Schema(implementation = UserProfileCreationDto.class)) + }) @Valid UserProfileCreationDto user){ return this.userProfileValidator.validateExistenceProfileType(user.getProfile()) .onItem() .transformToUni((x) -> this.userProfileService.createUser(user) .onItem() - .transformToUni(Unchecked.function(u -> Uni.createFrom().item(this.userProfileMapper.toUserProfileDto(u))))); + .transformToUni(Unchecked.function(u -> Uni.createFrom().item(this.userProfileMapper.toUserProfileAllDto(u))))); } @DELETE @Path("/search") + @Operation() + @APIResponses(value = { + @APIResponse(responseCode = "204", description = "Successfully deleted"), + @APIResponse(responseCode = "404", description = "Not found") + }) public Uni deleteUser(@NotNull @QueryParam("userId") String userId) { return this.userProfileService.deleteUser(userId); } @@ -80,12 +107,16 @@ public Uni deleteUser(@NotNull @QueryParam("userId") String userId) { @PUT @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) - public Uni updateUser( + @APIResponses(value = { + @APIResponse(responseCode = "200", description = "Successfully updated"), + @APIResponse(responseCode = "404", description = "Not found") + }) + public Uni updateUser( @RequestBody(required = true) @Valid UserProfileCreationDto user) { return this.userProfileValidator.validateExistenceProfileType(user.getProfile()) .onItem() .transformToUni((x) -> this.userProfileService.updateUser(user) .onItem() - .transformToUni(Unchecked.function(u -> Uni.createFrom().item(this.userProfileMapper.toUserProfileDto(u))))); + .transformToUni(Unchecked.function(u -> Uni.createFrom().item(this.userProfileMapper.toUserProfileAllDto(u))))); } } diff --git a/src/test/java/it/gov/pagopa/atmlayer/service/model/dto/UserProfileCreationDtoTest.java b/src/test/java/it/gov/pagopa/atmlayer/service/model/dto/UserProfileCreationDtoTest.java index d05a5e7c..cd4e0d81 100644 --- a/src/test/java/it/gov/pagopa/atmlayer/service/model/dto/UserProfileCreationDtoTest.java +++ b/src/test/java/it/gov/pagopa/atmlayer/service/model/dto/UserProfileCreationDtoTest.java @@ -16,8 +16,8 @@ void testConstructor(){ UserProfileCreationDto dto = new UserProfileCreationDto(); dto.setUserId("email@domain.com"); dto.setProfile(3); - dto.setCreatedAt(new Timestamp(System.currentTimeMillis())); - dto.setLastUpdatedAt(new Timestamp(System.currentTimeMillis())); + //dto.setCreatedAt(new Timestamp(System.currentTimeMillis())); + //dto.setLastUpdatedAt(new Timestamp(System.currentTimeMillis())); assertEquals("email@domain.com", dto.getUserId()); assertEquals(UserProfileEnum.ADMIN.getValue(), dto.getProfile()); } diff --git a/src/test/java/it/gov/pagopa/atmlayer/service/model/mapper/UserProfileMapperTest.java b/src/test/java/it/gov/pagopa/atmlayer/service/model/mapper/UserProfileMapperTest.java index 1a296485..57e5350b 100644 --- a/src/test/java/it/gov/pagopa/atmlayer/service/model/mapper/UserProfileMapperTest.java +++ b/src/test/java/it/gov/pagopa/atmlayer/service/model/mapper/UserProfileMapperTest.java @@ -54,6 +54,7 @@ void testToUserProfile() { when(userProfileDto.getProfile()).thenReturn(UserProfileEnum.GUEST.getValue()); when(userProfileDto.getCreatedAt()).thenReturn(create); when(userProfileDto.getLastUpdatedAt()).thenReturn(update); + UserProfile userProfile = userProfileMapper.toUserProfile(userProfileDto); assertNotNull(userProfile); diff --git a/src/test/java/it/gov/pagopa/atmlayer/service/model/resource/UserProfileResourceTest.java b/src/test/java/it/gov/pagopa/atmlayer/service/model/resource/UserProfileResourceTest.java index 573cfccd..2e31e0e0 100644 --- a/src/test/java/it/gov/pagopa/atmlayer/service/model/resource/UserProfileResourceTest.java +++ b/src/test/java/it/gov/pagopa/atmlayer/service/model/resource/UserProfileResourceTest.java @@ -3,6 +3,7 @@ import io.quarkus.test.InjectMock; import io.quarkus.test.junit.QuarkusTest; import io.smallrye.mutiny.Uni; +import it.gov.pagopa.atmlayer.service.model.dto.UserProfileAllDto; import it.gov.pagopa.atmlayer.service.model.dto.UserProfileCreationDto; import it.gov.pagopa.atmlayer.service.model.dto.UserProfileDto; import it.gov.pagopa.atmlayer.service.model.entity.UserProfile; @@ -73,11 +74,11 @@ void testGetAllUsersList() { UserProfile userProfile = new UserProfile(); userProfileList.add(userProfile); - List userProfileDtoList = new ArrayList<>(); - UserProfileDto userProfileDto = new UserProfileDto(); + List userProfileDtoList = new ArrayList<>(); + UserProfileAllDto userProfileDto = new UserProfileAllDto(); userProfileDtoList.add(userProfileDto); when(userProfileService.getUsers()).thenReturn(Uni.createFrom().item(userProfileList)); - when(userProfileMapper.toDtoList(any(ArrayList.class))).thenReturn(userProfileDtoList); + when(userProfileMapper.toDtoAllList(any(ArrayList.class))).thenReturn(userProfileDtoList); ArrayList result = given() .when().get("/api/v1/model/users") .then() @@ -87,13 +88,13 @@ void testGetAllUsersList() { .as(ArrayList.class); assertEquals(1, result.size()); verify(userProfileService, times(1)).getUsers(); - verify(userProfileMapper, times(1)).toDtoList(userProfileList); + verify(userProfileMapper, times(1)).toDtoAllList(userProfileList); } @Test void testCreateUser() { UserProfile userProfile = new UserProfile(); - UserProfileDto userProfileDto = new UserProfileDto(); + UserProfileAllDto userProfileDto = new UserProfileAllDto(); UserProfileCreationDto userProfileCreationDto = new UserProfileCreationDto(); userProfileCreationDto.setUserId("email@domain.com"); userProfileCreationDto.setProfile(1); @@ -104,18 +105,18 @@ void testCreateUser() { .thenReturn(userProfile); when(userProfileService.createUser(any(UserProfileCreationDto.class))) .thenReturn(Uni.createFrom().item(userProfile)); - when(userProfileMapper.toUserProfileDto(any(UserProfile.class))) + when(userProfileMapper.toUserProfileAllDto(any(UserProfile.class))) .thenReturn(userProfileDto); when(userProfileValidator.validateExistenceProfileType(1)) .thenReturn(Uni.createFrom().voidItem()); - UserProfileDto result = given() + UserProfileAllDto result = given() .contentType(MediaType.APPLICATION_JSON) .body(userProfileCreationDto) .when().post("/api/v1/model/users") .then() .statusCode(200) - .extract().as(UserProfileDto.class); + .extract().as(UserProfileAllDto.class); assertEquals(userProfileDto, result); @@ -124,26 +125,26 @@ void testCreateUser() { @Test void testUpdateUser() { UserProfile userProfile = new UserProfile(); - UserProfileDto userProfileDto = new UserProfileDto(); + UserProfileAllDto userProfileDto = new UserProfileAllDto(); UserProfileCreationDto userProfileCreationDto = new UserProfileCreationDto(); userProfileCreationDto.setUserId("email@domain.com"); userProfileCreationDto.setProfile(2); - userProfileCreationDto.setCreatedAt(new Timestamp(System.currentTimeMillis())); + userProfileCreationDto.setCreatedAt(new Timestamp(System.currentTimeMillis())); userProfileCreationDto.setLastUpdatedAt(new Timestamp(System.currentTimeMillis())); when(userProfileService.updateUser(any(UserProfileCreationDto.class))) .thenReturn(Uni.createFrom().item(userProfile)); - when(userProfileMapper.toUserProfileDto(any(UserProfile.class))).thenReturn(userProfileDto); + when(userProfileMapper.toUserProfileAllDto(any(UserProfile.class))).thenReturn(userProfileDto); when(userProfileValidator.validateExistenceProfileType(1)) .thenReturn(Uni.createFrom().voidItem()); - UserProfileDto result = given() + UserProfileAllDto result = given() .contentType(MediaType.APPLICATION_JSON) .body(userProfileCreationDto) .when().put("/api/v1/model/users") .then() .statusCode(200) - .extract().as(UserProfileDto.class); + .extract().as(UserProfileAllDto.class); assertEquals(userProfileDto, result); } From c6fe9fce1f4add94aaa6bfa8761e43cb52485b83 Mon Sep 17 00:00:00 2001 From: stefano Date: Wed, 7 Feb 2024 18:17:55 +0100 Subject: [PATCH 10/10] fix tests --- .../model/mapper/UserProfileMapperTest.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/test/java/it/gov/pagopa/atmlayer/service/model/mapper/UserProfileMapperTest.java b/src/test/java/it/gov/pagopa/atmlayer/service/model/mapper/UserProfileMapperTest.java index 57e5350b..44e8c84a 100644 --- a/src/test/java/it/gov/pagopa/atmlayer/service/model/mapper/UserProfileMapperTest.java +++ b/src/test/java/it/gov/pagopa/atmlayer/service/model/mapper/UserProfileMapperTest.java @@ -1,6 +1,7 @@ package it.gov.pagopa.atmlayer.service.model.mapper; import io.quarkus.test.junit.QuarkusTest; +import it.gov.pagopa.atmlayer.service.model.dto.UserProfileAllDto; import it.gov.pagopa.atmlayer.service.model.dto.UserProfileCreationDto; import it.gov.pagopa.atmlayer.service.model.dto.UserProfileDto; import it.gov.pagopa.atmlayer.service.model.entity.UserProfile; @@ -43,6 +44,25 @@ void testToUserProfileDto() { assertEquals(update, userProfileDto.getLastUpdatedAt()); } + @Test + void testToUserProfileDtoAll() { + UserProfile userProfile = mock(UserProfile.class); + Timestamp create = new Timestamp(System.currentTimeMillis()); + Timestamp update = new Timestamp(System.currentTimeMillis()); + + when(userProfile.getUserId()).thenReturn("email@domain.com"); + when(userProfile.getProfile()).thenReturn(UserProfileEnum.GUEST.getValue()); + when(userProfile.getCreatedAt()).thenReturn(create); + when(userProfile.getLastUpdatedAt()).thenReturn(update); + UserProfileAllDto userProfileDto = userProfileMapper.toUserProfileAllDto(userProfile); + + assertNotNull(userProfileDto); + assertEquals("email@domain.com", userProfileDto.getUserId()); + assertEquals(UserProfileEnum.GUEST, userProfileDto.getProfile()); + assertEquals(create, userProfileDto.getCreatedAt()); + assertEquals(update, userProfileDto.getLastUpdatedAt()); + } + @Test void testToUserProfile() { UserProfileCreationDto userProfileDto = mock(UserProfileCreationDto.class); @@ -103,6 +123,15 @@ void testToDtoList() { List listDto = userProfileMapper.toDtoList(list); assertNotNull(listDto); assertEquals(list.size(), listDto.size()); + } + @Test + void testToDtoAllList() { + UserProfile userProfile = mock(UserProfile.class); + List list = new ArrayList<>(); + list.add(userProfile); + List listDto = userProfileMapper.toDtoAllList(list); + assertNotNull(listDto); + assertEquals(list.size(), listDto.size()); } } \ No newline at end of file