blob: ba3e6b680cbcca22bec3dcb00828556560ae7279 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.flink.training.exercises.longrides;
import org.apache.flink.training.exercises.common.datatypes.TaxiRide;
import org.apache.flink.training.exercises.testing.TaxiRideTestBase;
import org.apache.flink.training.solutions.longrides.LongRidesSolution;
import org.joda.time.DateTime;
import org.junit.Test;
import java.util.Collections;
import java.util.List;
import static org.junit.Assert.assertEquals;
public class LongRidesTest extends TaxiRideTestBase<TaxiRide> {
static final Testable JAVA_EXERCISE = () -> LongRidesExercise.main(new String[]{});
private static final DateTime BEGINNING = new DateTime(2000, 1, 1, 0, 0);
@Test
public void shortRide() throws Exception {
DateTime oneMinLater = BEGINNING.plusMinutes(1);
TaxiRide rideStarted = startRide(1, BEGINNING);
TaxiRide endedOneMinLater = endRide(rideStarted, oneMinLater);
Long markOneMinLater = oneMinLater.getMillis();
TestRideSource source = new TestRideSource(rideStarted, endedOneMinLater, markOneMinLater);
assert(results(source).isEmpty());
}
@Test
public void outOfOrder() throws Exception {
DateTime oneMinLater = BEGINNING.plusMinutes(1);
TaxiRide rideStarted = startRide(1, BEGINNING);
TaxiRide endedOneMinLater = endRide(rideStarted, oneMinLater);
Long markOneMinLater = oneMinLater.getMillis();
TestRideSource source = new TestRideSource(endedOneMinLater, rideStarted, markOneMinLater);
assert(results(source).isEmpty());
}
@Test
public void noStartShort() throws Exception {
DateTime oneMinLater = BEGINNING.plusMinutes(1);
TaxiRide rideStarted = startRide(1, BEGINNING);
TaxiRide endedOneMinLater = endRide(rideStarted, oneMinLater);
Long markOneMinLater = oneMinLater.getMillis();
TestRideSource source = new TestRideSource(endedOneMinLater, markOneMinLater);
assert(results(source).isEmpty());
}
@Test
public void noEnd() throws Exception {
TaxiRide rideStarted = startRide(1, BEGINNING);
Long markThreeHoursLater = BEGINNING.plusHours(3).getMillis();
TestRideSource source = new TestRideSource(rideStarted, markThreeHoursLater);
assertEquals(Collections.singletonList(rideStarted), results(source));
}
@Test
public void longRide() throws Exception {
TaxiRide rideStarted = startRide(1, BEGINNING);
Long mark2HoursLater = BEGINNING.plusMinutes(120).getMillis();
TaxiRide rideEnded3HoursLater = endRide(rideStarted, BEGINNING.plusHours(3));
TestRideSource source = new TestRideSource(rideStarted, mark2HoursLater, rideEnded3HoursLater);
assertEquals(Collections.singletonList(rideStarted), results(source));
}
private TaxiRide testRide(long rideId, Boolean isStart, DateTime startTime, DateTime endTime) {
return new TaxiRide(rideId, isStart, startTime, endTime, -73.9947F, 40.750626F, -73.9947F, 40.750626F, (short) 1, 0, 0);
}
private TaxiRide startRide(long rideId, DateTime startTime) {
return testRide(rideId, true, startTime, new DateTime(0));
}
private TaxiRide endRide(TaxiRide started, DateTime endTime) {
return testRide(started.rideId, false, started.startTime, endTime);
}
protected List<TaxiRide> results(TestRideSource source) throws Exception {
Testable javaSolution = () -> LongRidesSolution.main(new String[]{});
return runApp(source, new TestSink<>(), JAVA_EXERCISE, javaSolution);
}
}