Skip to content

Commit

Permalink
[SELC-5740] Added custom code to parse different format of date
Browse files Browse the repository at this point in the history
  • Loading branch information
flaminiaScarciofolo committed Oct 17, 2024
1 parent ca3ce97 commit b3046ef
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import io.quarkus.runtime.Startup;
import io.quarkus.runtime.configuration.ConfigUtils;
import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;
import it.pagopa.selfcare.user.client.EventHubRestClient;
import it.pagopa.selfcare.user.event.entity.UserGroupEntity;
import it.pagopa.selfcare.user.event.mapper.UserGroupNotificationMapper;
Expand All @@ -30,16 +29,17 @@
import org.eclipse.microprofile.rest.client.inject.RestClient;

import java.time.Duration;
import java.util.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.mongodb.client.model.Projections.fields;
import static com.mongodb.client.model.Projections.include;
import static it.pagopa.selfcare.user.UserUtils.mapPropsForTrackEvent;
import static it.pagopa.selfcare.user.event.constant.CdcStartAtConstant.*;
import static it.pagopa.selfcare.user.model.TrackEventInput.toTrackEventInput;
import static it.pagopa.selfcare.user.model.TrackEventInput.toTrackEventInputForUserGroup;
import static it.pagopa.selfcare.user.model.constants.EventsMetric.*;
import static it.pagopa.selfcare.user.model.constants.EventsName.EVENT_USER_CDC_NAME;
import static it.pagopa.selfcare.user.model.constants.EventsName.EVENT_USER_GROUP_CDC_NAME;
import static java.util.Arrays.asList;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package it.pagopa.selfcare.user.event.config;

import org.bson.BsonReader;
import org.bson.BsonTimestamp;
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;

public class DateCodec implements Codec<Instant> {

@Override
public Instant decode(BsonReader reader, DecoderContext decoderContext) {
BsonType currentType = reader.getCurrentBsonType();
if (currentType.equals(BsonType.DATE_TIME)) {
return Instant.ofEpochMilli(reader.readDateTime());
} else if (currentType.equals(BsonType.TIMESTAMP)) {
BsonTimestamp timestamp = reader.readTimestamp();
return Instant.ofEpochSecond(timestamp.getTime() / 1000, timestamp.getInc() * 1000L);
}
return null;
}


@Override
public void encode(BsonWriter bsonWriter, Instant aLong, EncoderContext encoderContext) {

}

@Override
public Class<Instant> getEncoderClass() {
return Instant.class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package it.pagopa.selfcare.user.event.config;

import jakarta.enterprise.context.ApplicationScoped;

import jakarta.enterprise.inject.Produces;
import org.bson.codecs.configuration.CodecRegistries;
import org.bson.codecs.configuration.CodecRegistry;
import com.mongodb.MongoClientSettings;

@ApplicationScoped
public class MongoConfig {

@Produces
public CodecRegistry produceCodecRegistry() {
CodecRegistry defaultCodecRegistry = MongoClientSettings.getDefaultCodecRegistry();
CodecRegistry customCodecRegistry = CodecRegistries.fromCodecs(new DateCodec());

return CodecRegistries.fromRegistries(customCodecRegistry, defaultCodecRegistry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@
import io.quarkus.mongodb.panache.reactive.ReactivePanacheMongoEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.FieldNameConstants;
import org.bson.types.ObjectId;

import java.time.Instant;
import java.util.Set;

@EqualsAndHashCode(callSuper = true)
@Data
@Getter
@Setter
@MongoEntity(collection = "userGroups")
@FieldNameConstants(asEnum = true)
public class UserGroupEntity extends ReactivePanacheMongoEntity {

private String id;
private ObjectId id;
private String institutionId;
private String productId;
private String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import it.pagopa.selfcare.user.event.entity.UserGroupEntity;
import it.pagopa.selfcare.user.model.UserGroupNotificationToSend;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

@Mapper(componentModel = "cdi")
public interface UserGroupNotificationMapper {

@Mapping(target = "id", expression = "java(userGroupEntity.getId().toString())")
UserGroupNotificationToSend toUserGroupNotificationToSend(UserGroupEntity userGroupEntity);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ user-group-cdc.retry=${USER_GROUP_CDC-RETRY:3}

quarkus.rest-client.event-hub.url=${EVENT_HUB_BASE_PATH:test}
eventhub.rest-client.keyName=${SHARED_ACCESS_KEY_NAME:test}
eventhub.rest-client.key=${EVENTHUB-SC-USERS-GROUP-SELFCARE-WO-KEY-LC:test}
eventhub.rest-client.key=${EVENTHUB-SC-USER-GROUPS-SELFCARE-WO-KEY-LC:test}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import it.pagopa.selfcare.user.model.UserGroupNotificationToSend;
import jakarta.inject.Inject;
import org.bson.BsonDocument;
import org.bson.types.ObjectId;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -49,15 +50,15 @@ void consumerToSendScUserGroupEventTest() {
userGroupCdcService.consumerToSendScUserGroupEvent(document);
verify(eventHubRestClient, times(1)).
sendUserGroupMessage(notification.capture());
Assertions.assertEquals(userGroupEntity.getId(), notification.getValue().getId());
Assertions.assertEquals(userGroupEntity.getId().toString(), notification.getValue().getId());
Assertions.assertEquals(userGroupEntity.getInstitutionId(), notification.getValue().getInstitutionId());
Assertions.assertEquals(userGroupEntity.getProductId(), notification.getValue().getProductId());
Assertions.assertEquals(2, notification.getValue().getMembers().size());
}

UserGroupEntity dummyUserGroup() {
UserGroupEntity userGroupEntity = new UserGroupEntity();
userGroupEntity.setId("UserGroupId");
userGroupEntity.setId(new ObjectId());
userGroupEntity.setInstitutionId("InstitutionId");
userGroupEntity.setProductId("ProductId");
userGroupEntity.setMembers(Set.of("Member1", "Member2"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public static Map<String, String> mapPropsForTrackEvent(TrackEventInput trackEve
Optional.ofNullable(trackEventInput.getProductId()).ifPresent(value -> propertiesMap.put("productId", value));
Optional.ofNullable(trackEventInput.getInstitutionId()).ifPresent(value -> propertiesMap.put("institutionId", value));
Optional.ofNullable(trackEventInput.getProductRole()).ifPresent(value -> propertiesMap.put("productRole", value));
Optional.ofNullable(trackEventInput.getGroupMembers()).ifPresent(value -> propertiesMap.put("groupMembers", value.toString()));
Optional.ofNullable(trackEventInput.getException()).ifPresent(value -> propertiesMap.put("exec", value));
return propertiesMap;
}
Expand Down

0 comments on commit b3046ef

Please sign in to comment.