CerebroController { @RequestMapping(value="/alerts", method = RequestMethod.GET) @ApiResponses(value={@ApiResponse(code=200, message = "Returns the latest alarm alerts")}) public Collection<Alert> getAlerts(@ApiParam(name="nbItem", value="number of alerts to return") @RequestParam(value = "items", required = false, defaultValue = "20") final int nbItem, @ApiParam(name="start", value="starting date") @RequestParam(value = "start", required = false, defaultValue = "0") final int start, @ApiParam(name="from") @RequestParam(value = "from", required = false) final String from) { Collection<Alert> alerts; if (from != null) { alerts = alertService.getAlerts(from); } else { alerts = alertService.getAlerts(nbItem, start).getValues(); } return alerts; }  @RequestMapping(value="/datasources/locations", method = RequestMethod.GET) @ApiResponses(value={@ApiResponse(code=200, message = "Returns the datasource backend location(s)")}) Collection<URI> sources(); @RequestMapping(value="/alarms", method = RequestMethod.GET) @ApiResponses(value={@ApiResponse(code=200, message = "Returns the subscriber's alarms, or all alarms if no email is provided")}) Collection<Alarm> all(@ApiParam(name="subscriptionTarget", value="a subscriber's email") @RequestParam(name = "subscriptionTarget", required = false) final String subTarget); @RequestMapping(value="/alarms/{id}", method = RequestMethod.GET) CerebroAlarm getAlarm(@ApiParam(name="id", value="an alarm Id", required = true) @PathVariable("id") final String id); @RequestMapping(value = "/alarms", method = RequestMethod.POST) @ApiResponses(value={@ApiResponse(code=200, message = "Creates a new alarm and returns its Id")}) String addAlarm(@ApiParam(name="alarm", value="an alarm to add", required = true) @RequestBody final Alarm alarm); @RequestMapping(value = "/alarms/{alarmId}/subscriptions/{subscriptionId}", method = RequestMethod.GET) @ApiResponses(value={@ApiResponse(code=200, message = "Returns the subscription with the given alarm and subscription Ids")}) Subscription getSubscription(@ApiParam(name="alarmId", value="an alarm Id") @PathVariable("alarmId") final String alarmId,
        @ApiParam(name = "subscriptionId", value = "a subscription Id") @PathVariable("subscriptionId") final String subscriptionId); @RequestMapping(value = "/alarms/search", method = RequestMethod.POST) @ApiResponses(value={@ApiResponse(code=200, message = "search for an alarm")}) Alarm searchAlarm(@ApiParam(name="alarm", value="the alarm to search") @RequestBody final Alarm alarm); @RequestMapping(value = "/alarms", method = RequestMethod.PUT) @ApiResponses(value={@ApiResponse(code=200, message = "Updates the alarm and returns its Id")}) String updateAlarm(@ApiParam(name="alarm", value="an updated alarm", required = true) @RequestBody final Alarm alarm); @RequestMapping(value = "/alarms/{alarmId}/subscriptions", method = RequestMethod.POST) @ApiResponses(value={@ApiResponse(code=200, message = "Adds a subscription to an alarm")}) void addSubscription(@ApiParam(name="subscription", value="a subscription to add") @RequestBody final Subscription subscription,
                                @ApiParam(name="alarmId", value="the Id of the alarm") @PathVariable final String alarmId); @RequestMapping(value = "/alarms/{alarmId}/subscriptions/{subscriptionId}", method = RequestMethod.DELETE) @ApiResponses(value={@ApiResponse(code=200, message = "Deletes the subscription with the given alarm and subscription Ids")}) String deleteSubscription(@ApiParam(name="alarmId", value="an alarm Id") @PathVariable("alarmId") final String alarmId,
        @ApiParam(name = "subscriptionId", value = "a subscription Id") @PathVariable("subscriptionId") final String subscriptionId); @RequestMapping(value = "/alarms/{alarmId}/subscriptions/search", method = RequestMethod.POST) @ApiResponses(value={@ApiResponse(code=200, message = "Search for a subscription")}) Subscription searchSubscription(@ApiParam(name="subscription", value="the subscription to search for") @RequestBody final Subscription subscription,
                                           @ApiParam(name="alarmId") @PathVariable final String alarmId); @RequestMapping(value = "/alarms/{alarmId}/subscriptions/{subscriptionId}", method = RequestMethod.PUT) @ApiResponses(value={@ApiResponse(code=200, message = "Updates a subscription")}) void updateSubscription(@ApiParam(name="subscription", value="the updated subscription") @RequestBody final Subscription subscription,
                                   @ApiParam(name="alarmId", value="the Id of the alarm to which the subscription is linked") @PathVariable("alarmId") final String alarmId); @RequestMapping(value="/alerts", method = RequestMethod.GET) @ApiResponses(value={@ApiResponse(code=200, message = "Returns the latest alarm alerts")}) Collection<Alert> getAlerts(@ApiParam(name="nbItem", value="number of alerts to return") @RequestParam(value = "items", required = false, defaultValue = "20") final int nbItem,
        							   @ApiParam(name="start", value="starting date") @RequestParam(value = "start", required = false, defaultValue = "0") final int start,
        							   @ApiParam(name="from") @RequestParam(value = "from", required = false) final String from); @RequestMapping(value="/alerts/stats/nochanges", method = RequestMethod.GET) @ApiResponses(value={@ApiResponse(code=200, message = "Returns stats (number of occurrence,current status) about all alerts without status change from \"from\" to now")}) List<Stat> getAlertStatNoTypeChange(@ApiParam(name="from", value="beginning date") @RequestParam("from") final String from); @RequestMapping("/alerts/stats/changes") @ApiResponses(value={@ApiResponse(code=200, message = "Returns stats (number of occurrence, current status) about all alerts with status change from \"from\" to now")}) List<Stat> getAlertStatTypeChange(@RequestParam("from") final String from); @RequestMapping(value="/alarms/{id}/alerts", method = RequestMethod.GET) @ApiResponses(value={@ApiResponse(code=200, message = "Returns all alerts for a given alarm")}) Collection<Alert> getAlarmAlerts(@ApiParam(name="id", value="an alarm Id") @PathVariable("id") final String id); @ExceptionHandler(CerebroException.class) @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR) String getErrorCode(final CerebroException exception);  }
