From d76fd0f2eda68ee8fba32e9d8f379edf29bc4830 Mon Sep 17 00:00:00 2001 From: smcvb Date: Tue, 7 Nov 2017 21:15:30 +0100 Subject: [PATCH] Add tests Introduce additional tests to increase code coverage [#350] --- ...pringCloudHttpBackupCommandRouterTest.java | 82 +++++++++++++++++-- 1 file changed, 77 insertions(+), 5 deletions(-) diff --git a/distributed-commandbus-springcloud/src/test/java/org/axonframework/springcloud/commandhandling/SpringCloudHttpBackupCommandRouterTest.java b/distributed-commandbus-springcloud/src/test/java/org/axonframework/springcloud/commandhandling/SpringCloudHttpBackupCommandRouterTest.java index 624b8513ee..c36965e1c6 100644 --- a/distributed-commandbus-springcloud/src/test/java/org/axonframework/springcloud/commandhandling/SpringCloudHttpBackupCommandRouterTest.java +++ b/distributed-commandbus-springcloud/src/test/java/org/axonframework/springcloud/commandhandling/SpringCloudHttpBackupCommandRouterTest.java @@ -38,7 +38,6 @@ import java.util.function.Predicate; import static org.junit.Assert.*; -import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.*; @@ -62,6 +61,7 @@ public class SpringCloudHttpBackupCommandRouterTest { @Mock private ServiceInstance serviceInstance; + private URI testRemoteUri = URI.create("http://remote"); private MessageRoutingInformation expectedMessageRoutingInfo; @Captor private ArgumentCaptor uriArgumentCaptor; @@ -83,8 +83,9 @@ public void setUp() throws Exception { ResponseEntity responseEntity = mock(ResponseEntity.class); when(responseEntity.hasBody()).thenReturn(true); when(responseEntity.getBody()).thenReturn(expectedMessageRoutingInfo); + URI expectedRemoteUri = URI.create("http://remote/message-routing-information"); when(restTemplate.exchange( - any(), eq(HttpMethod.GET), eq(HttpEntity.EMPTY), eq(MessageRoutingInformation.class) + eq(expectedRemoteUri), eq(HttpMethod.GET), eq(HttpEntity.EMPTY), eq(MessageRoutingInformation.class) )).thenReturn(responseEntity); testSubject = new SpringCloudHttpBackupCommandRouter(discoveryClient, @@ -110,8 +111,13 @@ public void testGetLocalMessageRoutingInformationReturnsMessageRoutingInformatio @Test public void testGetMessageRoutingInformationReturnsLocalMessageRoutingInformationIfSimpleMemberIsLocal() throws Exception { + ServiceInstance serviceInstance = mock(ServiceInstance.class); + when(serviceInstance.getServiceId()).thenReturn(SERVICE_INSTANCE_ID); + when(serviceInstance.getUri()).thenReturn(SERVICE_INSTANCE_URI); + when(serviceInstance.getMetadata()).thenReturn(new HashMap<>()); + testSubject.updateMembership(LOAD_FACTOR, COMMAND_NAME_FILTER); - //TODO Still needs a fix + Optional result = testSubject.getMessageRoutingInformation(serviceInstance); assertTrue(result.isPresent()); @@ -131,7 +137,7 @@ public void testGetMessageRoutingInformationThrowsIllegalArgumentExceptionIfEndp public void testGetMessageRoutingInformationRequestsMessageRoutingInformation() throws Exception { ServiceInstance remoteInstance = mock(ServiceInstance.class); when(remoteInstance.getServiceId()).thenReturn(SERVICE_INSTANCE_ID); - when(remoteInstance.getUri()).thenReturn(URI.create("http://remote")); + when(remoteInstance.getUri()).thenReturn(testRemoteUri); Optional result = testSubject.getMessageRoutingInformation(remoteInstance); @@ -147,13 +153,41 @@ public void testGetMessageRoutingInformationRequestsMessageRoutingInformation() assertEquals(messageRoutingInformationEndpoint, resultUri.getPath()); } + @SuppressWarnings("unchecked") + @Test + public void testGetMessageRoutingInformationReturnsEmptyOptionalFromNonAxonServiceInstanceRequest() + throws Exception { + ServiceInstance nonAxonInstance = mock(ServiceInstance.class); + when(nonAxonInstance.getServiceId()).thenReturn(SERVICE_INSTANCE_ID); + when(nonAxonInstance.getUri()).thenReturn(URI.create("http://non-axon")); + + ResponseEntity responseEntity = mock(ResponseEntity.class); + when(responseEntity.hasBody()).thenReturn(false); + URI testRemoteUri = URI.create("http://non-axon/message-routing-information"); + when(restTemplate.exchange( + eq(testRemoteUri), eq(HttpMethod.GET), eq(HttpEntity.EMPTY), eq(MessageRoutingInformation.class) + )).thenReturn(responseEntity); + + Optional result = testSubject.getMessageRoutingInformation(nonAxonInstance); + + assertFalse(result.isPresent()); + + verify(restTemplate).exchange(uriArgumentCaptor.capture(), + eq(HttpMethod.GET), + eq(HttpEntity.EMPTY), + eq(MessageRoutingInformation.class)); + + URI resultUri = uriArgumentCaptor.getValue(); + assertEquals(messageRoutingInformationEndpoint, resultUri.getPath()); + } + @Test public void testUpdateMembershipsOnHeartbeatEventRequestsMessageRoutingInformationByHttpRequest() throws Exception { testSubject.updateMembership(LOAD_FACTOR, COMMAND_NAME_FILTER); ServiceInstance remoteInstance = mock(ServiceInstance.class); when(remoteInstance.getServiceId()).thenReturn(SERVICE_INSTANCE_ID); - when(remoteInstance.getUri()).thenReturn(URI.create("http://remote")); + when(remoteInstance.getUri()).thenReturn(testRemoteUri); when(discoveryClient.getServices()).thenReturn(ImmutableList.of(SERVICE_INSTANCE_ID)); when(discoveryClient.getInstances(SERVICE_INSTANCE_ID)) @@ -168,4 +202,42 @@ public void testUpdateMembershipsOnHeartbeatEventRequestsMessageRoutingInformati eq(HttpEntity.EMPTY), eq(MessageRoutingInformation.class)); } + + @SuppressWarnings("unchecked") + @Test + public void testUpdateMembershipsOnHeartbeatEventDoesNotRequestInfoFromBlackListedServiceInstance() + throws Exception { + testSubject.updateMembership(LOAD_FACTOR, COMMAND_NAME_FILTER); + + String nonAxonServiceInstanceId = "nonAxonInstance"; + ServiceInstance nonAxonInstance = mock(ServiceInstance.class); + when(nonAxonInstance.getServiceId()).thenReturn(nonAxonServiceInstanceId); + when(nonAxonInstance.getUri()).thenReturn(URI.create("http://non-axon")); + + when(discoveryClient.getServices()).thenReturn(ImmutableList.of(SERVICE_INSTANCE_ID, nonAxonServiceInstanceId)); + when(discoveryClient.getInstances(nonAxonServiceInstanceId)).thenReturn(ImmutableList.of(nonAxonInstance)); + + ResponseEntity responseEntity = mock(ResponseEntity.class); + when(responseEntity.hasBody()).thenReturn(false); + URI testRemoteUri = URI.create("http://non-axon/message-routing-information"); + when(restTemplate.exchange( + eq(testRemoteUri), eq(HttpMethod.GET), eq(HttpEntity.EMPTY), eq(MessageRoutingInformation.class) + )).thenReturn(responseEntity); + + // First update - black lists 'nonAxonServiceInstance' as it does not contain any message routing information + testSubject.updateMemberships(mock(HeartbeatEvent.class)); + // Second update + testSubject.updateMemberships(mock(HeartbeatEvent.class)); + + verify(discoveryClient, times(2)).getServices(); + verify(discoveryClient, times(2)).getInstances(nonAxonServiceInstanceId); + verify(discoveryClient, times(2)).getInstances(SERVICE_INSTANCE_ID); + verify(restTemplate, times(1)).exchange(uriArgumentCaptor.capture(), + eq(HttpMethod.GET), + eq(HttpEntity.EMPTY), + eq(MessageRoutingInformation.class)); + + URI resultUri = uriArgumentCaptor.getValue(); + assertEquals(messageRoutingInformationEndpoint, resultUri.getPath()); + } }