Skip to content

Commit 6a20d42

Browse files
feeblefakiejnmt
andauthored
Backport to branch(3.11) : Fix null condition handling (#250)
Co-authored-by: Jun Nemoto <[email protected]>
1 parent 4398244 commit 6a20d42

File tree

2 files changed

+55
-5
lines changed
  • generic-contracts/src
    • main/java/com/scalar/dl/genericcontracts/table/v1_0_0
    • test/java/com/scalar/dl/genericcontracts/table/v1_0_0

2 files changed

+55
-5
lines changed

generic-contracts/src/main/java/com/scalar/dl/genericcontracts/table/v1_0_0/Scan.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,29 +179,33 @@ private boolean hasIndexKeyCondition(
179179
}
180180

181181
private boolean isPrimaryKeyCondition(JsonNode condition, String keyType) {
182+
String operator = condition.get(Constants.CONDITION_OPERATOR).textValue().toUpperCase();
183+
if (!operator.equals(Constants.OPERATOR_EQ)) {
184+
return false;
185+
}
186+
182187
String givenType = condition.get(Constants.CONDITION_VALUE).getNodeType().name();
183188
if (!givenType.equalsIgnoreCase(keyType)) {
184189
throw new ContractContextException(Constants.INVALID_KEY_TYPE + givenType);
185190
}
186191

187-
return condition
188-
.get(Constants.CONDITION_OPERATOR)
189-
.textValue()
190-
.equalsIgnoreCase(Constants.OPERATOR_EQ);
192+
return true;
191193
}
192194

193195
private boolean isIndexKeyCondition(JsonNode condition, String keyType) {
194196
String operator = condition.get(Constants.CONDITION_OPERATOR).textValue().toUpperCase();
195197
if (operator.equals(Constants.OPERATOR_IS_NULL)) {
196198
return true;
199+
} else if (!operator.equals(Constants.OPERATOR_EQ)) {
200+
return false;
197201
}
198202

199203
String givenType = condition.get(Constants.CONDITION_VALUE).getNodeType().name();
200204
if (!givenType.equalsIgnoreCase(keyType)) {
201205
throw new ContractContextException(Constants.INVALID_INDEX_KEY_TYPE + givenType);
202206
}
203207

204-
return operator.equals(Constants.OPERATOR_EQ);
208+
return true;
205209
}
206210

207211
private JsonNode getKeyColumnCondition(

generic-contracts/src/test/java/com/scalar/dl/genericcontracts/table/v1_0_0/ScanTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,52 @@ public void invoke_ArgumentsWithInvalidIndexKeyConditionGiven_ShouldThrowExcepti
920920
verify(ledger).get(SOME_TABLE_ASSET_ID);
921921
}
922922

923+
@Test
924+
public void invoke_ArgumentsWithOnlyIsNullConditionForPrimaryKeyGiven_ShouldThrowException() {
925+
// Arrange
926+
ArrayNode conditions =
927+
mapper
928+
.createArrayNode()
929+
.add(createCondition(SOME_PRIMARY_KEY_COLUMN, Constants.OPERATOR_IS_NULL));
930+
JsonNode argument =
931+
mapper
932+
.createObjectNode()
933+
.put(Constants.QUERY_TABLE, SOME_TABLE_NAME)
934+
.set(Constants.QUERY_CONDITIONS, conditions);
935+
Asset<JsonNode> table = createAsset(SOME_TABLE);
936+
when(ledger.get(SOME_TABLE_ASSET_ID)).thenReturn(Optional.of(table));
937+
prepareTableAssetId(SOME_TABLE_NAME);
938+
939+
// Act Assert
940+
assertThatThrownBy(() -> scan.invoke(ledger, argument, null))
941+
.isExactlyInstanceOf(ContractContextException.class)
942+
.hasMessageContaining(Constants.INVALID_KEY_SPECIFICATION);
943+
verify(ledger).get(SOME_TABLE_ASSET_ID);
944+
}
945+
946+
@Test
947+
public void invoke_ArgumentsWithOnlyIsNotNullConditionForIndexKeyGiven_ShouldThrowException() {
948+
// Arrange
949+
ArrayNode conditions =
950+
mapper
951+
.createArrayNode()
952+
.add(createCondition(SOME_INDEX_KEY_COLUMN_1, Constants.OPERATOR_IS_NOT_NULL));
953+
JsonNode argument =
954+
mapper
955+
.createObjectNode()
956+
.put(Constants.QUERY_TABLE, SOME_TABLE_NAME)
957+
.set(Constants.QUERY_CONDITIONS, conditions);
958+
Asset<JsonNode> table = createAsset(SOME_TABLE);
959+
when(ledger.get(SOME_TABLE_ASSET_ID)).thenReturn(Optional.of(table));
960+
prepareTableAssetId(SOME_TABLE_NAME);
961+
962+
// Act Assert
963+
assertThatThrownBy(() -> scan.invoke(ledger, argument, null))
964+
.isExactlyInstanceOf(ContractContextException.class)
965+
.hasMessageContaining(Constants.INVALID_KEY_SPECIFICATION);
966+
verify(ledger).get(SOME_TABLE_ASSET_ID);
967+
}
968+
923969
@Test
924970
public void invoke_InvalidConditionsGiven_ShouldThrowException() {
925971
// Arrange

0 commit comments

Comments
 (0)