|
| 1 | +package org.opentripplanner.ext.emission.internal.csvdata.trip; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 7 | + |
| 8 | +import java.util.List; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.opentripplanner.framework.model.Gram; |
| 11 | +import org.opentripplanner.transit.model._data.TimetableRepositoryForTest; |
| 12 | +import org.opentripplanner.transit.model.framework.FeedScopedId; |
| 13 | +import org.opentripplanner.transit.model.site.StopLocation; |
| 14 | + |
| 15 | +class EmissionAggregatorTest { |
| 16 | + |
| 17 | + private static final String FEED_ID = "E"; |
| 18 | + |
| 19 | + private static final StopLocation STOP_A; |
| 20 | + private static final StopLocation STOP_B; |
| 21 | + private static final StopLocation STOP_C; |
| 22 | + private static final StopLocation STOP_D; |
| 23 | + |
| 24 | + private static final String STOP_A_ID; |
| 25 | + private static final String STOP_B_ID; |
| 26 | + private static final String STOP_C_ID; |
| 27 | + |
| 28 | + static { |
| 29 | + var builder = TimetableRepositoryForTest.of(); |
| 30 | + STOP_A = builder.stop("A").build(); |
| 31 | + STOP_B = builder.stop("B").build(); |
| 32 | + STOP_C = builder.stop("C").build(); |
| 33 | + STOP_D = builder.stop("D").build(); |
| 34 | + |
| 35 | + STOP_A_ID = STOP_A.getId().getId(); |
| 36 | + STOP_B_ID = STOP_B.getId().getId(); |
| 37 | + STOP_C_ID = STOP_C.getId().getId(); |
| 38 | + } |
| 39 | + |
| 40 | + private static final String TRIP_ID = "T:1"; |
| 41 | + private static final FeedScopedId FEED_SCOPED_TRIP_ID = new FeedScopedId(FEED_ID, TRIP_ID); |
| 42 | + |
| 43 | + private EmissionAggregator subject = new EmissionAggregator( |
| 44 | + FEED_SCOPED_TRIP_ID, |
| 45 | + List.of(STOP_A, STOP_B, STOP_C, STOP_D) |
| 46 | + ); |
| 47 | + |
| 48 | + @Test |
| 49 | + void mergeAFewRowsOk() { |
| 50 | + subject.mergeEmissionForleg(new TripLegsRow(TRIP_ID, STOP_A_ID, 1, Gram.of(3.0))); |
| 51 | + subject.mergeEmissionForleg(new TripLegsRow(TRIP_ID, STOP_B_ID, 2, Gram.of(7.0))); |
| 52 | + subject.mergeEmissionForleg(new TripLegsRow(TRIP_ID, STOP_C_ID, 3, Gram.of(10.0))); |
| 53 | + |
| 54 | + assertTrue(subject.validate()); |
| 55 | + assertEquals(List.of(), subject.listIssues()); |
| 56 | + |
| 57 | + assertEquals( |
| 58 | + "TripPatternEmission{emissions: [Emission{CO₂: 3.0g}, Emission{CO₂: 7.0g}, Emission{CO₂: 10.0g}]}", |
| 59 | + subject.build().toString() |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void mergeWithMissingLegs() { |
| 65 | + // Add same row twice, but no row for 2nd and 3rd leg |
| 66 | + subject.mergeEmissionForleg(new TripLegsRow(TRIP_ID, STOP_A_ID, 1, Gram.of(2.5))); |
| 67 | + subject.mergeEmissionForleg(new TripLegsRow(TRIP_ID, STOP_A_ID, 1, Gram.of(3.5))); |
| 68 | + |
| 69 | + assertFalse(subject.validate()); |
| 70 | + assertEquals(2, subject.listIssues().size(), () -> subject.listIssues().toString()); |
| 71 | + assertEquals( |
| 72 | + "EmissionMissingLeg(All legs in a trip(E:T:1) must have an emission value. " + |
| 73 | + "Leg number 2 and 3 does not have emissions.)", |
| 74 | + subject.listIssues().get(0).toString() |
| 75 | + ); |
| 76 | + assertEquals( |
| 77 | + "EmissionTripLegDuplicates(Warn! The emission import contains duplicate rows for the same " + |
| 78 | + "leg for trip(E:T:1). A average value is used.)", |
| 79 | + subject.listIssues().get(1).toString() |
| 80 | + ); |
| 81 | + var ex = assertThrows(IllegalStateException.class, () -> subject.build()); |
| 82 | + assertEquals("Can not build when there are issues!", ex.getMessage()); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void mergeWithStopIdMissmatch() { |
| 87 | + // Stop B and C are switched |
| 88 | + subject.mergeEmissionForleg(new TripLegsRow(TRIP_ID, STOP_A_ID, 1, Gram.of(3.0))); |
| 89 | + subject.mergeEmissionForleg(new TripLegsRow(TRIP_ID, STOP_C_ID, 2, Gram.of(7.0))); |
| 90 | + subject.mergeEmissionForleg(new TripLegsRow(TRIP_ID, STOP_B_ID, 3, Gram.of(10.0))); |
| 91 | + |
| 92 | + assertFalse(subject.validate()); |
| 93 | + assertEquals(2, subject.listIssues().size(), () -> subject.listIssues().toString()); |
| 94 | + assertEquals( |
| 95 | + "EmissionStopIdMissmatch(Emission 'from_stop_id'(C) not found in stop pattern for trip(E:T:1): " + |
| 96 | + "TripLegsRow[tripId=T:1, fromStopId=C, fromStopSequence=2, co2=7.0g])", |
| 97 | + subject.listIssues().get(0).toString() |
| 98 | + ); |
| 99 | + assertEquals( |
| 100 | + "EmissionStopIdMissmatch(Emission 'from_stop_id'(B) not found in stop pattern for trip(E:T:1): " + |
| 101 | + "TripLegsRow[tripId=T:1, fromStopId=B, fromStopSequence=3, co2=10.0g])", |
| 102 | + subject.listIssues().get(1).toString() |
| 103 | + ); |
| 104 | + var ex = assertThrows(IllegalStateException.class, () -> subject.build()); |
| 105 | + assertEquals("Can not build when there are issues!", ex.getMessage()); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + void mergeWithStopIndexOutOfBound() { |
| 110 | + subject.mergeEmissionForleg(new TripLegsRow(TRIP_ID, STOP_A_ID, -1, Gram.of(3.0))); |
| 111 | + subject.mergeEmissionForleg(new TripLegsRow(TRIP_ID, STOP_C_ID, 4, Gram.of(3.0))); |
| 112 | + |
| 113 | + assertFalse(subject.validate()); |
| 114 | + assertEquals(2, subject.listIssues().size(), () -> subject.listIssues().toString()); |
| 115 | + assertEquals( |
| 116 | + "EmissionStopSeqNr(The emission 'from_stop_sequence'(-1) is out of bounds[1 - 3]: " + |
| 117 | + "TripLegsRow[tripId=T:1, fromStopId=A, fromStopSequence=-1, co2=3.0g])", |
| 118 | + subject.listIssues().get(0).toString() |
| 119 | + ); |
| 120 | + assertEquals( |
| 121 | + "EmissionStopIdMissmatch(Emission 'from_stop_id'(C) not found in stop pattern for trip(E:T:1): " + |
| 122 | + "TripLegsRow[tripId=T:1, fromStopId=C, fromStopSequence=4, co2=3.0g])", |
| 123 | + subject.listIssues().get(1).toString() |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + void mergeWithoutCallingValidationMethod() { |
| 129 | + var ex = assertThrows(IllegalStateException.class, () -> subject.build()); |
| 130 | + assertEquals("Forgot to call validate()?", ex.getMessage()); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + void addRowsAfterValidationIsCalled() { |
| 135 | + subject.validate(); |
| 136 | + var ex = assertThrows(IllegalStateException.class, () -> |
| 137 | + subject.mergeEmissionForleg(new TripLegsRow(TRIP_ID, STOP_A_ID, 1, Gram.of(2.5))) |
| 138 | + ); |
| 139 | + assertEquals("Rows can not be added after validate() is called.", ex.getMessage()); |
| 140 | + } |
| 141 | +} |
0 commit comments