|
1 | 1 | /* |
2 | | -* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). |
3 | | -* |
4 | | -* The MIT License |
5 | | -* Copyright © 2014-2022 Ilkka Seppälä |
6 | | -* |
7 | | -* Permission is hereby granted, free of charge, to any person obtaining a copy |
8 | | -* of this software and associated documentation files (the "Software"), to deal |
9 | | -* in the Software without restriction, including without limitation the rights |
10 | | -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11 | | -* copies of the Software, and to permit persons to whom the Software is |
12 | | -* furnished to do so, subject to the following conditions: |
13 | | -* |
14 | | -* The above copyright notice and this permission notice shall be included in |
15 | | -* all copies or substantial portions of the Software. |
16 | | -* |
17 | | -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18 | | -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19 | | -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20 | | -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21 | | -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22 | | -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
23 | | -* THE SOFTWARE. |
24 | | -*/ |
| 2 | + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). |
| 3 | + * |
| 4 | + * The MIT License |
| 5 | + * Copyright © 2014-2022 Ilkka Seppälä |
| 6 | + * |
| 7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + * of this software and associated documentation files (the "Software"), to deal |
| 9 | + * in the Software without restriction, including without limitation the rights |
| 10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + * copies of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in |
| 15 | + * all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | + * THE SOFTWARE. |
| 24 | + */ |
25 | 25 | package com.iluwatar.service; |
26 | 26 |
|
27 | | - |
28 | 27 | import static org.junit.jupiter.api.Assertions.assertEquals; |
29 | 28 | import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
30 | 29 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
31 | 30 | import static org.junit.jupiter.api.Assertions.assertNull; |
32 | 31 | import static org.junit.jupiter.api.Assertions.assertTrue; |
33 | 32 |
|
34 | | - |
35 | 33 | import com.iluwatar.entity.Car; |
36 | 34 | import com.iluwatar.entity.Freighter; |
37 | 35 | import com.iluwatar.entity.Train; |
|
44 | 42 | import org.springframework.beans.factory.annotation.Autowired; |
45 | 43 | import org.springframework.boot.test.context.SpringBootTest; |
46 | 44 |
|
47 | | - |
48 | 45 | /** |
49 | | -* Tests for {@link VehicleService} exercising CRUD operations and verifying that all concrete |
50 | | -* Vehicle subtypes are persisted and retrieved correctly via the single-table inheritance strategy. |
51 | | -*/ |
| 46 | + * Tests for {@link VehicleService} exercising CRUD operations and verifying that all concrete |
| 47 | + * Vehicle subtypes are persisted and retrieved correctly via the single-table inheritance strategy. |
| 48 | + */ |
52 | 49 | @SpringBootTest |
53 | 50 | class VehicleServiceTest { |
54 | 51 |
|
55 | | - private static final double DELTA = 0.001; |
56 | | - |
57 | | - @Autowired private VehicleService vehicleService; |
58 | | - |
59 | | - |
60 | | - private Vehicle savedCar; |
61 | | - private Vehicle savedTruck; |
62 | | - private Vehicle savedTrain; |
63 | | - private Vehicle savedFreighter; |
64 | | - |
65 | | - |
66 | | - @BeforeEach |
67 | | - void setUp() { |
68 | | - savedCar = vehicleService.saveVehicle(new Car("Tesla", "Model S", 4, 825)); |
69 | | - savedTruck = vehicleService.saveVehicle(new Truck("Ford", "F-150", 3325, 14000)); |
70 | | - savedTrain = vehicleService.saveVehicle(new Train("Siemens", "Velaro", 600, 8)); |
71 | | - savedFreighter = vehicleService.saveVehicle(new Freighter("Boeing", "747F", 100000, 8130)); |
72 | | - } |
73 | | - |
74 | | - |
75 | | - @AfterEach |
76 | | - void tearDown() { |
77 | | - if (savedCar != null) vehicleService.deleteVehicle(savedCar); |
78 | | - if (savedTruck != null) vehicleService.deleteVehicle(savedTruck); |
79 | | - if (savedTrain != null) vehicleService.deleteVehicle(savedTrain); |
80 | | - if (savedFreighter != null) vehicleService.deleteVehicle(savedFreighter); |
81 | | - } |
82 | | - |
83 | | - |
84 | | - @Test |
85 | | - void saveVehicle_shouldPersistCarWithCorrectType() { |
86 | | - assertTrue(savedCar.getVehicleId() > 0); |
87 | | - assertInstanceOf(Car.class, savedCar); |
88 | | - assertEquals("Tesla", savedCar.getManufacturer()); |
89 | | - assertEquals("Model S", savedCar.getModel()); |
90 | | - assertEquals(825, ((Car) savedCar).getEngineCapacity()); |
91 | | - } |
92 | | - |
93 | | - |
94 | | - @Test |
95 | | - void saveVehicle_shouldPersistTruckWithCorrectType() { |
96 | | - assertTrue(savedTruck.getVehicleId() > 0); |
97 | | - assertInstanceOf(Truck.class, savedTruck); |
98 | | - assertEquals(14000, ((Truck) savedTruck).getTowingCapacity()); |
99 | | - } |
100 | | - |
101 | | - |
102 | | - @Test |
103 | | - void saveVehicle_shouldPersistTrainWithCorrectType() { |
104 | | - assertTrue(savedTrain.getVehicleId() > 0); |
105 | | - assertInstanceOf(Train.class, savedTrain); |
106 | | - assertEquals(8, ((Train) savedTrain).getNoOfCarriages()); |
107 | | - } |
108 | | - |
109 | | - |
110 | | - @Test |
111 | | - void saveVehicle_shouldPersistFreighterWithCorrectType() { |
112 | | - assertTrue(savedFreighter.getVehicleId() > 0); |
113 | | - assertInstanceOf(Freighter.class, savedFreighter); |
114 | | - assertEquals(8130.0, ((Freighter) savedFreighter).getFlightLength(), DELTA); |
115 | | - } |
116 | | - |
117 | | - |
118 | | - @Test |
119 | | - void getVehicle_shouldReturnCorrectVehicleById() { |
120 | | - Vehicle fetched = vehicleService.getVehicle(savedCar.getVehicleId()); |
121 | | - |
122 | | - assertNotNull(fetched); |
123 | | - assertInstanceOf(Car.class, fetched); |
124 | | - assertEquals(savedCar.getVehicleId(), fetched.getVehicleId()); |
125 | | - } |
126 | | - |
127 | | - |
128 | | - @Test |
129 | | - void getVehicle_shouldReturnNullForNonexistentId() { |
130 | | - Vehicle result = vehicleService.getVehicle(Integer.MAX_VALUE); |
131 | | - assertNull(result); |
132 | | - } |
133 | | - |
134 | | - |
135 | | - @Test |
136 | | - void getAllVehicles_shouldReturnAllSavedVehicles() { |
137 | | - List<Vehicle> all = vehicleService.getAllVehicles(); |
138 | | - assertNotNull(all); |
139 | | - // At minimum the 4 vehicles saved in @BeforeEach must be present |
140 | | - assertTrue_containsId(all, savedCar.getVehicleId()); |
141 | | - assertTrue_containsId(all, savedTruck.getVehicleId()); |
142 | | - assertTrue_containsId(all, savedTrain.getVehicleId()); |
143 | | - assertTrue_containsId(all, savedFreighter.getVehicleId()); |
144 | | - } |
145 | | - |
146 | | - |
147 | | - @Test |
148 | | - void updateVehicle_shouldPersistChanges() { |
149 | | - Car car = (Car) savedCar; |
150 | | - car.setModel("Model X"); |
151 | | - vehicleService.updateVehicle(car); |
152 | | - |
153 | | - |
154 | | - Vehicle updated = vehicleService.getVehicle(car.getVehicleId()); |
155 | | - assertEquals("Model X", updated.getModel()); |
156 | | - } |
157 | | - |
158 | | - |
159 | | - @Test |
160 | | - void deleteVehicle_shouldRemoveFromRepository() { |
161 | | - // Save a temporary vehicle and delete it |
162 | | - Vehicle tmp = vehicleService.saveVehicle(new Car("BMW", "M3", 4, 3000)); |
163 | | - int tmpId = tmp.getVehicleId(); |
164 | | - |
165 | | - |
166 | | - vehicleService.deleteVehicle(tmp); |
167 | | - |
168 | | - |
169 | | - assertNull(vehicleService.getVehicle(tmpId)); |
170 | | - } |
171 | | - |
172 | | - |
173 | | - // Helper: assert that a specific vehicle id exists in the list |
174 | | - private void assertTrue_containsId(List<Vehicle> vehicles, int id) { |
175 | | - boolean found = vehicles.stream().anyMatch(v -> v.getVehicleId() == id); |
176 | | - if (!found) { |
177 | | - throw new AssertionError("Expected vehicle with id=" + id + " in list but was not found."); |
178 | | - } |
179 | | - } |
| 52 | + private static final double DELTA = 0.001; |
| 53 | + |
| 54 | + @Autowired private VehicleService vehicleService; |
| 55 | + |
| 56 | + private Vehicle savedCar; |
| 57 | + private Vehicle savedTruck; |
| 58 | + private Vehicle savedTrain; |
| 59 | + private Vehicle savedFreighter; |
| 60 | + |
| 61 | + @BeforeEach |
| 62 | + void setUp() { |
| 63 | + savedCar = vehicleService.saveVehicle(new Car("Tesla", "Model S", 4, 825)); |
| 64 | + savedTruck = vehicleService.saveVehicle(new Truck("Ford", "F-150", 3325, 14000)); |
| 65 | + savedTrain = vehicleService.saveVehicle(new Train("Siemens", "Velaro", 600, 8)); |
| 66 | + savedFreighter = vehicleService.saveVehicle(new Freighter("Boeing", "747F", 100000, 8130)); |
| 67 | + } |
| 68 | + |
| 69 | + @AfterEach |
| 70 | + void tearDown() { |
| 71 | + if (savedCar != null) vehicleService.deleteVehicle(savedCar); |
| 72 | + if (savedTruck != null) vehicleService.deleteVehicle(savedTruck); |
| 73 | + if (savedTrain != null) vehicleService.deleteVehicle(savedTrain); |
| 74 | + if (savedFreighter != null) vehicleService.deleteVehicle(savedFreighter); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void saveVehicle_shouldPersistCarWithCorrectType() { |
| 79 | + assertTrue(savedCar.getVehicleId() > 0); |
| 80 | + assertInstanceOf(Car.class, savedCar); |
| 81 | + assertEquals("Tesla", savedCar.getManufacturer()); |
| 82 | + assertEquals("Model S", savedCar.getModel()); |
| 83 | + assertEquals(825, ((Car) savedCar).getEngineCapacity()); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void saveVehicle_shouldPersistTruckWithCorrectType() { |
| 88 | + assertTrue(savedTruck.getVehicleId() > 0); |
| 89 | + assertInstanceOf(Truck.class, savedTruck); |
| 90 | + assertEquals(14000, ((Truck) savedTruck).getTowingCapacity()); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void saveVehicle_shouldPersistTrainWithCorrectType() { |
| 95 | + assertTrue(savedTrain.getVehicleId() > 0); |
| 96 | + assertInstanceOf(Train.class, savedTrain); |
| 97 | + assertEquals(8, ((Train) savedTrain).getNoOfCarriages()); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + void saveVehicle_shouldPersistFreighterWithCorrectType() { |
| 102 | + assertTrue(savedFreighter.getVehicleId() > 0); |
| 103 | + assertInstanceOf(Freighter.class, savedFreighter); |
| 104 | + assertEquals(8130.0, ((Freighter) savedFreighter).getFlightLength(), DELTA); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + void getVehicle_shouldReturnCorrectVehicleById() { |
| 109 | + Vehicle fetched = vehicleService.getVehicle(savedCar.getVehicleId()); |
| 110 | + |
| 111 | + assertNotNull(fetched); |
| 112 | + assertInstanceOf(Car.class, fetched); |
| 113 | + assertEquals(savedCar.getVehicleId(), fetched.getVehicleId()); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + void getVehicle_shouldReturnNullForNonexistentId() { |
| 118 | + Vehicle result = vehicleService.getVehicle(Integer.MAX_VALUE); |
| 119 | + assertNull(result); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + void getAllVehicles_shouldReturnAllSavedVehicles() { |
| 124 | + List<Vehicle> all = vehicleService.getAllVehicles(); |
| 125 | + assertNotNull(all); |
| 126 | + // At minimum the 4 vehicles saved in @BeforeEach must be present |
| 127 | + assertTrue_containsId(all, savedCar.getVehicleId()); |
| 128 | + assertTrue_containsId(all, savedTruck.getVehicleId()); |
| 129 | + assertTrue_containsId(all, savedTrain.getVehicleId()); |
| 130 | + assertTrue_containsId(all, savedFreighter.getVehicleId()); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + void updateVehicle_shouldPersistChanges() { |
| 135 | + Car car = (Car) savedCar; |
| 136 | + car.setModel("Model X"); |
| 137 | + vehicleService.updateVehicle(car); |
| 138 | + |
| 139 | + Vehicle updated = vehicleService.getVehicle(car.getVehicleId()); |
| 140 | + assertEquals("Model X", updated.getModel()); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + void deleteVehicle_shouldRemoveFromRepository() { |
| 145 | + // Save a temporary vehicle and delete it |
| 146 | + Vehicle tmp = vehicleService.saveVehicle(new Car("BMW", "M3", 4, 3000)); |
| 147 | + int tmpId = tmp.getVehicleId(); |
| 148 | + |
| 149 | + vehicleService.deleteVehicle(tmp); |
| 150 | + |
| 151 | + assertNull(vehicleService.getVehicle(tmpId)); |
| 152 | + } |
| 153 | + |
| 154 | + // Helper: assert that a specific vehicle id exists in the list |
| 155 | + private void assertTrue_containsId(List<Vehicle> vehicles, int id) { |
| 156 | + boolean found = vehicles.stream().anyMatch(v -> v.getVehicleId() == id); |
| 157 | + if (!found) { |
| 158 | + throw new AssertionError("Expected vehicle with id=" + id + " in list but was not found."); |
| 159 | + } |
| 160 | + } |
180 | 161 | } |
181 | | - |
0 commit comments