package com.billpaymentservice.bbps; import java.net.URI; import java.net.http.*; import java.util.*; import java.util.concurrent.*; import org.json.*; public class BBPSAutomationTest { private static final String BASE_URL = "http://localhost:9090/masterData"; private static final String BILLER_API = "https://bbps-staging.digiledge.in/agent/cou-master/masters/billers"; private static final HttpClient client = HttpClient.newHttpClient(); // Run 10 threads in parallel private static final ExecutorService executor = Executors.newFixedThreadPool(10); public static void main(String[] args) { try { System.out.println("šŸš€ Starting BBPS Test Flow (Multi-Threaded)...\n"); List categories = getAllCategories(); if (categories.isEmpty()) { System.out.println("āŒ No categories found. Exiting..."); return; } System.out.println("\nāœ… Total Categories Found: " + categories.size()); int count = 1; for (String category : categories) { System.out.println("\nšŸ“‚ [" + count++ + "/" + categories.size() + "] Category: " + category); List billerIds = Collections.emptyList(); try { billerIds = fetchBillersByCategory(category); } catch (Exception ex) { System.err.println("āš ļø Skipping category '" + category + "' due to error: " + ex.getMessage()); continue; // skip and go to next category } if (billerIds == null || billerIds.isEmpty()) { System.out.println("āš ļø No billers found for: " + category + " — Skipping..."); continue; } System.out.println("šŸ”¹ Found " + billerIds.size() + " billers. Fetching details in parallel..."); // Run all biller calls in parallel threads List> futures = new ArrayList<>(); for (String billerId : billerIds) { futures.add(executor.submit(() -> { try { sendBillerDetails(billerId); } catch (Exception e) { System.err.println("āŒ Error with biller " + billerId + ": " + e.getMessage()); } })); } // Wait for all billers to finish for (Future f : futures) { try { f.get(); } catch (Exception e) { System.err.println("āš ļø Thread interrupted: " + e.getMessage()); } } System.out.println("āœ… Completed all billers for category: " + category); } executor.shutdown(); System.out.println("\nšŸŽÆ All categories processed successfully (multi-threaded & safe)!"); } catch (Exception e) { e.printStackTrace(); } } // Step 1 - Get All Categories private static List getAllCategories() throws Exception { String reqId = UUID.randomUUID().toString(); JSONObject body = new JSONObject(); body.put("mid", "ALLCOS00117BHO"); body.put("reqId", reqId); body.put("secretKey", "ABCD1234SECRET"); JSONObject device = new JSONObject(); device.put("deviceId", "DEVICE001"); device.put("deviceName", "Samsung Galaxy S23"); device.put("os", "Android 14"); device.put("appVersion", "1.0.5"); device.put("ipAddress", "192.168.1.101"); body.put("deviceDetial", device); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(BASE_URL + "/getAllMastersData")) .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofString(body.toString())) .build(); HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); if (response.statusCode() != 200) { System.out.println("āŒ getAllMastersData failed | HTTP " + response.statusCode()); return Collections.emptyList(); } JSONObject json = new JSONObject(response.body()); JSONArray catArray = json.optJSONObject("data") != null ? json.getJSONObject("data").optJSONArray("categoryRespList") : null; List categories = new ArrayList<>(); if (catArray != null && catArray.length() > 0) { for (int i = 0; i < catArray.length(); i++) { categories.add(catArray.getJSONObject(i).getString("categoryName")); } } System.out.println("āœ… Categories found: " + categories.size()); return categories; } // Step 2 - Fetch Billers by Category private static List fetchBillersByCategory(String category) throws Exception { // Encode category to handle spaces & special characters safely String encodedCategory = java.net.URLEncoder.encode(category, java.nio.charset.StandardCharsets.UTF_8); String url = BILLER_API + "?category=" + encodedCategory + "&page=0&pagesize=50"; HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url)).GET().build(); HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); if (response.statusCode() != 200) { System.out.println("āŒ Failed to fetch billers for " + category + " | HTTP " + response.statusCode()); return Collections.emptyList(); } JSONObject json = new JSONObject(response.body()); JSONArray billersArray = json.optJSONArray("billerResp"); if (billersArray == null || billersArray.length() == 0) { System.out.println("āš ļø billerResp empty or null for category: " + category); return Collections.emptyList(); } List billerIds = new ArrayList<>(); for (int i = 0; i < billersArray.length(); i++) { billerIds.add(billersArray.getJSONObject(i).getString("billerId")); } return billerIds; } // Step 3 - Get Biller Details private static void sendBillerDetails(String billerId) throws Exception { String reqId = UUID.randomUUID().toString(); JSONObject body = new JSONObject(); body.put("mid", "ALLCOS00117BHO"); body.put("reqId", reqId); body.put("billerId", billerId); body.put("secretKey", "ABCD1234SECRET"); JSONObject device = new JSONObject(); device.put("deviceId", "DEVICE001"); device.put("deviceName", "Samsung Galaxy S23"); device.put("os", "Android 14"); device.put("appVersion", "1.0.5"); device.put("ipAddress", "192.168.1.101"); body.put("deviceDetial", device); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(BASE_URL + "/getBillerDetails")) .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofString(body.toString())) .build(); HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); synchronized (System.out) { // thread-safe log printing if (response.statusCode() == 200) { System.out.println("āœ… Success: " + billerId); } else { System.out.println("āŒ Failed: " + billerId + " | HTTP " + response.statusCode()); } } } }