1
+ package net .devh .boot .grpc .server .health ;
2
+
3
+ import io .grpc .StatusException ;
4
+ import io .grpc .health .v1 .HealthCheckRequest ;
5
+ import io .grpc .health .v1 .HealthCheckResponse ;
6
+ import io .grpc .internal .testing .StreamRecorder ;
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 .Mock ;
11
+ import org .mockito .junit .jupiter .MockitoExtension ;
12
+ import org .springframework .boot .actuate .health .Health ;
13
+ import org .springframework .boot .actuate .health .HealthComponent ;
14
+ import org .springframework .boot .actuate .health .HealthEndpoint ;
15
+
16
+ import java .util .List ;
17
+
18
+ import static org .junit .jupiter .api .Assertions .*;
19
+ import static org .mockito .Mockito .when ;
20
+
21
+ @ ExtendWith (MockitoExtension .class )
22
+ class ActuatorGrpcHealthTest {
23
+ @ Mock
24
+ HealthEndpoint healthEndpoint ;
25
+
26
+ ActuatorGrpcHealth server ;
27
+
28
+ @ BeforeEach
29
+ public void setup () {
30
+ this .server = new ActuatorGrpcHealth (healthEndpoint );
31
+ }
32
+
33
+
34
+ @ Test
35
+ void testDefaultServiceWorking () {
36
+ StreamRecorder <HealthCheckResponse > response = StreamRecorder .create ();
37
+ HealthCheckRequest request = HealthCheckRequest .newBuilder ().setService ("" ).build ();
38
+
39
+ HealthComponent healthResult = Health .up ().build ();
40
+
41
+ when (healthEndpoint .health ())
42
+ .thenReturn (healthResult );
43
+
44
+ server .check (request , response );
45
+
46
+ List <HealthCheckResponse > result = response .getValues ();
47
+ assertEquals (1 , result .size ());
48
+ assertEquals (HealthCheckResponse .ServingStatus .SERVING , result .get (0 ).getStatus ());
49
+ }
50
+
51
+ @ Test
52
+ void testDefaultServiceNotWorking () {
53
+ StreamRecorder <HealthCheckResponse > response = StreamRecorder .create ();
54
+ HealthCheckRequest request = HealthCheckRequest .newBuilder ().setService ("" ).build ();
55
+
56
+ HealthComponent healthResult = Health .down ().build ();
57
+
58
+ when (healthEndpoint .health ())
59
+ .thenReturn (healthResult );
60
+
61
+ server .check (request , response );
62
+
63
+ List <HealthCheckResponse > result = response .getValues ();
64
+
65
+ assertEquals (1 , result .size ());
66
+
67
+ assertEquals (HealthCheckResponse .ServingStatus .NOT_SERVING , result .get (0 ).getStatus ());
68
+ }
69
+
70
+ @ Test
71
+ void testSpecificServiceNotFound () {
72
+ StreamRecorder <HealthCheckResponse > response = StreamRecorder .create ();
73
+ HealthCheckRequest request = HealthCheckRequest .newBuilder ().setService ("someunknownservice" ).build ();
74
+
75
+ when (healthEndpoint .healthForPath ("someunknownservice" ))
76
+ .thenReturn (null );
77
+
78
+ server .check (request , response );
79
+
80
+ assertEquals (0 , response .getValues ().size ());
81
+
82
+ var error = response .getError ();
83
+ assertNotNull (error );
84
+ assertInstanceOf (StatusException .class , error );
85
+
86
+ var statusException = (StatusException ) error ;
87
+ assertEquals (io .grpc .Status .NOT_FOUND .getCode (), statusException .getStatus ().getCode ());
88
+ }
89
+
90
+ @ Test
91
+ void testSpecificServiceUp () {
92
+ StreamRecorder <HealthCheckResponse > response = StreamRecorder .create ();
93
+ HealthCheckRequest request = HealthCheckRequest .newBuilder ().setService ("db" ).build ();
94
+
95
+ HealthComponent healthResult = Health .up ().build ();
96
+ when (healthEndpoint .healthForPath ("db" ))
97
+ .thenReturn (healthResult );
98
+
99
+ server .check (request , response );
100
+
101
+ List <HealthCheckResponse > result = response .getValues ();
102
+ assertEquals (1 , result .size ());
103
+ assertEquals (HealthCheckResponse .ServingStatus .SERVING , result .get (0 ).getStatus ());
104
+ }
105
+
106
+ }
0 commit comments