From 784dc57880c3502e81e4ec92dca481b6f0d5dca8 Mon Sep 17 00:00:00 2001 From: tkuzynow Date: Wed, 24 Apr 2024 08:22:17 +0200 Subject: [PATCH 1/3] fix: class cast exception for statistics --- .../RegistrationStatisticsDTOConverter.java | 8 ++-- ...egistrationStatisticsDTOConverterTest.java | 43 +++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/caritas/cob/statisticsservice/api/helper/RegistrationStatisticsDTOConverter.java b/src/main/java/de/caritas/cob/statisticsservice/api/helper/RegistrationStatisticsDTOConverter.java index bb87237..5ceb519 100644 --- a/src/main/java/de/caritas/cob/statisticsservice/api/helper/RegistrationStatisticsDTOConverter.java +++ b/src/main/java/de/caritas/cob/statisticsservice/api/helper/RegistrationStatisticsDTOConverter.java @@ -60,10 +60,12 @@ private Integer countEventsPerAdviceSeekerMatchingOnMetadataByReceiverId(String private long getCountOfEventsPerAdviceSeekerMatchingOnMetadata(String adviceSeekerId, Collection statisticsEvents) { return statisticsEvents.stream() + .filter(event -> event.getMetaData() instanceof AdviceSeekerAwareMetaData) .filter(event -> { - AdviceSeekerAwareMetaData metaData = (AdviceSeekerAwareMetaData) event.getMetaData(); - return metaData.getAdviceSeekerId() != null && metaData.getAdviceSeekerId() - .equals(adviceSeekerId); + AdviceSeekerAwareMetaData metaData = (AdviceSeekerAwareMetaData) event.getMetaData(); + return metaData.getAdviceSeekerId() != null && metaData.getAdviceSeekerId() + .equals(adviceSeekerId); + }) .count(); } diff --git a/src/test/java/de/caritas/cob/statisticsservice/api/helper/RegistrationStatisticsDTOConverterTest.java b/src/test/java/de/caritas/cob/statisticsservice/api/helper/RegistrationStatisticsDTOConverterTest.java index 0b82549..936587a 100644 --- a/src/test/java/de/caritas/cob/statisticsservice/api/helper/RegistrationStatisticsDTOConverterTest.java +++ b/src/test/java/de/caritas/cob/statisticsservice/api/helper/RegistrationStatisticsDTOConverterTest.java @@ -7,6 +7,7 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; +import com.google.common.collect.Lists; import de.caritas.cob.statisticsservice.api.model.EventType; import de.caritas.cob.statisticsservice.api.model.RegistrationStatisticsResponseDTO; import de.caritas.cob.statisticsservice.api.model.UserRole; @@ -19,6 +20,7 @@ import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.meta.DeleteAccountMetaData; import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.meta.RegistrationMetaData; import java.time.Instant; +import java.util.LinkedHashMap; import java.util.List; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; @@ -73,6 +75,47 @@ void convertStatisticsEvent_Should_convertToRegistrationStatisticResponse() { is("aReferer")); } + @Test + void convertStatisticsEvent_Should_convertToRegistrationStatisticResponseWithoutErrorIfMetadataIsLinkedHashMap() { + // given + givenValidStatisticEvent(1L); + + var videoCallStartedEvent = StatisticsEvent.builder() + .sessionId(1L) + .eventType(EventType.REGISTRATION) + .user(User.builder().userRole(UserRole.ASKER).id(ASKER_ID).build()) + .consultingType(ConsultingType.builder().id(CONSULTING_TYPE_ID).build()) + .agency(Agency.builder().id(AGENCY_ID).build()) + .timestamp(Instant.now()) + .metaData(new LinkedHashMap<>()) + .build(); + + var statisticEventsContainer = StatisticEventsContainer.builder().videoCallStartedEvents(Lists.newArrayList(videoCallStartedEvent)).build(); + // when + RegistrationStatisticsResponseDTO result = registrationStatisticsDTOConverter.convertStatisticsEvent(testEvent, statisticEventsContainer); + + // then + assertThat(result.getUserId(), is(ASKER_ID)); + assertThat(result.getRegistrationDate(), + is("2022-09-15T09:14:45Z")); + assertThat(result.getAge(), is(26)); + assertThat(result.getGender(), is("FEMALE")); + assertThat(result.getMainTopicInternalAttribute(), + is("alk")); + assertThat(result.getTopicsInternalAttributes(), + is(List.of("alk", "drogen"))); + assertThat(result.getPostalCode(), is("12345")); + assertThat(result.getCounsellingRelation(), + is("SELF_COUNSELLING")); + assertThat(result.getTenantName(), + is("tenantName")); + assertThat(result.getAgencyName(), + is("agencyName")); + assertThat(result.getReferer(), + is("aReferer")); + } + + @Test void convertStatisticsEvent_Should_notFail_When_archiveSessionEventsAreNull() { // given From d8ee53788b6243bf8c59ca6eef8b6df830761cd3 Mon Sep 17 00:00:00 2001 From: tkuzynow Date: Wed, 24 Apr 2024 09:08:24 +0200 Subject: [PATCH 2/3] fix: class cast exception for statistics --- .../RegistrationStatisticsDTOConverter.java | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/main/java/de/caritas/cob/statisticsservice/api/helper/RegistrationStatisticsDTOConverter.java b/src/main/java/de/caritas/cob/statisticsservice/api/helper/RegistrationStatisticsDTOConverter.java index 5ceb519..568ce47 100644 --- a/src/main/java/de/caritas/cob/statisticsservice/api/helper/RegistrationStatisticsDTOConverter.java +++ b/src/main/java/de/caritas/cob/statisticsservice/api/helper/RegistrationStatisticsDTOConverter.java @@ -21,8 +21,10 @@ public class RegistrationStatisticsDTOConverter { public RegistrationStatisticsResponseDTO convertStatisticsEvent( StatisticsEvent rawEvent, StatisticEventsContainer statisticEventsContainer) { RegistrationMetaData metadata = (RegistrationMetaData) rawEvent.getMetaData(); - String maxArchiveDate = findMaxArchiveDate(rawEvent.getSessionId(), statisticEventsContainer.getArchiveSessionEvents()); - String deleteAccountDate = findDeleteAccountDate(rawEvent.getUser().getId(), statisticEventsContainer.getDeleteAccountEvents()); + String maxArchiveDate = findMaxArchiveDate(rawEvent.getSessionId(), + statisticEventsContainer.getArchiveSessionEvents()); + String deleteAccountDate = findDeleteAccountDate(rawEvent.getUser().getId(), + statisticEventsContainer.getDeleteAccountEvents()); return new RegistrationStatisticsResponseDTO() .userId(rawEvent.getUser().getId()) @@ -38,9 +40,14 @@ public RegistrationStatisticsResponseDTO convertStatisticsEvent( .postalCode(metadata.getPostalCode()) .referer(metadata.getReferer()) .attendedVideoCallsCount( - countEventsPerAdviceSeekerMatchingOnMetadata(rawEvent.getUser().getId(), statisticEventsContainer.getVideoCallStartedEvents())) - .appointmentsBookedCount(countEventsPerAdviceSeekerMatchingOnMetadata(rawEvent.getUser().getId(), statisticEventsContainer.getBookingCreatedEvents())) - .consultantMessagesCount(countEventsPerAdviceSeekerMatchingOnMetadataByReceiverId(rawEvent.getUser().getId(), statisticEventsContainer.getConsultantMessageCreatedEvents())); + countEventsPerAdviceSeekerMatchingOnMetadata(rawEvent.getUser().getId(), + statisticEventsContainer.getVideoCallStartedEvents())) + .appointmentsBookedCount( + countEventsPerAdviceSeekerMatchingOnMetadata(rawEvent.getUser().getId(), + statisticEventsContainer.getBookingCreatedEvents())) + .consultantMessagesCount( + countEventsPerAdviceSeekerMatchingOnMetadataByReceiverId(rawEvent.getUser().getId(), + statisticEventsContainer.getConsultantMessageCreatedEvents())); } private Integer countEventsPerAdviceSeekerMatchingOnMetadata(String adviceSeekerId, @@ -54,7 +61,8 @@ private Integer countEventsPerAdviceSeekerMatchingOnMetadataByReceiverId(String Collection events) { return events != null - ? (int) getCountOfEventsPerAdviceSeekerMatchingOnMetadataByReceiverId(adviceSeekerId, events) : 0; + ? (int) getCountOfEventsPerAdviceSeekerMatchingOnMetadataByReceiverId(adviceSeekerId, + events) : 0; } private long getCountOfEventsPerAdviceSeekerMatchingOnMetadata(String adviceSeekerId, @@ -62,9 +70,9 @@ private long getCountOfEventsPerAdviceSeekerMatchingOnMetadata(String adviceSeek return statisticsEvents.stream() .filter(event -> event.getMetaData() instanceof AdviceSeekerAwareMetaData) .filter(event -> { - AdviceSeekerAwareMetaData metaData = (AdviceSeekerAwareMetaData) event.getMetaData(); - return metaData.getAdviceSeekerId() != null && metaData.getAdviceSeekerId() - .equals(adviceSeekerId); + AdviceSeekerAwareMetaData metaData = (AdviceSeekerAwareMetaData) event.getMetaData(); + return metaData.getAdviceSeekerId() != null && metaData.getAdviceSeekerId() + .equals(adviceSeekerId); }) .count(); @@ -85,14 +93,16 @@ private String getEndDate(String maxArchiveDate, String deleteAccountDate) { return deleteAccountDate != null ? deleteAccountDate : maxArchiveDate; } - private String findDeleteAccountDate(String userId, Collection deleteAccountEvents) { + private String findDeleteAccountDate(String userId, + Collection deleteAccountEvents) { return deleteAccountEvents != null ? deleteAccountEvents.stream() .filter(event -> event.getUser() != null && event.getUser().getId().equals(userId)) .map(event -> ((DeleteAccountMetaData) event.getMetaData()).getDeleteDate()) .findFirst().orElse(null) : null; } - private String findMaxArchiveDate(Long sessionId, Collection archiveSessionEvents) { + private String findMaxArchiveDate(Long sessionId, + Collection archiveSessionEvents) { var maxArchiveEvent = findMaxArchiveSessionEvent(sessionId, archiveSessionEvents); if (maxArchiveEvent.isPresent()) { ArchiveMetaData metaData = (ArchiveMetaData) maxArchiveEvent.get().getMetaData(); @@ -101,7 +111,8 @@ private String findMaxArchiveDate(Long sessionId, Collection ar return null; } - private Optional findMaxArchiveSessionEvent(Long sessionId, Collection archiveSessionEvents) { + private Optional findMaxArchiveSessionEvent(Long sessionId, + Collection archiveSessionEvents) { return nonNull(archiveSessionEvents) ? archiveSessionEvents.stream() .filter(event -> event.getSessionId() != null && event.getSessionId().equals(sessionId)) .max(comparing(StatisticsEvent::getTimestamp)) : Optional.empty(); From 69757f8020b42feea90a68fbe76673fce97b95f7 Mon Sep 17 00:00:00 2001 From: tkuzynow Date: Wed, 24 Apr 2024 10:30:05 +0200 Subject: [PATCH 3/3] fix: class cast exception for statistics --- .../RegistrationStatisticsDTOConverter.java | 1 + ...egistrationStatisticsDTOConverterTest.java | 27 ++++++++++++------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/main/java/de/caritas/cob/statisticsservice/api/helper/RegistrationStatisticsDTOConverter.java b/src/main/java/de/caritas/cob/statisticsservice/api/helper/RegistrationStatisticsDTOConverter.java index 568ce47..43abae5 100644 --- a/src/main/java/de/caritas/cob/statisticsservice/api/helper/RegistrationStatisticsDTOConverter.java +++ b/src/main/java/de/caritas/cob/statisticsservice/api/helper/RegistrationStatisticsDTOConverter.java @@ -81,6 +81,7 @@ private long getCountOfEventsPerAdviceSeekerMatchingOnMetadata(String adviceSeek private long getCountOfEventsPerAdviceSeekerMatchingOnMetadataByReceiverId(String adviceSeekerId, Collection createMessageEvents) { return createMessageEvents.stream() + .filter(event -> event.getMetaData() instanceof CreateMessageMetaData) .filter(event -> { CreateMessageMetaData metaData = (CreateMessageMetaData) event.getMetaData(); return metaData.getReceiverId() != null && metaData.getReceiverId() diff --git a/src/test/java/de/caritas/cob/statisticsservice/api/helper/RegistrationStatisticsDTOConverterTest.java b/src/test/java/de/caritas/cob/statisticsservice/api/helper/RegistrationStatisticsDTOConverterTest.java index 936587a..0774200 100644 --- a/src/test/java/de/caritas/cob/statisticsservice/api/helper/RegistrationStatisticsDTOConverterTest.java +++ b/src/test/java/de/caritas/cob/statisticsservice/api/helper/RegistrationStatisticsDTOConverterTest.java @@ -52,7 +52,8 @@ void convertStatisticsEvent_Should_convertToRegistrationStatisticResponse() { givenValidStatisticEvent(1L); // when - RegistrationStatisticsResponseDTO result = registrationStatisticsDTOConverter.convertStatisticsEvent(testEvent, new StatisticEventsContainer()); + RegistrationStatisticsResponseDTO result = registrationStatisticsDTOConverter.convertStatisticsEvent( + testEvent, new StatisticEventsContainer()); // then assertThat(result.getUserId(), is(ASKER_ID)); @@ -76,11 +77,11 @@ void convertStatisticsEvent_Should_convertToRegistrationStatisticResponse() { } @Test - void convertStatisticsEvent_Should_convertToRegistrationStatisticResponseWithoutErrorIfMetadataIsLinkedHashMap() { + void convertStatisticsEvent_Should_convertToRegistrationStatisticResponse_SkipCounting() { // given givenValidStatisticEvent(1L); - var videoCallStartedEvent = StatisticsEvent.builder() + var eventWithInvalidMetadataType = StatisticsEvent.builder() .sessionId(1L) .eventType(EventType.REGISTRATION) .user(User.builder().userRole(UserRole.ASKER).id(ASKER_ID).build()) @@ -90,9 +91,13 @@ void convertStatisticsEvent_Should_convertToRegistrationStatisticResponseWithout .metaData(new LinkedHashMap<>()) .build(); - var statisticEventsContainer = StatisticEventsContainer.builder().videoCallStartedEvents(Lists.newArrayList(videoCallStartedEvent)).build(); + var statisticEventsContainer = StatisticEventsContainer.builder() + .videoCallStartedEvents(Lists.newArrayList(eventWithInvalidMetadataType)) + .bookingCreatedEvents(Lists.newArrayList(eventWithInvalidMetadataType)) + .consultantMessageCreatedEvents(Lists.newArrayList(eventWithInvalidMetadataType)).build(); // when - RegistrationStatisticsResponseDTO result = registrationStatisticsDTOConverter.convertStatisticsEvent(testEvent, statisticEventsContainer); + RegistrationStatisticsResponseDTO result = registrationStatisticsDTOConverter.convertStatisticsEvent( + testEvent, statisticEventsContainer); // then assertThat(result.getUserId(), is(ASKER_ID)); @@ -220,13 +225,15 @@ private void givenValidStatisticEvent(Long sessionId) { } private void givenAccountDeleteStatisticEvents() { - deleteAccountEvents = List.of(deleteEvent(ASKER_ID, "2022-10-19T10:00:00.00Z", "delete date for user 1"), + deleteAccountEvents = List.of( + deleteEvent(ASKER_ID, "2022-10-19T10:00:00.00Z", "delete date for user 1"), deleteEvent("user 2", "2022-10-17T10:00:00.00Z", "delete date for user 2")); } private void givenValidArchiveStatisticEvents() { - archiveSessionEvents = List.of(archiveEvent(1L, "2022-10-17T10:00:00.00Z", "1 end date for session 1"), + archiveSessionEvents = List.of( + archiveEvent(1L, "2022-10-17T10:00:00.00Z", "1 end date for session 1"), archiveEvent(1L, "2022-10-18T10:00:00.00Z", "2 end date for session 1"), archiveEvent(2L, "2022-10-18T10:00:00.00Z", "end date for session 2"), archiveEvent(999L, "2022-10-19T10:00:00.00Z", "dummy end date")); @@ -234,13 +241,15 @@ private void givenValidArchiveStatisticEvents() { private StatisticsEvent archiveEvent(Long sessionId, String timestampString, String endDate) { Object metaData = ArchiveMetaData.builder().endDate(endDate).build(); - return StatisticsEvent.builder().timestamp(Instant.parse(timestampString)).sessionId(sessionId).metaData(metaData).build(); + return StatisticsEvent.builder().timestamp(Instant.parse(timestampString)).sessionId(sessionId) + .metaData(metaData).build(); } private StatisticsEvent deleteEvent(String userId, String timestampString, String deleteDate) { Object metaData = DeleteAccountMetaData.builder().deleteDate(deleteDate).build(); User user = new User(); user.setId(userId); - return StatisticsEvent.builder().timestamp(Instant.parse(timestampString)).user(user).metaData(metaData).build(); + return StatisticsEvent.builder().timestamp(Instant.parse(timestampString)).user(user) + .metaData(metaData).build(); } }