Skip to content

Commit 0b5c794

Browse files
committed
Finishing tests
1 parent 0c549c7 commit 0b5c794

File tree

5 files changed

+777
-0
lines changed

5 files changed

+777
-0
lines changed

s2/src/main/java/s2/services/AsyncStreamService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ private void initializeStubs(ManagedChannel channel, CallCredentials credentials
5353
*/
5454
public StreamObserver<AppendSessionRequest> openAppendSession(String stream,
5555
Consumer<AppendSessionResponse> onResponse, Consumer<Throwable> onError) {
56+
if (onResponse == null || onError == null) {
57+
throw new NullPointerException("Callbacks cannot be null");
58+
}
5659
return asyncStub.appendSession(new StreamObserver<AppendSessionResponse>() {
5760
@Override
5861
public void onNext(AppendSessionResponse response) {
@@ -82,6 +85,9 @@ public void onCompleted() {
8285
*/
8386
public void openReadSession(String stream, long startSeqNum, ReadLimit limit,
8487
Consumer<ReadSessionResponse> onResponse, Consumer<Throwable> onError) {
88+
if (onResponse == null || onError == null) {
89+
throw new NullPointerException("Callbacks cannot be null");
90+
}
8591
var requestBuilder =
8692
ReadSessionRequest.newBuilder().setStream(stream).setStartSeqNum(startSeqNum);
8793
if (limit != null) {
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
package s2.services;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
import static org.mockito.Mockito.*;
5+
6+
import org.junit.jupiter.api.extension.ExtendWith;
7+
import org.mockito.Mock;
8+
import org.mockito.MockedStatic;
9+
import org.mockito.junit.jupiter.MockitoExtension;
10+
import io.grpc.CallCredentials;
11+
import io.grpc.ManagedChannel;
12+
import org.junit.jupiter.api.AfterEach;
13+
import org.junit.jupiter.api.BeforeEach;
14+
import org.junit.jupiter.api.Test;
15+
import s2.v1alpha.*;
16+
17+
@ExtendWith(MockitoExtension.class)
18+
class AccountServiceTest {
19+
20+
private AccountService accountService;
21+
private static final String TEST_BASIN = "test-basin";
22+
23+
@Mock
24+
private ManagedChannel channel;
25+
26+
@Mock
27+
private CallCredentials credentials;
28+
29+
@Mock
30+
private AccountServiceGrpc.AccountServiceBlockingStub stub;
31+
32+
private MockedStatic<AccountServiceGrpc> accountServiceGrpcMock;
33+
34+
@BeforeEach
35+
void setUp() {
36+
accountServiceGrpcMock = mockStatic(AccountServiceGrpc.class);
37+
accountServiceGrpcMock.when(() -> AccountServiceGrpc.newBlockingStub(any(ManagedChannel.class)))
38+
.thenReturn(stub);
39+
when(stub.withCallCredentials(any(CallCredentials.class))).thenReturn(stub);
40+
41+
accountService = new AccountService(channel, credentials);
42+
}
43+
44+
@AfterEach
45+
void tearDown() {
46+
if (accountServiceGrpcMock != null) {
47+
accountServiceGrpcMock.close();
48+
}
49+
}
50+
51+
@Test
52+
void listBasins_shouldReturnAllBasinsForEmptyPrefix() {
53+
var basin1 = BasinInfo.newBuilder().setName("basin1").build();
54+
var basin2 = BasinInfo.newBuilder().setName("basin2").build();
55+
56+
var response1 = ListBasinsResponse.newBuilder()
57+
.addBasins(basin1)
58+
.addBasins(basin2)
59+
.setHasMore(false)
60+
.build();
61+
62+
when(stub.listBasins(any(ListBasinsRequest.class))).thenReturn(response1);
63+
64+
var result = accountService.listBasins("");
65+
66+
assertEquals(2, result.size());
67+
assertEquals("basin1", result.get(0).getName());
68+
assertEquals("basin2", result.get(1).getName());
69+
70+
verify(stub).listBasins(argThat(request ->
71+
request.getPrefix().isEmpty()
72+
));
73+
}
74+
75+
@Test
76+
void listBasins_shouldHandlePagination() {
77+
var basin1 = BasinInfo.newBuilder().setName("basin1").build();
78+
var basin2 = BasinInfo.newBuilder().setName("basin2").build();
79+
var basin3 = BasinInfo.newBuilder().setName("basin3").build();
80+
81+
var response1 = ListBasinsResponse.newBuilder()
82+
.addBasins(basin1)
83+
.setHasMore(true)
84+
.build();
85+
86+
var response2 = ListBasinsResponse.newBuilder()
87+
.addBasins(basin2)
88+
.addBasins(basin3)
89+
.setHasMore(false)
90+
.build();
91+
92+
when(stub.listBasins(any(ListBasinsRequest.class)))
93+
.thenReturn(response1)
94+
.thenReturn(response2);
95+
96+
var result = accountService.listBasins("test");
97+
98+
assertEquals(3, result.size());
99+
assertEquals("basin1", result.get(0).getName());
100+
assertEquals("basin2", result.get(1).getName());
101+
assertEquals("basin3", result.get(2).getName());
102+
103+
verify(stub, times(2)).listBasins(any(ListBasinsRequest.class));
104+
}
105+
106+
@Test
107+
void createBasin_shouldCreateBasinWithConfig() {
108+
var expectedBasin = BasinInfo.newBuilder()
109+
.setName(TEST_BASIN)
110+
.build();
111+
112+
var response = CreateBasinResponse.newBuilder()
113+
.setInfo(expectedBasin)
114+
.build();
115+
116+
var streamConfig = StreamConfig.newBuilder()
117+
.setStorageClass(StorageClass.STORAGE_CLASS_STANDARD)
118+
.build();
119+
120+
var config = BasinConfig.newBuilder()
121+
.setDefaultStreamConfig(streamConfig)
122+
.build();
123+
124+
when(stub.createBasin(any(CreateBasinRequest.class))).thenReturn(response);
125+
126+
var result = accountService.createBasin(TEST_BASIN, config);
127+
128+
assertEquals(TEST_BASIN, result.getName());
129+
verify(stub).createBasin(argThat(request ->
130+
request.getBasin().equals(TEST_BASIN) &&
131+
request.getConfig().equals(config)
132+
));
133+
}
134+
135+
@Test
136+
void deleteBasin_shouldDeleteExistingBasin() {
137+
accountService.deleteBasin(TEST_BASIN);
138+
139+
verify(stub).deleteBasin(argThat(request ->
140+
request.getBasin().equals(TEST_BASIN)
141+
));
142+
}
143+
144+
@Test
145+
void reconfigureBasin_shouldUpdateBasinConfig() {
146+
var streamConfig = StreamConfig.newBuilder()
147+
.setStorageClass(StorageClass.STORAGE_CLASS_EXPRESS)
148+
.build();
149+
150+
var newConfig = BasinConfig.newBuilder()
151+
.setDefaultStreamConfig(streamConfig)
152+
.build();
153+
154+
accountService.reconfigureBasin(TEST_BASIN, newConfig);
155+
156+
verify(stub).reconfigureBasin(argThat(request ->
157+
request.getBasin().equals(TEST_BASIN) &&
158+
request.getConfig().equals(newConfig)
159+
));
160+
}
161+
162+
@Test
163+
void createBasin_shouldThrowWhenConfigIsNull() {
164+
assertThrows(NullPointerException.class, () ->
165+
accountService.createBasin(TEST_BASIN, null)
166+
);
167+
}
168+
169+
@Test
170+
void reconfigureBasin_shouldThrowWhenConfigIsNull() {
171+
assertThrows(NullPointerException.class, () ->
172+
accountService.reconfigureBasin(TEST_BASIN, null)
173+
);
174+
}
175+
}

0 commit comments

Comments
 (0)