SeyrenRepository { public void addSubscription(final Subscription subscription, final String alarmId) { final ResponseEntity<Object> response = restTemplate.postForEntity(seyrenUrl + API_ALARMS + "/" + alarmId + "/subscriptions/", subscription, Object.class); if (!response.getStatusCode().is2xxSuccessful()) { throw new SeyrenException("addSubscription", response.getStatusCode().value()); } LOGGER.info("Add subscription for alarm {}. Response {}", alarmId, response.getStatusCode()); }  void setRestTemplate(final RestTemplate restTemplate); void setSeyrenUrl(final String seyrenUrl); Alarm getAlarm(final String id); Alarm.Alarms getAllAlarms(); void addAlarm(final Alarm alarm); void updateAlarm(final Alarm alarm); boolean deleteAlarm(final String alarmId); void addSubscription(final Subscription subscription, final String alarmId); void updateSubscription(final Subscription subscription, final String alarmId); boolean deleteSubscription(final String alarmId, final String subscriptionId); Alert.Alerts getAlerts(final int start, final int items); Alert.Alerts getAlarmAlerts(final String alarmId, final int start, final int items); static final String API_ALARMS; static final String API_ALERTS; }
@Test public void testAddSubscription_ok() { ResponseEntity<Object> response = new ResponseEntity(HttpStatus.OK); Subscription subscription = new Subscription(); when(restTemplate.postForEntity(anyString(),anyObject(),Matchers.<Class<Object>>any())).thenReturn(response); repository.addSubscription(subscription,"alarmid"); verify(restTemplate).postForEntity(DEFAULT_SEYREN_URL + SeyrenRepository.API_ALARMS+"/alarmid/subscriptions/", subscription, Object.class); }
@Test public void testAddSubscription_ko() { ResponseEntity<Object> response = new ResponseEntity(HttpStatus.CONFLICT); Subscription subscription = new Subscription(); when(restTemplate.postForEntity(anyString(),anyObject(),Matchers.<Class<Object>>any())).thenReturn(response); try{ repository.addSubscription(subscription,"alarmid"); verify(restTemplate).postForEntity(DEFAULT_SEYREN_URL + SeyrenRepository.API_ALARMS+"/alarmid/subscriptions/", subscription, Object.class); fail(); } catch(SeyrenException e){ assertEquals(e.getHttpStatus(),HttpStatus.CONFLICT.value()); assertEquals(e.getAction(),"addSubscription"); } }