Skip to content

Commit

Permalink
Added the test for HostHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
diptanu committed Oct 29, 2014
1 parent ead9f1f commit bc00434
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/netflix/prana/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.netflix.karyon.KaryonServer;
import com.netflix.prana.config.PranaConfig;
import com.netflix.prana.http.api.HandlersModule;
import com.netflix.prana.service.ServiceModule;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;

Expand All @@ -26,7 +27,7 @@ public static void main(String args[]) {
try {
MainModule sm = new MainModule(pranaConfig);
@SuppressWarnings("deprecation")
Injector injector = LifecycleInjector.builder().withModules(sm, new HandlersModule()).createInjector();
Injector injector = LifecycleInjector.builder().withModules(sm, new ServiceModule(), new HandlersModule()).createInjector();
LifecycleManager manager = injector.getInstance(LifecycleManager.class);
manager.start();
KaryonServer karyonServer = injector.getInstance(KaryonServer.class);
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/netflix/prana/http/api/HostsHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.google.inject.Inject;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.DiscoveryClient;
import com.netflix.prana.service.HostService;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.reactivex.netty.protocol.http.server.HttpServerRequest;
Expand All @@ -26,11 +27,11 @@ public class HostsHandler implements RequestHandler<ByteBuf, ByteBuf> {

private final ObjectMapper objectMapper;

private DiscoveryClient discoveryClient;
private HostService hostService;

@Inject
public HostsHandler(DiscoveryClient discoveryClient) {
this.discoveryClient = discoveryClient;
public HostsHandler(HostService hostService) {
this.hostService = hostService;
this.objectMapper = new ObjectMapper();
}

Expand All @@ -44,7 +45,7 @@ public Observable<Void> handle(HttpServerRequest<ByteBuf> serverRequest, HttpSer
serverResponse.writeString("appName has to be specified");
return serverResponse.close();
}
List<InstanceInfo> instances = discoveryClient.getApplication(appName).getInstances();
List<InstanceInfo> instances = hostService.getHosts(appName);
List<String> hosts = new ArrayList<>();
for (InstanceInfo instanceInfo : instances) {
if (vip != null && !instanceInfo.getVIPAddress().contains(vip) && instanceInfo.getStatus().equals(InstanceStatus.UP)) {
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/netflix/prana/service/EurekaHostService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.netflix.prana.service;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.DiscoveryClient;

import java.util.List;

public class EurekaHostService implements HostService {

private DiscoveryClient discoveryClient;

public EurekaHostService(DiscoveryClient discoveryClient) {
this.discoveryClient = discoveryClient;
}
@Override
public List<InstanceInfo> getHosts(String appName) {
return discoveryClient.getApplication(appName).getInstances();
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/netflix/prana/service/HostService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.netflix.prana.service;

import com.google.inject.Inject;
import com.netflix.appinfo.InstanceInfo;

import java.util.List;

public interface HostService {

public List<InstanceInfo> getHosts(String appName);
}
11 changes: 11 additions & 0 deletions src/main/java/com/netflix/prana/service/ServiceModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.netflix.prana.service;

import com.google.inject.AbstractModule;
import com.google.inject.Scopes;

public class ServiceModule extends AbstractModule {
@Override
protected void configure() {
bind(HostService.class).to(EurekaHostService.class).in(Scopes.SINGLETON);
}
}
62 changes: 62 additions & 0 deletions src/test/java/com/netflix/prana/http/api/HostsHandlerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.netflix.prana.http.api;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.DiscoveryClient;
import com.netflix.discovery.shared.Application;
import com.netflix.prana.service.HostService;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelOption;
import io.reactivex.netty.RxNetty;
import io.reactivex.netty.pipeline.PipelineConfigurators;
import io.reactivex.netty.protocol.http.client.HttpClient;
import io.reactivex.netty.protocol.http.client.HttpClientRequest;
import io.reactivex.netty.protocol.http.server.HttpServer;
import org.junit.*;

import java.util.ArrayList;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* Created by dchoudhury on 10/28/14.
*/
public class HostsHandlerTest {

private HttpServer<ByteBuf, ByteBuf> server;

private HttpClient<ByteBuf, ByteBuf> client;

private final int port = 23455;

private HostService hostService;

@Before
public void setUp() {
hostService = mock(HostService.class);
ArrayList<InstanceInfo> instanceInfos = new ArrayList<>();
instanceInfos.add(InstanceInfo.Builder.newBuilder().setAppName("foo").setVIPAddress("bar").setHostName("host1").build());
instanceInfos.add(InstanceInfo.Builder.newBuilder().setAppName("foo").setVIPAddress("bar").setHostName("host2").build());
when(hostService.getHosts("foo")).thenReturn(instanceInfos);
server = RxNetty.newHttpServerBuilder(port, new HostsHandler(hostService))
.pipelineConfigurator(PipelineConfigurators.<ByteBuf, ByteBuf>httpServerConfigurator()).build();
server.start();
client = RxNetty.<ByteBuf, ByteBuf>newHttpClientBuilder("localhost", port)
.pipelineConfigurator(PipelineConfigurators.<ByteBuf, ByteBuf>httpClientConfigurator())
.channelOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 2000)
.build();
}

@After
public void tearDown() throws InterruptedException {
server.shutdown();
}

@Test
public void shouldReturnAListOfHostsWhenBothVipAndAppIsSpecified() {
HttpClientRequest<ByteBuf> request = HttpClientRequest.<ByteBuf>createGet("/hosts?appName=foo&vip=bar");
String response = Utils.getResponse(request, client);
Assert.assertEquals("[\"host1\",\"host2\"]", response);
}

}

0 comments on commit bc00434

Please sign in to comment.