Added test for NPE in Zuul

This commit is contained in:
Marcin Grzejszczak
2016-01-15 11:51:23 +01:00
parent d37a6a0f69
commit 50a2b91fc5
2 changed files with 34 additions and 8 deletions

View File

@@ -16,23 +16,25 @@
package org.springframework.cloud.zookeeper.discovery;
import static org.springframework.util.ReflectionUtils.rethrowRuntimeException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.x.discovery.ServiceInstance;
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.zookeeper.discovery.dependency.ZookeeperDependencies;
import lombok.SneakyThrows;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import static org.springframework.util.ReflectionUtils.rethrowRuntimeException;
/**
* @author Spencer Gibb
* @author Marcin Grzejszczak, 4financeIT
*/
@Slf4j
public class ZookeeperDiscoveryClient implements DiscoveryClient {
private ZookeeperServiceDiscovery serviceDiscovery;
@@ -93,7 +95,11 @@ public class ZookeeperDiscoveryClient implements DiscoveryClient {
@Override
public List<String> getServices() {
ArrayList<String> services = null;
List<String> services = null;
if (this.serviceDiscovery.getServiceDiscovery() == null) {
log.warn("Service Discovery is not yet ready - returning empty list of services");
return Collections.emptyList();
}
try {
services = new ArrayList<>(this.serviceDiscovery.getServiceDiscovery().queryForNames());
}

View File

@@ -0,0 +1,20 @@
package org.springframework.cloud.zookeeper.discovery
import spock.lang.Specification
/**
* @author Marcin Grzejszczak
*/
class ZookeeperDiscoveryClientSpec extends Specification {
def "should return an empty list of services if service discovery is null"() {
given:
ZookeeperServiceDiscovery serviceDiscovery = Stub()
ZookeeperDiscoveryClient zookeeperDiscoveryClient = new ZookeeperDiscoveryClient(serviceDiscovery, null)
when:
List<String> services = zookeeperDiscoveryClient.getServices()
then:
assert services.empty
}
}