AdsOptimizationImpl implements AdsOptimization { @Override public AdsOptimization selectTopK(int k) { if (k <= 0) { throw new IllegalArgumentException("The parameter should be a positive integer."); } if (_candidateAds.size() == 0) { return this; } Collections.sort(_candidateAds, new Comparator<AdsStatsInfo>() { @Override public int compare(AdsStatsInfo ads1, AdsStatsInfo ads2) { float diff = ads2.getRankScore() - ads1.getRankScore(); return diff == 0 ? 0 : (diff > 0 ? 1 : -1); } }); if (_candidateAds.size() > k + 1) { _candidateAds.subList(k + 1, _candidateAds.size()).clear(); } 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 selectTopKShouldThrowIllegalArgumentException_For_KLessThanZero() { System.out.println("selectTopKShouldThrowIllegalArgumentException_For_KLessThanZero"); ads.selectTopK(-1); }
@Test(expected = IllegalArgumentException.class) public void selectTopKShouldThrowIllegalArgumentException_For_KEqualToZero() { System.out.println("selectTopKShouldThrowIllegalArgumentException_For_KEqualToZero"); ads.selectTopK(0); }
@Test @UseDataProvider("selectTopKData") public void selectTopKShouldReturnAValidOutput(int k, AdsOptimizationImpl expected) { System.out.println("selectTopKShouldReturnAValidOutput"); assertEquals(expected, ads.selectTopK(k)); }