forked from GeneralElectric/snowizard-discovery
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
140 additions
and
0 deletions.
There are no files selected for viewing
140 changes: 140 additions & 0 deletions
140
src/test/java/io/dropwizard/discovery/client/DiscoveryClientTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |