Skip to content

Commit

Permalink
Merge branch 'add-custom-logic-privacy-module' into tests-gpp-custom-…
Browse files Browse the repository at this point in the history
…logic
  • Loading branch information
osulzhenko committed Sep 19, 2023
2 parents 9c8d691 + fe425dd commit c2777ee
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
import org.prebid.server.activity.infrastructure.privacy.uscustomlogic.USCustomLogicModule;
import org.prebid.server.activity.infrastructure.rule.AndRule;
import org.prebid.server.auction.gpp.model.GppContext;
import org.prebid.server.exception.PreBidException;
import org.prebid.server.json.DecodeException;
import org.prebid.server.json.JsonLogic;
import org.prebid.server.metric.MetricName;
import org.prebid.server.metric.Metrics;
import org.prebid.server.settings.SettingsCache;
import org.prebid.server.settings.model.activity.privacy.AccountUSCustomLogicModuleConfig;

Expand All @@ -38,14 +42,17 @@ public class USCustomLogicModuleCreator implements PrivacyModuleCreator {
private final USCustomLogicGppReaderFactory gppReaderFactory;
private final JsonLogic jsonLogic;
private final Map<String, JsonLogicNode> jsonLogicNodesCache;
private final Metrics metrics;

public USCustomLogicModuleCreator(USCustomLogicGppReaderFactory gppReaderFactory,
JsonLogic jsonLogic,
Integer cacheTtl,
Integer cacheSize) {
Integer cacheSize,
Metrics metrics) {

this.gppReaderFactory = Objects.requireNonNull(gppReaderFactory);
this.jsonLogic = Objects.requireNonNull(jsonLogic);
this.metrics = Objects.requireNonNull(metrics);

jsonLogicNodesCache = cacheTtl != null && cacheSize != null
? SettingsCache.createCache(cacheTtl, cacheSize)
Expand Down Expand Up @@ -126,7 +133,16 @@ private PrivacyModule forConfig(int sectionId,
private JsonLogicNode jsonLogicNode(ObjectNode jsonLogicConfig) {
final String jsonAsString = jsonLogicConfig.toString();
return jsonLogicNodesCache != null
? jsonLogicNodesCache.computeIfAbsent(jsonAsString, jsonLogic::parse)
: jsonLogic.parse(jsonAsString);
? jsonLogicNodesCache.computeIfAbsent(jsonAsString, this::parseJsonLogicNode)
: parseJsonLogicNode(jsonAsString);
}

private JsonLogicNode parseJsonLogicNode(String jsonLogicConfig) {
try {
metrics.updateAlertsMetrics(MetricName.general);
return jsonLogic.parse(jsonLogicConfig);
} catch (DecodeException e) {
throw new PreBidException("JsonLogic exception: " + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private static class DataAggregator {
public void put(String key, Object value) {
if (value instanceof List<?> list) {
for (int i = 0; i < list.size(); i++) {
data.put(key + i, list.get(i));
data.put(key + (i + 1), list.get(i));
}
} else {
data.put(key, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ USCustomLogicModuleCreator usCustomLogicModuleCreator(
USCustomLogicGppReaderFactory gppReaderFactory,
JsonLogic jsonLogic,
@Value("${settings.in-memory-cache.ttl-seconds:#{null}}") Integer ttlSeconds,
@Value("${settings.in-memory-cache.cache-size:#{null}}") Integer cacheSize) {
@Value("${settings.in-memory-cache.cache-size:#{null}}") Integer cacheSize,
Metrics metrics) {

return new USCustomLogicModuleCreator(gppReaderFactory, jsonLogic, ttlSeconds, cacheSize);
return new USCustomLogicModuleCreator(gppReaderFactory, jsonLogic, ttlSeconds, cacheSize, metrics);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
import org.prebid.server.activity.infrastructure.privacy.usnat.reader.USNationalGppReader;
import org.prebid.server.activity.infrastructure.rule.Rule;
import org.prebid.server.auction.gpp.model.GppContextCreator;
import org.prebid.server.exception.PreBidException;
import org.prebid.server.json.DecodeException;
import org.prebid.server.json.JsonLogic;
import org.prebid.server.metric.MetricName;
import org.prebid.server.metric.Metrics;
import org.prebid.server.settings.model.activity.privacy.AccountUSCustomLogicModuleConfig;

import java.util.List;
Expand All @@ -27,6 +31,7 @@
import static java.util.Collections.singleton;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
Expand All @@ -48,6 +53,9 @@ public class USCustomLogicModuleCreatorTest extends VertxTest {
@Mock
private JsonLogic jsonLogic;

@Mock
private Metrics metrics;

private USCustomLogicModuleCreator target;

@Before
Expand All @@ -56,13 +64,14 @@ public void setUp() {
.willReturn(new USNationalGppReader(null));
given(jsonLogic.parse(any())).willReturn(JsonLogicBoolean.TRUE);

target = new USCustomLogicModuleCreator(gppReaderFactory, jsonLogic, null, null);
target = new USCustomLogicModuleCreator(gppReaderFactory, jsonLogic, null, null, metrics);
}

@Test
public void qualifierShouldReturnExpectedResult() {
// when and then
assertThat(target.qualifier()).isEqualTo(PrivacyModuleQualifier.US_CUSTOM_LOGIC);
verifyNoInteractions(metrics);
}

@Test
Expand All @@ -79,6 +88,7 @@ public void fromShouldCreateProperPrivacyModuleIfSectionsIdsIsNull() {
assertThat(privacyModule.proceed(null)).isEqualTo(Rule.Result.ABSTAIN);
verifyNoInteractions(gppReaderFactory);
verifyNoInteractions(jsonLogic);
verifyNoInteractions(metrics);
}

@Test
Expand All @@ -95,6 +105,7 @@ public void fromShouldCreateProperPrivacyModuleIfSectionsIdsIsEmpty() {
assertThat(privacyModule.proceed(null)).isEqualTo(Rule.Result.ABSTAIN);
verifyNoInteractions(gppReaderFactory);
verifyNoInteractions(jsonLogic);
verifyNoInteractions(metrics);
}

@Test
Expand All @@ -111,6 +122,7 @@ public void fromShouldCreateProperPrivacyModuleIfAllSectionsIdsSkipped() {
assertThat(privacyModule.proceed(null)).isEqualTo(Rule.Result.ABSTAIN);
verifyNoInteractions(gppReaderFactory);
verifyNoInteractions(jsonLogic);
verifyNoInteractions(metrics);
}

@Test
Expand All @@ -135,6 +147,7 @@ public void fromShouldShouldSkipNotSupportedSectionsIds() throws JsonLogicEvalua
verify(jsonLogic, times(6)).parse(eq("{}"));
verify(jsonLogic, times(6)).evaluate(any(), any());
verifyNoMoreInteractions(jsonLogic);
verifyNoInteractions(metrics);
}

@Test
Expand All @@ -154,6 +167,7 @@ public void fromShouldShouldSkipNotConfiguredSectionsIds() throws JsonLogicEvalu
verify(jsonLogic).parse(eq("{}"));
verify(jsonLogic).evaluate(any(), any());
verifyNoMoreInteractions(jsonLogic);
verifyNoInteractions(metrics);
}

@Test
Expand All @@ -170,6 +184,7 @@ public void fromShouldCreateProperPrivacyModuleIfCurrentActivityNotConfigured()
assertThat(privacyModule.proceed(null)).isEqualTo(Rule.Result.ABSTAIN);
verifyNoInteractions(gppReaderFactory);
verifyNoInteractions(jsonLogic);
verifyNoInteractions(metrics);
}

@Test
Expand All @@ -186,6 +201,7 @@ public void fromShouldCreateProperPrivacyModuleIfJsonLogicConfigIsNull() {
assertThat(privacyModule.proceed(null)).isEqualTo(Rule.Result.ABSTAIN);
verifyNoInteractions(gppReaderFactory);
verifyNoInteractions(jsonLogic);
verifyNoInteractions(metrics);
}

@Test
Expand All @@ -205,6 +221,27 @@ public void fromShouldUseDefaultValueForNormalizeSectionsIfItWasNull() throws Js
verify(jsonLogic).parse(eq("{}"));
verify(jsonLogic).evaluate(any(), any());
verifyNoMoreInteractions(jsonLogic);
verifyNoInteractions(metrics);
}

@Test
public void fromShouldThrowExceptionAndEmitMetricsOnInvalidJsonLogicConfig() {
// given
given(jsonLogic.parse(any())).willThrow(new DecodeException("Test exception"));

final PrivacyModuleCreationContext creationContext = givenCreationContext(
singletonList(7),
givenConfig(singleton(7), null, Activity.CALL_BIDDER, mapper.createObjectNode()));

// when and then
assertThatExceptionOfType(PreBidException.class).isThrownBy(() -> target.from(creationContext));

verify(jsonLogic).parse(any());
verify(metrics).updateAlertsMetrics(eq(MetricName.general));

verifyNoInteractions(gppReaderFactory);
verifyNoMoreInteractions(jsonLogic);
verifyNoMoreInteractions(metrics);
}

private static PrivacyModuleCreationContext givenCreationContext(List<Integer> sectionsIds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ public void getShouldCorrectlyHandleLists() {
// then
assertThat(result).containsAllEntriesOf(Map.of(
"Version", 0,
"KnownChildSensitiveDataConsents0", 9,
"KnownChildSensitiveDataConsents1", 8,
"KnownChildSensitiveDataConsents2", 7,
"KnownChildSensitiveDataConsents3", 6,
"KnownChildSensitiveDataConsents4", 5,
"KnownChildSensitiveDataConsents5", 4,
"KnownChildSensitiveDataConsents6", 3,
"KnownChildSensitiveDataConsents7", 2,
"KnownChildSensitiveDataConsents8", 1));
"KnownChildSensitiveDataConsents1", 9,
"KnownChildSensitiveDataConsents2", 8,
"KnownChildSensitiveDataConsents3", 7,
"KnownChildSensitiveDataConsents4", 6,
"KnownChildSensitiveDataConsents5", 5,
"KnownChildSensitiveDataConsents6", 4,
"KnownChildSensitiveDataConsents7", 3,
"KnownChildSensitiveDataConsents8", 2,
"KnownChildSensitiveDataConsents9", 1));
}

@NonNull
Expand Down

0 comments on commit c2777ee

Please sign in to comment.