33
44package org .terasology .persistence .typeHandling .coreTypes ;
55
6+ import org .junit .jupiter .api .DisplayName ;
67import org .junit .jupiter .api .Test ;
78import org .terasology .persistence .typeHandling .PersistedData ;
89import org .terasology .persistence .typeHandling .PersistedDataSerializer ;
1213import org .terasology .persistence .typeHandling .inMemory .PersistedString ;
1314import org .terasology .persistence .typeHandling .inMemory .arrays .PersistedValueArray ;
1415
15- import java .util .Collections ;
1616import java .util .List ;
1717import java .util .Map ;
1818import java .util .Optional ;
@@ -25,14 +25,102 @@ class GenericMapTypeHandlerTest {
2525 private static final String TEST_KEY = "health:baseRegen" ;
2626 private static final long TEST_VALUE = -1 ;
2727
28+ /**
29+ * JSON equivalent:
30+ * <pre><code>
31+ * [
32+ * {
33+ * "key": "health:baseRegen",
34+ * "value": -1
35+ * }
36+ * ]
37+ * </code></pre>
38+ */
2839 private final PersistedData testData = new PersistedValueArray (List .of (
2940 new PersistedMap (Map .of (
3041 GenericMapTypeHandler .KEY , new PersistedString (TEST_KEY ),
3142 GenericMapTypeHandler .VALUE , new PersistedLong (TEST_VALUE )
3243 ))
3344 ));
3445
46+ /**
47+ * JSON equivalent:
48+ * <pre><code>
49+ * {
50+ * "health:baseRegen": -1
51+ * }
52+ * </code></pre>
53+ */
54+ private final PersistedData testDataMalformatted = new PersistedValueArray (List .of (
55+ new PersistedMap (Map .of (
56+ TEST_KEY , new PersistedLong (TEST_VALUE )
57+ ))
58+ ));
59+
60+ /**
61+ * JSON equivalent:
62+ * <pre><code>
63+ * [
64+ * {
65+ * "not key": "health:baseRegen",
66+ * "value": -1
67+ * }
68+ * ]
69+ * </code></pre>
70+ */
71+ private final PersistedData testDataMissingKeyEntry = new PersistedValueArray (List .of (
72+ new PersistedMap (Map .of (
73+ "not key" , new PersistedString (TEST_KEY ),
74+ GenericMapTypeHandler .VALUE , new PersistedLong (TEST_VALUE )
75+ ))
76+ ));
77+
78+ /**
79+ * JSON equivalent:
80+ * <pre><code>
81+ * [
82+ * {
83+ * "key": "health:baseRegen",
84+ * "not value": -1
85+ * }
86+ * ]
87+ * </code></pre>
88+ */
89+ private final PersistedData testDataMissingValueEntry = new PersistedValueArray (List .of (
90+ new PersistedMap (Map .of (
91+ GenericMapTypeHandler .KEY , new PersistedString (TEST_KEY ),
92+ "not value" , new PersistedLong (TEST_VALUE )
93+ ))
94+ ));
95+
96+ /**
97+ * JSON equivalent:
98+ * <pre><code>
99+ * [
100+ * {
101+ * "key": "health:baseRegen",
102+ * "value": -1
103+ * },
104+ * {
105+ * "not key": "health:baseRegen",
106+ * "not value": -1
107+ * },
108+ * ]
109+ * </code></pre>
110+ */
111+ private final PersistedData testDataValidAndInvalidMix = new PersistedValueArray (List .of (
112+ new PersistedMap (Map .of (
113+ GenericMapTypeHandler .KEY , new PersistedString (TEST_KEY ),
114+ GenericMapTypeHandler .VALUE , new PersistedLong (TEST_VALUE )
115+ )),
116+ new PersistedMap (Map .of (
117+ "not key" , new PersistedString (TEST_KEY ),
118+ "not value" , new PersistedLong (TEST_VALUE )
119+ ))
120+ ));
121+
35122 @ Test
123+ @ DisplayName ("Data with valid formatting can be deserialized successfully." )
36124 void testDeserialize () {
37125 var th = new GenericMapTypeHandler <>(
38126 new StringTypeHandler (),
@@ -44,23 +132,69 @@ void testDeserialize() {
44132 }
45133
46134 @ Test
135+ @ DisplayName ("Deserializing valid data with a mismatching value type handler fails deserialization (returns empty `Optional`)" )
47136 void testDeserializeWithMismatchedValueHandler () {
48137 var th = new GenericMapTypeHandler <>(
49138 new StringTypeHandler (),
50139 new UselessTypeHandler <>()
51140 );
52141
53- assertThat (th .deserialize (testData )).hasValue ( Collections . emptyMap () );
142+ assertThat (th .deserialize (testData )).isEmpty ( );
54143 }
55144
56145 @ Test
146+ @ DisplayName ("Deserializing valid data with a mismatching key type handler fails deserialization (returns empty `Optional`)" )
57147 void testDeserializeWithMismatchedKeyHandler () {
58148 var th = new GenericMapTypeHandler <>(
59149 new UselessTypeHandler <>(),
60150 new LongTypeHandler ()
61151 );
62152
63- assertThat (th .deserialize (testData )).hasValue (Collections .emptyMap ());
153+ assertThat (th .deserialize (testData )).isEmpty ();
154+ }
155+
156+ @ Test
157+ @ DisplayName ("Incorrectly formatted data (without an outer array) fails deserialization (returns empty `Optional`)" )
158+ void testDeserializeWithObjectInsteadOfArray () {
159+ var th = new GenericMapTypeHandler <>(
160+ new StringTypeHandler (),
161+ new LongTypeHandler ()
162+ );
163+
164+ assertThat (th .deserialize (testDataMalformatted )).isEmpty ();
165+ }
166+
167+ @ Test
168+ @ DisplayName ("Incorrectly formatted data (without a map entry with key \" key\" ) fails deserialization (returns empty `Optional`)" )
169+ void testDeserializeWithMissingKeyEntry () {
170+ var th = new GenericMapTypeHandler <>(
171+ new StringTypeHandler (),
172+ new LongTypeHandler ()
173+ );
174+
175+ assertThat (th .deserialize (testDataMissingKeyEntry )).isEmpty ();
176+ }
177+
178+ @ Test
179+ @ DisplayName ("Incorrectly formatted data (without a map entry with key \" value\" ) fails deserialization (returns empty `Optional`)" )
180+ void testDeserializeWithMissingValueEntry () {
181+ var th = new GenericMapTypeHandler <>(
182+ new StringTypeHandler (),
183+ new LongTypeHandler ()
184+ );
185+
186+ assertThat (th .deserialize (testDataMissingValueEntry )).isEmpty ();
187+ }
188+
189+ @ Test
190+ @ DisplayName ("A map containing both, correctly and incorrectly formatted data, fails deserialization (returns empty `Optional`)" )
191+ void testDeserializeWithValidAndInvalidEntries () {
192+ var th = new GenericMapTypeHandler <>(
193+ new StringTypeHandler (),
194+ new LongTypeHandler ()
195+ );
196+
197+ assertThat (th .deserialize (testDataValidAndInvalidMix )).isEmpty ();
64198 }
65199
66200 /** Never returns a value. */
0 commit comments