12
12
import com .fasterxml .jackson .databind .ObjectMapper ;
13
13
import com .linkedin .venice .client .store .AbstractAvroStoreClient ;
14
14
import com .linkedin .venice .client .store .ClientConfig ;
15
+ import com .linkedin .venice .client .store .ClientFactory ;
16
+ import com .linkedin .venice .client .store .transport .TransportClient ;
15
17
import com .linkedin .venice .client .store .transport .TransportClientResponse ;
16
18
import com .linkedin .venice .utils .ObjectMapperFactory ;
17
19
import java .io .IOException ;
18
20
import java .nio .charset .StandardCharsets ;
19
21
import java .util .List ;
20
22
import java .util .concurrent .CompletableFuture ;
23
+ import java .util .function .Function ;
21
24
import org .apache .logging .log4j .LogManager ;
22
25
import org .apache .logging .log4j .Logger ;
23
26
import org .mockito .ArgumentCaptor ;
@@ -42,11 +45,13 @@ public void setUp() {
42
45
storeClient = mock (AbstractAvroStoreClient .class );
43
46
daVinciBlobFinder = spy (new DaVinciBlobFinder (clientConfig ));
44
47
45
- Mockito .doReturn (storeClient ).when (daVinciBlobFinder ). getStoreClient ( storeName );
48
+ Mockito .doReturn (storeName ).when (storeClient ). getStoreName ( );
46
49
}
47
50
48
51
@ Test
49
52
public void testDiscoverBlobPeers_Success () {
53
+ Mockito .doReturn (storeClient ).when (daVinciBlobFinder ).getStoreClient (storeName );
54
+
50
55
String responseBodyJson =
51
56
"{\" error\" :false,\" errorMessage\" :\" \" ,\" discoveryResult\" :[\" host1\" ,\" host2\" ,\" host3\" ]}" ;
52
57
byte [] responseBody = responseBodyJson .getBytes (StandardCharsets .UTF_8 );
@@ -63,6 +68,7 @@ public void testDiscoverBlobPeers_Success() {
63
68
64
69
@ Test
65
70
public void testDiscoverBlobPeers_CallsTransportClientWithCorrectURI () {
71
+ Mockito .doReturn (storeClient ).when (daVinciBlobFinder ).getStoreClient (storeName );
66
72
String responseBodyJson =
67
73
"{\" error\" :false,\" errorMessage\" :\" \" ,\" discoveryResult\" :[\" host1\" ,\" host2\" ,\" host3\" ]}" ;
68
74
byte [] responseBody = responseBodyJson .getBytes (StandardCharsets .UTF_8 );
@@ -84,6 +90,8 @@ public void testDiscoverBlobPeers_CallsTransportClientWithCorrectURI() {
84
90
85
91
@ Test
86
92
public void testDiscoverBlobPeers_ContentDeserializationError () throws Exception {
93
+ Mockito .doReturn (storeClient ).when (daVinciBlobFinder ).getStoreClient (storeName );
94
+
87
95
String responseBodyJson = "{\" error\" :true,\" errorMessage\" :\" some error\" ,\" discoveryResult\" :[]}" ;
88
96
byte [] responseBody = responseBodyJson .getBytes (StandardCharsets .UTF_8 );
89
97
TransportClientResponse mockResponse = new TransportClientResponse (0 , null , responseBody );
@@ -104,6 +112,8 @@ public void testDiscoverBlobPeers_ContentDeserializationError() throws Exception
104
112
105
113
@ Test
106
114
public void testDiscoverBlobPeers_ClientWithIncorrectUri () {
115
+ Mockito .doReturn (storeClient ).when (daVinciBlobFinder ).getStoreClient (storeName );
116
+
107
117
CompletableFuture <byte []> futureResponse = new CompletableFuture <>();
108
118
futureResponse .completeExceptionally (new RuntimeException ("Test Exception" ));
109
119
when (storeClient .getRaw (anyString ())).thenReturn (futureResponse );
@@ -115,4 +125,34 @@ public void testDiscoverBlobPeers_ClientWithIncorrectUri() {
115
125
response .getErrorMessage (),
116
126
"Error finding DVC peers for blob transfer in store: testStore, version: 1, partition: 1" );
117
127
}
128
+
129
+ @ Test
130
+ public void testGetStoreClient () {
131
+ // set up the transport client provider used to initialize the store client
132
+ TransportClient transportClient1 = mock (TransportClient .class );
133
+ TransportClient transportClient2 = mock (TransportClient .class );
134
+ Function <ClientConfig , TransportClient > clientConfigTransportClientFunction = (clientConfig ) -> {
135
+ if (clientConfig .getStoreName ().equals (storeName )) {
136
+ return transportClient1 ;
137
+ } else if (clientConfig .getStoreName ().equals ("storeName2" )) {
138
+ return transportClient2 ;
139
+ } else {
140
+ // Create TransportClient the regular way
141
+ return null ;
142
+ }
143
+ };
144
+ ClientFactory .setUnitTestMode ();
145
+ ClientFactory .setTransportClientProvider (clientConfigTransportClientFunction );
146
+
147
+ // ClientConfig is initialized with storeName
148
+ AbstractAvroStoreClient storeClient = daVinciBlobFinder .getStoreClient (storeName );
149
+ Assert .assertNotNull (storeClient );
150
+ Assert .assertEquals (storeClient .getStoreName (), storeName );
151
+
152
+ // Even if the daVinciBlobFinder is initialized at the beginning with "storeName", the getStoreClient
153
+ // method should be able to return a store client for "storeName2"
154
+ AbstractAvroStoreClient storeClient2 = daVinciBlobFinder .getStoreClient ("storeName2" );
155
+ Assert .assertNotNull (storeClient2 );
156
+ Assert .assertEquals (storeClient2 .getStoreName (), "storeName2" );
157
+ }
118
158
}
0 commit comments