1 | package clarin.cmdi.componentregistry.impl.database; |
---|
2 | |
---|
3 | import static org.junit.Assert.assertEquals; |
---|
4 | import static org.junit.Assert.assertFalse; |
---|
5 | import static org.junit.Assert.assertNotNull; |
---|
6 | import static org.junit.Assert.assertTrue; |
---|
7 | |
---|
8 | import java.util.Date; |
---|
9 | import java.util.List; |
---|
10 | |
---|
11 | import org.junit.Test; |
---|
12 | import org.springframework.beans.factory.annotation.Autowired; |
---|
13 | |
---|
14 | import clarin.cmdi.componentregistry.BaseUnitTest; |
---|
15 | import clarin.cmdi.componentregistry.model.BaseDescription; |
---|
16 | import clarin.cmdi.componentregistry.model.ComponentDescription; |
---|
17 | import clarin.cmdi.componentregistry.persistence.ComponentDao; |
---|
18 | import clarin.cmdi.componentregistry.persistence.jpa.UserDao; |
---|
19 | |
---|
20 | /** |
---|
21 | * Base test class for concrete tests to profile and component DAOs |
---|
22 | * |
---|
23 | * @author Twan Goosen <twan.goosen@mpi.nl> |
---|
24 | * @author George.Georgovassilis@mpi.nl |
---|
25 | */ |
---|
26 | public abstract class AbstractDescriptionDaoTest extends BaseUnitTest { |
---|
27 | |
---|
28 | @Autowired |
---|
29 | private UserDao userDao; |
---|
30 | |
---|
31 | protected abstract ComponentDao getDao(); |
---|
32 | |
---|
33 | @Test |
---|
34 | public void testInjection() { |
---|
35 | assertNotNull(jdbcTemplate); |
---|
36 | assertNotNull(getDao()); |
---|
37 | } |
---|
38 | |
---|
39 | @Test |
---|
40 | public void testInsertComponent() throws Exception { |
---|
41 | Date regDate = new Date(); |
---|
42 | |
---|
43 | BaseDescription description = createNewDescription(); |
---|
44 | description.setName("MyComponent"); |
---|
45 | description.setDescription("MyDescription"); |
---|
46 | description.setCreatorName("Aap"); |
---|
47 | description.setGroupName("MyGroup"); |
---|
48 | description.setDomainName("MyDomain \u00CA"); |
---|
49 | description.setHref("http://MyHref"); |
---|
50 | |
---|
51 | description.setRegistrationDate(regDate); |
---|
52 | |
---|
53 | String testComponent = getContentString(); |
---|
54 | Number newId = getDao().insertDescription(description, testComponent, |
---|
55 | true, null); |
---|
56 | assertNotNull(newId); |
---|
57 | BaseDescription descr = getDao().getById(newId); |
---|
58 | assertNotNull(descr); |
---|
59 | assertEquals("MyComponent", descr.getName()); |
---|
60 | assertEquals("MyDescription", descr.getDescription()); |
---|
61 | assertEquals("Aap", descr.getCreatorName()); |
---|
62 | assertEquals("MyGroup", descr.getGroupName()); |
---|
63 | assertEquals("MyDomain \u00CA", descr.getDomainName()); |
---|
64 | assertEquals("http://MyHref", descr.getHref()); |
---|
65 | assertEquals(regDate, descr.getRegistrationDate()); |
---|
66 | assertEquals(testComponent, |
---|
67 | getDao().getContent(false, description.getId())); |
---|
68 | } |
---|
69 | |
---|
70 | @Test |
---|
71 | public void testGetDescriptionsOrdered() throws Exception { |
---|
72 | insert("a", true, null); |
---|
73 | insert("A", true, null); |
---|
74 | insert("B", true, null); |
---|
75 | insert("a", true, null); |
---|
76 | |
---|
77 | List<BaseDescription> descs = getDao().getPublicDescriptions(); |
---|
78 | assertEquals(4, descs.size()); |
---|
79 | assertEquals("a", descs.get(0).getName()); // ordered by name case |
---|
80 | // insensitive then by cmdId |
---|
81 | assertEquals("A", descs.get(1).getName()); |
---|
82 | assertEquals("a", descs.get(2).getName()); |
---|
83 | assertTrue(descs.get(1).getId().compareTo(descs.get(2).getId()) < 0); |
---|
84 | assertEquals("B", descs.get(3).getName()); |
---|
85 | |
---|
86 | } |
---|
87 | |
---|
88 | @Test |
---|
89 | public void testGetPublicComponents() throws Exception { |
---|
90 | List<BaseDescription> descriptions = getDao().getPublicDescriptions(); |
---|
91 | assertNotNull(descriptions); |
---|
92 | } |
---|
93 | |
---|
94 | @Test |
---|
95 | public void testGetUserspaceDescriptions() throws Exception { |
---|
96 | // TODO: test queries empty database and is happy that no results are |
---|
97 | // returned. Should also test the case where there are userspace |
---|
98 | // descriptions |
---|
99 | List<BaseDescription> descriptions = getDao().getPrivateBaseDescriptions(-1, ComponentDescription.COMPONENT_PREFIX); |
---|
100 | assertEquals(0, descriptions.size()); |
---|
101 | } |
---|
102 | |
---|
103 | @Test |
---|
104 | public void testDeleteDescription() throws Exception { |
---|
105 | BaseDescription description = createNewDescription(); |
---|
106 | description.setName("Aap"); |
---|
107 | description.setDescription("MyDescription"); |
---|
108 | String testComponent = getContentString(); |
---|
109 | |
---|
110 | int count = getDao().getPublicDescriptions().size(); |
---|
111 | // insert |
---|
112 | getDao().insertDescription(description, testComponent, true, null); |
---|
113 | assertEquals(count + 1, getDao().getPublicDescriptions().size()); |
---|
114 | |
---|
115 | List<BaseDescription> deletedDescriptions = getDao().getDeletedDescriptions(null); |
---|
116 | assertEquals(0, deletedDescriptions.size()); |
---|
117 | |
---|
118 | // delete |
---|
119 | getDao().setDeleted(description, true); |
---|
120 | assertEquals(count, getDao().getPublicDescriptions().size()); |
---|
121 | |
---|
122 | deletedDescriptions = getDao().getDeletedDescriptions(null); |
---|
123 | assertEquals(1, deletedDescriptions.size()); |
---|
124 | } |
---|
125 | |
---|
126 | @Test |
---|
127 | public void testUpdateDescription() { |
---|
128 | BaseDescription description = createNewDescription(); |
---|
129 | description.setName("Aap"); |
---|
130 | description.setDescription("MyDescription"); |
---|
131 | description.setCreatorName("Aap"); |
---|
132 | description.setGroupName("MyGroup"); |
---|
133 | description.setDomainName("MyDomain"); |
---|
134 | description.setHref("http://MyHref"); |
---|
135 | |
---|
136 | String testComponent = getContentString(); |
---|
137 | Number newId = getDao().insertDescription(description, testComponent, |
---|
138 | true, null); |
---|
139 | |
---|
140 | // Change values |
---|
141 | description.setName("Noot"); |
---|
142 | description.setDescription("AnotherDescription"); |
---|
143 | description.setCreatorName("AnotherAap"); |
---|
144 | description.setGroupName("AnotherGroup"); |
---|
145 | description.setDomainName("AnotherDomain\u00CA"); |
---|
146 | description.setHref("http://AnotherHref"); |
---|
147 | // Update in db |
---|
148 | getDao().updateDescription(newId, description, null); |
---|
149 | description = getDao().getById(newId); |
---|
150 | // Test if new values are there |
---|
151 | assertNotNull(description); |
---|
152 | assertEquals("Noot", description.getName()); |
---|
153 | assertEquals("AnotherDescription", description.getDescription()); |
---|
154 | assertEquals("AnotherAap", description.getCreatorName()); |
---|
155 | assertEquals("AnotherGroup", description.getGroupName()); |
---|
156 | assertEquals("AnotherDomain\u00CA", description.getDomainName()); |
---|
157 | assertEquals("http://AnotherHref", description.getHref()); |
---|
158 | |
---|
159 | // Update content |
---|
160 | String testContent2 = "<test>Test content</test>"; |
---|
161 | getDao().updateDescription(newId, null, testContent2); |
---|
162 | // Test if new content is there |
---|
163 | assertEquals(testContent2, |
---|
164 | getDao().getContent(false, description.getId())); |
---|
165 | |
---|
166 | // Update both |
---|
167 | description.setName("Mies"); |
---|
168 | description.setDescription("YetAnotherDescription"); |
---|
169 | String testContent3 = "<test>More test \u00CA content</test>"; |
---|
170 | |
---|
171 | // Update in db |
---|
172 | getDao().updateDescription(newId, description, testContent3); |
---|
173 | description = getDao().getById(newId); |
---|
174 | // Test if new values are there |
---|
175 | assertNotNull(description); |
---|
176 | assertEquals("Mies", description.getName()); |
---|
177 | assertEquals("YetAnotherDescription", description.getDescription()); |
---|
178 | // Test if new content is there |
---|
179 | assertEquals(testContent3, |
---|
180 | getDao().getContent(false, description.getId())); |
---|
181 | } |
---|
182 | |
---|
183 | @Test |
---|
184 | public void testIsPublic() { |
---|
185 | Number userId = userDao.save(UserDaoTest.createTestUser()).getId(); |
---|
186 | BaseDescription publicDesc = insert(true, null); |
---|
187 | assertTrue(getDao().isPublic(publicDesc.getId())); |
---|
188 | assertFalse(getDao().isInUserSpace(publicDesc.getId(), userId)); |
---|
189 | |
---|
190 | BaseDescription privateDesc = insert(false, userId); |
---|
191 | assertFalse(getDao().isPublic(privateDesc.getId())); |
---|
192 | assertTrue(getDao().isInUserSpace(privateDesc.getId(), userId)); |
---|
193 | |
---|
194 | getDao().setDeleted(publicDesc, true); |
---|
195 | assertTrue(getDao().isPublic(publicDesc.getId())); |
---|
196 | assertFalse(getDao().isInUserSpace(publicDesc.getId(), userId)); |
---|
197 | |
---|
198 | getDao().setDeleted(privateDesc, true); |
---|
199 | assertFalse(getDao().isPublic(privateDesc.getId())); |
---|
200 | assertTrue(getDao().isInUserSpace(privateDesc.getId(), userId)); |
---|
201 | } |
---|
202 | |
---|
203 | private BaseDescription insert(boolean isPublic, Number userId) { |
---|
204 | return insert("Aap", isPublic, userId); |
---|
205 | } |
---|
206 | |
---|
207 | private BaseDescription insert(String name, boolean isPublic, Number userId) { |
---|
208 | BaseDescription desc = createNewDescription(); |
---|
209 | desc.setName(name); |
---|
210 | desc.setDescription("MyDescription"); |
---|
211 | getDao().insertDescription(desc, getContentString(), isPublic, userId); |
---|
212 | return desc; |
---|
213 | } |
---|
214 | |
---|
215 | protected abstract String getContentString(); |
---|
216 | |
---|
217 | protected abstract BaseDescription createNewDescription(); |
---|
218 | } |
---|