]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8651 add and use OrganizationDbTester.setDefaultTemplates with dtos
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 30 Jan 2017 16:36:15 +0000 (17:36 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 2 Feb 2017 15:12:13 +0000 (16:12 +0100)
server/sonar-server/src/test/java/org/sonar/server/component/ComponentUpdaterTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/PermissionTemplateServiceTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/DeleteTemplateActionTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/SearchTemplatesActionTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/SetDefaultTemplateActionTest.java
server/sonar-server/src/test/java/org/sonar/server/startup/RegisterPermissionTemplatesTest.java
sonar-db/src/test/java/org/sonar/db/organization/OrganizationDbTester.java

index d3ca2eea18f7771cf5a50d85af5de51fc455f177..5442ad996d3d70cbf8c49a2ed3b66b93c99244bb 100644 (file)
@@ -61,17 +61,17 @@ public class ComponentUpdaterTest {
   private PermissionTemplateService permissionTemplateService = mock(PermissionTemplateService.class);
 
   private ComponentUpdater underTest = new ComponentUpdater(db.getDbClient(), i18n, system2,
-    permissionTemplateService,
-    new FavoriteUpdater(db.getDbClient()),
-    projectIndexer);
+      permissionTemplateService,
+      new FavoriteUpdater(db.getDbClient()),
+      projectIndexer);
 
   @Test
   public void should_persist_and_index_when_creating_project() throws Exception {
     NewComponent project = NewComponent.newComponentBuilder()
-      .setKey(DEFAULT_PROJECT_KEY)
-      .setName(DEFAULT_PROJECT_NAME)
-      .setOrganizationUuid(db.getDefaultOrganization().getUuid())
-      .build();
+        .setKey(DEFAULT_PROJECT_KEY)
+        .setName(DEFAULT_PROJECT_NAME)
+        .setOrganizationUuid(db.getDefaultOrganization().getUuid())
+        .build();
     ComponentDto returned = underTest.create(db.getSession(), project, null);
 
     ComponentDto loaded = db.getDbClient().componentDao().selectOrFailByUuid(db.getSession(), returned.uuid());
@@ -95,13 +95,13 @@ public class ComponentUpdaterTest {
   @Test
   public void create_project_with_branch() throws Exception {
     ComponentDto project = underTest.create(db.getSession(),
-      NewComponent.newComponentBuilder()
-        .setKey(DEFAULT_PROJECT_KEY)
-        .setName(DEFAULT_PROJECT_NAME)
-        .setBranch("origin/master")
-        .setOrganizationUuid(db.getDefaultOrganization().getUuid())
-        .build(),
-      null);
+        NewComponent.newComponentBuilder()
+            .setKey(DEFAULT_PROJECT_KEY)
+            .setName(DEFAULT_PROJECT_NAME)
+            .setBranch("origin/master")
+            .setOrganizationUuid(db.getDefaultOrganization().getUuid())
+            .build(),
+        null);
 
     assertThat(project.getKey()).isEqualTo("project-key:origin/master");
   }
@@ -110,10 +110,10 @@ public class ComponentUpdaterTest {
   public void should_apply_default_permission_template() throws Exception {
     long userId = 42;
     NewComponent project = NewComponent.newComponentBuilder()
-      .setKey(DEFAULT_PROJECT_KEY)
-      .setName(DEFAULT_PROJECT_NAME)
-      .setOrganizationUuid(db.getDefaultOrganization().getUuid())
-      .build();
+        .setKey(DEFAULT_PROJECT_KEY)
+        .setName(DEFAULT_PROJECT_NAME)
+        .setOrganizationUuid(db.getDefaultOrganization().getUuid())
+        .build();
     ComponentDto dto = underTest.create(db.getSession(), project, userId);
 
     verify(permissionTemplateService).applyDefault(db.getSession(), dto.getOrganizationUuid(), dto, userId);
@@ -123,16 +123,16 @@ public class ComponentUpdaterTest {
   public void should_add_project_to_user_favorites_if_project_creator_is_defined_in_permission_template() throws Exception {
     UserDto userDto = db.users().insertUser();
     NewComponent project = NewComponent.newComponentBuilder()
-      .setKey(DEFAULT_PROJECT_KEY)
-      .setName(DEFAULT_PROJECT_NAME)
-      .setOrganizationUuid(db.getDefaultOrganization().getUuid())
-      .build();
+        .setKey(DEFAULT_PROJECT_KEY)
+        .setName(DEFAULT_PROJECT_NAME)
+        .setOrganizationUuid(db.getDefaultOrganization().getUuid())
+        .build();
     when(permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(eq(db.getSession()), eq(project.getOrganizationUuid()), any(ComponentDto.class)))
-      .thenReturn(true);
+        .thenReturn(true);
 
     ComponentDto dto = underTest.create(db.getSession(),
-      project,
-      userDto.getId());
+        project,
+        userDto.getId());
 
     assertThat(db.favorites().hasFavorite(dto, userDto.getId())).isTrue();
   }
@@ -140,12 +140,12 @@ public class ComponentUpdaterTest {
   @Test
   public void does_not_add_project_to_favorite_when_anonymously_created() throws Exception {
     ComponentDto project = underTest.create(db.getSession(),
-      NewComponent.newComponentBuilder()
-        .setKey(DEFAULT_PROJECT_KEY)
-        .setName(DEFAULT_PROJECT_NAME)
-        .setOrganizationUuid(db.getDefaultOrganization().getUuid())
-        .build(),
-      null);
+        NewComponent.newComponentBuilder()
+            .setKey(DEFAULT_PROJECT_KEY)
+            .setName(DEFAULT_PROJECT_NAME)
+            .setOrganizationUuid(db.getDefaultOrganization().getUuid())
+            .build(),
+        null);
 
     assertThat(db.favorites().hasNoFavorite(project)).isTrue();
   }
@@ -153,12 +153,12 @@ public class ComponentUpdaterTest {
   @Test
   public void does_not_add_project_to_favorite_when_project_has_no_permission_on_template() throws Exception {
     ComponentDto project = underTest.create(db.getSession(),
-      NewComponent.newComponentBuilder()
-        .setKey(DEFAULT_PROJECT_KEY)
-        .setName(DEFAULT_PROJECT_NAME)
-        .setOrganizationUuid(db.getDefaultOrganization().getUuid())
-        .build(),
-      null);
+        NewComponent.newComponentBuilder()
+            .setKey(DEFAULT_PROJECT_KEY)
+            .setName(DEFAULT_PROJECT_NAME)
+            .setOrganizationUuid(db.getDefaultOrganization().getUuid())
+            .build(),
+        null);
 
     assertThat(db.favorites().hasNoFavorite(project)).isTrue();
   }
@@ -171,12 +171,12 @@ public class ComponentUpdaterTest {
     expectedException.expectMessage("Could not create Project, key already exists: " + existing.key());
 
     underTest.create(db.getSession(),
-      NewComponent.newComponentBuilder()
-        .setKey(existing.key())
-        .setName(DEFAULT_PROJECT_NAME)
-        .setOrganizationUuid(existing.getOrganizationUuid())
-        .build(),
-      null);
+        NewComponent.newComponentBuilder()
+            .setKey(existing.key())
+            .setName(DEFAULT_PROJECT_NAME)
+            .setOrganizationUuid(existing.getOrganizationUuid())
+            .build(),
+        null);
   }
 
   @Test
@@ -185,12 +185,12 @@ public class ComponentUpdaterTest {
     expectedException.expectMessage("Malformed key for Project: 1234");
 
     underTest.create(db.getSession(),
-      NewComponent.newComponentBuilder()
-        .setKey("1234")
-        .setName(DEFAULT_PROJECT_NAME)
-        .setOrganizationUuid(db.getDefaultOrganization().getUuid())
-        .build(),
-      null);
+        NewComponent.newComponentBuilder()
+            .setKey("1234")
+            .setName(DEFAULT_PROJECT_NAME)
+            .setOrganizationUuid(db.getDefaultOrganization().getUuid())
+            .build(),
+        null);
   }
 
   @Test
@@ -199,23 +199,23 @@ public class ComponentUpdaterTest {
     expectedException.expectMessage("Malformed branch for Project: origin?branch. Allowed characters are alphanumeric, '-', '_', '.' and '/', with at least one non-digit.");
 
     underTest.create(db.getSession(),
-      NewComponent.newComponentBuilder()
-        .setKey(DEFAULT_PROJECT_KEY)
-        .setName(DEFAULT_PROJECT_NAME)
-        .setBranch("origin?branch")
-        .setOrganizationUuid(db.getDefaultOrganization().getUuid())
-        .build(),
-      null);
+        NewComponent.newComponentBuilder()
+            .setKey(DEFAULT_PROJECT_KEY)
+            .setName(DEFAULT_PROJECT_NAME)
+            .setBranch("origin?branch")
+            .setOrganizationUuid(db.getDefaultOrganization().getUuid())
+            .build(),
+        null);
   }
 
   @Test
   public void persist_and_index_when_creating_view() {
     NewComponent view = NewComponent.newComponentBuilder()
-      .setKey("view-key")
-      .setName("view-name")
-      .setQualifier(VIEW)
-      .setOrganizationUuid(db.getDefaultOrganization().getUuid())
-      .build();
+        .setKey("view-key")
+        .setName("view-name")
+        .setQualifier(VIEW)
+        .setOrganizationUuid(db.getDefaultOrganization().getUuid())
+        .build();
 
     ComponentDto returned = underTest.create(db.getSession(), view, null);
 
index 35386dca95650721986e61ebf55c5ec9c4f38bdf..cad1b8e54d7ec7adf7069d3f863ac573bb89ed90 100644 (file)
@@ -113,7 +113,7 @@ public class PermissionTemplateServiceTest {
     GroupDto group = dbTester.users().insertGroup(organization);
     dbTester.users().insertMember(group, user);
     PermissionTemplateDto template = templateDb.insertTemplate(organization);
-    dbTester.organizations().setDefaultTemplates(organization, template.getUuid(), null);
+    dbTester.organizations().setDefaultTemplates(template, null);
     templateDb.addProjectCreatorToTemplate(template.getId(), SCAN_EXECUTION);
     templateDb.addUserToTemplate(template.getId(), user.getId(), UserRole.USER);
     templateDb.addGroupToTemplate(template.getId(), group.getId(), UserRole.CODEVIEWER);
@@ -144,7 +144,7 @@ public class PermissionTemplateServiceTest {
   @Test
   public void would_user_have_permission_with_empty_template() {
     PermissionTemplateDto template = templateDb.insertTemplate(dbTester.getDefaultOrganization());
-    dbTester.organizations().setDefaultTemplates(dbTester.getDefaultOrganization(), template.getUuid(), null);
+    dbTester.organizations().setDefaultTemplates(template, null);
 
     checkWouldUserHavePermission(dbTester.getDefaultOrganization(), null, UserRole.ADMIN, false);
   }
index d6fbe605168b0d1d67169b8b3a29341a69a7a129..368888bef23fda4a2acfdcdf8fbad15d763f175e 100644 (file)
@@ -91,7 +91,9 @@ public class DeleteTemplateActionTest {
     runOnAllUnderTests((underTest) -> {
       OrganizationDto organization = db.organizations().insert();
       PermissionTemplateDto template = insertTemplateAndAssociatedPermissions(organization);
-      db.organizations().setDefaultTemplates(organization, "foo", "bar");
+      db.organizations().setDefaultTemplates(
+        db.permissionTemplates().insertTemplate(organization),
+        db.permissionTemplates().insertTemplate(organization));
       loginAsAdmin(organization);
 
       TestResponse result = newRequestByUuid(underTest, template.getUuid());
@@ -105,7 +107,9 @@ public class DeleteTemplateActionTest {
   public void delete_template_by_name_case_insensitive() throws Exception {
     runOnAllUnderTests((underTest) -> {
       OrganizationDto organization = db.organizations().insert();
-      db.organizations().setDefaultTemplates(organization, "project def template uuid", "view def template uuid");
+      db.organizations().setDefaultTemplates(
+        db.permissionTemplates().insertTemplate(organization),
+        db.permissionTemplates().insertTemplate(organization));
       PermissionTemplateDto template = insertTemplateAndAssociatedPermissions(organization);
       loginAsAdmin(organization);
       newRequestByName(underTest, organization, template);
@@ -117,7 +121,9 @@ public class DeleteTemplateActionTest {
   @Test
   public void delete_template_by_name_returns_empty_when_no_organization_is_provided_and_templates_does_not_belong_to_default_organization() throws Exception {
     OrganizationDto organization = db.organizations().insert();
-    db.organizations().setDefaultTemplates(organization, "project def template uuid", "view def template uuid");
+    db.organizations().setDefaultTemplates(
+      db.permissionTemplates().insertTemplate(organization),
+      db.permissionTemplates().insertTemplate(organization));
     PermissionTemplateDto template = insertTemplateAndAssociatedPermissions(organization);
     loginAsAdmin(organization);
 
@@ -134,7 +140,9 @@ public class DeleteTemplateActionTest {
   @Test
   public void delete_template_by_name_returns_empty_when_wrong_organization_is_provided() throws Exception {
     OrganizationDto organization = db.organizations().insert();
-    db.organizations().setDefaultTemplates(organization, "project def template uuid", "view def template uuid");
+    db.organizations().setDefaultTemplates(
+      db.permissionTemplates().insertTemplate(organization),
+      db.permissionTemplates().insertTemplate(organization));
     PermissionTemplateDto template = insertTemplateAndAssociatedPermissions(organization);
     OrganizationDto otherOrganization = db.organizations().insert();
     loginAsAdmin(organization);
@@ -179,14 +187,15 @@ public class DeleteTemplateActionTest {
 
   private void fail_to_delete_by_uuid_if_template_is_default_template_for_project(WsActionTester underTest) throws Exception {
     OrganizationDto organization = db.organizations().insert();
-    PermissionTemplateDto template = insertTemplateAndAssociatedPermissions(organization);
-    db.organizations().setDefaultTemplates(organization, template.getUuid(), "view def template uuid");
+    PermissionTemplateDto projectTemplate = insertTemplateAndAssociatedPermissions(organization);
+    db.organizations().setDefaultTemplates(projectTemplate,
+      db.permissionTemplates().insertTemplate(organization));
     loginAsAdmin(organization);
 
     expectedException.expect(BadRequestException.class);
     expectedException.expectMessage("It is not possible to delete the default permission template for projects");
 
-    newRequestByUuid(underTest, template.getUuid());
+    newRequestByUuid(underTest, projectTemplate.getUuid());
   }
 
   @Test
@@ -201,21 +210,21 @@ public class DeleteTemplateActionTest {
 
   private void fail_to_delete_by_name_if_template_is_default_template_for_project(WsActionTester underTest) throws Exception {
     OrganizationDto organization = db.organizations().insert();
-    PermissionTemplateDto template = insertTemplateAndAssociatedPermissions(organization);
-    db.organizations().setDefaultTemplates(organization, template.getUuid(), "view def template uuid");
+    PermissionTemplateDto projectTemplate = insertTemplateAndAssociatedPermissions(organization);
+    db.organizations().setDefaultTemplates(projectTemplate, db.permissionTemplates().insertTemplate(organization));
     loginAsAdmin(organization);
 
     expectedException.expect(BadRequestException.class);
     expectedException.expectMessage("It is not possible to delete the default permission template for projects");
 
-    newRequestByName(underTest, organization.getKey(), template.getName());
+    newRequestByName(underTest, organization.getKey(), projectTemplate.getName());
   }
 
   @Test
   public void fail_to_delete_by_uuid_if_template_is_default_template_for_view_with_views() throws Exception {
     OrganizationDto organization = db.organizations().insert();
     PermissionTemplateDto template = insertTemplateAndAssociatedPermissions(organization);
-    db.organizations().setDefaultTemplates(organization, "project def template uuid", template.getUuid());
+    db.organizations().setDefaultTemplates(db.permissionTemplates().insertTemplate(organization), template);
     loginAsAdmin(organization);
 
     expectedException.expect(BadRequestException.class);
@@ -227,13 +236,14 @@ public class DeleteTemplateActionTest {
   @Test
   public void default_template_for_views_can_be_deleted_by_uuid_if_views_is_not_installed_and_default_template_for_views_is_reset() throws Exception {
     OrganizationDto organization = db.organizations().insert();
-    PermissionTemplateDto template = insertTemplateAndAssociatedPermissions(organization);
-    db.organizations().setDefaultTemplates(organization, "project def template uuid", template.getUuid());
+    PermissionTemplateDto projectTemplate = db.permissionTemplates().insertTemplate(organization);
+    PermissionTemplateDto viewTemplate = insertTemplateAndAssociatedPermissions(organization);
+    db.organizations().setDefaultTemplates(projectTemplate, viewTemplate);
     loginAsAdmin(organization);
 
-    newRequestByUuid(this.underTestWithoutViews, template.getUuid());
+    newRequestByUuid(this.underTestWithoutViews, viewTemplate.getUuid());
 
-    assertTemplateDoesNotExist(template);
+    assertTemplateDoesNotExist(viewTemplate);
 
     assertThat(db.getDbClient().organizationDao().getDefaultTemplates(db.getSession(), organization.getUuid())
       .get().getViewUuid())
index a5decef047c17b283ae98ed5d1db525f08c2ed4e..4d53a40b7d60a0b3f37747f65701628bff92502f 100644 (file)
@@ -103,7 +103,7 @@ public class SearchTemplatesActionTest extends BasePermissionWsTest<SearchTempla
     addGroupToTemplate(viewsTemplate.getId(), group2.getId(), UserRole.ISSUE_ADMIN);
     addGroupToTemplate(viewsTemplate.getId(), group3.getId(), UserRole.ISSUE_ADMIN);
 
-    db.organizations().setDefaultTemplates(organization, projectTemplate.getUuid(), viewsTemplate.getUuid());
+    db.organizations().setDefaultTemplates(projectTemplate, viewsTemplate);
 
     String result = newRequest().execute().getInput();
 
@@ -156,7 +156,7 @@ public class SearchTemplatesActionTest extends BasePermissionWsTest<SearchTempla
 
   @Test
   public void search_by_name_in_default_organization() {
-    db.organizations().setDefaultTemplates(db.getDefaultOrganization(), "foo", null);
+    db.organizations().setDefaultTemplates(db.permissionTemplates().insertTemplate(db.getDefaultOrganization()), null);
     insertProjectTemplate(db.getDefaultOrganization());
     insertViewsTemplate(db.getDefaultOrganization());
 
@@ -173,7 +173,8 @@ public class SearchTemplatesActionTest extends BasePermissionWsTest<SearchTempla
   @Test
   public void search_in_organization() throws Exception {
     OrganizationDto org = db.organizations().insert();
-    db.organizations().setDefaultTemplates(org, "foo", null);
+    PermissionTemplateDto projectDefaultTemplate = db.permissionTemplates().insertTemplate(org);
+    db.organizations().setDefaultTemplates(projectDefaultTemplate, null);
     PermissionTemplateDto templateInOrg = insertProjectTemplate(org);
     insertProjectTemplate(db.getDefaultOrganization());
     db.commit();
@@ -186,8 +187,10 @@ public class SearchTemplatesActionTest extends BasePermissionWsTest<SearchTempla
         .execute()
         .getInputStream());
 
-    assertThat(result.getPermissionTemplatesCount()).isEqualTo(1);
-    assertThat(result.getPermissionTemplates(0).getId()).isEqualTo(templateInOrg.getUuid());
+    assertThat(result.getPermissionTemplatesCount()).isEqualTo(2);
+    assertThat(result.getPermissionTemplatesList())
+        .extracting(WsPermissions.PermissionTemplate::getId)
+        .containsOnly(projectDefaultTemplate.getUuid(), templateInOrg.getUuid());
   }
 
   @Test
@@ -200,7 +203,7 @@ public class SearchTemplatesActionTest extends BasePermissionWsTest<SearchTempla
 
   @Test
   public void display_all_project_permissions() {
-    db.organizations().setDefaultTemplates(db.getDefaultOrganization(), "foo", "bar");
+    db.organizations().setDefaultTemplates(db.permissionTemplates().insertTemplate(db.getDefaultOrganization()), null);
 
     String result = newRequest().execute().getInput();
 
index eec5cdd3f8cb46f2707ee9d65c558b66df9e9c17..ce5e5b703ba335713baba62cbe0c2aa818fb6a00 100644 (file)
@@ -57,13 +57,15 @@ public class SetDefaultTemplateActionTest extends BasePermissionWsTest<SetDefaul
 
   @Test
   public void update_project_default_template() throws Exception {
-    db.organizations().setDefaultTemplates(db.getDefaultOrganization(), "project-template-uuid", "view-template-uuid");
+    PermissionTemplateDto viewDefaultTemplate = db.permissionTemplates().insertTemplate(db.getDefaultOrganization());
+    db.organizations().setDefaultTemplates(db.permissionTemplates().insertTemplate(db.getDefaultOrganization()),
+        viewDefaultTemplate);
     PermissionTemplateDto template = insertTemplate(db.getDefaultOrganization());
     loginAsAdmin(db.getDefaultOrganization());
 
     newRequest(template.getUuid(), Qualifiers.PROJECT);
 
-    assertDefaultTemplates(db.getDefaultOrganization(), template.getUuid(), "view-template-uuid");
+    assertDefaultTemplates(db.getDefaultOrganization(), template.getUuid(), viewDefaultTemplate.getUuid());
   }
 
   @Test
@@ -82,7 +84,9 @@ public class SetDefaultTemplateActionTest extends BasePermissionWsTest<SetDefaul
   @Test
   public void update_project_default_template_by_template_name() throws Exception {
     OrganizationDto organization = db.organizations().insert();
-    db.organizations().setDefaultTemplates(organization, "bar", "roh");
+    PermissionTemplateDto viewDefaultTemplate = db.permissionTemplates().insertTemplate(organization);
+    db.organizations().setDefaultTemplates(db.permissionTemplates().insertTemplate(organization),
+        viewDefaultTemplate);
     PermissionTemplateDto template = insertTemplate(organization);
     loginAsAdmin(organization);
 
@@ -92,19 +96,20 @@ public class SetDefaultTemplateActionTest extends BasePermissionWsTest<SetDefaul
       .execute();
     db.getSession().commit();
 
-    assertDefaultTemplates(organization, template.getUuid(), "roh");
+    assertDefaultTemplates(organization, template.getUuid(), viewDefaultTemplate.getUuid());
   }
 
   @Test
   public void update_view_default_template() throws Exception {
     OrganizationDto organization = db.organizations().insert();
-    db.organizations().setDefaultTemplates(organization, "foo", null);
+    PermissionTemplateDto projectDefaultTemplate = db.permissionTemplates().insertTemplate(organization);
+    db.organizations().setDefaultTemplates(projectDefaultTemplate, null);
     PermissionTemplateDto template = insertTemplate(organization);
     loginAsAdmin(organization);
 
     newRequest(template.getUuid(), VIEW);
 
-    assertDefaultTemplates(organization, "foo", template.getUuid());
+    assertDefaultTemplates(organization, projectDefaultTemplate.getUuid(), template.getUuid());
   }
 
   @Test
index e917164c62c1bb6f9e8087e9b58d64a4fcb7350f..f3b74a7352fd34cdaa26002a7f89b6dc5943c0a8 100644 (file)
@@ -101,10 +101,10 @@ public class RegisterPermissionTemplatesTest {
 
   @Test
   public void do_not_fail_if_default_template_exists_and_is_registered() {
-    db.permissionTemplates().insertTemplate(newPermissionTemplateDto()
-      .setOrganizationUuid(db.getDefaultOrganization().getUuid())
-      .setUuid(DEFAULT_TEMPLATE_UUID));
-    db.organizations().setDefaultTemplates(db.getDefaultOrganization(), DEFAULT_TEMPLATE_UUID, null);
+    PermissionTemplateDto projectTemplate = db.permissionTemplates().insertTemplate(newPermissionTemplateDto()
+        .setOrganizationUuid(db.getDefaultOrganization().getUuid())
+        .setUuid(DEFAULT_TEMPLATE_UUID));
+    db.organizations().setDefaultTemplates(projectTemplate, null);
 
     underTest.start();
 
index 15f8c3126265d9b1c4e96a88128eee1c86e50fd7..ee67f83555bcb54185301bb4fc79ac82afec6292 100644 (file)
@@ -22,6 +22,9 @@ package org.sonar.db.organization;
 import javax.annotation.Nullable;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
+import org.sonar.db.permission.template.PermissionTemplateDto;
+
+import static com.google.common.base.Preconditions.checkArgument;
 
 public class OrganizationDbTester {
   private final DbTester dbTester;
@@ -55,10 +58,24 @@ public class OrganizationDbTester {
     return dto;
   }
 
+  public void setDefaultTemplates(PermissionTemplateDto projectDefaultTemplate, @Nullable PermissionTemplateDto viewDefaultTemplate) {
+    checkArgument(viewDefaultTemplate == null
+      || viewDefaultTemplate.getOrganizationUuid().equals(projectDefaultTemplate.getOrganizationUuid()),
+      "default template for project and view must belong to the same organization");
+
+    DbSession dbSession = dbTester.getSession();
+    dbTester.getDbClient().organizationDao().setDefaultTemplates(dbSession, projectDefaultTemplate.getOrganizationUuid(),
+      new DefaultTemplates()
+        .setProjectUuid(projectDefaultTemplate.getUuid())
+        .setViewUuid(viewDefaultTemplate == null ? null : viewDefaultTemplate.getUuid()));
+    dbSession.commit();
+  }
+
   public void setDefaultTemplates(OrganizationDto defaultOrganization,
     String projectDefaultTemplateUuid, @Nullable String viewDefaultTemplateUuid) {
     DbSession dbSession = dbTester.getSession();
-    dbTester.getDbClient().organizationDao().setDefaultTemplates(dbSession, defaultOrganization.getUuid(), new DefaultTemplates().setProjectUuid(projectDefaultTemplateUuid).setViewUuid(viewDefaultTemplateUuid));
+    dbTester.getDbClient().organizationDao().setDefaultTemplates(dbSession, defaultOrganization.getUuid(),
+      new DefaultTemplates().setProjectUuid(projectDefaultTemplateUuid).setViewUuid(viewDefaultTemplateUuid));
     dbSession.commit();
   }