package org.openstack.atlas.api.async; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Matchers; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.openstack.atlas.api.async.util.STMTestBase; import org.openstack.atlas.api.atom.EntryHelper; import org.openstack.atlas.api.integration.ReverseProxyLoadBalancerStmService; import org.openstack.atlas.cfg.ConfigurationKey; import org.openstack.atlas.cfg.RestApiConfiguration; import org.openstack.atlas.service.domain.entities.ConnectionLimit; import org.openstack.atlas.service.domain.entities.LoadBalancerStatus; import org.openstack.atlas.service.domain.events.entities.CategoryType; import org.openstack.atlas.service.domain.events.entities.EventSeverity; import org.openstack.atlas.service.domain.events.entities.EventType; import org.openstack.atlas.service.domain.exceptions.EntityNotFoundException; import org.openstack.atlas.service.domain.services.LoadBalancerService; import org.openstack.atlas.service.domain.services.NotificationService; import org.openstack.atlas.service.domain.services.helpers.AlertType; import javax.jms.ObjectMessage; import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.*; public class UpdateConnectionThrottleListenerTest extends STMTestBase { private Integer LOAD_BALANCER_ID; private Integer ACCOUNT_ID; private String USERNAME = "SOME_USERNAME"; private Integer CONNECTION_LIMIT_ID = 20; @Mock private ObjectMessage objectMessage; @Mock private LoadBalancerService loadBalancerService; @Mock private NotificationService notificationService; @Mock private ReverseProxyLoadBalancerStmService reverseProxyLoadBalancerStmService; @Mock private RestApiConfiguration config; private UpdateConnectionThrottleListener updateConnectionThrottleListener; @Before public void standUp() { MockitoAnnotations.initMocks(this); setupIvars(); ConnectionLimit connectionLimit = new ConnectionLimit(); connectionLimit.setId(CONNECTION_LIMIT_ID); LOAD_BALANCER_ID = lb.getId(); ACCOUNT_ID = lb.getAccountId(); lb.setUserName(USERNAME); lb.setConnectionLimit(connectionLimit); updateConnectionThrottleListener = new UpdateConnectionThrottleListener(); updateConnectionThrottleListener.setLoadBalancerService(loadBalancerService); updateConnectionThrottleListener.setNotificationService(notificationService); updateConnectionThrottleListener.setReverseProxyLoadBalancerStmService(reverseProxyLoadBalancerStmService); updateConnectionThrottleListener.setConfiguration(config); } @After public void tearDown() { } @Test public void testUpdateLoadBalancerWithValidThrottle() throws Exception { when(objectMessage.getObject()).thenReturn(lb); when(loadBalancerService.get(LOAD_BALANCER_ID, ACCOUNT_ID)).thenReturn(lb); when(config.getString(Matchers.<ConfigurationKey>any())).thenReturn("REST"); updateConnectionThrottleListener.doOnMessage(objectMessage); verify(reverseProxyLoadBalancerStmService).updateConnectionThrottle(lb); verify(loadBalancerService).setStatus(lb, LoadBalancerStatus.ACTIVE); verify(notificationService).saveConnectionLimitEvent(USERNAME, ACCOUNT_ID, LOAD_BALANCER_ID, lb.getConnectionLimit().getId(), EntryHelper.UPDATE_THROTTLE_TITLE, EntryHelper.createConnectionThrottleSummary(lb), EventType.UPDATE_CONNECTION_THROTTLE, CategoryType.UPDATE, EventSeverity.INFO); } @Test public void testUpdateInvalidLoadBalancer() throws Exception { EntityNotFoundException entityNotFoundException = new EntityNotFoundException(); when(objectMessage.getObject()).thenReturn(lb); when(loadBalancerService.get(LOAD_BALANCER_ID, ACCOUNT_ID)).thenThrow(entityNotFoundException); updateConnectionThrottleListener.doOnMessage(objectMessage); verify(notificationService).saveAlert(eq(ACCOUNT_ID), eq(LOAD_BALANCER_ID), eq(entityNotFoundException), eq(AlertType.DATABASE_FAILURE.name()), anyString()); verify(notificationService).saveConnectionLimitEvent(eq(USERNAME), eq(ACCOUNT_ID), eq(LOAD_BALANCER_ID), anyInt(), anyString(), anyString(), eq(EventType.UPDATE_CONNECTION_THROTTLE), eq(CategoryType.UPDATE), eq(EventSeverity.CRITICAL)); } @Test public void testUpdateLoadBalancerWithInvalidThrottle() throws Exception { Exception exception = new Exception(); when(objectMessage.getObject()).thenReturn(lb); when(loadBalancerService.get(LOAD_BALANCER_ID, ACCOUNT_ID)).thenReturn(lb); doThrow(exception).when(reverseProxyLoadBalancerStmService).updateConnectionThrottle(lb); when(config.getString(Matchers.<ConfigurationKey>any())).thenReturn("REST"); updateConnectionThrottleListener.doOnMessage(objectMessage); verify(reverseProxyLoadBalancerStmService).updateConnectionThrottle(lb); verify(loadBalancerService).setStatus(lb, LoadBalancerStatus.ERROR); verify(notificationService).saveAlert(eq(ACCOUNT_ID), eq(LOAD_BALANCER_ID), eq(exception), eq(AlertType.ZEUS_FAILURE.name()), anyString()); verify(notificationService).saveConnectionLimitEvent(eq(USERNAME), eq(ACCOUNT_ID), eq(LOAD_BALANCER_ID), anyInt(), anyString(), anyString(), eq(EventType.UPDATE_CONNECTION_THROTTLE), eq(CategoryType.UPDATE), eq(EventSeverity.CRITICAL)); } }