Skip to content
Permalink
Browse files

Merge branch 'feedLoad' of https://github.com/mohangandhiGH/gtfs-real…

…time-validator into feedLoad_new
  • Loading branch information...
mohangandhiGH committed Feb 23, 2017
2 parents 2da3769 + 0272fed commit e2764e45ea00acb526f24e5f19d84cba699f55ce
@@ -15,5 +15,4 @@ buildNumber.properties
*.log
*~

*.zip
*.db
@@ -123,7 +123,11 @@ public Response postGtfsFeed(@FormParam("gtfsurl") String gtfsFeedUrl) throws Js
+ "WHERE gtfsUrl = '"+gtfsFeedUrl+"'").uniqueResult();
GTFSDB.commitAndCloseSession(session);
ObjectMapper mapper = new ObjectMapper();
downloadGtfsFeed(saveFilePath, connection);
Response.Status response = downloadGtfsFeed(saveFilePath, connection);
if(response == null) {
return generateError("Download Failed", "Downloading static GTFS feed from provided Url failed", Response.Status.BAD_REQUEST);
}

System.out.println("GTFS File Downloaded Successfully");
//TODO: Move to one method
if (gtfsFeed == null) {
@@ -221,8 +225,9 @@ private String getSaveFilePath(String gtfsFeedUrl, HttpURLConnection connection)
} else {
//Extracts file name from header field
int index = disposition.indexOf("filename=");
int filenameLastIndex = disposition.indexOf("\"", index + 10);
if (index > 0) {
fileName = disposition.substring(index + 10, disposition.length() - 1);
fileName = disposition.substring(index + 10, filenameLastIndex);
}
}

@@ -296,7 +301,7 @@ private GtfsDaoImpl saveGtfsFeed(GtfsFeedModel gtfsFeed) {
return store;
}

private void downloadGtfsFeed(String saveFilePath, HttpURLConnection connection) {
private Response.Status downloadGtfsFeed(String saveFilePath, HttpURLConnection connection) {
try {
// opens input stream from the HTTP connection
InputStream inputStream = connection.getInputStream();
@@ -314,6 +319,8 @@ private void downloadGtfsFeed(String saveFilePath, HttpURLConnection connection)
inputStream.close();
} catch (IOException ex) {
System.out.println("Downloading GTFS Feed Failed");
return null;
}
return Response.Status.OK;
}
}
@@ -0,0 +1,106 @@
/*
* Copyright (C) 2017 University of South Florida.
* All rights reserved.
*
* Licensed 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 edu.usf.cutr.gtfsrtvalidator;

import com.google.transit.realtime.GtfsRealtime;
import edu.usf.cutr.gtfsrtvalidator.helper.ErrorListHelperModel;
import edu.usf.cutr.gtfsrtvalidator.test.headerandentity.FeedEntityTest;
import edu.usf.cutr.gtfsrtvalidator.test.headerandentity.FeedHeaderTest;
import java.io.File;
import java.io.IOException;
import junit.framework.TestCase;
import org.onebusaway.gtfs.impl.GtfsDaoImpl;
import org.onebusaway.gtfs.serialization.GtfsReader;
import org.onebusway.gtfs_realtime.exporter.GtfsRealtimeLibrary;


public class FeedMessageTest extends TestCase {

public GtfsDaoImpl gtfsData, gtfsData2; // gtfsData2 contains location_type = 1 for stop_id
public GtfsReader reader;
public final File staticGtfs = new File("src/test/resources/testagency.zip");
public final File staticGtfs2 = new File("src/test/resources/testagency2.zip");
public final static String ENTITY_ID = "TEST_ENTITY";

FeedHeaderTest feedHeaderTest;
FeedEntityTest feedEntityTest;

public ErrorListHelperModel errors;

public GtfsRealtime.FeedMessage.Builder feedMessageBuilder;
public GtfsRealtime.FeedEntity.Builder feedEntityBuilder;
public GtfsRealtime.FeedHeader.Builder feedHeaderBuilder;

public GtfsRealtime.TripUpdate.Builder tripUpdateBuilder;
public GtfsRealtime.VehiclePosition.Builder vehiclePositionBuilder;
public GtfsRealtime.Alert.Builder alertBuilder;

public FeedMessageTest() throws IOException {
errors = new ErrorListHelperModel();

feedMessageBuilder = GtfsRealtimeLibrary.createFeedMessageBuilder();
feedEntityBuilder = GtfsRealtime.FeedEntity.newBuilder();
feedHeaderBuilder = GtfsRealtime.FeedHeader.newBuilder();

tripUpdateBuilder = GtfsRealtime.TripUpdate.newBuilder();
vehiclePositionBuilder = GtfsRealtime.VehiclePosition.newBuilder();
alertBuilder = GtfsRealtime.Alert.newBuilder();

// Read GTFS data into a GtfsDaoImpl
gtfsData = new GtfsDaoImpl();
reader = new GtfsReader();
reader.setInputLocation(staticGtfs);
reader.setEntityStore(gtfsData);
reader.run();

gtfsData2 = new GtfsDaoImpl();
reader = new GtfsReader();
reader.setInputLocation(staticGtfs2);
reader.setEntityStore(gtfsData2);
reader.run();

clearAndInitRequiredFeedFields();
}

// Initialization of some required fields in FeedMessage
public final void clearAndInitRequiredFeedFields() {
// clear the fields first
feedEntityBuilder.clear();
feedMessageBuilder.clear();
feedHeaderBuilder.clear();

tripUpdateBuilder.clear();
vehiclePositionBuilder.clear();
alertBuilder.clear();

feedHeaderBuilder.setGtfsRealtimeVersion("1.0");
feedMessageBuilder.setHeader(feedHeaderBuilder);

feedEntityBuilder.setId(ENTITY_ID);
feedMessageBuilder.addEntity(feedEntityBuilder);
}

/**
* Tests the FeedHeader and FeedEntity. Initializing object of FeedHeader and FeedEntity will execute the
* tests in them.
* @throws Exception
*/
public final void testMessageFeed() throws Exception {
feedHeaderTest = new FeedHeaderTest();
feedEntityTest = new FeedEntityTest();
}
}
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2017 University of South Florida.
* All rights reserved.
*
* Licensed 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 edu.usf.cutr.gtfsrtvalidator.api.resource;

