import org.elasticsearch.action.admin.indices.cache.clear.ClearIndicesCacheRequestBuilder;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequestBuilder;
-import org.elasticsearch.action.admin.indices.flush.FlushRequestBuilder;
import org.elasticsearch.action.admin.indices.forcemerge.ForceMergeRequestBuilder;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequestBuilder;
import org.elasticsearch.action.admin.indices.refresh.RefreshRequestBuilder;
import org.elasticsearch.action.admin.indices.stats.IndicesStatsRequestBuilder;
-import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.get.GetRequestBuilder;
-import org.elasticsearch.action.get.MultiGetRequestBuilder;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchScrollRequestBuilder;
import org.elasticsearch.client.Client;
import org.elasticsearch.cluster.health.ClusterHealthStatus;
import org.elasticsearch.common.Priority;
-import org.elasticsearch.index.query.QueryBuilders;
-import org.elasticsearch.search.aggregations.AggregationBuilders;
-import org.elasticsearch.search.aggregations.metrics.max.Max;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
-import org.sonar.server.es.request.ProxyBulkRequestBuilder;
import org.sonar.server.es.request.ProxyClearCacheRequestBuilder;
import org.sonar.server.es.request.ProxyClusterHealthRequestBuilder;
import org.sonar.server.es.request.ProxyClusterStateRequestBuilder;
import org.sonar.server.es.request.ProxyClusterStatsRequestBuilder;
import org.sonar.server.es.request.ProxyCreateIndexRequestBuilder;
import org.sonar.server.es.request.ProxyDeleteRequestBuilder;
-import org.sonar.server.es.request.ProxyFlushRequestBuilder;
import org.sonar.server.es.request.ProxyGetRequestBuilder;
import org.sonar.server.es.request.ProxyIndexRequestBuilder;
import org.sonar.server.es.request.ProxyIndicesExistsRequestBuilder;
import org.sonar.server.es.request.ProxyIndicesStatsRequestBuilder;
-import org.sonar.server.es.request.ProxyMultiGetRequestBuilder;
import org.sonar.server.es.request.ProxyNodesStatsRequestBuilder;
import org.sonar.server.es.request.ProxyPutMappingRequestBuilder;
import org.sonar.server.es.request.ProxyRefreshRequestBuilder;
return new ProxyRefreshRequestBuilder(nativeClient()).setIndices(indices);
}
- public FlushRequestBuilder prepareFlush(String... indices) {
- return new ProxyFlushRequestBuilder(nativeClient()).setIndices(indices);
- }
-
public IndicesStatsRequestBuilder prepareStats(String... indices) {
return new ProxyIndicesStatsRequestBuilder(nativeClient()).setIndices(indices);
}
return new ProxySearchScrollRequestBuilder(scrollId, nativeClient());
}
- public GetRequestBuilder prepareGet() {
- return new ProxyGetRequestBuilder(nativeClient());
- }
-
public GetRequestBuilder prepareGet(IndexType indexType, String id) {
return new ProxyGetRequestBuilder(nativeClient()).setIndex(indexType.getIndex()).setType(indexType.getType()).setId(id);
}
- public MultiGetRequestBuilder prepareMultiGet() {
- return new ProxyMultiGetRequestBuilder(nativeClient());
- }
-
- public BulkRequestBuilder prepareBulk() {
- return new ProxyBulkRequestBuilder(nativeClient());
- }
-
public DeleteRequestBuilder prepareDelete(IndexType indexType, String id) {
return new ProxyDeleteRequestBuilder(nativeClient(), indexType.getIndex()).setType(indexType.getType()).setId(id);
}
return new ProxyClearCacheRequestBuilder(nativeClient()).setIndices(indices);
}
- public long getMaxFieldValue(IndexType indexType, String fieldName) {
- SearchRequestBuilder request = prepareSearch(indexType)
- .setQuery(QueryBuilders.matchAllQuery())
- .setSize(0)
- .addAggregation(AggregationBuilders.max("latest").field(fieldName));
-
- Max max = request.get().getAggregations().get("latest");
- return (long) max.getValue();
- }
-
public Client nativeClient() {
return nativeClient;
}
- /**
- * Checks whether there is any document in any mentioned type.
- */
- public boolean isEmpty(IndexType indexType) {
- return count(indexType) <= 0;
- }
-
- private long count(IndexType indexType) {
- return prepareSearch(indexType).setSize(0).get().getHits().getTotalHits();
- }
-
@Override
public void close() {
nativeClient.close();
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2018 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.server.es.request;
-
-import com.google.common.collect.LinkedHashMultiset;
-import com.google.common.collect.Multiset;
-import java.util.Set;
-import org.elasticsearch.action.DocWriteRequest;
-import org.elasticsearch.action.ListenableActionFuture;
-import org.elasticsearch.action.bulk.BulkAction;
-import org.elasticsearch.action.bulk.BulkRequestBuilder;
-import org.elasticsearch.action.bulk.BulkResponse;
-import org.elasticsearch.action.delete.DeleteRequest;
-import org.elasticsearch.action.index.IndexRequest;
-import org.elasticsearch.action.update.UpdateRequest;
-import org.elasticsearch.client.Client;
-import org.elasticsearch.common.unit.TimeValue;
-import org.sonar.api.utils.log.Profiler;
-import org.sonar.server.es.EsClient;
-
-public class ProxyBulkRequestBuilder extends BulkRequestBuilder {
-
- public ProxyBulkRequestBuilder(Client client) {
- super(client, BulkAction.INSTANCE);
- }
-
- @Override
- public BulkResponse get() {
- Profiler profiler = Profiler.createIfTrace(EsClient.LOGGER).start();
- try {
- return super.execute().actionGet();
- } catch (Exception e) {
- throw new IllegalStateException(String.format("Fail to execute %s", toString()), e);
- } finally {
- if (profiler.isTraceEnabled()) {
- profiler.stopTrace(toString());
- }
- }
- }
-
- @Override
- public BulkResponse get(TimeValue timeout) {
- throw unsupported();
- }
-
- @Override
- public BulkResponse get(String timeout) {
- // easy to implement if needed (copy get())
- throw unsupported();
- }
-
- @Override
- public ListenableActionFuture<BulkResponse> execute() {
- throw unsupported();
- }
-
- private static UnsupportedOperationException unsupported() {
- throw new UnsupportedOperationException("See " + ProxyBulkRequestBuilder.class.getName());
- }
-
- @Override
- public String toString() {
- StringBuilder message = new StringBuilder();
- message.append("Bulk[");
- Multiset<BulkRequestKey> groupedRequests = LinkedHashMultiset.create();
- for (int i = 0; i < request.requests().size(); i++) {
- DocWriteRequest item = request.requests().get(i);
- String requestType;
- String index;
- String docType;
- if (item instanceof IndexRequest) {
- IndexRequest request = (IndexRequest) item;
- requestType = "index";
- index = request.index();
- docType = request.type();
- } else if (item instanceof UpdateRequest) {
- UpdateRequest request = (UpdateRequest) item;
- requestType = "update";
- index = request.index();
- docType = request.type();
- } else if (item instanceof DeleteRequest) {
- DeleteRequest request = (DeleteRequest) item;
- requestType = "delete";
- index = request.index();
- docType = request.type();
- } else {
- // Cannot happen, not allowed by BulkRequest's contract
- throw new IllegalStateException("Unsupported bulk request type: " + item.getClass());
- }
- groupedRequests.add(new BulkRequestKey(requestType, index, docType));
- }
-
- Set<Multiset.Entry<BulkRequestKey>> entrySet = groupedRequests.entrySet();
- int size = entrySet.size();
- int current = 0;
- for (Multiset.Entry<BulkRequestKey> requestEntry : entrySet) {
- message.append(requestEntry.getCount()).append(" ").append(requestEntry.getElement().toString());
- current++;
- if (current < size) {
- message.append(", ");
- }
- }
-
- message.append("]");
- return message.toString();
- }
-
- private static class BulkRequestKey {
- private String requestType;
- private String index;
- private String docType;
-
- private BulkRequestKey(String requestType, String index, String docType) {
- this.requestType = requestType;
- this.index = index;
- this.docType = docType;
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) {
- return true;
- }
- if (o == null || getClass() != o.getClass()) {
- return false;
- }
- BulkRequestKey that = (BulkRequestKey) o;
- if (!docType.equals(that.docType)) {
- return false;
- }
- if (!index.equals(that.index)) {
- return false;
- }
- return requestType.equals(that.requestType);
- }
-
- @Override
- public int hashCode() {
- int result = requestType.hashCode();
- result = 31 * result + index.hashCode();
- result = 31 * result + docType.hashCode();
- return result;
- }
-
- @Override
- public String toString() {
- return String.format("%s request(s) on index %s and type %s", requestType, index, docType);
- }
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2018 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.server.es.request;
-
-import org.apache.commons.lang.StringUtils;
-import org.elasticsearch.action.ListenableActionFuture;
-import org.elasticsearch.action.admin.indices.flush.FlushAction;
-import org.elasticsearch.action.admin.indices.flush.FlushRequestBuilder;
-import org.elasticsearch.action.admin.indices.flush.FlushResponse;
-import org.elasticsearch.client.Client;
-import org.elasticsearch.common.unit.TimeValue;
-import org.sonar.api.utils.log.Profiler;
-import org.sonar.server.es.EsClient;
-
-public class ProxyFlushRequestBuilder extends FlushRequestBuilder {
-
- public ProxyFlushRequestBuilder(Client client) {
- super(client.admin().indices(), FlushAction.INSTANCE);
- }
-
- @Override
- public FlushResponse get() {
- Profiler profiler = Profiler.createIfTrace(EsClient.LOGGER).start();
- try {
- return super.execute().actionGet();
- } catch (Exception e) {
- throw new IllegalStateException(String.format("Fail to execute %s", toString()), e);
- } finally {
- if (profiler.isTraceEnabled()) {
- profiler.stopTrace(toString());
- }
- }
- }
-
- @Override
- public FlushResponse get(TimeValue timeout) {
- throw new IllegalStateException("Not yet implemented");
- }
-
- @Override
- public FlushResponse get(String timeout) {
- throw new IllegalStateException("Not yet implemented");
- }
-
- @Override
- public ListenableActionFuture<FlushResponse> execute() {
- throw new UnsupportedOperationException("execute() should not be called as it's used for asynchronous");
- }
-
- @Override
- public String toString() {
- StringBuilder message = new StringBuilder();
- message.append("ES flush request");
- if (request.indices().length > 0) {
- message.append(String.format(" on indices '%s'", StringUtils.join(request.indices(), ",")));
- }
- return message.toString();
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2018 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.server.es.request;
-
-import org.elasticsearch.action.ListenableActionFuture;
-import org.elasticsearch.action.get.MultiGetAction;
-import org.elasticsearch.action.get.MultiGetRequest;
-import org.elasticsearch.action.get.MultiGetRequestBuilder;
-import org.elasticsearch.action.get.MultiGetResponse;
-import org.elasticsearch.client.Client;
-import org.elasticsearch.common.unit.TimeValue;
-import org.sonar.api.utils.log.Profiler;
-import org.sonar.server.es.EsClient;
-
-public class ProxyMultiGetRequestBuilder extends MultiGetRequestBuilder {
-
- public ProxyMultiGetRequestBuilder(Client client) {
- super(client, MultiGetAction.INSTANCE);
- }
-
- @Override
- public MultiGetResponse get() {
- Profiler profiler = Profiler.createIfTrace(EsClient.LOGGER).start();
- try {
- return super.execute().actionGet();
- } catch (Exception e) {
- throw new IllegalStateException(String.format("Fail to execute %s", toString()), e);
- } finally {
- if (profiler.isTraceEnabled()) {
- profiler.stopTrace(toString());
- }
- }
- }
-
- @Override
- public MultiGetResponse get(TimeValue timeout) {
- throw new IllegalStateException("Not yet implemented");
- }
-
- @Override
- public MultiGetResponse get(String timeout) {
- throw new IllegalStateException("Not yet implemented");
- }
-
- @Override
- public ListenableActionFuture<MultiGetResponse> execute() {
- throw new UnsupportedOperationException("execute() should not be called as it's used for asynchronous");
- }
-
- @Override
- public String toString() {
- StringBuilder message = new StringBuilder();
- message.append("ES multi get request");
- for (MultiGetRequest.Item item : request) {
- message.append(String.format(" [key '%s'", item.id()));
- message.append(String.format(", index '%s'", item.index()));
- String type = item.type();
- if (type != null) {
- message.append(String.format(", type '%s'", type));
- }
- message.append("],");
- }
- return message.toString();
- }
-}
import org.junit.Rule;
import org.junit.Test;
-import org.sonar.server.es.request.ProxyBulkRequestBuilder;
import org.sonar.server.es.request.ProxyClusterHealthRequestBuilder;
import org.sonar.server.es.request.ProxyClusterStateRequestBuilder;
import org.sonar.server.es.request.ProxyClusterStatsRequestBuilder;
import org.sonar.server.es.request.ProxyCreateIndexRequestBuilder;
import org.sonar.server.es.request.ProxyDeleteRequestBuilder;
-import org.sonar.server.es.request.ProxyFlushRequestBuilder;
import org.sonar.server.es.request.ProxyGetRequestBuilder;
import org.sonar.server.es.request.ProxyIndicesExistsRequestBuilder;
import org.sonar.server.es.request.ProxyIndicesStatsRequestBuilder;
-import org.sonar.server.es.request.ProxyMultiGetRequestBuilder;
import org.sonar.server.es.request.ProxyNodesStatsRequestBuilder;
import org.sonar.server.es.request.ProxyPutMappingRequestBuilder;
import org.sonar.server.es.request.ProxyRefreshRequestBuilder;
import org.sonar.server.es.request.ProxySearchScrollRequestBuilder;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.sonar.server.es.FakeIndexDefinition.INDEX_TYPE_FAKE;
public class EsClientTest {
public void proxify_requests() {
EsClient underTest = es.client();
assertThat(underTest.nativeClient()).isNotNull();
- assertThat(underTest.prepareBulk()).isInstanceOf(ProxyBulkRequestBuilder.class);
assertThat(underTest.prepareClusterStats()).isInstanceOf(ProxyClusterStatsRequestBuilder.class);
assertThat(underTest.prepareCreate("fakes")).isInstanceOf(ProxyCreateIndexRequestBuilder.class);
assertThat(underTest.prepareDelete("fakes", "fake", "my_id")).isInstanceOf(ProxyDeleteRequestBuilder.class);
assertThat(underTest.prepareIndicesExist()).isInstanceOf(ProxyIndicesExistsRequestBuilder.class);
- assertThat(underTest.prepareFlush()).isInstanceOf(ProxyFlushRequestBuilder.class);
- assertThat(underTest.prepareGet()).isInstanceOf(ProxyGetRequestBuilder.class);
assertThat(underTest.prepareGet(new IndexType("fakes", "fake"), "1")).isInstanceOf(ProxyGetRequestBuilder.class);
assertThat(underTest.prepareHealth()).isInstanceOf(ProxyClusterHealthRequestBuilder.class);
- assertThat(underTest.prepareMultiGet()).isInstanceOf(ProxyMultiGetRequestBuilder.class);
assertThat(underTest.prepareNodesStats()).isInstanceOf(ProxyNodesStatsRequestBuilder.class);
assertThat(underTest.preparePutMapping()).isInstanceOf(ProxyPutMappingRequestBuilder.class);
assertThat(underTest.prepareRefresh()).isInstanceOf(ProxyRefreshRequestBuilder.class);
underTest.close();
}
-
- @Test
- public void isEmpty_should_return_true_if_index_is_empty() {
- assertThat(es.client().isEmpty(INDEX_TYPE_FAKE)).isTrue();
- }
-
- @Test
- public void isEmpty_should_return_false_if_index_is_not_empty() {
- es.putDocuments(INDEX_TYPE_FAKE, new FakeDoc());
- assertThat(es.client().isEmpty(INDEX_TYPE_FAKE)).isFalse();
- }
}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2018 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.server.es.request;
-
-import org.elasticsearch.action.bulk.BulkRequestBuilder;
-import org.elasticsearch.action.bulk.BulkResponse;
-import org.elasticsearch.action.delete.DeleteRequest;
-import org.elasticsearch.action.index.IndexRequest;
-import org.elasticsearch.action.update.UpdateRequest;
-import org.elasticsearch.common.unit.TimeValue;
-import org.junit.Rule;
-import org.junit.Test;
-import org.sonar.api.utils.log.LogTester;
-import org.sonar.api.utils.log.LoggerLevel;
-import org.sonar.server.es.EsTester;
-import org.sonar.server.es.FakeIndexDefinition;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class ProxyBulkRequestBuilderTest {
-
- @Rule
- public EsTester es = EsTester.createCustom(new FakeIndexDefinition());
-
- @Rule
- public LogTester logTester = new LogTester();
-
- @Test
- public void no_trace_logs() {
- logTester.setLevel(LoggerLevel.INFO);
- testBulk();
- assertThat(logTester.logs(LoggerLevel.TRACE)).isEmpty();
- }
-
- @Test
- public void trace_logs() {
- logTester.setLevel(LoggerLevel.TRACE);
- testBulk();
- assertThat(logTester.logs(LoggerLevel.TRACE)).hasSize(1);
- }
-
- private void testBulk() {
- BulkRequestBuilder req = es.client().prepareBulk();
- req.add(new UpdateRequest(FakeIndexDefinition.INDEX, FakeIndexDefinition.INDEX_TYPE_FAKE.getType(), "key1")
- .doc(FakeIndexDefinition.newDoc(1).getFields()));
- req.add(new DeleteRequest(FakeIndexDefinition.INDEX, FakeIndexDefinition.INDEX_TYPE_FAKE.getType(), "key2"));
- req.add(new IndexRequest(FakeIndexDefinition.INDEX, FakeIndexDefinition.INDEX_TYPE_FAKE.getType(), "key3")
- .source(FakeIndexDefinition.newDoc(3).getFields()));
-
- assertThat(req.toString()).isEqualTo(
- "Bulk[1 update request(s) on index fakes and type fake, 1 delete request(s) on index fakes and type fake, 1 index request(s) on index fakes and type fake]");
-
- BulkResponse response = req.get();
- assertThat(response.getItems()).hasSize(3);
- }
-
- @Test(expected = UnsupportedOperationException.class)
- public void get_with_string_timeout_is_not_yet_implemented() {
- es.client().prepareBulk().get("1");
- }
-
- @Test(expected = UnsupportedOperationException.class)
- public void get_with_time_value_timeout_is_not_yet_implemented() {
- es.client().prepareBulk().get(TimeValue.timeValueMinutes(1));
- }
-
- @Test(expected = UnsupportedOperationException.class)
- public void execute_is_not_yet_implemented() {
- es.client().prepareBulk().execute();
- }
-
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2018 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.server.es.request;
-
-import org.elasticsearch.common.unit.TimeValue;
-import org.junit.Rule;
-import org.junit.Test;
-import org.sonar.api.utils.log.LogTester;
-import org.sonar.api.utils.log.LoggerLevel;
-import org.sonar.server.es.EsTester;
-import org.sonar.server.es.FakeIndexDefinition;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.Assert.fail;
-
-public class ProxyFlushRequestBuilderTest {
-
- @Rule
- public EsTester es = EsTester.createCustom(new FakeIndexDefinition());
-
- @Rule
- public LogTester logTester = new LogTester();
-
- @Test
- public void flush() {
- es.client().prepareFlush(FakeIndexDefinition.INDEX).get();
- }
-
- @Test
- public void to_string() {
- assertThat(es.client().prepareFlush(FakeIndexDefinition.INDEX).toString()).isEqualTo("ES flush request on indices 'fakes'");
- assertThat(es.client().prepareFlush().toString()).isEqualTo("ES flush request");
- }
-
- @Test
- public void trace_logs() {
- logTester.setLevel(LoggerLevel.TRACE);
-
- es.client().prepareFlush(FakeIndexDefinition.INDEX).get();
- assertThat(logTester.logs(LoggerLevel.TRACE)).hasSize(1);
- }
-
- @Test
- public void fail_to_refresh() {
- try {
- es.client().prepareFlush("unknown").get();
- fail();
- } catch (Exception e) {
- assertThat(e).isInstanceOf(IllegalStateException.class);
- assertThat(e.getMessage()).contains("Fail to execute ES flush request on indices 'unknown'");
- }
- }
-
- @Test
- public void get_with_string_timeout_is_not_yet_implemented() {
- try {
- es.client().prepareFlush(FakeIndexDefinition.INDEX).get("1");
- fail();
- } catch (Exception e) {
- assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented");
- }
- }
-
- @Test
- public void get_with_time_value_timeout_is_not_yet_implemented() {
- try {
- es.client().prepareFlush(FakeIndexDefinition.INDEX).get(TimeValue.timeValueMinutes(1));
- fail();
- } catch (Exception e) {
- assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented");
- }
- }
-
- @Test
- public void execute_should_throw_an_unsupported_operation_exception() {
- try {
- es.client().prepareFlush(FakeIndexDefinition.INDEX).execute();
- fail();
- } catch (Exception e) {
- assertThat(e).isInstanceOf(UnsupportedOperationException.class).hasMessage("execute() should not be called as it's used for asynchronous");
- }
- }
-
-}
import org.sonar.api.utils.log.LoggerLevel;
import org.sonar.server.es.EsTester;
import org.sonar.server.es.FakeIndexDefinition;
+import org.sonar.server.es.IndexType;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
+import static org.sonar.server.es.FakeIndexDefinition.INDEX_TYPE_FAKE;
public class ProxyGetRequestBuilderTest {
@Rule
public LogTester logTester = new LogTester();
- @Test
- public void get() {
- es.client().prepareGet()
- .setIndex(FakeIndexDefinition.INDEX)
- .setType(FakeIndexDefinition.TYPE)
- .setId("ruleKey")
- .get();
- }
-
@Test
public void trace_logs() {
logTester.setLevel(LoggerLevel.TRACE);
- es.client().prepareGet()
- .setIndex(FakeIndexDefinition.INDEX)
- .setType(FakeIndexDefinition.TYPE)
- .setId("ruleKey")
+ es.client().prepareGet(INDEX_TYPE_FAKE, "ruleKey")
.get();
assertThat(logTester.logs(LoggerLevel.TRACE)).hasSize(1);
}
@Test
public void fail_to_get_bad_query() {
- GetRequestBuilder requestBuilder = es.client().prepareGet()
- .setIndex("unknown")
- .setType("test")
- .setId("rule1");
+ GetRequestBuilder requestBuilder = es.client().prepareGet(new IndexType("unknown", "test"), "rule1");
try {
requestBuilder.get();
fail();
}
@Test
- public void get_with_string_timeout_is_not_yet_implemented() {
+ public void get_with_string_timeout_is_not_implemented() {
try {
- es.client().prepareGet().get("1");
+ es.client().prepareGet(INDEX_TYPE_FAKE, "ruleKey").get("1");
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented");
@Test
public void get_with_time_value_timeout_is_not_yet_implemented() {
try {
- es.client().prepareGet().get(TimeValue.timeValueMinutes(1));
+ es.client().prepareGet(INDEX_TYPE_FAKE, "ruleKey").get(TimeValue.timeValueMinutes(1));
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented");
@Test
public void execute_should_throw_an_unsupported_operation_exception() {
try {
- es.client().prepareGet().execute();
+ es.client().prepareGet(INDEX_TYPE_FAKE, "ruleKey").execute();
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(UnsupportedOperationException.class).hasMessage("execute() should not be called as it's used for asynchronous");
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2018 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.server.es.request;
-
-import org.elasticsearch.action.get.MultiGetRequest;
-import org.elasticsearch.action.get.MultiGetRequestBuilder;
-import org.elasticsearch.common.unit.TimeValue;
-import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
-import org.junit.Rule;
-import org.junit.Test;
-import org.sonar.api.utils.log.LogTester;
-import org.sonar.api.utils.log.LoggerLevel;
-import org.sonar.server.es.EsTester;
-import org.sonar.server.es.FakeIndexDefinition;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.Assert.fail;
-
-public class ProxyMultiGetRequestBuilderTest {
-
- @Rule
- public EsTester es = EsTester.createCustom(new FakeIndexDefinition());
-
- @Rule
- public LogTester logTester = new LogTester();
-
- @Test
- public void multi_get() {
- MultiGetRequestBuilder request = es.client().prepareMultiGet();
- request.add(new MultiGetRequest.Item(FakeIndexDefinition.INDEX, FakeIndexDefinition.TYPE, "ruleKey")
- .fetchSourceContext(FetchSourceContext.FETCH_SOURCE));
- request.get();
-
- }
-
- @Test
- public void to_string() {
- assertThat(es.client().prepareMultiGet().toString()).isEqualTo("ES multi get request");
- assertThat(es.client().prepareMultiGet().add(new MultiGetRequest.Item(FakeIndexDefinition.INDEX, null, "fake1")
- .fetchSourceContext(FetchSourceContext.FETCH_SOURCE)).toString()).isEqualTo("ES multi get request [key 'fake1', index 'fakes'],");
- assertThat(es.client().prepareMultiGet().add(new MultiGetRequest.Item(FakeIndexDefinition.INDEX, FakeIndexDefinition.TYPE, "fake1")
- .fetchSourceContext(FetchSourceContext.FETCH_SOURCE)).toString()).isEqualTo("ES multi get request [key 'fake1', index 'fakes', type 'fake'],");
- }
-
- @Test
- public void trace_logs() {
- logTester.setLevel(LoggerLevel.TRACE);
-
- MultiGetRequestBuilder request = es.client().prepareMultiGet();
- request.add(new MultiGetRequest.Item(FakeIndexDefinition.INDEX, FakeIndexDefinition.TYPE, "ruleKey")
- .fetchSourceContext(FetchSourceContext.FETCH_SOURCE));
- request.get();
-
- assertThat(logTester.logs(LoggerLevel.TRACE)).hasSize(1);
- }
-
- @Test
- public void get_with_string_timeout_is_not_yet_implemented() {
- try {
- es.client().prepareMultiGet().get("1");
- fail();
- } catch (Exception e) {
- assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented");
- }
- }
-
- @Test
- public void get_with_time_value_timeout_is_not_yet_implemented() {
- try {
- es.client().prepareMultiGet().get(TimeValue.timeValueMinutes(1));
- fail();
- } catch (Exception e) {
- assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented");
- }
- }
-
- @Test
- public void execute_should_throw_an_unsupported_operation_exception() {
- try {
- es.client().prepareMultiGet().execute();
- fail();
- } catch (Exception e) {
- assertThat(e).isInstanceOf(UnsupportedOperationException.class).hasMessage("execute() should not be called as it's used for asynchronous");
- }
- }
-
-}