From: Jean-Baptiste Vilain Date: Fri, 19 Jul 2013 08:29:52 +0000 (+0200) Subject: Migrated unit tests for components permissions to ComponentPermissionFacade X-Git-Tag: 3.7~45 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=dc4e5f4997a7a24c44065537b25eee1a6ef5630f;p=sonarqube.git Migrated unit tests for components permissions to ComponentPermissionFacade --- diff --git a/sonar-core/src/test/java/org/sonar/core/permission/ComponentPermissionFacadeTest.java b/sonar-core/src/test/java/org/sonar/core/permission/ComponentPermissionFacadeTest.java index a3d67f787db..3763d3d6043 100644 --- a/sonar-core/src/test/java/org/sonar/core/permission/ComponentPermissionFacadeTest.java +++ b/sonar-core/src/test/java/org/sonar/core/permission/ComponentPermissionFacadeTest.java @@ -20,14 +20,26 @@ package org.sonar.core.permission; +import org.apache.ibatis.session.SqlSession; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.web.UserRole; import org.sonar.core.persistence.AbstractDaoTestCase; +import org.sonar.core.persistence.MyBatis; import org.sonar.core.user.RoleDao; import org.sonar.core.user.UserDao; +import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + public class ComponentPermissionFacadeTest extends AbstractDaoTestCase { + @Rule + public ExpectedException throwable = ExpectedException.none(); + private ComponentPermissionFacade permissionFacade; private RoleDao roleDao; private UserDao userDao; @@ -50,4 +62,100 @@ public class ComponentPermissionFacadeTest extends AbstractDaoTestCase { checkTable("should_apply_permission_template", "group_roles", "group_id", "resource_id", "role"); checkTable("should_apply_permission_template", "user_roles", "group_id", "resource_id", "role"); } + + @Test + public void should_set_user_permission() throws Exception { + setupData("should_set_user_permission"); + + permissionFacade.setUserPermission(123L, "dave.loper", UserRole.ADMIN); + + checkTable("should_set_user_permission", "user_roles", "user_id", "resource_id", "role"); + } + + @Test + public void should_set_group_permission() throws Exception { + setupData("should_set_group_permission"); + + permissionFacade.setGroupPermission(123L, "devs", UserRole.ADMIN); + + checkTable("should_set_group_permission", "group_roles", "group_id", "resource_id", "role"); + } + + @Test + public void should_count_component_permissions() throws Exception { + setupData("should_count_component_permissions"); + + assertThat(permissionFacade.countPermissions(123L)).isEqualTo(2); + } + + @Test + public void should_add_user_permission() throws Exception { + setupData("should_add_user_permission"); + + SqlSession session = getMyBatis().openSession(); + try { + permissionFacade.addUserPermission(123L, "dave.loper", UserRole.ADMIN, session); + session.commit(); + } finally { + MyBatis.closeQuietly(session); + } + + checkTable("should_add_user_permission", "user_roles", "user_id", "resource_id", "role"); + } + + @Test + public void should_add_group_permission() throws Exception { + setupData("should_add_group_permission"); + + SqlSession session = getMyBatis().openSession(); + try { + permissionFacade.addGroupPermission(123L, "devs", UserRole.USER, session); + session.commit(); + } finally { + MyBatis.closeQuietly(session); + } + + checkTable("should_add_group_permission", "group_roles", "group_id", "resource_id", "role"); + } + + @Test + public void should_retrieve_permission_template() throws Exception { + PermissionTemplateDto permissionTemplateDto = new PermissionTemplateDto().setName("Test template"); + PermissionTemplateDto templateWithPermissions = new PermissionTemplateDto(); + permissionDao = mock(PermissionDao.class); + when(permissionDao.selectTemplateByKey("test_template")).thenReturn(permissionTemplateDto); + when(permissionDao.selectPermissionTemplate("Test template")).thenReturn(templateWithPermissions); + + permissionFacade = new ComponentPermissionFacade(null, null, null, permissionDao); + + PermissionTemplateDto permissionTemplate = permissionFacade.getPermissionTemplate("test_template"); + + assertThat(permissionTemplate).isSameAs(templateWithPermissions); + } + + @Test + public void should_fail_on_unmatched_template() throws Exception { + throwable.expect(IllegalArgumentException.class); + + permissionDao = mock(PermissionDao.class); + + permissionFacade = new ComponentPermissionFacade(null, null, null, permissionDao); + permissionFacade.getPermissionTemplate("unmatched"); + } + + @Test + public void should_remove_all_permissions() throws Exception { + setupData("should_remove_all_permissions"); + + SqlSession session = getMyBatis().openSession(); + try { + permissionFacade.removeAllPermissions(123L, session); + session.commit(); + } finally { + MyBatis.closeQuietly(session); + } + + checkTable("should_remove_all_permissions", "group_roles", "group_id", "resource_id", "role"); + checkTable("should_remove_all_permissions", "user_roles", "user_id", "resource_id", "role"); + } } diff --git a/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_add_group_permission-result.xml b/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_add_group_permission-result.xml new file mode 100644 index 00000000000..c984ad5c4bd --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_add_group_permission-result.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_add_group_permission.xml b/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_add_group_permission.xml new file mode 100644 index 00000000000..edca0139eb9 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_add_group_permission.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_add_user_permission-result.xml b/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_add_user_permission-result.xml new file mode 100644 index 00000000000..542cb239477 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_add_user_permission-result.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_add_user_permission.xml b/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_add_user_permission.xml new file mode 100644 index 00000000000..2097c30631b --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_add_user_permission.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_count_component_permissions.xml b/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_count_component_permissions.xml new file mode 100644 index 00000000000..890aec96d49 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_count_component_permissions.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_remove_all_permissions-result.xml b/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_remove_all_permissions-result.xml new file mode 100644 index 00000000000..38def462ed1 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_remove_all_permissions-result.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_remove_all_permissions.xml b/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_remove_all_permissions.xml new file mode 100644 index 00000000000..890aec96d49 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_remove_all_permissions.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_set_group_permission-result.xml b/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_set_group_permission-result.xml new file mode 100644 index 00000000000..e4e1c62f64d --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_set_group_permission-result.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_set_group_permission.xml b/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_set_group_permission.xml new file mode 100644 index 00000000000..06f0143fff9 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_set_group_permission.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_set_user_permission-result.xml b/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_set_user_permission-result.xml new file mode 100644 index 00000000000..542cb239477 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_set_user_permission-result.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_set_user_permission.xml b/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_set_user_permission.xml new file mode 100644 index 00000000000..2097c30631b --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_set_user_permission.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file