1. Project Clover database Wed Jan 17 2024 23:40:18 CST
  2. Package org.europa.together.application

File ConfigurationHbmDAOTest.java

 

Code metrics

20
174
37
1
450
355
47
0.27
4.7
37
1.27

Classes

Class Line # Actions
ConfigurationHbmDAOTest 30 174 0% 47 3
0.98701398.7%
 

Contributing tests

This file is covered by 32 tests. .

Source view

1    package org.europa.together.application;
2   
3    import static com.google.code.beanmatchers.BeanMatchers.*;
4    import java.util.ArrayList;
5    import java.util.List;
6    import org.europa.together.JUnit5Preperator;
7    import org.europa.together.business.ConfigurationDAO;
8    import org.europa.together.business.DatabaseActions;
9    import org.europa.together.business.Logger;
10    import org.europa.together.domain.ConfigurationDO;
11    import org.europa.together.domain.JpaPagination;
12    import org.europa.together.domain.LogLevel;
13    import static org.hamcrest.MatcherAssert.assertThat;
14    import org.junit.jupiter.api.AfterAll;
15    import org.junit.jupiter.api.AfterEach;
16    import static org.junit.jupiter.api.Assertions.*;
17    import org.junit.jupiter.api.Assumptions;
18    import org.junit.jupiter.api.BeforeAll;
19    import org.junit.jupiter.api.BeforeEach;
20    import org.junit.jupiter.api.Test;
21    import org.junit.jupiter.api.extension.ExtendWith;
22    import org.springframework.beans.factory.annotation.Autowired;
23    import org.springframework.test.context.ContextConfiguration;
24    import org.springframework.test.context.junit.jupiter.SpringExtension;
25   
26    @SuppressWarnings("unchecked")
27    @ExtendWith({JUnit5Preperator.class})
28    @ExtendWith(SpringExtension.class)
29    @ContextConfiguration(locations = {"/applicationContext.xml"})
 
