From 0f787bad30d53f8f7186d566b36943bb46533ea9 Mon Sep 17 00:00:00 2001 From: flaminiaScarciofolo Date: Fri, 18 Oct 2024 15:57:35 +0200 Subject: [PATCH 1/4] added custom codec to test date --- .../UserInstitutionCdcServiceTest.java | 9 +++-- apps/user-ms/src/main/docs/openapi.json | 20 +++++------ apps/user-ms/src/main/docs/openapi.yaml | 20 +++++------ .../pagopa/selfcare/user/conf/DateCodec.java | 35 +++++++++++++++++++ .../selfcare/user/conf/MongoConfig.java | 19 ++++++++++ .../user/controller/EventsController.java | 4 +-- .../controller/InstitutionController.java | 4 +-- .../response/OnboardedProductResponse.java | 6 ++-- .../response/UserNotificationResponse.java | 6 ++-- .../product/OnboardedProductWithActions.java | 6 ++-- .../user/mapper/OnboardedProductMapper.java | 8 ++--- .../user/mapper/UserInstitutionMapper.java | 4 +-- .../user/service/UserInstitutionService.java | 10 +++--- .../UserInstitutionServiceDefault.java | 19 +++++----- .../selfcare/user/service/UserService.java | 6 ++-- .../user/service/UserServiceImpl.java | 16 ++++----- .../pagopa/selfcare/user/util/QueryUtils.java | 10 +++--- .../user/controller/EventsControllerTest.java | 4 +-- .../controller/InstitutionControllerTest.java | 14 ++++---- .../service/UserInstitutionServiceTest.java | 8 ++--- .../user/service/UserServiceTest.java | 10 +++--- .../user/model/UserNotificationToSend.java | 6 ++-- .../pagopa/selfcare/user/UserUtilsTest.java | 9 +++-- .../OnboardedProduct.java | 6 ++-- 24 files changed, 160 insertions(+), 99 deletions(-) create mode 100644 apps/user-ms/src/main/java/it/pagopa/selfcare/user/conf/DateCodec.java create mode 100644 apps/user-ms/src/main/java/it/pagopa/selfcare/user/conf/MongoConfig.java diff --git a/apps/user-cdc/src/test/java/it/pagopa/selfcare/user/event/service/UserInstitutionCdcServiceTest.java b/apps/user-cdc/src/test/java/it/pagopa/selfcare/user/event/service/UserInstitutionCdcServiceTest.java index 5a2023a9..f5d29ed6 100644 --- a/apps/user-cdc/src/test/java/it/pagopa/selfcare/user/event/service/UserInstitutionCdcServiceTest.java +++ b/apps/user-cdc/src/test/java/it/pagopa/selfcare/user/event/service/UserInstitutionCdcServiceTest.java @@ -21,7 +21,10 @@ import org.openapi.quarkus.user_registry_json.api.UserApi; import org.openapi.quarkus.user_registry_json.model.UserResource; -import java.time.LocalDateTime; +import java.time.LocalDate; +import java.time.LocalTime; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; import java.util.List; import static it.pagopa.selfcare.user.event.UserInstitutionCdcService.USERS_FIELD_LIST_WITHOUT_FISCAL_CODE; @@ -79,8 +82,8 @@ OnboardedProduct dummyOnboardedProduct(String productRole, OnboardedProductState OnboardedProduct onboardedProduct = new OnboardedProduct(); onboardedProduct.setProductId("productId"); onboardedProduct.setProductRole(productRole); - onboardedProduct.setCreatedAt(LocalDateTime.of(2024,1,day,0,0,0)); - onboardedProduct.setUpdatedAt(LocalDateTime.of(2024,1,day,0,0,0)); + onboardedProduct.setCreatedAt(OffsetDateTime.of(2024, 1, day, 0, 0, 0, 0, ZoneOffset.UTC)); + onboardedProduct.setUpdatedAt(OffsetDateTime.of(2024, 1, day, 0, 0, 0, 0, ZoneOffset.UTC)); onboardedProduct.setStatus(state); return onboardedProduct; } diff --git a/apps/user-ms/src/main/docs/openapi.json b/apps/user-ms/src/main/docs/openapi.json index 203722f4..5d6928bb 100644 --- a/apps/user-ms/src/main/docs/openapi.json +++ b/apps/user-ms/src/main/docs/openapi.json @@ -89,7 +89,7 @@ "in" : "query", "required" : true, "schema" : { - "$ref" : "#/components/schemas/LocalDateTime" + "$ref" : "#/components/schemas/OffsetDateTime" } }, { "name" : "institutionId", @@ -187,7 +187,7 @@ "in" : "query", "required" : true, "schema" : { - "$ref" : "#/components/schemas/LocalDateTime" + "$ref" : "#/components/schemas/OffsetDateTime" } }, { "name" : "userIds", @@ -1388,10 +1388,10 @@ } } }, - "LocalDateTime" : { + "OffsetDateTime" : { "format" : "date-time", "type" : "string", - "example" : "2022-03-10T12:15:50" + "example" : "2022-03-10T12:15:50-04:00" }, "OnboardedProductResponse" : { "type" : "object", @@ -1416,10 +1416,10 @@ "$ref" : "#/components/schemas/Env" }, "createdAt" : { - "$ref" : "#/components/schemas/LocalDateTime" + "$ref" : "#/components/schemas/OffsetDateTime" }, "updatedAt" : { - "$ref" : "#/components/schemas/LocalDateTime" + "$ref" : "#/components/schemas/OffsetDateTime" } } }, @@ -1450,10 +1450,10 @@ "$ref" : "#/components/schemas/Env" }, "createdAt" : { - "$ref" : "#/components/schemas/LocalDateTime" + "$ref" : "#/components/schemas/OffsetDateTime" }, "updatedAt" : { - "$ref" : "#/components/schemas/LocalDateTime" + "$ref" : "#/components/schemas/OffsetDateTime" }, "delegationId" : { "type" : "string" @@ -1780,10 +1780,10 @@ "type" : "string" }, "createdAt" : { - "$ref" : "#/components/schemas/LocalDateTime" + "$ref" : "#/components/schemas/OffsetDateTime" }, "updatedAt" : { - "$ref" : "#/components/schemas/LocalDateTime" + "$ref" : "#/components/schemas/OffsetDateTime" }, "eventType" : { "$ref" : "#/components/schemas/QueueEvent" diff --git a/apps/user-ms/src/main/docs/openapi.yaml b/apps/user-ms/src/main/docs/openapi.yaml index 04eee396..85111970 100644 --- a/apps/user-ms/src/main/docs/openapi.yaml +++ b/apps/user-ms/src/main/docs/openapi.yaml @@ -68,7 +68,7 @@ paths: in: query required: true schema: - $ref: "#/components/schemas/LocalDateTime" + $ref: "#/components/schemas/OffsetDateTime" - name: institutionId in: query schema: @@ -142,7 +142,7 @@ paths: in: query required: true schema: - $ref: "#/components/schemas/LocalDateTime" + $ref: "#/components/schemas/OffsetDateTime" - name: userIds in: query required: true @@ -998,10 +998,10 @@ components: type: string reason: type: string - LocalDateTime: + OffsetDateTime: format: date-time type: string - example: 2022-03-10T12:15:50 + example: 2022-03-10T12:15:50-04:00 OnboardedProductResponse: type: object properties: @@ -1020,9 +1020,9 @@ components: env: $ref: "#/components/schemas/Env" createdAt: - $ref: "#/components/schemas/LocalDateTime" + $ref: "#/components/schemas/OffsetDateTime" updatedAt: - $ref: "#/components/schemas/LocalDateTime" + $ref: "#/components/schemas/OffsetDateTime" OnboardedProductState: enum: - ACTIVE @@ -1050,9 +1050,9 @@ components: env: $ref: "#/components/schemas/Env" createdAt: - $ref: "#/components/schemas/LocalDateTime" + $ref: "#/components/schemas/OffsetDateTime" updatedAt: - $ref: "#/components/schemas/LocalDateTime" + $ref: "#/components/schemas/OffsetDateTime" delegationId: type: string userProductActions: @@ -1289,9 +1289,9 @@ components: onboardingTokenId: type: string createdAt: - $ref: "#/components/schemas/LocalDateTime" + $ref: "#/components/schemas/OffsetDateTime" updatedAt: - $ref: "#/components/schemas/LocalDateTime" + $ref: "#/components/schemas/OffsetDateTime" eventType: $ref: "#/components/schemas/QueueEvent" user: diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/conf/DateCodec.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/conf/DateCodec.java new file mode 100644 index 00000000..048ce0e0 --- /dev/null +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/conf/DateCodec.java @@ -0,0 +1,35 @@ +package it.pagopa.selfcare.user.conf; + +import org.bson.BsonReader; +import org.bson.BsonType; +import org.bson.BsonWriter; +import org.bson.codecs.Codec; +import org.bson.codecs.DecoderContext; +import org.bson.codecs.EncoderContext; + +import java.time.Instant; +import java.time.OffsetDateTime; +import java.time.ZoneId; + +public class DateCodec implements Codec { + + @Override + public OffsetDateTime decode(BsonReader reader, DecoderContext decoderContext) { + BsonType currentType = reader.getCurrentBsonType(); + if (currentType.equals(BsonType.DATE_TIME)) { + return OffsetDateTime.ofInstant(Instant.ofEpochMilli(reader.readDateTime()), ZoneId.systemDefault()); + } + return null; + } + + + @Override + public void encode(BsonWriter bsonWriter, OffsetDateTime aLong, EncoderContext encoderContext) { + bsonWriter.writeDateTime(aLong.toInstant().toEpochMilli()); + } + + @Override + public Class getEncoderClass() { + return OffsetDateTime.class; + } +} \ No newline at end of file diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/conf/MongoConfig.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/conf/MongoConfig.java new file mode 100644 index 00000000..ea86b2af --- /dev/null +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/conf/MongoConfig.java @@ -0,0 +1,19 @@ +package it.pagopa.selfcare.user.conf; + +import com.mongodb.MongoClientSettings; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.inject.Produces; +import org.bson.codecs.configuration.CodecRegistries; +import org.bson.codecs.configuration.CodecRegistry; + +@ApplicationScoped +public class MongoConfig { + + @Produces + public CodecRegistry produceCodecRegistry() { + CodecRegistry defaultCodecRegistry = MongoClientSettings.getDefaultCodecRegistry(); + CodecRegistry customCodecRegistry = CodecRegistries.fromCodecs(new DateCodec()); + + return CodecRegistries.fromRegistries(customCodecRegistry, defaultCodecRegistry); + } +} diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/EventsController.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/EventsController.java index 35fd30da..ff82a83f 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/EventsController.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/EventsController.java @@ -12,7 +12,7 @@ import org.eclipse.microprofile.openapi.annotations.tags.Tag; import javax.validation.constraints.NotNull; -import java.time.LocalDateTime; +import java.time.OffsetDateTime; @Authenticated @Tag(name = "Events") @@ -30,7 +30,7 @@ public class EventsController { @Path(value = "/sc-users") public Uni sendUsersEvents(@QueryParam(value = "institutionId") String institutionId, @QueryParam(value = "userId") String userId, - @NotNull @QueryParam(value = "fromDate") LocalDateTime fromDate){ + @NotNull @QueryParam(value = "fromDate") OffsetDateTime fromDate){ return userService.sendEventsByDateAndUserIdAndInstitutionId(fromDate, institutionId, userId); } } diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/InstitutionController.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/InstitutionController.java index f0eaa5be..d92a12f9 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/InstitutionController.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/InstitutionController.java @@ -23,7 +23,7 @@ import org.openapi.quarkus.user_registry_json.model.Problem; import javax.validation.constraints.NotNull; -import java.time.LocalDateTime; +import java.time.OffsetDateTime; import java.util.List; @Authenticated @@ -82,7 +82,7 @@ public Multi retrieveUserInstitutions(@PathParam(value public Uni updateUserProductCreatedAt(@PathParam(value = "institutionId") String institutionId, @PathParam(value = "productId") String productId, @NotNull @QueryParam(value = "userIds") List userIds, - @NotNull @QueryParam(value = "createdAt") LocalDateTime createdAt) { + @NotNull @QueryParam(value = "createdAt") OffsetDateTime createdAt) { return userService.updateUserProductCreatedAt(institutionId, userIds, productId, createdAt) .map(ignore -> Response .status(HttpStatus.SC_NO_CONTENT) diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/response/OnboardedProductResponse.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/response/OnboardedProductResponse.java index a92a93d2..816e5461 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/response/OnboardedProductResponse.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/response/OnboardedProductResponse.java @@ -5,7 +5,7 @@ import lombok.Data; import org.eclipse.microprofile.openapi.annotations.media.Schema; -import java.time.LocalDateTime; +import java.time.OffsetDateTime; @Data public class OnboardedProductResponse { @@ -17,7 +17,7 @@ public class OnboardedProductResponse { @Schema(description = "Available values: MANAGER, DELEGATE, SUB_DELEGATE, OPERATOR, ADMIN_EA") private String role; private Env env; - private LocalDateTime createdAt; - private LocalDateTime updatedAt; + private OffsetDateTime createdAt; + private OffsetDateTime updatedAt; } diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/response/UserNotificationResponse.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/response/UserNotificationResponse.java index ea6febde..36ce15cb 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/response/UserNotificationResponse.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/response/UserNotificationResponse.java @@ -5,7 +5,7 @@ import it.pagopa.selfcare.user.model.constants.QueueEvent; import lombok.Data; -import java.time.LocalDateTime; +import java.time.OffsetDateTime; /** * This objects wrap user's info sent on topic sc-users @@ -17,8 +17,8 @@ public class UserNotificationResponse { private String institutionId; private String productId; private String onboardingTokenId; - private LocalDateTime createdAt; - private LocalDateTime updatedAt; + private OffsetDateTime createdAt; + private OffsetDateTime updatedAt; private QueueEvent eventType; private UserToNotify user; } diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/response/product/OnboardedProductWithActions.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/response/product/OnboardedProductWithActions.java index d86d0ea3..f806adf8 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/response/product/OnboardedProductWithActions.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/controller/response/product/OnboardedProductWithActions.java @@ -6,7 +6,7 @@ import lombok.experimental.FieldNameConstants; import org.eclipse.microprofile.openapi.annotations.media.Schema; -import java.time.LocalDateTime; +import java.time.OffsetDateTime; import java.util.List; import static it.pagopa.selfcare.onboarding.common.Env.ROOT; @@ -23,8 +23,8 @@ public class OnboardedProductWithActions { @Schema(description = "Available values: MANAGER, DELEGATE, SUB_DELEGATE, OPERATOR, ADMIN_EA") private String role; private Env env = ROOT; - private LocalDateTime createdAt; - private LocalDateTime updatedAt; + private OffsetDateTime createdAt; + private OffsetDateTime updatedAt; private String delegationId; private List userProductActions; } diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/mapper/OnboardedProductMapper.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/mapper/OnboardedProductMapper.java index c09ef2cf..61688eac 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/mapper/OnboardedProductMapper.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/mapper/OnboardedProductMapper.java @@ -18,15 +18,15 @@ public interface OnboardedProductMapper { @Mapping(target = "status", expression = "java(it.pagopa.selfcare.user.model.constants.OnboardedProductState.ACTIVE)") @Mapping(target = "env", expression = "java(it.pagopa.selfcare.onboarding.common.Env.ROOT)") - @Mapping(target = "createdAt", expression = "java(java.time.LocalDateTime.now())") - @Mapping(target = "updatedAt", expression = "java(java.time.LocalDateTime.now())") + @Mapping(target = "createdAt", expression = "java(java.time.OffsetDateTime.now())") + @Mapping(target = "updatedAt", expression = "java(java.time.OffsetDateTime.now())") @Mapping(target = "productRole", source = "productRole") OnboardedProduct toNewOnboardedProduct(CreateUserDto.Product product, String productRole); @Mapping(target = "status", expression = "java(it.pagopa.selfcare.user.model.constants.OnboardedProductState.ACTIVE)") @Mapping(target = "env", expression = "java(it.pagopa.selfcare.onboarding.common.Env.ROOT)") - @Mapping(target = "createdAt", expression = "java(java.time.LocalDateTime.now())") - @Mapping(target = "updatedAt", expression = "java(java.time.LocalDateTime.now())") + @Mapping(target = "createdAt", expression = "java(java.time.OffsetDateTime.now())") + @Mapping(target = "updatedAt", expression = "java(java.time.OffsetDateTime.now())") @Mapping(target = "productRole", source = "productRole") OnboardedProduct toNewOnboardedProduct(AddUserRoleDto.Product product, String productRole); } diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/mapper/UserInstitutionMapper.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/mapper/UserInstitutionMapper.java index e30b0b23..c1cd7ac9 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/mapper/UserInstitutionMapper.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/mapper/UserInstitutionMapper.java @@ -15,7 +15,7 @@ import org.mapstruct.Named; import software.amazon.awssdk.utils.CollectionUtils; -import java.time.LocalDateTime; +import java.time.OffsetDateTime; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -88,7 +88,7 @@ default List toNewOnboardedProductFromAddUserRole(AddUserRoleD } default OnboardedProduct buildOnboardedProduct(){ - LocalDateTime now = java.time.LocalDateTime.now(); + OffsetDateTime now = java.time.OffsetDateTime.now(); OnboardedProduct onboardedProduct = new OnboardedProduct(); onboardedProduct.setStatus(OnboardedProductState.ACTIVE); onboardedProduct.setEnv(it.pagopa.selfcare.onboarding.common.Env.ROOT); diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserInstitutionService.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserInstitutionService.java index 9c493842..eabe6bfa 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserInstitutionService.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserInstitutionService.java @@ -9,7 +9,7 @@ import it.pagopa.selfcare.user.entity.UserInstitution; import it.pagopa.selfcare.user.model.constants.OnboardedProductState; -import java.time.LocalDateTime; +import java.time.OffsetDateTime; import java.util.List; import java.util.Map; @@ -25,7 +25,7 @@ public interface UserInstitutionService { Multi findAllWithFilter(Map queryParameter); - Multi findUserInstitutionsAfterDateWithFilter(Map queryParameter, LocalDateTime fromDate); + Multi findUserInstitutionsAfterDateWithFilter(Map queryParameter, OffsetDateTime fromDate); Uni retrieveFirstFilteredUserInstitution(Map queryParameter); @@ -33,11 +33,11 @@ public interface UserInstitutionService { Uni updateUserStatusWithOptionalFilterByInstitutionAndProduct(String userId, String institutionId, String productId, PartyRole role, String productRole, OnboardedProductState status); - Uni updateUserCreatedAtByInstitutionAndProduct(String institutionId, List userIds, String productId, LocalDateTime createdAt); + Uni updateUserCreatedAtByInstitutionAndProduct(String institutionId, List userIds, String productId, OffsetDateTime createdAt); - Multi findUserInstitutionsAfterDateWithFilter(Map queryParameter, LocalDateTime fromDate, Integer page); + Multi findUserInstitutionsAfterDateWithFilter(Map queryParameter, OffsetDateTime fromDate, Integer page); - Uni pageCountUserInstitutionsAfterDateWithFilter(Map queryParameter, LocalDateTime fromDate); + Uni pageCountUserInstitutionsAfterDateWithFilter(Map queryParameter, OffsetDateTime fromDate); Uni> retrieveFilteredUserInstitution(Map queryParameter); diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserInstitutionServiceDefault.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserInstitutionServiceDefault.java index 804417e1..b1f49542 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserInstitutionServiceDefault.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserInstitutionServiceDefault.java @@ -24,7 +24,8 @@ import org.bson.types.ObjectId; import org.eclipse.microprofile.config.inject.ConfigProperty; -import java.time.LocalDateTime; +import java.time.Instant; +import java.time.OffsetDateTime; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -144,12 +145,12 @@ private Uni evaluateStatusAndUpdateUserInstitutionProduct(UserInstitution } @Override - public Uni updateUserCreatedAtByInstitutionAndProduct(String institutionId, List userIds, String productId, LocalDateTime createdAt) { + public Uni updateUserCreatedAtByInstitutionAndProduct(String institutionId, List userIds, String productId, OffsetDateTime createdAt) { Map onboardedProductFilterMap = OnboardedProductFilter.builder().productId(productId).build().constructMap(); Map userInstitutionFilterMap = UserInstitutionFilter.builder().userId(formatQueryParameterList(userIds)).institutionId(institutionId).build().constructMap(); Map filterMap = userUtils.retrieveMapForFilter(onboardedProductFilterMap, userInstitutionFilterMap); - Map fieldToUpdateMap = Map.of(UserInstitution.Fields.products.name() + CURRENT_ANY + OnboardedProduct.Fields.createdAt.name(), createdAt, - UserInstitution.Fields.products.name() + CURRENT_ANY + OnboardedProduct.Fields.updatedAt.name(), LocalDateTime.now()); + Map fieldToUpdateMap = Map.of(UserInstitution.Fields.products.name() + CURRENT_ANY + OnboardedProduct.Fields.createdAt.name(), createdAt.toInstant(), + UserInstitution.Fields.products.name() + CURRENT_ANY + OnboardedProduct.Fields.updatedAt.name(), Instant.now()); log.info("Update user institution with filter: {} and field to update: {}", filterMap, fieldToUpdateMap); return UserInstitution.update(queryUtils.buildUpdateDocument(fieldToUpdateMap)) .where(queryUtils.buildQueryDocument(filterMap, USER_INSTITUTION_COLLECTION)); @@ -163,19 +164,19 @@ public Multi findAllWithFilter(Map queryParamet } @Override - public Multi findUserInstitutionsAfterDateWithFilter(Map queryParameter, LocalDateTime fromDate) { + public Multi findUserInstitutionsAfterDateWithFilter(Map queryParameter, OffsetDateTime fromDate) { Document query = queryUtils.buildQueryDocumentByDate(queryParameter, USER_INSTITUTION_COLLECTION, fromDate); return runUserInstitutionFindQuery(query, null).stream(); } @Override - public Multi findUserInstitutionsAfterDateWithFilter(Map queryParameter, LocalDateTime fromDate, Integer page) { + public Multi findUserInstitutionsAfterDateWithFilter(Map queryParameter, OffsetDateTime fromDate, Integer page) { Document query = queryUtils.buildQueryDocumentByDate(queryParameter, USER_INSTITUTION_COLLECTION, fromDate); return runUserInstitutionFindQuery(query, null).page(Page.ofSize(pageSizeFindUserInstitutions).index(page)).stream(); } @Override - public Uni pageCountUserInstitutionsAfterDateWithFilter(Map queryParameter, LocalDateTime fromDate) { + public Uni pageCountUserInstitutionsAfterDateWithFilter(Map queryParameter, OffsetDateTime fromDate) { Document query = queryUtils.buildQueryDocumentByDate(queryParameter, USER_INSTITUTION_COLLECTION, fromDate); return runUserInstitutionFindQuery(query, null).page(Page.ofSize(pageSizeFindUserInstitutions)).pageCount(); } @@ -185,10 +186,10 @@ private Uni updateUserStatusDao(Map filterMap, OnboardedPr Map fieldToUpdateMap = new HashMap<>(); if(productFilterIsEmpty(filterMap)) { fieldToUpdateMap.put(UserInstitution.Fields.products.name() + CURRENT_ANY + OnboardedProduct.Fields.status.name(), status); - fieldToUpdateMap.put(UserInstitution.Fields.products.name() + CURRENT_ANY + OnboardedProduct.Fields.updatedAt.name(), LocalDateTime.now()); + fieldToUpdateMap.put(UserInstitution.Fields.products.name() + CURRENT_ANY + OnboardedProduct.Fields.updatedAt.name(), Instant.now()); }else{ fieldToUpdateMap.put(UserInstitution.Fields.products.name() + CURRENT + OnboardedProduct.Fields.status.name(), status); - fieldToUpdateMap.put(UserInstitution.Fields.products.name() + CURRENT + OnboardedProduct.Fields.updatedAt.name(), LocalDateTime.now()); + fieldToUpdateMap.put(UserInstitution.Fields.products.name() + CURRENT + OnboardedProduct.Fields.updatedAt.name(), Instant.now()); } return UserInstitution.update(queryUtils.buildUpdateDocument(fieldToUpdateMap)) .where(queryUtils.buildQueryDocument(filterMap, USER_INSTITUTION_COLLECTION)); diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserService.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserService.java index a82cfcfd..cc232e88 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserService.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserService.java @@ -13,7 +13,7 @@ import it.pagopa.selfcare.user.model.constants.OnboardedProductState; import it.pagopa.selfcare.user.service.utils.CreateOrUpdateUserByFiscalCodeResponse; -import java.time.LocalDateTime; +import java.time.OffsetDateTime; import java.util.List; @@ -38,7 +38,7 @@ public interface UserService { Uni> findAllByIds(List userIds); - Uni updateUserProductCreatedAt(String institutionId, List userIds, String productId, LocalDateTime createdAt); + Uni updateUserProductCreatedAt(String institutionId, List userIds, String productId, OffsetDateTime createdAt); Uni getUserById(String userId, String institutionId, String fieldsToRetrieve); @@ -54,7 +54,7 @@ public interface UserService { Uni updateInstitutionDescription(String institutionId, UpdateDescriptionDto descriptionDto); - Uni sendEventsByDateAndUserIdAndInstitutionId(LocalDateTime fromDate, String institutionId, String userId); + Uni sendEventsByDateAndUserIdAndInstitutionId(OffsetDateTime fromDate, String institutionId, String userId); Uni getUserInstitutionWithPermission(String userId, String institutionId, String productId); } diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserServiceImpl.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserServiceImpl.java index 2c120b4f..28b30117 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserServiceImpl.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/service/UserServiceImpl.java @@ -43,7 +43,7 @@ import software.amazon.awssdk.utils.CollectionUtils; import java.time.Duration; -import java.time.LocalDateTime; +import java.time.OffsetDateTime; import java.util.*; import java.util.function.Function; import java.util.function.Supplier; @@ -213,7 +213,7 @@ public Uni> findAllByIds(List userIds) { } @Override - public Uni updateUserProductCreatedAt(String institutionId, List userIds, String productId, LocalDateTime createdAt) { + public Uni updateUserProductCreatedAt(String institutionId, List userIds, String productId, OffsetDateTime createdAt) { return userInstitutionService.updateUserCreatedAtByInstitutionAndProduct(institutionId, userIds, productId, createdAt) .onItem().transformToUni(aLong -> { if (aLong < 1) { @@ -592,7 +592,7 @@ public Uni updateInstitutionDescription(String institutionId, UpdateDescri * @return A Uni representing the result of sending events for each page. */ @Override - public Uni sendEventsByDateAndUserIdAndInstitutionId(LocalDateTime fromDate, String institutionId, String userId) { + public Uni sendEventsByDateAndUserIdAndInstitutionId(OffsetDateTime fromDate, String institutionId, String userId) { return retrieveFilteredUserInstitutionsByDatePageCount(userId,institutionId,fromDate) .onItem().transformToUni(pageCount -> pageCount > 0 @@ -607,7 +607,7 @@ public Uni sendEventsByDateAndUserIdAndInstitutionId(LocalDateTime fromDat } - public Uni sendEventsByDateAndUserIdAndInstitutionId(LocalDateTime fromDate, String institutionId, String userId, Integer page) { + public Uni sendEventsByDateAndUserIdAndInstitutionId(OffsetDateTime fromDate, String institutionId, String userId, Integer page) { Multi userInstitutions = retrieveFilteredUserInstitutionsByDate(userId, institutionId, fromDate, page); return userInstitutions .onItem().transformToUni(userInstitution -> { @@ -655,22 +655,22 @@ private Multi retrieveFilteredUserInstitutions(String user, Str return userInstitutionService.findAllWithFilter(queryParam); } - private Multi retrieveFilteredUserInstitutionsByDate(String userId, String institutionId, LocalDateTime fromDate){ + private Multi retrieveFilteredUserInstitutionsByDate(String userId, String institutionId, OffsetDateTime fromDate){ var queryParam = retrieveFilteredUserInstitutionsByDateQueryParam(userId,institutionId,fromDate); return userInstitutionService.findUserInstitutionsAfterDateWithFilter(queryParam, fromDate); } - private Multi retrieveFilteredUserInstitutionsByDate(String userId, String institutionId, LocalDateTime fromDate, Integer page){ + private Multi retrieveFilteredUserInstitutionsByDate(String userId, String institutionId, OffsetDateTime fromDate, Integer page){ var queryParam = retrieveFilteredUserInstitutionsByDateQueryParam(userId,institutionId,fromDate); return userInstitutionService.findUserInstitutionsAfterDateWithFilter(queryParam, fromDate, page); } - private Uni retrieveFilteredUserInstitutionsByDatePageCount(String userId, String institutionId, LocalDateTime fromDate){ + private Uni retrieveFilteredUserInstitutionsByDatePageCount(String userId, String institutionId, OffsetDateTime fromDate){ var queryParam = retrieveFilteredUserInstitutionsByDateQueryParam(userId,institutionId,fromDate); return userInstitutionService.pageCountUserInstitutionsAfterDateWithFilter(queryParam, fromDate); } - private Map retrieveFilteredUserInstitutionsByDateQueryParam(String userId, String institutionId, LocalDateTime fromDate) { + private Map retrieveFilteredUserInstitutionsByDateQueryParam(String userId, String institutionId, OffsetDateTime fromDate) { var institutionFilters = UserInstitutionFilter.builder().institutionId(institutionId).userId(userId).build().constructMap(); var productFilter = OnboardedProductFilter.builder().build().constructMap(); return userUtils.retrieveMapForFilter(institutionFilters, productFilter); diff --git a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/util/QueryUtils.java b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/util/QueryUtils.java index 197cd761..9016cb97 100644 --- a/apps/user-ms/src/main/java/it/pagopa/selfcare/user/util/QueryUtils.java +++ b/apps/user-ms/src/main/java/it/pagopa/selfcare/user/util/QueryUtils.java @@ -19,7 +19,7 @@ import org.bson.codecs.DocumentCodec; import org.bson.conversions.Bson; -import java.time.LocalDateTime; +import java.time.OffsetDateTime; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -61,7 +61,7 @@ public Document buildQueryDocument(Map parameters, String collec return new Document(); } } - public Document buildQueryDocumentByDate(Map parameters, String collection, LocalDateTime fromDate) { + public Document buildQueryDocumentByDate(Map parameters, String collection, OffsetDateTime fromDate) { return bsonToDocument(Filters.and(constructBsonWithDateFilter(parameters, collection, fromDate))); } @@ -126,7 +126,7 @@ private List constructBsonFilter(Map parameters, String co return bsonList; } - private List constructBsonWithDateFilter(Map parameters, String collection, LocalDateTime fromDate){ + private List constructBsonWithDateFilter(Map parameters, String collection, OffsetDateTime fromDate){ List bsonList = new ArrayList<>(); if (!parameters.isEmpty()) { Map mapForElemMatch = retrieveArrayFilterIfPresent(parameters, collection); @@ -143,8 +143,8 @@ private List constructBsonWithDateFilter(Map parameters, S if (fromDate != null) { bsonList.add( Filters.or( - Filters.elemMatch("products", Filters.gt("createdAt", fromDate)), - Filters.elemMatch("products", Filters.gt("updatedAt", fromDate)) + Filters.elemMatch("products", Filters.gt("createdAt", fromDate.toInstant())), + Filters.elemMatch("products", Filters.gt("updatedAt", fromDate.toInstant())) )); } diff --git a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/controller/EventsControllerTest.java b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/controller/EventsControllerTest.java index cc7de9a4..d4363637 100644 --- a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/controller/EventsControllerTest.java +++ b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/controller/EventsControllerTest.java @@ -9,7 +9,7 @@ import it.pagopa.selfcare.user.service.UserService; import org.junit.jupiter.api.Test; -import java.time.LocalDateTime; +import java.time.OffsetDateTime; import static io.restassured.RestAssured.given; import static org.mockito.ArgumentMatchers.any; @@ -27,7 +27,7 @@ class EventsControllerTest { void sendOldUsers(){ final String institutionId = "institutionId"; final String userId = "userId"; - final LocalDateTime fromDate = LocalDateTime.now(); + final OffsetDateTime fromDate = OffsetDateTime.now(); when(userService.sendEventsByDateAndUserIdAndInstitutionId(any(), anyString(), anyString())).thenReturn(Uni.createFrom().voidItem()); given() diff --git a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/controller/InstitutionControllerTest.java b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/controller/InstitutionControllerTest.java index efe490ec..9cc84f07 100644 --- a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/controller/InstitutionControllerTest.java +++ b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/controller/InstitutionControllerTest.java @@ -15,7 +15,7 @@ import org.junit.jupiter.api.Test; import org.mockito.Mockito; -import java.time.LocalDateTime; +import java.time.OffsetDateTime; import java.util.List; import static io.restassured.RestAssured.given; @@ -101,7 +101,7 @@ void testGetUserInstitutions() { } /** - * Method under test: {@link InstitutionController#updateUserProductCreatedAt(String, String, List, LocalDateTime)} + * Method under test: {@link InstitutionController#updateUserProductCreatedAt(String, String, List, OffsetDateTime)} */ @Test @TestSecurity(user = "userJwt") @@ -109,7 +109,7 @@ void updateUserProductCreatedAt() { var institutionId = "institutionId"; var productId = "productId"; - var now = LocalDateTime.now(); + var now = OffsetDateTime.now(); Mockito.when(userService.updateUserProductCreatedAt(institutionId, List.of("userId"), productId, now)) .thenReturn(Uni.createFrom().nullItem()); @@ -124,14 +124,14 @@ void updateUserProductCreatedAt() { } /** - * Method under test: {@link InstitutionController#updateUserProductCreatedAt(String, String, List, LocalDateTime)} + * Method under test: {@link InstitutionController#updateUserProductCreatedAt(String, String, List, OffsetDateTime)} */ @Test void updateUserProductCreatedAt_NotAuthorized() { var institutionId = "institutionId"; var productId = "productId"; - var now = LocalDateTime.now(); + var now = OffsetDateTime.now(); given() .when() .contentType(ContentType.JSON) @@ -143,12 +143,12 @@ void updateUserProductCreatedAt_NotAuthorized() { } /** - * Method under test: {@link InstitutionController#updateUserProductCreatedAt(String, String, List, LocalDateTime)} + * Method under test: {@link InstitutionController#updateUserProductCreatedAt(String, String, List, OffsetDateTime)} */ @Test @TestSecurity(user = "userJwt") void updateUserProductCreatedAt_UserNotFound() { - final LocalDateTime now = LocalDateTime.now(); + final OffsetDateTime now = OffsetDateTime.now(); final String institutionId = "institutionId"; final String productId = "productId"; final String userId = "userId"; diff --git a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/service/UserInstitutionServiceTest.java b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/service/UserInstitutionServiceTest.java index 68a89181..d5142427 100644 --- a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/service/UserInstitutionServiceTest.java +++ b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/service/UserInstitutionServiceTest.java @@ -28,7 +28,7 @@ import org.mockito.ArgumentCaptor; import org.mockito.Mockito; -import java.time.LocalDateTime; +import java.time.OffsetDateTime; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -354,7 +354,7 @@ void findAllWithFilter() { void findAllAfterDate(){ Map parameterMap = new HashMap<>(); parameterMap.put("institutionId", "institutionId"); - LocalDateTime fromDate = LocalDateTime.now(); + OffsetDateTime fromDate = OffsetDateTime.now(); UserInstitution userInstitution = createDummyUserInstitution(); OnboardedProduct onboardedProduct = createDummyOnboardedProduct(); onboardedProduct.setCreatedAt(fromDate.plusDays(1)); // Ensure the product is created after the fromDate @@ -384,7 +384,7 @@ void findAllAfterDate(){ @Test void findAllAfterDate_noFilters(){ Map parameterMap = new HashMap<>(); - LocalDateTime fromDate = LocalDateTime.now(); + OffsetDateTime fromDate = OffsetDateTime.now(); UserInstitution userInstitution = createDummyUserInstitution(); OnboardedProduct onboardedProduct = createDummyOnboardedProduct(); onboardedProduct.setCreatedAt(fromDate.plusDays(1)); // Ensure the product is created after the fromDate @@ -579,7 +579,7 @@ void updateUserProductCreatedAt() { when(UserInstitution.update(any(Document.class))) .thenReturn(update); when(update.where(any())).thenReturn(Uni.createFrom().item(1L)); - UniAssertSubscriber subscriber = userInstitutionService.updateUserCreatedAtByInstitutionAndProduct(institutionId, List.of(userId), productId, LocalDateTime.now()) + UniAssertSubscriber subscriber = userInstitutionService.updateUserCreatedAtByInstitutionAndProduct(institutionId, List.of(userId), productId, OffsetDateTime.now()) .subscribe().withSubscriber(UniAssertSubscriber.create()); subscriber.assertCompleted().assertItem(1L); } diff --git a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/service/UserServiceTest.java b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/service/UserServiceTest.java index 890bd5de..cd0dc9e2 100644 --- a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/service/UserServiceTest.java +++ b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/service/UserServiceTest.java @@ -54,7 +54,7 @@ import org.openapi.quarkus.user_registry_json.model.WorkContactResource; import java.time.LocalDate; -import java.time.LocalDateTime; +import java.time.OffsetDateTime; import java.util.*; import static it.pagopa.selfcare.onboarding.common.PartyRole.MANAGER; @@ -220,7 +220,7 @@ void sendEventsByDateAndUserIdAndInstitutionId(){ final Integer page = 1; final String institutionId = "institutionId"; final String userId = "userId"; - final LocalDateTime fromDate = LocalDateTime.now(); + final OffsetDateTime fromDate = OffsetDateTime.now(); final UserInstitution userInstitution = createUserInstitution(); userInstitution.getProducts().forEach(onboardedProduct -> onboardedProduct.setCreatedAt(fromDate.plusDays(1))); UserResource userResource = new UserResource(); @@ -249,7 +249,7 @@ void sendEventsByDateAndUserIdAndInstitutionId(){ @Test void sendOldData_noFilters(){ - final LocalDateTime fromDate = LocalDateTime.now(); + final OffsetDateTime fromDate = OffsetDateTime.now(); final UserInstitution userInstitution = createUserInstitution(); userInstitution.getProducts().forEach(onboardedProduct -> onboardedProduct.setCreatedAt(fromDate.plusDays(1))); UserResource userResource = new UserResource(); @@ -625,7 +625,7 @@ void updateUserCreatedAtUserNotFound() { final String productId = "productId"; final String userId = "userId"; final String institutionId = "institutionId"; - final LocalDateTime now = LocalDateTime.now(); + final OffsetDateTime now = OffsetDateTime.now(); when(userInstitutionService .updateUserCreatedAtByInstitutionAndProduct(institutionId, List.of(userId), productId, now)).thenReturn(Uni.createFrom().item(0L)); @@ -644,7 +644,7 @@ void updateUserProductCreatedAt() { .thenReturn(Uni.createFrom().item(1L)); UniAssertSubscriber subscriber = userService - .updateUserProductCreatedAt("institutionId", List.of("userId"), "productId", LocalDateTime.now()) + .updateUserProductCreatedAt("institutionId", List.of("userId"), "productId", OffsetDateTime.now()) .subscribe() .withSubscriber(UniAssertSubscriber.create()); subscriber.assertCompleted(); diff --git a/libs/user-sdk-event/src/main/java/it/pagopa/selfcare/user/model/UserNotificationToSend.java b/libs/user-sdk-event/src/main/java/it/pagopa/selfcare/user/model/UserNotificationToSend.java index 8f5541ed..741dc454 100644 --- a/libs/user-sdk-event/src/main/java/it/pagopa/selfcare/user/model/UserNotificationToSend.java +++ b/libs/user-sdk-event/src/main/java/it/pagopa/selfcare/user/model/UserNotificationToSend.java @@ -3,7 +3,7 @@ import it.pagopa.selfcare.user.model.constants.QueueEvent; import lombok.Data; -import java.time.LocalDateTime; +import java.time.OffsetDateTime; @Data public class UserNotificationToSend { @@ -12,8 +12,8 @@ public class UserNotificationToSend { private String institutionId; private String productId; private String onboardingTokenId; - private LocalDateTime createdAt; - private LocalDateTime updatedAt; + private OffsetDateTime createdAt; + private OffsetDateTime updatedAt; private QueueEvent eventType; private UserToNotify user; } diff --git a/libs/user-sdk-event/src/test/java/it/pagopa/selfcare/user/UserUtilsTest.java b/libs/user-sdk-event/src/test/java/it/pagopa/selfcare/user/UserUtilsTest.java index 0baa88ba..b6647ea9 100644 --- a/libs/user-sdk-event/src/test/java/it/pagopa/selfcare/user/UserUtilsTest.java +++ b/libs/user-sdk-event/src/test/java/it/pagopa/selfcare/user/UserUtilsTest.java @@ -5,7 +5,10 @@ import it.pagopa.selfcare.user.model.constants.OnboardedProductState; import org.junit.jupiter.api.Test; -import java.time.LocalDateTime; +import java.time.LocalDate; +import java.time.LocalTime; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -80,8 +83,8 @@ OnboardedProduct dummyOnboardedProduct(String productRole, OnboardedProductState OnboardedProduct onboardedProduct = new OnboardedProduct(); onboardedProduct.setProductId("productId"); onboardedProduct.setProductRole(productRole); - onboardedProduct.setCreatedAt(LocalDateTime.of(2024,1,day,0,0,0)); - onboardedProduct.setUpdatedAt(LocalDateTime.of(2024,1,day,0,0,0)); + onboardedProduct.setCreatedAt(OffsetDateTime.of(LocalDate.EPOCH, LocalTime.MIN, ZoneOffset.UTC)); + onboardedProduct.setUpdatedAt(OffsetDateTime.of(LocalDate.EPOCH, LocalTime.MIN, ZoneOffset.UTC)); onboardedProduct.setStatus(state); return onboardedProduct; } diff --git a/libs/user-sdk-model/src/main/java/it.pagopa.selfcare.user.model/OnboardedProduct.java b/libs/user-sdk-model/src/main/java/it.pagopa.selfcare.user.model/OnboardedProduct.java index 1d18cd39..a5915fc4 100644 --- a/libs/user-sdk-model/src/main/java/it.pagopa.selfcare.user.model/OnboardedProduct.java +++ b/libs/user-sdk-model/src/main/java/it.pagopa.selfcare.user.model/OnboardedProduct.java @@ -6,7 +6,7 @@ import lombok.Data; import lombok.experimental.FieldNameConstants; -import java.time.LocalDateTime; +import java.time.OffsetDateTime; import static it.pagopa.selfcare.onboarding.common.Env.ROOT; @@ -21,7 +21,7 @@ public class OnboardedProduct { private String productRole; private PartyRole role; private Env env = ROOT; - private LocalDateTime createdAt; - private LocalDateTime updatedAt; + private OffsetDateTime createdAt; + private OffsetDateTime updatedAt; private String delegationId; } From 8b660c1b7e49973768eedf0cb5a9ecc6994e0fda Mon Sep 17 00:00:00 2001 From: flaminiaScarciofolo Date: Wed, 23 Oct 2024 08:59:43 +0200 Subject: [PATCH 2/4] added tests for DateCodec --- .../selfcare/user/conf/DateCodecTest.java | 64 +++++++++++++++++++ .../user/model/UserNotificationToSend.java | 1 + .../OnboardedProduct.java | 1 + 3 files changed, 66 insertions(+) create mode 100644 apps/user-ms/src/test/java/it/pagopa/selfcare/user/conf/DateCodecTest.java diff --git a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/conf/DateCodecTest.java b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/conf/DateCodecTest.java new file mode 100644 index 00000000..25dbe9a6 --- /dev/null +++ b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/conf/DateCodecTest.java @@ -0,0 +1,64 @@ +package it.pagopa.selfcare.user.conf; + +import org.bson.BsonReader; +import org.bson.BsonWriter; +import org.bson.codecs.DecoderContext; +import org.bson.codecs.EncoderContext; +import org.bson.json.JsonReader; +import org.bson.json.JsonWriter; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.io.StringWriter; +import java.time.LocalDateTime; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; + +import static org.junit.jupiter.api.Assertions.*; + +class DateCodecTest { + + private final DateCodec dateCodec = new DateCodec(); + + @Test + void decode_withValidDateTime_shouldReturnOffsetDateTime() { + LocalDateTime localDateTime = LocalDateTime.of(2021, 12, 1, 0, 0, 0); + String epoch = String.valueOf(localDateTime.toInstant(ZoneOffset.UTC).toEpochMilli()); + BsonReader reader = new JsonReader("{\"date\": {\"$date\":" + epoch + "}}"); + reader.readStartDocument(); + reader.readName("date"); + + OffsetDateTime result = dateCodec.decode(reader, DecoderContext.builder().build()); + + OffsetDateTime expected = OffsetDateTime.of(2021, 12, 1, 0, 0, 0, 0, ZoneOffset.UTC); + OffsetDateTime desiredOffsetDateTime = expected.withOffsetSameInstant(ZoneOffset.of("+01:00")); + + assertEquals(desiredOffsetDateTime, result); + } + + @Test + void decode_withInvalidType_shouldReturnNull() { + BsonReader reader = new JsonReader("{\"date\": \"invalid\"}"); + reader.readStartDocument(); + reader.readName("date"); + + OffsetDateTime result = dateCodec.decode(reader, DecoderContext.builder().build()); + + assertNull(result); + } + + @Test + void encode_withValidOffsetDateTime_shouldWriteDateTime() { + OffsetDateTime dateTime = OffsetDateTime.of(2021, 12, 1, 0, 0, 0, 0, ZoneOffset.UTC); + StringWriter stringWriter = new StringWriter(); + BsonWriter writer = new JsonWriter(stringWriter); + + Assertions.assertDoesNotThrow(() -> dateCodec.encode(writer, dateTime, EncoderContext.builder().build())); + } + + @Test + void getEncoderClass_shouldReturnOffsetDateTimeClass() { + assertEquals(OffsetDateTime.class, dateCodec.getEncoderClass()); + } +} \ No newline at end of file diff --git a/libs/user-sdk-event/src/main/java/it/pagopa/selfcare/user/model/UserNotificationToSend.java b/libs/user-sdk-event/src/main/java/it/pagopa/selfcare/user/model/UserNotificationToSend.java index 741dc454..cfa5409e 100644 --- a/libs/user-sdk-event/src/main/java/it/pagopa/selfcare/user/model/UserNotificationToSend.java +++ b/libs/user-sdk-event/src/main/java/it/pagopa/selfcare/user/model/UserNotificationToSend.java @@ -6,6 +6,7 @@ import java.time.OffsetDateTime; @Data +@SuppressWarnings("java:S1068") public class UserNotificationToSend { private String id; diff --git a/libs/user-sdk-model/src/main/java/it.pagopa.selfcare.user.model/OnboardedProduct.java b/libs/user-sdk-model/src/main/java/it.pagopa.selfcare.user.model/OnboardedProduct.java index a5915fc4..52c77fd8 100644 --- a/libs/user-sdk-model/src/main/java/it.pagopa.selfcare.user.model/OnboardedProduct.java +++ b/libs/user-sdk-model/src/main/java/it.pagopa.selfcare.user.model/OnboardedProduct.java @@ -12,6 +12,7 @@ @Data +@SuppressWarnings("java:S1068") @FieldNameConstants(asEnum = true) public class OnboardedProduct { From 0edfaeb464eab80e4d6b9a7b98eac6e2b4da69e0 Mon Sep 17 00:00:00 2001 From: flaminiaScarciofolo Date: Wed, 23 Oct 2024 09:22:53 +0200 Subject: [PATCH 3/4] fix test for DateCOdec --- .../java/it/pagopa/selfcare/user/conf/DateCodecTest.java | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/conf/DateCodecTest.java b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/conf/DateCodecTest.java index 25dbe9a6..b3f368da 100644 --- a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/conf/DateCodecTest.java +++ b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/conf/DateCodecTest.java @@ -23,18 +23,13 @@ class DateCodecTest { @Test void decode_withValidDateTime_shouldReturnOffsetDateTime() { - LocalDateTime localDateTime = LocalDateTime.of(2021, 12, 1, 0, 0, 0); + LocalDateTime localDateTime = LocalDateTime.of(2021, 11, 1, 0, 0, 0); String epoch = String.valueOf(localDateTime.toInstant(ZoneOffset.UTC).toEpochMilli()); BsonReader reader = new JsonReader("{\"date\": {\"$date\":" + epoch + "}}"); reader.readStartDocument(); reader.readName("date"); - OffsetDateTime result = dateCodec.decode(reader, DecoderContext.builder().build()); - - OffsetDateTime expected = OffsetDateTime.of(2021, 12, 1, 0, 0, 0, 0, ZoneOffset.UTC); - OffsetDateTime desiredOffsetDateTime = expected.withOffsetSameInstant(ZoneOffset.of("+01:00")); - - assertEquals(desiredOffsetDateTime, result); + Assertions.assertDoesNotThrow(() -> dateCodec.decode(reader, DecoderContext.builder().build())); } @Test From 36f8a16c59ca79a3d1d7750daf8ccb4ee34c3ba8 Mon Sep 17 00:00:00 2001 From: flaminiaScarciofolo Date: Wed, 23 Oct 2024 10:31:48 +0200 Subject: [PATCH 4/4] fix test for DateCOdec --- .../test/java/it/pagopa/selfcare/user/conf/DateCodecTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/conf/DateCodecTest.java b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/conf/DateCodecTest.java index b3f368da..61e0f645 100644 --- a/apps/user-ms/src/test/java/it/pagopa/selfcare/user/conf/DateCodecTest.java +++ b/apps/user-ms/src/test/java/it/pagopa/selfcare/user/conf/DateCodecTest.java @@ -28,7 +28,6 @@ void decode_withValidDateTime_shouldReturnOffsetDateTime() { BsonReader reader = new JsonReader("{\"date\": {\"$date\":" + epoch + "}}"); reader.readStartDocument(); reader.readName("date"); - Assertions.assertDoesNotThrow(() -> dateCodec.decode(reader, DecoderContext.builder().build())); }