-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathJsonNullableStreamApiTest.java
More file actions
71 lines (52 loc) · 1.92 KB
/
JsonNullableStreamApiTest.java
File metadata and controls
71 lines (52 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package org.openapitools.jackson.nullable;
import junit.framework.TestCase;
import org.junit.Test;
import static junit.framework.TestCase.*;
public class JsonNullableStreamApiTest {
@Test
public void shouldReturnUndefinedAfterStream() {
final BigDto bigDto = new BigDto(null);
final JsonNullable<String> accessedValue = JsonNullable.ofMissable(bigDto)
.map(BigDto::getChild)
.map(ChildOfBigDto::getGrandChild);
assertEquals(accessedValue, JsonNullable.undefined());
}
@Test
public void shouldReturnNullAfterStream() {
final BigDto bigDto = new BigDto(null);
// we want to receive null state in the end
JsonNullable.ofMissable(bigDto)
// that is why we explicitly allow JsonNullable to produce NULL state in the flow
.flatMap(dto -> JsonNullable.of(dto.getChild()))
.map(ChildOfBigDto::getGrandChild)
.ifPresent(TestCase::assertNull);
}
@Test
public void consumeNotNullValue() {
final String goldenEgg = "goldenEgg";
final ChildOfBigDto child = new ChildOfBigDto("goldenEgg");
final BigDto testedDto = new BigDto(child);
JsonNullable.ofMissable(testedDto)
.map(BigDto::getChild)
.map(ChildOfBigDto::getGrandChild)
.ifNotNull(grandChild -> assertEquals(grandChild, goldenEgg));
}
private static class BigDto {
private ChildOfBigDto child;
public BigDto(ChildOfBigDto child) {
this.child = child;
}
public ChildOfBigDto getChild() {
return this.child;
}
}
private static class ChildOfBigDto {
private String grandChild;
public ChildOfBigDto(String grandChild) {
this.grandChild = grandChild;
}
public String getGrandChild() {
return grandChild;
}
}
}