53
53
import org .mockito .MockedStatic ;
54
54
import org .mockito .Mockito ;
55
55
import org .mockito .junit .jupiter .MockitoExtension ;
56
- import org .slf4j .Logger ;
57
56
58
57
import java .io .File ;
59
58
import java .lang .reflect .Field ;
60
59
import java .lang .reflect .Method ;
61
- import java .lang .reflect .Modifier ;
62
60
import java .util .ArrayList ;
63
61
import java .util .Arrays ;
64
62
import java .util .Collections ;
70
68
import java .util .concurrent .atomic .AtomicReference ;
71
69
72
70
import static com .alibaba .nacos .api .annotation .NacosProperties .NAMESPACE ;
71
+ import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
73
72
import static org .junit .jupiter .api .Assertions .assertEquals ;
74
73
import static org .junit .jupiter .api .Assertions .assertFalse ;
75
74
import static org .junit .jupiter .api .Assertions .assertNotNull ;
@@ -103,17 +102,14 @@ class ClientWorkerTest {
103
102
104
103
private ClientWorker clientWorkerSpy ;
105
104
106
- @ Mock
107
- private Logger logger ;
108
-
109
105
@ BeforeEach
110
106
void before () throws Exception {
111
107
rpcClientFactoryMockedStatic = Mockito .mockStatic (RpcClientFactory .class );
112
108
113
- rpcClientFactoryMockedStatic .when (
114
- () -> RpcClientFactory . createClient ( anyString (), any ( ConnectionType . class ), any (GrpcClientConfig .class ))).thenReturn (rpcClient );
115
- rpcClientFactoryMockedStatic .when (
116
- () -> RpcClientFactory . createClient ( anyString (), any ( ConnectionType . class ), any (GrpcClientConfig .class ))).thenReturn (rpcClient );
109
+ rpcClientFactoryMockedStatic .when (() -> RpcClientFactory . createClient ( anyString (), any ( ConnectionType . class ),
110
+ any (GrpcClientConfig .class ))).thenReturn (rpcClient );
111
+ rpcClientFactoryMockedStatic .when (() -> RpcClientFactory . createClient ( anyString (), any ( ConnectionType . class ),
112
+ any (GrpcClientConfig .class ))).thenReturn (rpcClient );
117
113
localConfigInfoProcessorMockedStatic = Mockito .mockStatic (LocalConfigInfoProcessor .class );
118
114
Properties properties = new Properties ();
119
115
properties .put (PropertyKeyConst .NAMESPACE , TEST_NAMESPACE );
@@ -126,14 +122,6 @@ void before() throws Exception {
126
122
throw new RuntimeException (e );
127
123
}
128
124
clientWorkerSpy = Mockito .spy (clientWorker );
129
-
130
- Field loggerField = ClientWorker .class .getDeclaredField ("LOGGER" );
131
- loggerField .setAccessible (true );
132
-
133
- Field modifiersField = Field .class .getDeclaredField ("modifiers" );
134
- modifiersField .setAccessible (true );
135
- modifiersField .setInt (loggerField , loggerField .getModifiers () & ~Modifier .FINAL );
136
- loggerField .set (null , logger );
137
125
}
138
126
139
127
@ AfterEach
@@ -250,8 +238,7 @@ void testPublishConfigSuccess() throws NacosException {
250
238
String casMd5 = "1111" ;
251
239
252
240
String type = "properties" ;
253
- Mockito .when (rpcClient .request (any (ConfigPublishRequest .class )))
254
- .thenReturn (new ConfigPublishResponse ());
241
+ Mockito .when (rpcClient .request (any (ConfigPublishRequest .class ))).thenReturn (new ConfigPublishResponse ());
255
242
boolean b = clientWorker .publishConfig (dataId , group , tenant , appName , tag , betaIps , content , null , casMd5 ,
256
243
type );
257
244
assertTrue (b );
@@ -575,11 +562,10 @@ public void receiveConfigInfo(String configInfo) {
575
562
576
563
RpcClient rpcClientInner = Mockito .mock (RpcClient .class );
577
564
Mockito .when (rpcClientInner .isWaitInitiated ()).thenReturn (true , false );
578
- rpcClientFactoryMockedStatic .when (
579
- () -> RpcClientFactory . createClient ( anyString (), any ( ConnectionType . class ), any (GrpcClientConfig .class ))).thenReturn (rpcClientInner );
565
+ rpcClientFactoryMockedStatic .when (() -> RpcClientFactory . createClient ( anyString (), any ( ConnectionType . class ),
566
+ any (GrpcClientConfig .class ))).thenReturn (rpcClientInner );
580
567
// mock listen and remove listen request
581
- Mockito .when (rpcClientInner .request (any (ConfigBatchListenRequest .class )))
582
- .thenReturn (response , response );
568
+ Mockito .when (rpcClientInner .request (any (ConfigBatchListenRequest .class ))).thenReturn (response , response );
583
569
// mock query changed config
584
570
ConfigQueryResponse configQueryResponse = new ConfigQueryResponse ();
585
571
configQueryResponse .setContent ("content" + System .currentTimeMillis ());
@@ -789,8 +775,7 @@ void testResponse403() throws NacosException {
789
775
790
776
ConfigRemoveResponse response = ConfigRemoveResponse .buildFailResponse ("accessToken invalid" );
791
777
response .setErrorCode (ConfigQueryResponse .NO_RIGHT );
792
- Mockito .when (rpcClient .request (any (ConfigRemoveRequest .class )))
793
- .thenReturn (response );
778
+ Mockito .when (rpcClient .request (any (ConfigRemoveRequest .class ))).thenReturn (response );
794
779
boolean result = clientWorker .removeConfig ("a" , "b" , "c" , "tag" );
795
780
assertFalse (result );
796
781
}
@@ -889,12 +874,7 @@ void testMetricsMonitorSetThrowsException() throws NacosException {
889
874
RuntimeException exception = new RuntimeException ("Mocked exception" );
890
875
doThrow (exception ).when (mockGaugeChild ).set (0 );
891
876
892
- clientWorker .removeCache (dataId , group , tenant );
893
-
894
- String groupKey = GroupKey .getKeyTenant (dataId , group , tenant );
895
-
896
- verify (logger , times (1 )).info ("[{}] [unsubscribe] {}" , null , groupKey );
897
- verify (logger , times (1 )).error ("Failed to update metrics for listen config count" , exception );
877
+ assertDoesNotThrow (() -> clientWorker .removeCache (dataId , group , tenant ));
898
878
}
899
879
}
900
880
@@ -915,16 +895,16 @@ public void testAddCacheDataIfAbsentEnableClientMetricsTrue() throws NacosExcept
915
895
Gauge .Child mockGaugeChild = mock (Gauge .Child .class );
916
896
try (MockedStatic <MetricsMonitor > mockedMetricsMonitor = Mockito .mockStatic (MetricsMonitor .class )) {
917
897
mockedMetricsMonitor .when (MetricsMonitor ::getListenConfigCountMonitor ).thenReturn (mockGaugeChild );
918
-
898
+
919
899
clientWorker .addCacheDataIfAbsent (dataId , group , tenant );
920
-
900
+
921
901
verify (mockGaugeChild , times (1 )).set (1 );
922
902
}
923
903
}
924
904
925
905
@ Test
926
906
public void testAddCacheDataIfAbsentEnableClientMetricsFalse () throws NacosException {
927
-
907
+
928
908
String dataId = "testDataId" ;
929
909
String group = "testGroup" ;
930
910
String tenant = "testTenant" ;
0 commit comments