14 changed files with 491 additions and 23 deletions
@ -0,0 +1,39 @@
|
||||
package org.leolo.nrdatad.util; |
||||
|
||||
import org.apache.logging.log4j.LogManager; |
||||
import org.apache.logging.log4j.Logger; |
||||
|
||||
import java.text.ParseException; |
||||
import java.text.SimpleDateFormat; |
||||
import java.util.Date; |
||||
|
||||
public class DateUtil { |
||||
|
||||
private static Logger log = LogManager.getLogger(); |
||||
|
||||
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd"; |
||||
|
||||
public static Date parseDate(String date){ |
||||
return parseDate(date, DEFAULT_DATE_FORMAT, true); |
||||
} |
||||
|
||||
public static Date parseDate(String date, boolean suppressException){ |
||||
return parseDate(date, DEFAULT_DATE_FORMAT, suppressException); |
||||
} |
||||
|
||||
public static Date parseDate(String data, String dateFormat){ |
||||
return parseDate(data, dateFormat, true); |
||||
} |
||||
public static Date parseDate(String data, String dateFormat, boolean suppressException){ |
||||
try{ |
||||
return new SimpleDateFormat(dateFormat).parse(data); |
||||
} catch (ParseException e) { |
||||
log.atWarn().withThrowable(e).log("Unable to parse date"); |
||||
if(suppressException){ |
||||
return null; |
||||
} |
||||
throw new RuntimeException(e.getMessage(), e); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,20 @@
|
||||
package org.leolo.nrdatad.model; |
||||
import org.junit.Test; |
||||
import org.leolo.nrdatad.util.TestUtil; |
||||
|
||||
import java.io.BufferedReader; |
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
import java.io.InputStreamReader; |
||||
|
||||
import static org.junit.Assert.*; |
||||
public class TrainScheduleTest { |
||||
|
||||
@Test public void testSchedule1() throws IOException{ |
||||
String json = TestUtil.openResourceFileAsString("org/leolo/nrdatad/test/V56331_N.json"); |
||||
TrainSchedule ts = TrainSchedule.parseJSON(json); |
||||
assertEquals("V56331", ts.getTrainUid()); |
||||
//TODO: finish the function
|
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,41 @@
|
||||
package org.leolo.nrdatad.util; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
import java.util.Calendar; |
||||
import java.util.Date; |
||||
import java.util.GregorianCalendar; |
||||
|
||||
import static org.junit.Assert.*; |
||||
|
||||
public class DateUtilTest { |
||||
|
||||
@Test public void testDate1(){ |
||||
// Second day of January 2022
|
||||
Date d1 = DateUtil.parseDate("2022-01-02"); |
||||
Calendar c = GregorianCalendar.getInstance(); |
||||
c.setTime(d1); |
||||
assertEquals(2022, c.get(Calendar.YEAR)); |
||||
assertEquals(Calendar.JANUARY, c.get(Calendar.MONTH)); |
||||
assertEquals(2, c.get(Calendar.DAY_OF_MONTH)); |
||||
} |
||||
|
||||
@Test public void testDate2(){ |
||||
// Second day of January 2022
|
||||
Date d1 = DateUtil.parseDate("2022-01-02", "yyyy-dd-MM"); |
||||
Calendar c = GregorianCalendar.getInstance(); |
||||
c.setTime(d1); |
||||
assertEquals(2022, c.get(Calendar.YEAR)); |
||||
assertEquals(Calendar.FEBRUARY, c.get(Calendar.MONTH)); |
||||
assertEquals(1, c.get(Calendar.DAY_OF_MONTH)); |
||||
} |
||||
|
||||
@Test public void testDateInvalidSuppressed(){ |
||||
assertNull(DateUtil.parseDate("This is not a date")); |
||||
} |
||||
|
||||
@Test(expected = RuntimeException.class) public void testDateException(){ |
||||
DateUtil.parseDate("This is not a date", false); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,31 @@
|
||||
package org.leolo.nrdatad.util; |
||||
|
||||
import java.io.*; |
||||
|
||||
public class TestUtil { |
||||
|
||||
public static BufferedReader openResourceFileAsBufferedReader(String fileName) throws IOException { |
||||
ClassLoader classloader = Thread.currentThread().getContextClassLoader(); |
||||
InputStream is = classloader.getResourceAsStream(fileName); |
||||
if(is==null){ |
||||
throw new FileNotFoundException("Resource file "+fileName+" cannot be found"); |
||||
}else{ |
||||
return new BufferedReader(new InputStreamReader(is)); |
||||
} |
||||
} |
||||
|
||||
public static String openResourceFileAsString(String fileName) throws IOException { |
||||
ClassLoader classloader = Thread.currentThread().getContextClassLoader(); |
||||
InputStream is = classloader.getResourceAsStream(fileName); |
||||
if(is==null){ |
||||
throw new FileNotFoundException("Resource file "+fileName+" cannot be found"); |
||||
} |
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
||||
is.transferTo(baos); |
||||
String result = new String(baos.toByteArray()); |
||||
baos.close(); |
||||
is.close(); |
||||
return result; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,178 @@
|
||||
{ |
||||
"CIF_bank_holiday_running": null, |
||||
"train_status": "1", |
||||
"CIF_train_uid": "V56331", |
||||
"schedule_days_runs": "0010000", |
||||
"CIF_stp_indicator": "N", |
||||
"applicable_timetable": "Y", |
||||
"atoc_code": "AW", |
||||
"schedule_start_date": "2023-01-04", |
||||
"new_schedule_segment": { |
||||
"uic_code": "", |
||||
"traction_class": "" |
||||
}, |
||||
"transaction_type": "Create", |
||||
"schedule_end_date": "2023-01-04", |
||||
"schedule_segment": { |
||||
"CIF_service_branding": "", |
||||
"CIF_train_category": "OO", |
||||
"CIF_business_sector": "??", |
||||
"CIF_connection_indicator": null, |
||||
"CIF_speed": "075", |
||||
"CIF_reservations": null, |
||||
"CIF_catering_code": null, |
||||
"CIF_power_type": "DMU", |
||||
"CIF_course_indicator": 1, |
||||
"CIF_timing_load": "S", |
||||
"signalling_id": "2F60", |
||||
"CIF_headcode": "", |
||||
"CIF_operating_characteristics": null, |
||||
"schedule_location": [ |
||||
{ |
||||
"pathing_allowance": null, |
||||
"record_identity": "LO", |
||||
"engineering_allowance": null, |
||||
"line": null, |
||||
"public_departure": "1654", |
||||
"tiploc_code": "PTYPRID", |
||||
"tiploc_instance": null, |
||||
"departure": "1654", |
||||
"performance_allowance": null, |
||||
"location_type": "LO", |
||||
"platform": "1" |
||||
}, |
||||
{ |
||||
"engineering_allowance": null, |
||||
"arrival": "1656", |
||||
"pass": null, |
||||
"line": null, |
||||
"public_arrival": "1656", |
||||
"performance_allowance": null, |
||||
"location_type": "LI", |
||||
"platform": null, |
||||
"pathing_allowance": null, |
||||
"record_identity": "LI", |
||||
"path": null, |
||||
"public_departure": "1657", |
||||
"tiploc_code": "TREFRST", |
||||
"tiploc_instance": null, |
||||
"departure": "1657" |
||||
}, |
||||
{ |
||||
"engineering_allowance": null, |
||||
"arrival": "1703", |
||||
"pass": null, |
||||
"line": null, |
||||
"public_arrival": "1704", |
||||
"performance_allowance": null, |
||||
"location_type": "LI", |
||||
"platform": null, |
||||
"pathing_allowance": null, |
||||
"record_identity": "LI", |
||||
"path": null, |
||||
"public_departure": "1704", |
||||
"tiploc_code": "TAFFSWL", |
||||
"tiploc_instance": null, |
||||
"departure": "1704" |
||||
}, |
||||
{ |
||||
"engineering_allowance": null, |
||||
"arrival": "1707", |
||||
"pass": null, |
||||
"line": null, |
||||
"public_arrival": "1708", |
||||
"performance_allowance": null, |
||||
"location_type": "LI", |
||||
"platform": "1", |
||||
"pathing_allowance": null, |
||||
"record_identity": "LI", |
||||
"path": null, |
||||
"public_departure": "1708", |
||||
"tiploc_code": "RADYR", |
||||
"tiploc_instance": null, |
||||
"departure": "1708" |
||||
}, |
||||
{ |
||||
"engineering_allowance": null, |
||||
"arrival": "1710", |
||||
"pass": null, |
||||
"line": null, |
||||
"public_arrival": "1710", |
||||
"performance_allowance": null, |
||||
"location_type": "LI", |
||||
"platform": null, |
||||
"pathing_allowance": null, |
||||
"record_identity": "LI", |
||||
"path": null, |
||||
"public_departure": "1711", |
||||
"tiploc_code": "LLANDAF", |
||||
"tiploc_instance": null, |
||||
"departure": "1711" |
||||
}, |
||||
{ |
||||
"engineering_allowance": "1", |
||||
"arrival": "1714H", |
||||
"pass": null, |
||||
"line": null, |
||||
"public_arrival": "1715", |
||||
"performance_allowance": null, |
||||
"location_type": "LI", |
||||
"platform": null, |
||||
"pathing_allowance": "H", |
||||
"record_identity": "LI", |
||||
"path": null, |
||||
"public_departure": "1715", |
||||
"tiploc_code": "CATHAYS", |
||||
"tiploc_instance": null, |
||||
"departure": "1715H" |
||||
}, |
||||
{ |
||||
"engineering_allowance": null, |
||||
"arrival": "1719", |
||||
"pass": null, |
||||
"line": null, |
||||
"public_arrival": "1720", |
||||
"performance_allowance": null, |
||||
"location_type": "LI", |
||||
"platform": "3", |
||||
"pathing_allowance": null, |
||||
"record_identity": "LI", |
||||
"path": null, |
||||
"public_departure": "1721", |
||||
"tiploc_code": "CARDFQS", |
||||
"tiploc_instance": null, |
||||
"departure": "1721" |
||||
}, |
||||
{ |
||||
"engineering_allowance": null, |
||||
"arrival": null, |
||||
"pass": "1723", |
||||
"line": null, |
||||
"public_arrival": null, |
||||
"performance_allowance": null, |
||||
"location_type": "LI", |
||||
"platform": null, |
||||
"pathing_allowance": null, |
||||
"record_identity": "LI", |
||||
"path": null, |
||||
"public_departure": null, |
||||
"tiploc_code": "CVLESBY", |
||||
"tiploc_instance": null, |
||||
"departure": null |
||||
}, |
||||
{ |
||||
"record_identity": "LT", |
||||
"path": null, |
||||
"arrival": "1724", |
||||
"public_arrival": "1725", |
||||
"tiploc_code": "CRDFCEN", |
||||
"tiploc_instance": null, |
||||
"location_type": "LT", |
||||
"platform": "7" |
||||
} |
||||
], |
||||
"CIF_sleepers": null, |
||||
"CIF_train_service_code": "25441000", |
||||
"CIF_train_class": "S" |
||||
} |
||||
} |
||||
Loading…
Reference in new issue