Skip to content

Commit

Permalink
fix review10: refacto mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
EvgeniiMunin committed Oct 15, 2024
1 parent fab616e commit 19a9632
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.prebid.server.hooks.modules.greenbids.real.time.data.model.data;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.iab.openrtb.request.Banner;
import com.iab.openrtb.request.BidRequest;
import com.iab.openrtb.request.Device;
Expand All @@ -16,8 +15,7 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.prebid.server.exception.PreBidException;
import org.prebid.server.json.JacksonMapper;
import org.prebid.server.json.ObjectMapperProvider;
import org.prebid.server.hooks.modules.greenbids.real.time.data.util.TestBidRequestProvider;

import java.io.IOException;
import java.net.InetAddress;
Expand Down Expand Up @@ -46,15 +44,11 @@ public class GreenbidsInferenceDataServiceTest {
@Mock
private Country country;

private JacksonMapper jacksonMapper;

private GreenbidsInferenceDataService target;

@BeforeEach
public void setUp() {
final ObjectMapper mapper = ObjectMapperProvider.mapper();
jacksonMapper = new JacksonMapper(mapper);
target = new GreenbidsInferenceDataService(dbReader, jacksonMapper.mapper());
target = new GreenbidsInferenceDataService(dbReader, TestBidRequestProvider.mapper);
}

@Test
Expand All @@ -64,7 +58,7 @@ public void extractThrottlingMessagesFromBidRequestShouldReturnValidThrottlingMe
final Banner banner = givenBanner();
final Imp imp = Imp.builder()
.id("adunitcodevalue")
.ext(givenImpExt(jacksonMapper))
.ext(givenImpExt())
.banner(banner)
.build();
final Device device = givenDevice(identity());
Expand Down Expand Up @@ -105,7 +99,7 @@ public void extractThrottlingMessagesFromBidRequestShouldHandleMissingIp() {
final Banner banner = givenBanner();
final Imp imp = Imp.builder()
.id("adunitcodevalue")
.ext(givenImpExt(jacksonMapper))
.ext(givenImpExt())
.banner(banner)
.build();
final Device device = givenDeviceWithoutIp(identity());
Expand Down Expand Up @@ -142,7 +136,7 @@ public void extractThrottlingMessagesFromBidRequestShouldThrowPreBidExceptionWhe
final Banner banner = givenBanner();
final Imp imp = Imp.builder()
.id("adunitcodevalue")
.ext(givenImpExt(jacksonMapper))
.ext(givenImpExt())
.banner(banner)
.build();
final Device device = givenDevice(identity());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.prebid.server.hooks.modules.greenbids.real.time.data.model.predictor;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.benmanes.caffeine.cache.Cache;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.Bucket;
Expand All @@ -16,7 +15,7 @@
import org.prebid.server.exception.PreBidException;
import org.prebid.server.hooks.modules.greenbids.real.time.data.core.ThrottlingThresholds;
import org.prebid.server.hooks.modules.greenbids.real.time.data.core.ThrottlingThresholdsFactory;
import org.prebid.server.json.ObjectMapperProvider;
import org.prebid.server.hooks.modules.greenbids.real.time.data.util.TestBidRequestProvider;

import java.io.IOException;
import java.lang.reflect.Field;
Expand Down Expand Up @@ -59,18 +58,15 @@ public class ThresholdCacheTest {

private Vertx vertx;

private ObjectMapper mapper;

private ThresholdCache target;

@BeforeEach
public void setUp() {
mapper = ObjectMapperProvider.mapper();
vertx = Vertx.vertx();
target = new ThresholdCache(
storage,
GCS_BUCKET_NAME,
mapper,
TestBidRequestProvider.mapper,
cache,
THRESHOLD_CACHE_KEY_PREFIX,
vertx,
Expand Down Expand Up @@ -127,7 +123,8 @@ public void getShouldFetchThresholdsWhenNotInCache() throws IOException {
when(storage.get(GCS_BUCKET_NAME)).thenReturn(bucket);
when(bucket.get(THRESHOLDS_PATH)).thenReturn(blob);
when(blob.getContent()).thenReturn(bytes);
when(throttlingThresholdsFactory.create(bytes, mapper)).thenReturn(throttlingThresholds);
when(throttlingThresholdsFactory.create(bytes, TestBidRequestProvider.mapper))
.thenReturn(throttlingThresholds);

// when
final Future<ThrottlingThresholds> future = target.get(THRESHOLDS_PATH, PBUUID);
Expand Down Expand Up @@ -167,7 +164,7 @@ public void getShouldThrowExceptionWhenLoadingJsonFails() throws IOException {
when(storage.get(GCS_BUCKET_NAME)).thenReturn(bucket);
when(bucket.get(THRESHOLDS_PATH)).thenReturn(blob);
when(blob.getContent()).thenReturn(bytes);
when(throttlingThresholdsFactory.create(bytes, mapper)).thenThrow(
when(throttlingThresholdsFactory.create(bytes, TestBidRequestProvider.mapper)).thenThrow(
new IOException("Failed to load throttling thresholds json"));

// when
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.prebid.server.hooks.modules.greenbids.real.time.data.model.result;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.iab.openrtb.request.Banner;
import com.iab.openrtb.request.BidRequest;
import com.iab.openrtb.request.Device;
Expand All @@ -12,11 +11,7 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.prebid.server.analytics.reporter.greenbids.model.Ortb2ImpExtResult;
import org.prebid.server.hooks.modules.greenbids.real.time.data.core.Partner;
import org.prebid.server.hooks.modules.greenbids.real.time.data.model.result.GreenbidsInvocationResult;
import org.prebid.server.hooks.modules.greenbids.real.time.data.model.result.GreenbidsInvocationService;
import org.prebid.server.hooks.v1.InvocationAction;
import org.prebid.server.json.JacksonMapper;
import org.prebid.server.json.ObjectMapperProvider;

import java.util.HashMap;
import java.util.List;
Expand All @@ -32,14 +27,10 @@
@ExtendWith(MockitoExtension.class)
public class GreenbidsInvocationServiceTest {

private JacksonMapper jacksonMapper;

private GreenbidsInvocationService target;

@BeforeEach
public void setUp() {
final ObjectMapper mapper = ObjectMapperProvider.mapper();
jacksonMapper = new JacksonMapper(mapper);
target = new GreenbidsInvocationService();
}

Expand All @@ -49,7 +40,7 @@ public void createGreenbidsInvocationResultShouldReturnUpdateBidRequestWhenNotEx
final Banner banner = givenBanner();
final Imp imp = Imp.builder()
.id("adunitcodevalue")
.ext(givenImpExt(jacksonMapper))
.ext(givenImpExt())
.banner(banner)
.build();
final Device device = givenDevice(identity());
Expand Down Expand Up @@ -86,7 +77,7 @@ public void createGreenbidsInvocationResultShouldReturnNoActionWhenExploration()
final Banner banner = givenBanner();
final Imp imp = Imp.builder()
.id("adunitcodevalue")
.ext(givenImpExt(jacksonMapper))
.ext(givenImpExt())
.banner(banner)
.build();
final Device device = givenDevice(identity());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.prebid.server.hooks.modules.greenbids.real.time.data.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.iab.openrtb.request.Banner;
Expand All @@ -9,6 +10,7 @@
import com.iab.openrtb.request.Imp;
import com.iab.openrtb.request.Site;
import org.prebid.server.json.JacksonMapper;
import org.prebid.server.json.ObjectMapperProvider;
import org.prebid.server.proto.openrtb.ext.request.ExtRequest;

import java.util.Collections;
Expand All @@ -17,6 +19,9 @@

public class TestBidRequestProvider {

public static final ObjectMapper mapper = ObjectMapperProvider.mapper();
public static final JacksonMapper jacksonMapper = new JacksonMapper(mapper);

private TestBidRequestProvider() { }

public static BidRequest givenBidRequest(
Expand All @@ -37,7 +42,7 @@ public static Site givenSite(UnaryOperator<Site.SiteBuilder> siteCustomizer) {
return siteCustomizer.apply(Site.builder().domain("www.leparisien.fr")).build();
}

public static ObjectNode givenImpExt(JacksonMapper jacksonMapper) {
public static ObjectNode givenImpExt() {
final ObjectNode bidderNode = jacksonMapper.mapper().createObjectNode();

final ObjectNode rubiconNode = jacksonMapper.mapper().createObjectNode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import ai.onnxruntime.OrtException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.github.benmanes.caffeine.cache.Cache;
Expand Down Expand Up @@ -35,6 +34,7 @@
import org.prebid.server.hooks.modules.greenbids.real.time.data.model.predictor.ThresholdCache;
import org.prebid.server.hooks.modules.greenbids.real.time.data.model.result.AnalyticsResult;
import org.prebid.server.hooks.modules.greenbids.real.time.data.model.result.GreenbidsInvocationService;
import org.prebid.server.hooks.modules.greenbids.real.time.data.util.TestBidRequestProvider;
import org.prebid.server.hooks.modules.greenbids.real.time.data.v1.model.analytics.ActivityImpl;
import org.prebid.server.hooks.modules.greenbids.real.time.data.v1.model.analytics.AppliedToImpl;
import org.prebid.server.hooks.modules.greenbids.real.time.data.v1.model.analytics.ResultImpl;
Expand All @@ -46,8 +46,6 @@
import org.prebid.server.hooks.v1.analytics.Tags;
import org.prebid.server.hooks.v1.auction.AuctionInvocationContext;
import org.prebid.server.hooks.v1.auction.AuctionRequestPayload;
import org.prebid.server.json.JacksonMapper;
import org.prebid.server.json.ObjectMapperProvider;
import org.prebid.server.model.HttpRequestContext;
import org.prebid.server.proto.openrtb.ext.request.ExtRequest;
import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebid;
Expand Down Expand Up @@ -80,14 +78,10 @@ public class GreenbidsRealTimeDataProcessedAuctionRequestHookTest {
@Mock
private Cache<String, ThrottlingThresholds> thresholdsCacheWithExpiration;

private JacksonMapper jacksonMapper;

private GreenbidsRealTimeDataProcessedAuctionRequestHook target;

@BeforeEach
public void setUp() throws IOException {
final ObjectMapper mapper = ObjectMapperProvider.mapper();
jacksonMapper = new JacksonMapper(mapper);
final Storage storage = StorageOptions.newBuilder()
.setProjectId("test_project").build().getService();
final File database = new File("src/test/resources/GeoLite2-Country.mmdb");
Expand All @@ -105,7 +99,7 @@ public void setUp() throws IOException {
final ThresholdCache thresholdCache = new ThresholdCache(
storage,
"test_bucket",
ObjectMapperProvider.mapper(),
TestBidRequestProvider.mapper,
thresholdsCacheWithExpiration,
"throttlingThresholds_",
Vertx.vertx(),
Expand All @@ -115,10 +109,10 @@ public void setUp() throws IOException {
thresholdCache);
final GreenbidsInferenceDataService greenbidsInferenceDataService = new GreenbidsInferenceDataService(
dbReader,
ObjectMapperProvider.mapper());
TestBidRequestProvider.mapper);
final GreenbidsInvocationService greenbidsInvocationService = new GreenbidsInvocationService();
target = new GreenbidsRealTimeDataProcessedAuctionRequestHook(
ObjectMapperProvider.mapper(),
TestBidRequestProvider.mapper,
filterService,
onnxModelRunnerWithThresholds,
greenbidsInferenceDataService,
Expand All @@ -132,7 +126,7 @@ public void callShouldExitEarlyWhenPartnerNotActivatedInBidRequest() {

final Imp imp = Imp.builder()
.id("adunitcodevalue")
.ext(givenImpExt(jacksonMapper))
.ext(givenImpExt())
.banner(banner)
.build();

Expand Down Expand Up @@ -163,7 +157,7 @@ public void callShouldNotFilterBiddersAndReturnAnalyticsTagWhenExploration() thr

final Imp imp = Imp.builder()
.id("adunitcodevalue")
.ext(givenImpExt(jacksonMapper))
.ext(givenImpExt())
.banner(banner)
.build();

Expand Down Expand Up @@ -209,7 +203,7 @@ public void callShouldFilterBiddersBasedOnModelWhenAnyFeatureNotAvailable() thro

final Imp imp = Imp.builder()
.id("adunitcodevalue")
.ext(givenImpExt(jacksonMapper))
.ext(givenImpExt())
.banner(banner)
.build();

Expand All @@ -225,7 +219,7 @@ public void callShouldFilterBiddersBasedOnModelWhenAnyFeatureNotAvailable() thro
.thenReturn(givenThrottlingThresholds());

final BidRequest expectedBidRequest = expectedUpdatedBidRequest(
request -> request, jacksonMapper, explorationRate);
request -> request, explorationRate);
final AnalyticsResult expectedAnalyticsResult = expectedAnalyticsResult(false, false);

// when
Expand Down Expand Up @@ -264,7 +258,7 @@ public void callShouldFilterBiddersBasedOnModelResults() throws OrtException, IO

final Imp imp = Imp.builder()
.id("adunitcodevalue")
.ext(givenImpExt(jacksonMapper))
.ext(givenImpExt())
.banner(banner)
.build();

Expand All @@ -281,7 +275,7 @@ public void callShouldFilterBiddersBasedOnModelResults() throws OrtException, IO
.thenReturn(givenThrottlingThresholds());

final BidRequest expectedBidRequest = expectedUpdatedBidRequest(
request -> request, jacksonMapper, explorationRate);
request -> request, explorationRate);
final AnalyticsResult expectedAnalyticsResult = expectedAnalyticsResult(false, false);

// when
Expand Down Expand Up @@ -313,12 +307,12 @@ public void callShouldFilterBiddersBasedOnModelResults() throws OrtException, IO
}

static ExtRequest givenExtRequest(Double explorationRate) {
final ObjectNode greenbidsNode = new ObjectMapper().createObjectNode();
final ObjectNode greenbidsNode = TestBidRequestProvider.mapper.createObjectNode();
greenbidsNode.put("pbuid", "test-pbuid");
greenbidsNode.put("targetTpr", 0.60);
greenbidsNode.put("explorationRate", explorationRate);

final ObjectNode analyticsNode = new ObjectMapper().createObjectNode();
final ObjectNode analyticsNode = TestBidRequestProvider.mapper.createObjectNode();
analyticsNode.set("greenbids-rtd", greenbidsNode);

return ExtRequest.of(ExtRequestPrebid
Expand Down Expand Up @@ -351,25 +345,24 @@ private OnnxModelRunner givenOnnxModelRunner() throws OrtException, IOException
}

private ThrottlingThresholds givenThrottlingThresholds() throws IOException {
final JsonNode thresholdsJsonNode = jacksonMapper.mapper().readTree(
final JsonNode thresholdsJsonNode = TestBidRequestProvider.mapper.readTree(
Files.newInputStream(Paths.get(
"src/test/resources/thresholds_pbuid=test-pbuid.json")));
return jacksonMapper.mapper()
return TestBidRequestProvider.mapper
.treeToValue(thresholdsJsonNode, ThrottlingThresholds.class);
}

private BidRequest expectedUpdatedBidRequest(
UnaryOperator<BidRequest.BidRequestBuilder> bidRequestCustomizer,
JacksonMapper jacksonMapper,
Double explorationRate) {

final Banner banner = givenBanner();

final ObjectNode bidderNode = jacksonMapper.mapper().createObjectNode();
final ObjectNode prebidNode = jacksonMapper.mapper().createObjectNode();
final ObjectNode bidderNode = TestBidRequestProvider.mapper.createObjectNode();
final ObjectNode prebidNode = TestBidRequestProvider.mapper.createObjectNode();
prebidNode.set("bidder", bidderNode);

final ObjectNode extNode = jacksonMapper.mapper().createObjectNode();
final ObjectNode extNode = TestBidRequestProvider.mapper.createObjectNode();
extNode.set("prebid", prebidNode);
extNode.set("tid", TextNode.valueOf("67eaab5f-27a6-4689-93f7-bd8f024576e3"));

Expand Down Expand Up @@ -432,6 +425,6 @@ private Result toResult(AnalyticsResult analyticsResult) {
}

private ObjectNode toObjectNode(Map<String, Ortb2ImpExtResult> values) {
return values != null ? jacksonMapper.mapper().valueToTree(values) : null;
return values != null ? TestBidRequestProvider.mapper.valueToTree(values) : null;
}
}

0 comments on commit 19a9632

Please sign in to comment.