From 04836ddf08d45774c47a61e9d13e9289eb91802e Mon Sep 17 00:00:00 2001 From: Denis Logachev Date: Wed, 14 Dec 2016 02:48:00 +0200 Subject: [PATCH] Adkernel adapter aliasing (#857) * initial adkernel adapter commit * minor refactoring * minor changes * adkernel adapter aliasing --- src/adapters/adkernel.js | 35 ++++-- test/spec/adapters/adkernel_spec.js | 164 +++++++++++++++------------- 2 files changed, 109 insertions(+), 90 deletions(-) diff --git a/src/adapters/adkernel.js b/src/adapters/adkernel.js index 48d73326d35..a78416999c2 100644 --- a/src/adapters/adkernel.js +++ b/src/adapters/adkernel.js @@ -2,13 +2,13 @@ import bidmanager from 'src/bidmanager'; import bidfactory from 'src/bidfactory'; import * as utils from 'src/utils'; import {ajax} from 'src/ajax'; +import Adapter from 'src/adapters/adapter'; /** * Adapter for requesting bids from AdKernel white-label platform * @class */ -const AdkernelAdapter = function AdkernelAdapter() { - const ADKERNEL = 'adkernel'; +const AdKernelAdapter = function AdKernelAdapter() { const AJAX_REQ_PARAMS = { contentType: 'text/plain', withCredentials: true, @@ -16,6 +16,8 @@ const AdkernelAdapter = function AdkernelAdapter() { }; const EMPTY_BID_RESPONSE = {'seatbid': [{'bid': []}]}; + let baseAdapter = Adapter.createNew('adkernel'); + /** * Helper object to build multiple bid requests in case of multiple zones/ad-networks * @constructor @@ -119,10 +121,10 @@ const AdkernelAdapter = function AdkernelAdapter() { /** * Main module export function implementation */ - function _callBids(params) { + baseAdapter.callBids = function (params) { var bids = params.bids || []; processBids(bids); - } + }; /** * Process all bids grouped by network/zone @@ -156,8 +158,8 @@ const AdkernelAdapter = function AdkernelAdapter() { */ function createBidObject(resp, bid, width, height) { return utils.extend(bidfactory.createBid(1, bid), { - bidderCode: ADKERNEL, - ad: formatAdmarkup(resp), + bidderCode: bid.bidder, + ad: formatAdMarkup(resp), width: width, height: height, cpm: parseFloat(resp.price) @@ -169,14 +171,14 @@ const AdkernelAdapter = function AdkernelAdapter() { */ function createEmptyBidObject(bid) { return utils.extend(bidfactory.createBid(2, bid), { - bidderCode: ADKERNEL + bidderCode: bid.bidder }); } /** * Format creative with optional nurl call */ - function formatAdmarkup(bid) { + function formatAdMarkup(bid) { var adm = bid.adm; if ('nurl' in bid) { adm += utils.createTrackPixelHtml(bid.nurl); @@ -194,14 +196,23 @@ const AdkernelAdapter = function AdkernelAdapter() { function createSite() { var location = utils.getTopWindowLocation(); return { - 'domain': location.hostname, - 'page': location.pathname + 'domain': location.hostname }; } return { - callBids: _callBids + callBids: baseAdapter.callBids, + setBidderCode: baseAdapter.setBidderCode, + getBidderCode : baseAdapter.getBidderCode, + createNew : AdKernelAdapter.createNew }; }; -module.exports = AdkernelAdapter; \ No newline at end of file +/** + * Creates new instance of AdKernel bidder adapter + */ +AdKernelAdapter.createNew = function() { + return new AdKernelAdapter(); +}; + +module.exports = AdKernelAdapter; diff --git a/test/spec/adapters/adkernel_spec.js b/test/spec/adapters/adkernel_spec.js index 1887662ba19..149f9c7a44d 100644 --- a/test/spec/adapters/adkernel_spec.js +++ b/test/spec/adapters/adkernel_spec.js @@ -1,67 +1,67 @@ -import {expect} from "chai"; -import Adapter from "src/adapters/adkernel"; -import * as ajax from "src/ajax"; -import * as utils from "src/utils"; -import bidmanager from "src/bidmanager"; -import CONSTANTS from "src/constants.json"; +import {expect} from 'chai'; +import Adapter from 'src/adapters/adkernel'; +import * as ajax from 'src/ajax'; +import * as utils from 'src/utils'; +import bidmanager from 'src/bidmanager'; +import CONSTANTS from 'src/constants.json'; -describe("Adkernel adapter", () => { +describe('Adkernel adapter', () => { const bid1_zone1 = { - bidder: "adkernel", - bidId: "Bid_01", - params: {zoneId: 1, host: "rtb.adkernel.com"}, - placementCode: "ad-unit-1", + bidder: 'adkernel', + bidId: 'Bid_01', + params: {zoneId: 1, host: 'rtb.adkernel.com'}, + placementCode: 'ad-unit-1', sizes: [[300, 250]] }, bid2_zone2 = { - bidder: "adkernel", - bidId: "Bid_02", - params: {zoneId: 2, host: "rtb.adkernel.com"}, - placementCode: "ad-unit-2", + bidder: 'adkernel', + bidId: 'Bid_02', + params: {zoneId: 2, host: 'rtb.adkernel.com'}, + placementCode: 'ad-unit-2', sizes: [[728, 90]] }, bid3_host2 = { - bidder: "adkernel", - bidId: "Bid_02", - params: {zoneId: 1, host: "rtb-private.adkernel.com"}, - placementCode: "ad-unit-2", + bidder: 'adkernel', + bidId: 'Bid_02', + params: {zoneId: 1, host: 'rtb-private.adkernel.com'}, + placementCode: 'ad-unit-2', sizes: [[728, 90]] }, bid_without_zone = { - bidder: "adkernel", - bidId: "Bid_W", - params: {host: "rtb-private.adkernel.com"}, - placementCode: "ad-unit-1", + bidder: 'adkernel', + bidId: 'Bid_W', + params: {host: 'rtb-private.adkernel.com'}, + placementCode: 'ad-unit-1', sizes: [[728, 90]] }, bid_without_host = { - bidder: "adkernel", - bidId: "Bid_W", + bidder: 'adkernel', + bidId: 'Bid_W', params: {zoneId: 1}, - placementCode: "ad-unit-1", + placementCode: 'ad-unit-1', sizes: [[728, 90]] }; const bidResponse1 = { - "id": "bid1", - "seatbid": [{ - "bid": [{ - "id": "1", - "impid": "Bid_01", - "price": 3.01, - "nurl": "https://rtb.com/win?i=ZjKoPYSFI3Y_0", - "adm": "" + 'id': 'bid1', + 'seatbid': [{ + 'bid': [{ + 'id': '1', + 'impid': 'Bid_01', + 'price': 3.01, + 'nurl': 'https://rtb.com/win?i=ZjKoPYSFI3Y_0', + 'adm': '' }] }], - "cur": "USD" + 'cur': 'USD' }, bidResponse2 = { - "id": "bid2", - "seatbid": [{ - "bid": [{ - "id": "2", - "impid": "Bid_02", - "price": 1.31, - "adm": "" + 'id': 'bid2', + 'seatbid': [{ + 'bid': [{ + 'id': '2', + 'impid': 'Bid_02', + 'price': 1.31, + 'adm': '' }] }], - "cur": "USD" + 'cur': 'USD' }; let adapter, @@ -70,8 +70,8 @@ describe("Adkernel adapter", () => { beforeEach(() => { sandbox = sinon.sandbox.create(); - adapter = new Adapter(); - ajaxStub = sandbox.stub(ajax, "ajax"); + adapter = Adapter.createNew(); + ajaxStub = sandbox.stub(ajax, 'ajax'); }); afterEach(() => { @@ -80,12 +80,12 @@ describe("Adkernel adapter", () => { function doRequest(bids) { adapter.callBids({ - bidderCode: "adkernel", + bidderCode: 'adkernel', bids: bids }); } - describe("input parameters validation", ()=> { + describe('input parameters validation', ()=> { let spy; beforeEach(() => { @@ -93,28 +93,28 @@ describe("Adkernel adapter", () => { sandbox.stub(bidmanager, 'addBidResponse'); }); - it("empty request shouldn't generate exception", () => { + it('empty request shouldn\'t generate exception', () => { expect(adapter.callBids({ - bidderCode: "adkernel" + bidderCode: 'adkernel' })).to.be.an('undefined'); }); - it("request without zone shouldn't issue a request", () => { + it('request without zone shouldn\'t issue a request', () => { doRequest([bid_without_zone]); sinon.assert.notCalled(ajaxStub); expect(bidmanager.addBidResponse.firstCall.args[1].getStatusCode()).to.equal(CONSTANTS.STATUS.NO_BID); - expect(bidmanager.addBidResponse.firstCall.args[1].bidderCode).to.equal("adkernel"); + expect(bidmanager.addBidResponse.firstCall.args[1].bidderCode).to.equal('adkernel'); }); - it("request without host shouldn't issue a request", () => { + it('request without host shouldn\'t issue a request', () => { doRequest([bid_without_host]); sinon.assert.notCalled(ajaxStub); expect(bidmanager.addBidResponse.firstCall.args[1].getStatusCode()).to.equal(CONSTANTS.STATUS.NO_BID); - expect(bidmanager.addBidResponse.firstCall.args[1].bidderCode).to.equal("adkernel"); + expect(bidmanager.addBidResponse.firstCall.args[1].bidderCode).to.equal('adkernel'); }); }); - describe("request building", () => { + describe('request building', () => { let bidRequest; beforeEach(() => { @@ -132,29 +132,28 @@ describe("Adkernel adapter", () => { bidRequest = JSON.parse(decodeURIComponent(ajaxStub.getCall(0).args[2].r)); }); - it("should be a first-price auction", () => { + it('should be a first-price auction', () => { expect(bidRequest).to.have.property('at', 1); }); - it("should have banner object", () => { + it('should have banner object', () => { expect(bidRequest.imp[0]).to.have.property('banner'); }); - it("should have h/w", () => { + it('should have h/w', () => { expect(bidRequest.imp[0].banner).to.have.property('w', 300); expect(bidRequest.imp[0].banner).to.have.property('h', 250); }); - it("should respect secure connection", () => { + it('should respect secure connection', () => { expect(bidRequest.imp[0]).to.have.property('secure', 1); }); - it("should create proper site block", () => { + it('should create proper site block', () => { expect(bidRequest.site).to.have.property('domain', 'example.com'); - expect(bidRequest.site).to.have.property('page', '/index.html'); }); - it("should fill device with caller macro", ()=> { + it('should fill device with caller macro', ()=> { expect(bidRequest).to.have.property('device'); expect(bidRequest.device).to.have.property('ip', 'caller'); expect(bidRequest.device).to.have.property('ua', 'caller'); @@ -162,25 +161,25 @@ describe("Adkernel adapter", () => { }); - describe("requests routing", () => { + describe('requests routing', () => { - it("should issue a request for each network", () => { - ajaxStub.onFirstCall().callsArgWith(1, "") - .onSecondCall().callsArgWith(1, ""); + it('should issue a request for each network', () => { + ajaxStub.onFirstCall().callsArgWith(1, '') + .onSecondCall().callsArgWith(1, ''); doRequest([bid1_zone1, bid3_host2]); expect(ajaxStub.calledTwice); expect(ajaxStub.firstCall.args[0]).to.include(bid1_zone1.params.host); expect(ajaxStub.secondCall.args[0]).to.include(bid3_host2.params.host); }); - it("should issue a request for each zone", () => { + it('should issue a request for each zone', () => { ajaxStub.onCall(0).callsArgWith(1, JSON.stringify(bidResponse1)); ajaxStub.onCall(1).callsArgWith(1, JSON.stringify(bidResponse2)); doRequest([bid1_zone1, bid2_zone2]); expect(ajaxStub.calledTwice); }); - it("should route calls to proper zones", () => { + it('should route calls to proper zones', () => { ajaxStub.onCall(0).callsArgWith(1, JSON.stringify(bidResponse1)); ajaxStub.onCall(1).callsArgWith(1, JSON.stringify(bidResponse2)); doRequest([bid1_zone1, bid2_zone2]); @@ -189,50 +188,50 @@ describe("Adkernel adapter", () => { }); }); - describe("responses processing", () => { + describe('responses processing', () => { beforeEach(() => { sandbox.stub(bidmanager, 'addBidResponse'); }); - it("should return fully-initialized bid-response", () => { + it('should return fully-initialized bid-response', () => { ajaxStub.onCall(0).callsArgWith(1, JSON.stringify(bidResponse1)); doRequest([bid1_zone1]); let bidResponse = bidmanager.addBidResponse.firstCall.args[1]; expect(bidmanager.addBidResponse.firstCall.args[0]).to.equal('ad-unit-1'); expect(bidResponse.getStatusCode()).to.equal(CONSTANTS.STATUS.GOOD); - expect(bidResponse.bidderCode).to.equal("adkernel"); + expect(bidResponse.bidderCode).to.equal('adkernel'); expect(bidResponse.cpm).to.equal(3.01); expect(bidResponse.ad).to.include(''); expect(bidResponse.width).to.equal(300); expect(bidResponse.height).to.equal(250); }); - it("should map responses to proper ad units", () => { + it('should map responses to proper ad units', () => { ajaxStub.onCall(0).callsArgWith(1, JSON.stringify(bidResponse1)); ajaxStub.onCall(1).callsArgWith(1, JSON.stringify(bidResponse2)); doRequest([bid1_zone1, bid2_zone2]); expect(bidmanager.addBidResponse.firstCall.args[1].getStatusCode()).to.equal(CONSTANTS.STATUS.GOOD); - expect(bidmanager.addBidResponse.firstCall.args[1].bidderCode).to.equal("adkernel"); + expect(bidmanager.addBidResponse.firstCall.args[1].bidderCode).to.equal('adkernel'); expect(bidmanager.addBidResponse.firstCall.args[0]).to.equal('ad-unit-1'); expect(bidmanager.addBidResponse.secondCall.args[1].getStatusCode()).to.equal(CONSTANTS.STATUS.GOOD); - expect(bidmanager.addBidResponse.secondCall.args[1].bidderCode).to.equal("adkernel"); + expect(bidmanager.addBidResponse.secondCall.args[1].bidderCode).to.equal('adkernel'); expect(bidmanager.addBidResponse.secondCall.args[0]).to.equal('ad-unit-2'); }); - it("should process empty responses", () => { + it('should process empty responses', () => { ajaxStub.onCall(0).callsArgWith(1, JSON.stringify(bidResponse1)); - ajaxStub.onCall(1).callsArgWith(1, ""); + ajaxStub.onCall(1).callsArgWith(1, ''); doRequest([bid1_zone1, bid2_zone2]); expect(bidmanager.addBidResponse.firstCall.args[1].getStatusCode()).to.equal(CONSTANTS.STATUS.GOOD); - expect(bidmanager.addBidResponse.firstCall.args[1].bidderCode).to.equal("adkernel"); + expect(bidmanager.addBidResponse.firstCall.args[1].bidderCode).to.equal('adkernel'); expect(bidmanager.addBidResponse.firstCall.args[0]).to.equal('ad-unit-1'); expect(bidmanager.addBidResponse.secondCall.args[1].getStatusCode()).to.equal(CONSTANTS.STATUS.NO_BID); - expect(bidmanager.addBidResponse.secondCall.args[1].bidderCode).to.equal("adkernel"); + expect(bidmanager.addBidResponse.secondCall.args[1].bidderCode).to.equal('adkernel'); expect(bidmanager.addBidResponse.secondCall.args[0]).to.equal('ad-unit-2'); }); - it("should add nurl as pixel", () => { + it('should add nurl as pixel', () => { sandbox.spy(utils, 'createTrackPixelHtml'); ajaxStub.onCall(0).callsArgWith(1, JSON.stringify(bidResponse1)); doRequest([bid1_zone1]); @@ -244,4 +243,13 @@ describe("Adkernel adapter", () => { }); + describe('adapter aliasing', () => { + const ALIAS_NAME = 'adkernelAlias'; + + it('should allow bidder code changing', () => { + expect(adapter.getBidderCode()).to.equal('adkernel'); + adapter.setBidderCode(ALIAS_NAME); + expect(adapter.getBidderCode()).to.equal(ALIAS_NAME); + }); + }); });