44import static io .arex .inst .common .util .FluxReplayUtil .FLUX_FROM_ITERATOR ;
55import static io .arex .inst .common .util .FluxReplayUtil .FLUX_FROM_STREAM ;
66import static org .junit .jupiter .api .Assertions .assertEquals ;
7- import static org .junit .jupiter .api .Assertions .assertNotNull ;
7+
8+ import com .fasterxml .jackson .core .JsonProcessingException ;
9+ import com .fasterxml .jackson .databind .JavaType ;
10+ import com .fasterxml .jackson .databind .ObjectMapper ;
11+ import io .arex .agent .bootstrap .util .StringUtil ;
812import io .arex .inst .common .util .FluxReplayUtil .FluxElementResult ;
913import io .arex .inst .common .util .FluxReplayUtil .FluxResult ;
14+ import io .arex .inst .runtime .serializer .Serializer ;
15+ import io .arex .inst .runtime .serializer .StringSerializable ;
1016import io .arex .inst .runtime .util .TypeUtil ;
17+ import io .lettuce .core .KeyValue ;
18+ import java .lang .reflect .Type ;
1119import java .util .ArrayList ;
1220import java .util .List ;
21+ import org .junit .jupiter .api .AfterAll ;
22+ import org .junit .jupiter .api .BeforeAll ;
1323import org .junit .jupiter .api .Test ;
24+ import org .mockito .Mockito ;
1425import reactor .core .publisher .Flux ;
26+ import reactor .test .StepVerifier ;
1527
1628public class FluxReplayUtilTest {
29+ @ BeforeAll
30+ static void setUp () {
31+ Serializer .builder (new TestJacksonSerializable ()).build ();
32+ }
1733
34+ @ AfterAll
35+ static void tearDown () {
36+ Mockito .clearAllCaches ();
37+ }
1838 @ Test
19- void FluxRecory () {
39+ void restore () {
40+ // flux is empty
41+ Flux <?> result = FluxReplayUtil .restore (null );
42+ StepVerifier .create (result )
43+ .expectComplete ()
44+ .verify ();
45+
2046 List <FluxElementResult > list = new ArrayList <>();
2147 FluxResult fluxResult = new FluxResult (null , list );
22- // flux is empty
23- assertNotNull ( FluxReplayUtil . restore ( null ));
24- Flux <?> result = FluxReplayUtil . restore ( fluxResult );
25- assertNotNull ( result );
48+ result = FluxReplayUtil . restore ( fluxResult );
49+ StepVerifier . create ( result )
50+ . expectComplete ()
51+ . verify ( );
2652
2753 // flux is not empty
2854 FluxElementResult fluxElement1 = new FluxElementResult (1 , "1" , "java.lang.Integer" );
@@ -33,21 +59,95 @@ void FluxRecory() {
3359 // Flux.just()
3460 fluxResult = new FluxResult (null , list );
3561 result = FluxReplayUtil .restore (fluxResult );
36- assertEquals (TypeUtil .getName (result ),"reactor.core.publisher.FluxJust-java.util.ArrayList-" );
62+ assertEquals (TypeUtil .getName (result ),"reactor.core.publisher.FluxIterable-" );
63+ StepVerifier .create (result )
64+ .expectNextMatches (item -> item .equals (1 ))
65+ .expectError (NullPointerException .class )
66+ .verify ();
3767
3868 // Flux.fromIterable()
3969 fluxResult = new FluxResult (FLUX_FROM_ITERATOR , list );
4070 result = FluxReplayUtil .restore (fluxResult );
4171 assertEquals (TypeUtil .getName (result ),FLUX_FROM_ITERATOR );
72+ StepVerifier .create (result )
73+ .expectNextMatches (item -> item .equals (1 ))
74+ .expectError (NullPointerException .class )
75+ .verify ();
4276
4377 // Flux.fromArray()
4478 fluxResult = new FluxResult (FLUX_FROM_ARRAY , list );
4579 result = FluxReplayUtil .restore (fluxResult );
4680 assertEquals (TypeUtil .getName (result ),FLUX_FROM_ARRAY );
81+ StepVerifier .create (result )
82+ .expectNextMatches (item -> item .equals (1 ))
83+ .expectError (NullPointerException .class )
84+ .verify ();
4785
4886 // Flux.fromStream()
4987 fluxResult = new FluxResult (FLUX_FROM_STREAM , list );
5088 result = FluxReplayUtil .restore (fluxResult );
5189 assertEquals (TypeUtil .getName (result ),FLUX_FROM_STREAM );
90+ StepVerifier .create (result )
91+ .expectNextMatches (item -> item .equals (1 ))
92+ .expectError (NullPointerException .class )
93+ .verify ();
94+ }
95+
96+ @ Test
97+ void testReactiveMGet () {
98+ List <FluxElementResult > fluxElementResults = new ArrayList <>(2 );
99+ FluxElementResult elementResult1 = new FluxElementResult (1 , "{\" key\" :\" mget-key1\" ,\" value\" :\" mget-value1-2024-04-02 16:37\" }" , "io.lettuce.core.KeyValue-java.lang.String,java.lang.String" );
100+ FluxElementResult elementResult2 = new FluxElementResult (1 , "{\" key\" :\" mget-key2\" ,\" value\" :\" mget-value2-2024-04-02 16:37\" }" , "io.lettuce.core.KeyValue-java.lang.String,java.lang.String" );
101+ fluxElementResults .add (elementResult1 );
102+ fluxElementResults .add (elementResult2 );
103+ FluxResult fluxResult = new FluxResult ("reactor.core.publisher.FluxSource-" , fluxElementResults );
104+ Flux <KeyValue <String , String >> restore = FluxReplayUtil .restore (fluxResult );
105+ StepVerifier .create (restore )
106+ .expectNextMatches (item -> item .getKey ().equals ("mget-key1" ) && item .getValue ().equals ("mget-value1-2024-04-02 16:37" ))
107+ .expectNextMatches (item -> item .getKey ().equals ("mget-key2" ) && item .getValue ().equals ("mget-value2-2024-04-02 16:37" ))
108+ .expectComplete ().verify ();
109+ }
110+
111+ public static class TestJacksonSerializable implements StringSerializable {
112+ private final ObjectMapper MAPPER = new ObjectMapper ();
113+
114+ @ Override
115+ public boolean isDefault () {
116+ return true ;
117+ }
118+
119+ @ Override
120+ public String name () {
121+ return "jackson" ;
122+ }
123+
124+ @ Override
125+ public String serialize (Object object ) throws JsonProcessingException {
126+ return MAPPER .writeValueAsString (object );
127+ }
128+
129+ @ Override
130+ public <T > T deserialize (String json , Class <T > clazz ) throws JsonProcessingException {
131+ if (StringUtil .isEmpty (json ) || clazz == null ) {
132+ return null ;
133+ }
134+
135+ return MAPPER .readValue (json , clazz );
136+ }
137+
138+ @ Override
139+ public <T > T deserialize (String json , Type type ) throws JsonProcessingException {
140+ if (StringUtil .isEmpty (json ) || type == null ) {
141+ return null ;
142+ }
143+
144+ JavaType javaType = MAPPER .getTypeFactory ().constructType (type );
145+ return MAPPER .readValue (json , javaType );
146+ }
147+
148+ @ Override
149+ public StringSerializable reCreateSerializer () {
150+ return new TestJacksonSerializable ();
151+ }
52152 }
53153}
0 commit comments