AverageNumberOfTenderersController extends GenericOCDSController { @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/averageNumberOfTenderers", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json") public List<DBObject> averageNumberOfTenderers(@ModelAttribute @Valid final YearFilterPagingRequest filter) { DBObject project = new BasicDBObject(); addYearlyMonthlyProjection(filter, project, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE_REF); project.put("tender.numberOfTenderers", 1); Aggregation agg = newAggregation( match(where("tender.numberOfTenderers").gt(0) .and(MongoConstants.FieldNames.TENDER_PERIOD_START_DATE).exists(true) .andOperator(getYearDefaultFilterCriteria(filter, MongoConstants.FieldNames.TENDER_PERIOD_START_DATE))), new CustomProjectionOperation(project), group(getYearlyMonthlyGroupingFields(filter)).avg("tender.numberOfTenderers") .as(Keys.AVERAGE_NO_OF_TENDERERS), transformYearlyGrouping(filter).andInclude(Keys.AVERAGE_NO_OF_TENDERERS), getSortByYearMonth(filter), skip(filter.getSkip()), limit(filter.getPageSize())); AggregationResults<DBObject> results = mongoTemplate.aggregate(agg, "release", DBObject.class); List<DBObject> list = results.getMappedResults(); return list; }  @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/averageNumberOfTenderers", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json") List<DBObject> averageNumberOfTenderers(@ModelAttribute @Valid final YearFilterPagingRequest filter);  }
@Test public void averageNumberOfTenderers() throws Exception { final List<DBObject> averageNumberOfTenderers = averageNumberOfTenderersController .averageNumberOfTenderers(new YearFilterPagingRequest()); final DBObject 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 DBObject 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); }