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

File JacksonJsonToolsTest.java

 

Code metrics

0
33
10
1
126
101
10
0.3
3.3
10
1

Classes

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