diff options
author | Jacek <jacek.poreda@sonarsource.com> | 2021-09-08 11:14:57 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2021-09-13 20:03:33 +0000 |
commit | 86f2c502104fbc03eb5aac19598e9b5c2e7290b8 (patch) | |
tree | 991eeb3f2d83d402a730ba912e0770aa9bbe0146 /server/sonar-main/src/test | |
parent | 144e0f8903ef6bbcd24eb8aba10b43fcf09138c8 (diff) | |
download | sonarqube-86f2c502104fbc03eb5aac19598e9b5c2e7290b8.tar.gz sonarqube-86f2c502104fbc03eb5aac19598e9b5c2e7290b8.zip |
SONAR-15375 Fix SSF-191
Diffstat (limited to 'server/sonar-main/src/test')
-rw-r--r-- | server/sonar-main/src/test/java/org/sonar/application/es/EsConnectorImplTest.java | 26 | ||||
-rw-r--r-- | server/sonar-main/src/test/java/org/sonar/application/process/EsManagedProcessTest.java | 27 |
2 files changed, 47 insertions, 6 deletions
diff --git a/server/sonar-main/src/test/java/org/sonar/application/es/EsConnectorImplTest.java b/server/sonar-main/src/test/java/org/sonar/application/es/EsConnectorImplTest.java index 9c5e3d0d914..a9d473d9cc8 100644 --- a/server/sonar-main/src/test/java/org/sonar/application/es/EsConnectorImplTest.java +++ b/server/sonar-main/src/test/java/org/sonar/application/es/EsConnectorImplTest.java @@ -56,6 +56,24 @@ public class EsConnectorImplTest { " \"error\" : \"i-have-a-bad-feelings-about-this\"" + "}"; + private static final String ES_INFO_RESPONSE = "{" + + " \"name\" : \"sonarqube\"," + + " \"cluster_name\" : \"sonarqube\"," + + " \"cluster_uuid\" : \"6Oj9lFIyQVa_d5HgQWqQpA\"," + + " \"version\" : {" + + " \"number\" : \"7.14.1\"," + + " \"build_flavor\" : \"default\"," + + " \"build_type\" : \"tar\"," + + " \"build_hash\" : \"66b55ebfa59c92c15db3f69a335d500018b3331e\"," + + " \"build_date\" : \"2021-08-26T09:01:05.390870785Z\"," + + " \"build_snapshot\" : false," + + " \"lucene_version\" : \"8.9.0\"," + + " \"minimum_wire_compatibility_version\" : \"6.8.0\"," + + " \"minimum_index_compatibility_version\" : \"6.0.0-beta1\"" + + " }," + + " \"tagline\" : \"You Know, for Search\"" + + "}"; + @Rule public MockWebServer mockWebServer = new MockWebServer(); @@ -96,9 +114,15 @@ public class EsConnectorImplTest { private void mockServerResponse(int httpCode, String jsonResponse) { mockWebServer.enqueue(new MockResponse() + .setResponseCode(200) + .setBody(ES_INFO_RESPONSE) + .setHeader("Content-Type", "application/json") + .setHeader("X-elastic-product", "Elasticsearch")); + mockWebServer.enqueue(new MockResponse() .setResponseCode(httpCode) .setBody(jsonResponse) - .setHeader("Content-Type", "application/json")); + .setHeader("Content-Type", "application/json") + .setHeader("X-elastic-product", "Elasticsearch")); } } diff --git a/server/sonar-main/src/test/java/org/sonar/application/process/EsManagedProcessTest.java b/server/sonar-main/src/test/java/org/sonar/application/process/EsManagedProcessTest.java index d097fa39f53..72c475118dc 100644 --- a/server/sonar-main/src/test/java/org/sonar/application/process/EsManagedProcessTest.java +++ b/server/sonar-main/src/test/java/org/sonar/application/process/EsManagedProcessTest.java @@ -26,6 +26,7 @@ import ch.qos.logback.core.AppenderBase; import java.util.ArrayList; import java.util.List; import java.util.Optional; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.cluster.health.ClusterHealthStatus; import org.elasticsearch.rest.RestStatus; @@ -104,6 +105,24 @@ public class EsManagedProcessTest { } @Test + public void isOperational_should_return_false_if_ElasticsearchException_with_connection_refused_thrown() { + EsConnector esConnector = mock(EsConnector.class); + when(esConnector.getClusterHealthStatus()) + .thenThrow(new ElasticsearchException("Connection refused")); + EsManagedProcess underTest = new EsManagedProcess(mock(Process.class), ProcessId.ELASTICSEARCH, esConnector); + assertThat(underTest.isOperational()).isFalse(); + } + + @Test + public void isOperational_should_return_false_if_ElasticsearchException_thrown() { + EsConnector esConnector = mock(EsConnector.class); + when(esConnector.getClusterHealthStatus()) + .thenThrow(new ElasticsearchException("test")); + EsManagedProcess underTest = new EsManagedProcess(mock(Process.class), ProcessId.ELASTICSEARCH, esConnector); + assertThat(underTest.isOperational()).isFalse(); + } + + @Test public void isOperational_must_log_once_when_master_is_not_elected() { MemoryAppender<ILoggingEvent> memoryAppender = new MemoryAppender<>(); LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); @@ -122,20 +141,18 @@ public class EsManagedProcessTest { assertThat(memoryAppender.events) .extracting(ILoggingEvent::getLevel, ILoggingEvent::getMessage) .containsOnlyOnce( - tuple(Level.INFO, "Elasticsearch is waiting for a master to be elected. Did you start all the search nodes ?") - ); + tuple(Level.INFO, "Elasticsearch is waiting for a master to be elected. Did you start all the search nodes ?")); // Second call must not log another message assertThat(underTest.isOperational()).isFalse(); assertThat(memoryAppender.events) .extracting(ILoggingEvent::getLevel, ILoggingEvent::getMessage) .containsOnlyOnce( - tuple(Level.INFO, "Elasticsearch is waiting for a master to be elected. Did you start all the search nodes ?") - ); + tuple(Level.INFO, "Elasticsearch is waiting for a master to be elected. Did you start all the search nodes ?")); } private static class MemoryAppender<E> extends AppenderBase<E> { - private final List<E> events = new ArrayList(); + private final List<E> events = new ArrayList<>(); @Override protected void append(E eventObject) { |