]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9126 Add ITs
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 26 Apr 2017 08:56:00 +0000 (10:56 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 26 Apr 2017 14:34:31 +0000 (16:34 +0200)
it/it-plugins/billing-plugin/pom.xml [new file with mode: 0644]
it/it-plugins/billing-plugin/src/main/java/BillingPlugin.java [new file with mode: 0644]
it/it-plugins/billing-plugin/src/main/java/BillingValidations.java [new file with mode: 0644]
it/it-plugins/pom.xml
it/it-tests/src/test/java/it/Category6Suite.java
it/it-tests/src/test/java/it/organization/BillingTest.java [new file with mode: 0644]

diff --git a/it/it-plugins/billing-plugin/pom.xml b/it/it-plugins/billing-plugin/pom.xml
new file mode 100644 (file)
index 0000000..e3fc4d6
--- /dev/null
@@ -0,0 +1,44 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>org.sonarsource.sonarqube</groupId>
+    <artifactId>it-plugins</artifactId>
+    <version>6.4-SNAPSHOT</version>
+  </parent>
+
+  <artifactId>billing-plugin</artifactId>
+  <packaging>sonar-plugin</packaging>
+  <description>Plugins :: Billing</description>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.sonarsource.sonarqube</groupId>
+      <artifactId>sonar-plugin-api</artifactId>
+      <version>${apiVersion}</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.sonarsource.sonarqube</groupId>
+      <artifactId>sonar-server</artifactId>
+      <version>${apiVersion}</version>
+      <scope>provided</scope>
+    </dependency>
+  </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.sonarsource.sonar-packaging-maven-plugin</groupId>
+        <artifactId>sonar-packaging-maven-plugin</artifactId>
+        <version>1.15</version>
+        <extensions>true</extensions>
+        <configuration>
+          <pluginClass>BillingPlugin</pluginClass>
+          <pluginKey>billing</pluginKey>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/it/it-plugins/billing-plugin/src/main/java/BillingPlugin.java b/it/it-plugins/billing-plugin/src/main/java/BillingPlugin.java
new file mode 100644 (file)
index 0000000..9552569
--- /dev/null
@@ -0,0 +1,42 @@
+
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 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.
+ */
+
+import org.sonar.api.Plugin;
+
+public class BillingPlugin implements Plugin {
+
+  @Override
+  public void define(Context context) {
+    // Nothing should be loaded when the plugin is running within by the scanner
+    if (isRunningInSQ()) {
+      context.addExtension(BillingValidations.class);
+    }
+  }
+
+  private static boolean isRunningInSQ() {
+    try {
+      Class.forName("org.sonar.plugin.PrivilegedPluginBridge");
+      return true;
+    } catch (ClassNotFoundException e) {
+      return false;
+    }
+  }
+}
diff --git a/it/it-plugins/billing-plugin/src/main/java/BillingValidations.java b/it/it-plugins/billing-plugin/src/main/java/BillingValidations.java
new file mode 100644 (file)
index 0000000..b21d9c6
--- /dev/null
@@ -0,0 +1,44 @@
+
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 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.
+ */
+
+import org.sonar.api.config.Settings;
+import org.sonar.server.organization.BillingValidationsExtension;
+
+import static java.lang.String.format;
+
+public class BillingValidations implements BillingValidationsExtension {
+
+  private static final String PREVENT_PROJECT_ANALYSIS_SETTING = "sonar.billing.preventProjectAnalysis";
+
+  private final Settings settings;
+
+  public BillingValidations(Settings settings) {
+    this.settings = settings;
+  }
+
+  @Override
+  public void checkOnProjectAnalysis(Organization organization) {
+    boolean preventProjectAnalysis = settings.getBoolean(PREVENT_PROJECT_ANALYSIS_SETTING);
+    if (preventProjectAnalysis) {
+      throw new BillingValidationsException(format("Organization %s cannot perform analysis", organization.getKey()));
+    }
+  }
+}
index 3d4bc6f8ad3d2fdddb684ac0f5bbd906a65070b9..a36083c8433ea3d74d4fa88daafbd2563610a994 100644 (file)
@@ -33,6 +33,7 @@
     <module>access-secured-props-plugin</module>
     <module>base-auth-plugin</module>
     <module>batch-plugin</module>
+    <module>billing-plugin</module>
     <module>extension-lifecycle-plugin</module>
     <module>global-property-change-plugin</module>
     <module>issue-filter-plugin</module>
index 9e627ae9bf54709877024e02988e188cd1ed309f..65a39793a756d12928134227c8960e6785c7e57f 100644 (file)
@@ -21,6 +21,7 @@ package it;
 
 import com.sonar.orchestrator.Orchestrator;
 import it.issue.OrganizationIssueAssignTest;
+import it.organization.BillingTest;
 import it.organization.OrganizationMembershipTest;
 import it.organization.OrganizationTest;
 import it.qualityProfile.OrganizationQualityProfilesPageTest;
@@ -46,6 +47,7 @@ import static util.ItUtils.xooPlugin;
   OrganizationQualityProfilesPageTest.class,
   OrganizationTest.class,
   OrganizationUiExtensionsTest.class,
+  BillingTest.class
 })
 public class Category6Suite {
 
@@ -54,6 +56,7 @@ public class Category6Suite {
     .addPlugin(xooPlugin())
     .addPlugin(pluginArtifact("base-auth-plugin"))
     .addPlugin(pluginArtifact("ui-extensions-plugin"))
+    .addPlugin(pluginArtifact("billing-plugin"))
     .build();
 
   @BeforeClass
diff --git a/it/it-tests/src/test/java/it/organization/BillingTest.java b/it/it-tests/src/test/java/it/organization/BillingTest.java
new file mode 100644 (file)
index 0000000..10a623d
--- /dev/null
@@ -0,0 +1,109 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 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 it.organization;
+
+import com.sonar.orchestrator.Orchestrator;
+import com.sonar.orchestrator.build.BuildResult;
+import com.sonar.orchestrator.build.SonarScanner;
+import it.Category6Suite;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.ClassRule;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonarqube.ws.client.WsClient;
+import org.sonarqube.ws.client.organization.CreateWsRequest;
+import util.ItUtils;
+import util.user.UserRule;
+
+import static it.Category6Suite.enableOrganizationsSupport;
+import static java.lang.String.format;
+import static org.assertj.core.api.Java6Assertions.assertThat;
+import static org.sonarqube.ws.WsCe.TaskResponse;
+import static util.ItUtils.newAdminWsClient;
+import static util.ItUtils.newOrganizationKey;
+import static util.ItUtils.newProjectKey;
+import static util.ItUtils.projectDir;
+import static util.ItUtils.resetSettings;
+import static util.ItUtils.setServerProperty;
+
+public class BillingTest {
+  @ClassRule
+  public static Orchestrator orchestrator = Category6Suite.ORCHESTRATOR;
+
+  @ClassRule
+  public static UserRule userRule = UserRule.from(orchestrator);
+
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  private static WsClient adminClient;
+
+  @BeforeClass
+  public static void setUp() throws Exception {
+    adminClient = newAdminWsClient(orchestrator);
+    enableOrganizationsSupport();
+    resetSettings(orchestrator, "sonar.billing.preventProjectAnalysis");
+  }
+
+  @AfterClass
+  public static void tearDown() throws Exception {
+    resetSettings(orchestrator, "sonar.billing.preventProjectAnalysis");
+  }
+
+  @Test
+  public void execute_successfully_ce_analysis_on_organization() {
+    String organizationKey = createOrganization();
+    setServerProperty(orchestrator, "sonar.billing.preventProjectAnalysis", "false");
+
+    String taskUuid = executeAnalysis(organizationKey);
+
+    TaskResponse taskResponse = adminClient.ce().task(taskUuid);
+    assertThat(taskResponse.getTask().hasErrorMessage()).isFalse();
+  }
+
+  @Test
+  public void fail_to_execute_ce_analysis_on_organization() {
+    String organizationKey = createOrganization();
+    setServerProperty(orchestrator, "sonar.billing.preventProjectAnalysis", "true");
+
+    String taskUuid = executeAnalysis(organizationKey);
+
+    TaskResponse taskResponse = adminClient.ce().task(taskUuid);
+    assertThat(taskResponse.getTask().hasErrorMessage()).isTrue();
+    assertThat(taskResponse.getTask().getErrorMessage()).contains(format("Organization %s cannot perform analysis", organizationKey));
+  }
+
+  private static String createOrganization() {
+    String key = newOrganizationKey();
+    adminClient.organizations().create(new CreateWsRequest.Builder().setKey(key).setName(key).build()).getOrganization();
+    return key;
+  }
+
+  private static String executeAnalysis(String organizationKey) {
+    BuildResult buildResult = orchestrator.executeBuild(SonarScanner.create(projectDir("shared/xoo-sample"),
+      "sonar.organization", organizationKey,
+      "sonar.projectKey", newProjectKey(),
+      "sonar.login", "admin",
+      "sonar.password", "admin"));
+    return ItUtils.extractCeTaskId(buildResult);
+  }
+}