blob: c6b5c43bcac58d61fff768435ae7a37a14c80f34 [file] [log] [blame]
margaretha318fec32017-10-24 12:11:58 +02001package de.ids_mannheim.korap.dao;
margaretha1f106f62017-10-18 22:27:30 +02002
3import static org.junit.Assert.assertEquals;
4
margarethaf6d5a822017-10-19 19:51:20 +02005import java.util.Iterator;
margaretha1f106f62017-10-18 22:27:30 +02006import java.util.List;
7
margarethaf7abb362018-09-18 20:09:37 +02008import javax.persistence.PersistenceException;
9
margarethaf6d5a822017-10-19 19:51:20 +020010import org.junit.Rule;
margaretha1f106f62017-10-18 22:27:30 +020011import org.junit.Test;
margarethaf6d5a822017-10-19 19:51:20 +020012import org.junit.rules.ExpectedException;
margaretha1f106f62017-10-18 22:27:30 +020013import org.springframework.beans.factory.annotation.Autowired;
14
margaretha85273f12019-02-04 18:13:17 +010015import de.ids_mannheim.korap.config.SpringJerseyTest;
Akronda080152020-12-03 13:53:29 +010016import de.ids_mannheim.korap.constant.QueryType;
17import de.ids_mannheim.korap.constant.ResourceType;
margaretha1f106f62017-10-18 22:27:30 +020018import de.ids_mannheim.korap.entity.VirtualCorpus;
19import de.ids_mannheim.korap.exceptions.KustvaktException;
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
margarethaf438c592017-10-25 15:23:50 +020025 private VirtualCorpusDao dao;
margarethaf6d5a822017-10-19 19:51:20 +020026
27 @Rule
28 public ExpectedException thrown = ExpectedException.none();
margaretha1f106f62017-10-18 22:27:30 +020029
margaretha45dde682018-01-04 21:33:46 +010030 @Test
31 public void testListVCByType () throws KustvaktException {
32 List<VirtualCorpus> vcList =
margaretha652c4dc2021-02-12 17:07:44 +010033 dao.retrieveVCByType(ResourceType.PUBLISHED, null, QueryType.VIRTUAL_CORPUS);
margaretha45dde682018-01-04 21:33:46 +010034 assertEquals(1, vcList.size());
margaretha541b8cc2018-01-10 13:02:46 +010035
margaretha45dde682018-01-04 21:33:46 +010036 VirtualCorpus vc = vcList.get(0);
37 assertEquals(4, vc.getId());
margaretha3a579402019-07-04 15:40:46 +020038 assertEquals("published-vc", vc.getName());
margaretha45dde682018-01-04 21:33:46 +010039 assertEquals("marlin", vc.getCreatedBy());
40 }
margaretha1f106f62017-10-18 22:27:30 +020041
42 @Test
margaretha44573832018-03-21 16:59:59 +010043 public void testSystemVC () throws KustvaktException {
margarethaf6d5a822017-10-19 19:51:20 +020044 // insert vc
Akronda080152020-12-03 13:53:29 +010045 int id = dao.createVirtualCorpus("system-vc", ResourceType.SYSTEM,
46 QueryType.VIRTUAL_CORPUS, User.CorpusAccess.FREE,
47 "corpusSigle=GOE", "definition", "description", "experimental",
48 false, "test class");
margarethaf6d5a822017-10-19 19:51:20 +020049
50 // select vc
51 List<VirtualCorpus> vcList =
margaretha652c4dc2021-02-12 17:07:44 +010052 dao.retrieveVCByType(ResourceType.SYSTEM, null, QueryType.VIRTUAL_CORPUS);
margarethaf6d5a822017-10-19 19:51:20 +020053 assertEquals(2, vcList.size());
54
margaretha44573832018-03-21 16:59:59 +010055 VirtualCorpus vc = dao.retrieveVCById(id);
margarethaf6d5a822017-10-19 19:51:20 +020056 // delete vc
margaretha44573832018-03-21 16:59:59 +010057 dao.deleteVirtualCorpus(vc);
margarethaf6d5a822017-10-19 19:51:20 +020058
59 // check if vc has been deleted
60 thrown.expect(KustvaktException.class);
margaretha61966dd2017-10-26 19:32:21 +020061 dao.retrieveVCById(id);
margaretha1f106f62017-10-18 22:27:30 +020062 }
63
margarethaf7abb362018-09-18 20:09:37 +020064 @Test
65 public void testNonUniqueVC () throws KustvaktException {
66 thrown.expect(PersistenceException.class);
67 thrown.expectMessage("could not execute statement");
68
Akronda080152020-12-03 13:53:29 +010069 dao.createVirtualCorpus("system-vc", ResourceType.SYSTEM,
70 QueryType.VIRTUAL_CORPUS, User.CorpusAccess.FREE,
71 "corpusSigle=GOE", "definition", "description", "experimental",
72 false, "system");
margarethaf7abb362018-09-18 20:09:37 +020073 }
margaretha1f106f62017-10-18 22:27:30 +020074
75 @Test
margaretha44573832018-03-21 16:59:59 +010076 public void retrieveSystemVC () throws KustvaktException {
margarethaf7abb362018-09-18 20:09:37 +020077 List<VirtualCorpus> vc =
margaretha652c4dc2021-02-12 17:07:44 +010078 dao.retrieveVCByType(ResourceType.SYSTEM, null, QueryType.VIRTUAL_CORPUS);
margarethaf6d5a822017-10-19 19:51:20 +020079 assertEquals(1, vc.size());
margaretha1f106f62017-10-18 22:27:30 +020080 }
81
margarethaf7abb362018-09-18 20:09:37 +020082 /**
83 * retrieve private and group VC
84 *
margarethaf6d5a822017-10-19 19:51:20 +020085 * @throws KustvaktException
86 */
margaretha1f106f62017-10-18 22:27:30 +020087 @Test
margarethaf438c592017-10-25 15:23:50 +020088 public void retrieveVCByUserDory () throws KustvaktException {
margaretha652c4dc2021-02-12 17:07:44 +010089 List<VirtualCorpus> virtualCorpora =
90 dao.retrieveVCByUser("dory", QueryType.VIRTUAL_CORPUS);
margarethaf7abb362018-09-18 20:09:37 +020091 // System.out.println(virtualCorpora);
margaretha71e6fca2018-01-18 18:11:48 +010092 assertEquals(4, virtualCorpora.size());
margaretha98ec15b2018-01-22 17:14:02 +010093 // ordered by id
94 Iterator<VirtualCorpus> i = virtualCorpora.iterator();
margaretha3a579402019-07-04 15:40:46 +020095 assertEquals("dory-vc", i.next().getName());
96 assertEquals("group-vc", i.next().getName());
97 assertEquals("system-vc", i.next().getName());
98 assertEquals("published-vc", i.next().getName());
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)
104 *
margarethaf6d5a822017-10-19 19:51:20 +0200105 * @throws KustvaktException
106 */
margaretha1f106f62017-10-18 22:27:30 +0200107 @Test
margarethaf438c592017-10-25 15:23:50 +0200108 public void retrieveVCByUserNemo () throws KustvaktException {
margaretha652c4dc2021-02-12 17:07:44 +0100109 List<VirtualCorpus> virtualCorpora =
110 dao.retrieveVCByUser("nemo", QueryType.VIRTUAL_CORPUS);
margaretha98ec15b2018-01-22 17:14:02 +0100111 assertEquals(3, virtualCorpora.size());
margarethaf6d5a822017-10-19 19:51:20 +0200112 Iterator<VirtualCorpus> i = virtualCorpora.iterator();
margaretha3a579402019-07-04 15:40:46 +0200113 assertEquals("group-vc", i.next().getName());
114 assertEquals("system-vc", i.next().getName());
115 assertEquals("nemo-vc", i.next().getName());
margaretha1f106f62017-10-18 22:27:30 +0200116 }
117
margarethaf7abb362018-09-18 20:09:37 +0200118 /**
119 * retrieves published VC by the owner and
120 * excludes group vc when a user is a pending member
121 *
margarethaf6d5a822017-10-19 19:51:20 +0200122 * @throws KustvaktException
123 */
124 @Test
margarethaf438c592017-10-25 15:23:50 +0200125 public void retrieveVCByUserMarlin () throws KustvaktException {
margaretha652c4dc2021-02-12 17:07:44 +0100126 List<VirtualCorpus> virtualCorpora =
127 dao.retrieveVCByUser("marlin", QueryType.VIRTUAL_CORPUS);
margaretha98ec15b2018-01-22 17:14:02 +0100128 assertEquals(3, virtualCorpora.size());
margarethaf6d5a822017-10-19 19:51:20 +0200129 Iterator<VirtualCorpus> i = virtualCorpora.iterator();
margaretha3a579402019-07-04 15:40:46 +0200130 assertEquals("system-vc", i.next().getName());
131 assertEquals("published-vc", i.next().getName());
132 assertEquals("marlin-vc", i.next().getName());
margarethaf6d5a822017-10-19 19:51:20 +0200133 }
134
margarethaf7abb362018-09-18 20:09:37 +0200135 /**
136 * retrieves published VC from an auto-generated hidden group and
137 * excludes group vc when a user is a deleted member
138 *
margarethaf6d5a822017-10-19 19:51:20 +0200139 * @throws KustvaktException
140 */
141 @Test
margarethaf438c592017-10-25 15:23:50 +0200142 public void retrieveVCByUserPearl () throws KustvaktException {
margaretha652c4dc2021-02-12 17:07:44 +0100143 List<VirtualCorpus> virtualCorpora =
144 dao.retrieveVCByUser("pearl", QueryType.VIRTUAL_CORPUS);
margarethaf6d5a822017-10-19 19:51:20 +0200145 assertEquals(2, virtualCorpora.size());
146 Iterator<VirtualCorpus> i = virtualCorpora.iterator();
margaretha3a579402019-07-04 15:40:46 +0200147 assertEquals("system-vc", i.next().getName());
148 assertEquals("published-vc", i.next().getName());
margaretha1f106f62017-10-18 22:27:30 +0200149 }
150
151}