From 6793f543cc7b6d8a074dfa00d20c5706007d13a7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Mon, 30 Jan 2017 17:36:15 +0100 Subject: [PATCH] SONAR-8651 add and use OrganizationDbTester.setDefaultTemplates with dtos --- .../component/ComponentUpdaterTest.java | 122 +++++++++--------- .../PermissionTemplateServiceTest.java | 4 +- .../ws/template/DeleteTemplateActionTest.java | 40 +++--- .../template/SearchTemplatesActionTest.java | 15 ++- .../SetDefaultTemplateActionTest.java | 17 ++- .../RegisterPermissionTemplatesTest.java | 8 +- .../db/organization/OrganizationDbTester.java | 19 ++- 7 files changed, 130 insertions(+), 95 deletions(-) diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ComponentUpdaterTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ComponentUpdaterTest.java index d3ca2eea18f..5442ad996d3 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/ComponentUpdaterTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/ComponentUpdaterTest.java @@ -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); diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/PermissionTemplateServiceTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/PermissionTemplateServiceTest.java index 35386dca956..cad1b8e54d7 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/PermissionTemplateServiceTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/PermissionTemplateServiceTest.java @@ -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); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/DeleteTemplateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/DeleteTemplateActionTest.java index d6fbe605168..368888bef23 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/DeleteTemplateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/DeleteTemplateActionTest.java @@ -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()) diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/SearchTemplatesActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/SearchTemplatesActionTest.java index a5decef047c..4d53a40b7d6 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/SearchTemplatesActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/SearchTemplatesActionTest.java @@ -103,7 +103,7 @@ public class SearchTemplatesActionTest extends BasePermissionWsTest