From fd1d487267b46639e1c7609a44066809eda1cd7b Mon Sep 17 00:00:00 2001 From: Nathanael Jourdane Date: Tue, 24 May 2016 13:10:47 +0200 Subject: [PATCH] Immplement unit test for granule and make it works. --- src/main/java/eu/omp/irap/vespa/epntapclient/EpnTapConnection.java | 21 +++++---------------- src/main/java/eu/omp/irap/vespa/epntapclient/granule/GranuleCtrl.java | 36 ++++++++++++------------------------ src/test/java/eu/omp/irap/vespa/epntapclient/granule/GranuleCtrlTest.java | 34 ++++++++++++++++++++++++++-------- src/test/java/eu/omp/irap/vespa/epntapclient/granule/GranuleTest.java | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------- 4 files changed, 123 insertions(+), 59 deletions(-) diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/EpnTapConnection.java b/src/main/java/eu/omp/irap/vespa/epntapclient/EpnTapConnection.java index 6695667..0ceb005 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/EpnTapConnection.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/EpnTapConnection.java @@ -16,7 +16,6 @@ package eu.omp.irap.vespa.epntapclient; -import java.text.ParseException; import java.util.ArrayList; import java.util.List; @@ -31,7 +30,6 @@ import eu.omp.irap.vespa.epntapclient.voresource.model.Resource; import eu.omp.irap.vespa.epntapclient.votable.model.VOTABLE; import eu.omp.irap.vespa.votable.controller.VOTableController; import eu.omp.irap.vespa.votable.controller.VOTableException; -import eu.omp.irap.vespa.votable.controller.VOTableException.CantParseVOTableException; import eu.omp.irap.vespa.votable.utils.Debug; import eu.omp.irap.vespa.votable.utils.StringJoiner; import eu.omp.irap.vespa.votable.votabledata.VOTableData; @@ -124,19 +122,14 @@ public class EpnTapConnection implements EpnTapInterface { // *** Queries *** @Override - public List sendADQLQuery(String tapURL, String adqlQuery) - throws VOTableException { + public List sendADQLQuery(String tapURL, String adqlQuery) throws VOTableException { VOTableController voTableCtrl = new VOTableController(tapURL, adqlQuery); voTableCtrl.readTable(); VOTableData data = voTableCtrl.getVOTableData(); List granules; - try { - GranuleCtrl gc = new GranuleCtrl(data); - granules = gc.getGranules(); - } catch (ParseException e) { - throw new CantParseVOTableException("Parsing error on a granule.", e); - } + GranuleCtrl gc = new GranuleCtrl(data); + granules = gc.getGranules(); return granules; } @@ -150,12 +143,8 @@ public class EpnTapConnection implements EpnTapInterface { Debug.writeObject("data", data); List granules; - try { - GranuleCtrl gc = new GranuleCtrl(data); - granules = gc.getGranules(); - } catch (ParseException e) { - throw new CantParseVOTableException("Parsing error on a granule.", e); - } + GranuleCtrl gc = new GranuleCtrl(data); + granules = gc.getGranules(); return granules; } diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/granule/GranuleCtrl.java b/src/main/java/eu/omp/irap/vespa/epntapclient/granule/GranuleCtrl.java index 786aba0..b3ceb81 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/granule/GranuleCtrl.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/granule/GranuleCtrl.java @@ -21,7 +21,6 @@ import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; -import java.util.logging.Level; import java.util.logging.Logger; import eu.omp.irap.vespa.votable.controller.VOTableException.CanNotParseDataException; @@ -56,11 +55,9 @@ public class GranuleCtrl { * * @param rowId the row identified * @return the granule at the position rowId in the VOTable data. - * @throws ParseException If an element can not be parsed (ie., a date). * @throws CanNotParseDataException The column name was not found in the list. */ - public Granule getGranuleFromVOTableRow(int rowId) - throws ParseException, CanNotParseDataException { + public Granule getGranuleFromVOTableRow(int rowId) throws CanNotParseDataException { //@noformat Granule g = new Granule(parseString(rowId, GranuleEnum.GRANULE_UID)); g.setGranuleGid(parseString(rowId, GranuleEnum.GRANULE_GID)); @@ -86,7 +83,7 @@ public class GranuleCtrl { g.setC2Max(parseDouble(rowId, GranuleEnum.C2MAX)); g.setC3Min(parseDouble(rowId, GranuleEnum.C3MIN)); g.setC3Max(parseDouble(rowId, GranuleEnum.C3MAX)); - g.setsRegion(parseString(rowId, GranuleEnum.S_REGION)); + g.setSRegion(parseString(rowId, GranuleEnum.S_REGION)); g.setC1ResolMin(parseDouble(rowId, GranuleEnum.C1_RESOL_MIN)); g.setC1ResolMax(parseDouble(rowId, GranuleEnum.C1_RESOL_MAX)); g.setC2ResolMin(parseDouble(rowId, GranuleEnum.C2_RESOL_MIN)); @@ -118,12 +115,12 @@ public class GranuleCtrl { /** * @return A list of Granules from a VOTable, where each Granule is a row of the VOTable data. - * @throws ParseException If the granule can not be parsed. - * @throws CanNotParseDataException The column name was not found in the list. + * @throws CanNotParseDataException If the granule can not be parsed or the column name was not + * found in the list. */ - public List getGranules() throws ParseException, CanNotParseDataException { + public List getGranules() throws CanNotParseDataException { if (!isV2()) { - throw new IllegalArgumentException( + throw new CanNotParseDataException( "The EPN-CORE is not v2, which is the only suported version"); } List granules = new ArrayList<>(); @@ -149,17 +146,16 @@ public class GranuleCtrl { * @param rowId The row identifier * @param granule The Granule enumeration, representing the column name. * @return The value as Date. - * @throws ParseException The date format is not correct. * @throws CanNotParseDataException The column name was not found in the list. */ - private Date parseDate(int rowId, GranuleEnum granule) - throws ParseException, CanNotParseDataException { + private Date parseDate(int rowId, GranuleEnum granule) throws CanNotParseDataException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd"); Date date = new Date(); try { date = sdf.parse((String) data.getCell(rowId, granule.toString())); - } catch (IllegalArgumentException e) { - LOGGER.log(Level.WARNING, String.format(ERROR_MSG, granule, rowId, "empty date"), e); + } catch (ParseException e) { + throw new CanNotParseDataException("The date " + granule + " in the row " + rowId + + " can not be parsed.", e); } return date; @@ -178,11 +174,9 @@ public class GranuleCtrl { Object lObj = data.getCell(rowId, granule.toString()); if (lObj instanceof Double) { d = (Double) lObj; - } - if (lObj instanceof Float) { + } else if (lObj instanceof Float) { d = new Double((Float) lObj); } - return d == null ? Double.NaN : d; } @@ -207,13 +201,7 @@ public class GranuleCtrl { * @throws CanNotParseDataException The column name was not found in the list. */ private String parseString(int rowId, GranuleEnum granule) throws CanNotParseDataException { - String res = ""; - try { - res = (String) data.getCell(rowId, granule.toString()); - } catch (IllegalArgumentException e) { - LOGGER.log(Level.WARNING, String.format(ERROR_MSG, granule, rowId, "empty string"), e); - } - return res; + return (String) data.getCell(rowId, granule.toString()); } } diff --git a/src/test/java/eu/omp/irap/vespa/epntapclient/granule/GranuleCtrlTest.java b/src/test/java/eu/omp/irap/vespa/epntapclient/granule/GranuleCtrlTest.java index 62b866a..742b872 100644 --- a/src/test/java/eu/omp/irap/vespa/epntapclient/granule/GranuleCtrlTest.java +++ b/src/test/java/eu/omp/irap/vespa/epntapclient/granule/GranuleCtrlTest.java @@ -16,9 +16,11 @@ package eu.omp.irap.vespa.epntapclient.granule; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import java.text.ParseException; +import java.util.List; import org.junit.Test; @@ -34,19 +36,35 @@ public class GranuleCtrlTest { public GranuleCtrlTest() { - granuleCtrl = new GranuleCtrl(VoTableDataTest.createvoTableData()); + granuleCtrl = new GranuleCtrl(VoTableDataTest.createVoTableData()); } @Test - public void parseStringTest() { + public void getGranulesTest() { + List granules = null; try { - granuleCtrl.getGranuleFromVOTableRow(0); - granuleCtrl.getGranuleFromVOTableRow(1); - } catch (ParseException e) { - fail("getGranuleFromVOTableRow(int) failed." + e.getMessage()); + granules = granuleCtrl.getGranules(); } catch (CanNotParseDataException e) { - fail("new GranuleCtrl(VOtable) failed: " + e.getMessage()); + fail("Can not parse granule: " + e.getMessage()); + } + assertNotNull(granules); + assertTrue(granules.get(0).equals(GranuleTest.createGranule1())); + assertTrue(granules.get(1).equals(GranuleTest.createGranule2())); + } + @Test + public void parseStringTest() { + Granule g1 = null; + Granule g2 = null; + try { + g1 = granuleCtrl.getGranuleFromVOTableRow(0); + g2 = granuleCtrl.getGranuleFromVOTableRow(1); + } catch (CanNotParseDataException e) { + fail("Can not parse granule: " + e.getMessage()); } + assertNotNull(g1); + assertNotNull(g2); + assertTrue(g1.equals(GranuleTest.createGranule1())); + assertTrue(g2.equals(GranuleTest.createGranule2())); } } diff --git a/src/test/java/eu/omp/irap/vespa/epntapclient/granule/GranuleTest.java b/src/test/java/eu/omp/irap/vespa/epntapclient/granule/GranuleTest.java index 95cc05f..baa7afb 100644 --- a/src/test/java/eu/omp/irap/vespa/epntapclient/granule/GranuleTest.java +++ b/src/test/java/eu/omp/irap/vespa/epntapclient/granule/GranuleTest.java @@ -19,7 +19,10 @@ package eu.omp.irap.vespa.epntapclient.granule; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.Date; +import java.util.logging.Logger; import org.junit.Test; @@ -28,12 +31,78 @@ import org.junit.Test; */ public class GranuleTest { - public static Granule createCompleteGranule() { - Granule completeGranule = new Granule("Mercury"); + /** The logger for the class GranuleTest. */ + private static final Logger LOGGER = Logger.getLogger(GranuleTest.class.getName()); + + + public static Granule createGranule1() { + Date testDate = null; + try { + testDate = new SimpleDateFormat("yyyy-mm-dd").parse("2015-08-20"); + } catch (ParseException e) { + LOGGER.warning("Can not parse the test date."); + } + + Granule completeGranule = new Granule("Uranus"); + completeGranule.setGranuleGid("Planet"); + completeGranule.setObsId("7"); + completeGranule.setDataproductType("ca"); + completeGranule.setTargetName("Uranus"); + completeGranule.setTargetClass("planet"); + completeGranule.setTimeMin(Double.NaN); + completeGranule.setTimeMax(Double.NaN); + completeGranule.setTimeSamplingStepMin(Double.NaN); + completeGranule.setTimeSamplingStepMax(Double.NaN); + completeGranule.setTimeExpMin(Double.NaN); + completeGranule.setTimeExpMax(Double.NaN); + completeGranule.setSpectralRangeMin(Double.NaN); + completeGranule.setSpectralRangeMax(Double.NaN); + completeGranule.setSpectralResolutionMin(Double.NaN); + completeGranule.setSpectralResolutionMax(Double.NaN); + completeGranule.setC1Min(Double.NaN); + completeGranule.setC1Max(Double.NaN); + completeGranule.setC2Min(Double.NaN); + completeGranule.setC2Max(Double.NaN); + completeGranule.setC3Min(Double.NaN); + completeGranule.setC3Max(Double.NaN); + completeGranule.setSRegion(""); + completeGranule.setC1ResolMin(Double.NaN); + completeGranule.setC1ResolMax(Double.NaN); + completeGranule.setC2ResolMin(Double.NaN); + completeGranule.setC2ResolMax(Double.NaN); + completeGranule.setC3ResolMin(Double.NaN); + completeGranule.setC3ResolMax(Double.NaN); + completeGranule.setSpatialFrameType("celestial"); + completeGranule.setIncidenceMin(Double.NaN); + completeGranule.setIncidenceMax(Double.NaN); + completeGranule.setEmergenceMin(Double.NaN); + completeGranule.setEmergenceMax(Double.NaN); + completeGranule.setPhaseMin(Double.NaN); + completeGranule.setPhaseMax(Double.NaN); + completeGranule.setInstrumentHostName(""); + completeGranule.setInstrumentName(""); + completeGranule.setMeasurementType("\"phys.mass\" \"phys.size.radius\""); + completeGranule.setProcessingLevel(5); + completeGranule.setCreationDate(testDate); + completeGranule.setModificationDate(testDate); + completeGranule.setReleaseDate(testDate); + completeGranule.setServiceTitle("Planets"); + return completeGranule; + } + + public static Granule createGranule2() { + Date testDate = null; + try { + testDate = new SimpleDateFormat("yyyy-mm-dd").parse("2015-08-20"); + } catch (ParseException e) { + LOGGER.warning("Can not parse the test date."); + } + + Granule completeGranule = new Granule("Neptune"); completeGranule.setGranuleGid("Planet"); - completeGranule.setObsId("1"); + completeGranule.setObsId("8"); completeGranule.setDataproductType("ca"); - completeGranule.setTargetName("Mercury"); + completeGranule.setTargetName("Neptune"); completeGranule.setTargetClass("planet"); completeGranule.setTimeMin(Double.NaN); completeGranule.setTimeMax(Double.NaN); @@ -51,7 +120,7 @@ public class GranuleTest { completeGranule.setC2Max(Double.NaN); completeGranule.setC3Min(Double.NaN); completeGranule.setC3Max(Double.NaN); - completeGranule.setsRegion(""); + completeGranule.setSRegion(""); completeGranule.setC1ResolMin(Double.NaN); completeGranule.setC1ResolMax(Double.NaN); completeGranule.setC2ResolMin(Double.NaN); @@ -69,9 +138,9 @@ public class GranuleTest { completeGranule.setInstrumentName(""); completeGranule.setMeasurementType("\"phys.mass\" \"phys.size.radius\""); completeGranule.setProcessingLevel(5); - completeGranule.setCreationDate(new Date(1421755709)); - completeGranule.setModificationDate(new Date(1421755709)); - completeGranule.setReleaseDate(new Date(1421755709)); + completeGranule.setCreationDate(testDate); + completeGranule.setModificationDate(testDate); + completeGranule.setReleaseDate(testDate); completeGranule.setServiceTitle("Planets"); return completeGranule; } @@ -83,13 +152,13 @@ public class GranuleTest { } @Test - public static void isNotValidTest() { + public void isNotValidTest() { assertFalse("The incomplete granule is valid.", createIncompleteGranule().isValid()); } @Test - public static void isValidTest() { - assertTrue("The complete granule is not valid.", createCompleteGranule().isValid()); + public void isValidTest() { + assertTrue("The complete granule is not valid.", createGranule1().isValid()); } } -- libgit2 0.21.2