30    public class ConfigurationHbmDAOTest {
31   
32    private static final Logger LOGGER = new LogbackLogger(ConfigurationHbmDAOTest.class);
33   
34    private static final String FLUSH_TABLE
35    = "TRUNCATE TABLE " + ConfigurationDO.TABLE_NAME + ";";
36    private static final String FILE
37    = "org/europa/together/sql/configuration-test.sql";
38   
39    @Autowired
40    private ConfigurationDAO configurationDAO;
41   
42    private ConfigurationDO configDO;
43    private static DatabaseActions jdbcActions = new JdbcActions();
44   
 
45  32 toggle public ConfigurationHbmDAOTest() {
46  32 configDO = new ConfigurationDO();
47  32 configDO.setUuid("QWERTZ");
48  32 configDO.setKey("key");
49  32 configDO.setValue("no value");
50  32 configDO.setDefaultValue("DEFAULT");
51  32 configDO.setConfigurationSet("confSet");
52  32 configDO.setModulName("MOD");
53  32 configDO.setVersion("1.0");
54    }
55   
56    //<editor-fold defaultstate="collapsed" desc="Test Preparation">
 
57  1 toggle @BeforeAll
58    static void setUp() {
59  1 Assumptions.assumeTrue(jdbcActions.connect("test"), "JDBC DBMS Connection failed.");
60  1 jdbcActions.executeSqlFromClasspath(FILE);
61   
62  1 LOGGER.log("Assumptions passed ...\n\n", LogLevel.DEBUG);
63    }
64   
 
65  1 toggle @AfterAll
66    static void tearDown() {
67    }
68   
 
69  32 toggle @BeforeEach
70    void testCaseInitialization() {
71  32 jdbcActions.executeSqlFromClasspath(FILE);
72    }
73   
 
74  32 toggle @AfterEach
75    void testCaseTermination() throws Exception {
76  32 jdbcActions.executeQuery(FLUSH_TABLE);
77    }
78    //</editor-fold>
79   
 
80  1 toggle @Test
81    void constructor() {
82  1 LOGGER.log("TEST CASE: constructor", LogLevel.DEBUG);
83   
84  1 assertThat(ConfigurationHbmDAO.class, hasValidBeanConstructor());
85    }
86   
 
87  1 toggle @Test
88    void countEntries() {
89  1 LOGGER.log("TEST CASE: countEntries", LogLevel.DEBUG);
90   
91  1 assertEquals(14, configurationDAO.countAllElements());
92    }
93   
 
94  1 toggle @Test
95    void listAll() {
96  1 LOGGER.log("TEST CASE: listAll", LogLevel.DEBUG);
97   
98  1 JpaPagination seekElement = new JpaPagination("uuid");
99  1 seekElement.setPageSize(25);
100  1 List<ConfigurationDO> result = configurationDAO.listAllElements(seekElement);
101  1 assertEquals(14, result.size());
102    }
103   
 
104  1 toggle @Test
105    void getPrimaryKeyOfObject() {
106  1 LOGGER.log("TEST CASE: getPrimaryKeyOfObject", LogLevel.DEBUG);
107   
108  1 assertEquals("QWERTZ", configurationDAO.getPrimaryKeyOfObject(configDO));
109    }
110   
 
111  1 toggle @Test
112    void failGetPrimaryKeyOfObject() {
113  1 LOGGER.log("TEST CASE: failGetPrimaryKeyOfObject", LogLevel.DEBUG);
114   
115  1 assertNull(configurationDAO.getPrimaryKeyOfObject(null));
116    }
117   
 
118  1 toggle @Test
119    void find() {
120  1 LOGGER.log("TEST CASE: find", LogLevel.DEBUG);
121   
122  1 ConfigurationDO config = configurationDAO.find("88888888-4444-4444-4444-cccccccccc");
123  1 assertEquals("key", config.getKey());
124    }
125   
 
126  1 toggle @Test
127    void findNotExist() {
128  1 LOGGER.log("TEST CASE: findNotExist", LogLevel.DEBUG);
129   
130  1 assertNull(configurationDAO.find("NotExist"));
131    }
132   
 
133  1 toggle @Test
134    void failFind() throws Exception {
135  1 LOGGER.log("TEST CASE: failFind", LogLevel.DEBUG);
136   
137  1 assertThrows(Exception.class, () -> {
138  1 configurationDAO.find(null);
139    });
140    }
141   
 
142  1 toggle @Test
143    void create() throws Exception {
144  1 LOGGER.log("TEST CASE: create", LogLevel.DEBUG);
145   
146  1 configurationDAO.create(configDO);
147  1 assertEquals(configDO, configurationDAO.find("QWERTZ"));
148    }
149   
 
150  1 toggle @Test
151    void failCreate() throws Exception {
152  1 LOGGER.log("TEST CASE: failCreate", LogLevel.DEBUG);
153   
154  1 assertFalse(configurationDAO.create(null));
155    }
156   
 
157  1 toggle @Test
158    void update() throws Exception {
159  1 LOGGER.log("TEST CASE: update", LogLevel.DEBUG);
160   
161  1 assertTrue(configurationDAO.create(configDO));
162  1 configDO.setValue("changed value");
163   
164  1 configurationDAO.update("QWERTZ", configDO);
165  1 assertEquals("changed value", configurationDAO.find("QWERTZ").getValue());
166    }
167   
 
168  1 toggle @Test
169    void updateNotExist() throws Exception {
170  1 LOGGER.log("TEST CASE: updateNotExist", LogLevel.DEBUG);
171   
172  1 assertThrows(Exception.class, () -> {
173  1 configurationDAO.update("NotExist", configDO);
174    });
175    }
176   
 
177  1 toggle @Test
178    void failUpdate() throws Exception {
179  1 LOGGER.log("TEST CASE: failUpdate", LogLevel.DEBUG);
180   
181  1 assertThrows(Exception.class, () -> {
182  1 configurationDAO.update(null, configDO);
183    });
184  1 assertThrows(Exception.class, () -> {
185  1 configurationDAO.update("", null);
186    });
187  1 assertThrows(Exception.class, () -> {
188  1 configurationDAO.update(null, null);
189    });
190    }
191   
 
192  1 toggle @Test
193    void delete() throws Exception {
194  1 LOGGER.log("TEST CASE: delete", LogLevel.DEBUG);
195   
196  1 configurationDAO.create(configDO);
197  1 assertNotNull(configurationDAO.find("QWERTZ"));
198   
199  1 configurationDAO.delete(configDO.getUuid());
200  1 assertNull(configurationDAO.find(configDO.getUuid()));
201    }
202   
 
203  1 toggle @Test
204    void deleteNotExist() throws Exception {
205  1 LOGGER.log("TEST CASE: deleteNotExist", LogLevel.DEBUG);
206   
207  1 assertThrows(Exception.class, () -> {
208  1 configurationDAO.delete("notExist");
209    });
210    }
211   
 
212  1 toggle @Test
213    void failDelete() throws Exception {
214  1 LOGGER.log("TEST CASE: failDelete", LogLevel.DEBUG);
215   
216  1 assertThrows(Exception.class, () -> {
217  1 configurationDAO.delete(null);
218    });
219    }
220   
 
221  1 toggle @Test
222    void serilizeJsonasObject() throws Exception {
223  1 LOGGER.log("TEST CASE: serilizeAsJson", LogLevel.DEBUG);
224   
225  1 String json
226    = "{\"uuid\":\"QWERTZ\",\"key\":\"key\",\"value\":\"no value\",\"defaultValue\":\"DEFAULT\",\"modulName\":\"MOD\",\"version\":\"1.0\",\"configurationSet\":\"confSet\",\"deprecated\":false,\"mandatory\":false,\"comment\":null}";
227  1 assertEquals(json, configurationDAO.serializeAsJson(configDO));
228    }
229   
 
230  1 toggle @Test
231    void deserializeJsonAsObject() throws Exception {
232  1 LOGGER.log("TEST CASE: deserilizeJsonAsObject", LogLevel.DEBUG);
233   
234  1 String json
235    = "{\"uuid\":\"QWERTZ\",\"key\":\"key\",\"value\":\"no value\",\"defaultValue\":\"DEFAULT\",\"modulName\":\"MOD\",\"version\":\"1.0\",\"configurationSet\":\"confSet\",\"deprecated\":false,\"mandatory\":false,\"comment\":null}";
236  1 ConfigurationDO deserialize
237    = configurationDAO.deserializeJsonAsObject(json, ConfigurationDO.class);
238  1 assertEquals(configDO, deserialize);
239    }
240   
 
241  1 toggle @Test
242    void deserializeJsonAsObjectList() throws Exception {
243  1 LOGGER.log("TEST CASE: deserializeJsonAsObjectList", LogLevel.DEBUG);
244   
245  1 String json
246    = "["
247    + "{\"uuid\":\"QWERTZ\",\"key\":\"key\",\"value\":\"no value\",\"defaultValue\":\"DEFAULT\",\"modulName\":\"MOD\",\"version\":\"1.0\",\"configurationSet\":\"confSet\",\"deprecated\":false,\"mandatory\":false,\"comment\":null}"
248    + "]";
249  1 assertEquals(1, configurationDAO.deserializeJsonAsList(json).size());
250    }
251   
 
252  1 toggle @Test
253    void getConfigurationByKey() {
254  1 LOGGER.log("TEST CASE: getConfigurationByKey", LogLevel.DEBUG);
255   
256  1 ConfigurationDO config = configurationDAO.getConfigurationByKey("key", "Module_A", "2.0.1");
257  1 assertEquals("Y.1", config.getValue());
258    }
259   
 
260  1 toggle @Test
261    void getConfigurationByKeyNoExist() throws Exception {
262  1 LOGGER.log("TEST CASE: getConfigurationByKeyNoExist", LogLevel.DEBUG);
263   
264  1 assertThrows(Exception.class, () -> {
265  1 configurationDAO.getConfigurationByKey("noope", "Module", "1");
266    });
267    }
268   
 
269  1 toggle @Test
270    void getValueByKey() {
271  1 LOGGER.log("TEST CASE: getValueByKey", LogLevel.DEBUG);
272   
273  1 String value = configurationDAO.getValueByKey("key", "Module_A", "2.0.1");
274  1 assertEquals("Y.1", value);
275    }
276   
 
277  1 toggle @Test
278    void getValueByKeyNoExist() throws Exception {
279  1 LOGGER.log("TEST CASE: getValueByKeyNoExist", LogLevel.DEBUG);
280   
281  1 assertThrows(Exception.class, () -> {
282  1 configurationDAO.getValueByKey("key", "Module", "1");
283    });
284    }
285   
 
286  1 toggle @Test
287    void getValueByKeyFallbackToDefault() {
288  1 LOGGER.log("TEST CASE: getValueByKeyFallbackToDefault", LogLevel.DEBUG);
289   
290  1 String value = configurationDAO.getValueByKey("key", "Module", "1.0.1");
291  1 assertEquals("default entry", value);
292    }
293   
 
294  1 toggle @Test
295    void restoreKeyToDefault() throws Exception {
296  1 LOGGER.log("TEST CASE: restoreKeyToDefault", LogLevel.DEBUG);
297   
298  1 ConfigurationDO before = configurationDAO.getConfigurationByKey("key", "Module_A", "2.0");
299  1 assertEquals("6ff62a22-9820-406d-b55a-a86fa1c5a033", before.getUuid());
300  1 assertEquals("Y.1", before.getValue());
301  1 assertEquals("Y", before.getDefaultValue());
302   
303  1 configurationDAO.restoreKeyToDefault(before);
304   
305  1 ConfigurationDO after = configurationDAO.getConfigurationByKey("key", "Module_A", "2.0");
306  1 assertEquals("6ff62a22-9820-406d-b55a-a86fa1c5a033", after.getUuid());
307  1 assertEquals("Y", after.getValue());
308  1 assertEquals("Y", after.getDefaultValue());
309    }
310   
 
311  1 toggle @Test
312    void failRestoreKeyToDefault() throws Exception {
313  1 LOGGER.log("TEST CASE: failRestoreKeyToDefault", LogLevel.DEBUG);
314   
315  1 assertThrows(Exception.class, () -> {
316  1 ConfigurationDO entry = new ConfigurationDO();
317  1 entry.setKey("NOT-EXIST");
318  1 entry.setModulName("no-module");
319  1 configurationDAO.restoreKeyToDefault(entry);
320    });
321    }
322   
 
323  1 toggle @Test
324    void getAllSetEntries() {
325  1 LOGGER.log("TEST CASE: getAllSetEntries", LogLevel.DEBUG);
326   
327  1 List<ConfigurationDO> set
328    = configurationDAO.getAllConfigurationSetEntries("Module_A", "1.0", "Set_1");
329   
330  1 assertEquals(3, set.size());
331   
332  1 for (ConfigurationDO entry : set) {
333  3 if (entry.getUuid().equals("7127260b-ded6-4fab-a7d8-e09fd91bc2bc")) {
334  1 assertEquals("a", entry.getDefaultValue());
335    }
336  3 if (entry.getUuid().equals("2fedf7e2-ef5e-41da-a82f-eb22e40a02b7")) {
337  1 assertEquals("b", entry.getDefaultValue());
338    }
339  3 if (entry.getUuid().equals("e2a14186-4d63-4596-b8f0-419ca095830f")) {
340  1 assertEquals("c", entry.getDefaultValue());
341    }
342    }
343    }
344   
 
345  1 toggle @Test
346    void getHistoryOfAEntry() {
347  1 LOGGER.log("TEST CASE: getHistoryOfAEntry", LogLevel.DEBUG);
348   
349  1 List<ConfigurationDO> set
350    = configurationDAO.getHistoryOfAEntry("Module_A", "key", "none");
351   
352  1 assertEquals(6, set.size());
353  1 for (ConfigurationDO entry : set) {
354  6 if (entry.getUuid().equals("69173a15-185f-4338-ab49-5de2c704d029")) {
355  1 assertEquals("1.0", entry.getVersion());
356    }
357  6 if (entry.getUuid().equals("b82ea5d2-f682-4309-b229-f6fe835bf69c")) {
358  1 assertEquals("1.1", entry.getVersion());
359    }
360  6 if (entry.getUuid().equals("1e6c8151-831c-4eae-97fb-3c60846ba2a0")) {
361  1 assertEquals("1.2", entry.getVersion());
362    }
363  6 if (entry.getUuid().equals("1a59f6fd-0300-40b1-ad41-5b77e3766b50")) {
364  1 assertEquals("1.3", entry.getVersion());
365    }
366  6 if (entry.getUuid().equals("6ff62a22-9820-406d-b55a-a86fa1c5a033")) {
367  1 assertEquals("2.0", entry.getVersion());
368    }
369  6 if (entry.getUuid().equals("1de21a70-591b-4af8-8706-fc5581e90b0a")) {
370  1 assertEquals("2.0.1", entry.getVersion());
371    }
372    }
373    }
374   
 
375  1 toggle @Test
376    void updateConfigurationEntries() throws Exception {
377  1 LOGGER.log("TEST CASE: updateConfigurationEntries", LogLevel.DEBUG);
378   
379  1 List<ConfigurationDO> set
380    = configurationDAO.getAllConfigurationSetEntries("Module_B", "1.0", "Set_2");
381   
382  1 LOGGER.log(set.get(0).toString(), LogLevel.DEBUG);
383  1 LOGGER.log(set.get(1).toString(), LogLevel.DEBUG);
384  1 LOGGER.log(set.get(2).toString(), LogLevel.DEBUG);
385   
386  1 assertEquals(3, set.size());
387  1 ConfigurationDO a = set.get(0);
388  1 ConfigurationDO b = set.get(1);
389  1 ConfigurationDO c = set.get(2);
390   
391  1 assertEquals("d", a.getDefaultValue());
392  1 assertEquals("f", b.getDefaultValue());
393  1 assertEquals("e", c.getDefaultValue());
394   
395  1 a.setValue("a_01");
396  1 b.setValue("a_02");
397  1 c.setValue("a_03");
398   
399  1 List<ConfigurationDO> newSet = new ArrayList<>();
400  1 newSet.add(a);
401  1 newSet.add(b);
402  1 newSet.add(c);
403   
404  1 configurationDAO.updateConfigurationEntries(newSet);
405   
406  1 assertEquals("a_01", configurationDAO.find(a.getUuid()).getValue());
407  1 assertEquals("a_02", configurationDAO.find(b.getUuid()).getValue());
408  1 assertEquals("a_03", configurationDAO.find(c.getUuid()).getValue());
409    }
410   
 
411  1 toggle @Test
412    void failUpdateConfigurationEntries() throws Exception {
413  1 LOGGER.log("TEST CASE: failUpdateConfigurationEntries", LogLevel.DEBUG);
414   
415  1 ConfigurationDO entry = new ConfigurationDO();
416  1 entry.setKey("NOT-EXIST");
417  1 entry.setModulName("no-module");
418   
419  1 List<ConfigurationDO> set = new ArrayList<>();
420  1 set.add(entry);
421   
422  1 assertThrows(Exception.class, () -> {
423  1 configurationDAO.updateConfigurationEntries(set);
424    });
425    }
426   
 
427  1 toggle @Test
428    void getDeprecatedEntries() {
429  1 LOGGER.log("TEST CASE: getDeprecatedEntries", LogLevel.DEBUG);
430   
431  1 List<ConfigurationDO> result = configurationDAO.getAllDeprecatedEntries();
432  1 assertEquals(2, result.size());
433   
434  1 if (result.get(0).getUuid().equals("1de21a70-591b-4af8-8706-fc5581e90b0a")) {
435  0 assertEquals("1de21a70-591b-4af8-8706-fc5581e90b0a", result.get(0).getUuid());
436  0 assertEquals("6ff62a22-9820-406d-b55a-a86fa1c5a033", result.get(1).getUuid());
437    } else {
438  1 assertEquals("1de21a70-591b-4af8-8706-fc5581e90b0a", result.get(1).getUuid());
439  1 assertEquals("6ff62a22-9820-406d-b55a-a86fa1c5a033", result.get(0).getUuid());
440    }
441    }
442   
 
443  1 toggle @Test
444    void getAllModuleEntries() {
445  1 LOGGER.log("TEST CASE: getAllModuleEntries", LogLevel.DEBUG);
446   
447  1 List<ConfigurationDO> set = configurationDAO.getAllModuleEntries("Module_A");
448  1 assertEquals(10, set.size());
449    }
450    }