### Question:
PolicyConfigurator { void sync(@NonNull PolicyConfiguration config) { LOG.info("Synchronizing policy {}...", config.getPolicyName()); AlertsPolicy alertsPolicyFromConfig = AlertsPolicy.builder() .name(config.getPolicyName()) .incidentPreference(config.getIncidentPreference().name()) .build(); Optional<AlertsPolicy> policy = api.getAlertsPoliciesApi().getByName(config.getPolicyName()); if (policy.isPresent()) { AlertsPolicy oldPolicy = policy.get(); if (!StringUtils.equals(alertsPolicyFromConfig.getIncidentPreference(), oldPolicy.getIncidentPreference())) { api.getAlertsPoliciesApi().delete(oldPolicy.getId()); api.getAlertsPoliciesApi().create(alertsPolicyFromConfig); LOG.info(format("Policy %s updated", config.getPolicyName())); } } else { api.getAlertsPoliciesApi().create(alertsPolicyFromConfig); LOG.info("Policy {} created", config.getPolicyName()); } LOG.info("Policy {} synchronized", config.getPolicyName()); } PolicyConfigurator(@NonNull NewRelicApi api);   }### Answer:
@Test public void shouldCreateNewPolicy_whenPolicyDoesNotExist() { when(alertsPoliciesApiMock.getByName(POLICY_NAME)).thenReturn(Optional.empty()); AlertsPolicy expectedPolicy = AlertsPolicy.builder().name(POLICY_NAME).incidentPreference(INCIDENT_PREFERENCE.name()).build(); testee.sync(CONFIGURATION); InOrder order = inOrder(alertsPoliciesApiMock); order.verify(alertsPoliciesApiMock).getByName(POLICY_NAME); order.verify(alertsPoliciesApiMock).create(expectedPolicy); order.verifyNoMoreInteractions(); }
@Test public void shouldDeleteAndCreateNewPolicy_whenPolicyUpdated() { when(alertsPoliciesApiMock.getByName(POLICY_NAME)).thenReturn(Optional.of(ALERT_POLICY_DIFFERENT)); AlertsPolicy expectedPolicy = AlertsPolicy.builder().name(POLICY_NAME).incidentPreference(INCIDENT_PREFERENCE.name()).build(); testee.sync(CONFIGURATION); InOrder order = inOrder(alertsPoliciesApiMock); order.verify(alertsPoliciesApiMock).getByName(POLICY_NAME); order.verify(alertsPoliciesApiMock).delete(ALERT_POLICY_DIFFERENT.getId()); order.verify(alertsPoliciesApiMock).create(expectedPolicy); order.verifyNoMoreInteractions(); }
@Test public void shouldDoNothing_whenPolicyNotUpdated() { when(alertsPoliciesApiMock.getByName(POLICY_NAME)).thenReturn(Optional.of(ALERT_POLICY_SAME)); testee.sync(CONFIGURATION); InOrder order = inOrder(alertsPoliciesApiMock); order.verify(alertsPoliciesApiMock).getByName(POLICY_NAME); order.verifyNoMoreInteractions(); }