|
| 1 | +package io.confluent.developer.models.flight; |
| 2 | + |
| 3 | +import org.apache.avro.io.BinaryDecoder; |
| 4 | +import org.apache.avro.io.BinaryEncoder; |
| 5 | +import org.apache.avro.io.DatumReader; |
| 6 | +import org.apache.avro.io.DatumWriter; |
| 7 | +import org.apache.avro.io.DecoderFactory; |
| 8 | +import org.apache.avro.io.EncoderFactory; |
| 9 | +import org.apache.avro.io.JsonDecoder; |
| 10 | +import org.apache.avro.io.JsonEncoder; |
| 11 | +import org.apache.avro.specific.SpecificDatumReader; |
| 12 | +import org.apache.avro.specific.SpecificDatumWriter; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.junit.jupiter.params.ParameterizedTest; |
| 15 | +import org.junit.jupiter.params.provider.Arguments; |
| 16 | +import org.junit.jupiter.params.provider.MethodSource; |
| 17 | + |
| 18 | +import java.io.ByteArrayOutputStream; |
| 19 | +import java.io.IOException; |
| 20 | +import java.nio.ByteBuffer; |
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | +import java.time.Instant; |
| 23 | +import java.util.stream.Stream; |
| 24 | + |
| 25 | +import static org.assertj.core.api.Assertions.assertThat; |
| 26 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 27 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 28 | + |
| 29 | +/** |
| 30 | + * Tests for the Avro-generated Flight class. |
| 31 | + */ |
| 32 | +public class FlightTest { |
| 33 | + |
| 34 | + @Test |
| 35 | + public void testFlightCreationAndGetters() { |
| 36 | + // Given |
| 37 | + String flightNumber = "AA123"; |
| 38 | + String airline = "American Airlines"; |
| 39 | + String origin = "JFK"; |
| 40 | + String destination = "LAX"; |
| 41 | + long scheduledDeparture = Instant.now().toEpochMilli(); |
| 42 | + Long actualDeparture = scheduledDeparture + 1800000; // 30 minutes later |
| 43 | + String status = "ON_TIME"; |
| 44 | + |
| 45 | + // When |
| 46 | + Flight flight = Flight.newBuilder() |
| 47 | + .setFlightNumber(flightNumber) |
| 48 | + .setAirline(airline) |
| 49 | + .setOrigin(origin) |
| 50 | + .setDestination(destination) |
| 51 | + .setScheduledDeparture(scheduledDeparture) |
| 52 | + .setActualDeparture(actualDeparture) |
| 53 | + .setStatus(status) |
| 54 | + .build(); |
| 55 | + |
| 56 | + // Then |
| 57 | + assertEquals(flightNumber, flight.getFlightNumber()); |
| 58 | + assertEquals(airline, flight.getAirline()); |
| 59 | + assertEquals(origin, flight.getOrigin()); |
| 60 | + assertEquals(destination, flight.getDestination()); |
| 61 | + assertEquals(scheduledDeparture, flight.getScheduledDeparture()); |
| 62 | + assertEquals(actualDeparture, flight.getActualDeparture()); |
| 63 | + assertEquals(status, flight.getStatus()); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testNullableActualDeparture() { |
| 68 | + // Given |
| 69 | + Flight flight = Flight.newBuilder() |
| 70 | + .setFlightNumber("AA123") |
| 71 | + .setAirline("American Airlines") |
| 72 | + .setOrigin("JFK") |
| 73 | + .setDestination("LAX") |
| 74 | + .setScheduledDeparture(Instant.now().toEpochMilli()) |
| 75 | + .setStatus("SCHEDULED") |
| 76 | + .build(); |
| 77 | + |
| 78 | + // Then |
| 79 | + assertNull(flight.getActualDeparture()); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testEquals() { |
| 84 | + // Given |
| 85 | + long now = Instant.now().toEpochMilli(); |
| 86 | + Flight flight1 = createSampleFlight("AA123", "JFK", "LAX", now, now + 1800000, "ON_TIME"); |
| 87 | + Flight flight2 = createSampleFlight("AA123", "JFK", "LAX", now, now + 1800000, "ON_TIME"); |
| 88 | + Flight flight3 = createSampleFlight("AA456", "JFK", "LAX", now, now + 1800000, "ON_TIME"); |
| 89 | + |
| 90 | + // Then |
| 91 | + assertThat(flight1).isEqualTo(flight2); |
| 92 | + assertThat(flight1).isNotEqualTo(flight3); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testHashCode() { |
| 97 | + // Given |
| 98 | + long now = Instant.now().toEpochMilli(); |
| 99 | + Flight flight1 = createSampleFlight("AA123", "JFK", "LAX", now, now + 1800000, "ON_TIME"); |
| 100 | + Flight flight2 = createSampleFlight("AA123", "JFK", "LAX", now, now + 1800000, "ON_TIME"); |
| 101 | + |
| 102 | + // Then |
| 103 | + assertThat(flight1.hashCode()).isEqualTo(flight2.hashCode()); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testToString() { |
| 108 | + // Given |
| 109 | + Flight flight = createSampleFlight("AA123", "JFK", "LAX", 1614556800000L, 1614558600000L, "ON_TIME"); |
| 110 | + |
| 111 | + // Then |
| 112 | + String toString = flight.toString(); |
| 113 | + assertThat(toString).contains("\"flightNumber\": \"AA123\""); |
| 114 | + assertThat(toString).contains("\"airline\": \"American Airlines\""); |
| 115 | + assertThat(toString).contains("\"origin\": \"JFK\""); |
| 116 | + assertThat(toString).contains("\"destination\": \"LAX\""); |
| 117 | + assertThat(toString).contains("\"scheduledDeparture\": 1614556800000"); |
| 118 | + assertThat(toString).contains("\"actualDeparture\": 1614558600000"); |
| 119 | + assertThat(toString).contains("\"status\": \"ON_TIME\""); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void testSerializationDeserialization() throws IOException { |
| 124 | + // Given |
| 125 | + Flight originalFlight = createSampleFlight("AA123", "JFK", "LAX", 1614556800000L, 1614558600000L, "ON_TIME"); |
| 126 | + |
| 127 | + // When |
| 128 | + byte[] serialized = serializeToJson(originalFlight); |
| 129 | + Flight deserializedFlight = deserializeFromJson(serialized); |
| 130 | + |
| 131 | + // Then |
| 132 | + assertThat(deserializedFlight).isEqualTo(originalFlight); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void testBinarySerializationDeserialization() throws IOException { |
| 137 | + // Given |
| 138 | + Flight originalFlight = createSampleFlight("AA123", "JFK", "LAX", 1614556800000L, 1614558600000L, "ON_TIME"); |
| 139 | + |
| 140 | + // When |
| 141 | + byte[] serialized = serializeToBinary(originalFlight); |
| 142 | + Flight deserializedFlight = deserializeFromBinary(serialized); |
| 143 | + |
| 144 | + // Then |
| 145 | + assertThat(deserializedFlight).isEqualTo(originalFlight); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void testBuiltInEncoderDecoder() throws IOException { |
| 150 | + // Given |
| 151 | + Flight originalFlight = createSampleFlight("AA123", "JFK", "LAX", 1614556800000L, 1614558600000L, "ON_TIME"); |
| 152 | + |
| 153 | + // When - Using the built-in encoder/decoder from the Avro-generated class |
| 154 | + ByteBuffer byteBuffer = Flight.getEncoder().encode(originalFlight); |
| 155 | + byte[] serialized = new byte[byteBuffer.remaining()]; |
| 156 | + byteBuffer.get(serialized); |
| 157 | + Flight deserializedFlight = Flight.getDecoder().decode(serialized); |
| 158 | + |
| 159 | + // Then |
| 160 | + assertThat(deserializedFlight).isEqualTo(originalFlight); |
| 161 | + } |
| 162 | + |
| 163 | + @ParameterizedTest |
| 164 | + @MethodSource("provideFlightStatusScenarios") |
| 165 | + public void testFlightWithDifferentStatuses(String status, Long actualDeparture) { |
| 166 | + // Given |
| 167 | + long scheduledDeparture = Instant.now().toEpochMilli(); |
| 168 | + |
| 169 | + // When |
| 170 | + Flight flight = Flight.newBuilder() |
| 171 | + .setFlightNumber("AA123") |
| 172 | + .setAirline("American Airlines") |
| 173 | + .setOrigin("JFK") |
| 174 | + .setDestination("LAX") |
| 175 | + .setScheduledDeparture(scheduledDeparture) |
| 176 | + .setActualDeparture(actualDeparture) |
| 177 | + .setStatus(status) |
| 178 | + .build(); |
| 179 | + |
| 180 | + // Then |
| 181 | + assertEquals(status, flight.getStatus()); |
| 182 | + assertEquals(actualDeparture, flight.getActualDeparture()); |
| 183 | + } |
| 184 | + |
| 185 | + private static Stream<Arguments> provideFlightStatusScenarios() { |
| 186 | + long now = Instant.now().toEpochMilli(); |
| 187 | + return Stream.of( |
| 188 | + Arguments.of("SCHEDULED", null), |
| 189 | + Arguments.of("BOARDING", null), |
| 190 | + Arguments.of("DEPARTED", now), |
| 191 | + Arguments.of("DELAYED", now + 3600000), // 1 hour delay |
| 192 | + Arguments.of("CANCELLED", null) |
| 193 | + ); |
| 194 | + } |
| 195 | + |
| 196 | + private Flight createSampleFlight(String flightNumber, String origin, String destination, |
| 197 | + long scheduledDeparture, Long actualDeparture, String status) { |
| 198 | + return Flight.newBuilder() |
| 199 | + .setFlightNumber(flightNumber) |
| 200 | + .setAirline("American Airlines") |
| 201 | + .setOrigin(origin) |
| 202 | + .setDestination(destination) |
| 203 | + .setScheduledDeparture(scheduledDeparture) |
| 204 | + .setActualDeparture(actualDeparture) |
| 205 | + .setStatus(status) |
| 206 | + .build(); |
| 207 | + } |
| 208 | + |
| 209 | + private byte[] serializeToJson(Flight flight) throws IOException { |
| 210 | + DatumWriter<Flight> writer = new SpecificDatumWriter<>(Flight.class); |
| 211 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 212 | + JsonEncoder encoder = EncoderFactory.get().jsonEncoder(Flight.getClassSchema(), out); |
| 213 | + writer.write(flight, encoder); |
| 214 | + encoder.flush(); |
| 215 | + return out.toByteArray(); |
| 216 | + } |
| 217 | + |
| 218 | + private Flight deserializeFromJson(byte[] data) throws IOException { |
| 219 | + DatumReader<Flight> reader = new SpecificDatumReader<>(Flight.class); |
| 220 | + JsonDecoder decoder = DecoderFactory.get().jsonDecoder( |
| 221 | + Flight.getClassSchema(), |
| 222 | + new String(data, StandardCharsets.UTF_8) |
| 223 | + ); |
| 224 | + return reader.read(null, decoder); |
| 225 | + } |
| 226 | + |
| 227 | + private byte[] serializeToBinary(Flight flight) throws IOException { |
| 228 | + DatumWriter<Flight> writer = new SpecificDatumWriter<>(Flight.class); |
| 229 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 230 | + BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(out, null); |
| 231 | + writer.write(flight, encoder); |
| 232 | + encoder.flush(); |
| 233 | + return out.toByteArray(); |
| 234 | + } |
| 235 | + |
| 236 | + private Flight deserializeFromBinary(byte[] data) throws IOException { |
| 237 | + DatumReader<Flight> reader = new SpecificDatumReader<>(Flight.class); |
| 238 | + BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(data, null); |
| 239 | + return reader.read(null, decoder); |
| 240 | + } |
| 241 | +} |
0 commit comments