Skip to content

Commit

Permalink
Merge d0fa61f into 2605472
Browse files Browse the repository at this point in the history
  • Loading branch information
kstafford3 committed May 5, 2019
2 parents 2605472 + d0fa61f commit b807881
Showing 1 changed file with 140 additions and 0 deletions.
140 changes: 140 additions & 0 deletions src/test/java/io/dropwizard/discovery/client/DiscoveryClientTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package io.dropwizard.discovery.client;

import org.apache.curator.x.discovery.*;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;
import static org.mockito.Mockito.*;

public class DiscoveryClientTest {

DiscoveryClient client;

final String SERVICE_NAME = "service-name";

ServiceInstance<String> instanceOne = mock(ServiceInstance.class);
ServiceInstance<String> instanceTwo = mock(ServiceInstance.class);
ServiceInstance<String> instanceThree = mock(ServiceInstance.class);

ServiceDiscovery<String> discovery = mock(ServiceDiscovery.class);
DownInstancePolicy downInstancePolicy = mock(DownInstancePolicy.class);
ProviderStrategy<String> providerStrategy = mock(ProviderStrategy.class);
ServiceProvider<String> serviceProvider = mock(ServiceProvider.class);
ServiceCache<String> serviceCache = mock(ServiceCache.class);

@Before
public void setup() throws Exception {
ServiceProviderBuilder serviceProviderBuilder = mock(ServiceProviderBuilder.class);
when(discovery.serviceProviderBuilder()).thenReturn(serviceProviderBuilder);
when(serviceProviderBuilder.serviceName(anyString())).thenReturn(serviceProviderBuilder);
when(serviceProviderBuilder.downInstancePolicy(any())).thenReturn(serviceProviderBuilder);
when(serviceProviderBuilder.providerStrategy(any())).thenReturn(serviceProviderBuilder);
when(serviceProviderBuilder.build()).thenReturn(serviceProvider);

ServiceCacheBuilder serviceCacheBuilder = mock(ServiceCacheBuilder.class);
when(discovery.serviceCacheBuilder()).thenReturn(serviceCacheBuilder);
when(serviceCacheBuilder.name(anyString())).thenReturn(serviceCacheBuilder);
when(serviceCacheBuilder.build()).thenReturn(serviceCache);

when(discovery.queryForNames()).thenReturn(Arrays.asList("service-one", "service-two", "service-three"));
when(discovery.queryForInstances("service-one")).thenReturn(Arrays.asList(instanceOne, instanceTwo, instanceThree));
when(serviceCache.getInstances()).thenReturn(Arrays.asList(instanceOne, instanceTwo, instanceThree));
when(serviceProvider.getInstance()).thenReturn(instanceOne);

client = new DiscoveryClient(SERVICE_NAME, discovery, downInstancePolicy, providerStrategy);
}

@Test
public void testConstructorInvalidServiceName() {
try {
new DiscoveryClient(null, discovery, downInstancePolicy, providerStrategy);
failBecauseExceptionWasNotThrown(NullPointerException.class);
} catch(NullPointerException npe) {
}

try {
new DiscoveryClient("", discovery, downInstancePolicy, providerStrategy);
failBecauseExceptionWasNotThrown(IllegalArgumentException.class);
} catch (IllegalArgumentException e) {
}
}

@Test
public void testConstructorInvalidProviderStrategy() {
try {
new DiscoveryClient(SERVICE_NAME, discovery, downInstancePolicy, null);
failBecauseExceptionWasNotThrown(NullPointerException.class);
} catch (NullPointerException npe) {
}
}

@Test
public void testConstructorInvalidServiceDiscovery() {
try {
new DiscoveryClient(SERVICE_NAME, null, downInstancePolicy, providerStrategy);
failBecauseExceptionWasNotThrown(NullPointerException.class);
} catch (NullPointerException npe) {
}
}

@Test
public void testGetServices() throws Exception {
assertThat(client.getServices()).containsOnly("service-one", "service-two", "service-three");
}

@Test
public void testGetInstancesByName() throws Exception {
assertThat(client.getInstances("service-one")).containsOnly(instanceOne, instanceTwo, instanceThree);
}

@Test
public void testGetInstances() {
assertThat(client.getInstances()).containsOnly(instanceOne, instanceTwo, instanceThree);
}

@Test
public void testGetInstance() throws Exception {
assertThat(client.getInstance()).isEqualTo(instanceOne);
}

@Test
public void testNoteError() {
client.noteError(instanceOne);
verify(serviceProvider).noteError(instanceOne);
}

@Test
public void testStart() throws Exception {
client.start();
verify(serviceProvider).start();
verify(serviceCache).start();
}

@Test
public void testClose() throws Exception {
client.close();
verify(serviceCache).close();
verify(serviceProvider).close();
}

@Test
public void testCloseWithExceptionInCacheClose() throws Exception {
doThrow(IOException.class).when(serviceCache).close();
client.close();
verify(serviceCache).close();
verify(serviceProvider).close();
}

@Test
public void testCloseWithExceptionInProviderClose() throws Exception {
doThrow(IOException.class).when(serviceProvider).close();
client.close();
verify(serviceCache).close();
verify(serviceProvider).close();
}
}

0 comments on commit b807881

Please sign in to comment.