AlarmService { public Alarm subscribeToAAlarm(final Alarm alarm) { LOGGER.debug("Search identical alarm to: " + alarm); Alarm identicalAlarm = this.searchAlarm(alarm); if (identicalAlarm != null) { LOGGER.debug("Identical alarm found ({}).", identicalAlarm.getId()); activateAlarm(identicalAlarm); Subscription subscriptionToAdd = alarm.getSubscriptions().iterator().next(); try { subscriptionService.addSubscription(subscriptionToAdd, identicalAlarm.getId()); return identicalAlarm; } catch (DuplicateSubscriptionException exception) { LOGGER.debug("Adding subscription not possible, identical subscription found for alarm {}.", identicalAlarm.getId()); return identicalAlarm; } } else { Alarm alarmByName = this.searchAlarmByName(alarm.getName()); if (alarmByName != null) { throw new CerebroException(ErrorCode.ALARM_DUPLICATE_NAME, "A alarm with name '" + alarm.getName() + "' already exist"); } seyrenRepository.addAlarm(alarm); return this.searchAlarmByName(alarm.getName()); } }  Alarm getAlarm(String id); Collection<Alarm> getAllAlarms(); Alarm subscribeToAAlarm(final Alarm alarm); Alarm searchAlarm(final Alarm alarm); Collection<Alarm> searchAlarmsBySubscriptionTarget(String subTarget); Alarm updateAlarm(final Alarm alarm);  }
@Test public void subscribeAlarmWithSameName() throws Exception { Alarm alarm = new Alarm(); alarm.setName(TestUtils.DEFAULT_ALARM_NAME); try { alarmService.subscribeToAAlarm(alarm); fail("Adding a alarm with same name"); } catch (CerebroException ce) { assertEquals(ErrorCode.ALARM_DUPLICATE_NAME, ce.getErrorCode()); } }
@Test public void subscribeToAnExistingAlarm() throws Exception { Alarm alarm = TestUtils.getDefaultAlarm(); Subscription subToAdd = TestUtils.getDefaultSubscription(); subToAdd.setId("subTestId"); subToAdd.setTarget("test@anotherteam.org"); subToAdd.setSu(true); alarm.setSubscriptions(Arrays.asList(subToAdd)); alarmService.subscribeToAAlarm(alarm); verify(seyrenRepository, never()).addAlarm(any()); verify(subscriptionService).addSubscription(subToAdd, TestUtils.DEFAULT_ALARM_ID); }
@Test public void subscribeToAExistingAlarmAlreadyHaveSubscription() throws Exception { Alarm addedAlarm = TestUtils.getDefaultAlarm(); Alarm returnedAlarm = alarmService.subscribeToAAlarm(addedAlarm); verify(seyrenRepository, never()).addAlarm(any()); verify(seyrenRepository, never()).addSubscription(any(), any()); assertNotNull(returnedAlarm); assertEquals(addedAlarm.getId(), returnedAlarm.getId()); }
@Test public void subscribeToAExistingAlarmAlreadyHaveSubscriptionTargetCase() throws Exception { Alarm addedAlarm = TestUtils.getDefaultAlarm(); Subscription s1 = TestUtils.getDefaultSubscription(); s1.setTarget(TestUtils.DEFAULT_EMAIL.toUpperCase()); addedAlarm.setSubscriptions(Arrays.asList(s1)); alarmService.subscribeToAAlarm(addedAlarm); verify(seyrenRepository, never()).addAlarm(any()); verify(seyrenRepository, never()).addSubscription(s1, addedAlarm.getId()); }