From: Stephane Gamard Date: Fri, 25 Apr 2014 10:50:51 +0000 (+0200) Subject: SONAR-5237 - initial JUnit for BaseIndex X-Git-Tag: 4.4-RC1~1388 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=d2629c324ec15631bb12ccfa6844925b5c90308c;p=sonarqube.git SONAR-5237 - initial JUnit for BaseIndex --- diff --git a/sonar-server/src/test/java/org/sonar/server/es/BaseIndexTest.java b/sonar-server/src/test/java/org/sonar/server/es/BaseIndexTest.java new file mode 100644 index 00000000000..c6b30dc8943 --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/es/BaseIndexTest.java @@ -0,0 +1,99 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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; + +import java.io.Serializable; +import java.util.Map; + +import org.elasticsearch.client.transport.NoNodeAvailableException; +import org.elasticsearch.common.settings.ImmutableSettings; +import org.elasticsearch.node.Node; +import org.elasticsearch.node.NodeBuilder; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.sonar.api.config.Settings; +import org.sonar.core.profiling.Profiling; +import org.sonar.server.search.BaseIndex; +import static org.fest.assertions.Assertions.assertThat; + +public class BaseIndexTest { + + private static final String TEST_NODE_NAME = "es_node_for_tests"; + + private BaseIndex searchIndex; + private Node node; + + @Before + public void setUp() throws Exception { + + this.node = NodeBuilder.nodeBuilder() + .settings(ImmutableSettings.settingsBuilder() + .put("node.name", TEST_NODE_NAME).build()) + .clusterName(BaseIndex.ES_CLUSTER_NAME).node(); + } + + private BaseIndex getBaseIndex(){ + Settings settings = new Settings(); + settings.setProperty("sonar.log.profilingLevel", "BASIC"); + return new BaseIndex(null, new Profiling(settings)) { + @Override + public Map normalize(Serializable key) { + // TODO Auto-generated method stub + return null; + } + }; + } + + @After + public void tearDown() { + if (node != null && !node.isClosed()) { + node.close(); + } + } + + @Test + public void should_start_and_stop_properly() { + + searchIndex = getBaseIndex(); + searchIndex.start(); + + assertThat(node.client().admin().cluster().prepareClusterStats().get().getNodesStats().getCounts().getTotal()) + .isEqualTo(searchIndex.getNodesStats().getCounts().getTotal()); + + searchIndex.stop(); + + } + + @Test(expected = NoNodeAvailableException.class) + public void fails_when_es_gone(){ + searchIndex = getBaseIndex(); + searchIndex.start(); + + node.stop(); + + + assertThat(searchIndex.getNodesStats().getCounts().getTotal()); + + node.start(); + + + } +}