diff options
author | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2013-11-07 12:06:36 +0100 |
---|---|---|
committer | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2013-11-08 18:21:45 +0100 |
commit | d32682708852e29b65f53e4ac57c88e8ff0b93ec (patch) | |
tree | df82d5764cf23109fd7c475aa9fc1f9386f22f3f /sonar-server | |
parent | 7569d68ee31cd13d69c883571b9782d3c37b9841 (diff) | |
download | sonarqube-d32682708852e29b65f53e4ac57c88e8ff0b93ec.tar.gz sonarqube-d32682708852e29b65f53e4ac57c88e8ff0b93ec.zip |
SONAR-4832 Cleanup a bit experiment leftovers
Introduce elasticsearch-test for ES integration tests
Enhance coverage on SearchIndex
Rename node in SearchNode
Unplug index at startup (will be triggered by call from RegisterRule to
RuleRegistry)
Diffstat (limited to 'sonar-server')
-rw-r--r-- | sonar-server/pom.xml | 6 | ||||
-rw-r--r-- | sonar-server/src/main/java/org/sonar/server/platform/Platform.java | 4 | ||||
-rw-r--r-- | sonar-server/src/main/java/org/sonar/server/rule/RuleRegistry.java (renamed from sonar-server/src/main/java/org/sonar/server/startup/IndexRules.java) | 17 | ||||
-rw-r--r-- | sonar-server/src/main/java/org/sonar/server/search/SearchIndex.java | 22 | ||||
-rw-r--r-- | sonar-server/src/main/java/org/sonar/server/search/SearchNode.java | 1 | ||||
-rw-r--r-- | sonar-server/src/test/java/org/sonar/server/search/SearchIndexTest.java | 65 | ||||
-rw-r--r-- | sonar-server/src/test/resources/org/sonar/server/search/SearchIndexTest/correct_mapping1.json | 9 | ||||
-rw-r--r-- | sonar-server/src/test/resources/org/sonar/server/search/SearchIndexTest/correct_mapping2.json | 9 | ||||
-rw-r--r-- | sonar-server/src/test/resources/org/sonar/server/search/SearchIndexTest/malformed.json | 1 |
9 files changed, 106 insertions, 28 deletions
diff --git a/sonar-server/pom.xml b/sonar-server/pom.xml index ed96eff0aa2..e67c953a9b8 100644 --- a/sonar-server/pom.xml +++ b/sonar-server/pom.xml @@ -147,6 +147,12 @@ <artifactId>sonar-testing-harness</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>com.github.tlrx</groupId> + <artifactId>elasticsearch-test</artifactId> + <version>${elasticsearch.version}</version> + <scope>test</scope> + </dependency> </dependencies> diff --git a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java index 777e93e08cc..c7e56a72e19 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java @@ -19,6 +19,8 @@ */ package org.sonar.server.platform; +import org.sonar.server.rule.RuleRegistry; + import org.apache.commons.configuration.BaseConfiguration; import org.slf4j.LoggerFactory; import org.sonar.api.config.EmailSettings; @@ -354,7 +356,7 @@ public final class Platform { startupContainer.addSingleton(LogServerId.class); startupContainer.addSingleton(RegisterServletFilters.class); startupContainer.addSingleton(CleanDryRunCache.class); - startupContainer.addSingleton(IndexRules.class); + startupContainer.addSingleton(RuleRegistry.class); startupContainer.startComponents(); startupContainer.getComponentByType(ServerLifecycleNotifier.class).notifyStart(); diff --git a/sonar-server/src/main/java/org/sonar/server/startup/IndexRules.java b/sonar-server/src/main/java/org/sonar/server/rule/RuleRegistry.java index b765b05ebfd..de7ffa48d7f 100644 --- a/sonar-server/src/main/java/org/sonar/server/startup/IndexRules.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/RuleRegistry.java @@ -18,7 +18,7 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package org.sonar.server.startup; +package org.sonar.server.rule; import org.elasticsearch.common.collect.Lists; import org.elasticsearch.common.io.BytesStream; @@ -28,6 +28,7 @@ import org.sonar.api.database.DatabaseSession; import org.sonar.api.rules.Rule; import org.sonar.api.rules.RuleParam; import org.sonar.core.i18n.RuleI18nManager; +import org.sonar.jpa.session.DatabaseSessionFactory; import org.sonar.server.search.SearchIndex; import java.io.IOException; @@ -38,25 +39,29 @@ import java.util.Locale; * Fill search index with rules * @since 4.1 */ -public final class IndexRules { +public final class RuleRegistry { private static final String INDEX_RULES = "rules"; private static final String TYPE_RULE = "rule"; private SearchIndex searchIndex; - private DatabaseSession session; + private DatabaseSessionFactory sessionFactory; private RuleI18nManager ruleI18nManager; - public IndexRules(SearchIndex searchIndex, DatabaseSession session, RuleI18nManager ruleI18nManager) { + public RuleRegistry(SearchIndex searchIndex, DatabaseSessionFactory sessionFactory, RuleI18nManager ruleI18nManager) { this.searchIndex = searchIndex; - this.session = session; + this.sessionFactory = sessionFactory; this.ruleI18nManager = ruleI18nManager; } public void start() { + searchIndex.addMappingFromClasspath(INDEX_RULES, TYPE_RULE, "/com/sonar/search/rule_mapping.json"); + } + + public void bulkRegisterRules() { + DatabaseSession session = sessionFactory.getSession(); try { - searchIndex.addMappingFromClasspath(INDEX_RULES, TYPE_RULE, "/com/sonar/search/rule_mapping.json"); List<String> ids = Lists.newArrayList(); List<BytesStream> docs = Lists.newArrayList(); for (Rule rule: session.getResults(Rule.class)) { diff --git a/sonar-server/src/main/java/org/sonar/server/search/SearchIndex.java b/sonar-server/src/main/java/org/sonar/server/search/SearchIndex.java index 99704461929..8935db2faba 100644 --- a/sonar-server/src/main/java/org/sonar/server/search/SearchIndex.java +++ b/sonar-server/src/main/java/org/sonar/server/search/SearchIndex.java @@ -20,6 +20,7 @@ package org.sonar.server.search; import org.apache.commons.io.IOUtils; +import org.elasticsearch.ElasticSearchParseException; import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest; import org.elasticsearch.action.bulk.BulkItemResponse; import org.elasticsearch.action.bulk.BulkRequestBuilder; @@ -29,7 +30,6 @@ import org.elasticsearch.client.Client; import org.elasticsearch.client.IndicesAdminClient; import org.elasticsearch.client.Requests; import org.elasticsearch.common.io.BytesStream; -import org.elasticsearch.index.query.QueryBuilders; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -61,10 +61,6 @@ public class SearchIndex { client.prepareIndex(index, type, id).setSource(source.bytes()).execute().actionGet(); } - public void put(String index, String type, String id, BytesStream source, String parent) { - client.prepareIndex(index, type, id).setParent(parent).setSource(source.bytes()).execute().actionGet(); - } - public void bulkIndex(String index, String type, String[] ids, BytesStream[] sources) { BulkRequestBuilder builder = new BulkRequestBuilder(client); for (int i=0; i<ids.length; i++) { @@ -91,8 +87,10 @@ public class SearchIndex { public void addMappingFromClasspath(String index, String type, String resourcePath) { try { addMapping(index, type, IOUtils.toString(getClass().getResource(resourcePath))); + } catch(NullPointerException nonExisting) { + throw new IllegalArgumentException("Could not load unexisting file at " + resourcePath, nonExisting); } catch(IOException ioException) { - throw new IllegalStateException("Could not find mapping in classpath at "+resourcePath, ioException); + throw new IllegalArgumentException("Problem loading file at " + resourcePath, ioException); } } @@ -105,14 +103,12 @@ public class SearchIndex { } catch (Exception e) { LOG.error("While checking for index existence", e); } - indices.putMapping(Requests.putMappingRequest(index).type(type).source(mapping)).actionGet(); - } - public void stats(String index) { - LOG.info( - String.format( - "Index %s contains %d elements", index, - client.prepareSearch(index).setQuery(QueryBuilders.matchAllQuery()).execute().actionGet().getHits().totalHits())); + try { + indices.putMapping(Requests.putMappingRequest(index).type(type).source(mapping)).actionGet(); + } catch(ElasticSearchParseException parseException) { + throw new IllegalArgumentException("Invalid mapping file", parseException); + } } public SearchResponse find(SearchQuery query) { diff --git a/sonar-server/src/main/java/org/sonar/server/search/SearchNode.java b/sonar-server/src/main/java/org/sonar/server/search/SearchNode.java index 7bae90ca352..d928543fc12 100644 --- a/sonar-server/src/main/java/org/sonar/server/search/SearchNode.java +++ b/sonar-server/src/main/java/org/sonar/server/search/SearchNode.java @@ -61,6 +61,7 @@ public class SearchNode { public void start() { LOG.info("Starting {} in {}", this.getClass().getSimpleName(), nodeDir); nodeSettingsBuilder + .put("node.name", "sonarqube") .put("node.path.conf", nodeDir) .put("node.path.data", nodeDir) .put("node.path.work", nodeDir) diff --git a/sonar-server/src/test/java/org/sonar/server/search/SearchIndexTest.java b/sonar-server/src/test/java/org/sonar/server/search/SearchIndexTest.java index f10047b8ea9..0edd4146ff7 100644 --- a/sonar-server/src/test/java/org/sonar/server/search/SearchIndexTest.java +++ b/sonar-server/src/test/java/org/sonar/server/search/SearchIndexTest.java @@ -20,39 +20,88 @@ package org.sonar.server.search; -import org.elasticsearch.client.Client; +import com.github.tlrx.elasticsearch.test.EsSetup; +import org.junit.After; import org.junit.Before; import org.junit.Test; +import static org.fest.assertions.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; public class SearchIndexTest { + private EsSetup esSetup; private SearchNode searchNode; - private Client client; - private SearchIndex searchIndex; @Before public void setUp() { + esSetup = new EsSetup(); + esSetup.execute(EsSetup.deleteAll()); + searchNode = mock(SearchNode.class); - client = mock(Client.class); - when(searchNode.client()).thenReturn(client); + when(searchNode.client()).thenReturn(esSetup.client()); searchIndex = new SearchIndex(searchNode); + searchIndex.start(); + } + + @After + public void tearDown() { + esSetup.terminate(); } @Test public void should_start_and_stop_properly() { + verify(searchNode).client(); + searchIndex.stop(); + } + + @Test + public void should_create_index_when_loading_mapping_from_classpath() { + String index = "index"; + String type = "type"; + String resourcePath = "/org/sonar/server/search/SearchIndexTest/correct_mapping1.json"; + searchIndex.start(); + searchIndex.addMappingFromClasspath(index, type, resourcePath); - verify(searchNode).client(); + assertThat(esSetup.exists(index)).isTrue(); + } - searchIndex.stop(); + @Test + public void should_reuse_index_when_loading_mapping_from_classpath() { + String index = "index"; + String type1 = "type1"; + String type2 = "type2"; + String resourcePath1 = "/org/sonar/server/search/SearchIndexTest/correct_mapping1.json"; + String resourcePath2 = "/org/sonar/server/search/SearchIndexTest/correct_mapping2.json"; + + searchIndex.start(); + searchIndex.addMappingFromClasspath(index, type1, resourcePath1); + searchIndex.addMappingFromClasspath(index, type2, resourcePath2); - verify(client).close(); + assertThat(esSetup.exists(index)).isTrue(); } + + + @Test(expected = IllegalArgumentException.class) + public void should_fail_to_load_inexistent_mapping() { + String resourcePath = "/org/sonar/server/search/SearchIndexTest/inexistent.json"; + + searchIndex.start(); + searchIndex.addMappingFromClasspath("unchecked", "unchecked", resourcePath); + } + + @Test(expected = IllegalArgumentException.class) + public void should_fail_to_load_malformed_mapping() { + String resourcePath = "/org/sonar/server/search/SearchIndexTest/malformed.json"; + + searchIndex.start(); + searchIndex.addMappingFromClasspath("unchecked", "unchecked", resourcePath); + } + } diff --git a/sonar-server/src/test/resources/org/sonar/server/search/SearchIndexTest/correct_mapping1.json b/sonar-server/src/test/resources/org/sonar/server/search/SearchIndexTest/correct_mapping1.json new file mode 100644 index 00000000000..67e2dddf823 --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/search/SearchIndexTest/correct_mapping1.json @@ -0,0 +1,9 @@ +{ + "type1": { + "properties": { + "value": { + "type": "string" + } + } + } +} diff --git a/sonar-server/src/test/resources/org/sonar/server/search/SearchIndexTest/correct_mapping2.json b/sonar-server/src/test/resources/org/sonar/server/search/SearchIndexTest/correct_mapping2.json new file mode 100644 index 00000000000..d1b9448215a --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/search/SearchIndexTest/correct_mapping2.json @@ -0,0 +1,9 @@ +{ + "type2": { + "properties": { + "value": { + "type": "string" + } + } + } +} diff --git a/sonar-server/src/test/resources/org/sonar/server/search/SearchIndexTest/malformed.json b/sonar-server/src/test/resources/org/sonar/server/search/SearchIndexTest/malformed.json new file mode 100644 index 00000000000..a01ad5e86aa --- /dev/null +++ b/sonar-server/src/test/resources/org/sonar/server/search/SearchIndexTest/malformed.json @@ -0,0 +1 @@ +blarg |