Search in sources :

Example 1 with MissingRequiredFieldException

use of org.onebusaway.csv_entities.exceptions.MissingRequiredFieldException in project onebusaway-gtfs-modules by OneBusAway.

the class GtfsReaderTest method testFeedInfo.

@Test
public void testFeedInfo() throws CsvEntityIOException, IOException {
    GtfsReader reader = new GtfsReader();
    StringBuilder b = new StringBuilder();
    b.append("feed_publisher_name,feed_publisher_url,feed_lang,feed_start_date,feed_end_date,feed_version\n");
    b.append("Test,http://test/,en,20110928,20120131,1.0\n");
    reader.readEntities(FeedInfo.class, new StringReader(b.toString()));
    FeedInfo feedInfo = reader.getEntityStore().getEntityForId(FeedInfo.class, "1");
    assertEquals("Test", feedInfo.getPublisherName());
    assertEquals("http://test/", feedInfo.getPublisherUrl());
    assertEquals("en", feedInfo.getLang());
    assertEquals(new ServiceDate(2011, 9, 28), feedInfo.getStartDate());
    assertEquals(new ServiceDate(2012, 1, 31), feedInfo.getEndDate());
    assertEquals("1.0", feedInfo.getVersion());
    /**
     * Test with a missing "field_publisher_url" field
     */
    b = new StringBuilder();
    b.append("feed_publisher_name\n");
    b.append("Test\n");
    try {
        reader.readEntities(FeedInfo.class, new StringReader(b.toString()));
        fail();
    } catch (CsvEntityIOException ex) {
        MissingRequiredFieldException ex2 = (MissingRequiredFieldException) ex.getCause();
        assertEquals(FeedInfo.class, ex2.getEntityType());
        assertEquals("feed_publisher_url", ex2.getFieldName());
    }
    /**
     * Test with a missing "field_lang" field
     */
    b = new StringBuilder();
    b.append("feed_publisher_name,feed_publisher_url\n");
    b.append("Test,http://test/\n");
    try {
        reader.readEntities(FeedInfo.class, new StringReader(b.toString()));
        fail();
    } catch (CsvEntityIOException ex) {
        MissingRequiredFieldException ex2 = (MissingRequiredFieldException) ex.getCause();
        assertEquals(FeedInfo.class, ex2.getEntityType());
        assertEquals("feed_lang", ex2.getFieldName());
    }
    /**
     * Test with a malformed "feed_start_date" field
     */
    b = new StringBuilder();
    b.append("feed_publisher_name,feed_publisher_url,feed_lang,feed_start_date\n");
    b.append("Test,http://test/,en,2011XX01\n");
    try {
        reader.readEntities(FeedInfo.class, new StringReader(b.toString()));
        fail();
    } catch (CsvEntityIOException ex) {
        InvalidValueEntityException ex2 = (InvalidValueEntityException) ex.getCause();
        assertEquals(FeedInfo.class, ex2.getEntityType());
        assertEquals("feed_start_date", ex2.getFieldName());
        assertEquals("2011XX01", ex2.getFieldValue());
    }
    /**
     * Test with a malformed "feed_end_date" field
     */
    b = new StringBuilder();
    b.append("feed_publisher_name,feed_publisher_url,feed_lang,feed_end_date\n");
    b.append("Test,http://test/,en,2011XX01\n");
    try {
        reader.readEntities(FeedInfo.class, new StringReader(b.toString()));
        fail();
    } catch (CsvEntityIOException ex) {
        InvalidValueEntityException ex2 = (InvalidValueEntityException) ex.getCause();
        assertEquals(FeedInfo.class, ex2.getEntityType());
        assertEquals("feed_end_date", ex2.getFieldName());
        assertEquals("2011XX01", ex2.getFieldValue());
    }
}
Also used : ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) MissingRequiredFieldException(org.onebusaway.csv_entities.exceptions.MissingRequiredFieldException) CsvEntityIOException(org.onebusaway.csv_entities.exceptions.CsvEntityIOException) StringReader(java.io.StringReader) InvalidValueEntityException(org.onebusaway.csv_entities.exceptions.InvalidValueEntityException) Test(org.junit.Test)

Example 2 with MissingRequiredFieldException

use of org.onebusaway.csv_entities.exceptions.MissingRequiredFieldException in project onebusaway-gtfs-modules by OneBusAway.

the class GtfsReaderTest method testDefaultAgencyForRoutes.

