Skip to content

Commit 2907fec

Browse files
committed
Issue #109. Added a new field to the Callable object to now track all the crud operations and queries that occur in it as an array list. Also updated the test case to capture this.
Signed-off-by: Rahul Krishna <i.m.ralk@gmail.com>
1 parent da55430 commit 2907fec

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

src/main/java/com/ibm/cldk/SymbolTable.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,12 @@ private static Pair<String, Callable> processCallableDeclaration(CallableDeclara
418418
.filter(Objects::nonNull)
419419
.collect(Collectors.toList())
420420
);
421+
callableNode.setCrudQueries(
422+
callableNode.getCallSites().stream()
423+
.map(CallSite::getCrudQuery)
424+
.filter(Objects::nonNull)
425+
.collect(Collectors.toList())
426+
);
421427
callableNode.setVariableDeclarations(getVariableDeclarations(body));
422428
callableNode.setCyclomaticComplexity(getCyclomaticComplexity(callableDecl));
423429

@@ -697,20 +703,22 @@ private static List<CallSite> getCallSites(Optional<BlockStmt> callableBody) {
697703
// Get argument string from the callsite
698704
List<String> listOfArgumentStrings = methodCallExpr.getArguments().stream().map(Expression::toString).collect(Collectors.toList());
699705
// Determine if this call site is potentially a CRUD operation.
700-
CRUDOperation crudOperation = new CRUDOperation();
706+
CRUDOperation crudOperation = null;
701707
Optional<CRUDOperationType> crudOperationType = findCRUDOperation(declaringType, methodCallExpr.getNameAsString());
702708
if (crudOperationType.isPresent()) {
703709
// We found a CRUD operation, so we need to populate the details of the call site this CRUD operation.
704710
int lineNumber = methodCallExpr.getRange().isPresent() ? methodCallExpr.getRange().get().begin.line : -1;
711+
crudOperation = new CRUDOperation();
705712
crudOperation.setLineNumber(lineNumber);
706713
crudOperation.setOperationType(crudOperationType.get());
707714
}
708715
// Determine if this call site is potentially a CRUD query.
709-
CRUDQuery crudQuery = new CRUDQuery();
716+
CRUDQuery crudQuery = null;
710717
Optional<CRUDQueryType> crudQueryType = findCRUDQuery(declaringType, methodCallExpr.getNameAsString(), Optional.of(listOfArgumentStrings));
711718
if (crudQueryType.isPresent()) {
712719
// We found a CRUD query, so we need to populate the details of the call site this CRUD query.
713720
int lineNumber = methodCallExpr.getRange().isPresent() ? methodCallExpr.getRange().get().begin.line : -1;
721+
crudQuery = new CRUDQuery();
714722
crudQuery.setLineNumber(lineNumber);
715723
crudQuery.setQueryType(crudQueryType.get());
716724
crudQuery.setQueryArguments(listOfArgumentStrings);

src/main/java/com/ibm/cldk/entities/Callable.java

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class Callable {
2626
private List<CallSite> callSites;
2727
private List<VariableDeclaration> variableDeclarations;
2828
private List<CRUDOperation> crudOperations = new ArrayList<>();
29+
private List<CRUDQuery> crudQueries = new ArrayList<>();
2930
private int cyclomaticComplexity;
3031
private boolean isEntrypoint = false;
3132
}

src/test/java/com/ibm/cldk/CodeAnalyzerIntegrationTest.java

+28-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.nio.file.Paths;
1616
import java.util.Properties;
1717

18+
1819
@Testcontainers
1920
@SuppressWarnings("resource")
2021
public class CodeAnalyzerIntegrationTest {
@@ -165,9 +166,32 @@ void shouldBeAbleToDetectCRUDOperationsAndQueriesForPlantByWebsphere() throws Ex
165166
"--input=/test-applications/plantsbywebsphere",
166167
"--analysis-level=1", "--verbose"
167168
);
168-
Assertions.assertTrue(runCodeAnalyzerOnPlantsByWebsphere.getStdout().contains("\"query_type\": \"NAMED\""), "No entry point classes found");
169-
Assertions.assertTrue(runCodeAnalyzerOnPlantsByWebsphere.getStdout().contains("\"operation_type\": \"READ\""), "No entry point methods found");
170-
Assertions.assertTrue(runCodeAnalyzerOnPlantsByWebsphere.getStdout().contains("\"operation_type\": \"UPDATE\""), "No entry point methods found");
171-
Assertions.assertTrue(runCodeAnalyzerOnPlantsByWebsphere.getStdout().contains("\"operation_type\": \"CREATE\""), "No entry point methods found");
169+
170+
String output = runCodeAnalyzerOnPlantsByWebsphere.getStdout();
171+
172+
Assertions.assertTrue(output.contains("\"query_type\": \"NAMED\""), "No entry point classes found");
173+
Assertions.assertTrue(output.contains("\"operation_type\": \"READ\""), "No entry point methods found");
174+
Assertions.assertTrue(output.contains("\"operation_type\": \"UPDATE\""), "No entry point methods found");
175+
Assertions.assertTrue(output.contains("\"operation_type\": \"CREATE\""), "No entry point methods found");
176+
177+
// Convert the expected JSON structure into a string
178+
String expectedCrudOperation =
179+
"\"crud_operations\": [" +
180+
"{" +
181+
"\"line_number\": 115," +
182+
"\"operation_type\": \"READ\"," +
183+
"\"target_table\": null," +
184+
"\"involved_fields\": null," +
185+
"\"condition\": null," +
186+
"\"joined_tables\": null," +
187+
"\"technology\": null," +
188+
"\"is_batch_operation\": false" +
189+
"}]";
190+
191+
// Remove whitespace and newlines for a more robust comparison
192+
String normalizedOutput = output.replaceAll("\\s+", "");
193+
String normalizedExpectedCrudOperation = expectedCrudOperation.replaceAll("\\s+", "");
194+
195+
Assertions.assertTrue(normalizedOutput.contains(normalizedExpectedCrudOperation), "Expected CRUD operation JSON structure not found");
172196
}
173197
}

0 commit comments

Comments
 (0)