TopTenController extends GenericOCDSController { @ApiOperation(value = "This endpoint should return the following data for the Top 10 suppliers (by award value)." + "Returns supplier id, total awarded amount of all awarded contracts, count of awarded contracts," + "Ids of the procuring entities from which they have received an award, and their count. " + "All filters ally here, the year filter uses the awards.date field.") @RequestMapping(value = "/api/topTenSuppliers", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json") public List<DBObject> topTenLargestSuppliers(@ModelAttribute @Valid final YearFilterPagingRequest filter) { BasicDBObject project = new BasicDBObject(); project.put(Fields.UNDERSCORE_ID, 0); project.put("awards.suppliers._id", 1); project.put("awards.value.amount", 1); project.put("tender.procuringEntity._id", 1); BasicDBObject group = new BasicDBObject(); group.put(Fields.UNDERSCORE_ID, "$awards.suppliers._id"); group.put(Keys.TOTAL_AWARD_AMOUNT, new BasicDBObject("$sum", "$awards.value.amount")); group.put(Keys.TOTAL_CONTRACTS, new BasicDBObject("$sum", 1)); group.put(Keys.PROCURING_ENTITY_IDS, new BasicDBObject("$addToSet", "$tender.procuringEntity._id")); Aggregation agg = newAggregation( match(where("awards.value.amount").exists(true).and("awards.status").is("active") .andOperator(getDefaultFilterCriteria(filter))), unwind("$awards"), match(where("awards.status").is("active")), unwind("$awards.suppliers"), match(getYearFilterCriteria(filter, "awards.date")), new CustomProjectionOperation(project), new CustomGroupingOperation(group), sort(Direction.DESC, Keys.TOTAL_AWARD_AMOUNT), limit(10), project().and(Fields.UNDERSCORE_ID). as(Keys.SUPPLIER_ID). andInclude(Keys.TOTAL_AWARD_AMOUNT, Keys.TOTAL_CONTRACTS, Keys.PROCURING_ENTITY_IDS) .andExclude(Fields.UNDERSCORE_ID) .and(Keys.PROCURING_ENTITY_IDS).size().as(Keys.PROCURING_ENTITY_IDS_COUNT) ); AggregationResults<DBObject> results = mongoTemplate.aggregate(agg, "release", DBObject.class); List<DBObject> tagCount = results.getMappedResults(); return tagCount; }  @ApiOperation(value = "Returns the top ten largest active awards." + " The amount is taken from the award.value field. The returned data will contain" + "the following fields: " + "planning.bidNo, awards.date, awards.suppliers.name, " + "awards.value.amount, awards.suppliers.name, planning.budget (if any)") @RequestMapping(value = "/api/topTenLargestAwards", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json") List<DBObject> topTenLargestAwards(@ModelAttribute @Valid final YearFilterPagingRequest filter); @ApiOperation(value = "Returns the top ten largest active tenders." + " The amount is taken from the tender.value.amount field." + " The returned data will contain" + "the following fields: " + "planning.bidNo, tender.date, tender.value.amount, tender.tenderPeriod, " + "tender.procuringEntity.name") @RequestMapping(value = "/api/topTenLargestTenders", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json") List<DBObject> topTenLargestTenders(@ModelAttribute @Valid final YearFilterPagingRequest filter); @ApiOperation(value = "This endpoint should return the following data for the Top 10 suppliers (by award value)." + "Returns supplier id, total awarded amount of all awarded contracts, count of awarded contracts," + "Ids of the procuring entities from which they have received an award, and their count. " + "All filters ally here, the year filter uses the awards.date field.") @RequestMapping(value = "/api/topTenSuppliers", method = {RequestMethod.POST, RequestMethod.GET}, produces = "application/json") List<DBObject> topTenLargestSuppliers(@ModelAttribute @Valid final YearFilterPagingRequest filter);  }
@Test public void topTenLargestSuppliers() throws Exception { final List<DBObject> topTenLargestSuppliers = topTenController .topTenLargestSuppliers(new YearFilterPagingRequest()); final DBObject first = topTenLargestSuppliers.get(0); Assert.assertEquals(6000000d, first.get(TopTenController.Keys.TOTAL_AWARD_AMOUNT)); Assert.assertEquals(1, first.get(TopTenController.Keys.TOTAL_CONTRACTS)); Assert.assertEquals("E09000005", ((Collection) first.get(TopTenController.Keys.PROCURING_ENTITY_IDS)).iterator().next()); Assert.assertEquals("GB-COH-1234567845", first.get(TopTenController.Keys.SUPPLIER_ID)); Assert.assertEquals(1, first.get(TopTenController.Keys.PROCURING_ENTITY_IDS_COUNT)); Assert.assertEquals(2, topTenLargestSuppliers.size()); }