adamsaghy commented on code in PR #5353:
URL: https://github.com/apache/fineract/pull/5353#discussion_r2746476456


##########
integration-tests/src/test/java/org/apache/fineract/integrationtests/ReportExecutionIntegrationTest.java:
##########
@@ -0,0 +1,215 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.fineract.integrationtests;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+import lombok.extern.slf4j.Slf4j;
+import okhttp3.MediaType;
+import okhttp3.ResponseBody;
+import org.apache.fineract.client.models.GetReportsResponse;
+import org.apache.fineract.client.models.PostClientsRequest;
+import org.apache.fineract.client.models.PostLoanProductsRequest;
+import org.apache.fineract.client.models.PostLoanProductsResponse;
+import org.apache.fineract.client.models.PostLoansLoanIdRequest;
+import org.apache.fineract.client.models.PostLoansRequest;
+import org.apache.fineract.client.models.PostLoansResponse;
+import org.apache.fineract.client.models.RunReportsResponse;
+import org.apache.fineract.integrationtests.client.IntegrationTest;
+import org.apache.fineract.integrationtests.common.Utils;
+import org.junit.jupiter.api.Test;
+import retrofit2.Call;
+import retrofit2.Response;
+import retrofit2.http.GET;
+
+@Slf4j
+class ReportExecutionIntegrationTest extends IntegrationTest {
+
+    // Internal interface to reuse the working FineractClient configuration
+    interface MetricsApi {
+
+        @GET("../../actuator/metrics/fineract.report.execution")
+        Call<ResponseBody> getReportExecutionMetrics();
+    }
+
+    @Test
+    void verifyClientListingReportAndMetrics() throws IOException {
+        String uniqueClientName = "ReportTestClient_" + 
UUID.randomUUID().toString().substring(0, 8);
+        Long clientId = createClient(uniqueClientName);
+        assertThat(clientId).isNotNull();
+
+        // Run the Client Listing Report (Table/JSON format)
+        Response<RunReportsResponse> response = okR(
+                fineractClient().reportsRun.runReportGetData("Client Listing", 
Map.of("R_officeId", "1"), false));
+
+        assertThat(response.code()).isEqualTo(200);
+        RunReportsResponse body = response.body();
+        assertThat(body).isNotNull();
+        assertThat(body.getColumnHeaders()).isNotEmpty();
+        assertThat(body.getData()).isNotNull();
+        assertThat(body.getData().toString()).contains(uniqueClientName);
+
+        // Run the Client Listing Report (CSV format)
+        Response<ResponseBody> csvResponse = okR(
+                fineractClient().reportsRun.runReportGetFile("Client Listing", 
Map.of("R_officeId", "1", "exportCSV", "true"), false));
+
+        assertThat(csvResponse.body()).isNotNull();
+
+        try (ResponseBody csvBody = csvResponse.body()) {
+            
assertThat(csvBody.contentType()).isEqualTo(MediaType.parse("text/csv"));
+            String csvContent = csvBody.string();
+            assertThat(csvContent).contains(uniqueClientName);
+        }
+    }
+
+    @Test
+    void verifyLoanListingReport() {
+        String uniqueClientName = "LoanReportClient_" + 
UUID.randomUUID().toString().substring(0, 8);
+        Long clientId = createClient(uniqueClientName);
+        Long loanProductId = createLoanProduct();
+        Long loanId = applyForLoan(clientId, loanProductId);
+        approveLoan(loanId);
+        disburseLoan(loanId, BigDecimal.valueOf(1000.0));
+
+        Response<RunReportsResponse> response = okR(
+                fineractClient().reportsRun.runReportGetData("Active Loans - 
Details", Map.of("R_officeId", "1", "R_loanOfficerId", "-1",
+                        "R_currencyId", "-1", "R_fundId", "-1", 
"R_loanProductId", "-1", "R_loanPurposeId", "-1"), false));
+
+        assertThat(response.code()).isEqualTo(200);
+        RunReportsResponse body = response.body();
+        assertThat(body).isNotNull();
+        assertThat(body.getData()).isNotNull();
+        assertThat(body.getData().toString()).contains(String.valueOf(loanId));
+    }
+
+    @Test
+    void verifyAllReportsExecution() throws IOException {
+        // Setup Data needed to populate reports with context
+        String uniqueClientName = "LoanReportClient_" + 
UUID.randomUUID().toString().substring(0, 8);
+        Long clientId = createClient(uniqueClientName);
+        Long loanProductId = createLoanProduct();
+        Long loanId = applyForLoan(clientId, loanProductId);
+        approveLoan(loanId);
+        disburseLoan(loanId, BigDecimal.valueOf(1000.0));
+
+        // Build the map of parameters
+        // We pass ALL known IDs. Fineract reports will ignore parameters they 
don't need, ensuring this is safe.
+        Map<String, String> commonParams = new HashMap<>();
+        commonParams.put("R_officeId", "1");
+        commonParams.put("R_clientId", clientId.toString());
+        commonParams.put("R_loanId", loanId.toString());
+        commonParams.put("R_loanProductId", loanProductId.toString());
+        commonParams.put("R_loanOfficerId", "-1");
+        commonParams.put("R_currencyId", "-1");
+        commonParams.put("R_fundId", "-1");
+        commonParams.put("R_startDate", "2023-01-01");
+        commonParams.put("R_endDate", "2025-12-31");
+
+        // Fetch ALL reports
+        Response<List<GetReportsResponse>> reportListResponse = 
okR(fineractClient().reports.retrieveReportList());
+        assertThat(reportListResponse.isSuccessful()).isTrue();
+        List<GetReportsResponse> allReports = reportListResponse.body();
+        assertThat(allReports).isNotEmpty();
+
+        int successCount = 0;
+        int validationErrorCount = 0;
+
+        // Iterate and Execute
+        for (GetReportsResponse report : allReports) {
+            String reportName = report.getReportName();
+
+            Response<RunReportsResponse> response = 
fineractClient().reportsRun.runReportGetData(reportName, commonParams, 
false).execute();
+
+            // Strict Validation
+            // Allow 200 (Success) OR 400 (Bad Request - Validation Error).
+            // FAIL on 500 (Server Error) or any other code.
+            if (response.code() == 200) {
+                successCount++;
+            } else if (response.code() == 400) {
+                // If it fails, ensure it's a controlled validation error, not 
a crash.
+                validationErrorCount++;
+                log.info("Report '{}' returned 400 (Expected for missing 
specialized params).", reportName);
+            } else {
+                // Fail the test if we get a 500 or 404
+                assertThat(response.code()).as("Report '%s' failed with 
unexpected status code", reportName).isNotEqualTo(500);
+            }
+        }
+
+        log.info("Report Execution Summary: {} Success, {} Validation Errors. 
Total: {}", successCount, validationErrorCount,
+                allReports.size());
+
+        // Ensure we completed the process successfully
+        assertThat(successCount).as("Should have successfully executed at 
least some reports").isGreaterThan(0);

Review Comment:
   This assertion is not useful at all.. If at least one report was executed 
successfully, it will not fail the test case. 
   
   We should make sure there are no any failed reports!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to