AverageNumberOfTenderersController extends GenericOCDSController { @ApiOperation(value = "Calculate average number of tenderers. The endpoint can be filtered" + "by year read from tender.tenderPeriod.startDate. " + "The number of tenderers are read from tender.numberOfTenderers") @RequestMapping(value = "/api/averageNumberOfTenderers", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json") public List<Document> averageNumberOfTenderers(@ModelAttribute @Valid final YearFilterPagingRequest filter) { DBObject project = new BasicDBObject(); addYearlyMonthlyProjection(filter, project, ref(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE)); project.put(MongoConstants.FieldNames.TENDER_NO_TENDERERS, 1); Aggregation agg = newAggregation( match(where(MongoConstants.FieldNames.TENDER_NO_TENDERERS).gt(0) .and(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).exists(true) .andOperator(getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))), new CustomProjectionOperation(project), group().avg(MongoConstants.FieldNames.TENDER_NO_TENDERERS) .as(Keys.AVERAGE_NO_OF_TENDERERS)); return releaseAgg(agg); }  @ApiOperation(value = "Calculate average number of tenderers, by year. The endpoint can be filtered" + "by year read from tender.tenderPeriod.startDate. " + "The number of tenderers are read from tender.numberOfTenderers") @RequestMapping(value = "/api/averageNumberOfTenderersYearly", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json") List<Document> averageNumberOfTenderersYearly(@ModelAttribute @Valid final YearFilterPagingRequest filter); @ApiOperation(value = "Calculate average number of tenderers. The endpoint can be filtered" + "by year read from tender.tenderPeriod.startDate. " + "The number of tenderers are read from tender.numberOfTenderers") @RequestMapping(value = "/api/averageNumberOfTenderers", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json") List<Document> averageNumberOfTenderers(@ModelAttribute @Valid final YearFilterPagingRequest filter);  }
@Test public void averageNumberOfTenderers() throws Exception { final List<Document> averageNumberOfTenderers = averageNumberOfTenderersController .averageNumberOfTenderersYearly(new YearFilterPagingRequest()); final Document sec = averageNumberOfTenderers.get(1); int year = (int) sec.get(AverageNumberOfTenderersController.Keys.YEAR); double averageNoTenderers = (double) sec.get(AverageNumberOfTenderersController.Keys.AVERAGE_NO_OF_TENDERERS); Assert.assertEquals(2015, year); Assert.assertEquals(5.5, averageNoTenderers, 0); final Document first = averageNumberOfTenderers.get(0); year = (int) first.get(AverageNumberOfTenderersController.Keys.YEAR); averageNoTenderers = (double) first.get(AverageNumberOfTenderersController.Keys.AVERAGE_NO_OF_TENDERERS); Assert.assertEquals(2014, year); Assert.assertEquals(5.0, averageNoTenderers, 0); }