Skip to content

Commit 37aa780

Browse files
Refactorings and fix handling of unsupported operations
1 parent bfbe32b commit 37aa780

2 files changed

Lines changed: 38 additions & 33 deletions

File tree

client-java/controller/src/main/java/org/evomaster/client/java/controller/cassandra/calculator/CassandraOperationEvaluator.java

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public Truthness evaluate(CqlQueryOperation op, CassandraRow candidateRow) {
7878
} else if (op instanceof ContainsKeyOperation<?>) {
7979
return evaluateContainsKey((ContainsKeyOperation<?>) op, candidateRow);
8080
} else {
81-
return FALSE_TRUTHNESS;
81+
throw new IllegalArgumentException("Unsupported operation: " + op.getClass().getName());
8282
}
8383
}
8484

@@ -168,7 +168,7 @@ private Truthness compareByType(Object rowValue, Object literalValue, Comparison
168168
} else if (isTemporalType(rowValue)) {
169169
return compareTemporal(rowValue, literalValue, comparisonType);
170170
} else if (isCqlDuration(rowValue)) {
171-
return compareDuration(rowValue, literalValue, comparisonType);
171+
return compareDuration((TemporalAmount) rowValue, literalValue, comparisonType);
172172
} else {
173173
throw new IllegalArgumentException("Unsupported row value type: " + rowValue.getClass().getName());
174174
}
@@ -184,33 +184,33 @@ private Truthness compareNumeric(Object rowValue, Object literalValue, Compariso
184184
private Truthness compareString(Object rowValue, Object literalValue, ComparisonType comparisonType) {
185185
if (comparisonType != ComparisonType.EQUALS) {
186186
throw new IllegalArgumentException("Unsupported operator for string literals: " + comparisonType);
187-
} else {
188-
String a = (rowValue instanceof InetAddress)
189-
? ((InetAddress) rowValue).getHostAddress()
190-
: (String) rowValue;
191-
String b = (String) literalValue;
192-
193-
return TruthnessUtils.getStringEqualityTruthness(a, b);
194187
}
188+
189+
String a = (rowValue instanceof InetAddress)
190+
? ((InetAddress) rowValue).getHostAddress()
191+
: (String) rowValue;
192+
String b = (String) literalValue;
193+
194+
return TruthnessUtils.getStringEqualityTruthness(a, b);
195195
}
196196

197197
private Truthness compareBoolean(Object rowValue, Object literalValue, ComparisonType comparisonType) {
198198
if (comparisonType != ComparisonType.EQUALS) {
199199
throw new IllegalArgumentException("Unsupported operator for boolean literals: " + comparisonType);
200-
} else {
201-
double x = ((Boolean) rowValue) ? 1.0 : 0.0;
202-
double y = ((Boolean) literalValue) ? 1.0 : 0.0;
203-
204-
return TruthnessUtils.getEqualityTruthness(x, y);
205200
}
201+
202+
double x = ((Boolean) rowValue) ? 1.0 : 0.0;
203+
double y = ((Boolean) literalValue) ? 1.0 : 0.0;
204+
205+
return TruthnessUtils.getEqualityTruthness(x, y);
206206
}
207207

208208
private Truthness compareUuid(Object rowValue, Object literalValue, ComparisonType comparisonType) {
209209
if (comparisonType != ComparisonType.EQUALS) {
210210
throw new IllegalArgumentException("Unsupported operator for UUID literals: " + comparisonType);
211-
} else {
212-
return TruthnessUtils.getEqualityTruthness((UUID) rowValue, (UUID) literalValue);
213211
}
212+
213+
return TruthnessUtils.getEqualityTruthness((UUID) rowValue, (UUID) literalValue);
214214
}
215215

216216
private static boolean isTemporalType(Object rowValue) {
@@ -226,7 +226,7 @@ private Truthness compareTemporal(Object rowValue, Object literalValue, Comparis
226226

227227
return getTruthnessForNumeric(comparisonType, x, y);
228228
} catch (Exception e) {
229-
return FALSE_TRUTHNESS;
229+
throw new IllegalArgumentException("Failed to compare temporal values: " + e.getMessage(), e);
230230
}
231231
}
232232

@@ -350,23 +350,26 @@ private static boolean isCqlDuration(Object rowValue) {
350350
}
351351
}
352352

