4
4
package com .azure .cosmos .kafka .connect .implementation ;
5
5
6
6
import com .azure .cosmos .CosmosAsyncClient ;
7
- import org .mockito .Mockito ;
8
- import org .testng .annotations .BeforeMethod ;
7
+ import com .azure .cosmos .CosmosBridgeInternal ;
8
+ import com .azure .cosmos .CosmosClientBuilder ;
9
+ import com .azure .cosmos .implementation .RxDocumentClientImpl ;
10
+ import com .azure .cosmos .kafka .connect .KafkaCosmosTestConfigurations ;
11
+ import com .azure .cosmos .kafka .connect .KafkaCosmosTestSuiteBase ;
12
+ import org .testng .annotations .AfterClass ;
13
+ import org .testng .annotations .BeforeClass ;
9
14
import org .testng .annotations .Test ;
10
15
11
16
import java .time .Instant ;
12
17
13
18
import static org .assertj .core .api .Assertions .assertThat ;
14
- import static org .mockito .Mockito .verify ;
15
19
16
20
/**
17
21
* Test for CosmosClientCacheMetadata
18
22
*/
19
- public class CosmosClientCacheMetadataTest {
20
- private CosmosAsyncClient mockClient ;
21
- private Instant createdTime ;
22
- private CosmosClientCacheMetadata metadata ;
23
+ public class CosmosClientCacheMetadataTest extends KafkaCosmosTestSuiteBase {
24
+ private CosmosAsyncClient cosmosClient ;
23
25
24
- @ BeforeMethod
26
+ @ BeforeClass ( groups = "kafka-emulator" )
25
27
void setUp () {
26
- mockClient = Mockito .mock (CosmosAsyncClient .class );
27
- createdTime = Instant .now ();
28
- metadata = new CosmosClientCacheMetadata (mockClient , createdTime );
28
+ cosmosClient = new CosmosClientBuilder ()
29
+ .endpoint (KafkaCosmosTestConfigurations .HOST )
30
+ .key (KafkaCosmosTestConfigurations .MASTER_KEY )
31
+ .buildAsyncClient ();
29
32
}
30
33
31
- @ Test
34
+ @ AfterClass (groups = "kafka-emulator" )
35
+ void cleanup () {
36
+ if (this .cosmosClient != null ) {
37
+ this .cosmosClient .close ();
38
+ }
39
+ }
40
+
41
+ @ Test (groups = "kafka-emulator" )
32
42
void shouldInitializeWithRefCountOne () {
43
+ Instant createdTime = Instant .now ();
44
+ CosmosClientCacheMetadata metadata = new CosmosClientCacheMetadata (cosmosClient , createdTime );
45
+
33
46
assertThat (metadata .getRefCount ()).isEqualTo (1 );
34
47
assertThat (metadata .getLastAccessed ()).isEqualTo (createdTime );
35
48
}
36
49
37
- @ Test
50
+ @ Test ( groups = "kafka-emulator" )
38
51
void shouldIncrementRefCount () {
52
+ CosmosClientCacheMetadata metadata = new CosmosClientCacheMetadata (cosmosClient , Instant .now ());
53
+
39
54
long initialCount = metadata .getRefCount ();
40
55
metadata .incrementRefCount ();
41
56
@@ -44,8 +59,10 @@ void shouldIncrementRefCount() {
44
59
.isEqualTo (2 );
45
60
}
46
61
47
- @ Test
62
+ @ Test ( groups = "kafka-emulator" )
48
63
void shouldDecrementRefCount () {
64
+ CosmosClientCacheMetadata metadata = new CosmosClientCacheMetadata (cosmosClient , Instant .now ());
65
+
49
66
metadata .incrementRefCount (); // ref count = 2
50
67
long countBeforeDecrement = metadata .getRefCount ();
51
68
metadata .decrementRefCount ();
@@ -55,8 +72,10 @@ void shouldDecrementRefCount() {
55
72
.isEqualTo (1 );
56
73
}
57
74
58
- @ Test
75
+ @ Test ( groups = "kafka-emulator" )
59
76
void shouldUpdateLastAccessedTime () {
77
+ CosmosClientCacheMetadata metadata = new CosmosClientCacheMetadata (cosmosClient , Instant .now ());
78
+
60
79
Instant originalLastAccessed = metadata .getLastAccessed ();
61
80
// Sleep briefly to ensure time difference
62
81
try {
@@ -72,14 +91,23 @@ void shouldUpdateLastAccessedTime() {
72
91
.isNotEqualTo (originalLastAccessed );
73
92
}
74
93
75
- @ Test
94
+ @ Test ( groups = "kafka-emulator" )
76
95
void shouldReturnClient () {
77
- assertThat (metadata .getClient ()).isSameAs (mockClient );
96
+ CosmosClientCacheMetadata metadata = new CosmosClientCacheMetadata (cosmosClient , Instant .now ());
97
+
98
+ assertThat (metadata .getClient ()).isSameAs (cosmosClient );
78
99
}
79
100
80
- @ Test
101
+ @ Test ( groups = "kafka-emulator" )
81
102
void shouldCloseClientOnClose () {
103
+ CosmosAsyncClient cosmosAsyncClient = new CosmosClientBuilder ()
104
+ .endpoint (KafkaCosmosTestConfigurations .HOST )
105
+ .key (KafkaCosmosTestConfigurations .MASTER_KEY )
106
+ .buildAsyncClient ();
107
+ CosmosClientCacheMetadata metadata = new CosmosClientCacheMetadata (cosmosAsyncClient , Instant .now ());
82
108
metadata .close ();
83
- verify (mockClient ).close ();
109
+
110
+ assertThat (((RxDocumentClientImpl )CosmosBridgeInternal .getAsyncDocumentClient (cosmosAsyncClient )).isClosed ())
111
+ .isTrue ();
84
112
}
85
113
}
0 commit comments