blob: b46ff3a5199f3d7758676bef5dfa546071cd4e88 [file] [log] [blame]
margaretha318fec32017-10-24 12:11:58 +02001package de.ids_mannheim.korap.dao;
margaretha1f106f62017-10-18 22:27:30 +02002
Marc Kupietzd43a98d2023-09-22 17:11:46 +02003import static org.junit.jupiter.api.Assertions.assertEquals;
4import static org.junit.jupiter.api.Assertions.assertThrows;
margaretha1f106f62017-10-18 22:27:30 +02005
margarethaf6d5a822017-10-19 19:51:20 +02006import java.util.Iterator;
margaretha1f106f62017-10-18 22:27:30 +02007import java.util.List;
8
margaretha6e796842023-08-17 15:10:45 +02009import jakarta.persistence.PersistenceException;
margarethaf7abb362018-09-18 20:09:37 +020010
Marc Kupietzd43a98d2023-09-22 17:11:46 +020011import org.junit.jupiter.api.Test;
margaretha1f106f62017-10-18 22:27:30 +020012import org.springframework.beans.factory.annotation.Autowired;
13
margaretha85273f12019-02-04 18:13:17 +010014import de.ids_mannheim.korap.config.SpringJerseyTest;
Akronda080152020-12-03 13:53:29 +010015import de.ids_mannheim.korap.constant.QueryType;
16import de.ids_mannheim.korap.constant.ResourceType;
margarethab097fb02021-02-22 19:28:33 +010017import de.ids_mannheim.korap.entity.QueryDO;
margaretha1f106f62017-10-18 22:27:30 +020018import de.ids_mannheim.korap.exceptions.KustvaktException;
margaretha9f2cff02022-01-03 15:42:45 +010019import de.ids_mannheim.korap.exceptions.StatusCodes;
margaretha61966dd2017-10-26 19:32:21 +020020import de.ids_mannheim.korap.user.User;
margaretha1f106f62017-10-18 22:27:30 +020021
margaretha652c4dc2021-02-12 17:07:44 +010022public class VirtualCorpusDaoTest extends SpringJerseyTest {
margaretha1f106f62017-10-18 22:27:30 +020023
24 @Autowired
margarethab097fb02021-02-22 19:28:33 +010025 private QueryDao dao;
margarethaf6d5a822017-10-19 19:51:20 +020026
margaretha45dde682018-01-04 21:33:46 +010027 @Test
margaretha35e1ca22023-11-16 22:00:01 +010028 public void testListVCByType () throws KustvaktException {
29 List<QueryDO> vcList = dao.retrieveQueryByType(ResourceType.PUBLISHED,
30 null, QueryType.VIRTUAL_CORPUS);
margaretha45dde682018-01-04 21:33:46 +010031 assertEquals(1, vcList.size());
margarethab097fb02021-02-22 19:28:33 +010032 QueryDO vc = vcList.get(0);
margaretha45dde682018-01-04 21:33:46 +010033 assertEquals(4, vc.getId());
Marc Kupietzd43a98d2023-09-22 17:11:46 +020034 assertEquals(vc.getName(), "published-vc");
35 assertEquals(vc.getCreatedBy(), "marlin");
margaretha45dde682018-01-04 21:33:46 +010036 }
margaretha1f106f62017-10-18 22:27:30 +020037
38 @Test
margaretha35e1ca22023-11-16 22:00:01 +010039 public void testSystemVC () throws KustvaktException {
margarethaf6d5a822017-10-19 19:51:20 +020040 // insert vc
margaretha35e1ca22023-11-16 22:00:01 +010041 int id = dao.createQuery("system-vc", ResourceType.SYSTEM,
42 QueryType.VIRTUAL_CORPUS, User.CorpusAccess.FREE,
43 "corpusSigle=GOE", "definition", "description", "experimental",
44 false, "test class", null, null);
margarethaf6d5a822017-10-19 19:51:20 +020045 // select vc
margaretha35e1ca22023-11-16 22:00:01 +010046 List<QueryDO> vcList = dao.retrieveQueryByType(ResourceType.SYSTEM,
47 null, QueryType.VIRTUAL_CORPUS);
margarethaf6d5a822017-10-19 19:51:20 +020048 assertEquals(2, vcList.size());
margarethab097fb02021-02-22 19:28:33 +010049 QueryDO vc = dao.retrieveQueryById(id);
margarethaf6d5a822017-10-19 19:51:20 +020050 // delete vc
margarethab097fb02021-02-22 19:28:33 +010051 dao.deleteQuery(vc);
margarethaf6d5a822017-10-19 19:51:20 +020052 // check if vc has been deleted
margaretha35e1ca22023-11-16 22:00:01 +010053 KustvaktException exception = assertThrows(KustvaktException.class,
54 () -> {
55 dao.retrieveQueryById(id);
56 });
57 assertEquals(StatusCodes.NO_RESOURCE_FOUND,
58 exception.getStatusCode().intValue());
margaretha1f106f62017-10-18 22:27:30 +020059 }
60
margarethaf7abb362018-09-18 20:09:37 +020061 @Test
62 public void testNonUniqueVC () throws KustvaktException {
margaretha35e1ca22023-11-16 22:00:01 +010063
64 PersistenceException exception = assertThrows(
65 PersistenceException.class, () -> {
66 dao.createQuery("system-vc", ResourceType.SYSTEM,
67 QueryType.VIRTUAL_CORPUS, User.CorpusAccess.FREE,
68 "corpusSigle=GOE", "definition", "description",
69 "experimental", false, "system", null, null);
70 });
71
margaretha93bfbea2023-11-06 21:09:21 +010072 assertEquals(exception.getMessage(),
73 "Converting `org.hibernate.exception.GenericJDBCException` "
margaretha35e1ca22023-11-16 22:00:01 +010074 + "to JPA `PersistenceException` : could not execute statement");
margarethaf7abb362018-09-18 20:09:37 +020075 }
margaretha1f106f62017-10-18 22:27:30 +020076
77 @Test
margaretha35e1ca22023-11-16 22:00:01 +010078 public void retrieveSystemVC () throws KustvaktException {
79 List<QueryDO> vc = dao.retrieveQueryByType(ResourceType.SYSTEM, null,
80 QueryType.VIRTUAL_CORPUS);
margarethaf6d5a822017-10-19 19:51:20 +020081 assertEquals(1, vc.size());
margaretha1f106f62017-10-18 22:27:30 +020082 }
83
margarethaf7abb362018-09-18 20:09:37 +020084 /**
85 * retrieve private and group VC
Marc Kupietzd43a98d2023-09-22 17:11:46 +020086 *
margarethaf6d5a822017-10-19 19:51:20 +020087 * @throws KustvaktException
88 */
margaretha1f106f62017-10-18 22:27:30 +020089 @Test
margaretha35e1ca22023-11-16 22:00:01 +010090 public void retrieveVCByUserDory () throws KustvaktException {
91 List<QueryDO> virtualCorpora = dao.retrieveQueryByUser("dory",
92 QueryType.VIRTUAL_CORPUS);
margaretha340d06a2024-07-25 12:14:42 +020093 assertEquals(3, virtualCorpora.size());
margaretha98ec15b2018-01-22 17:14:02 +010094 // ordered by id
margarethab097fb02021-02-22 19:28:33 +010095 Iterator<QueryDO> i = virtualCorpora.iterator();
Marc Kupietzd43a98d2023-09-22 17:11:46 +020096 assertEquals(i.next().getName(), "dory-vc");
97 assertEquals(i.next().getName(), "group-vc");
98 assertEquals(i.next().getName(), "system-vc");
margaretha1f106f62017-10-18 22:27:30 +020099 }
100
margarethaf7abb362018-09-18 20:09:37 +0200101 /**
102 * retrieves group VC and
103 * excludes hidden published VC (user has never used it)
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200104 *
margarethaf6d5a822017-10-19 19:51:20 +0200105 * @throws KustvaktException
106 */
margaretha1f106f62017-10-18 22:27:30 +0200107 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100108 public void retrieveVCByUserNemo () throws KustvaktException {
109 List<QueryDO> virtualCorpora = dao.retrieveQueryByUser("nemo",
110 QueryType.VIRTUAL_CORPUS);
margaretha340d06a2024-07-25 12:14:42 +0200111 assertEquals(2, virtualCorpora.size());
margarethab097fb02021-02-22 19:28:33 +0100112 Iterator<QueryDO> i = virtualCorpora.iterator();
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200113 assertEquals(i.next().getName(), "system-vc");
114 assertEquals(i.next().getName(), "nemo-vc");
margaretha1f106f62017-10-18 22:27:30 +0200115 }
116
margarethaf7abb362018-09-18 20:09:37 +0200117 /**
118 * retrieves published VC by the owner and
119 * excludes group vc when a user is a pending member
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200120 *
margarethaf6d5a822017-10-19 19:51:20 +0200121 * @throws KustvaktException
122 */
123 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100124 public void retrieveVCByUserMarlin () throws KustvaktException {
125 List<QueryDO> virtualCorpora = dao.retrieveQueryByUser("marlin",
126 QueryType.VIRTUAL_CORPUS);
margaretha98ec15b2018-01-22 17:14:02 +0100127 assertEquals(3, virtualCorpora.size());
margarethab097fb02021-02-22 19:28:33 +0100128 Iterator<QueryDO> i = virtualCorpora.iterator();
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200129 assertEquals(i.next().getName(), "system-vc");
130 assertEquals(i.next().getName(), "published-vc");
131 assertEquals(i.next().getName(), "marlin-vc");
margarethaf6d5a822017-10-19 19:51:20 +0200132 }
133
margarethaf7abb362018-09-18 20:09:37 +0200134 /**
135 * retrieves published VC from an auto-generated hidden group and
136 * excludes group vc when a user is a deleted member
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200137 *
margarethaf6d5a822017-10-19 19:51:20 +0200138 * @throws KustvaktException
139 */
140 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100141 public void retrieveVCByUserPearl () throws KustvaktException {
142 List<QueryDO> virtualCorpora = dao.retrieveQueryByUser("pearl",
143 QueryType.VIRTUAL_CORPUS);
margaretha340d06a2024-07-25 12:14:42 +0200144 assertEquals(1, virtualCorpora.size());
margarethab097fb02021-02-22 19:28:33 +0100145 Iterator<QueryDO> i = virtualCorpora.iterator();
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200146 assertEquals(i.next().getName(), "system-vc");
margaretha1f106f62017-10-18 22:27:30 +0200147 }
margaretha1f106f62017-10-18 22:27:30 +0200148}