Skip to content

Commit 36f5389

Browse files
committed
Fix
1 parent 11240e8 commit 36f5389

File tree

2 files changed

+131
-9
lines changed

2 files changed

+131
-9
lines changed

core/src/test/java/com/scalar/db/transaction/consensuscommit/ConsensusCommitTest.java

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,10 @@ private Delete prepareDelete() {
101101
}
102102

103103
@Test
104-
public void get_GetGiven_ShouldCallCrudHandlerGet() throws CrudException {
104+
public void get_GetGiven_ShouldCallCrudHandlerGet() throws CrudException, ExecutionException {
105105
// Arrange
106106
Get get = prepareGet();
107+
doNothing().when(operationChecker).check(get, context);
107108
TransactionResult result = mock(TransactionResult.class);
108109
when(crud.get(get, context)).thenReturn(Optional.of(result));
109110

@@ -112,13 +113,32 @@ public void get_GetGiven_ShouldCallCrudHandlerGet() throws CrudException {
112113

113114
// Assert
114115
assertThat(actual).isPresent();
116+
verify(operationChecker).check(get, context);
115117
verify(crud).get(get, context);
116118
}
117119

118120
@Test
119-
public void scan_ScanGiven_ShouldCallCrudHandlerScan() throws CrudException {
121+
public void get_OperationCheckerThrowsExecutionException_ShouldThrowCrudException()
122+
throws ExecutionException, CrudException {
123+
// Arrange
124+
Get get = prepareGet();
125+
ExecutionException exception = new ExecutionException("operation check failed");
126+
doThrow(exception).when(operationChecker).check(get, context);
127+
128+
// Act Assert
129+
assertThatThrownBy(() -> consensus.get(get))
130+
.isInstanceOf(CrudException.class)
131+
.hasMessage("operation check failed. Transaction ID: " + ANY_ID)
132+
.hasCause(exception);
133+
verify(operationChecker).check(get, context);
134+
verify(crud, never()).get(any(), any());
135+
}
136+
137+
@Test
138+
public void scan_ScanGiven_ShouldCallCrudHandlerScan() throws CrudException, ExecutionException {
120139
// Arrange
121140
Scan scan = prepareScan();
141+
doNothing().when(operationChecker).check(scan, context);
122142
TransactionResult result = mock(TransactionResult.class);
123143
List<Result> results = Collections.singletonList(result);
124144
when(crud.scan(scan, context)).thenReturn(results);
@@ -128,14 +148,33 @@ public void scan_ScanGiven_ShouldCallCrudHandlerScan() throws CrudException {
128148

129149
// Assert
130150
assertThat(actual.size()).isEqualTo(1);
151+
verify(operationChecker).check(scan, context);
131152
verify(crud).scan(scan, context);
132153
}
133154

155+
@Test
156+
public void scan_OperationCheckerThrowsExecutionException_ShouldThrowCrudException()
157+
throws ExecutionException, CrudException {
158+
// Arrange
159+
Scan scan = prepareScan();
160+
ExecutionException exception = new ExecutionException("operation check failed");
161+
doThrow(exception).when(operationChecker).check(scan, context);
162+
163+
// Act Assert
164+
assertThatThrownBy(() -> consensus.scan(scan))
165+
.isInstanceOf(CrudException.class)
166+
.hasMessage("operation check failed. Transaction ID: " + ANY_ID)
167+
.hasCause(exception);
168+
verify(operationChecker).check(scan, context);
169+
verify(crud, never()).scan(any(), any());
170+
}
171+
134172
@Test
135173
public void getScannerAndScannerOne_ShouldCallCrudHandlerGetScannerAndScannerOne()
136-
throws CrudException {
174+
throws CrudException, ExecutionException {
137175
// Arrange
138176
Scan scan = prepareScan();
177+
doNothing().when(operationChecker).check(scan, context);
139178
TransactionCrudOperable.Scanner scanner = mock(TransactionCrudOperable.Scanner.class);
140179
Result result = mock(Result.class);
141180
when(scanner.one()).thenReturn(Optional.of(result));
@@ -147,15 +186,17 @@ public void getScannerAndScannerOne_ShouldCallCrudHandlerGetScannerAndScannerOne
147186

148187
// Assert
149188
assertThat(actualResult).hasValue(result);
189+
verify(operationChecker).check(scan, context);
150190
verify(crud).getScanner(scan, context);
151191
verify(scanner).one();
152192
}
153193

154194
@Test
155195
public void getScannerAndScannerAll_ShouldCallCrudHandlerGetScannerAndScannerAll()
156-
throws CrudException {
196+
throws CrudException, ExecutionException {
157197
// Arrange
158198
Scan scan = prepareScan();
199+
doNothing().when(operationChecker).check(scan, context);
159200
TransactionCrudOperable.Scanner scanner = mock(TransactionCrudOperable.Scanner.class);
160201
Result result1 = mock(Result.class);
161202
Result result2 = mock(Result.class);
@@ -168,10 +209,28 @@ public void getScannerAndScannerAll_ShouldCallCrudHandlerGetScannerAndScannerAll
168209

169210
// Assert
170211
assertThat(actualResults).containsExactly(result1, result2);
212+
verify(operationChecker).check(scan, context);
171213
verify(crud).getScanner(scan, context);
172214
verify(scanner).all();
173215
}
174216

217+
@Test
218+
public void getScanner_OperationCheckerThrowsExecutionException_ShouldThrowCrudException()
219+
throws ExecutionException, CrudException {
220+
// Arrange
221+
Scan scan = prepareScan();
222+
ExecutionException exception = new ExecutionException("operation check failed");
223+
doThrow(exception).when(operationChecker).check(scan, context);
224+
225+
// Act Assert
226+
assertThatThrownBy(() -> consensus.getScanner(scan))
227+
.isInstanceOf(CrudException.class)
228+
.hasMessage("operation check failed. Transaction ID: " + ANY_ID)
229+
.hasCause(exception);
230+
verify(operationChecker).check(scan, context);
231+
verify(crud, never()).getScanner(any(), any());
232+
}
233+
175234
@Test
176235
public void put_PutGiven_ShouldCallCrudHandlerPut() throws ExecutionException, CrudException {
177236
// Arrange

core/src/test/java/com/scalar/db/transaction/consensuscommit/TwoPhaseConsensusCommitTest.java

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import static org.assertj.core.api.Assertions.assertThat;
44
import static org.assertj.core.api.Assertions.assertThatCode;
55
import static org.assertj.core.api.Assertions.assertThatThrownBy;
6+
import static org.mockito.ArgumentMatchers.any;
7+
import static org.mockito.Mockito.doNothing;
68
import static org.mockito.Mockito.doThrow;
79
import static org.mockito.Mockito.mock;
810
import static org.mockito.Mockito.never;
@@ -105,9 +107,10 @@ private Delete prepareDelete() {
105107
}
106108

107109
@Test
108-
public void get_GetGiven_ShouldCallCrudHandlerGet() throws CrudException {
110+
public void get_GetGiven_ShouldCallCrudHandlerGet() throws CrudException, ExecutionException {
109111
// Arrange
110112
Get get = prepareGet();
113+
doNothing().when(operationChecker).check(get, context);
111114
TransactionResult result = mock(TransactionResult.class);
112115
when(crud.get(get, context)).thenReturn(Optional.of(result));
113116

@@ -116,13 +119,32 @@ public void get_GetGiven_ShouldCallCrudHandlerGet() throws CrudException {
116119

117120
// Assert
118121
assertThat(actual).isPresent();
122+
verify(operationChecker).check(get, context);
119123
verify(crud).get(get, context);
120124
}
121125

122126
@Test
123-
public void scan_ScanGiven_ShouldCallCrudHandlerScan() throws CrudException {
127+
public void get_OperationCheckerThrowsExecutionException_ShouldThrowCrudException()
128+
throws ExecutionException, CrudException {
129+
// Arrange
130+
Get get = prepareGet();
131+
ExecutionException exception = new ExecutionException("operation check failed");
132+
doThrow(exception).when(operationChecker).check(get, context);
133+
134+
// Act Assert
135+
assertThatThrownBy(() -> transaction.get(get))
136+
.isInstanceOf(CrudException.class)
137+
.hasMessage("operation check failed. Transaction ID: " + ANY_TX_ID)
138+
.hasCause(exception);
139+
verify(operationChecker).check(get, context);
140+
verify(crud, never()).get(any(), any());
141+
}
142+
143+
@Test
144+
public void scan_ScanGiven_ShouldCallCrudHandlerScan() throws CrudException, ExecutionException {
124145
// Arrange
125146
Scan scan = prepareScan();
147+
doNothing().when(operationChecker).check(scan, context);
126148
TransactionResult result = mock(TransactionResult.class);
127149
List<Result> results = Collections.singletonList(result);
128150
when(crud.scan(scan, context)).thenReturn(results);
@@ -132,14 +154,33 @@ public void scan_ScanGiven_ShouldCallCrudHandlerScan() throws CrudException {
132154

133155
// Assert
134156
assertThat(actual.size()).isEqualTo(1);
157+
verify(operationChecker).check(scan, context);
135158
verify(crud).scan(scan, context);
136159
}
137160

161+
@Test
162+
public void scan_OperationCheckerThrowsExecutionException_ShouldThrowCrudException()
163+
throws ExecutionException, CrudException {
164+
// Arrange
165+
Scan scan = prepareScan();
166+
ExecutionException exception = new ExecutionException("operation check failed");
167+
doThrow(exception).when(operationChecker).check(scan, context);
168+
169+
// Act Assert
170+
assertThatThrownBy(() -> transaction.scan(scan))
171+
.isInstanceOf(CrudException.class)
172+
.hasMessage("operation check failed. Transaction ID: " + ANY_TX_ID)
173+
.hasCause(exception);
174+
verify(operationChecker).check(scan, context);
175+
verify(crud, never()).scan(any(), any());
176+
}
177+
138178
@Test
139179
public void getScannerAndScannerOne_ShouldCallCrudHandlerGetScannerAndScannerOne()
140-
throws CrudException {
180+
throws CrudException, ExecutionException {
141181
// Arrange
142182
Scan scan = prepareScan();
183+
doNothing().when(operationChecker).check(scan, context);
143184
TransactionCrudOperable.Scanner scanner = mock(TransactionCrudOperable.Scanner.class);
144185
Result result = mock(Result.class);
145186
when(scanner.one()).thenReturn(Optional.of(result));
@@ -151,15 +192,17 @@ public void getScannerAndScannerOne_ShouldCallCrudHandlerGetScannerAndScannerOne
151192

152193
// Assert
153194
assertThat(actualResult).hasValue(result);
195+
verify(operationChecker).check(scan, context);
154196
verify(crud).getScanner(scan, context);
155197
verify(scanner).one();
156198
}
157199

158200
@Test
159201
public void getScannerAndScannerAll_ShouldCallCrudHandlerGetScannerAndScannerAll()
160-
throws CrudException {
202+
throws CrudException, ExecutionException {
161203
// Arrange
162204
Scan scan = prepareScan();
205+
doNothing().when(operationChecker).check(scan, context);
163206
TransactionCrudOperable.Scanner scanner = mock(TransactionCrudOperable.Scanner.class);
164207
Result result1 = mock(Result.class);
165208
Result result2 = mock(Result.class);
@@ -172,10 +215,28 @@ public void getScannerAndScannerAll_ShouldCallCrudHandlerGetScannerAndScannerAll
172215

173216
// Assert
174217
assertThat(actualResults).containsExactly(result1, result2);
218+
verify(operationChecker).check(scan, context);
175219
verify(crud).getScanner(scan, context);
176220
verify(scanner).all();
177221
}
178222

223+
@Test
224+
public void getScanner_OperationCheckerThrowsExecutionException_ShouldThrowCrudException()
225+
throws ExecutionException, CrudException {
226+
// Arrange
227+
Scan scan = prepareScan();
228+
ExecutionException exception = new ExecutionException("operation check failed");
229+
doThrow(exception).when(operationChecker).check(scan, context);
230+
231+
// Act Assert
232+
assertThatThrownBy(() -> transaction.getScanner(scan))
233+
.isInstanceOf(CrudException.class)
234+
.hasMessage("operation check failed. Transaction ID: " + ANY_TX_ID)
235+
.hasCause(exception);
236+
verify(operationChecker).check(scan, context);
237+
verify(crud, never()).getScanner(any(), any());
238+
}
239+
179240
@Test
180241
public void put_PutGiven_ShouldCallCrudHandlerPut() throws ExecutionException, CrudException {
181242
// Arrange
@@ -589,7 +650,9 @@ public void commit_ShouldCommitStateAndRecords()
589650

590651
@Test
591652
public void commit_SerializableUsedAndValidatedState_ShouldCommitProperly()
592-
throws CommitException, UnknownTransactionStatusException, PreparationException,
653+
throws CommitException,
654+
UnknownTransactionStatusException,
655+
PreparationException,
593656
ValidationException {
594657
// Arrange
595658
transaction.prepare();

0 commit comments

Comments
 (0)