Skip to content

Commit

Permalink
Configure formatting for sandbox code
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed Jan 24, 2025
1 parent cc2fe88 commit ed6cdce
Show file tree
Hide file tree
Showing 24 changed files with 277 additions and 212 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,12 @@ void calculateFareThatIncludesNoFreeTransfers() {
);
calculateFare(rides, FareType.youth, Money.ZERO_USD);
// We don't get any fares for the skagit transit leg below here because they don't accept ORCA (electronic)
calculateFare(rides, FareType.electronicSpecial, ONE_DOLLAR.plus(ONE_DOLLAR).plus(DEFAULT_TEST_RIDE_PRICE.times(2)));
calculateFare(
rides,
FareType.electronicRegular,
DEFAULT_TEST_RIDE_PRICE.times(4)
FareType.electronicSpecial,
ONE_DOLLAR.plus(ONE_DOLLAR).plus(DEFAULT_TEST_RIDE_PRICE.times(2))
);
calculateFare(rides, FareType.electronicRegular, DEFAULT_TEST_RIDE_PRICE.times(4));
calculateFare(
rides,
FareType.electronicSenior,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,4 @@ public static StopTime regularStop(int arrivalTime, int departureTime) {
stopTime.setTrip(TRIP);
return stopTime;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ private static List<List<StopTime>> isNotScheduledDeviatedTripCases() {
areaWithContinuousStopping("10:40"),
regularStop("10:50", "11:00")
),
List.of(
regularStop("10:10"),
regularStop("10:20")
)
List.of(regularStop("10:10"), regularStop("10:20"))
);
}

