1. Project Clover database Mon Dec 22 2025 14:59:38 CST
  2. Package org.europa.together.application

File JacksonJsonToolsTest.java

 

Code metrics

0
33
10
1
125
100
10
0.3
3.3
10
1

Classes

Class Line # Actions
JacksonJsonToolsTest 23 33 0% 10 0
1.0100%
 

Contributing tests

This file is covered by 6 tests. .

Source view

1    package org.europa.together.application;
2   
3    import org.europa.together.JUnit5Preperator;
4    import org.europa.together.business.JsonTools;
5    import org.europa.together.business.Logger;
6    import org.europa.together.domain.LogLevel;
7    import org.europa.together.domain.JacksonObjectTest;
8    import org.junit.jupiter.api.AfterAll;
9    import org.junit.jupiter.api.AfterEach;
10    import org.junit.jupiter.api.BeforeAll;
11    import org.junit.jupiter.api.BeforeEach;
12    import org.junit.jupiter.api.extension.ExtendWith;
13    import org.springframework.test.context.ContextConfiguration;
14    import org.springframework.test.context.junit.jupiter.SpringExtension;
15    import org.junit.jupiter.api.Test;
16    import static org.junit.jupiter.api.Assertions.*;
17    import org.junit.jupiter.api.Assumptions;
18    import org.springframework.beans.factory.annotation.Autowired;
19   
20    @SuppressWarnings("unchecked")
21    @ExtendWith({SpringExtension.class, JUnit5Preperator.class})
22    @ContextConfiguration(locations = {"/applicationContext.xml"})
 
23    public class JacksonJsonToolsTest {
24   
25    private static final Logger LOGGER = new LogbackLogger(JacksonJsonToolsTest.class);
26   
27    @Autowired
28    private JsonTools<JacksonObjectTest> jsonTools;
29   
30    //<editor-fold defaultstate="collapsed" desc="Test Preparation">
 
31  1 toggle @BeforeAll
32    static void setUp() {
33  1 Assumptions.assumeTrue(true, "Assumtion failed.");
34   
35  1 LOGGER.log("Assumptions passed ...\n\n", LogLevel.DEBUG);
36    }
37   
 
38  1 toggle @AfterAll
39    static void tearDown() {
40    }
41   
 
42  6 toggle @BeforeEach
43    void testCaseInitialization() {
44    }
45   
 
46  6 toggle @AfterEach
47    void testCaseTermination() {
48    }
49    //</editor-fold>
50   
 
51  1 toggle @Test
52    void serializeObjectToJson() throws Exception {
53  1 LOGGER.log("TEST CASE: serializeObjectToJson", LogLevel.DEBUG);
54   
55  1 JacksonObjectTest domainObject = new JacksonObjectTest();
56  1 domainObject.setId("identifier");
57  1 domainObject.setKey("key");
58  1 domainObject.setValue(12345);
59  1 domainObject.setActivation(true);
60   
61  1 String json = "{\"id\":\"identifier\",\"key\":\"key\",\"value\":12345,\"activation\":true}";
62  1 assertEquals(json, jsonTools.serializeAsJsonObject(domainObject));
63    }
64   
 
65  1 toggle @Test
66    void failSerializeObjectToJson() throws Exception {
67  1 LOGGER.log("TEST CASE: failSerializeObjectToJson", LogLevel.DEBUG);
68   
69  1 assertThrows(Exception.class, () -> {
70  1 jsonTools.serializeAsJsonObject(null);
71    });
72    }
73   
 
74  1 toggle @Test
75    void deserializeJsonToObject() throws Exception {
76  1 LOGGER.log("TEST CASE: deserializeJsonToObject", LogLevel.DEBUG);
77   
78  1 JacksonObjectTest domainObject = new JacksonObjectTest();
79  1 domainObject.setId("identifier");
80  1 domainObject.setKey("key");
81  1 domainObject.setValue(12345);
82  1 domainObject.setActivation(true);
83   
84  1 String json = "{\"id\":\"identifier\",\"key\":\"key\",\"value\":12345,\"activation\":true}";
85  1 JacksonObjectTest deserializion = jsonTools.deserializeJsonAsObject(json, JacksonObjectTest.class);
86  1 assertEquals("JacksonObjectTest{id=identifier, key=key, value=12345, activation=true}",
87    deserializion.toString());
88    }
89   
 
90  1 toggle @Test
91    void failDeserializeJsonToObject() throws Exception {
92  1 LOGGER.log("TEST CASE: failDeserializeJsonToObject", LogLevel.DEBUG);
93   
94  1 String json = "{\"xx\":\"identifier01\",\"key\":\"key\",\"value\":'123',\"activation\":false}";
95  1 assertThrows(Exception.class, () -> {
96  1 jsonTools.deserializeJsonAsObject(json, JacksonObjectTest.class);
97    });
98    }
99   
 
100  1 toggle @Test
101    void deserializeJsonAsObjectList() throws Exception {
102  1 LOGGER.log("TEST CASE: deserializeJsonAsObjectList", LogLevel.DEBUG);
103   
104  1 String json = "["
105    + "{\"id\":\"identifier00\",\"key\":\"low.number\",\"value\":12345,\"activation\":true}, "
106    + "{\"id\":\"identifier01\",\"key\":\"high.number\",\"value\":67890,\"activation\":false}, "
107    + "{\"id\":\"identifier02\",\"key\":\"amount\",\"value\":100,\"activation\":false}, "
108    + "{\"id\":\"identifier03\",\"key\":\"key\",\"value\":99099,\"activation\":true}"
109    + "]";
110  1 assertEquals(4, jsonTools.deserializeJsonAsList(json).size());
111    }
112   
 
113  1 toggle @Test
114    void failDeserializeJsonAsObjectList() throws Exception {
115  1 LOGGER.log("TEST CASE: failDeserializeJsonAsObjectList", LogLevel.DEBUG);
116   
117  1 String json = "{"
118    + "{\"id\":\"identifier00\",\"key\":\"low.number\",\"value\":12345,\"activation\":true}"
119    + "{\"id\":\"identifier01\",\"key\":\"high.number\",\"value\":67890,\"activation\":false}"
120    + "}";
121  1 assertThrows(Exception.class, () -> {
122  1 jsonTools.deserializeJsonAsList(json);
123    });
124    }
125    }