blob: 826a6875434e88e83f690a096ebd63882ebd7424 [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
margarethaf7abb362018-09-18 20:09:37 +02009import javax.persistence.PersistenceException;
10
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
Marc Kupietzd43a98d2023-09-22 17:11:46 +020028 public void testListVCByType() throws KustvaktException {
29 List<QueryDO> vcList = dao.retrieveQueryByType(ResourceType.PUBLISHED, null, QueryType.VIRTUAL_CORPUS);
margaretha45dde682018-01-04 21:33:46 +010030 assertEquals(1, vcList.size());
margarethab097fb02021-02-22 19:28:33 +010031 QueryDO vc = vcList.get(0);
margaretha45dde682018-01-04 21:33:46 +010032 assertEquals(4, vc.getId());
Marc Kupietzd43a98d2023-09-22 17:11:46 +020033 assertEquals(vc.getName(), "published-vc");
34 assertEquals(vc.getCreatedBy(), "marlin");
margaretha45dde682018-01-04 21:33:46 +010035 }
margaretha1f106f62017-10-18 22:27:30 +020036
37 @Test
Marc Kupietzd43a98d2023-09-22 17:11:46 +020038 public void testSystemVC() throws KustvaktException {
margarethaf6d5a822017-10-19 19:51:20 +020039 // insert vc
Marc Kupietzd43a98d2023-09-22 17:11:46 +020040 int id = dao.createQuery("system-vc", ResourceType.SYSTEM, QueryType.VIRTUAL_CORPUS, User.CorpusAccess.FREE, "corpusSigle=GOE", "definition", "description", "experimental", false, "test class", null, null);
margarethaf6d5a822017-10-19 19:51:20 +020041 // select vc
Marc Kupietzd43a98d2023-09-22 17:11:46 +020042 List<QueryDO> vcList = dao.retrieveQueryByType(ResourceType.SYSTEM, null, QueryType.VIRTUAL_CORPUS);
margarethaf6d5a822017-10-19 19:51:20 +020043 assertEquals(2, vcList.size());
margarethab097fb02021-02-22 19:28:33 +010044 QueryDO vc = dao.retrieveQueryById(id);
margarethaf6d5a822017-10-19 19:51:20 +020045 // delete vc
margarethab097fb02021-02-22 19:28:33 +010046 dao.deleteQuery(vc);
margarethaf6d5a822017-10-19 19:51:20 +020047 // check if vc has been deleted
Marc Kupietzd43a98d2023-09-22 17:11:46 +020048 KustvaktException exception = assertThrows(KustvaktException.class, () -> {
49 dao.retrieveQueryById(id);
50 });
51 assertEquals(StatusCodes.NO_RESOURCE_FOUND, exception.getStatusCode().intValue());
margaretha1f106f62017-10-18 22:27:30 +020052 }
53
margarethaf7abb362018-09-18 20:09:37 +020054 @Test
Marc Kupietzd43a98d2023-09-22 17:11:46 +020055 public void testNonUniqueVC() throws KustvaktException {
56 PersistenceException exception = assertThrows(PersistenceException.class, () -> {
57 dao.createQuery("system-vc", ResourceType.SYSTEM, QueryType.VIRTUAL_CORPUS, User.CorpusAccess.FREE, "corpusSigle=GOE", "definition", "description", "experimental", false, "system", null, null);
58 });
59 assertEquals(exception.getMessage(), "org.hibernate.exception.GenericJDBCException: could not execute statement");
margarethaf7abb362018-09-18 20:09:37 +020060 }
margaretha1f106f62017-10-18 22:27:30 +020061
62 @Test
Marc Kupietzd43a98d2023-09-22 17:11:46 +020063 public void retrieveSystemVC() throws KustvaktException {
64 List<QueryDO> vc = dao.retrieveQueryByType(ResourceType.SYSTEM, null, QueryType.VIRTUAL_CORPUS);
margarethaf6d5a822017-10-19 19:51:20 +020065 assertEquals(1, vc.size());
margaretha1f106f62017-10-18 22:27:30 +020066 }
67
margarethaf7abb362018-09-18 20:09:37 +020068 /**
69 * retrieve private and group VC
Marc Kupietzd43a98d2023-09-22 17:11:46 +020070 *
margarethaf6d5a822017-10-19 19:51:20 +020071 * @throws KustvaktException
72 */
margaretha1f106f62017-10-18 22:27:30 +020073 @Test
Marc Kupietzd43a98d2023-09-22 17:11:46 +020074 public void retrieveVCByUserDory() throws KustvaktException {
75 List<QueryDO> virtualCorpora = dao.retrieveQueryByUser("dory", QueryType.VIRTUAL_CORPUS);
margarethaf7abb362018-09-18 20:09:37 +020076 // System.out.println(virtualCorpora);
margaretha71e6fca2018-01-18 18:11:48 +010077 assertEquals(4, virtualCorpora.size());
margaretha98ec15b2018-01-22 17:14:02 +010078 // ordered by id
margarethab097fb02021-02-22 19:28:33 +010079 Iterator<QueryDO> i = virtualCorpora.iterator();
Marc Kupietzd43a98d2023-09-22 17:11:46 +020080 assertEquals(i.next().getName(), "dory-vc");
81 assertEquals(i.next().getName(), "group-vc");
82 assertEquals(i.next().getName(), "system-vc");
83 assertEquals(i.next().getName(), "published-vc");
margaretha1f106f62017-10-18 22:27:30 +020084 }
85
margarethaf7abb362018-09-18 20:09:37 +020086 /**
87 * retrieves group VC and
88 * excludes hidden published VC (user has never used it)
Marc Kupietzd43a98d2023-09-22 17:11:46 +020089 *
margarethaf6d5a822017-10-19 19:51:20 +020090 * @throws KustvaktException
91 */
margaretha1f106f62017-10-18 22:27:30 +020092 @Test
Marc Kupietzd43a98d2023-09-22 17:11:46 +020093 public void retrieveVCByUserNemo() throws KustvaktException {
94 List<QueryDO> virtualCorpora = dao.retrieveQueryByUser("nemo", QueryType.VIRTUAL_CORPUS);
margaretha98ec15b2018-01-22 17:14:02 +010095 assertEquals(3, virtualCorpora.size());
margarethab097fb02021-02-22 19:28:33 +010096 Iterator<QueryDO> i = virtualCorpora.iterator();
Marc Kupietzd43a98d2023-09-22 17:11:46 +020097 assertEquals(i.next().getName(), "group-vc");
98 assertEquals(i.next().getName(), "system-vc");
99 assertEquals(i.next().getName(), "nemo-vc");
margaretha1f106f62017-10-18 22:27:30 +0200100 }
101
margarethaf7abb362018-09-18 20:09:37 +0200102 /**
103 * retrieves published VC by the owner and
104 * excludes group vc when a user is a pending member
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200105 *
margarethaf6d5a822017-10-19 19:51:20 +0200106 * @throws KustvaktException
107 */
108 @Test
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200109 public void retrieveVCByUserMarlin() throws KustvaktException {
110 List<QueryDO> virtualCorpora = dao.retrieveQueryByUser("marlin", QueryType.VIRTUAL_CORPUS);
margaretha98ec15b2018-01-22 17:14:02 +0100111 assertEquals(3, 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(), "published-vc");
115 assertEquals(i.next().getName(), "marlin-vc");
margarethaf6d5a822017-10-19 19:51:20 +0200116 }
117
margarethaf7abb362018-09-18 20:09:37 +0200118 /**
119 * retrieves published VC from an auto-generated hidden group and
120 * excludes group vc when a user is a deleted member
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200121 *
margarethaf6d5a822017-10-19 19:51:20 +0200122 * @throws KustvaktException
123 */
124 @Test
Marc Kupietzd43a98d2023-09-22 17:11:46 +0200125 public void retrieveVCByUserPearl() throws KustvaktException {
126 List<QueryDO> virtualCorpora = dao.retrieveQueryByUser("pearl", QueryType.VIRTUAL_CORPUS);
margarethaf6d5a822017-10-19 19:51:20 +0200127 assertEquals(2, 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");
margaretha1f106f62017-10-18 22:27:30 +0200131 }
margaretha1f106f62017-10-18 22:27:30 +0200132}