fixes for 3rd practical

This commit is contained in:
2025-12-25 05:12:04 +02:00
parent d203544a6f
commit a50b754881
8 changed files with 149 additions and 75 deletions

View File

@@ -7,6 +7,7 @@ import io.swagger.v3.oas.annotations.tags.Tag;
import org.jspecify.annotations.Nullable;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import ua.com.dxrkness.dto.FreightRequest;
import ua.com.dxrkness.model.Freight;
import ua.com.dxrkness.service.FreightService;
@@ -74,8 +75,8 @@ public class FreightController {
description = "Invalid freight data (e.g., weight exceeds vehicle capacity)"
)
@PostMapping
public Freight add(@RequestBody Freight newFreight) {
return service.add(newFreight);
public Freight add(@RequestBody FreightRequest newFreight) {
return service.add(newFreight.toEntity());
}
@Operation(
@@ -91,8 +92,8 @@ public class FreightController {
description = "Invalid input"
)
@PutMapping("/{id}")
public Freight update(@PathVariable("id") long id, @RequestBody Freight newFreight) {
return service.update(id, newFreight);
public Freight update(@PathVariable("id") long id, @RequestBody FreightRequest newFreight) {
return service.update(id, newFreight.toEntity());
}
@Operation(
@@ -108,8 +109,8 @@ public class FreightController {
description = "Invalid input"
)
@PatchMapping("/{id}")
public Freight updatePatch(@PathVariable("id") long id, @RequestBody Freight newFreight) {
return service.update(id, newFreight);
public Freight updatePatch(@PathVariable("id") long id, @RequestBody FreightRequest newFreight) {
return service.update(id, newFreight.toEntity());
}
@Operation(

View File

@@ -0,0 +1,19 @@
package ua.com.dxrkness.dto;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import ua.com.dxrkness.model.Freight;
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
@tools.jackson.databind.annotation.JsonNaming(tools.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy.class)
public record FreightRequest(
String name,
String description,
int weightKg,
Freight.Dimensions dimensions,
Freight.Status status
) {
public Freight toEntity() {
return new Freight(0, name, description, weightKg, dimensions, status);
}
}

View File

@@ -0,0 +1,21 @@
package ua.com.dxrkness.dto;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import ua.com.dxrkness.model.Route;
import java.util.List;
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
@tools.jackson.databind.annotation.JsonNaming(tools.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy.class)
public record RouteRequest(long vehicleId,
List<Long> freightId,
String startLocation,
String endLocation,
Double distanceKm,
Double estimatedDurationHours,
Route.Status status) {
public Route toEntity() {
return new Route(0, vehicleId, freightId, startLocation, endLocation, distanceKm, estimatedDurationHours, status);
}
}

View File

@@ -0,0 +1,20 @@
package ua.com.dxrkness.dto;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import ua.com.dxrkness.model.Vehicle;
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
@tools.jackson.databind.annotation.JsonNaming(tools.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy.class)
public record VehicleRequest(
String brand,
String model,
String licensePlate,
int year,
int capacityKg,
Vehicle.Status status
) {
public Vehicle toEntity() {
return new Vehicle(0, brand, model, licensePlate, year, capacityKg, status);
}
}

View File

@@ -10,6 +10,7 @@ import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import ua.com.dxrkness.client.FreightClient;
import ua.com.dxrkness.client.VehicleClient;
import ua.com.dxrkness.dto.RouteRequest;
import ua.com.dxrkness.model.Freight;
import ua.com.dxrkness.model.Route;
import ua.com.dxrkness.model.Vehicle;
@@ -153,8 +154,8 @@ public class RouteController {
description = "Route object to be created. Must include valid vehicleId and freightId list.",
required = true
)
@RequestBody Route newRoute) {
return routeService.add(newRoute);
@RequestBody RouteRequest newRoute) {
return routeService.add(newRoute.toEntity());
}
@Operation(
@@ -177,8 +178,8 @@ public class RouteController {
description = "Updated route object",
required = true
)
@RequestBody Route newRoute) {
return routeService.update(id, newRoute);
@RequestBody RouteRequest newRoute) {
return routeService.update(id, newRoute.toEntity());
}
@Operation(
@@ -201,8 +202,8 @@ public class RouteController {
description = "Route object with fields to update",
required = true
)
@RequestBody Route newRoute) {
return routeService.update(id, newRoute);
@RequestBody RouteRequest newRoute) {
return routeService.update(id, newRoute.toEntity());
}

View File

@@ -52,7 +52,7 @@ public final class RouteService {
route.distanceKm(),
route.estimatedDurationHours(),
route.status());
repo.add(route);
repo.add(newRoute);
return newRoute;
}

View File

@@ -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");
}

View File

@@ -8,6 +8,7 @@ import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import ua.com.dxrkness.dto.VehicleRequest;
import ua.com.dxrkness.model.Vehicle;
import ua.com.dxrkness.service.VehicleService;
@@ -68,8 +69,8 @@ public class VehicleController {
description = "Vehicle object to be created",
required = true
)
@RequestBody Vehicle newVehicle) {
return service.add(newVehicle);
@RequestBody VehicleRequest newVehicle) {
return service.add(newVehicle.toEntity());
}
@Operation(
@@ -86,8 +87,8 @@ public class VehicleController {
description = "Updated vehicle object",
required = true
)
@RequestBody Vehicle newVehicle) {
return service.update(id, newVehicle);
@RequestBody VehicleRequest newVehicle) {
return service.update(id, newVehicle.toEntity());
}
@Operation(
@@ -104,8 +105,8 @@ public class VehicleController {
description = "Vehicle object with fields to update",
required = true
)
@RequestBody Vehicle newVehicle) {
return service.update(id, newVehicle);
@RequestBody VehicleRequest newVehicle) {
return service.update(id, newVehicle.toEntity());
}