Skip to content

Commit

Permalink
Error message fix, added unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
VeryExtraordinaryUsername committed Nov 24, 2023
1 parent 0a28aaf commit a5cff21
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/main/java/org/prebid/server/handler/SetuidHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -133,17 +135,18 @@ private static UsersyncMethodType preferredUserSyncType(Usersyncer usersyncer) {
}

private static void validateUsersyncers(Stream<Usersyncer> usersyncers) {
final Stream<String> cookieFamilyNameDuplicates = usersyncers.map(Usersyncer::getCookieFamilyName)
final List<String> 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));
}
}

Expand Down
35 changes: 35 additions & 0 deletions src/test/java/org/prebid/server/handler/SetuidHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
Expand Down

0 comments on commit a5cff21

Please sign in to comment.