import com.sonar.orchestrator.Orchestrator;
import com.sonar.orchestrator.selenium.Selenese;
import it.Category1Suite;
+import java.util.List;
+import java.util.Map;
+import org.junit.After;
+import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
-import org.sonar.wsclient.services.PropertyQuery;
import org.sonar.wsclient.services.PropertyUpdateQuery;
+import org.sonarqube.ws.Settings;
+import org.sonarqube.ws.client.setting.ResetRequest;
+import org.sonarqube.ws.client.setting.SettingsService;
+import org.sonarqube.ws.client.setting.ValuesRequest;
import util.selenium.SeleneseTest;
+import static java.util.Arrays.asList;
+import static java.util.Arrays.stream;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.entry;
+import static util.ItUtils.newAdminWsClient;
public class PropertySetsTest {
@ClassRule
public static Orchestrator orchestrator = Category1Suite.ORCHESTRATOR;
+ static SettingsService SETTINGS;
+
+ @BeforeClass
+ public static void initSettingsService() throws Exception {
+ SETTINGS = newAdminWsClient(orchestrator).settingsService();
+ }
+
+ @After
+ public void reset_settings() throws Exception {
+ resetSetting("sonar.demo", "sonar.autogenerated", "sonar.test.jira.servers");
+ }
+
@Test
- public void should_support_property_sets() {
+ public void support_property_sets() {
Selenese selenese = Selenese.builder().setHtmlTestsInClasspath("property-sets",
"/settings/PropertySetsTest/property-sets/create.html",
"/settings/PropertySetsTest/property-sets/delete.html",
"/settings/PropertySetsTest/property-sets/reference.html",
- "/settings/PropertySetsTest/property-sets/all_types.html"
- ).build();
+ "/settings/PropertySetsTest/property-sets/all_types.html").build();
// Use the old runner because it fails with the new Selenium runner
orchestrator.executeSelenese(selenese);
// SSF-25 Check that the password has well be setted as now it does not appears in the html source code
- String sonarDemoValue = getProperty("sonar.demo");
- assertThat(getProperty("sonar.demo." + sonarDemoValue + ".password")).isEqualTo("abcde");
+ assertPropertySet("sonar.demo",
+ asList(entry("password", "abcde"),
+ entry("boolean", "true"),
+ entry("float", "42.0"),
+ entry("license", "abc"),
+ entry("list", "AAA"),
+ entry("metric", "overall_branch_coverage"),
+ entry("regexp", ".*"),
+ entry("text", "text")));
+ }
+
+ @Test
+ public void support_property_sets_with_auto_generated_keys() {
+ new SeleneseTest(
+ Selenese.builder().setHtmlTestsInClasspath("create-auto-generated",
+ "/settings/PropertySetsTest/auto-generated/create.html").build()).runOn(orchestrator);
+ assertPropertySet("sonar.autogenerated", asList(entry("value", "FIRST")), asList(entry("value", "SECOND")), asList(entry("value", "THIRD")));
+
+ new SeleneseTest(Selenese.builder().setHtmlTestsInClasspath("update-auto-generated",
+ "/settings/PropertySetsTest/auto-generated/update.html").build()).runOn(orchestrator);
+ assertPropertySet("sonar.autogenerated", asList(entry("value", "FIRST")), asList(entry("value", "THIRD")));
}
@Test
- public void should_edit_properties() {
+ public void edit_property_set() {
setProperty("sonar.test.jira.servers", "jira1,jira2");
+ setProperty("sonar.test.jira.servers.jira1.key", "jira1");
setProperty("sonar.test.jira.servers.jira1.url", "http://jira1");
- setProperty("sonar.test.jira.servers.jira2.url", "http://jira2");
setProperty("sonar.test.jira.servers.jira1.port", "12345");
- setProperty("sonar.test.jira.servers.jira2.port", "66666");
+ setProperty("sonar.test.jira.servers.jira2.key", "jira2");
+ setProperty("sonar.test.jira.servers.jira2.url", "http://jira2");
+ setProperty("sonar.test.jira.servers.jira2.port", "54321");
- assertThat(getProperty("sonar.test.jira.servers")).isEqualTo("jira1,jira2");
- assertThat(getProperty("sonar.test.jira.servers.jira1.url")).isEqualTo("http://jira1");
- assertThat(getProperty("sonar.test.jira.servers.jira2.url")).isEqualTo("http://jira2");
- assertThat(getProperty("sonar.test.jira.servers.jira1.port")).isEqualTo("12345");
- assertThat(getProperty("sonar.test.jira.servers.jira2.port")).isEqualTo("66666");
+ assertPropertySet("sonar.test.jira.servers",
+ asList(entry("key", "jira1"), entry("url", "http://jira1"), entry("port", "12345")),
+ asList(entry("key", "jira2"), entry("url", "http://jira2"), entry("port", "54321")));
}
@Test
- public void should_support_property_sets_with_auto_generated_keys() {
- new SeleneseTest(
- Selenese.builder().setHtmlTestsInClasspath("create-auto-generated",
- "/settings/PropertySetsTest/auto-generated/create.html"
- ).build()).runOn(orchestrator);
+ public void delete_property_set() throws Exception {
+ setProperty("sonar.test.jira.servers", "jira1");
+ setProperty("sonar.test.jira.servers.jira1.url", "http://jira1");
+ setProperty("sonar.test.jira.servers.jira1.port", "12345");
- String[] keys = getProperty("sonar.autogenerated").split("[,]");
- assertThat(getProperty("sonar.autogenerated." + keys[0] + ".value")).isEqualTo("FIRST");
- assertThat(getProperty("sonar.autogenerated." + keys[1] + ".value")).isEqualTo("SECOND");
- assertThat(getProperty("sonar.autogenerated." + keys[2] + ".value")).isEqualTo("THIRD");
+ resetSetting("sonar.test.jira.servers");
- new SeleneseTest(Selenese.builder().setHtmlTestsInClasspath("update-auto-generated",
- "/settings/PropertySetsTest/auto-generated/update.html"
- ).build()).runOn(orchestrator);
+ assertThat(SETTINGS.values(ValuesRequest.builder().setKeys("sonar.test.jira.servers").build()).getSettingsList()).isEmpty();
+ }
- keys = getProperty("sonar.autogenerated").split("[,]");
- assertThat(getProperty("sonar.autogenerated." + keys[0] + ".value")).isEqualTo("FIRST");
- assertThat(getProperty("sonar.autogenerated." + keys[1] + ".value")).isEqualTo("THIRD");
+ private void assertPropertySet(String baseSettingKey, List<Map.Entry<String, String>>... fieldsValues) {
+ Settings.Setting setting = getSetting(baseSettingKey);
+ assertThat(setting.getFieldsValues().getFieldsValuesList()).hasSize(fieldsValues.length);
+ int index = 0;
+ for (Settings.FieldsValues.Value fieldsValue : setting.getFieldsValues().getFieldsValuesList()) {
+ assertThat(fieldsValue.getValue()).containsOnly(fieldsValues[index].toArray(new Map.Entry[] {}));
+ index++;
+ }
}
+ private Settings.Setting getSetting(String key) {
+ Settings.ValuesWsResponse response = SETTINGS.values(ValuesRequest.builder().setKeys(key).build());
+ List<Settings.Setting> settings = response.getSettingsList();
+ assertThat(settings).hasSize(1);
+ return settings.get(0);
+ }
+
+ /**
+ * @deprecated Replace with api/settings/set WS when setting property set will be possible in the WS
+ */
+ @Deprecated
static void setProperty(String key, String value) {
orchestrator.getServer().getAdminWsClient().update(new PropertyUpdateQuery(key, value));
}
- static String getProperty(String key) {
- return orchestrator.getServer().getAdminWsClient().find(new PropertyQuery().setKey(key)).getValue();
+ static void resetSetting(String... keys) {
+ stream(keys).forEach(key -> SETTINGS.reset(ResetRequest.builder().setKey(key).build()));
}
+
}