AdsOptimizationImpl implements AdsOptimization { @Override public AdsOptimization adsPricingAndAllocation(Inventory inventory, float mainlineReservePrice) { if (mainlineReservePrice < 0) { throw new IllegalArgumentException("The parameter should be a non-negtive integer."); } if (_candidateAds.size() == 0) { return this; } if (_candidateAds.size() == 1) { AdsStatsInfo ads = _candidateAds.get(0); ads.setCpc(inventory.findAds(ads.getAdsId()).getBid()); inventory.findCampaign(ads.getCampaignId()).deductBudget(ads.getCpc()); CampaignDataOperation.deductCampaignBudget(ads.getCampaignId(), ads.getCpc()); ads.setIsMainline(ads.getCpc() >= mainlineReservePrice); return this; } for (int i = 0; i < _candidateAds.size() - 1; i++) { AdsStatsInfo currentAds = _candidateAds.get(i); AdsStatsInfo nextAds = _candidateAds.get(i + 1); float bid = inventory.findAds(nextAds.getAdsId()).getBid(); float price = nextAds.getQualityScore() / currentAds.getQualityScore() * bid + 0.01f; currentAds.setCpc(price); BudgetLock.acquireWriteLock(); try { inventory.findCampaign(currentAds.getCampaignId()) .deductBudget(currentAds.getCpc()); CampaignDataOperation.deductCampaignBudget(currentAds.getCampaignId(), currentAds.getCpc()); } finally { BudgetLock.releaseWriteLock(); } currentAds.setIsMainline(currentAds.getCpc() >= mainlineReservePrice); } _candidateAds.remove(_candidateAds.size() - 1); return this; } AdsOptimizationImpl(List<AdsStatsInfo> candidateAds); @Override List<AdsStatsInfo> filterCandidateAds(Inventory inventory, float minRelevanceScore, float minReservePrice); @Override AdsOptimization filterAds(Inventory inventory, float minRelevanceScore, float minReservePrice); @Override AdsOptimization selectTopK(int k); @Override AdsOptimization adsPricingAndAllocation(Inventory inventory, float mainlineReservePrice); @Override String toString(); @Override AdsOptimization deDup(); @Override boolean equals(Object o); @Override int hashCode(); @Override List<AdsStatsInfo> showResult();  }
@Test(expected = IllegalArgumentException.class) public void adsPricingAndAllocationShouldThrowIllegalArgumentException_For_MainlineReservePriceLessThanZero() { System.out .println("adsPricingAndAllocationShouldThrowIllegalArgumentException_For_MainlineReservePriceLessThanZero"); ads.adsPricingAndAllocation(inventory, -1); }
@Test @UseDataProvider("adsPricingAndAllocationData") public void adsPricingAndAllocationShouldReturnAValidOutput(float mainlineReservePrice, AdsOptimizationImpl expected) { System.out.println("adsPricingAndAllocationShouldReturnAValidOutput"); AdsOptimization result = ads.adsPricingAndAllocation(inventory, mainlineReservePrice); assertEquals(expected, result); }