Skip to content

Commit

Permalink
Merge pull request #370 from mwl/refactor/controller-tests
Browse files Browse the repository at this point in the history
@philwinder told me to merge
  • Loading branch information
mwl committed Oct 22, 2015
2 parents e0f2e30 + 80b69a9 commit 2cdff8a
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 24 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ subprojects {

compile "org.apache.mesos:mesos:${mesosVer}"
compile 'com.google.code.gson:gson:2.3' // marshalling between the scheduler and executor
compile 'com.google.code.findbugs:annotations:3.0.0'

testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-all:1.9.5"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public ClusterInfoResponse clusterInfo() {
}

private Map<String, Object> toMap(Configuration configuration) {
return Arrays.stream(configuration.getClass().getDeclaredMethods()).filter(this::isGetter).collect(Collectors.toMap(method -> method.getName().substring(3), this::invokeConfigurationGetter));
return Arrays.stream(configuration.getClass().getMethods()).filter(this::isGetter).collect(Collectors.toMap(method -> method.getName().substring(3), this::invokeConfigurationGetter));
}

private boolean isGetter(Method method) {
Expand All @@ -52,8 +52,6 @@ private Object invokeConfigurationGetter(Method method) {
return "************";
} else if (result instanceof Number || result instanceof Boolean || result instanceof String) {
return result;
} else if (result instanceof Protos.FrameworkID) {
return ((Protos.FrameworkID) result).getValue();
}
return result.toString();
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

/**
Expand All @@ -26,10 +27,11 @@ public void shouldNotExceptionWhenGeneratingConfiguration() {

@Test
public void shouldNotHaveErrorInText() {
clusterController.clusterInfo().configuration.values().forEach(v -> {
assertFalse(v.toString().contains("ERROR"));
});
clusterController.clusterInfo().configuration.values().forEach(v -> assertFalse(v.toString().contains("ERROR")));
}


@Test
public void willNotExposePasswordFieldsFromConfigurationInClearText() throws Exception {
assertEquals("************", clusterController.clusterInfo().configuration.get("FakePassword"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.apache.mesos.elasticsearch.scheduler.controllers;

import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.entity.InputStreamEntity;
import org.apache.mesos.elasticsearch.scheduler.ElasticsearchScheduler;
import org.apache.mesos.elasticsearch.scheduler.Task;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.ResponseEntity;

import java.io.ByteArrayInputStream;
import java.net.InetSocketAddress;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
* Test the search proxy, Sherlock
*/
@RunWith(MockitoJUnitRunner.class)
public class SearchProxyControllerTest {

@SuppressWarnings("PMD.AvoidUsingHardCodedIP")
public static final String HOSTNAME = "1.0.0.1";
@Mock
HttpClient httpClient;

@Mock
ElasticsearchScheduler elasticsearchScheduler;

@Mock
HttpResponse httpResponse;

@InjectMocks
SearchProxyController controller;

private Map<String, Task> createTasksMap(int nodes) {
return IntStream.rangeClosed(1, nodes)
.mapToObj(value -> new Task(null, "task-" + value, null, null, new InetSocketAddress(HOSTNAME, 1000 + value), null))
.collect(Collectors.toMap(Task::getTaskId, task -> task));
}

@Test
public void willForwardSearchRequestToARandomNode() throws Exception {
when(elasticsearchScheduler.getTasks()).thenReturn(createTasksMap(3));
when(httpClient.execute(any(HttpHost.class), any(HttpRequest.class))).thenReturn(httpResponse);
when(httpResponse.getEntity()).thenReturn(new InputStreamEntity(new ByteArrayInputStream("Search result".getBytes("UTF-8"))));

final ResponseEntity<InputStreamResource> search = controller.search("test", null);
assertEquals(200, search.getStatusCode().value());

final ArgumentCaptor<HttpHost> httpHostArgumentCaptor = ArgumentCaptor.forClass(HttpHost.class);
final ArgumentCaptor<HttpRequest> httpRequestArgumentCaptor = ArgumentCaptor.forClass(HttpRequest.class);
verify(httpClient).execute(httpHostArgumentCaptor.capture(), httpRequestArgumentCaptor.capture());
assertEquals(HOSTNAME, httpHostArgumentCaptor.getValue().getHostName());
assertEquals("/_search?q=test", httpRequestArgumentCaptor.getValue().getRequestLine().getUri());
}

@Test
public void willForwardSearchRequestToAChosenNode() throws Exception {
final String chosenNode = "1.0.0.1:1002";

when(elasticsearchScheduler.getTasks()).thenReturn(createTasksMap(3));
when(httpClient.execute(any(HttpHost.class), any(HttpRequest.class))).thenReturn(httpResponse);
when(httpResponse.getEntity()).thenReturn(new InputStreamEntity(new ByteArrayInputStream("Search result".getBytes("UTF-8"))));

final ResponseEntity<InputStreamResource> search = controller.search("test", chosenNode);
assertEquals(200, search.getStatusCode().value());

assertEquals(chosenNode, search.getHeaders().getFirst("X-ElasticSearch-host"));
final ArgumentCaptor<HttpHost> httpHostArgumentCaptor = ArgumentCaptor.forClass(HttpHost.class);
final ArgumentCaptor<HttpRequest> httpRequestArgumentCaptor = ArgumentCaptor.forClass(HttpRequest.class);
verify(httpClient).execute(httpHostArgumentCaptor.capture(), httpRequestArgumentCaptor.capture());
assertEquals(HOSTNAME, httpHostArgumentCaptor.getValue().getHostName());
assertEquals("/_search?q=test", httpRequestArgumentCaptor.getValue().getRequestLine().getUri());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.apache.mesos.elasticsearch.scheduler.controllers;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.apache.http.client.HttpClient;
import org.apache.mesos.elasticsearch.common.cli.ZookeeperCLIParameter;
import org.apache.mesos.elasticsearch.scheduler.ElasticsearchScheduler;
Expand Down Expand Up @@ -29,7 +30,12 @@ public ElasticsearchScheduler getMockScheduler() {

@Bean
public org.apache.mesos.elasticsearch.scheduler.Configuration getConfig() {
return new org.apache.mesos.elasticsearch.scheduler.Configuration(ZookeeperCLIParameter.ZOOKEEPER_MESOS_URL, "zk://dummy.mesos.master:2181");
return new org.apache.mesos.elasticsearch.scheduler.Configuration(ZookeeperCLIParameter.ZOOKEEPER_MESOS_URL, "zk://dummy.mesos.master:2181") {
@SuppressFBWarnings("UMAC_UNCALLABLE_METHOD_OF_ANONYMOUS_CLASS")
public String getFakePassword() {
return "Sh0uld n0t be v1s1bl3";
}
};
}

@Bean
Expand Down
16 changes: 0 additions & 16 deletions scheduler/src/test/resources/log4j.xml

This file was deleted.

0 comments on commit 2cdff8a

Please sign in to comment.