mirror of
https://github.com/davidalves04/Trabalho-Pratico-SD.git
synced 2025-12-08 12:33:31 +00:00
141 lines
4.9 KiB
Java
141 lines
4.9 KiB
Java
package sd.serialization;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
import org.junit.jupiter.api.DisplayName;
|
|
import sd.model.Message;
|
|
import sd.model.Vehicle;
|
|
import sd.model.VehicleType;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
/**
|
|
* Test suite for JSON serialization.
|
|
*
|
|
* Tests JSON serialization to ensure:
|
|
* - Correct serialization and deserialization
|
|
* - Data integrity during round-trip conversion
|
|
* - Proper error handling
|
|
*/
|
|
class SerializationTest {
|
|
|
|
private MessageSerializer jsonSerializer = new JsonMessageSerializer();
|
|
|
|
private Vehicle testVehicle = new Vehicle("V001", VehicleType.LIGHT, 10.5,
|
|
Arrays.asList("Cr1", "Cr2", "Cr5", "S"));
|
|
private Message testMessage = new Message(
|
|
sd.model.MessageType.VEHICLE_TRANSFER,
|
|
"Cr1",
|
|
"Cr2",
|
|
testVehicle
|
|
);
|
|
|
|
|
|
// ===== JSON Serialization Tests =====
|
|
|
|
@Test
|
|
@DisplayName("JSON: Should serialize and deserialize Vehicle correctly")
|
|
void testJsonVehicleRoundTrip() throws SerializationException {
|
|
// Serialize
|
|
byte[] data = jsonSerializer.serialize(testVehicle);
|
|
assertNotNull(data);
|
|
assertTrue(data.length > 0);
|
|
|
|
// Print JSON for inspection
|
|
System.out.println("JSON Vehicle:");
|
|
System.out.println(new String(data));
|
|
|
|
// Deserialize
|
|
Vehicle deserialized = jsonSerializer.deserialize(data, Vehicle.class);
|
|
|
|
// Verify
|
|
assertNotNull(deserialized);
|
|
assertEquals(testVehicle.getId(), deserialized.getId());
|
|
assertEquals(testVehicle.getType(), deserialized.getType());
|
|
assertEquals(testVehicle.getEntryTime(), deserialized.getEntryTime());
|
|
assertEquals(testVehicle.getRoute(), deserialized.getRoute());
|
|
assertEquals(testVehicle.getTotalWaitingTime(), deserialized.getTotalWaitingTime());
|
|
assertEquals(testVehicle.getTotalCrossingTime(), deserialized.getTotalCrossingTime());
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("JSON: Should serialize and deserialize Message correctly")
|
|
void testJsonMessageRoundTrip() throws SerializationException {
|
|
// Serialize
|
|
byte[] data = jsonSerializer.serialize(testMessage);
|
|
assertNotNull(data);
|
|
|
|
// Print JSON for inspection
|
|
System.out.println("\nJSON Message:");
|
|
System.out.println(new String(data));
|
|
|
|
// Deserialize
|
|
Message deserialized = jsonSerializer.deserialize(data, Message.class);
|
|
|
|
// Verify
|
|
assertNotNull(deserialized);
|
|
assertEquals(testMessage.getType(), deserialized.getType());
|
|
assertEquals(testMessage.getSenderId(), deserialized.getSenderId());
|
|
assertEquals(testMessage.getDestinationId(), deserialized.getDestinationId());
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("JSON: Should throw exception on null object")
|
|
void testJsonSerializeNull() {
|
|
assertThrows(IllegalArgumentException.class, () -> {
|
|
jsonSerializer.serialize(null);
|
|
});
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("JSON: Should throw exception on null data")
|
|
void testJsonDeserializeNull() {
|
|
assertThrows(IllegalArgumentException.class, () -> {
|
|
jsonSerializer.deserialize(null, Vehicle.class);
|
|
});
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("JSON: Should throw exception on invalid JSON")
|
|
void testJsonDeserializeInvalid() {
|
|
byte[] invalidData = "{ invalid json }".getBytes();
|
|
assertThrows(SerializationException.class, () -> {
|
|
jsonSerializer.deserialize(invalidData, Vehicle.class);
|
|
});
|
|
}
|
|
|
|
@Test
|
|
@DisplayName("JSON: Should preserve data integrity for complex objects")
|
|
void testDataIntegrity() throws SerializationException {
|
|
// Create a more complex vehicle
|
|
Vehicle vehicle = new Vehicle("V999", VehicleType.HEAVY, 100.5,
|
|
Arrays.asList("Cr1", "Cr2", "Cr3", "Cr4", "Cr5", "S"));
|
|
vehicle.addWaitingTime(10.5);
|
|
vehicle.addWaitingTime(5.3);
|
|
vehicle.addCrossingTime(2.1);
|
|
vehicle.advanceRoute();
|
|
vehicle.advanceRoute();
|
|
|
|
// Serialize and deserialize
|
|
byte[] jsonData = jsonSerializer.serialize(vehicle);
|
|
Vehicle deserialized = jsonSerializer.deserialize(jsonData, Vehicle.class);
|
|
|
|
// Verify all fields match
|
|
assertEquals(vehicle.getId(), deserialized.getId());
|
|
assertEquals(vehicle.getType(), deserialized.getType());
|
|
assertEquals(vehicle.getTotalWaitingTime(), deserialized.getTotalWaitingTime());
|
|
assertEquals(vehicle.getCurrentRouteIndex(), deserialized.getCurrentRouteIndex());
|
|
}
|
|
|
|
// ===== Factory Tests =====
|
|
|
|
@Test
|
|
@DisplayName("Factory: Should create JSON serializer by default")
|
|
void testFactoryDefault() {
|
|
MessageSerializer serializer = SerializerFactory.createDefault();
|
|
assertNotNull(serializer);
|
|
assertEquals("JSON (Gson)", serializer.getName());
|
|
}
|
|
}
|