SubscriptionService { public SubscriptionService.DeletedSubscriptionStatus deleteSubscription(String alarmId, String subscriptionId) { LOGGER.info("Delete subscription {} for alarm {}", subscriptionId, alarmId); if (this.seyrenRepository.deleteSubscription(alarmId, subscriptionId)) { Alarm alarm = seyrenRepository.getAlarm(alarmId); if (alarm.getSubscriptions().isEmpty()) { if (seyrenRepository.deleteAlarm(alarmId)) { return SubscriptionService.DeletedSubscriptionStatus.ALARM_DELETED; } throw new CerebroException(ErrorCode.SEYREN_ERROR, "Delete alarm '" + alarmId + "'has fail"); } return SubscriptionService.DeletedSubscriptionStatus.OK; } throw new CerebroException(ErrorCode.SUBSCRIPTION_DELETE_ERROR, "Delete subscription '" + subscriptionId + "' on alarm '" + alarmId + "' has fail"); }  Subscription getSubscription(String alarmId, String subscriptionId); void addSubscription(Subscription subscription, String alarmId); Subscription searchSubscription(Subscription subscription, String alarmId); void updateSubscription(Subscription subscription, String alarmId); SubscriptionService.DeletedSubscriptionStatus deleteSubscription(String alarmId, String subscriptionId);  }
@Test public void deleteSubscription() throws Exception { when(seyrenRepository.deleteSubscription(TestUtils.DEFAULT_ALARM_ID, TestUtils.DEFAULT_SUBSCRIPTION_ID)).thenReturn(true); assertEquals(SubscriptionService.DeletedSubscriptionStatus.OK, subscriptionService.deleteSubscription(TestUtils.DEFAULT_ALARM_ID, TestUtils.DEFAULT_SUBSCRIPTION_ID)); verify(seyrenRepository).deleteSubscription(TestUtils.DEFAULT_ALARM_ID, TestUtils.DEFAULT_SUBSCRIPTION_ID); verify(seyrenRepository).getAlarm(TestUtils.DEFAULT_ALARM_ID); verifyNoMoreInteractions(seyrenRepository); }
@Test public void deleteSubscriptionFail() throws Exception { when(seyrenRepository.deleteSubscription(TestUtils.DEFAULT_ALARM_ID, TestUtils.DEFAULT_SUBSCRIPTION_ID)).thenReturn(false); try { subscriptionService.deleteSubscription(TestUtils.DEFAULT_ALARM_ID, TestUtils.DEFAULT_SUBSCRIPTION_ID); } catch (CerebroException ce) { assertEquals(ErrorCode.SUBSCRIPTION_DELETE_ERROR, ce.getErrorCode()); } verify(seyrenRepository).deleteSubscription(TestUtils.DEFAULT_ALARM_ID, TestUtils.DEFAULT_SUBSCRIPTION_ID); verifyNoMoreInteractions(seyrenRepository); }
@Test public void deleteTheOnlyOneSubscriptionForAAlarm() throws Exception { when(seyrenRepository.deleteSubscription(TestUtils.DEFAULT_ALARM_ID, TestUtils.DEFAULT_SUBSCRIPTION_ID)).thenReturn(true); Alarm c = TestUtils.getDefaultAlarm(); c.setSubscriptions(new ArrayList<>()); assertTrue(c.getSubscriptions().isEmpty()); when(seyrenRepository.getAlarm(TestUtils.DEFAULT_ALARM_ID)).thenReturn(c); when(seyrenRepository.deleteAlarm(TestUtils.DEFAULT_ALARM_ID)).thenReturn(true); assertEquals(SubscriptionService.DeletedSubscriptionStatus.ALARM_DELETED, subscriptionService.deleteSubscription(TestUtils.DEFAULT_ALARM_ID, TestUtils.DEFAULT_SUBSCRIPTION_ID)); verify(seyrenRepository).deleteSubscription(TestUtils.DEFAULT_ALARM_ID, TestUtils.DEFAULT_SUBSCRIPTION_ID); verify(seyrenRepository).getAlarm(TestUtils.DEFAULT_ALARM_ID); verify(seyrenRepository).deleteAlarm(TestUtils.DEFAULT_ALARM_ID); verifyNoMoreInteractions(seyrenRepository); }