From a5cff210ba711b8cc9bd72acf548e785528fd898 Mon Sep 17 00:00:00 2001 From: Serhii Kolomiiets Date: Fri, 24 Nov 2023 19:20:12 +0200 Subject: [PATCH] Error message fix, added unit test --- .../prebid/server/handler/SetuidHandler.java | 11 +++--- .../server/handler/SetuidHandlerTest.java | 35 +++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/prebid/server/handler/SetuidHandler.java b/src/main/java/org/prebid/server/handler/SetuidHandler.java index 2832dd03bd9..205a1292edc 100644 --- a/src/main/java/org/prebid/server/handler/SetuidHandler.java +++ b/src/main/java/org/prebid/server/handler/SetuidHandler.java @@ -11,6 +11,7 @@ import io.vertx.core.logging.Logger; import io.vertx.core.logging.LoggerFactory; import io.vertx.ext.web.RoutingContext; +import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.StringUtils; import org.prebid.server.activity.Activity; @@ -52,6 +53,7 @@ import org.prebid.server.util.HttpUtil; import java.util.Collections; +import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Optional; @@ -133,17 +135,18 @@ private static UsersyncMethodType preferredUserSyncType(Usersyncer usersyncer) { } private static void validateUsersyncers(Stream usersyncers) { - final Stream cookieFamilyNameDuplicates = usersyncers.map(Usersyncer::getCookieFamilyName) + final List cookieFamilyNameDuplicates = usersyncers.map(Usersyncer::getCookieFamilyName) .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())) .entrySet() .stream() .filter(name -> name.getValue() > 1) .map(Map.Entry::getKey) - .distinct(); - if (cookieFamilyNameDuplicates.findAny().isPresent()) { + .distinct() + .toList(); + if (CollectionUtils.isNotEmpty(cookieFamilyNameDuplicates)) { throw new IllegalArgumentException( "Duplicated \"cookie-family-name\" found, values: " - + String.join(", ", cookieFamilyNameDuplicates.toList())); + + String.join(", ", cookieFamilyNameDuplicates)); } } diff --git a/src/test/java/org/prebid/server/handler/SetuidHandlerTest.java b/src/test/java/org/prebid/server/handler/SetuidHandlerTest.java index 4fb10c14128..da2885f42d2 100644 --- a/src/test/java/org/prebid/server/handler/SetuidHandlerTest.java +++ b/src/test/java/org/prebid/server/handler/SetuidHandlerTest.java @@ -10,6 +10,8 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.function.Executable; import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.junit.MockitoJUnit; @@ -771,6 +773,39 @@ public void shouldPassSuccessfulEventToAnalyticsReporter() { .build()); } + @Test + public void shouldThrowExceptionInCaseOfCookieFamilyNameDuplicates() { + // given + final Clock clock = Clock.fixed(Instant.now(), ZoneId.systemDefault()); + final String firstDuplicateName = "firstBidderWithDuplicate"; + final String secondDuplicateName = "secondBidderWithDuplicate"; + given(bidderCatalog.names()) + .willReturn(new HashSet<>(asList(RUBICON, FACEBOOK, firstDuplicateName, secondDuplicateName))); + given(bidderCatalog.usersyncerByName(eq(firstDuplicateName))).willReturn( + Optional.of(Usersyncer.of(RUBICON, iframeMethod(), redirectMethod()))); + given(bidderCatalog.usersyncerByName(eq(secondDuplicateName))).willReturn( + Optional.of(Usersyncer.of(FACEBOOK, iframeMethod(), redirectMethod()))); + final Executable exceptionSource = () -> new SetuidHandler( + 2000, + uidsCookieService, + applicationSettings, + bidderCatalog, + privacyEnforcementService, + gppService, + activityInfrastructureCreator, + tcfDefinerService, + analyticsReporterDelegator, + metrics, + new TimeoutFactory(clock)); + + //when + final IllegalArgumentException exception = + Assertions.assertThrows(IllegalArgumentException.class, exceptionSource); + + //then + assertThat(exception).hasMessage("Duplicated \"cookie-family-name\" found, values: audienceNetwork, rubicon"); + } + private String getUidsCookie() { return httpResponse.headers().get("Set-Cookie"); }