Skip to content

Commit 1f451f2

Browse files
committed
Add STI tests
1 parent 2d39fe7 commit 1f451f2

2 files changed

Lines changed: 221 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
*/
25+
package com.iluwatar;
26+
27+
28+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
29+
30+
31+
import org.junit.jupiter.api.Test;
32+
33+
34+
/** Tests that SingleTableInheritance example application runs without errors. */
35+
class SingleTableInheritanceTest {
36+
37+
38+
@Test
39+
void shouldExecuteWithoutException() {
40+
assertDoesNotThrow(() -> SingleTableInheritance.main(new String[] {}));
41+
}
42+
}
43+
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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+
*/
25+
package com.iluwatar.service;
26+
27+
28+
import static org.junit.jupiter.api.Assertions.assertEquals;
29+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
30+
import static org.junit.jupiter.api.Assertions.assertNull;
31+
import static org.junit.jupiter.api.Assertions.assertTrue;
32+
33+
34+
import com.iluwatar.entity.Car;
35+
import com.iluwatar.entity.Freighter;
36+
import com.iluwatar.entity.Train;
37+
import com.iluwatar.entity.Truck;
38+
import com.iluwatar.entity.Vehicle;
39+
import java.util.List;
40+
import org.junit.jupiter.api.AfterEach;
41+
import org.junit.jupiter.api.BeforeEach;
42+
import org.junit.jupiter.api.Test;
43+
import org.springframework.beans.factory.annotation.Autowired;
44+
import org.springframework.boot.test.context.SpringBootTest;
45+
46+
47+
/**
48+
* Tests for {@link VehicleService} exercising CRUD operations and verifying that all concrete
49+
* Vehicle subtypes are persisted and retrieved correctly via the single-table inheritance strategy.
50+
*/
51+
@SpringBootTest
52+
class VehicleServiceTest {
53+
54+
55+
@Autowired private VehicleService vehicleService;
56+
57+
58+
private Vehicle savedCar;
59+
private Vehicle savedTruck;
60+
private Vehicle savedTrain;
61+
private Vehicle savedFreighter;
62+
63+
64+
@BeforeEach
65+
void setUp() {
66+
savedCar = vehicleService.saveVehicle(new Car("Tesla", "Model S", 4, 825));
67+
savedTruck = vehicleService.saveVehicle(new Truck("Ford", "F-150", 3325, 14000));
68+
savedTrain = vehicleService.saveVehicle(new Train("Siemens", "Velaro", 600, 8));
69+
savedFreighter = vehicleService.saveVehicle(new Freighter("Boeing", "747F", 100000, 8130));
70+
}
71+
72+
73+
@AfterEach
74+
void tearDown() {
75+
vehicleService.deleteVehicle(savedCar);
76+
vehicleService.deleteVehicle(savedTruck);
77+
vehicleService.deleteVehicle(savedTrain);
78+
vehicleService.deleteVehicle(savedFreighter);
79+
}
80+
81+
82+
@Test
83+
void saveVehicle_shouldPersistCarWithCorrectType() {
84+
assertTrue(savedCar.getVehicleId() > 0);
85+
assertInstanceOf(Car.class, savedCar);
86+
assertEquals("Tesla", savedCar.getManufacturer());
87+
assertEquals("Model S", savedCar.getModel());
88+
assertEquals(825, ((Car) savedCar).getEngineCapacity());
89+
}
90+
91+
92+
@Test
93+
void saveVehicle_shouldPersistTruckWithCorrectType() {
94+
assertTrue(savedTruck.getVehicleId() > 0);
95+
assertInstanceOf(Truck.class, savedTruck);
96+
assertEquals(14000, ((Truck) savedTruck).getTowingCapacity());
97+
}
98+
99+
100+
@Test
101+
void saveVehicle_shouldPersistTrainWithCorrectType() {
102+
assertTrue(savedTrain.getVehicleId() > 0);
103+
assertInstanceOf(Train.class, savedTrain);
104+
assertEquals(8, ((Train) savedTrain).getNoOfCarriages());
105+
}
106+
107+
108+
@Test
109+
void saveVehicle_shouldPersistFreighterWithCorrectType() {
110+
assertTrue(savedFreighter.getVehicleId() > 0);
111+
assertInstanceOf(Freighter.class, savedFreighter);
112+
assertEquals(8130.0, ((Freighter) savedFreighter).getFlightLength());
113+
}
114+
115+
116+
@Test
117+
void getVehicle_shouldReturnCorrectVehicleById() {
118+
Vehicle fetched = vehicleService.getVehicle(savedCar.getVehicleId());
119+
assertTrue(fetched != null);
120+
assertInstanceOf(Car.class, fetched);
121+
assertEquals(savedCar.getVehicleId(), fetched.getVehicleId());
122+
}
123+
124+
125+
@Test
126+
void getVehicle_shouldReturnNullForNonexistentId() {
127+
Vehicle result = vehicleService.getVehicle(Integer.MAX_VALUE);
128+
assertNull(result);
129+
}
130+
131+
132+
@Test
133+
void getAllVehicles_shouldReturnAllSavedVehicles() {
134+
List<Vehicle> all = vehicleService.getAllVehicles();
135+
assertTrue(all != null);
136+
// At minimum the 4 vehicles saved in @BeforeEach must be present
137+
assertTrue_containsId(all, savedCar.getVehicleId());
138+
assertTrue_containsId(all, savedTruck.getVehicleId());
139+
assertTrue_containsId(all, savedTrain.getVehicleId());
140+
assertTrue_containsId(all, savedFreighter.getVehicleId());
141+
}
142+
143+
144+
@Test
145+
void updateVehicle_shouldPersistChanges() {
146+
Car car = (Car) savedCar;
147+
car.setModel("Model X");
148+
vehicleService.updateVehicle(car);
149+
150+
151+
Vehicle updated = vehicleService.getVehicle(car.getVehicleId());
152+
assertEquals("Model X", updated.getModel());
153+
}
154+
155+
156+
@Test
157+
void deleteVehicle_shouldRemoveFromRepository() {
158+
// Save a temporary vehicle and delete it
159+
Vehicle tmp = vehicleService.saveVehicle(new Car("BMW", "M3", 4, 3000));
160+
int tmpId = tmp.getVehicleId();
161+
162+
163+
vehicleService.deleteVehicle(tmp);
164+
165+
166+
assertNull(vehicleService.getVehicle(tmpId));
167+
}
168+
169+
170+
// Helper: assert that a specific vehicle id exists in the list
171+
private void assertTrue_containsId(List<Vehicle> vehicles, int id) {
172+
boolean found = vehicles.stream().anyMatch(v -> v.getVehicleId() == id);
173+
if (!found) {
174+
throw new AssertionError("Expected vehicle with id=" + id + " in list but was not found.");
175+
}
176+
}
177+
}
178+

0 commit comments

Comments
 (0)