| margaretha | e4be1e6 | 2024-07-31 12:58:56 +0200 | [diff] [blame^] | 1 | package de.ids_mannheim.korap.dao; |
| 2 | |
| 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| 6 | |
| 7 | import org.junit.jupiter.api.Disabled; |
| 8 | import org.junit.jupiter.api.Test; |
| 9 | import org.junit.jupiter.api.extension.ExtendWith; |
| 10 | import org.springframework.beans.factory.annotation.Autowired; |
| 11 | import org.springframework.test.context.ContextConfiguration; |
| 12 | import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 13 | import org.sqlite.SQLiteException; |
| 14 | |
| 15 | import de.ids_mannheim.korap.constant.PredefinedRole; |
| 16 | import de.ids_mannheim.korap.constant.PrivilegeType; |
| 17 | import de.ids_mannheim.korap.entity.QueryDO; |
| 18 | import de.ids_mannheim.korap.entity.Role; |
| 19 | import de.ids_mannheim.korap.entity.UserGroup; |
| 20 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| 21 | import jakarta.persistence.PersistenceException; |
| 22 | |
| 23 | @Disabled |
| 24 | @ExtendWith(SpringExtension.class) |
| 25 | @ContextConfiguration("classpath:test-config.xml") |
| 26 | public class RoleDaoTest extends DaoTestBase { |
| 27 | |
| 28 | @Autowired |
| 29 | private RoleDao roleDao; |
| 30 | @Autowired |
| 31 | private QueryDao queryDao; |
| 32 | |
| 33 | @Test |
| 34 | public void testUniqueRoleWithoutQuery () throws KustvaktException { |
| 35 | UserGroup group = createDoryGroup(); |
| 36 | |
| 37 | Role r = new Role(PredefinedRole.GROUP_ADMIN, PrivilegeType.READ_MEMBER, |
| 38 | group); |
| 39 | |
| 40 | Exception exception = assertThrows(PersistenceException.class, () -> { |
| 41 | roleDao.addRole(r); |
| 42 | }); |
| 43 | |
| 44 | Throwable rootCause = exception; |
| 45 | while (rootCause.getCause() != null) { |
| 46 | rootCause = rootCause.getCause(); |
| 47 | } |
| 48 | |
| 49 | assertEquals(SQLiteException.class, rootCause.getClass()); |
| 50 | assertTrue(rootCause.getMessage() |
| 51 | .startsWith("[SQLITE_CONSTRAINT_UNIQUE]")); |
| 52 | |
| 53 | deleteUserGroup(group.getId(), "dory"); |
| 54 | } |
| 55 | |
| 56 | @Test |
| 57 | public void testUniqueRoleWithQuery () throws KustvaktException { |
| 58 | QueryDO query = queryDao.retrieveQueryByName("dory-vc", "dory"); |
| 59 | |
| 60 | UserGroup group = createDoryGroup(); |
| 61 | |
| 62 | Role r1 = new Role(PredefinedRole.GROUP_ADMIN, |
| 63 | PrivilegeType.READ_MEMBER, group, query); |
| 64 | roleDao.addRole(r1); |
| 65 | |
| 66 | Role r2 = new Role(PredefinedRole.GROUP_ADMIN, |
| 67 | PrivilegeType.READ_MEMBER, group, query); |
| 68 | |
| 69 | Exception exception = assertThrows(PersistenceException.class, () -> { |
| 70 | roleDao.addRole(r2); |
| 71 | }); |
| 72 | |
| 73 | Throwable rootCause = exception; |
| 74 | while (rootCause.getCause() != null) { |
| 75 | rootCause = rootCause.getCause(); |
| 76 | } |
| 77 | |
| 78 | assertEquals(SQLiteException.class, rootCause.getClass()); |
| 79 | assertTrue(rootCause.getMessage() |
| 80 | .startsWith("[SQLITE_CONSTRAINT_UNIQUE]")); |
| 81 | |
| 82 | deleteUserGroup(group.getId(), "dory"); |
| 83 | } |
| 84 | } |