12
12
import io .vavr .collection .Set ;
13
13
import io .vavr .control .Either ;
14
14
import io .vavr .control .Option ;
15
- import org .junit .jupiter .api .Assertions ;
16
15
import org .junit .jupiter .api .Test ;
17
16
18
17
import java .io .IOException ;
19
18
import java .math .BigInteger ;
20
19
21
20
import static io .vavr .control .Option .none ;
22
21
import static io .vavr .control .Option .some ;
22
+ import static org .assertj .core .api .Assertions .assertThat ;
23
+ import static org .assertj .core .api .AssertionsForClassTypes .assertThatExceptionOfType ;
23
24
24
- public class EitherTest extends BaseTest {
25
+ class EitherTest extends BaseTest {
25
26
26
27
@ Test
27
28
void shouldSerializeAndDeserializeWrappedEitherObject () throws IOException {
28
29
ObjectMapper mapper = mapper ().addMixIn (Either .class , WrapperObject .class );
29
30
Either <Integer , Integer > src = Either .right (1 );
30
31
String plainJson = mapper ().writeValueAsString (src );
31
32
String wrappedJson = mapper .writeValueAsString (src );
32
- Assertions . assertEquals ( wrappedJson , wrapToObject (Either .Right .class .getName (), plainJson ));
33
+ assertThat ( wrapToObject (Either .Right .class .getName (), plainJson )). isEqualTo ( wrappedJson );
33
34
Either <Integer , Integer > restored = (Either <Integer , Integer >) mapper .readValue (wrappedJson , Either .class );
34
- Assertions . assertEquals (src , restored );
35
+ assertThat ( restored ). isEqualTo (src );
35
36
}
36
37
37
38
@ Test
38
39
void shouldSerializeAndDeserializeLeftEither () throws IOException {
39
40
Either <String , Integer > left = Either .left ("left" );
40
41
String json = mapper ().writer ().writeValueAsString (left );
41
42
Either <String , Integer > restored = mapper ().readValue (json , Either .class );
42
- Assertions . assertEquals (left , restored );
43
+ assertThat ( restored ). isEqualTo (left );
43
44
}
44
45
45
46
@ Test
46
47
void shouldSerializeAndDeserializeRightEither () throws IOException {
47
48
Either <String , Integer > right = Either .right (1 );
48
49
String json = mapper ().writer ().writeValueAsString (right );
49
50
Either <String , Integer > restored = mapper ().readValue (json , Either .class );
50
- Assertions . assertEquals (right , restored );
51
+ assertThat ( restored ). isEqualTo (right );
51
52
}
52
53
53
54
@ Test
54
55
void shouldThrowExceptionWhenInvalidJsonHasExtraRightValue () throws IOException {
55
- Assertions .assertThrows (JsonMappingException .class , () -> {
56
- String json = "[\" right\" , 2, 3]" ;
57
- mapper ().readValue (json , Either .class );
58
- });
56
+ String json = "[\" right\" , 2, 3]" ;
57
+ assertThatExceptionOfType (JsonMappingException .class ).isThrownBy (() ->
58
+ mapper ().readValue (json , Either .class ));
59
59
}
60
60
61
61
@ Test
62
62
void shouldThrowExceptionWhenInvalidJsonHasMissingRightValue () throws IOException {
63
- Assertions .assertThrows (JsonMappingException .class , () -> {
64
- String json = "[\" right\" ]" ;
65
- mapper ().readValue (json , Either .class );
66
- });
63
+ String json = "[\" right\" ]" ;
64
+ assertThatExceptionOfType (JsonMappingException .class ).isThrownBy (() ->
65
+ mapper ().readValue (json , Either .class ));
67
66
}
68
67
69
68
@ Test
70
69
void shouldThrowExceptionWhenInvalidJsonHasMalformedLeftType () throws IOException {
71
- Assertions .assertThrows (JsonMappingException .class , () -> {
72
- String json = "[\" lEft\" , 42]" ;
73
- mapper ().readValue (json , Either .class );
74
- });
70
+ String json = "[\" lEft\" , 42]" ;
71
+ assertThatExceptionOfType (JsonMappingException .class ).isThrownBy (() ->
72
+ mapper ().readValue (json , Either .class ));
75
73
}
76
74
77
75
@ Test
78
76
void shouldThrowExceptionWhenInvalidJsonHasNonStringLeftOrRight () throws IOException {
79
- Assertions .assertThrows (JsonMappingException .class , () -> {
80
- String json = "[42, 42]" ;
81
- mapper ().readValue (json , Either .class );
82
- });
77
+ String json = "[42, 42]" ;
78
+ assertThatExceptionOfType (JsonMappingException .class ).isThrownBy (() ->
79
+ mapper ().readValue (json , Either .class ));
83
80
}
84
81
85
82
@ Test
@@ -88,7 +85,7 @@ void shouldSerializeAndDeserializeLeftEitherWithGenericTypes() throws IOExceptio
88
85
String json = mapper ().writer ().writeValueAsString (either );
89
86
Either <List <Integer >, Set <Double >> restored = mapper ().readValue (json , new TypeReference <Either <List <Integer >, Set <Double >>>() {
90
87
});
91
- Assertions . assertEquals (either , restored );
88
+ assertThat ( restored ). isEqualTo (either );
92
89
}
93
90
94
91
@ Test
@@ -97,23 +94,23 @@ void shouldSerializeAndDeserializeRightEitherWithGenericTypes() throws IOExcepti
97
94
String json = mapper ().writer ().writeValueAsString (either );
98
95
Either <List <Integer >, Set <Double >> restored = mapper ().readValue (json , new TypeReference <Either <List <Integer >, Set <Double >>>() {
99
96
});
100
- Assertions . assertEquals (either , restored );
97
+ assertThat ( restored ). isEqualTo (either );
101
98
}
102
99
103
100
@ Test
104
101
void shouldHandleNullInLeftEitherDuringSerialization () throws IOException {
105
102
Either <String , Integer > left = Either .left (null );
106
103
String leftJson = mapper ().writer ().writeValueAsString (left );
107
104
Either <String , Integer > restoredLeft = mapper ().readValue (leftJson , Either .class );
108
- Assertions . assertEquals (left , restoredLeft );
105
+ assertThat ( restoredLeft ). isEqualTo (left );
109
106
}
110
107
111
108
@ Test
112
109
void shouldHandleNullInRightEitherDuringSerialization () throws IOException {
113
110
Either <String , Integer > right = Either .left (null );
114
111
String rightJson = mapper ().writer ().writeValueAsString (right );
115
112
Either <String , Integer > restoredRight = mapper ().readValue (rightJson , Either .class );
116
- Assertions . assertEquals (right , restoredRight );
113
+ assertThat ( restoredRight ). isEqualTo (right );
117
114
}
118
115
119
116
@ Test
@@ -132,13 +129,13 @@ void shouldSerializeAndDeserializeEitherWithOption() throws IOException {
132
129
void shouldSerializeAndDeserializeCustomTypeWithJsonTypeInfo () throws IOException {
133
130
String javaUtilValue ;
134
131
javaUtilValue = mapper ().writeValueAsString (new Left ());
135
- Assertions . assertEquals ("{\" f\" :[\" left\" ,{\" card\" :{\" type\" :\" hello\" }}]}" , javaUtilValue );
132
+ assertThat ( javaUtilValue ). isEqualTo ("{\" f\" :[\" left\" ,{\" card\" :{\" type\" :\" hello\" }}]}" );
136
133
Left restoredLeft = mapper ().readValue (javaUtilValue , Left .class );
137
- Assertions . assertEquals ( "hello" , restoredLeft .f .getLeft ().type );
134
+ assertThat ( restoredLeft .f .getLeft ().type ). isEqualTo ( "hello" );
138
135
javaUtilValue = mapper ().writeValueAsString (new Right ());
139
- Assertions . assertEquals ("{\" f\" :[\" right\" ,{\" card\" :{\" type\" :\" hello\" }}]}" , javaUtilValue );
136
+ assertThat ( javaUtilValue ). isEqualTo ("{\" f\" :[\" right\" ,{\" card\" :{\" type\" :\" hello\" }}]}" );
140
137
Right restoredRight = mapper ().readValue (javaUtilValue , Right .class );
141
- Assertions . assertEquals ( "hello" , restoredRight .f .get ().type );
138
+ assertThat ( restoredRight .f .get ().type ). isEqualTo ( "hello" );
142
139
}
143
140
144
141
@ Test
@@ -151,13 +148,13 @@ void shouldSerializeAndDeserializeScalaEither() throws IOException {
151
148
final Either <String , BigInteger > deserializedLeft =
152
149
mapper .readValue (serializedLeft , new TypeReference <Either <String , BigInteger >>() {
153
150
});
154
- Assertions . assertEquals ( "test" , deserializedLeft .getLeft ());
151
+ assertThat ( deserializedLeft .getLeft ()). isEqualTo ( "test" );
155
152
156
153
final String serializedRight = mapper .writeValueAsString (right );
157
154
final Either <String , BigInteger > deserializedRight =
158
155
mapper .readValue (serializedRight , new TypeReference <Either <String , BigInteger >>() {
159
156
});
160
- Assertions . assertEquals ( BigInteger . ONE , deserializedRight .get ());
157
+ assertThat ( deserializedRight .get ()). isEqualTo ( BigInteger . ONE );
161
158
}
162
159
163
160
@ JsonTypeInfo (
0 commit comments