ProductSetPersistable { public static ProductSet convertFromCSV(String text) { String trimmedText = text.trim(); if (trimmedText.startsWith("#") || trimmedText.isEmpty()) { return null; } String[] splits = trimmedText.split(";"); String productType; String name; String path; Date date1; Date date2; String regionName = null; String regionWKT = null; String[] bandNames = new String[0]; String geoInventory = null; if (splits.length >= 5) { productType = nullAware(splits[0]); name = nullAware(splits[1]); path = nullAware(splits[2]); date1 = asDate(splits[3]); date2 = asDate(splits[4]); } else { return null; } if (splits.length >= 7) { regionName = nullAware(splits[5]); regionWKT = nullAware(splits[6]); } if (splits.length >= 8) { String bandNameCSV = nullAware(splits[7]); if (bandNameCSV != null && !bandNameCSV.isEmpty()) { bandNames = bandNameCSV.split(","); } } if (splits.length >= 9) { geoInventory = nullAware(splits[8]); } return new ProductSet(productType, name, path, date1, date2, regionName, regionWKT, bandNames, geoInventory); } private  ProductSetPersistable(); static String convertToCSV(ProductSet productSet); static ProductSet convertFromCSV(String text); static final String FILENAME; static final String INDEX; }
@Test public void testConvertFromCSV_EdgeCases() throws Exception { assertNull(ProductSetPersistable.convertFromCSV("")); try { ProductSetPersistable.convertFromCSV(null); fail(); } catch (NullPointerException expected) { } }
@Test public void testConvertFromCSV() throws Exception { ProductSet productSet = ProductSetPersistable.convertFromCSV( "MER_RR__1;MERIS RR L1b 2004-2008;eodata/MER_RR__1P/r03/${yyyy}/${MM}/${dd}/.*.N1;2004-01-01;2008-12-31;WesternEurope;polygon((-7 54, -7 38.5, 5.5 38.5, 5.5 54, -7 54))"); assertNotNull(productSet); assertEquals("MER_RR__1", productSet.getProductType()); assertEquals("MERIS RR L1b 2004-2008", productSet.getName()); assertEquals("eodata/MER_RR__1P/r03/${yyyy}/${MM}/${dd}/.*.N1", productSet.getPath()); assertEquals("2004-01-01", ProductSetPersistable.format(productSet.getMinDate())); assertEquals("2008-12-31", ProductSetPersistable.format(productSet.getMaxDate())); assertEquals("WesternEurope", productSet.getRegionName()); assertEquals("polygon((-7 54, -7 38.5, 5.5 38.5, 5.5 54, -7 54))", productSet.getRegionWKT()); assertArrayEquals(new String[0], productSet.getBandNames()); productSet = ProductSetPersistable.convertFromCSV( "MER_RR__1;MERIS RR L1b 2004-2008;eodata/MER_RR__1P/r03/${yyyy}/${MM}/${dd}/.*.N1;2004-01-01;2008-12-31;WesternEurope;polygon((-7 54, -7 38.5, 5.5 38.5, 5.5 54, -7 54));a,b,c"); assertNotNull(productSet); assertEquals("MER_RR__1", productSet.getProductType()); assertEquals("MERIS RR L1b 2004-2008", productSet.getName()); assertEquals("eodata/MER_RR__1P/r03/${yyyy}/${MM}/${dd}/.*.N1", productSet.getPath()); assertEquals("2004-01-01", ProductSetPersistable.format(productSet.getMinDate())); assertEquals("2008-12-31", ProductSetPersistable.format(productSet.getMaxDate())); assertEquals("WesternEurope", productSet.getRegionName()); assertEquals("polygon((-7 54, -7 38.5, 5.5 38.5, 5.5 54, -7 54))", productSet.getRegionWKT()); assertArrayEquals(new String[]{"a", "b", "c"}, productSet.getBandNames()); productSet = ProductSetPersistable.convertFromCSV("pt;pn;pp;null;null"); assertNotNull(productSet); assertEquals("pt", productSet.getProductType()); assertEquals("pn", productSet.getName()); assertEquals("pp", productSet.getPath()); assertNull(productSet.getMinDate()); assertNull(productSet.getMaxDate()); assertNull(productSet.getRegionName()); assertNull(productSet.getRegionWKT()); assertArrayEquals(new String[0], productSet.getBandNames()); productSet = ProductSetPersistable.convertFromCSV("pt;pn;pp;null;null;null;null"); assertNotNull(productSet); assertEquals("pt", productSet.getProductType()); assertEquals("pn", productSet.getName()); assertEquals("pp", productSet.getPath()); assertNull(productSet.getMinDate()); assertNull(productSet.getMaxDate()); assertNull(productSet.getRegionName()); assertNull(productSet.getRegionWKT()); assertArrayEquals(new String[0], productSet.getBandNames()); productSet = ProductSetPersistable.convertFromCSV("pt;pn;pp;null"); assertNull(productSet); }