mirror of
https://github.com/davidalves04/Trabalho-Pratico-SD.git
synced 2025-12-08 12:33:31 +00:00
165 lines
5.7 KiB
Java
165 lines
5.7 KiB
Java
package sd.dashboard;
|
|
|
|
import org.junit.jupiter.api.AfterEach;
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import sd.config.SimulationConfig;
|
|
import sd.model.VehicleType;
|
|
|
|
/**
|
|
* Unit tests for Dashboard Server components.
|
|
*/
|
|
class DashboardTest {
|
|
|
|
private DashboardStatistics statistics;
|
|
|
|
@BeforeEach
|
|
void setUp() {
|
|
statistics = new DashboardStatistics();
|
|
}
|
|
|
|
@AfterEach
|
|
void tearDown() {
|
|
statistics = null;
|
|
}
|
|
|
|
@Test
|
|
void testInitialStatistics() {
|
|
assertEquals(0, statistics.getTotalVehiclesGenerated(),
|
|
"Initial vehicles generated should be 0");
|
|
assertEquals(0, statistics.getTotalVehiclesCompleted(),
|
|
"Initial vehicles completed should be 0");
|
|
assertEquals(0.0, statistics.getAverageSystemTime(),
|
|
"Initial average system time should be 0.0");
|
|
assertEquals(0.0, statistics.getAverageWaitingTime(),
|
|
"Initial average waiting time should be 0.0");
|
|
}
|
|
|
|
@Test
|
|
void testVehicleCounters() {
|
|
statistics.incrementVehiclesGenerated();
|
|
assertEquals(1, statistics.getTotalVehiclesGenerated());
|
|
|
|
statistics.updateVehiclesGenerated(10);
|
|
assertEquals(10, statistics.getTotalVehiclesGenerated());
|
|
|
|
statistics.incrementVehiclesCompleted();
|
|
assertEquals(1, statistics.getTotalVehiclesCompleted());
|
|
}
|
|
|
|
@Test
|
|
void testAverageCalculations() {
|
|
// Add 3 completed vehicles with known times
|
|
statistics.updateVehiclesCompleted(3);
|
|
statistics.addSystemTime(3000); // 3000ms total
|
|
statistics.addWaitingTime(1500); // 1500ms total
|
|
|
|
assertEquals(1000.0, statistics.getAverageSystemTime(), 0.01,
|
|
"Average system time should be 1000ms");
|
|
assertEquals(500.0, statistics.getAverageWaitingTime(), 0.01,
|
|
"Average waiting time should be 500ms");
|
|
}
|
|
|
|
@Test
|
|
void testVehicleTypeStatistics() {
|
|
statistics.incrementVehicleType(VehicleType.LIGHT);
|
|
statistics.incrementVehicleType(VehicleType.LIGHT);
|
|
statistics.incrementVehicleType(VehicleType.HEAVY);
|
|
|
|
assertEquals(2, statistics.getVehicleTypeCount(VehicleType.LIGHT));
|
|
assertEquals(1, statistics.getVehicleTypeCount(VehicleType.HEAVY));
|
|
assertEquals(0, statistics.getVehicleTypeCount(VehicleType.BIKE));
|
|
}
|
|
|
|
@Test
|
|
void testIntersectionStatistics() {
|
|
statistics.updateIntersectionStats("Cr1", 10, 8, 2);
|
|
|
|
DashboardStatistics.IntersectionStats stats =
|
|
statistics.getIntersectionStats("Cr1");
|
|
|
|
assertNotNull(stats, "Intersection stats should not be null");
|
|
assertEquals("Cr1", stats.getIntersectionId());
|
|
assertEquals(10, stats.getTotalArrivals());
|
|
assertEquals(8, stats.getTotalDepartures());
|
|
assertEquals(2, stats.getCurrentQueueSize());
|
|
}
|
|
|
|
@Test
|
|
void testMultipleIntersections() {
|
|
statistics.updateIntersectionStats("Cr1", 10, 8, 2);
|
|
statistics.updateIntersectionStats("Cr2", 15, 12, 3);
|
|
statistics.updateIntersectionStats("Cr3", 5, 5, 0);
|
|
|
|
assertEquals(3, statistics.getAllIntersectionStats().size(),
|
|
"Should have 3 intersections");
|
|
}
|
|
|
|
@Test
|
|
void testStatsUpdatePayload() {
|
|
StatsUpdatePayload payload = new StatsUpdatePayload()
|
|
.setTotalVehiclesGenerated(50)
|
|
.setTotalVehiclesCompleted(20)
|
|
.setIntersectionArrivals(30)
|
|
.setIntersectionDepartures(25)
|
|
.setIntersectionQueueSize(5);
|
|
|
|
assertEquals(50, payload.getTotalVehiclesGenerated());
|
|
assertEquals(20, payload.getTotalVehiclesCompleted());
|
|
assertEquals(30, payload.getIntersectionArrivals());
|
|
assertEquals(25, payload.getIntersectionDepartures());
|
|
assertEquals(5, payload.getIntersectionQueueSize());
|
|
}
|
|
|
|
@Test
|
|
void testStatsMessage() {
|
|
StatsUpdatePayload payload = new StatsUpdatePayload()
|
|
.setIntersectionArrivals(10);
|
|
|
|
StatsMessage message = new StatsMessage("Cr1", payload);
|
|
|
|
assertEquals("Cr1", message.getSourceNode());
|
|
assertEquals("DashboardServer", message.getDestinationNode());
|
|
assertEquals(sd.model.MessageType.STATS_UPDATE, message.getType());
|
|
assertNotNull(message.getPayload());
|
|
}
|
|
|
|
@Test
|
|
void testThreadSafety() throws InterruptedException {
|
|
// Test concurrent updates
|
|
Thread t1 = new Thread(() -> {
|
|
for (int i = 0; i < 100; i++) {
|
|
statistics.incrementVehiclesGenerated();
|
|
}
|
|
});
|
|
|
|
Thread t2 = new Thread(() -> {
|
|
for (int i = 0; i < 100; i++) {
|
|
statistics.incrementVehiclesGenerated();
|
|
}
|
|
});
|
|
|
|
t1.start();
|
|
t2.start();
|
|
t1.join();
|
|
t2.join();
|
|
|
|
assertEquals(200, statistics.getTotalVehiclesGenerated(),
|
|
"Concurrent increments should total 200");
|
|
}
|
|
|
|
@Test
|
|
void testDashboardServerCreation() throws Exception {
|
|
SimulationConfig config = new SimulationConfig("simulation.properties");
|
|
DashboardServer server = new DashboardServer(config);
|
|
|
|
assertNotNull(server, "Server should be created successfully");
|
|
assertNotNull(server.getStatistics(), "Statistics should be initialized");
|
|
assertFalse(server.isRunning(), "Server should not be running initially");
|
|
}
|
|
}
|