aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core
diff options
context:
space:
mode:
authorJean-Baptiste Vilain <jean-baptiste.vilain@sonarsource.com>2013-07-19 10:29:52 +0200
committerJean-Baptiste Vilain <jean-baptiste.vilain@sonarsource.com>2013-07-19 10:29:52 +0200
commitdc4e5f4997a7a24c44065537b25eee1a6ef5630f (patch)
tree409d825b1a96fb480ca1b2f45d5df7ed8528f273 /sonar-core
parent1dc832879c260753d804eaba88084e02acfe41db (diff)
downloadsonarqube-dc4e5f4997a7a24c44065537b25eee1a6ef5630f.tar.gz
sonarqube-dc4e5f4997a7a24c44065537b25eee1a6ef5630f.zip
Migrated unit tests for components permissions to ComponentPermissionFacade
Diffstat (limited to 'sonar-core')
-rw-r--r--sonar-core/src/test/java/org/sonar/core/permission/ComponentPermissionFacadeTest.java108
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_add_group_permission-result.xml8
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_add_group_permission.xml7
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_add_user_permission-result.xml8
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_add_user_permission.xml7
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_count_component_permissions.xml11
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_remove_all_permissions-result.xml11
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_remove_all_permissions.xml11
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_set_group_permission-result.xml8
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_set_group_permission.xml7
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_set_user_permission-result.xml8
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_set_user_permission.xml7
12 files changed, 201 insertions, 0 deletions
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 @@
+<dataset>
+
+ <groups id="100" name="devs"/>
+
+ <group_roles group_id="100" resource_id="123" role="admin"/>
+ <group_roles group_id="100" resource_id="123" role="user"/>
+
+</dataset> \ 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 @@
+<dataset>
+
+ <groups id="100" name="devs"/>
+
+ <group_roles group_id="100" resource_id="123" role="admin"/>
+
+</dataset> \ 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 @@
+<dataset>
+
+ <users id="200" login="dave.loper" name="Dave Loper" email="dave.loper@company.net" active="[true]"/>
+
+ <user_roles user_id="200" resource_id="123" role="user"/>
+ <user_roles user_id="200" resource_id="123" role="admin"/>
+
+</dataset> \ 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 @@
+<dataset>
+
+ <users id="200" login="dave.loper" name="Dave Loper" email="dave.loper@company.net" active="[true]"/>
+
+ <user_roles user_id="200" resource_id="123" role="user"/>
+
+</dataset> \ 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 @@
+<dataset>
+
+ <users id="200" login="dave.loper" name="Dave Loper" email="dave.loper@company.net" active="[true]"/>
+
+ <groups id="100" name="devs"/>
+
+ <user_roles user_id="200" resource_id="123" role="user"/>
+
+ <group_roles group_id="100" resource_id="123" role="codeviewer"/>
+
+</dataset> \ 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 @@
+<dataset>
+
+ <users id="200" login="dave.loper" name="Dave Loper" email="dave.loper@company.net" active="[true]"/>
+
+ <groups id="100" name="devs"/>
+
+ <user_roles/>
+
+ <group_roles/>
+
+</dataset> \ 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 @@
+<dataset>
+
+ <users id="200" login="dave.loper" name="Dave Loper" email="dave.loper@company.net" active="[true]"/>
+
+ <groups id="100" name="devs"/>
+
+ <user_roles user_id="200" resource_id="123" role="user"/>
+
+ <group_roles group_id="100" resource_id="123" role="codeviewer"/>
+
+</dataset> \ 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 @@
+<dataset>
+
+ <groups id="100" name="devs"/>
+
+ <group_roles group_id="100" resource_id="123" role="codeviewer"/>
+ <group_roles group_id="100" resource_id="123" role="admin"/>
+
+</dataset> \ 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 @@
+<dataset>
+
+ <groups id="100" name="devs"/>
+
+ <group_roles group_id="100" resource_id="123" role="codeviewer"/>
+
+</dataset> \ 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 @@
+<dataset>
+
+ <users id="200" login="dave.loper" name="Dave Loper" email="dave.loper@company.net" active="[true]"/>
+
+ <user_roles user_id="200" resource_id="123" role="user"/>
+ <user_roles user_id="200" resource_id="123" role="admin"/>
+
+</dataset> \ 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 @@
+<dataset>
+
+ <users id="200" login="dave.loper" name="Dave Loper" email="dave.loper@company.net" active="[true]"/>
+
+ <user_roles user_id="200" resource_id="123" role="user"/>
+
+</dataset> \ No newline at end of file