@Test public void test_getAlerts_without_params() throws Exception{ Collection<Alert> values = Arrays.asList(getAlert("alert1"),getAlert("alert2")); Alert.Alerts alerts = new Alert.Alerts(); alerts.setValues(values); when(alertService.getAlerts(anyInt(), anyInt())).thenReturn(alerts); mockMvc.perform(get("/alerts")).andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)) .andExpect(jsonPath("$", hasSize(2))) .andExpect(jsonPath("$[0].id", is("alert1"))) .andExpect(jsonPath("$[1].id", is("alert2"))) ; }
@Test public void test_getAlerts_with_from_param() throws Exception{ Collection<Alert> values = Arrays.asList(getAlert("alert1"),getAlert("alert2")); when(alertService.getAlerts(anyString())).thenReturn(values); mockMvc.perform(get("/alerts").param("from","test")) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)) .andExpect(jsonPath("$", hasSize(2))) .andExpect(jsonPath("$[0].id", is("alert1"))) .andExpect(jsonPath("$[1].id", is("alert2"))) ; }
@Test public void test_getAlerts_with_start_param_and_nbItem_param() throws Exception{ Collection<Alert> values = Arrays.asList(getAlert("alert1"),getAlert("alert2")); Alert.Alerts alerts = new Alert.Alerts(); alerts.setValues(values); when(alertService.getAlerts(anyInt(), anyInt())).thenReturn(alerts); mockMvc.perform(get("/alerts") .param("start","1") .param("nbItem","2") ).andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)) .andExpect(jsonPath("$", hasSize(2))) .andExpect(jsonPath("$[0].id", is("alert1"))) .andExpect(jsonPath("$[1].id", is("alert2"))) ; }