import com.fasterxml.jackson.core.JsonProcessingException;
import edu.usf.cutr.gtfsrtvalidator.db.GTFSDB;
import edu.usf.cutr.gtfsrtvalidator.hibernate.HibernateUtil;
import junit.framework.TestCase;
import javax.ws.rs.core.Response;

/*
* Tests loading GTFS data.
*/
public class GtfsFeedTest extends TestCase {

private final String validGtfsFeedURL = "http://gohart.org/google/google_transit.zip";
private final String invalidGtfsFeedURL = "DUMMY";
private final String downloadFailURL = "http://gohart.org/google/file_not_exist.zip";

GtfsFeed gtfsFeed;

protected void setUp() {
gtfsFeed = new GtfsFeed();
HibernateUtil.configureSessionFactory();
GTFSDB.InitializeDB();
}

public void testGtfsFeed() throws JsonProcessingException {
Response response;

response = gtfsFeed.postGtfsFeed(validGtfsFeedURL);
assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());

response = gtfsFeed.postGtfsFeed(invalidGtfsFeedURL);
assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode());

response = gtfsFeed.postGtfsFeed(downloadFailURL);
assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode());
}
}
@@ -0,0 +1,26 @@
/*
* Copyright (C) 2017 University of South Florida.
* All rights reserved.
*
* Licensed 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 edu.usf.cutr.gtfsrtvalidator.test.feeds;

/*
* Tests all the warnings and rules that validate Alert feed.
*/
import edu.usf.cutr.gtfsrtvalidator.FeedMessageTest;

