import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;
public class ApiEndpointDiff {
public static void main(String[] args) {
// Example datasets - replace with your actual data
Map<String, Map<String, String>> sandboxEndpoints1 = new HashMap<>();
sandboxEndpoints1.put("users", new HashMap<>());
sandboxEndpoints1.get("users").put("get", "GET /users");
sandboxEndpoints1.get("users").put("create", "POST /users");
sandboxEndpoints1.get("users").put("update", "PUT /users/{id}");
sandboxEndpoints1.get("users").put("delete", "DELETE /users/{id}");
sandboxEndpoints1.get("products").put("get", "GET /products");
sandboxEndpoints1.get("products").put("create", "POST /products");
Map<String, Map<String, String>> sandboxEndpoints2 = new HashMap<>();
sandboxEndpoints2.put("users", new HashMap<>());
sandboxEndpoints2.get("users").put("get", "GET /users");
sandboxEndpoints2.get("users").put("create", "POST /users");
sandboxEndpoints2.get("users").put("update", "PUT /users/{id}");
sandboxEndpoints2.get("users").put("delete", "DELETE /users/{id}");
sandboxEndpoints2.get("products").put("get", "GET /products");
sandboxEndpoints2.get("products").put("create", "POST /products");
sandboxEndpoints2.get("products").put("list", "GET /products?page={page}&size={size}"); //New Endpoint
// Compare the datasets
compareEndpoints(sandboxEndpoints1, sandboxEndpoints2);
}
public static void compareEndpoints(Map<String, Map<String, String>> endpoints1, Map<String, Map<String, String>> endpoints2) {
// Find all unique endpoint names
Set<String> allEndpoints = new HashSet<>();
for (Map.Entry<String, Map<String, String>> entry : endpoints1.entrySet()) {
allEndpoints.add(entry.getKey());
for (String method : entry.getValue().keySet()) {
allEndpoints.add(entry.getKey() + "-" + method);
}
}
for (Map.Entry<String, Map<String, String>> entry : endpoints2.entrySet()) {
allEndpoints.add(entry.getKey());
for (String method : entry.getValue().keySet()) {
allEndpoints.add(entry.getKey() + "-" + method);
}
}
// Compare each endpoint
for (String endpointName : allEndpoints) {
String[] parts = endpointName.split("-");
String resource = parts[0];
String method = parts[1];
String endpoint1 = getEndpointDetails(endpoints1, resource, method);
String endpoint2 = getEndpointDetails(endpoints2, resource, method);
if (!endpoint1.equals(endpoint2)) {
System.out.println("Difference found for " + resource + "-" + method + ":");
System.out.println(" Sandbox 1: " + endpoint1);
System.out.println(" Sandbox 2: " + endpoint2);
}
}
}
// Helper function to retrieve endpoint details
private static String getEndpointDetails(Map<String, Map<String, String>> endpoints, String resource, String method) {
if (endpoints.containsKey(resource) && endpoints.get(resource).containsKey(method)) {
return endpoints.get(resource).get(method);
}
return null; // Or handle the case where the endpoint is missing
}
}
Add your comment