]> source.dussan.org Git - sonarqube.git/commitdiff
Migrated unit tests for components permissions to ComponentPermissionFacade
authorJean-Baptiste Vilain <jean-baptiste.vilain@sonarsource.com>
Fri, 19 Jul 2013 08:29:52 +0000 (10:29 +0200)
committerJean-Baptiste Vilain <jean-baptiste.vilain@sonarsource.com>
Fri, 19 Jul 2013 08:29:52 +0000 (10:29 +0200)
12 files changed:
sonar-core/src/test/java/org/sonar/core/permission/ComponentPermissionFacadeTest.java
sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_add_group_permission-result.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_add_group_permission.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_add_user_permission-result.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_add_user_permission.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_count_component_permissions.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_remove_all_permissions-result.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_remove_all_permissions.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_set_group_permission-result.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_set_group_permission.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_set_user_permission-result.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/permission/ComponentPermissionFacadeTest/should_set_user_permission.xml [new file with mode: 0644]

index a3d67f787db8ab4a69dc1003227f1da732cdd238..3763d3d60431bf9681e6c44fb05ae904b4929d06 100644 (file)
 
 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 (file)
index 0000000..c984ad5
--- /dev/null
@@ -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 (file)
index 0000000..edca013
--- /dev/null
@@ -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 (file)
index 0000000..542cb23
--- /dev/null
@@ -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 (file)
index 0000000..2097c30
--- /dev/null
@@ -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 (file)
index 0000000..890aec9
--- /dev/null
@@ -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 (file)
index 0000000..38def46
--- /dev/null
@@ -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 (file)
index 0000000..890aec9
--- /dev/null
@@ -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 (file)
index 0000000..e4e1c62
--- /dev/null
@@ -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 (file)
index 0000000..06f0143
--- /dev/null
@@ -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 (file)
index 0000000..542cb23
--- /dev/null
@@ -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 (file)
index 0000000..2097c30
--- /dev/null
@@ -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