mirror of
https://github.com/davidalves04/Trabalho-Pratico-SD.git
synced 2025-12-08 20:43:32 +00:00
Refactor IntersectionProcess and add unit tests
This commit is contained in:
473
main/src/test/java/IntersectionProcessTest.java
Normal file
473
main/src/test/java/IntersectionProcessTest.java
Normal file
@@ -0,0 +1,473 @@
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.net.Socket;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.Timeout;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import sd.IntersectionProcess;
|
||||
import sd.model.MessageType;
|
||||
import sd.model.Vehicle;
|
||||
import sd.model.VehicleType;
|
||||
|
||||
/**
|
||||
* Tests for IntersectionProcess - covers initialization, traffic lights,
|
||||
* vehicle transfer and network stuff
|
||||
*/
|
||||
public class IntersectionProcessTest {
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
private Path configFile;
|
||||
private IntersectionProcess intersectionProcess;
|
||||
|
||||
// setup test config before each test
|
||||
@BeforeEach
|
||||
public void setUp() throws IOException {
|
||||
// create temp config file
|
||||
configFile = tempDir.resolve("test-simulation.properties");
|
||||
|
||||
String configContent = """
|
||||
# Test Simulation Configuration
|
||||
|
||||
# Intersection Network Configuration
|
||||
intersection.Cr1.host=localhost
|
||||
intersection.Cr1.port=18001
|
||||
intersection.Cr2.host=localhost
|
||||
intersection.Cr2.port=18002
|
||||
intersection.Cr3.host=localhost
|
||||
intersection.Cr3.port=18003
|
||||
intersection.Cr4.host=localhost
|
||||
intersection.Cr4.port=18004
|
||||
intersection.Cr5.host=localhost
|
||||
intersection.Cr5.port=18005
|
||||
|
||||
# Exit Configuration
|
||||
exit.host=localhost
|
||||
exit.port=18099
|
||||
|
||||
# Dashboard Configuration
|
||||
dashboard.host=localhost
|
||||
dashboard.port=18100
|
||||
|
||||
# Traffic Light Timing (seconds)
|
||||
trafficLight.Cr1.East.greenTime=5.0
|
||||
trafficLight.Cr1.East.redTime=5.0
|
||||
trafficLight.Cr1.South.greenTime=5.0
|
||||
trafficLight.Cr1.South.redTime=5.0
|
||||
trafficLight.Cr1.West.greenTime=5.0
|
||||
trafficLight.Cr1.West.redTime=5.0
|
||||
|
||||
trafficLight.Cr2.West.greenTime=4.0
|
||||
trafficLight.Cr2.West.redTime=6.0
|
||||
trafficLight.Cr2.East.greenTime=4.0
|
||||
trafficLight.Cr2.East.redTime=6.0
|
||||
trafficLight.Cr2.South.greenTime=4.0
|
||||
trafficLight.Cr2.South.redTime=6.0
|
||||
|
||||
trafficLight.Cr3.West.greenTime=3.0
|
||||
trafficLight.Cr3.West.redTime=7.0
|
||||
trafficLight.Cr3.East.greenTime=3.0
|
||||
trafficLight.Cr3.East.redTime=7.0
|
||||
|
||||
trafficLight.Cr4.East.greenTime=6.0
|
||||
trafficLight.Cr4.East.redTime=4.0
|
||||
|
||||
trafficLight.Cr5.East.greenTime=5.0
|
||||
trafficLight.Cr5.East.redTime=5.0
|
||||
|
||||
# Vehicle Crossing Times (seconds)
|
||||
vehicle.bike.crossingTime=2.0
|
||||
vehicle.light.crossingTime=3.0
|
||||
vehicle.heavy.crossingTime=5.0
|
||||
""";
|
||||
|
||||
Files.writeString(configFile, configContent);
|
||||
}
|
||||
|
||||
// cleanup after tests
|
||||
@AfterEach
|
||||
public void tearDown() {
|
||||
if (intersectionProcess != null) {
|
||||
intersectionProcess.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== Initialization Tests ====================
|
||||
|
||||
@Test
|
||||
public void testConstructor_Success() throws IOException {
|
||||
intersectionProcess = new IntersectionProcess("Cr1", configFile.toString());
|
||||
assertNotNull(intersectionProcess);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructor_InvalidConfig() {
|
||||
Exception exception = assertThrows(IOException.class, () -> {
|
||||
new IntersectionProcess("Cr1", "non-existent-config.properties");
|
||||
});
|
||||
assertNotNull(exception);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitialize_Cr1() throws IOException {
|
||||
intersectionProcess = new IntersectionProcess("Cr1", configFile.toString());
|
||||
assertDoesNotThrow(() -> intersectionProcess.initialize());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitialize_Cr2() throws IOException {
|
||||
intersectionProcess = new IntersectionProcess("Cr2", configFile.toString());
|
||||
assertDoesNotThrow(() -> intersectionProcess.initialize());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitialize_Cr3() throws IOException {
|
||||
intersectionProcess = new IntersectionProcess("Cr3", configFile.toString());
|
||||
assertDoesNotThrow(() -> intersectionProcess.initialize());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitialize_Cr4() throws IOException {
|
||||
intersectionProcess = new IntersectionProcess("Cr4", configFile.toString());
|
||||
assertDoesNotThrow(() -> intersectionProcess.initialize());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitialize_Cr5() throws IOException {
|
||||
intersectionProcess = new IntersectionProcess("Cr5", configFile.toString());
|
||||
assertDoesNotThrow(() -> intersectionProcess.initialize());
|
||||
}
|
||||
|
||||
// traffic light creation tests
|
||||
|
||||
@Test
|
||||
public void testTrafficLightCreation_Cr1_HasCorrectDirections() throws IOException {
|
||||
intersectionProcess = new IntersectionProcess("Cr1", configFile.toString());
|
||||
intersectionProcess.initialize();
|
||||
|
||||
// cant access private fields but initialization succeds
|
||||
assertNotNull(intersectionProcess);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTrafficLightCreation_Cr3_HasCorrectDirections() throws IOException {
|
||||
intersectionProcess = new IntersectionProcess("Cr3", configFile.toString());
|
||||
intersectionProcess.initialize();
|
||||
|
||||
// Cr3 has west and south only
|
||||
assertNotNull(intersectionProcess);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTrafficLightCreation_Cr4_HasSingleDirection() throws IOException {
|
||||
intersectionProcess = new IntersectionProcess("Cr4", configFile.toString());
|
||||
intersectionProcess.initialize();
|
||||
|
||||
// Cr4 only has east direction
|
||||
assertNotNull(intersectionProcess);
|
||||
}
|
||||
|
||||
// server startup tests
|
||||
|
||||
@Test
|
||||
@Timeout(5)
|
||||
public void testServerStart_BindsToCorrectPort() throws IOException, InterruptedException {
|
||||
intersectionProcess = new IntersectionProcess("Cr1", configFile.toString());
|
||||
intersectionProcess.initialize();
|
||||
|
||||
// start server in seperate thread
|
||||
Thread serverThread = new Thread(() -> {
|
||||
try {
|
||||
intersectionProcess.start();
|
||||
} catch (IOException e) {
|
||||
// expected on shutdown
|
||||
}
|
||||
});
|
||||
serverThread.start();
|
||||
|
||||
Thread.sleep(500); // wait for server to start
|
||||
|
||||
// try connecting to check if its running
|
||||
try (Socket clientSocket = new Socket("localhost", 18001)) {
|
||||
assertTrue(clientSocket.isConnected());
|
||||
}
|
||||
|
||||
intersectionProcess.shutdown();
|
||||
serverThread.join(2000);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Timeout(5)
|
||||
public void testServerStart_MultipleIntersections() throws IOException, InterruptedException {
|
||||
// test 2 intersections on diferent ports
|
||||
IntersectionProcess cr1 = new IntersectionProcess("Cr1", configFile.toString());
|
||||
IntersectionProcess cr2 = new IntersectionProcess("Cr2", configFile.toString());
|
||||
|
||||
cr1.initialize();
|
||||
cr2.initialize();
|
||||
|
||||
Thread thread1 = new Thread(() -> {
|
||||
try { cr1.start(); } catch (IOException e) { }
|
||||
});
|
||||
|
||||
Thread thread2 = new Thread(() -> {
|
||||
try { cr2.start(); } catch (IOException e) { }
|
||||
});
|
||||
|
||||
thread1.start();
|
||||
thread2.start();
|
||||
|
||||
Thread.sleep(500);
|
||||
|
||||
// check both are running
|
||||
try (Socket socket1 = new Socket("localhost", 18001);
|
||||
Socket socket2 = new Socket("localhost", 18002)) {
|
||||
assertTrue(socket1.isConnected());
|
||||
assertTrue(socket2.isConnected());
|
||||
}
|
||||
|
||||
cr1.shutdown();
|
||||
cr2.shutdown();
|
||||
thread1.join(2000);
|
||||
thread2.join(2000);
|
||||
}
|
||||
|
||||
// vehicle transfer tests
|
||||
|
||||
@Test
|
||||
@Timeout(10)
|
||||
public void testVehicleTransfer_ReceiveVehicle() throws IOException, InterruptedException {
|
||||
// setup reciever intersection
|
||||
intersectionProcess = new IntersectionProcess("Cr2", configFile.toString());
|
||||
intersectionProcess.initialize();
|
||||
|
||||
Thread serverThread = new Thread(() -> {
|
||||
try {
|
||||
intersectionProcess.start();
|
||||
} catch (IOException e) { }
|
||||
});
|
||||
serverThread.start();
|
||||
|
||||
Thread.sleep(500);
|
||||
|
||||
// create test vehicle
|
||||
java.util.List<String> route = Arrays.asList("Cr2", "Cr3", "S");
|
||||
Vehicle vehicle = new Vehicle("V001", VehicleType.LIGHT, 0.0, route);
|
||||
|
||||
// send vehicle from Cr1 to Cr2
|
||||
try (Socket socket = new Socket("localhost", 18002)) {
|
||||
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
|
||||
|
||||
TestVehicleMessage message = new TestVehicleMessage("Cr1", "Cr2", vehicle);
|
||||
out.writeObject(message);
|
||||
out.flush();
|
||||
|
||||
Thread.sleep(1000); // wait for procesing
|
||||
}
|
||||
|
||||
intersectionProcess.shutdown();
|
||||
serverThread.join(2000);
|
||||
}
|
||||
|
||||
// routing config tests
|
||||
|
||||
@Test
|
||||
public void testRoutingConfiguration_Cr1() throws IOException {
|
||||
intersectionProcess = new IntersectionProcess("Cr1", configFile.toString());
|
||||
intersectionProcess.initialize();
|
||||
|
||||
// indirect test - if init works routing should be ok
|
||||
assertNotNull(intersectionProcess);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRoutingConfiguration_Cr5() throws IOException {
|
||||
intersectionProcess = new IntersectionProcess("Cr5", configFile.toString());
|
||||
intersectionProcess.initialize();
|
||||
|
||||
// Cr5 routes to exit
|
||||
assertNotNull(intersectionProcess);
|
||||
}
|
||||
|
||||
// shutdown tests
|
||||
|
||||
@Test
|
||||
@Timeout(5)
|
||||
public void testShutdown_GracefulTermination() throws IOException, InterruptedException {
|
||||
intersectionProcess = new IntersectionProcess("Cr1", configFile.toString());
|
||||
intersectionProcess.initialize();
|
||||
|
||||
Thread serverThread = new Thread(() -> {
|
||||
try {
|
||||
intersectionProcess.start();
|
||||
} catch (IOException e) { }
|
||||
});
|
||||
serverThread.start();
|
||||
|
||||
Thread.sleep(500);
|
||||
|
||||
// shutdown should be fast
|
||||
assertDoesNotThrow(() -> intersectionProcess.shutdown());
|
||||
|
||||
serverThread.join(2000);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Timeout(5)
|
||||
public void testShutdown_ClosesServerSocket() throws IOException, InterruptedException {
|
||||
intersectionProcess = new IntersectionProcess("Cr1", configFile.toString());
|
||||
intersectionProcess.initialize();
|
||||
|
||||
Thread serverThread = new Thread(() -> {
|
||||
try {
|
||||
intersectionProcess.start();
|
||||
} catch (IOException e) { }
|
||||
});
|
||||
serverThread.start();
|
||||
|
||||
Thread.sleep(500);
|
||||
|
||||
// verify server running
|
||||
try (Socket socket = new Socket("localhost", 18001)) {
|
||||
assertTrue(socket.isConnected());
|
||||
}
|
||||
|
||||
intersectionProcess.shutdown();
|
||||
serverThread.join(2000);
|
||||
|
||||
// after shutdown conection should fail
|
||||
Thread.sleep(500);
|
||||
Exception exception = assertThrows(IOException.class, () -> {
|
||||
Socket socket = new Socket("localhost", 18001);
|
||||
socket.close();
|
||||
});
|
||||
assertNotNull(exception);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Timeout(5)
|
||||
public void testShutdown_StopsTrafficLightThreads() throws IOException, InterruptedException {
|
||||
intersectionProcess = new IntersectionProcess("Cr1", configFile.toString());
|
||||
intersectionProcess.initialize();
|
||||
|
||||
Thread serverThread = new Thread(() -> {
|
||||
try {
|
||||
intersectionProcess.start();
|
||||
} catch (IOException e) { }
|
||||
});
|
||||
serverThread.start();
|
||||
|
||||
Thread.sleep(500);
|
||||
|
||||
int threadCountBefore = Thread.activeCount();
|
||||
|
||||
intersectionProcess.shutdown();
|
||||
serverThread.join(2000);
|
||||
|
||||
Thread.sleep(500); // wait for threads to die
|
||||
|
||||
// thread count should decrese (traffic light threads stop)
|
||||
int threadCountAfter = Thread.activeCount();
|
||||
assertTrue(threadCountAfter <= threadCountBefore);
|
||||
}
|
||||
|
||||
// integration tests
|
||||
|
||||
@Test
|
||||
@Timeout(15)
|
||||
public void testIntegration_TwoIntersectionsVehicleTransfer() throws IOException, InterruptedException {
|
||||
// setup 2 intersections
|
||||
IntersectionProcess cr1 = new IntersectionProcess("Cr1", configFile.toString());
|
||||
IntersectionProcess cr2 = new IntersectionProcess("Cr2", configFile.toString());
|
||||
|
||||
cr1.initialize();
|
||||
cr2.initialize();
|
||||
|
||||
// start both
|
||||
Thread thread1 = new Thread(() -> {
|
||||
try { cr1.start(); } catch (IOException e) { }
|
||||
});
|
||||
|
||||
Thread thread2 = new Thread(() -> {
|
||||
try { cr2.start(); } catch (IOException e) { }
|
||||
});
|
||||
|
||||
thread1.start();
|
||||
thread2.start();
|
||||
|
||||
Thread.sleep(1000); // wait for servers
|
||||
|
||||
// send vehicle to Cr1 that goes to Cr2
|
||||
java.util.List<String> route = Arrays.asList("Cr1", "Cr2", "S");
|
||||
Vehicle vehicle = new Vehicle("V001", VehicleType.LIGHT, 0.0, route);
|
||||
|
||||
try (Socket socket = new Socket("localhost", 18001)) {
|
||||
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
|
||||
|
||||
TestVehicleMessage message = new TestVehicleMessage("Entry", "Cr1", vehicle);
|
||||
out.writeObject(message);
|
||||
out.flush();
|
||||
|
||||
Thread.sleep(2000); // time for processing
|
||||
}
|
||||
|
||||
cr1.shutdown();
|
||||
cr2.shutdown();
|
||||
thread1.join(2000);
|
||||
thread2.join(2000);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMain_MissingArguments() {
|
||||
// main needs intersection ID as argument
|
||||
// cant test System.exit easily in modern java
|
||||
assertTrue(true, "Main method expects intersection ID as first argument");
|
||||
}
|
||||
|
||||
// helper class for testing vehicle messages
|
||||
private static class TestVehicleMessage implements sd.protocol.MessageProtocol {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final String sourceNode;
|
||||
private final String destinationNode;
|
||||
private final Vehicle payload;
|
||||
|
||||
public TestVehicleMessage(String sourceNode, String destinationNode, Vehicle vehicle) {
|
||||
this.sourceNode = sourceNode;
|
||||
this.destinationNode = destinationNode;
|
||||
this.payload = vehicle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MessageType getType() {
|
||||
return MessageType.VEHICLE_TRANSFER;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getPayload() {
|
||||
return payload;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSourceNode() {
|
||||
return sourceNode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDestinationNode() {
|
||||
return destinationNode;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user