1
+ package com .orange .lo .sample .lo2iothub .lo ;
2
+
3
+ import com .orange .lo .sample .lo2iothub .utils .Counters ;
4
+ import io .micrometer .core .instrument .Counter ;
5
+ import io .micrometer .core .instrument .MeterRegistry ;
6
+ import io .micrometer .core .instrument .step .StepMeterRegistry ;
7
+ import org .junit .jupiter .api .BeforeEach ;
8
+ import org .junit .jupiter .api .Test ;
9
+ import org .junit .jupiter .api .extension .ExtendWith ;
10
+ import org .mockito .junit .jupiter .MockitoExtension ;
11
+
12
+ import java .util .concurrent .atomic .AtomicInteger ;
13
+
14
+ import static org .junit .jupiter .api .Assertions .assertFalse ;
15
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
16
+ import static org .mockito .ArgumentMatchers .any ;
17
+ import static org .mockito .ArgumentMatchers .eq ;
18
+ import static org .mockito .Mockito .mock ;
19
+ import static org .mockito .Mockito .when ;
20
+
21
+ @ ExtendWith (MockitoExtension .class )
22
+ class LoMqttReconnectHandlerTest {
23
+
24
+ private Counters counters ;
25
+ private LoMqttReconnectHandler loMqttReconnectHandler ;
26
+
27
+ @ BeforeEach
28
+ void setUp () {
29
+ MeterRegistry meterRegistry = mockMeterRegistry ();
30
+ this .counters = new Counters (meterRegistry );
31
+ this .loMqttReconnectHandler = new LoMqttReconnectHandler (counters );
32
+ }
33
+
34
+ @ Test
35
+ void shouldChangeLoConnectionStausWhenConnectComplete () {
36
+ loMqttReconnectHandler .connectComplete (false , "" );
37
+
38
+ assertTrue (counters .isLoConnectionStatusUp ());
39
+ }
40
+
41
+ @ Test
42
+ void shouldChangeLoConnectionStausWhenConnectionLost () {
43
+ loMqttReconnectHandler .connectionLost (new Exception ());
44
+
45
+ assertFalse (counters .isLoConnectionStatusUp ());
46
+ }
47
+
48
+ private MeterRegistry mockMeterRegistry () {
49
+ StepMeterRegistry meterRegistry = mock (StepMeterRegistry .class );
50
+ when (meterRegistry .counter ("message.read" )).thenReturn (mock (Counter .class ));
51
+ when (meterRegistry .counter ("message.sent" )).thenReturn (mock (Counter .class ));
52
+ when (meterRegistry .counter ("message.sent.attempt" )).thenReturn (mock (Counter .class ));
53
+ when (meterRegistry .counter ("message.sent.attempt.failed" )).thenReturn (mock (Counter .class ));
54
+ when (meterRegistry .counter ("message.sent.failed" )).thenReturn (mock (Counter .class ));
55
+ when (meterRegistry .gauge (eq ("status.connection.lo" ), any ())).thenReturn (new AtomicInteger ());
56
+ when (meterRegistry .gauge (eq ("status.connection.cloud" ), any ())).thenReturn (new AtomicInteger ());
57
+
58
+ return meterRegistry ;
59
+ }
60
+ }
0 commit comments