@Test public void shouldThrowException_whenPolicyDoesNotExist() { // given when(alertsPoliciesApiMock.getByName(POLICY_NAME)).thenReturn(Optional.empty()); PolicyConfiguration policyConfiguration = buildPolicyConfiguration(); // then - exception expectedException.expect(NewRelicSyncException.class); expectedException.expectMessage(format("Policy %s does not exist", POLICY_NAME)); // when testee.sync(policyConfiguration); }
@Test public void shouldDoNothing_whenEmptyChannelsInConfiguration() { // given PolicyConfiguration policyConfiguration = buildPolicyConfiguration(); // when testee.sync(policyConfiguration); // then InOrder order = inOrder(alertsChannelsApiMock); order.verify(alertsChannelsApiMock).list(); order.verifyNoMoreInteractions(); }
@Test public void shouldThrowException_whenUserChannelDosNotExist() { // given PolicyConfiguration policyConfiguration = buildPolicyConfiguration(USER_CHANNEL); // then - exception expectedException.expect(NewRelicSyncException.class); expectedException.expectMessage("Alerts channel with configuration"); // when testee.sync(policyConfiguration); }
@Test public void shouldNotCreateUserChannel() { // given when(alertsChannelsApiMock.list()).thenReturn(ImmutableList.of(savedUserChannel)); PolicyConfiguration policyConfiguration = buildPolicyConfiguration(USER_CHANNEL); AlertsPolicyChannels expected = AlertsPolicyChannels.builder() .policyId(POLICY.getId()) .channelIds(ImmutableSet.of(savedUserChannel.getId())) .build(); // when testee.sync(policyConfiguration); // then InOrder order = inOrder(alertsChannelsApiMock, alertsPoliciesApiMock); order.verify(alertsChannelsApiMock).list(); order.verify(alertsPoliciesApiMock).updateChannels(expected); order.verifyNoMoreInteractions(); }
@Test public void shouldCreateRequiredChannels() { // given when(alertsChannelsApiMock.list()).thenReturn(ImmutableList.of(savedUserChannel)); PolicyConfiguration policyConfiguration = buildPolicyConfiguration(EMAIL_CHANNEL, SLACK_CHANNEL); // when testee.sync(policyConfiguration); // then AlertsPolicyChannels expected = AlertsPolicyChannels.builder() .policyId(POLICY_ID) .channelIds(ImmutableSet.of(savedEmailChannel.getId(), savedSlackChannel.getId())) .build(); InOrder order = inOrder(alertsChannelsApiMock, alertsPoliciesApiMock); order.verify(alertsChannelsApiMock).list(); order.verify(alertsChannelsApiMock).create(configuredEmailChannel); order.verify(alertsChannelsApiMock).create(configuredSlackChannel); order.verify(alertsPoliciesApiMock).updateChannels(expected); order.verifyNoMoreInteractions(); }
@Test public void shouldNotRemoveUnusedUserChannel() { // given AlertsChannel userChannelInPolicy = channelInPolicy(savedUserChannel, POLICY_ID); when(alertsChannelsApiMock.list()).thenReturn(ImmutableList.of(userChannelInPolicy)); when(alertsChannelsApiMock.deleteFromPolicy(POLICY_ID, userChannelInPolicy.getId())) .thenReturn(userChannelInPolicy); // when PolicyConfiguration policyConfiguration = buildPolicyConfiguration(); testee.sync(policyConfiguration); // then AlertsPolicyChannels expected = AlertsPolicyChannels.builder() .policyId(POLICY_ID) .channelIds(emptySet()) .build(); InOrder order = inOrder(alertsChannelsApiMock, alertsPoliciesApiMock); order.verify(alertsChannelsApiMock).list(); order.verify(alertsPoliciesApiMock).updateChannels(expected); order.verify(alertsChannelsApiMock).deleteFromPolicy(POLICY_ID, savedUserChannel.getId()); order.verify(alertsChannelsApiMock, never()).delete(savedUserChannel.getId()); order.verifyNoMoreInteractions(); }
@Test public void shouldNotRemoveUnsuedChannel_whenChannelBelongsToDifferentPolicy() { // given AlertsChannel emailChannelInPolicy = channelInPolicy(savedEmailChannel, POLICY_ID, POLICY_ID + 1); when(alertsChannelsApiMock.list()).thenReturn(ImmutableList.of( savedUserChannel, emailChannelInPolicy, channelInPolicy(savedSlackChannel, POLICY_ID) )); when(alertsChannelsApiMock.deleteFromPolicy(POLICY_ID, emailChannelInPolicy.getId())) .thenReturn(emailChannelInPolicy); // when PolicyConfiguration policyConfiguration = buildPolicyConfiguration(SLACK_CHANNEL); testee.sync(policyConfiguration); // then AlertsPolicyChannels expected = AlertsPolicyChannels.builder() .policyId(POLICY_ID) .channelIds(ImmutableSet.of(savedSlackChannel.getId())) .build(); InOrder order = inOrder(alertsChannelsApiMock, alertsPoliciesApiMock); order.verify(alertsChannelsApiMock).list(); order.verify(alertsPoliciesApiMock).updateChannels(expected); order.verify(alertsChannelsApiMock).deleteFromPolicy(POLICY_ID, savedEmailChannel.getId()); order.verify(alertsChannelsApiMock, never()).delete(savedEmailChannel.getId()); order.verifyNoMoreInteractions(); }
@Test public void shouldRemoveUnusedPolicyChannel() { // given AlertsChannel emailChannelInPolicy = channelInPolicy(savedEmailChannel, POLICY_ID); when(alertsChannelsApiMock.list()).thenReturn(ImmutableList.of( savedUserChannel, emailChannelInPolicy, channelInPolicy(savedSlackChannel, POLICY_ID) )); when(alertsChannelsApiMock.deleteFromPolicy(POLICY_ID, emailChannelInPolicy.getId())) .thenReturn(emailChannelInPolicy); // when PolicyConfiguration policyConfiguration = buildPolicyConfiguration(SLACK_CHANNEL); testee.sync(policyConfiguration); // then AlertsPolicyChannels expected = AlertsPolicyChannels.builder() .policyId(POLICY_ID) .channelIds(ImmutableSet.of(savedSlackChannel.getId())) .build(); InOrder order = inOrder(alertsChannelsApiMock, alertsPoliciesApiMock); order.verify(alertsChannelsApiMock).list(); order.verify(alertsPoliciesApiMock).updateChannels(expected); order.verify(alertsChannelsApiMock).deleteFromPolicy(POLICY_ID, savedEmailChannel.getId()); order.verify(alertsChannelsApiMock).delete(savedEmailChannel.getId()); order.verifyNoMoreInteractions(); }
.thenReturn(emailChannelInPolicy); PolicyConfiguration policyConfiguration = buildPolicyConfiguration(updatedEmailChannel, SLACK_CHANNEL);