Expand All @@ -57,6 +54,4 @@ private static List<List<StopTime>> isNotScheduledDeviatedTripCases() {
void isNotScheduledDeviatedTrip(List<StopTime> stopTimes) {
assertFalse(ScheduledDeviatedTrip.isScheduledDeviatedFlexTrip(stopTimes));
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,20 @@ class IsUnscheduledTrip {

private static final StopTime SCHEDULED_STOP = FlexStopTimesForTest.regularStop("10:00");
private static final StopTime UNSCHEDULED_STOP = FlexStopTimesForTest.area("10:10", "10:20");
private static final StopTime CONTINUOUS_PICKUP_STOP = FlexStopTimesForTest.regularStopWithContinuousPickup("10:30");
private static final StopTime CONTINUOUS_DROP_OFF_STOP = FlexStopTimesForTest.regularStopWithContinuousDropOff("10:40");
private static final StopTime CONTINUOUS_PICKUP_STOP = FlexStopTimesForTest.regularStopWithContinuousPickup(
"10:30"
);
private static final StopTime CONTINUOUS_DROP_OFF_STOP = FlexStopTimesForTest.regularStopWithContinuousDropOff(
"10:40"
);

// disallowed by the GTFS spec
private static final StopTime FLEX_AND_CONTINUOUS_PICKUP_STOP = FlexStopTimesForTest.areaWithContinuousPickup("10:50");
private static final StopTime FLEX_AND_CONTINUOUS_DROP_OFF_STOP = FlexStopTimesForTest.areaWithContinuousDropOff("11:00");
private static final StopTime FLEX_AND_CONTINUOUS_PICKUP_STOP = FlexStopTimesForTest.areaWithContinuousPickup(
"10:50"
);
private static final StopTime FLEX_AND_CONTINUOUS_DROP_OFF_STOP = FlexStopTimesForTest.areaWithContinuousDropOff(
"11:00"
);

static List<List<StopTime>> notUnscheduled() {
return List.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,4 @@ private <D extends Enum<?>, A extends Enum<?>> void verifyExplicitMatch(
}
assertTrue(rest.isEmpty());
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.opentripplanner.ext.siri.updater.azure;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.Mockito.*;

import com.azure.core.util.ExpandableStringEnum;
Expand Down Expand Up @@ -56,21 +56,22 @@ public void setUp() throws Exception {
when(mockConfig.isFuzzyTripMatching()).thenReturn(true);

// Create a spy on AbstractAzureSiriUpdater with the mock configuration
updater = spy(new AbstractAzureSiriUpdater(mockConfig) {
@Override
protected void messageConsumer(ServiceBusReceivedMessageContext messageContext) {
}

@Override
protected void errorConsumer(ServiceBusErrorContext errorContext) {
}

@Override
protected void initializeData(String url,
Consumer<ServiceBusReceivedMessageContext> consumer
) throws URISyntaxException {
}
});
updater =
spy(
new AbstractAzureSiriUpdater(mockConfig) {
@Override
protected void messageConsumer(ServiceBusReceivedMessageContext messageContext) {}

@Override
protected void errorConsumer(ServiceBusErrorContext errorContext) {}

@Override
protected void initializeData(
String url,
Consumer<ServiceBusReceivedMessageContext> consumer
) throws URISyntaxException {}
}
);

task = mock(AbstractAzureSiriUpdater.CheckedRunnable.class);
}
Expand All @@ -81,8 +82,8 @@ protected void initializeData(String url,
*/
@Test
void testExecuteWithRetry_FullBackoffSequence() throws Throwable {
final int totalRunCalls = 10; // 9 failures + 1 success
final int totalSleepCalls = 9; // 9 retries
final int totalRunCalls = 10; // 9 failures + 1 success
final int totalSleepCalls = 9; // 9 retries

doNothing().when(updater).sleep(anyInt());

Expand All @@ -97,7 +98,8 @@ void testExecuteWithRetry_FullBackoffSequence() throws Throwable {
.doThrow(createServiceBusException(ServiceBusFailureReason.SERVICE_BUSY))
.doThrow(createServiceBusException(ServiceBusFailureReason.SERVICE_BUSY))
.doNothing() // Succeed on the 10th attempt
.when(task).run();
.when(task)
.run();

updater.executeWithRetry(task, "Test Task");

Expand Down Expand Up @@ -126,14 +128,20 @@ void testExecuteWithRetry_FullBackoffSequence() throws Throwable {
public void testExecuteWithRetry_NonRetryableException() throws Throwable {
doNothing().when(updater).sleep(anyInt());

ServiceBusException serviceBusException = createServiceBusException(ServiceBusFailureReason.MESSAGE_SIZE_EXCEEDED);
ServiceBusException serviceBusException = createServiceBusException(
ServiceBusFailureReason.MESSAGE_SIZE_EXCEEDED
);

doThrow(serviceBusException).when(task).run();

try {
updater.executeWithRetry(task, "Test Task");
} catch (ServiceBusException e) {
assertEquals(ServiceBusFailureReason.MESSAGE_SIZE_EXCEEDED, e.getReason(), "Exception should have reason MESSAGE_SIZE_EXCEEDED");
assertEquals(
ServiceBusFailureReason.MESSAGE_SIZE_EXCEEDED,
e.getReason(),
"Exception should have reason MESSAGE_SIZE_EXCEEDED"
);
}

verify(updater, never()).sleep(anyInt());
Expand All @@ -153,12 +161,15 @@ public void testExecuteWithRetry_MultipleRetriesThenSuccess() throws Throwable {
.doThrow(createServiceBusException(ServiceBusFailureReason.SERVICE_BUSY))
.doThrow(createServiceBusException(ServiceBusFailureReason.SERVICE_BUSY))
.doNothing()
.when(task).run();
.when(task)
.run();

doAnswer(invocation -> {
latch.countDown();
return null;
}).when(updater).sleep(anyInt());
latch.countDown();
return null;
})
.when(updater)
.sleep(anyInt());

updater.executeWithRetry(task, "Test Task");

Expand All @@ -169,11 +180,14 @@ public void testExecuteWithRetry_MultipleRetriesThenSuccess() throws Throwable {
verify(updater, times(retriesBeforeSuccess)).sleep(sleepCaptor.capture());

var sleepDurations = sleepCaptor.getAllValues();
long[] expectedBackoffSequence = {1000, 2000, 4000};
long[] expectedBackoffSequence = { 1000, 2000, 4000 };

for (int i = 0; i < expectedBackoffSequence.length; i++) {
assertEquals(expectedBackoffSequence[i], Long.valueOf(sleepDurations.get(i)),
"Backoff duration mismatch at retry " + (i + 1));
assertEquals(
expectedBackoffSequence[i],
Long.valueOf(sleepDurations.get(i)),
"Backoff duration mismatch at retry " + (i + 1)
);
}

verify(task, times(retriesBeforeSuccess + 1)).run();
Expand Down Expand Up @@ -205,14 +219,17 @@ public void testExecuteWithRetry_OneRetryThenSuccess() throws Throwable {

doThrow(createServiceBusException(ServiceBusFailureReason.SERVICE_BUSY))
.doNothing()
.when(task).run();
.when(task)
.run();

doAnswer(invocation -> {
if (invocation.getArgument(0).equals(1000)) {
latch.countDown();
}
return null;
}).when(updater).sleep(anyInt());
if (invocation.getArgument(0).equals(1000)) {
latch.countDown();
}
return null;
})
.when(updater)
.sleep(anyInt());

updater.executeWithRetry(task, "Test Task");

Expand All @@ -232,10 +249,17 @@ public void testExecuteWithRetry_OneRetryThenSuccess() throws Throwable {
@ParameterizedTest(name = "shouldRetry with reason {0} should return {1}")
@MethodSource("provideServiceBusFailureReasons")
@DisplayName("Test shouldRetry for all ServiceBusFailureReason values")
void testShouldRetry_ServiceBusFailureReasons(ServiceBusFailureReason reason, boolean expectedRetry) throws Exception {
void testShouldRetry_ServiceBusFailureReasons(
ServiceBusFailureReason reason,
boolean expectedRetry
) throws Exception {
ServiceBusException serviceBusException = createServiceBusException(reason);
boolean result = updater.shouldRetry(serviceBusException);
assertEquals(expectedRetry, result, "shouldRetry should return " + expectedRetry + " for reason " + reason);
assertEquals(
expectedRetry,
result,
"shouldRetry should return " + expectedRetry + " for reason " + reason
);
}

/**
Expand All @@ -258,7 +282,11 @@ public void testShouldRetry_NonServiceBusException() {
public void testShouldRetry_CoversAllReasons() {
long enumCount = getExpandableStringEnumValues(ServiceBusFailureReason.class).size();
long testCaseCount = provideServiceBusFailureReasons().count();
assertEquals(enumCount, testCaseCount, "All ServiceBusFailureReason values should be covered by tests.");
assertEquals(
enumCount,
testCaseCount,
"All ServiceBusFailureReason values should be covered by tests."
);
}

@Test
Expand All @@ -268,15 +296,24 @@ void testExecuteWithRetry_InterruptedException() throws Throwable {

doThrow(createServiceBusException(ServiceBusFailureReason.SERVICE_BUSY))
.doThrow(new InterruptedException("Sleep interrupted"))
.when(task).run();
.when(task)
.run();

doNothing().when(updater).sleep(1000);

InterruptedException thrownException = assertThrows(InterruptedException.class, () -> {
updater.executeWithRetry(task, "Test Task");
}, "Expected executeWithRetry to throw InterruptedException");
InterruptedException thrownException = assertThrows(
InterruptedException.class,
() -> {
updater.executeWithRetry(task, "Test Task");
},
"Expected executeWithRetry to throw InterruptedException"
);

assertEquals("Sleep interrupted", thrownException.getMessage(), "Exception message should match");
assertEquals(
"Sleep interrupted",
thrownException.getMessage(),
"Exception message should match"
);
verify(updater, times(expectedSleepCalls)).sleep(1000);
verify(task, times(expectedRunCalls)).run();
assertTrue(Thread.currentThread().isInterrupted(), "Thread should be interrupted");
Expand All @@ -291,7 +328,8 @@ void testExecuteWithRetry_OtpHttpClientException() throws Throwable {
.doThrow(new OtpHttpClientException("could not get historical data"))
.doThrow(new OtpHttpClientException("could not get historical data"))
.doNothing()
.when(task).run();
.when(task)
.run();

doNothing().when(updater).sleep(anyInt());

Expand All @@ -304,8 +342,11 @@ void testExecuteWithRetry_OtpHttpClientException() throws Throwable {
List<Integer> expectedBackoffSequence = Arrays.asList(1000, 2000, 4000);

for (int i = 0; i < retryAttempts; i++) {
assertEquals(expectedBackoffSequence.get(i), sleepDurations.get(i),
"Backoff duration mismatch at retry " + (i + 1));
assertEquals(
expectedBackoffSequence.get(i),
sleepDurations.get(i),
"Backoff duration mismatch at retry " + (i + 1)
);
}

verify(task, times(retryAttempts + 1)).run();
Expand All @@ -318,9 +359,13 @@ void testExecuteWithRetry_UnexpectedException() throws Throwable {
Exception unexpectedException = new NullPointerException("Unexpected null value");
doThrow(unexpectedException).when(task).run();

Exception thrown = assertThrows(NullPointerException.class, () -> {
updater.executeWithRetry(task, "Test Task");
}, "Expected executeWithRetry to throw NullPointerException");
Exception thrown = assertThrows(
NullPointerException.class,
() -> {
updater.executeWithRetry(task, "Test Task");
},
"Expected executeWithRetry to throw NullPointerException"
);

assertEquals("Unexpected null value", thrown.getMessage(), "Exception message should match");
verify(updater, never()).sleep(anyInt());
Expand All @@ -344,7 +389,6 @@ private static Stream<Arguments> provideServiceBusFailureReasons() {
Arguments.of(ServiceBusFailureReason.QUOTA_EXCEEDED, true),
Arguments.of(ServiceBusFailureReason.GENERAL_ERROR, true),
Arguments.of(ServiceBusFailureReason.UNAUTHORIZED, true),

// Non-Retryable Errors
Arguments.of(ServiceBusFailureReason.MESSAGING_ENTITY_NOT_FOUND, false),
Arguments.of(ServiceBusFailureReason.MESSAGING_ENTITY_DISABLED, false),
Expand All @@ -361,7 +405,10 @@ private static Stream<Arguments> provideServiceBusFailureReasons() {
* @return A ServiceBusException instance with the specified reason.
*/
private ServiceBusException createServiceBusException(ServiceBusFailureReason reason) {
ServiceBusException exception = new ServiceBusException(new Throwable(), ServiceBusErrorSource.RECEIVE);
ServiceBusException exception = new ServiceBusException(
new Throwable(),
ServiceBusErrorSource.RECEIVE
);
try {
Field reasonField = ServiceBusException.class.getDeclaredField("reason");
reasonField.setAccessible(true);
Expand All @@ -379,7 +426,9 @@ private ServiceBusException createServiceBusException(ServiceBusFailureReason re
* @param <T> The type parameter extending ExpandableStringEnum.
* @return A Collection of all registered instances.
*/
private static <T extends ExpandableStringEnum<T>> Collection<T> getExpandableStringEnumValues(Class<T> clazz) {
private static <T extends ExpandableStringEnum<T>> Collection<T> getExpandableStringEnumValues(
Class<T> clazz
) {
try {
Method valuesMethod = ExpandableStringEnum.class.getDeclaredMethod("values", Class.class);
valuesMethod.setAccessible(true);
Expand All @@ -390,4 +439,4 @@ private static <T extends ExpandableStringEnum<T>> Collection<T> getExpandableSt
throw new RuntimeException("Failed to retrieve values from ExpandableStringEnum.", e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import java.util.List;
import org.junit.jupiter.api.Test;
import org.opentripplanner.service.vehiclerental.model.VehicleRentalPlace;
import org.opentripplanner.updater.vehicle_rental.datasources.params.RentalPickupType;
import org.opentripplanner.updater.spi.HttpHeaders;
import org.opentripplanner.updater.vehicle_rental.datasources.params.RentalPickupType;

class SmooveBikeRentalDataSourceTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ public void vehicleParkingGeometryTest() {
var nodeAdapter = newNodeAdapterForTest(config);
var tiles = VectorTileConfig.mapVectorTilesParameters(nodeAdapter, "vectorTiles");
assertEquals(1, tiles.layers().size());
var builder = new VehicleParkingsLayerBuilder(new DefaultVehicleParkingService(repo), tiles.layers().getFirst(), Locale.US);
var builder = new VehicleParkingsLayerBuilder(
new DefaultVehicleParkingService(repo),
tiles.layers().getFirst(),
Locale.US
);

List<Geometry> geometries = builder.getGeometries(new Envelope(0.99, 1.01, 1.99, 2.01));

Expand Down
Loading

0 comments on commit ed6cdce

Please sign in to comment.