blob: 5faeb800f2d525ff291f07cf8414514a85050115 [file] [log] [blame]
margaretha541b8cc2018-01-10 13:02:46 +01001package de.ids_mannheim.korap.service;
2
3import static org.junit.Assert.assertEquals;
margaretha89bd8f52021-02-26 17:08:01 +01004import static org.junit.Assert.assertThrows;
margaretha3ccaeb72019-02-28 18:40:22 +01005import static org.junit.Assert.assertTrue;
margaretha541b8cc2018-01-10 13:02:46 +01006
7import java.util.List;
8
margaretha89bd8f52021-02-26 17:08:01 +01009import org.junit.Assert;
margaretha541b8cc2018-01-10 13:02:46 +010010import org.junit.Test;
11import org.junit.runner.RunWith;
12import org.springframework.beans.factory.annotation.Autowired;
13import org.springframework.test.context.ContextConfiguration;
14import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
15
Akronda080152020-12-03 13:53:29 +010016import de.ids_mannheim.korap.constant.QueryType;
Akronda080152020-12-03 13:53:29 +010017import de.ids_mannheim.korap.constant.ResourceType;
margaretha89bd8f52021-02-26 17:08:01 +010018import de.ids_mannheim.korap.constant.UserGroupStatus;
margarethab097fb02021-02-22 19:28:33 +010019import de.ids_mannheim.korap.dto.QueryAccessDto;
20import de.ids_mannheim.korap.dto.QueryDto;
margarethab097fb02021-02-22 19:28:33 +010021import de.ids_mannheim.korap.entity.QueryDO;
margaretha89bd8f52021-02-26 17:08:01 +010022import de.ids_mannheim.korap.entity.UserGroup;
margaretha541b8cc2018-01-10 13:02:46 +010023import de.ids_mannheim.korap.exceptions.KustvaktException;
Akronda080152020-12-03 13:53:29 +010024import de.ids_mannheim.korap.web.input.QueryJson;
margaretha541b8cc2018-01-10 13:02:46 +010025
26@RunWith(SpringJUnit4ClassRunner.class)
27@ContextConfiguration("classpath:test-config.xml")
28public class VirtualCorpusServiceTest {
29
30 @Autowired
margarethab097fb02021-02-22 19:28:33 +010031 private QueryService vcService;
margaretha3ccaeb72019-02-28 18:40:22 +010032 @Autowired
33 private UserGroupService groupService;
margaretha541b8cc2018-01-10 13:02:46 +010034
margarethaf7abb362018-09-18 20:09:37 +020035 @Test
36 public void testCreateNonUniqueVC () throws KustvaktException {
margaretha89bd8f52021-02-26 17:08:01 +010037
margaretha85273f12019-02-04 18:13:17 +010038 // EM: message differs depending on the database used
39 // for testing. The message below is from sqlite.
margaretha3ccaeb72019-02-28 18:40:22 +010040 // thrown.expectMessage("A UNIQUE constraint failed "
41 // + "(UNIQUE constraint failed: virtual_corpus.name, "
42 // + "virtual_corpus.created_by)");
margarethaf7abb362018-09-18 20:09:37 +020043
Akronda080152020-12-03 13:53:29 +010044 QueryJson vc = new QueryJson();
margarethaf7abb362018-09-18 20:09:37 +020045 vc.setCorpusQuery("corpusSigle=GOE");
Akronda080152020-12-03 13:53:29 +010046 vc.setType(ResourceType.PRIVATE);
47 vc.setQueryType(QueryType.VIRTUAL_CORPUS);
margaretha89bd8f52021-02-26 17:08:01 +010048
49 Assert.assertThrows(KustvaktException.class,
margarethab3ecbe32021-08-16 12:55:54 +020050 () -> vcService.storeQuery(vc, "dory-vc", "dory", "dory"));
margarethaf7abb362018-09-18 20:09:37 +020051 }
52
margaretha541b8cc2018-01-10 13:02:46 +010053 @Test
margarethab874ef52018-01-23 20:26:31 +010054 public void createDeletePublishVC () throws KustvaktException {
margaretha3a579402019-07-04 15:40:46 +020055 String vcName = "new-published-vc";
margarethab874ef52018-01-23 20:26:31 +010056
Akronda080152020-12-03 13:53:29 +010057 QueryJson vc = new QueryJson();
margaretha541b8cc2018-01-10 13:02:46 +010058 vc.setCorpusQuery("corpusSigle=GOE");
Akronda080152020-12-03 13:53:29 +010059 vc.setType(ResourceType.PUBLISHED);
60 vc.setQueryType(QueryType.VIRTUAL_CORPUS);
margaretha3ccaeb72019-02-28 18:40:22 +010061 String username = "VirtualCorpusServiceTest";
margarethab3ecbe32021-08-16 12:55:54 +020062 vcService.storeQuery(vc, vcName, username, username);
margaretha541b8cc2018-01-10 13:02:46 +010063
margarethab097fb02021-02-22 19:28:33 +010064 List<QueryAccessDto> accesses =
65 vcService.listQueryAccessByUsername("admin");
margaretha3ccaeb72019-02-28 18:40:22 +010066 int size = accesses.size();
margarethab874ef52018-01-23 20:26:31 +010067
margarethab097fb02021-02-22 19:28:33 +010068 QueryAccessDto dto = accesses.get(accesses.size() - 1);
69 assertEquals(vcName, dto.getQueryName());
margaretha3ccaeb72019-02-28 18:40:22 +010070 assertEquals("system", dto.getCreatedBy());
71 assertTrue(dto.getUserGroupName().startsWith("auto"));
margarethab874ef52018-01-23 20:26:31 +010072
margaretha3ccaeb72019-02-28 18:40:22 +010073 // check hidden group
74 int groupId = dto.getUserGroupId();
75 UserGroup group = groupService.retrieveUserGroupById(groupId);
76 assertEquals(UserGroupStatus.HIDDEN, group.getStatus());
77
78 //delete vc
margaretha89bd8f52021-02-26 17:08:01 +010079 vcService.deleteQueryByName(username, vcName, username,
80 QueryType.VIRTUAL_CORPUS);
margaretha3ccaeb72019-02-28 18:40:22 +010081
82 // check hidden access
margarethab097fb02021-02-22 19:28:33 +010083 accesses = vcService.listQueryAccessByUsername("admin");
margaretha3ccaeb72019-02-28 18:40:22 +010084 assertEquals(size-1, accesses.size());
85
86 // check hidden group
margaretha89bd8f52021-02-26 17:08:01 +010087 KustvaktException e = assertThrows(KustvaktException.class,
88 () -> groupService.retrieveUserGroupById(groupId));
89 assertEquals("Group with id " + groupId + " is not found",
90 e.getMessage());
margaretha541b8cc2018-01-10 13:02:46 +010091 }
92
margarethab874ef52018-01-23 20:26:31 +010093 @Test
94 public void testEditPublishVC () throws KustvaktException {
95 String username = "dory";
96 int vcId = 2;
97
margaretha3a579402019-07-04 15:40:46 +020098 String vcName = "group-vc";
margarethab097fb02021-02-22 19:28:33 +010099 QueryDO existingVC =
100 vcService.searchQueryByName(username, vcName, username, QueryType.VIRTUAL_CORPUS);
Akronda080152020-12-03 13:53:29 +0100101 QueryJson vcJson = new QueryJson();
102 vcJson.setType(ResourceType.PUBLISHED);
margarethab874ef52018-01-23 20:26:31 +0100103
margarethab097fb02021-02-22 19:28:33 +0100104 vcService.editQuery(existingVC, vcJson, vcName, username);
margarethab874ef52018-01-23 20:26:31 +0100105
106 // check VC
margarethab097fb02021-02-22 19:28:33 +0100107 QueryDto vcDto = vcService.searchQueryById("dory", vcId);
margaretha3ccaeb72019-02-28 18:40:22 +0100108 assertEquals(vcName, vcDto.getName());
Akronda080152020-12-03 13:53:29 +0100109 assertEquals(ResourceType.PUBLISHED.displayName(),
margarethaf7abb362018-09-18 20:09:37 +0200110 vcDto.getType());
margarethab874ef52018-01-23 20:26:31 +0100111
112 // check access
margarethab097fb02021-02-22 19:28:33 +0100113 List<QueryAccessDto> accesses =
114 vcService.listQueryAccessByUsername("admin");
margaretha3ccaeb72019-02-28 18:40:22 +0100115 int size = accesses.size();
margarethab097fb02021-02-22 19:28:33 +0100116 QueryAccessDto dto = accesses.get(accesses.size() - 1);
117 assertEquals(vcName, dto.getQueryName());
margaretha3ccaeb72019-02-28 18:40:22 +0100118 assertEquals("system", dto.getCreatedBy());
119 assertTrue(dto.getUserGroupName().startsWith("auto"));
margarethaf7abb362018-09-18 20:09:37 +0200120
margarethab874ef52018-01-23 20:26:31 +0100121 // check auto hidden group
margaretha3ccaeb72019-02-28 18:40:22 +0100122 int groupId = dto.getUserGroupId();
123 UserGroup group = groupService.retrieveUserGroupById(groupId);
124 assertEquals(UserGroupStatus.HIDDEN, group.getStatus());
margarethab874ef52018-01-23 20:26:31 +0100125
126 // 2nd edit (withdraw from publication)
margaretha3ccaeb72019-02-28 18:40:22 +0100127
Akronda080152020-12-03 13:53:29 +0100128 vcJson = new QueryJson();
129 vcJson.setType(ResourceType.PROJECT);
margarethab874ef52018-01-23 20:26:31 +0100130
margarethab097fb02021-02-22 19:28:33 +0100131 vcService.editQuery(existingVC, vcJson, vcName, username);
margarethab874ef52018-01-23 20:26:31 +0100132
133 // check VC
margarethab097fb02021-02-22 19:28:33 +0100134 vcDto = vcService.searchQueryById("dory", vcId);
margaretha3a579402019-07-04 15:40:46 +0200135 assertEquals("group-vc", vcDto.getName());
Akronda080152020-12-03 13:53:29 +0100136 assertEquals(ResourceType.PROJECT.displayName(), vcDto.getType());
margarethab874ef52018-01-23 20:26:31 +0100137
138 // check access
margarethab097fb02021-02-22 19:28:33 +0100139 accesses = vcService.listQueryAccessByUsername("admin");
margaretha3ccaeb72019-02-28 18:40:22 +0100140 assertEquals(size - 1, accesses.size());
141
margaretha89bd8f52021-02-26 17:08:01 +0100142 KustvaktException e = assertThrows(KustvaktException.class,
143 () -> groupService.retrieveUserGroupById(groupId));
Marc Kupietz92047722021-03-02 16:02:52 +0100144
145 assertEquals("Group with id " + groupId + " is not found", e.getMessage());
margarethab874ef52018-01-23 20:26:31 +0100146 }
147
margaretha541b8cc2018-01-10 13:02:46 +0100148}