SubscriptionService { public void updateSubscription(Subscription subscription, String alarmId) { Alarm alarm = seyrenRepository.getAlarm(alarmId); Optional<Subscription> actualSub = alarm.getSubscriptions().stream().filter(s -> s.getId().equals(subscription.getId())).findAny(); if (!actualSub.isPresent()) { throw new CerebroException(ErrorCode.SUBSCRIPTION_UNKNOWN, "The subscription passed as parameter does not exist in the Alarm " + alarmId); } validateUpdateSubscription(actualSub.get(), subscription); if (alarm.isEnabled()) { if (isLastSubscriptionActiveToDisable(actualSub.get(), subscription, alarm)) { alarm.setEnabled(false); seyrenRepository.updateAlarm(alarm); } } else if (!actualSub.get().isEnabled() && subscription.isEnabled()) { alarm.setEnabled(true); seyrenRepository.updateAlarm(alarm); } seyrenRepository.updateSubscription(subscription, alarmId); }  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 updateSubscriptionOK() throws Exception { Subscription subscription = TestUtils.getDefaultSubscription(); subscription.setMo(!subscription.isMo()); subscriptionService.updateSubscription(subscription, TestUtils.DEFAULT_ALARM_ID); verify(seyrenRepository).updateSubscription(subscription, TestUtils.DEFAULT_ALARM_ID); }
@Test public void updateSubscriptionFailIfTargetChange() throws Exception { Subscription subscription = TestUtils.getDefaultSubscription(); subscription.setTarget("coucou"); try { subscriptionService.updateSubscription(subscription, TestUtils.DEFAULT_ALARM_ID); fail("Update target of a subscription should throw an exception"); } catch (CerebroException ce) { assertEquals(ErrorCode.SUBSCRIPTION_UPDATE_INVALID, ce.getErrorCode()); } }
@Test public void updateSubscriptionFailIfTypeChange() throws Exception { Subscription subscription = TestUtils.getDefaultSubscription(); subscription.setType(SubscriptionType.HIPCHAT); try { subscriptionService.updateSubscription(subscription, TestUtils.DEFAULT_ALARM_ID); fail("Update type of a subscription should throw an exception"); } catch (CerebroException ce) { assertEquals(ErrorCode.SUBSCRIPTION_UPDATE_INVALID, ce.getErrorCode()); } }
@Test public void updateSubscriptionFailIfNoDayToSendAlert() throws Exception { Subscription subscription = TestUtils.getDefaultSubscription(); subscription.setMo(false); subscription.setTu(false); subscription.setWe(false); subscription.setTh(false); subscription.setFr(false); subscription.setSa(false); subscription.setSu(false); try { subscriptionService.updateSubscription(subscription, TestUtils.DEFAULT_ALARM_ID); fail("Update a subscription set no alerting day should throw an exception"); } catch (CerebroException ce) { assertEquals(ErrorCode.SUBSCRIPTION_INVALID, ce.getErrorCode()); } }
@Test public void updateDisableLastSubscriptionAndDisableAlarmOK() throws Exception { Alarm alarm = TestUtils.getDefaultAlarm(); Subscription subscription = alarm.getSubscriptions().get(0); subscription.setEnabled(false); subscriptionService.updateSubscription(subscription, TestUtils.DEFAULT_ALARM_ID); verify(seyrenRepository).updateSubscription(subscription, TestUtils.DEFAULT_ALARM_ID); verify(seyrenRepository).updateAlarm(argThat(new ArgumentMatcher<Alarm>() { @Override public boolean matches(Object argument) { Alarm argAlarm = (Alarm) argument; return argAlarm.getId().equals(alarm.getId()) && !argAlarm.isEnabled(); } })); }
@Test public void updateSubscriptionEnableAlarm() throws Exception { Alarm disabledAlarm = TestUtils.getDefaultAlarm(); disabledAlarm.getSubscriptions().get(0).setEnabled(false); when(seyrenRepository.getAlarm(TestUtils.DEFAULT_ALARM_ID)).thenReturn(disabledAlarm); Alarm alarm = TestUtils.getDefaultAlarm(); Subscription subscription = alarm.getSubscriptions().get(0); subscription.setEnabled(true); subscriptionService.updateSubscription(subscription, TestUtils.DEFAULT_ALARM_ID); verify(seyrenRepository).updateSubscription(subscription, TestUtils.DEFAULT_ALARM_ID); verify(seyrenRepository).updateAlarm(argThat(new ArgumentMatcher<Alarm>() { @Override public boolean matches(Object argument) { Alarm argAlarm = (Alarm) argument; return argAlarm.getId().equals(alarm.getId()) && argAlarm.isEnabled(); } })); }