1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. public class ApiEndpointDiff {
  6. public static void main(String[] args) {
  7. // Sample datasets - replace with your actual data
  8. List<ApiEndpoint> dataset1 = getApiEndpoints();
  9. List<ApiEndpoint> dataset2 = getApiEndpoints();
  10. if (dataset1 == null || dataset2 == null) {
  11. System.err.println("Error: One or both datasets are null.");
  12. return;
  13. }
  14. diffEndpoints(dataset1, dataset2);
  15. }
  16. //Helper method to create sample datasets. Replace with your data source.
  17. private static List<ApiEndpoint> getApiEndpoints() {
  18. List<ApiEndpoint> endpoints = new ArrayList<>();
  19. endpoints.add(new ApiEndpoint("GET /users", "Retrieves user list", null, null));
  20. endpoints.add(new ApiEndpoint("POST /users", "Creates a new user", null, null));
  21. endpoints.add(new ApiEndpoint("GET /users/{id}", "Retrieves a specific user", "id", null));
  22. endpoints.add(new ApiEndpoint("PUT /users/{id}", "Updates a user", "id", null));
  23. endpoints.add(new ApiEndpoint("DELETE /users/{id}", "Deletes a user", "id", null));
  24. endpoints.add(new ApiEndpoint("GET /products", "Retrieves product list", null, null));
  25. endpoints.add(new ApiEndpoint("POST /products", "Creates a new product", null, null));
  26. return endpoints;
  27. }
  28. public static void diffEndpoints(List<ApiEndpoint> dataset1, List<ApiEndpoint> dataset2) {
  29. if (dataset1.size() != dataset2.size()) {
  30. System.out.println("Dataset sizes differ. Cannot perform meaningful diff.");
  31. System.out.println("Dataset 1 size: " + dataset1.size());
  32. System.out.println("Dataset 2 size: " + dataset2.size());
  33. return;
  34. }
  35. // Iterate through the datasets and compare each endpoint
  36. for (int i = 0; i < dataset1.size(); i++) {
  37. ApiEndpoint endpoint1 = dataset1.get(i);
  38. ApiEndpoint endpoint2 = dataset2.get(i);
  39. if (!endpoint1.equals(endpoint2)) {
  40. System.out.println("--- Endpoint Comparison ---");
  41. System.out.println("Dataset 1: " + endpoint1);
  42. System.out.println("Dataset 2: " + endpoint2);
  43. System.out.println("--- Differences ---");
  44. //Compare method and parameters
  45. if(endpoint1.getMethod() != endpoint2.getMethod()){
  46. System.out.println("Method mismatch: " + endpoint1.getMethod() + " vs " + endpoint2.getMethod());
  47. }
  48. if(endpoint1.getParams() != null && endpoint2.getParams() != null) {
  49. if(!endpoint1.getParams().equals(endpoint2.getParams())){
  50. System.out.println("Parameters mismatch: " + endpoint1.getParams() + " vs " + endpoint2.getParams());
  51. }
  52. } else if ((endpoint1.getParams() != null) != (endpoint2.getParams() != null)){
  53. System.out.println("Parameter presence mismatch: " + (endpoint1.getParams() != null ? "Present" : "Absent") + " vs " + (endpoint2.getParams() != null ? "Present" : "Absent"));
  54. }
  55. System.out.println("--- End of Comparison ---");
  56. }
  57. }
  58. }
  59. // Inner class representing an API endpoint
  60. static class ApiEndpoint {
  61. private String method;
  62. private String description;
  63. private Map<String, String> params; // Key: parameter name, Value: parameter type
  64. private String id; //Optional ID
  65. public ApiEndpoint(String method, String description, String id, Map<String, String> params) {
  66. this.method = method;
  67. this.description = description;
  68. this.id = id;
  69. this.params = params;
  70. }
  71. public String getMethod() {
  72. return method;
  73. }
  74. public String getDescription() {
  75. return description;
  76. }
  77. public Map<String, String> get

Add your comment