]> source.dussan.org Git - sonarqube.git/commitdiff
Add tests to MyBatis RoleMapper
authorSimon Brandhof <simon.brandhof@gmail.com>
Fri, 6 Jul 2012 17:12:26 +0000 (19:12 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Fri, 6 Jul 2012 17:12:26 +0000 (19:12 +0200)
sonar-core/src/test/java/org/sonar/core/user/RoleMapperTest.java [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/user/RoleMapperTest/countRoles.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/user/RoleMapperTest/deleteRolesByResourceId-result.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/user/RoleMapperTest/deleteRolesByResourceId.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/user/RoleMapperTest/insertRoles-result.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/user/RoleMapperTest/insertRoles.xml [new file with mode: 0644]

diff --git a/sonar-core/src/test/java/org/sonar/core/user/RoleMapperTest.java b/sonar-core/src/test/java/org/sonar/core/user/RoleMapperTest.java
new file mode 100644 (file)
index 0000000..2c26969
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.core.user;
+
+import org.apache.ibatis.session.SqlSession;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.core.persistence.AbstractDaoTestCase;
+import org.sonar.core.persistence.MyBatis;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class RoleMapperTest extends AbstractDaoTestCase {
+
+  private SqlSession session;
+
+  @Before
+  public void openSession() {
+    session = getMyBatis().openSession();
+  }
+
+  @After
+  public void closeSession() {
+    MyBatis.closeQuietly(session);
+  }
+
+  @Test
+  public void countRoles() {
+    setupData("countRoles");
+
+    RoleMapper mapper = session.getMapper(RoleMapper.class);
+    assertThat(mapper.countGroupRoles(123L)).isEqualTo(2);
+    assertThat(mapper.countUserRoles(123L)).isEqualTo(1);
+  }
+
+  @Test
+  public void deleteRolesByResourceId() {
+    setupData("deleteRolesByResourceId");
+
+    RoleMapper mapper = session.getMapper(RoleMapper.class);
+    mapper.deleteGroupRolesByResourceId(123L);
+    mapper.deleteUserRolesByResourceId(123L);
+    session.commit();
+
+
+    checkTables("deleteRolesByResourceId", "group_roles", "user_roles");
+  }
+
+  @Test
+  public void insertRoles() {
+    setupData("insertRoles");
+
+    RoleMapper mapper = session.getMapper(RoleMapper.class);
+    mapper.insertGroupRole(new GroupRoleDto().setRole("admin").setGroupId(100L).setResourceId(123L));
+    mapper.insertGroupRole(new GroupRoleDto().setRole("user").setResourceId(123L));// Anyone
+    mapper.insertUserRole(new UserRoleDto().setRole("codeviewer").setUserId(200L).setResourceId(123L));// Anyone
+    session.commit();
+
+    checkTables("insertRoles", "group_roles", "user_roles");
+  }
+}
diff --git a/sonar-core/src/test/resources/org/sonar/core/user/RoleMapperTest/countRoles.xml b/sonar-core/src/test/resources/org/sonar/core/user/RoleMapperTest/countRoles.xml
new file mode 100644 (file)
index 0000000..ec1ed3b
--- /dev/null
@@ -0,0 +1,14 @@
+<dataset>
+  <groups id="100" name="sonar-administrators"/>
+  <groups id="101" name="sonar-users"/>
+  <users id="200" login="marius" name="Marius" email="[null]" active="[true]"/>
+
+  <group_roles id="1" group_id="100" resource_id="123" role="admin"/>
+  <group_roles id="2" group_id="[null]" resource_id="123" role="user"/>
+  <user_roles id="1" user_id="200" resource_id="123" role="codeviewer"/>
+
+  <!-- other resource -->
+  <group_roles id="3" group_id="101" resource_id="999" role="codeviewer"/>
+  <user_roles id="2" user_id="200" resource_id="999" role="codeviewer"/>
+
+</dataset>
\ No newline at end of file
diff --git a/sonar-core/src/test/resources/org/sonar/core/user/RoleMapperTest/deleteRolesByResourceId-result.xml b/sonar-core/src/test/resources/org/sonar/core/user/RoleMapperTest/deleteRolesByResourceId-result.xml
new file mode 100644 (file)
index 0000000..dae82dd
--- /dev/null
@@ -0,0 +1,14 @@
+<dataset>
+  <groups id="100" name="sonar-administrators"/>
+  <groups id="101" name="sonar-users"/>
+  <users id="200" login="marius" name="Marius" email="[null]" active="[true]"/>
+
+  <!--<group_roles id="1" group_id="100" resource_id="123" role="admin"/>-->
+  <!--<group_roles id="2" group_id="[null]" resource_id="123" role="user"/>-->
+  <!--<user_roles id="1" user_id="200" resource_id="123" role="codeviewer"/>-->
+
+  <!-- other resource -->
+  <group_roles id="3" group_id="101" resource_id="999" role="codeviewer"/>
+  <user_roles id="2" user_id="200" resource_id="999" role="codeviewer"/>
+
+</dataset>
\ No newline at end of file
diff --git a/sonar-core/src/test/resources/org/sonar/core/user/RoleMapperTest/deleteRolesByResourceId.xml b/sonar-core/src/test/resources/org/sonar/core/user/RoleMapperTest/deleteRolesByResourceId.xml
new file mode 100644 (file)
index 0000000..ec1ed3b
--- /dev/null
@@ -0,0 +1,14 @@
+<dataset>
+  <groups id="100" name="sonar-administrators"/>
+  <groups id="101" name="sonar-users"/>
+  <users id="200" login="marius" name="Marius" email="[null]" active="[true]"/>
+
+  <group_roles id="1" group_id="100" resource_id="123" role="admin"/>
+  <group_roles id="2" group_id="[null]" resource_id="123" role="user"/>
+  <user_roles id="1" user_id="200" resource_id="123" role="codeviewer"/>
+
+  <!-- other resource -->
+  <group_roles id="3" group_id="101" resource_id="999" role="codeviewer"/>
+  <user_roles id="2" user_id="200" resource_id="999" role="codeviewer"/>
+
+</dataset>
\ No newline at end of file
diff --git a/sonar-core/src/test/resources/org/sonar/core/user/RoleMapperTest/insertRoles-result.xml b/sonar-core/src/test/resources/org/sonar/core/user/RoleMapperTest/insertRoles-result.xml
new file mode 100644 (file)
index 0000000..7a7e6f8
--- /dev/null
@@ -0,0 +1,11 @@
+<dataset>
+  <groups id="100" name="sonar-administrators"/>
+  <groups id="101" name="sonar-users"/>
+  <users id="200" login="marius" name="Marius" email="[null]" active="[true]"/>
+
+  <group_roles id="1" group_id="100" resource_id="123" role="admin"/>
+  <group_roles id="2" group_id="[null]" resource_id="123" role="user"/>
+  <user_roles id="1" user_id="200" resource_id="123" role="codeviewer"/>
+
+
+</dataset>
\ No newline at end of file
diff --git a/sonar-core/src/test/resources/org/sonar/core/user/RoleMapperTest/insertRoles.xml b/sonar-core/src/test/resources/org/sonar/core/user/RoleMapperTest/insertRoles.xml
new file mode 100644 (file)
index 0000000..b99df83
--- /dev/null
@@ -0,0 +1,5 @@
+<dataset>
+  <groups id="100" name="sonar-administrators"/>
+  <groups id="101" name="sonar-users"/>
+  <users id="200" login="marius" name="Marius" email="[null]" active="[true]"/>
+</dataset>
\ No newline at end of file