public class AlertFeedTest extends FeedMessageTest {
public AlertFeedTest() throws Exception {}
}
@@ -0,0 +1,157 @@
/*
* Copyright (C) 2017 University of South Florida.
* All rights reserved.
*
* Licensed 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 edu.usf.cutr.gtfsrtvalidator.test.feeds;

import com.google.transit.realtime.GtfsRealtime;
import edu.usf.cutr.gtfsrtvalidator.FeedMessageTest;
import edu.usf.cutr.gtfsrtvalidator.validation.entity.CheckTripId;
import edu.usf.cutr.gtfsrtvalidator.validation.entity.StopTimeSequanceValidator;
import edu.usf.cutr.gtfsrtvalidator.validation.entity.VehicleIdValidator;
import edu.usf.cutr.gtfsrtvalidator.validation.gtfs.StopLocationTypeValidator;
import org.junit.Test;

/*
* Tests all the warnings and rules that validate TripUpdate feed.
* Tests: w002 - "vehicle_id should be populated in trip_update"
* e003 - "All trip_ids provided in the GTFS-rt feed must appear in the GTFS data"
* e002 - "stop_time_updates for a given trip_id must be sorted by increasing stop_sequence"
* e010 - "If location_type is used in stops.txt, all stops referenced in stop_times.txt must have location_type of 0"
*/
public class TripUpdateFeedTest extends FeedMessageTest {

public TripUpdateFeedTest() throws Exception {}

@Test
public void testTripIdValidation() {

CheckTripId tripIdValidator = new CheckTripId();

GtfsRealtime.TripDescriptor.Builder tripDescriptorBuilder = GtfsRealtime.TripDescriptor.newBuilder();

// setting valid trip id = 1.1 that match with trip id in static Gtfs data
tripDescriptorBuilder.setTripId("1.1");
tripUpdateBuilder.setTrip(tripDescriptorBuilder.build());
feedEntityBuilder.setTripUpdate(tripUpdateBuilder.build());
feedMessageBuilder.setEntity(0, feedEntityBuilder.build());

errors = tripIdValidator.validate(gtfsData, feedMessageBuilder.build());
assertEquals(0, errors.getOccurrenceList().size());

// setting invalid trip id = 100 that does not match with any trip id in static Gtfs data
tripDescriptorBuilder.setTripId("100");
tripUpdateBuilder.setTrip(tripDescriptorBuilder.build());
feedEntityBuilder.setTripUpdate(tripUpdateBuilder.build());
feedMessageBuilder.setEntity(0, feedEntityBuilder.build());

errors = tripIdValidator.validate(gtfsData, feedMessageBuilder.build());
assertEquals(1, errors.getOccurrenceList().size());

clearAndInitRequiredFeedFields();
}

@Test
public void testStopSequenceValidation() {
StopTimeSequanceValidator stopSequenceValidator = new StopTimeSequanceValidator();

GtfsRealtime.TripUpdate.StopTimeUpdate.Builder stopTimeUpdateBuilder = GtfsRealtime.TripUpdate.StopTimeUpdate.newBuilder();
GtfsRealtime.TripDescriptor.Builder tripDescriptorBuilder = GtfsRealtime.TripDescriptor.newBuilder();

// tripDescriptor is a required field in tripUpdate
tripUpdateBuilder.setTrip(tripDescriptorBuilder.build());
feedEntityBuilder.setTripUpdate(tripUpdateBuilder.build());
feedMessageBuilder.setEntity(0, feedEntityBuilder.build());

// ordered stop sequence 1, 5
stopTimeUpdateBuilder.setStopSequence(1);
tripUpdateBuilder.addStopTimeUpdate(stopTimeUpdateBuilder.build());
stopTimeUpdateBuilder.setStopSequence(5);
tripUpdateBuilder.addStopTimeUpdate(stopTimeUpdateBuilder.build());
feedEntityBuilder.setTripUpdate(tripUpdateBuilder.build());
feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
// StopTimeUpdate count should be 2
assertEquals(2, feedMessageBuilder.getEntity(0).getTripUpdate().getStopTimeUpdateCount());

errors = stopSequenceValidator.validate(gtfsData, feedMessageBuilder.build());
assertNull(errors);

/* Adding stop sequence 3. So, the stop sequence now is 1, 5, 3 which is unordered.
So, the validation fails and the assertion test passes
*/
stopTimeUpdateBuilder.setStopSequence(3);
tripUpdateBuilder.addStopTimeUpdate(stopTimeUpdateBuilder.build());
feedEntityBuilder.setTripUpdate(tripUpdateBuilder.build());
feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
// StopTimeUpdate count should be 3
assertEquals(3, feedMessageBuilder.getEntity(0).getTripUpdate().getStopTimeUpdateCount());

errors = stopSequenceValidator.validate(gtfsData, feedMessageBuilder.build());
assertEquals(1, errors.getOccurrenceList().size());

clearAndInitRequiredFeedFields();
}

@Test
public void testVehicleIdValidation() {
VehicleIdValidator vehicleIdValidator = new VehicleIdValidator();

GtfsRealtime.TripDescriptor.Builder tripDescriptorBuilder = GtfsRealtime.TripDescriptor.newBuilder();
GtfsRealtime.VehicleDescriptor.Builder vehicleDescriptorBuilder = GtfsRealtime.VehicleDescriptor.newBuilder();

// tripDescriptor is a required field in tripUpdate
tripUpdateBuilder.setTrip(tripDescriptorBuilder.build());
feedEntityBuilder.setTripUpdate(tripUpdateBuilder.build());
feedMessageBuilder.setEntity(0, feedEntityBuilder.build());

// setting a value for vehicle id = 1
vehicleDescriptorBuilder.setId("1");
tripUpdateBuilder.setVehicle(vehicleDescriptorBuilder.build());
feedEntityBuilder.setTripUpdate(tripUpdateBuilder.build());
feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
// No errors, if vehicle id has a value.
errors = vehicleIdValidator.validate(gtfsData, feedMessageBuilder.build());
assertNull(errors);

// unable to set Id = null as it's throwing NullPointerException.
// Assuming, there will be no values with Id = null in TripUpdate feed
/*vehicleDescriptor.setId(null);
tripUpdateBuilder.setVehicle(vehicleDescriptorBuilder.build());
feedEntityBuilder.setTripUpdate(tripUpdateBuilder.build());
feedMessageBuilder.setEntity(0, feedEntityBuilder.build());
errors = vehicleIdValidator.validate(gtfsData, feedMessageBuilder.build());
assertEquals(1, errors.getOccurrenceList().size());*/

clearAndInitRequiredFeedFields();
}



@Test
public void testLocationTypeValidation() {
StopLocationTypeValidator stopLocationValidator = new StopLocationTypeValidator();

// gtfsData does not contain location_type = 1 for stop_id. Therefore returns 0 errors
errors = stopLocationValidator.validate(gtfsData);
assertEquals(0, errors.getOccurrenceList().size());

// gtfsData2 contains location_type = 1 for stop_ids. Therefore returns errorcount = (number of location_type = 1 for stop_ids)
errors = stopLocationValidator.validate(gtfsData2);
assertTrue(errors.getOccurrenceList().size() >= 1);

clearAndInitRequiredFeedFields();
}
}

0 comments on commit e2764e4

Please sign in to comment.
You can’t perform that action at this time.