|
|
|
@@ -9,6 +9,9 @@ import org.springframework.http.HttpMethod;
|
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
|
import tools.jackson.databind.ObjectMapper;
|
|
|
|
|
import tools.jackson.dataformat.xml.XmlMapper;
|
|
|
|
|
import ua.com.dxrkness.dto.FreightRequest;
|
|
|
|
|
import ua.com.dxrkness.dto.RouteRequest;
|
|
|
|
|
import ua.com.dxrkness.dto.VehicleRequest;
|
|
|
|
|
import ua.com.dxrkness.model.Freight;
|
|
|
|
|
import ua.com.dxrkness.model.Route;
|
|
|
|
|
import ua.com.dxrkness.model.Vehicle;
|
|
|
|
@@ -16,7 +19,9 @@ import ua.com.dxrkness.model.Vehicle;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class ApplicationClient {
|
|
|
|
|
private static final String BASE_URL = "http://localhost:8080";
|
|
|
|
|
private static final String FREIGHTS_BASE_URL = "http://localhost:8080";
|
|
|
|
|
private static final String VEHICLES_BASE_URL = "http://localhost:8081";
|
|
|
|
|
private static final String ROUTES_BASE_URL = "http://localhost:8082";
|
|
|
|
|
private static final ObjectMapper jsonMapper = new ObjectMapper();
|
|
|
|
|
private static final XmlMapper xmlMapper = new XmlMapper();
|
|
|
|
|
|
|
|
|
@@ -39,30 +44,30 @@ public class ApplicationClient {
|
|
|
|
|
private static void testVehicles(CloseableHttpClient client) throws Exception {
|
|
|
|
|
System.out.println("--- VEHICLES ---");
|
|
|
|
|
|
|
|
|
|
execute(client, new HttpGet(BASE_URL + "/vehicles"), "GET all vehicles");
|
|
|
|
|
execute(client, new HttpGet(VEHICLES_BASE_URL + "/vehicles"), "GET all vehicles");
|
|
|
|
|
|
|
|
|
|
Vehicle newVehicle = new Vehicle(0, "Mercedes", "Actros", "AA1234BB", 2023, 18000, Vehicle.Status.AVAILABLE);
|
|
|
|
|
var newVehicle = new VehicleRequest("Mercedes", "Actros", "AA1234BB", 2023, 18000, Vehicle.Status.AVAILABLE);
|
|
|
|
|
String vehicleXml = xmlMapper.writeValueAsString(newVehicle);
|
|
|
|
|
HttpPost post = new HttpPost(BASE_URL + "/vehicles");
|
|
|
|
|
HttpPost post = new HttpPost(VEHICLES_BASE_URL + "/vehicles");
|
|
|
|
|
post.setEntity(new StringEntity(vehicleXml, "UTF-8"));
|
|
|
|
|
post.setHeader("Content-Type", "application/xml");
|
|
|
|
|
post.setHeader("Accept", "application/xml");
|
|
|
|
|
String createdVehicle = execute(client, post, "POST new vehicle");
|
|
|
|
|
|
|
|
|
|
var get = new HttpGet(BASE_URL + "/vehicles/0");
|
|
|
|
|
var get = new HttpGet(VEHICLES_BASE_URL + "/vehicles/1");
|
|
|
|
|
get.setHeader("Content-Type", "application/xml");
|
|
|
|
|
execute(client, get, "GET vehicle by ID=0");
|
|
|
|
|
execute(client, new HttpGet(BASE_URL + "/vehicles/999"), "GET vehicle by ID=999 (not found)");
|
|
|
|
|
execute(client, get, "GET vehicle by ID=1");
|
|
|
|
|
execute(client, new HttpGet(VEHICLES_BASE_URL + "/vehicles/999"), "GET vehicle by ID=999 (not found)");
|
|
|
|
|
|
|
|
|
|
Vehicle updateVehicle = new Vehicle(1, "Volvo", "FH16", "BB5678CC", 2024, 20000, Vehicle.Status.MAINTENANCE);
|
|
|
|
|
var vehiclePut = createHttpRequest(updateVehicle, "/vehicles/0", HttpMethod.PUT);
|
|
|
|
|
execute(client, vehiclePut, "PUT update vehicle ID=0");
|
|
|
|
|
var updateVehicle = new VehicleRequest("Volvo", "FH16", "BB5678CC", 2024, 20000, Vehicle.Status.MAINTENANCE);
|
|
|
|
|
var vehiclePut = createHttpRequest(updateVehicle, "/vehicles/1", HttpMethod.PUT);
|
|
|
|
|
execute(client, vehiclePut, "PUT update vehicle ID=1");
|
|
|
|
|
|
|
|
|
|
Vehicle patchVehicle = new Vehicle(1, "Volvo", "FH16", "BB5678CC", 2024, 20000, Vehicle.Status.IN_TRANSIT);
|
|
|
|
|
var vehiclePatch = createHttpRequest(patchVehicle, "/vehicles/0", HttpMethod.PATCH);
|
|
|
|
|
execute(client, vehiclePatch, "PATCH update vehicle ID=0");
|
|
|
|
|
var patchVehicle = new VehicleRequest("Volvo", "FH16", "BB5678CC", 2024, 20000, Vehicle.Status.IN_TRANSIT);
|
|
|
|
|
var vehiclePatch = createHttpRequest(patchVehicle, "/vehicles/1", HttpMethod.PATCH);
|
|
|
|
|
execute(client, vehiclePatch, "PATCH update vehicle ID=1");
|
|
|
|
|
|
|
|
|
|
execute(client, new HttpDelete(BASE_URL + "/vehicles/999"), "DELETE vehicle ID=999 (not found)");
|
|
|
|
|
execute(client, new HttpDelete(VEHICLES_BASE_URL + "/vehicles/999"), "DELETE vehicle ID=999 (not found)");
|
|
|
|
|
|
|
|
|
|
System.out.println();
|
|
|
|
|
}
|
|
|
|
@@ -70,25 +75,25 @@ public class ApplicationClient {
|
|
|
|
|
private static void testFreights(CloseableHttpClient client) throws Exception {
|
|
|
|
|
System.out.println("--- FREIGHTS ---");
|
|
|
|
|
|
|
|
|
|
execute(client, new HttpGet(BASE_URL + "/freights"), "GET all freights");
|
|
|
|
|
execute(client, new HttpGet(FREIGHTS_BASE_URL + "/freights"), "GET all freights");
|
|
|
|
|
|
|
|
|
|
Freight.Dimensions dims = new Freight.Dimensions(120, 100, 200);
|
|
|
|
|
Freight newFreight = new Freight(0, "Electronics", "Laptops and monitors", 500, dims, Freight.Status.PENDING);
|
|
|
|
|
var dims = new Freight.Dimensions(120, 100, 200);
|
|
|
|
|
var newFreight = new FreightRequest("Electronics", "Laptops and monitors", 500, dims, Freight.Status.PENDING);
|
|
|
|
|
var freightPost = createHttpRequest(newFreight, "/freights", HttpMethod.POST);
|
|
|
|
|
execute(client, freightPost, "POST new freight");
|
|
|
|
|
|
|
|
|
|
execute(client, new HttpGet(BASE_URL + "/freights/0"), "GET freight by ID=0");
|
|
|
|
|
execute(client, new HttpGet(BASE_URL + "/freights/999"), "GET freight by ID=999 (not found)");
|
|
|
|
|
execute(client, new HttpGet(FREIGHTS_BASE_URL + "/freights/1"), "GET freight by ID=1");
|
|
|
|
|
execute(client, new HttpGet(FREIGHTS_BASE_URL + "/freights/999"), "GET freight by ID=999 (not found)");
|
|
|
|
|
|
|
|
|
|
Freight updateFreight = new Freight(1, "Furniture", "Office desks", 800, dims, Freight.Status.IN_TRANSIT);
|
|
|
|
|
var freightPut = createHttpRequest(updateFreight, "/freights/0", HttpMethod.PUT);
|
|
|
|
|
execute(client, freightPut, "PUT update freight ID=0");
|
|
|
|
|
var updateFreight = new FreightRequest("Furniture", "Office desks", 800, dims, Freight.Status.IN_TRANSIT);
|
|
|
|
|
var freightPut = createHttpRequest(updateFreight, "/freights/1", HttpMethod.PUT);
|
|
|
|
|
execute(client, freightPut, "PUT update freight ID=1");
|
|
|
|
|
|
|
|
|
|
Freight patchFreight = new Freight(1, "Furniture", "Office desks", 800, dims, Freight.Status.DELIVERED);
|
|
|
|
|
var freightPatch = createHttpRequest(patchFreight, "/freights/0", HttpMethod.PATCH);
|
|
|
|
|
execute(client, freightPatch, "PATCH update freight ID=0");
|
|
|
|
|
var patchFreight = new FreightRequest("Furniture", "Office desks", 800, dims, Freight.Status.DELIVERED);
|
|
|
|
|
var freightPatch = createHttpRequest(patchFreight, "/freights/1", HttpMethod.PATCH);
|
|
|
|
|
execute(client, freightPatch, "PATCH update freight ID=1");
|
|
|
|
|
|
|
|
|
|
execute(client, new HttpDelete(BASE_URL + "/freights/999"), "DELETE freight ID=999 (not found)");
|
|
|
|
|
execute(client, new HttpDelete(FREIGHTS_BASE_URL + "/freights/999"), "DELETE freight ID=999 (not found)");
|
|
|
|
|
|
|
|
|
|
System.out.println();
|
|
|
|
|
}
|
|
|
|
@@ -96,24 +101,24 @@ public class ApplicationClient {
|
|
|
|
|
private static void testRoutes(CloseableHttpClient client) throws Exception {
|
|
|
|
|
System.out.println("--- ROUTES ---");
|
|
|
|
|
|
|
|
|
|
Route newRoute = new Route(0, 0, List.of(0L), "Kyiv", "Lviv", 540.0, 8.5, Route.Status.PLANNED);
|
|
|
|
|
var newRoute = new RouteRequest(1, List.of(1L), "Kyiv", "Lviv", 540.0, 8.5, Route.Status.PLANNED);
|
|
|
|
|
var routePost = createHttpRequest(newRoute, "/routes", HttpMethod.POST);
|
|
|
|
|
execute(client, routePost, "POST new route");
|
|
|
|
|
|
|
|
|
|
execute(client, new HttpGet(BASE_URL + "/routes"), "GET all routes");
|
|
|
|
|
execute(client, new HttpGet(ROUTES_BASE_URL + "/routes"), "GET all routes");
|
|
|
|
|
|
|
|
|
|
execute(client, new HttpGet(BASE_URL + "/routes/0"), "GET route by ID=0");
|
|
|
|
|
execute(client, new HttpGet(BASE_URL + "/routes/999"), "GET route by ID=999 (not found)");
|
|
|
|
|
execute(client, new HttpGet(ROUTES_BASE_URL + "/routes/1"), "GET route by ID=1");
|
|
|
|
|
execute(client, new HttpGet(ROUTES_BASE_URL + "/routes/999"), "GET route by ID=999 (not found)");
|
|
|
|
|
|
|
|
|
|
Route updateRoute = new Route(0, 0, List.of(0L), "Kyiv", "Odesa", 480.0, 7.0, Route.Status.IN_PROGRESS);
|
|
|
|
|
var routePut = createHttpRequest(updateRoute, "/routes/0", HttpMethod.PUT);
|
|
|
|
|
execute(client, routePut, "PUT update route ID=0");
|
|
|
|
|
var updateRoute = new RouteRequest(1, List.of(1L), "Kyiv", "Odesa", 480.0, 7.0, Route.Status.IN_PROGRESS);
|
|
|
|
|
var routePut = createHttpRequest(updateRoute, "/routes/1", HttpMethod.PUT);
|
|
|
|
|
execute(client, routePut, "PUT update route ID=1");
|
|
|
|
|
|
|
|
|
|
Route patchRoute = new Route(0, 0, List.of(0L), "Kyiv", "Odesa", 480.0, 7.0, Route.Status.COMPLETED);
|
|
|
|
|
var routePatch = createHttpRequest(patchRoute, "/routes/0", HttpMethod.PATCH);
|
|
|
|
|
execute(client, routePatch, "PATCH update route ID=0");
|
|
|
|
|
var patchRoute = new RouteRequest(1, List.of(1L), "Kyiv", "Odesa", 480.0, 7.0, Route.Status.COMPLETED);
|
|
|
|
|
var routePatch = createHttpRequest(patchRoute, "/routes/1", HttpMethod.PATCH);
|
|
|
|
|
execute(client, routePatch, "PATCH update route ID=1");
|
|
|
|
|
|
|
|
|
|
execute(client, new HttpDelete(BASE_URL + "/routes/999"), "DELETE route ID=999 (not found)");
|
|
|
|
|
execute(client, new HttpDelete(ROUTES_BASE_URL + "/routes/999"), "DELETE route ID=999 (not found)");
|
|
|
|
|
|
|
|
|
|
System.out.println();
|
|
|
|
|
}
|
|
|
|
@@ -121,8 +126,8 @@ public class ApplicationClient {
|
|
|
|
|
private static void testSubResources(CloseableHttpClient client) throws Exception {
|
|
|
|
|
System.out.println("--- SUB-RESOURCES ---");
|
|
|
|
|
|
|
|
|
|
execute(client, new HttpGet(BASE_URL + "/routes/0/freights"), "GET freights for route ID=0 (sub-resource)");
|
|
|
|
|
execute(client, new HttpGet(BASE_URL + "/routes/0/vehicle"), "GET vehicle for route ID=0 (sub-resource)");
|
|
|
|
|
execute(client, new HttpGet(ROUTES_BASE_URL + "/routes/1/freights"), "GET freights for route ID=1 (sub-resource)");
|
|
|
|
|
execute(client, new HttpGet(ROUTES_BASE_URL + "/routes/1/vehicle"), "GET vehicle for route ID=1 (sub-resource)");
|
|
|
|
|
|
|
|
|
|
System.out.println();
|
|
|
|
|
}
|
|
|
|
@@ -130,12 +135,12 @@ public class ApplicationClient {
|
|
|
|
|
private static void testStatusFiltering(CloseableHttpClient client) throws Exception {
|
|
|
|
|
System.out.println("--- STATUS FILTERING ---");
|
|
|
|
|
|
|
|
|
|
execute(client, new HttpGet(BASE_URL + "/vehicles?status=AVAILABLE"), "GET vehicles with status=AVAILABLE");
|
|
|
|
|
execute(client, new HttpGet(BASE_URL + "/vehicles?status=IN_TRANSIT"), "GET vehicles with status=IN_TRANSIT");
|
|
|
|
|
execute(client, new HttpGet(BASE_URL + "/freights?status=PENDING"), "GET freights with status=PENDING");
|
|
|
|
|
execute(client, new HttpGet(BASE_URL + "/freights?status=DELIVERED"), "GET freights with status=DELIVERED");
|
|
|
|
|
execute(client, new HttpGet(BASE_URL + "/routes?status=PLANNED"), "GET routes with status=PLANNED");
|
|
|
|
|
execute(client, new HttpGet(BASE_URL + "/routes?status=COMPLETED"), "GET routes with status=COMPLETED");
|
|
|
|
|
execute(client, new HttpGet(VEHICLES_BASE_URL + "/vehicles?status=AVAILABLE"), "GET vehicles with status=AVAILABLE");
|
|
|
|
|
execute(client, new HttpGet(VEHICLES_BASE_URL + "/vehicles?status=IN_TRANSIT"), "GET vehicles with status=IN_TRANSIT");
|
|
|
|
|
execute(client, new HttpGet(FREIGHTS_BASE_URL + "/freights?status=PENDING"), "GET freights with status=PENDING");
|
|
|
|
|
execute(client, new HttpGet(FREIGHTS_BASE_URL + "/freights?status=DELIVERED"), "GET freights with status=DELIVERED");
|
|
|
|
|
execute(client, new HttpGet(ROUTES_BASE_URL + "/routes?status=PLANNED"), "GET routes with status=PLANNED");
|
|
|
|
|
execute(client, new HttpGet(ROUTES_BASE_URL + "/routes?status=COMPLETED"), "GET routes with status=COMPLETED");
|
|
|
|
|
|
|
|
|
|
System.out.println();
|
|
|
|
|
}
|
|
|
|
@@ -143,9 +148,9 @@ public class ApplicationClient {
|
|
|
|
|
private static void testErrorCases(CloseableHttpClient client) throws Exception {
|
|
|
|
|
System.out.println("--- ERROR CASES (TESTING EXCEPTION HANDLING) ---");
|
|
|
|
|
|
|
|
|
|
execute(client, new HttpGet(BASE_URL + "/vehicles/999"), "GET non-existent vehicle (404 Not Found)");
|
|
|
|
|
execute(client, new HttpGet(BASE_URL + "/freights/-1"), "GET freight with invalid ID (404 Not Found)");
|
|
|
|
|
execute(client, new HttpGet(BASE_URL + "/routes/-1"), "GET route with invalid ID (404 Not Found)");
|
|
|
|
|
execute(client, new HttpGet(VEHICLES_BASE_URL + "/vehicles/999"), "GET non-existent vehicle (404 Not Found)");
|
|
|
|
|
execute(client, new HttpGet(FREIGHTS_BASE_URL + "/freights/-1"), "GET freight with invalid ID (404 Not Found)");
|
|
|
|
|
execute(client, new HttpGet(ROUTES_BASE_URL + "/routes/-1"), "GET route with invalid ID (404 Not Found)");
|
|
|
|
|
|
|
|
|
|
System.out.println();
|
|
|
|
|
}
|
|
|
|
@@ -155,19 +160,19 @@ public class ApplicationClient {
|
|
|
|
|
|
|
|
|
|
// Test 1: Create route with valid references (should succeed)
|
|
|
|
|
System.out.println("[INTER-SERVICE TEST 1] Creating route with valid vehicle and freight references:");
|
|
|
|
|
Route validRoute = new Route(0, 0, List.of(0L), "Kyiv", "Lviv", 540.0, 8.5, Route.Status.PLANNED);
|
|
|
|
|
var validRoute = new RouteRequest(1, List.of(1L), "Kyiv", "Lviv", 540.0, 8.5, Route.Status.PLANNED);
|
|
|
|
|
var validRoutePost = createHttpRequest(validRoute, "/routes", HttpMethod.POST);
|
|
|
|
|
execute(client, validRoutePost, "POST route with valid vehicle and freights (should succeed)");
|
|
|
|
|
|
|
|
|
|
// Test 2: Create route with invalid vehicle (should fail with 404)
|
|
|
|
|
System.out.println("[INTER-SERVICE TEST 2] Creating route with INVALID vehicle ID:");
|
|
|
|
|
Route invalidVehicleRoute = new Route(0, -999, List.of(1L), "Kyiv", "Lviv", 540.0, 8.5, Route.Status.PLANNED);
|
|
|
|
|
var invalidVehicleRoute = new RouteRequest(-999, List.of(1L), "Kyiv", "Lviv", 540.0, 8.5, Route.Status.PLANNED);
|
|
|
|
|
var invalidRoutePost = createHttpRequest(invalidVehicleRoute, "/routes", HttpMethod.POST);
|
|
|
|
|
execute(client, invalidRoutePost, "POST route with invalid vehicle ID, should result in 404");
|
|
|
|
|
|
|
|
|
|
// Test 3: Create route with invalid freight (should fail with 404)
|
|
|
|
|
System.out.println("[INTER-SERVICE TEST 3] Creating route with INVALID freight ID:");
|
|
|
|
|
Route invalidFreightRoute = new Route(0, 0, List.of(-999L), "Kyiv", "Lviv", 540.0, 8.5, Route.Status.PLANNED);
|
|
|
|
|
var invalidFreightRoute = new RouteRequest(1, List.of(-999L), "Kyiv", "Lviv", 540.0, 8.5, Route.Status.PLANNED);
|
|
|
|
|
var invalidFreightPost = createHttpRequest(invalidFreightRoute, "/routes", HttpMethod.POST);
|
|
|
|
|
execute(client, invalidFreightPost, "POST route with invalid freight ID, should result in 404");
|
|
|
|
|
}
|
|
|
|
@@ -175,26 +180,32 @@ public class ApplicationClient {
|
|
|
|
|
private static <T> HttpUriRequest createHttpRequest(T entity, String path, HttpMethod method) {
|
|
|
|
|
String entityJson = jsonMapper.writeValueAsString(entity);
|
|
|
|
|
HttpEntityEnclosingRequestBase req;
|
|
|
|
|
final String baseUrl = switch(entity) {
|
|
|
|
|
case FreightRequest _ -> FREIGHTS_BASE_URL;
|
|
|
|
|
case VehicleRequest _ -> VEHICLES_BASE_URL;
|
|
|
|
|
case RouteRequest _ -> ROUTES_BASE_URL;
|
|
|
|
|
default -> throw new IllegalArgumentException();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (method == HttpMethod.GET) {
|
|
|
|
|
return new HttpGet(BASE_URL + path);
|
|
|
|
|
return new HttpGet(baseUrl + path);
|
|
|
|
|
} else if (method == HttpMethod.POST) {
|
|
|
|
|
req = new HttpPost(BASE_URL + path);
|
|
|
|
|
req = new HttpPost(baseUrl + path);
|
|
|
|
|
req.setEntity(new StringEntity(entityJson, "UTF-8"));
|
|
|
|
|
req.setHeader("Content-Type", "application/json");
|
|
|
|
|
return req;
|
|
|
|
|
} else if (method == HttpMethod.PUT) {
|
|
|
|
|
req = new HttpPut(BASE_URL + path);
|
|
|
|
|
req = new HttpPut(baseUrl + path);
|
|
|
|
|
req.setEntity(new StringEntity(entityJson, "UTF-8"));
|
|
|
|
|
req.setHeader("Content-Type", "application/json");
|
|
|
|
|
return req;
|
|
|
|
|
} else if (method == HttpMethod.PATCH) {
|
|
|
|
|
req = new HttpPatch(BASE_URL + path);
|
|
|
|
|
req = new HttpPatch(baseUrl + path);
|
|
|
|
|
req.setEntity(new StringEntity(entityJson, "UTF-8"));
|
|
|
|
|
req.setHeader("Content-Type", "application/json");
|
|
|
|
|
return req;
|
|
|
|
|
} else if (method == HttpMethod.DELETE) {
|
|
|
|
|
return new HttpDelete(BASE_URL + path);
|
|
|
|
|
return new HttpDelete(baseUrl + path);
|
|
|
|
|
} else {
|
|
|
|
|
throw new IllegalArgumentException("method is invalid");
|
|
|
|
|
}
|
|
|
|
|