From 1f451f2de5766393c02c46a459f2c96413357419 Mon Sep 17 00:00:00 2001 From: rranjangupta Date: Tue, 21 Apr 2026 21:16:50 +0530 Subject: [PATCH 1/4] Add STI tests --- .../iluwatar/SingleTableInheritanceTest.java | 43 +++++ .../iluwatar/service/VehicleServiceTest.java | 178 ++++++++++++++++++ 2 files changed, 221 insertions(+) create mode 100644 single-table-inheritance/src/test/java/com/iluwatar/SingleTableInheritanceTest.java create mode 100644 single-table-inheritance/src/test/java/com/iluwatar/service/VehicleServiceTest.java diff --git a/single-table-inheritance/src/test/java/com/iluwatar/SingleTableInheritanceTest.java b/single-table-inheritance/src/test/java/com/iluwatar/SingleTableInheritanceTest.java new file mode 100644 index 000000000000..09c70011567c --- /dev/null +++ b/single-table-inheritance/src/test/java/com/iluwatar/SingleTableInheritanceTest.java @@ -0,0 +1,43 @@ +/* +* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). +* +* The MIT License +* Copyright © 2014-2022 Ilkka Seppälä +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ +package com.iluwatar; + + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + + +import org.junit.jupiter.api.Test; + + +/** Tests that SingleTableInheritance example application runs without errors. */ +class SingleTableInheritanceTest { + + + @Test + void shouldExecuteWithoutException() { + assertDoesNotThrow(() -> SingleTableInheritance.main(new String[] {})); + } +} + diff --git a/single-table-inheritance/src/test/java/com/iluwatar/service/VehicleServiceTest.java b/single-table-inheritance/src/test/java/com/iluwatar/service/VehicleServiceTest.java new file mode 100644 index 000000000000..48e403d9a4ad --- /dev/null +++ b/single-table-inheritance/src/test/java/com/iluwatar/service/VehicleServiceTest.java @@ -0,0 +1,178 @@ +/* +* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). +* +* The MIT License +* Copyright © 2014-2022 Ilkka Seppälä +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ +package com.iluwatar.service; + + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + + +import com.iluwatar.entity.Car; +import com.iluwatar.entity.Freighter; +import com.iluwatar.entity.Train; +import com.iluwatar.entity.Truck; +import com.iluwatar.entity.Vehicle; +import java.util.List; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + + +/** +* Tests for {@link VehicleService} exercising CRUD operations and verifying that all concrete +* Vehicle subtypes are persisted and retrieved correctly via the single-table inheritance strategy. +*/ +@SpringBootTest +class VehicleServiceTest { + + + @Autowired private VehicleService vehicleService; + + + private Vehicle savedCar; + private Vehicle savedTruck; + private Vehicle savedTrain; + private Vehicle savedFreighter; + + + @BeforeEach + void setUp() { + savedCar = vehicleService.saveVehicle(new Car("Tesla", "Model S", 4, 825)); + savedTruck = vehicleService.saveVehicle(new Truck("Ford", "F-150", 3325, 14000)); + savedTrain = vehicleService.saveVehicle(new Train("Siemens", "Velaro", 600, 8)); + savedFreighter = vehicleService.saveVehicle(new Freighter("Boeing", "747F", 100000, 8130)); + } + + + @AfterEach + void tearDown() { + vehicleService.deleteVehicle(savedCar); + vehicleService.deleteVehicle(savedTruck); + vehicleService.deleteVehicle(savedTrain); + vehicleService.deleteVehicle(savedFreighter); + } + + + @Test + void saveVehicle_shouldPersistCarWithCorrectType() { + assertTrue(savedCar.getVehicleId() > 0); + assertInstanceOf(Car.class, savedCar); + assertEquals("Tesla", savedCar.getManufacturer()); + assertEquals("Model S", savedCar.getModel()); + assertEquals(825, ((Car) savedCar).getEngineCapacity()); + } + + + @Test + void saveVehicle_shouldPersistTruckWithCorrectType() { + assertTrue(savedTruck.getVehicleId() > 0); + assertInstanceOf(Truck.class, savedTruck); + assertEquals(14000, ((Truck) savedTruck).getTowingCapacity()); + } + + + @Test + void saveVehicle_shouldPersistTrainWithCorrectType() { + assertTrue(savedTrain.getVehicleId() > 0); + assertInstanceOf(Train.class, savedTrain); + assertEquals(8, ((Train) savedTrain).getNoOfCarriages()); + } + + + @Test + void saveVehicle_shouldPersistFreighterWithCorrectType() { + assertTrue(savedFreighter.getVehicleId() > 0); + assertInstanceOf(Freighter.class, savedFreighter); + assertEquals(8130.0, ((Freighter) savedFreighter).getFlightLength()); + } + + + @Test + void getVehicle_shouldReturnCorrectVehicleById() { + Vehicle fetched = vehicleService.getVehicle(savedCar.getVehicleId()); + assertTrue(fetched != null); + assertInstanceOf(Car.class, fetched); + assertEquals(savedCar.getVehicleId(), fetched.getVehicleId()); + } + + + @Test + void getVehicle_shouldReturnNullForNonexistentId() { + Vehicle result = vehicleService.getVehicle(Integer.MAX_VALUE); + assertNull(result); + } + + + @Test + void getAllVehicles_shouldReturnAllSavedVehicles() { + List all = vehicleService.getAllVehicles(); + assertTrue(all != null); + // At minimum the 4 vehicles saved in @BeforeEach must be present + assertTrue_containsId(all, savedCar.getVehicleId()); + assertTrue_containsId(all, savedTruck.getVehicleId()); + assertTrue_containsId(all, savedTrain.getVehicleId()); + assertTrue_containsId(all, savedFreighter.getVehicleId()); + } + + + @Test + void updateVehicle_shouldPersistChanges() { + Car car = (Car) savedCar; + car.setModel("Model X"); + vehicleService.updateVehicle(car); + + + Vehicle updated = vehicleService.getVehicle(car.getVehicleId()); + assertEquals("Model X", updated.getModel()); + } + + + @Test + void deleteVehicle_shouldRemoveFromRepository() { + // Save a temporary vehicle and delete it + Vehicle tmp = vehicleService.saveVehicle(new Car("BMW", "M3", 4, 3000)); + int tmpId = tmp.getVehicleId(); + + + vehicleService.deleteVehicle(tmp); + + + assertNull(vehicleService.getVehicle(tmpId)); + } + + + // Helper: assert that a specific vehicle id exists in the list + private void assertTrue_containsId(List vehicles, int id) { + boolean found = vehicles.stream().anyMatch(v -> v.getVehicleId() == id); + if (!found) { + throw new AssertionError("Expected vehicle with id=" + id + " in list but was not found."); + } + } +} + From 3fdda6680a2cfbceff933352639e6960b1b221c5 Mon Sep 17 00:00:00 2001 From: rranjangupta Date: Tue, 21 Apr 2026 21:29:14 +0530 Subject: [PATCH 2/4] test: add delta for floating-point comparison in VehicleServiceTest --- .../src/test/java/com/iluwatar/service/VehicleServiceTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/single-table-inheritance/src/test/java/com/iluwatar/service/VehicleServiceTest.java b/single-table-inheritance/src/test/java/com/iluwatar/service/VehicleServiceTest.java index 48e403d9a4ad..2a1d080bb622 100644 --- a/single-table-inheritance/src/test/java/com/iluwatar/service/VehicleServiceTest.java +++ b/single-table-inheritance/src/test/java/com/iluwatar/service/VehicleServiceTest.java @@ -51,6 +51,7 @@ @SpringBootTest class VehicleServiceTest { + private static final double DELTA = 0.001; @Autowired private VehicleService vehicleService; @@ -109,7 +110,7 @@ void saveVehicle_shouldPersistTrainWithCorrectType() { void saveVehicle_shouldPersistFreighterWithCorrectType() { assertTrue(savedFreighter.getVehicleId() > 0); assertInstanceOf(Freighter.class, savedFreighter); - assertEquals(8130.0, ((Freighter) savedFreighter).getFlightLength()); + assertEquals(8130.0, ((Freighter) savedFreighter).getFlightLength(), DELTA); } From 0476a66b82281be14a47d90e06c773bee2252779 Mon Sep 17 00:00:00 2001 From: rranjangupta Date: Tue, 21 Apr 2026 21:34:47 +0530 Subject: [PATCH 3/4] fix: guard teardown against nulls and use assertNotNull in VehicleServiceTest Add null checks in @AfterEach tearDown() to prevent NullPointerException if setUp() partially fails. Replace assertTrue(x != null) with assertNotNull(x). --- .../com/iluwatar/service/VehicleServiceTest.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/single-table-inheritance/src/test/java/com/iluwatar/service/VehicleServiceTest.java b/single-table-inheritance/src/test/java/com/iluwatar/service/VehicleServiceTest.java index 2a1d080bb622..098d79a2b969 100644 --- a/single-table-inheritance/src/test/java/com/iluwatar/service/VehicleServiceTest.java +++ b/single-table-inheritance/src/test/java/com/iluwatar/service/VehicleServiceTest.java @@ -27,6 +27,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -73,10 +74,10 @@ void setUp() { @AfterEach void tearDown() { - vehicleService.deleteVehicle(savedCar); - vehicleService.deleteVehicle(savedTruck); - vehicleService.deleteVehicle(savedTrain); - vehicleService.deleteVehicle(savedFreighter); + if (savedCar != null) vehicleService.deleteVehicle(savedCar); + if (savedTruck != null) vehicleService.deleteVehicle(savedTruck); + if (savedTrain != null) vehicleService.deleteVehicle(savedTrain); + if (savedFreighter != null) vehicleService.deleteVehicle(savedFreighter); } @@ -117,7 +118,8 @@ void saveVehicle_shouldPersistFreighterWithCorrectType() { @Test void getVehicle_shouldReturnCorrectVehicleById() { Vehicle fetched = vehicleService.getVehicle(savedCar.getVehicleId()); - assertTrue(fetched != null); + + assertNotNull(fetched); assertInstanceOf(Car.class, fetched); assertEquals(savedCar.getVehicleId(), fetched.getVehicleId()); } @@ -133,7 +135,7 @@ void getVehicle_shouldReturnNullForNonexistentId() { @Test void getAllVehicles_shouldReturnAllSavedVehicles() { List all = vehicleService.getAllVehicles(); - assertTrue(all != null); + assertNotNull(all); // At minimum the 4 vehicles saved in @BeforeEach must be present assertTrue_containsId(all, savedCar.getVehicleId()); assertTrue_containsId(all, savedTruck.getVehicleId()); From 5c788623fd00ca8a1b37f83c35521ae82df29780 Mon Sep 17 00:00:00 2001 From: rranjangupta Date: Tue, 21 Apr 2026 22:33:22 +0530 Subject: [PATCH 4/4] style: format code in SingleTableInheritanceTest and VehicleServiceTest for consistency --- .../iluwatar/SingleTableInheritanceTest.java | 59 ++-- .../iluwatar/service/VehicleServiceTest.java | 290 ++++++++---------- 2 files changed, 162 insertions(+), 187 deletions(-) diff --git a/single-table-inheritance/src/test/java/com/iluwatar/SingleTableInheritanceTest.java b/single-table-inheritance/src/test/java/com/iluwatar/SingleTableInheritanceTest.java index 09c70011567c..8c4415cf4bfd 100644 --- a/single-table-inheritance/src/test/java/com/iluwatar/SingleTableInheritanceTest.java +++ b/single-table-inheritance/src/test/java/com/iluwatar/SingleTableInheritanceTest.java @@ -1,43 +1,38 @@ /* -* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). -* -* The MIT License -* Copyright © 2014-2022 Ilkka Seppälä -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in -* all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -* THE SOFTWARE. -*/ + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package com.iluwatar; - import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; - import org.junit.jupiter.api.Test; - /** Tests that SingleTableInheritance example application runs without errors. */ class SingleTableInheritanceTest { - - @Test - void shouldExecuteWithoutException() { - assertDoesNotThrow(() -> SingleTableInheritance.main(new String[] {})); - } + @Test + void shouldExecuteWithoutException() { + assertDoesNotThrow(() -> SingleTableInheritance.main(new String[] {})); + } } - diff --git a/single-table-inheritance/src/test/java/com/iluwatar/service/VehicleServiceTest.java b/single-table-inheritance/src/test/java/com/iluwatar/service/VehicleServiceTest.java index 098d79a2b969..2bff4b354627 100644 --- a/single-table-inheritance/src/test/java/com/iluwatar/service/VehicleServiceTest.java +++ b/single-table-inheritance/src/test/java/com/iluwatar/service/VehicleServiceTest.java @@ -1,37 +1,35 @@ /* -* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). -* -* The MIT License -* Copyright © 2014-2022 Ilkka Seppälä -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in -* all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -* THE SOFTWARE. -*/ + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package com.iluwatar.service; - import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; - import com.iluwatar.entity.Car; import com.iluwatar.entity.Freighter; import com.iluwatar.entity.Train; @@ -44,138 +42,120 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; - /** -* Tests for {@link VehicleService} exercising CRUD operations and verifying that all concrete -* Vehicle subtypes are persisted and retrieved correctly via the single-table inheritance strategy. -*/ + * Tests for {@link VehicleService} exercising CRUD operations and verifying that all concrete + * Vehicle subtypes are persisted and retrieved correctly via the single-table inheritance strategy. + */ @SpringBootTest class VehicleServiceTest { - private static final double DELTA = 0.001; - - @Autowired private VehicleService vehicleService; - - - private Vehicle savedCar; - private Vehicle savedTruck; - private Vehicle savedTrain; - private Vehicle savedFreighter; - - - @BeforeEach - void setUp() { - savedCar = vehicleService.saveVehicle(new Car("Tesla", "Model S", 4, 825)); - savedTruck = vehicleService.saveVehicle(new Truck("Ford", "F-150", 3325, 14000)); - savedTrain = vehicleService.saveVehicle(new Train("Siemens", "Velaro", 600, 8)); - savedFreighter = vehicleService.saveVehicle(new Freighter("Boeing", "747F", 100000, 8130)); - } - - - @AfterEach - void tearDown() { - if (savedCar != null) vehicleService.deleteVehicle(savedCar); - if (savedTruck != null) vehicleService.deleteVehicle(savedTruck); - if (savedTrain != null) vehicleService.deleteVehicle(savedTrain); - if (savedFreighter != null) vehicleService.deleteVehicle(savedFreighter); - } - - - @Test - void saveVehicle_shouldPersistCarWithCorrectType() { - assertTrue(savedCar.getVehicleId() > 0); - assertInstanceOf(Car.class, savedCar); - assertEquals("Tesla", savedCar.getManufacturer()); - assertEquals("Model S", savedCar.getModel()); - assertEquals(825, ((Car) savedCar).getEngineCapacity()); - } - - - @Test - void saveVehicle_shouldPersistTruckWithCorrectType() { - assertTrue(savedTruck.getVehicleId() > 0); - assertInstanceOf(Truck.class, savedTruck); - assertEquals(14000, ((Truck) savedTruck).getTowingCapacity()); - } - - - @Test - void saveVehicle_shouldPersistTrainWithCorrectType() { - assertTrue(savedTrain.getVehicleId() > 0); - assertInstanceOf(Train.class, savedTrain); - assertEquals(8, ((Train) savedTrain).getNoOfCarriages()); - } - - - @Test - void saveVehicle_shouldPersistFreighterWithCorrectType() { - assertTrue(savedFreighter.getVehicleId() > 0); - assertInstanceOf(Freighter.class, savedFreighter); - assertEquals(8130.0, ((Freighter) savedFreighter).getFlightLength(), DELTA); - } - - - @Test - void getVehicle_shouldReturnCorrectVehicleById() { - Vehicle fetched = vehicleService.getVehicle(savedCar.getVehicleId()); - - assertNotNull(fetched); - assertInstanceOf(Car.class, fetched); - assertEquals(savedCar.getVehicleId(), fetched.getVehicleId()); - } - - - @Test - void getVehicle_shouldReturnNullForNonexistentId() { - Vehicle result = vehicleService.getVehicle(Integer.MAX_VALUE); - assertNull(result); - } - - - @Test - void getAllVehicles_shouldReturnAllSavedVehicles() { - List all = vehicleService.getAllVehicles(); - assertNotNull(all); - // At minimum the 4 vehicles saved in @BeforeEach must be present - assertTrue_containsId(all, savedCar.getVehicleId()); - assertTrue_containsId(all, savedTruck.getVehicleId()); - assertTrue_containsId(all, savedTrain.getVehicleId()); - assertTrue_containsId(all, savedFreighter.getVehicleId()); - } - - - @Test - void updateVehicle_shouldPersistChanges() { - Car car = (Car) savedCar; - car.setModel("Model X"); - vehicleService.updateVehicle(car); - - - Vehicle updated = vehicleService.getVehicle(car.getVehicleId()); - assertEquals("Model X", updated.getModel()); - } - - - @Test - void deleteVehicle_shouldRemoveFromRepository() { - // Save a temporary vehicle and delete it - Vehicle tmp = vehicleService.saveVehicle(new Car("BMW", "M3", 4, 3000)); - int tmpId = tmp.getVehicleId(); - - - vehicleService.deleteVehicle(tmp); - - - assertNull(vehicleService.getVehicle(tmpId)); - } - - - // Helper: assert that a specific vehicle id exists in the list - private void assertTrue_containsId(List vehicles, int id) { - boolean found = vehicles.stream().anyMatch(v -> v.getVehicleId() == id); - if (!found) { - throw new AssertionError("Expected vehicle with id=" + id + " in list but was not found."); - } - } + private static final double DELTA = 0.001; + + @Autowired private VehicleService vehicleService; + + private Vehicle savedCar; + private Vehicle savedTruck; + private Vehicle savedTrain; + private Vehicle savedFreighter; + + @BeforeEach + void setUp() { + savedCar = vehicleService.saveVehicle(new Car("Tesla", "Model S", 4, 825)); + savedTruck = vehicleService.saveVehicle(new Truck("Ford", "F-150", 3325, 14000)); + savedTrain = vehicleService.saveVehicle(new Train("Siemens", "Velaro", 600, 8)); + savedFreighter = vehicleService.saveVehicle(new Freighter("Boeing", "747F", 100000, 8130)); + } + + @AfterEach + void tearDown() { + if (savedCar != null) vehicleService.deleteVehicle(savedCar); + if (savedTruck != null) vehicleService.deleteVehicle(savedTruck); + if (savedTrain != null) vehicleService.deleteVehicle(savedTrain); + if (savedFreighter != null) vehicleService.deleteVehicle(savedFreighter); + } + + @Test + void saveVehicle_shouldPersistCarWithCorrectType() { + assertTrue(savedCar.getVehicleId() > 0); + assertInstanceOf(Car.class, savedCar); + assertEquals("Tesla", savedCar.getManufacturer()); + assertEquals("Model S", savedCar.getModel()); + assertEquals(825, ((Car) savedCar).getEngineCapacity()); + } + + @Test + void saveVehicle_shouldPersistTruckWithCorrectType() { + assertTrue(savedTruck.getVehicleId() > 0); + assertInstanceOf(Truck.class, savedTruck); + assertEquals(14000, ((Truck) savedTruck).getTowingCapacity()); + } + + @Test + void saveVehicle_shouldPersistTrainWithCorrectType() { + assertTrue(savedTrain.getVehicleId() > 0); + assertInstanceOf(Train.class, savedTrain); + assertEquals(8, ((Train) savedTrain).getNoOfCarriages()); + } + + @Test + void saveVehicle_shouldPersistFreighterWithCorrectType() { + assertTrue(savedFreighter.getVehicleId() > 0); + assertInstanceOf(Freighter.class, savedFreighter); + assertEquals(8130.0, ((Freighter) savedFreighter).getFlightLength(), DELTA); + } + + @Test + void getVehicle_shouldReturnCorrectVehicleById() { + Vehicle fetched = vehicleService.getVehicle(savedCar.getVehicleId()); + + assertNotNull(fetched); + assertInstanceOf(Car.class, fetched); + assertEquals(savedCar.getVehicleId(), fetched.getVehicleId()); + } + + @Test + void getVehicle_shouldReturnNullForNonexistentId() { + Vehicle result = vehicleService.getVehicle(Integer.MAX_VALUE); + assertNull(result); + } + + @Test + void getAllVehicles_shouldReturnAllSavedVehicles() { + List all = vehicleService.getAllVehicles(); + assertNotNull(all); + // At minimum the 4 vehicles saved in @BeforeEach must be present + assertTrue_containsId(all, savedCar.getVehicleId()); + assertTrue_containsId(all, savedTruck.getVehicleId()); + assertTrue_containsId(all, savedTrain.getVehicleId()); + assertTrue_containsId(all, savedFreighter.getVehicleId()); + } + + @Test + void updateVehicle_shouldPersistChanges() { + Car car = (Car) savedCar; + car.setModel("Model X"); + vehicleService.updateVehicle(car); + + Vehicle updated = vehicleService.getVehicle(car.getVehicleId()); + assertEquals("Model X", updated.getModel()); + } + + @Test + void deleteVehicle_shouldRemoveFromRepository() { + // Save a temporary vehicle and delete it + Vehicle tmp = vehicleService.saveVehicle(new Car("BMW", "M3", 4, 3000)); + int tmpId = tmp.getVehicleId(); + + vehicleService.deleteVehicle(tmp); + + assertNull(vehicleService.getVehicle(tmpId)); + } + + // Helper: assert that a specific vehicle id exists in the list + private void assertTrue_containsId(List vehicles, int id) { + boolean found = vehicles.stream().anyMatch(v -> v.getVehicleId() == id); + if (!found) { + throw new AssertionError("Expected vehicle with id=" + id + " in list but was not found."); + } + } } -