@Test
public void testDefaultAgencyForRoutes() throws IOException {
    MockGtfs gtfs = MockGtfs.create();
    gtfs.putAgencies(1);
    gtfs.putRoutes(1);
    gtfs.putTrips(1, "r0", "sid0");
    gtfs.putStops(1);
    gtfs.putStopTimes("t0", "s0");
    {
        GtfsMutableRelationalDao dao = gtfs.read(newReader("tacos"));
        assertNotNull(dao.getRouteForId(new AgencyAndId("a0", "r0")));
    }
    {
        gtfs.putAgencies(2);
        try {
            gtfs.read(newReader("tacos"));
            fail();
        } catch (CsvEntityIOException e) {
            MissingRequiredFieldException ex = (MissingRequiredFieldException) e.getCause();
            assertEquals("agency_id", ex.getFieldName());
            assertEquals(Route.class, ex.getEntityType());
        }
    }
    {
        gtfs.putAgencies(2);
        gtfs.putRoutes(1, "agency_id=a1");
        GtfsMutableRelationalDao dao = gtfs.read(newReader("tacos"));
        assertNotNull(dao.getRouteForId(new AgencyAndId("a1", "r0")));
    }
    {
        gtfs.putAgencies(2);
        gtfs.putRoutes(1, "agency_id=a2");
        try {
            gtfs.read(newReader("tacos"));
            fail();
        } catch (CsvEntityIOException e) {
            assertTrue(e.getCause() instanceof AgencyNotFoundForRouteException);
        }
    }
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) MissingRequiredFieldException(org.onebusaway.csv_entities.exceptions.MissingRequiredFieldException) CsvEntityIOException(org.onebusaway.csv_entities.exceptions.CsvEntityIOException) MockGtfs(org.onebusaway.gtfs.services.MockGtfs) AgencyNotFoundForRouteException(org.onebusaway.gtfs.serialization.mappings.AgencyNotFoundForRouteException) Test(org.junit.Test)

Example 3 with MissingRequiredFieldException

use of org.onebusaway.csv_entities.exceptions.MissingRequiredFieldException in project onebusaway-gtfs-modules by OneBusAway.

the class TransformFactory method setObjectPropertiesFromJsonUsingCsvFields.

private void setObjectPropertiesFromJsonUsingCsvFields(Object object, JSONObject json, String line) throws JSONException, TransformSpecificationMissingArgumentException {
    EntitySchemaFactory entitySchemaFactory = _transformer.getReader().getEntitySchemaFactory();
    EntitySchema schema = entitySchemaFactory.getSchema(object.getClass());
    BeanWrapper wrapped = BeanWrapperFactory.wrap(object);
    Map<String, Object> values = new HashMap<String, Object>();
    for (Iterator<?> it = json.keys(); it.hasNext(); ) {
        String key = (String) it.next();
        Object v = json.get(key);
        if (v instanceof JSONArray) {
            JSONArray array = (JSONArray) v;
            List<Object> asList = new ArrayList<Object>();
            for (int i = 0; i < array.length(); ++i) {
                asList.add(array.get(i));
            }
            v = asList;
        }
        values.put(key, v);
    }
    CsvEntityContext context = new CsvEntityContextImpl();
    for (FieldMapping mapping : schema.getFields()) {
        try {
            mapping.translateFromCSVToObject(context, values, wrapped);
        } catch (MissingRequiredFieldException ex) {
            String verboseMessage = "line=" + line + ", context=" + context + ", json=" + json + ", object=" + object;
            _log.error("missing required field; details:" + verboseMessage);
            throw new TransformSpecificationMissingArgumentException(verboseMessage, ex.getFieldName());
        }
    }
}
Also used : MissingRequiredFieldException(org.onebusaway.csv_entities.exceptions.MissingRequiredFieldException) JSONArray(org.json.JSONArray) CsvEntityContextImpl(org.onebusaway.csv_entities.CsvEntityContextImpl) CsvEntityContext(org.onebusaway.csv_entities.CsvEntityContext) JSONObject(org.json.JSONObject) TransformSpecificationMissingArgumentException(org.onebusaway.gtfs_transformer.TransformSpecificationMissingArgumentException)

Aggregations

MissingRequiredFieldException (org.onebusaway.csv_entities.exceptions.MissingRequiredFieldException)3 Test (org.junit.Test)2 CsvEntityIOException (org.onebusaway.csv_entities.exceptions.CsvEntityIOException)2 StringReader (java.io.StringReader)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1 CsvEntityContext (org.onebusaway.csv_entities.CsvEntityContext)1 CsvEntityContextImpl (org.onebusaway.csv_entities.CsvEntityContextImpl)1 InvalidValueEntityException (org.onebusaway.csv_entities.exceptions.InvalidValueEntityException)1 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)1 AgencyNotFoundForRouteException (org.onebusaway.gtfs.serialization.mappings.AgencyNotFoundForRouteException)1 GtfsMutableRelationalDao (org.onebusaway.gtfs.services.GtfsMutableRelationalDao)1 MockGtfs (org.onebusaway.gtfs.services.MockGtfs)1 TransformSpecificationMissingArgumentException (org.onebusaway.gtfs_transformer.TransformSpecificationMissingArgumentException)1