import java.io.IOException; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; import sd.config.SimulationConfig; import sd.engine.SimulationEngine; import sd.model.Event; import sd.model.EventType; import sd.model.Intersection; import sd.model.TrafficLight; import sd.model.TrafficLightState; import sd.model.Vehicle; import sd.model.VehicleType; import sd.util.StatisticsCollector; import sd.util.VehicleGenerator; /** * Basic tests for the simulation components. */ class SimulationTest { @Test void testConfigurationLoading() throws IOException { SimulationConfig config = new SimulationConfig("src/main/resources/simulation.properties"); assertEquals(60.0, config.getSimulationDuration()); assertEquals("POISSON", config.getArrivalModel()); assertEquals(0.5, config.getArrivalRate()); assertEquals(10.0, config.getStatisticsUpdateInterval()); } @Test void testVehicleGeneration() throws IOException { SimulationConfig config = new SimulationConfig("src/main/resources/simulation.properties"); VehicleGenerator generator = new VehicleGenerator(config); Vehicle vehicle = generator.generateVehicle("TEST1", 0.0); assertNotNull(vehicle); assertEquals("TEST1", vehicle.getId()); assertNotNull(vehicle.getType()); assertNotNull(vehicle.getRoute()); assertTrue(!vehicle.getRoute().isEmpty()); } @Test void testEventOrdering() { Event e1 = new Event(5.0, EventType.VEHICLE_ARRIVAL, null, "Cr1"); Event e2 = new Event(3.0, EventType.VEHICLE_ARRIVAL, null, "Cr2"); Event e3 = new Event(7.0, EventType.TRAFFIC_LIGHT_CHANGE, null, "Cr1"); assertTrue(e2.compareTo(e1) < 0); // e2 should come before e1 assertTrue(e1.compareTo(e3) < 0); // e1 should come before e3 } @Test void testIntersectionVehicleQueue() { Intersection intersection = new Intersection("TestCr"); TrafficLight light = new TrafficLight("TestCr-N", "North", 30.0, 30.0); intersection.addTrafficLight(light); Vehicle v1 = new Vehicle("V1", VehicleType.LIGHT, 0.0, java.util.Arrays.asList("TestCr", "S")); intersection.configureRoute("S", "North"); // Advance route to next destination v1.advanceRoute(); intersection.receiveVehicle(v1); assertEquals(1, intersection.getTotalQueueSize()); assertEquals(1, intersection.getTotalVehiclesReceived()); } @Test void testTrafficLightStateChange() { TrafficLight light = new TrafficLight("Test-Light", "North", 30.0, 30.0); assertEquals(TrafficLightState.RED, light.getState()); light.changeState(TrafficLightState.GREEN); assertEquals(TrafficLightState.GREEN, light.getState()); light.changeState(TrafficLightState.RED); assertEquals(TrafficLightState.RED, light.getState()); } @Test void testSimulationEngineInitialization() throws IOException { SimulationConfig config = new SimulationConfig("src/main/resources/simulation.properties"); SimulationEngine engine = new SimulationEngine(config); engine.initialize(); assertNotNull(engine.getIntersections()); assertEquals(5, engine.getIntersections().size()); // Check that intersections have traffic lights for (Intersection intersection : engine.getIntersections().values()) { assertEquals(3, intersection.getTrafficLights().size()); // North, South, East, West } } @Test void testStatisticsCollector() throws IOException { SimulationConfig config = new SimulationConfig("src/main/resources/simulation.properties"); StatisticsCollector collector = new StatisticsCollector(config); Vehicle v1 = new Vehicle("V1", VehicleType.LIGHT, 0.0, java.util.Arrays.asList("Cr1", "Cr2", "S")); collector.recordVehicleGeneration(v1, 0.0); assertEquals(1, collector.getTotalVehiclesGenerated()); collector.recordVehicleArrival(v1, "Cr1", 1.0); collector.recordVehicleExit(v1, 10.0); assertEquals(1, collector.getTotalVehiclesCompleted()); } }