353-
private Truthness compareDuration(Object rowValue, Object literalValue, ComparisonType comparisonType) {
353+
private Truthness compareDuration(TemporalAmount rowValue, Object literalValue, ComparisonType comparisonType) {
354354
if (comparisonType != ComparisonType.EQUALS) {
355355
throw new IllegalArgumentException("Unsupported operator for duration literals: " + comparisonType);
356-
} else {
357-
TemporalAmount duration = (TemporalAmount) rowValue;
358-
long months = duration.get(ChronoUnit.MONTHS);
359-
long days = duration.get(ChronoUnit.DAYS);
360-
long nanos = duration.get(ChronoUnit.NANOS);
361-
362-
CqlDurationLiteral literalDurationValue = CqlDurationLiteralParser.parse((String) literalValue);
363-
364-
return TruthnessUtils.buildAndAggregationTruthness(
365-
TruthnessUtils.getEqualityTruthness(months, literalDurationValue.months),
366-
TruthnessUtils.getEqualityTruthness(days, literalDurationValue.days),
367-
TruthnessUtils.getEqualityTruthness(nanos, literalDurationValue.nanos)
368-
);
369356
}
357+
358+
long months = rowValue.get(ChronoUnit.MONTHS);
359+
long days = rowValue.get(ChronoUnit.DAYS);
360+
long nanos = rowValue.get(ChronoUnit.NANOS);
361+
362+
CqlDurationLiteral literalDurationValue = CqlDurationLiteralParser.parse((String) literalValue);
363+
364+
Truthness monthsTruthness = TruthnessUtils.getEqualityTruthness(months, literalDurationValue.months);
365+
Truthness daysTruthness = TruthnessUtils.getEqualityTruthness(days, literalDurationValue.days);
366+
Truthness nanosTruthness = TruthnessUtils.getEqualityTruthness(nanos, literalDurationValue.nanos);
367+
368+
return TruthnessUtils.buildAndAggregationTruthness(
369+
monthsTruthness,
370+
daysTruthness,
371+
nanosTruthness
372+
);
370373
}
371374

372375
private Truthness any(Object value, List<?> candidates) {

client-java/controller/src/main/java/org/evomaster/client/java/controller/cassandra/parser/CqlDurationLiteralParser.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ private CqlDurationLiteralParser() {}
5353
private static final String ISO_DURATION_PREFIX_UPPER = "P";
5454
private static final String ISO_DURATION_PREFIX_LOWER = "p";
5555
private static final String ISO_WEEK_SUFFIX = "W";
56+
private static final String MINUS_SIGN = "-";
57+
private static final String ISO8601_ALTERNATIVE_DATE_SEPARATOR = "-";
5658

5759
/**
5860
* Matches the standard Cassandra format: an optional digit sequence followed by a unit
@@ -105,7 +107,7 @@ public static CqlDurationLiteral parse(String stringDuration) {
105107
if (t.isEmpty()) {
106108
throw new IllegalArgumentException("Empty duration literal");
107109
} else {
108-
boolean isNegative = t.startsWith("-");
110+
boolean isNegative = t.startsWith(MINUS_SIGN);
109111
String unsigned = isNegative ? t.substring(1) : t;
110112
if (unsigned.isEmpty()) {
111113
throw new IllegalArgumentException("Empty duration literal");
@@ -134,7 +136,7 @@ private static CqlDurationLiteral parseUnsigned(String unsigned) {
134136
String upper = unsigned.toUpperCase();
135137
if (upper.endsWith(ISO_WEEK_SUFFIX)) {
136138
return parseIso8601WeekPattern(upper);
137-
} else if (upper.contains("-")) {
139+
} else if (upper.contains(ISO8601_ALTERNATIVE_DATE_SEPARATOR)) {
138140
return parseIso8601AlternativePattern(upper);
139141
} else {
140142
return parseIso8601Pattern(upper);

0 commit comments

Comments
 (0)