forked from schananas/practical-reactor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
c2_TransformingSequence.java
139 lines (122 loc) · 4.24 KB
/
c2_TransformingSequence.java
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import org.junit.jupiter.api.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
/**
* It's time to do some data manipulation!
*
* Read first:
*
* https://projectreactor.io/docs/core/release/reference/#which.values
*
* Useful documentation:
*
* https://projectreactor.io/docs/core/release/reference/#which-operator
* https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Mono.html
* https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html
*
* @author Stefan Dragisic
*/
public class c2_TransformingSequence extends TransformingSequenceBase {
/***
* Your task is simple:
* Increment each number emitted by the numerical service
*/
@Test
public void transforming_sequence() {
Flux<Integer> numbersFlux = numerical_service()
//todo change only this line
;
//StepVerifier is used for testing purposes
//ignore it for now, or explore it independently
StepVerifier.create(numbersFlux)
.expectNext(2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
.verifyComplete();
}
/***
* Transform given number sequence to:
* - ">": if given number is greater than 0
* - "=": if number is equal to 0
* - "<": if given number is lesser then 0
*/
@Test
public void transforming_sequence_2() {
Flux<Integer> numbersFlux = numerical_service_2();
//todo: do your changes here
Flux<String> resultSequence = null;
//don't change code below
StepVerifier.create(resultSequence)
.expectNext(">", "<", "=", ">", ">")
.verifyComplete();
}
/**
* `object_service()` streams sequence of Objects, but if you peek into service implementation, you can see
* that these items are in fact strings!
* Casting using `map()` to cast is one way to do it, but there is more convenient way.
* Remove `map` operator and use more appropriate operator to cast sequence to String.
*/
@Test
public void cast() {
Flux<String> numbersFlux = object_service()
.map(i -> (String) i); //todo: change this line only
StepVerifier.create(numbersFlux)
.expectNext("1", "2", "3", "4", "5")
.verifyComplete();
}
/**
* `maybe_service()` may return some result.
* In case it doesn't return any result, return value "no results".
*/
@Test
public void maybe() {
Mono<String> result = maybe_service()
//todo: change this line only
;
StepVerifier.create(result)
.expectNext("no results")
.verifyComplete();
}
/**
* Reduce the values from `numerical_service()` into a single number that is equal to sum of all numbers emitted by
* this service.
*/
@Test
public void sequence_sum() {
//todo: change code as you need
Mono<Integer> sum = null;
numerical_service();
//don't change code below
StepVerifier.create(sum)
.expectNext(55)
.verifyComplete();
}
/***
* Reduce the values from `numerical_service()` but emit each intermediary number
* Use first Flux value as initial value.
*/
@Test
public void sum_each_successive() {
Flux<Integer> sumEach = numerical_service()
//todo: do your changes here
;
StepVerifier.create(sumEach)
.expectNext(1, 3, 6, 10, 15, 21, 28, 36, 45, 55)
.verifyComplete();
}
/**
* A developer who wrote `numerical_service()` forgot that sequence should start with zero, so you must prepend zero
* to result sequence.
*
* Do not alter `numerical_service` implementation!
* Use only one operator.
*/
@Test
public void sequence_starts_with_zero() {
Flux<Integer> result = numerical_service()
//todo: change this line only
;
StepVerifier.create(result)
.expectNext(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
.verifyComplete();
}
}