Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Introduce additional tests to increase code coverage

[#350]
  • Loading branch information
smcvb committed Nov 7, 2017
1 parent c853857 commit d76fd0f
Showing 1 changed file with 77 additions and 5 deletions.
Expand Up @@ -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.*;

Expand All @@ -62,6 +61,7 @@ public class SpringCloudHttpBackupCommandRouterTest {
@Mock
private ServiceInstance serviceInstance;

private URI testRemoteUri = URI.create("http://remote");
private MessageRoutingInformation expectedMessageRoutingInfo;
@Captor
private ArgumentCaptor<URI> uriArgumentCaptor;
Expand All @@ -83,8 +83,9 @@ public void setUp() throws Exception {
ResponseEntity<MessageRoutingInformation> 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,
Expand All @@ -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<MessageRoutingInformation> result = testSubject.getMessageRoutingInformation(serviceInstance);

assertTrue(result.isPresent());
Expand All @@ -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<MessageRoutingInformation> result = testSubject.getMessageRoutingInformation(remoteInstance);

Expand All @@ -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<MessageRoutingInformation> 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<MessageRoutingInformation> 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))
Expand All @@ -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<MessageRoutingInformation> 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());
}
}

0 comments on commit d76fd0f

Please sign in to comment.