Skip to content

Commit 334ee98

Browse files
committed
Add units
1 parent c9263e2 commit 334ee98

2 files changed

Lines changed: 510 additions & 9 deletions

File tree

src/main/java/org/prebid/server/auction/externalortb/ProfilesProcessor.java

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,21 @@ public Future<BidRequest> process(AuctionContext auctionContext, BidRequest bidR
8585
return Future.succeededFuture(bidRequest);
8686
}
8787

88+
final boolean failOnUnknown = isFailOnUnknown(auctionContext.getAccount());
89+
8890
return fetchProfiles(accountId, profilesIds, timeoutMillis(bidRequest))
89-
.compose(profiles -> emitMetrics(accountId, profiles, auctionContext))
91+
.compose(profiles -> emitMetrics(accountId, profiles, auctionContext, failOnUnknown))
9092
.map(profiles -> mergeResults(
91-
applyRequestProfiles(profilesIds.request(), profiles.getStoredIdToRequest(), bidRequest),
92-
applyImpsProfiles(profilesIds.imps(), profiles.getStoredIdToImp(), bidRequest.getImp())))
93+
applyRequestProfiles(
94+
profilesIds.request(),
95+
profiles.getStoredIdToRequest(),
96+
bidRequest,
97+
failOnUnknown),
98+
applyImpsProfiles(
99+
profilesIds.imps(),
100+
profiles.getStoredIdToImp(),
101+
bidRequest.getImp(),
102+
failOnUnknown)))
93103
.recover(e -> Future.failedFuture(
94104
new InvalidRequestException("Error during processing profiles: " + e.getMessage())));
95105
}
@@ -161,6 +171,14 @@ private long timeoutMillis(BidRequest bidRequest) {
161171
return tmax != null && tmax > 0 ? tmax : defaultTimeoutMillis;
162172
}
163173

174+
private boolean isFailOnUnknown(Account account) {
175+
return Optional.ofNullable(account)
176+
.map(Account::getAuction)
177+
.map(AccountAuctionConfig::getProfiles)
178+
.map(AccountProfilesConfig::getFailOnUnknown)
179+
.orElse(failOnUnknown);
180+
}
181+
164182
private Future<StoredDataResult<Profile>> fetchProfiles(String accountId,
165183
AllProfilesIds allProfilesIds,
166184
long timeoutMillis) {
@@ -176,7 +194,8 @@ private Future<StoredDataResult<Profile>> fetchProfiles(String accountId,
176194

177195
private Future<StoredDataResult<Profile>> emitMetrics(String accountId,
178196
StoredDataResult<Profile> fetchResult,
179-
AuctionContext auctionContext) {
197+
AuctionContext auctionContext,
198+
boolean failOnUnknown) {
180199

181200
final List<String> errors = fetchResult.getErrors();
182201
if (!errors.isEmpty()) {
@@ -197,17 +216,19 @@ private Future<StoredDataResult<Profile>> emitMetrics(String accountId,
197216

198217
private BidRequest applyRequestProfiles(List<String> profilesIds,
199218
Map<String, Profile> idToRequestProfile,
200-
BidRequest bidRequest) {
219+
BidRequest bidRequest,
220+
boolean failOnUnknown) {
201221

202222
return !idToRequestProfile.isEmpty()
203-
? applyProfiles(profilesIds, idToRequestProfile, bidRequest, BidRequest.class)
223+
? applyProfiles(profilesIds, idToRequestProfile, bidRequest, BidRequest.class, failOnUnknown)
204224
: bidRequest;
205225
}
206226

207227
private <T> T applyProfiles(List<String> profilesIds,
208228
Map<String, Profile> idToProfile,
209229
T original,
210-
Class<T> tClass) {
230+
Class<T> tClass,
231+
boolean failOnUnknown) {
211232

212233
if (profilesIds.isEmpty()) {
213234
return original;
@@ -244,20 +265,30 @@ private ObjectNode mergeProfile(ObjectNode original, Profile profile) {
244265
}
245266

246267
private ObjectNode merge(JsonNode takePrecedence, JsonNode other) {
268+
if (!takePrecedence.isObject() || !other.isObject()) {
269+
throw new InvalidRequestException("One of the merge arguments is not an object.");
270+
}
271+
247272
return (ObjectNode) jsonMerger.merge(takePrecedence, other);
248273
}
249274

250275
private List<Imp> applyImpsProfiles(List<List<String>> profilesIds,
251276
Map<String, Profile> idToImpProfile,
252-
List<Imp> imps) {
277+
List<Imp> imps,
278+
boolean failOnUnknown) {
253279

254280
if (idToImpProfile.isEmpty()) {
255281
return imps;
256282
}
257283

258284
final List<Imp> updatedImps = new ArrayList<>(imps);
259285
for (int i = 0; i < profilesIds.size(); i++) {
260-
updatedImps.set(i, applyProfiles(profilesIds.get(i), idToImpProfile, imps.get(i), Imp.class));
286+
updatedImps.set(i, applyProfiles(
287+
profilesIds.get(i),
288+
idToImpProfile,
289+
imps.get(i),
290+
Imp.class,
291+
failOnUnknown));
261292
}
262293

263294
return Collections.unmodifiableList(updatedImps);

0 commit comments

Comments
 (0)