Skip to content

Commit

Permalink
Tests: Fix for currency conversion (#2436)
Browse files Browse the repository at this point in the history
  • Loading branch information
marki1an authored Jul 13, 2023
1 parent 32256d6 commit 8721978
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import org.prebid.server.functional.model.config.AccountConfig
import org.prebid.server.functional.model.config.AccountPriceFloorsConfig
import org.prebid.server.functional.model.config.PriceFloorsFetch
import org.prebid.server.functional.model.db.Account
import org.prebid.server.functional.model.mock.services.currencyconversion.CurrencyConversionRatesResponse
import org.prebid.server.functional.model.pricefloors.Country
import org.prebid.server.functional.model.pricefloors.MediaType
import org.prebid.server.functional.model.pricefloors.Rule
Expand All @@ -19,16 +18,12 @@ import org.prebid.server.functional.model.request.auction.Prebid
import org.prebid.server.functional.model.request.auction.Video
import org.prebid.server.functional.model.response.currencyrates.CurrencyRatesResponse
import org.prebid.server.functional.service.PrebidServerService
import org.prebid.server.functional.testcontainers.scaffolding.CurrencyConversion
import org.prebid.server.functional.testcontainers.scaffolding.FloorsProvider
import org.prebid.server.functional.tests.BaseSpec
import org.prebid.server.functional.util.PBSUtils

import java.math.RoundingMode

import static org.prebid.server.functional.model.Currency.EUR
import static org.prebid.server.functional.model.Currency.GBP
import static org.prebid.server.functional.model.Currency.USD
import static org.prebid.server.functional.model.request.auction.DistributionChannel.SITE
import static org.prebid.server.functional.model.request.auction.FetchStatus.INPROGRESS
import static org.prebid.server.functional.testcontainers.Dependencies.getNetworkServiceContainer
Expand All @@ -37,33 +32,20 @@ abstract class PriceFloorsBaseSpec extends BaseSpec {

public static final BigDecimal FLOOR_MIN = 0.5
public static final BigDecimal FLOOR_MAX = 2
public static final Map<String, String> floorsConfig = ["price-floors.enabled" : "true",
"settings.default-account-config": encode(defaultAccountConfigSettings)]
public static final Map<String, String> FLOORS_CONFIG = ["price-floors.enabled" : "true",
"settings.default-account-config": encode(defaultAccountConfigSettings)]

protected static final String basicFetchUrl = networkServiceContainer.rootUri + FloorsProvider.FLOORS_ENDPOINT
protected static final FloorsProvider floorsProvider = new FloorsProvider(networkServiceContainer)
protected static final CurrencyConversion currencyConversion = new CurrencyConversion(networkServiceContainer)
protected static final int MAX_MODEL_WEIGHT = 100

private static final int DEFAULT_MODEL_WEIGHT = 1
private static final int CURRENCY_CONVERSION_PRECISION = 3
private static final int FLOOR_VALUE_PRECISION = 4
private static final Map<String, String> CURRENCY_CONVERTER_CONFIG = ["auction.ad-server-currency" : USD as String,
"currency-converter.external-rates.enabled" : "true",
"currency-converter.external-rates.url" : "$networkServiceContainer.rootUri/currency".toString(),
"currency-converter.external-rates.default-timeout-ms": "4000",
"currency-converter.external-rates.refresh-period-ms" : "900000"]

protected final PrebidServerService floorsPbsService = pbsServiceFactory.getService(floorsConfig + CURRENCY_CONVERTER_CONFIG)
protected final PrebidServerService floorsPbsService = pbsServiceFactory.getService(FLOORS_CONFIG)

def setupSpec() {
currencyConversion.setCurrencyConversionRatesResponse(CurrencyConversionRatesResponse.getDefaultCurrencyConversionRatesResponse().tap {
conversions = [(USD): [(EUR): 0.9124920156948626,
(GBP): 0.793776804452961],
(GBP): [(USD): 1.2597999770088517,
(EUR): 1.1495574203931487],
(EUR): [(USD): 1.3429368029739777]]
})
floorsProvider.setResponse()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package org.prebid.server.functional.tests.pricefloors

import org.prebid.server.functional.model.Currency
import org.prebid.server.functional.model.mock.services.currencyconversion.CurrencyConversionRatesResponse
import org.prebid.server.functional.model.pricefloors.PriceFloorData
import org.prebid.server.functional.model.request.auction.ImpExtPrebidFloors
import org.prebid.server.functional.model.response.auction.Bid
import org.prebid.server.functional.model.response.auction.BidResponse
import org.prebid.server.functional.model.response.auction.ErrorType
import org.prebid.server.functional.service.PrebidServerService
import org.prebid.server.functional.testcontainers.scaffolding.CurrencyConversion
import org.prebid.server.functional.util.PBSUtils

import static org.prebid.server.functional.model.Currency.BOGUS
Expand All @@ -16,9 +20,26 @@ import static org.prebid.server.functional.model.request.auction.FetchStatus.NON
import static org.prebid.server.functional.model.request.auction.FetchStatus.SUCCESS
import static org.prebid.server.functional.model.request.auction.Location.FETCH
import static org.prebid.server.functional.model.response.auction.ErrorType.PREBID
import static org.prebid.server.functional.testcontainers.Dependencies.getNetworkServiceContainer

class PriceFloorsCurrencySpec extends PriceFloorsBaseSpec {

private static final Map<Currency, Map<Currency, BigDecimal>> DEFAULT_CURRENCY_RATES = [(USD): [(EUR): 0.9124920156948626,
(GBP): 0.793776804452961],
(GBP): [(USD): 1.2597999770088517,
(EUR): 1.1495574203931487],
(EUR): [(USD): 1.3429368029739777]]
private static final CurrencyConversion currencyConversion = new CurrencyConversion(networkServiceContainer).tap {
setCurrencyConversionRatesResponse(CurrencyConversionRatesResponse.getDefaultCurrencyConversionRatesResponse(DEFAULT_CURRENCY_RATES))
}
private static final Map<String, String> CURRENCY_CONVERTER_CONFIG = ["auction.ad-server-currency" : "USD",
"currency-converter.external-rates.enabled" : "true",
"currency-converter.external-rates.url" : "$networkServiceContainer.rootUri/currency".toString(),
"currency-converter.external-rates.default-timeout-ms": "4000",
"currency-converter.external-rates.refresh-period-ms" : "900000"]
private final PrebidServerService currencyFloorsPbsService = pbsServiceFactory.getService(FLOORS_CONFIG +
CURRENCY_CONVERTER_CONFIG)

private static final String GENERAL_ERROR_METRIC = "price-floors.general.err"

def "PBS should update bidFloor, bidFloorCur for signalling when request.cur is specified"() {
Expand All @@ -40,10 +61,10 @@ class PriceFloorsCurrencySpec extends PriceFloorsBaseSpec {
floorsProvider.setResponse(bidRequest.site.publisher.id, floorsResponse)

and: "PBS fetch rules from floors provider"
cacheFloorsProviderRules(bidRequest)
cacheFloorsProviderRules(currencyFloorsPbsService, bidRequest)

when: "PBS processes auction request"
floorsPbsService.sendAuctionRequest(bidRequest)
currencyFloorsPbsService.sendAuctionRequest(bidRequest)

then: "Bidder request should contain bidFloor, bidFloorCur from floors provider"
def bidderRequest = bidder.getBidderRequests(bidRequest.id).last()
Expand Down Expand Up @@ -72,10 +93,10 @@ class PriceFloorsCurrencySpec extends PriceFloorsBaseSpec {
floorsProvider.setResponse(bidRequest.site.publisher.id, floorsResponse)

and: "PBS fetch rules from floors provider"
cacheFloorsProviderRules(bidRequest)
cacheFloorsProviderRules(currencyFloorsPbsService, bidRequest)

and: "Get currency rates"
def currencyRatesResponse = floorsPbsService.sendCurrencyRatesRequest()
def currencyRatesResponse = currencyFloorsPbsService.sendCurrencyRatesRequest()

and: "Bid response with 2 bids: price < floorMin, price = floorMin"
def convertedMinFloorValue = getPriceAfterCurrencyConversion(floorValue,
Expand All @@ -89,7 +110,7 @@ class PriceFloorsCurrencySpec extends PriceFloorsBaseSpec {
bidder.setResponse(bidRequest.id, bidResponse)

when: "PBS processes auction request"
def response = floorsPbsService.sendAuctionRequest(bidRequest)
def response = currencyFloorsPbsService.sendAuctionRequest(bidRequest)

then: "PBS should suppress bids lower than floorRuleValue"
assert response.seatbid?.first()?.bid?.collect { it.price } == [convertedMinFloorValue]
Expand All @@ -113,7 +134,7 @@ class PriceFloorsCurrencySpec extends PriceFloorsBaseSpec {
accountDao.save(account)

and: "Get currency rates"
def currencyRatesResponse = floorsPbsService.sendCurrencyRatesRequest()
def currencyRatesResponse = currencyFloorsPbsService.sendCurrencyRatesRequest()

and: "Set Floors Provider response with a currency different from the floorMinCur, floorValur lower then floorMin"
def floorProviderCur = EUR
Expand All @@ -127,10 +148,10 @@ class PriceFloorsCurrencySpec extends PriceFloorsBaseSpec {
floorsProvider.setResponse(bidRequest.site.publisher.id, floorsResponse)

and: "PBS fetch rules from floors provider"
cacheFloorsProviderRules(bidRequest)
cacheFloorsProviderRules(currencyFloorsPbsService, bidRequest)

when: "PBS processes auction request"
floorsPbsService.sendAuctionRequest(bidRequest)
currencyFloorsPbsService.sendAuctionRequest(bidRequest)

then: "Bidder request bidFloor should correspond floorMin"
def bidderRequest = bidder.getBidderRequests(bidRequest.id).last()
Expand All @@ -144,7 +165,7 @@ class PriceFloorsCurrencySpec extends PriceFloorsBaseSpec {

def "PBS should not update bidFloor, bidFloorCur for signalling when currency conversion is not available"() {
given: "Pbs config with disabled conversion"
def pbsService = pbsServiceFactory.getService(floorsConfig +
def pbsService = pbsServiceFactory.getService(FLOORS_CONFIG +
["currency-converter.external-rates.enabled": "false"])

and: "BidRequest with floorMinCur"
Expand Down Expand Up @@ -224,7 +245,7 @@ class PriceFloorsCurrencySpec extends PriceFloorsBaseSpec {
accountDao.save(account)

when: "PBS processes auction request"
floorsPbsService.sendAuctionRequest(bidRequest)
currencyFloorsPbsService.sendAuctionRequest(bidRequest)

then: "Bidder request should contain bidFloor, bidFloorCur from request"
def bidderRequest = bidder.getBidderRequests(bidRequest.id).last()
Expand Down Expand Up @@ -255,7 +276,7 @@ class PriceFloorsCurrencySpec extends PriceFloorsBaseSpec {
accountDao.save(account)

when: "PBS processes auction request"
floorsPbsService.sendAuctionRequest(bidRequest)
currencyFloorsPbsService.sendAuctionRequest(bidRequest)

then: "Bidder request should contain bidFloor, bidFloorCur from request"
def bidderRequest = bidder.getBidderRequests(bidRequest.id).last()
Expand Down Expand Up @@ -287,10 +308,10 @@ class PriceFloorsCurrencySpec extends PriceFloorsBaseSpec {
floorsProvider.setResponse(bidRequest.site.publisher.id, floorsResponse)

and: "PBS fetch rules from floors provider"
cacheFloorsProviderRules(bidRequest)
cacheFloorsProviderRules(currencyFloorsPbsService, bidRequest)

and: "Get currency rates"
def currencyRatesResponse = floorsPbsService.sendCurrencyRatesRequest()
def currencyRatesResponse = currencyFloorsPbsService.sendCurrencyRatesRequest()

and: "Bid response with 2 bids: price < floorMin, price = floorMin"
def bidResponseCur = GBP
Expand All @@ -306,7 +327,7 @@ class PriceFloorsCurrencySpec extends PriceFloorsBaseSpec {
bidder.setResponse(bidRequest.id, bidResponse)

when: "PBS processes auction request"
def response = floorsPbsService.sendAuctionRequest(bidRequest)
def response = currencyFloorsPbsService.sendAuctionRequest(bidRequest)

then: "Bidder request should contain bidFloor, bidFloorCur from floors provider"
def bidderRequest = bidder.getBidderRequests(bidRequest.id).last()
Expand Down Expand Up @@ -347,13 +368,13 @@ class PriceFloorsCurrencySpec extends PriceFloorsBaseSpec {
floorsProvider.setResponse(bidRequest.site.publisher.id, floorsResponse)

and: "PBS fetch rules from floors provider"
cacheFloorsProviderRules(bidRequest)
cacheFloorsProviderRules(currencyFloorsPbsService, bidRequest)

and: "Flush metrics"
flushMetrics(floorsPbsService)
flushMetrics(currencyFloorsPbsService)

when: "PBS processes auction request"
def response = floorsPbsService.sendAuctionRequest(bidRequest)
def response = currencyFloorsPbsService.sendAuctionRequest(bidRequest)

then: "PBS should log a warning"
assert response.ext?.warnings[PREBID]*.code == [999]
Expand All @@ -362,7 +383,7 @@ class PriceFloorsCurrencySpec extends PriceFloorsBaseSpec {
"to convert from currency $requestFloorCur to desired ad server currency $floorsProviderCur"]

and: "Metric #GENERAL_ERROR_METRIC should be update"
assert getCurrentMetricValue(floorsPbsService, GENERAL_ERROR_METRIC) == 1
assert getCurrentMetricValue(currencyFloorsPbsService, GENERAL_ERROR_METRIC) == 1

and: "Bidder request should contain bidFloor, bidFloorCur from request"
def bidderRequest = bidder.getBidderRequests(bidRequest.id).last()
Expand All @@ -385,7 +406,7 @@ class PriceFloorsCurrencySpec extends PriceFloorsBaseSpec {
accountDao.save(account)

when: "PBS processes auction request"
floorsPbsService.sendAuctionRequest(bidRequest)
currencyFloorsPbsService.sendAuctionRequest(bidRequest)

then: "Bidder request should contain floorMin, floorMinCur, currency from request"
verifyAll(bidder.getBidderRequest(bidRequest.id)) {
Expand All @@ -408,7 +429,7 @@ class PriceFloorsCurrencySpec extends PriceFloorsBaseSpec {
accountDao.save(account)

when: "PBS processes auction request"
def response = floorsPbsService.sendAuctionRequest(bidRequest)
def response = currencyFloorsPbsService.sendAuctionRequest(bidRequest)

then: "PBS should log a warning"
assert response.ext?.warnings[PREBID]*.code == [999]
Expand Down Expand Up @@ -438,7 +459,7 @@ class PriceFloorsCurrencySpec extends PriceFloorsBaseSpec {
accountDao.save(account)

when: "PBS processes auction request"
floorsPbsService.sendAuctionRequest(bidRequest)
currencyFloorsPbsService.sendAuctionRequest(bidRequest)

then: "Bidder request should contain floorMin, floorValue, bidFloor, bidFloorCur"
verifyAll(bidder.getBidderRequest(bidRequest.id)) {
Expand All @@ -463,7 +484,7 @@ class PriceFloorsCurrencySpec extends PriceFloorsBaseSpec {
accountDao.save(account)

when: "PBS processes auction request"
floorsPbsService.sendAuctionRequest(bidRequest)
currencyFloorsPbsService.sendAuctionRequest(bidRequest)

then: "Bidder request should contain bidFloorCur, bidFloor, floorValue"
verifyAll(bidder.getBidderRequest(bidRequest.id)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ class PriceFloorsEnforcementSpec extends PriceFloorsBaseSpec {
def defaultAccountConfigSettings = defaultAccountConfigSettings.tap {
auction.priceFloors.enforceDealFloors = false
}
def pbsService = pbsServiceFactory.getService(floorsConfig +
def pbsService = pbsServiceFactory.getService(FLOORS_CONFIG +
["settings.default-account-config": encode(defaultAccountConfigSettings)])

and: "Default basic BidRequest with generic bidder with preferdeals = true"
Expand Down Expand Up @@ -303,7 +303,7 @@ class PriceFloorsEnforcementSpec extends PriceFloorsBaseSpec {
def defaultAccountConfigSettings = defaultAccountConfigSettings.tap {
auction.priceFloors.enforceDealFloors = pbsConfigEnforceDealFloors
}
def pbsService = pbsServiceFactory.getService(floorsConfig +
def pbsService = pbsServiceFactory.getService(FLOORS_CONFIG +
["settings.default-account-config": encode(defaultAccountConfigSettings)])

and: "Default basic BidRequest with generic bidder with preferdeals = true"
Expand Down Expand Up @@ -359,7 +359,7 @@ class PriceFloorsEnforcementSpec extends PriceFloorsBaseSpec {
def defaultAccountConfigSettings = defaultAccountConfigSettings.tap {
auction.priceFloors.enforceFloorsRate = pbsConfigEnforceRate
}
def pbsService = pbsServiceFactory.getService(floorsConfig +
def pbsService = pbsServiceFactory.getService(FLOORS_CONFIG +
["settings.default-account-config": encode(defaultAccountConfigSettings)])

and: "Default BidRequest"
Expand Down Expand Up @@ -414,7 +414,7 @@ class PriceFloorsEnforcementSpec extends PriceFloorsBaseSpec {
def defaultAccountConfigSettings = defaultAccountConfigSettings.tap {
auction.priceFloors.enforceFloorsRate = pbsConfigEnforceFloorsRate
}
def pbsService = pbsServiceFactory.getService(floorsConfig +
def pbsService = pbsServiceFactory.getService(FLOORS_CONFIG +
["settings.default-account-config": encode(defaultAccountConfigSettings)])

and: "Default BidRequest"
Expand Down
Loading

0 comments on commit 8721978

Please sign in to comment.