Commit fd1d487267b46639e1c7609a44066809eda1cd7b
1 parent
49c4255b
Exists in
master
Immplement unit test for granule and make it works.
Showing
4 changed files
with
123 additions
and
59 deletions
Show diff stats
src/main/java/eu/omp/irap/vespa/epntapclient/EpnTapConnection.java
... | ... | @@ -16,7 +16,6 @@ |
16 | 16 | |
17 | 17 | package eu.omp.irap.vespa.epntapclient; |
18 | 18 | |
19 | -import java.text.ParseException; | |
20 | 19 | import java.util.ArrayList; |
21 | 20 | import java.util.List; |
22 | 21 | |
... | ... | @@ -31,7 +30,6 @@ import eu.omp.irap.vespa.epntapclient.voresource.model.Resource; |
31 | 30 | import eu.omp.irap.vespa.epntapclient.votable.model.VOTABLE; |
32 | 31 | import eu.omp.irap.vespa.votable.controller.VOTableController; |
33 | 32 | import eu.omp.irap.vespa.votable.controller.VOTableException; |
34 | -import eu.omp.irap.vespa.votable.controller.VOTableException.CantParseVOTableException; | |
35 | 33 | import eu.omp.irap.vespa.votable.utils.Debug; |
36 | 34 | import eu.omp.irap.vespa.votable.utils.StringJoiner; |
37 | 35 | import eu.omp.irap.vespa.votable.votabledata.VOTableData; |
... | ... | @@ -124,19 +122,14 @@ public class EpnTapConnection implements EpnTapInterface { |
124 | 122 | // *** Queries *** |
125 | 123 | |
126 | 124 | @Override |
127 | - public List<Granule> sendADQLQuery(String tapURL, String adqlQuery) | |
128 | - throws VOTableException { | |
125 | + public List<Granule> sendADQLQuery(String tapURL, String adqlQuery) throws VOTableException { | |
129 | 126 | VOTableController voTableCtrl = new VOTableController(tapURL, adqlQuery); |
130 | 127 | voTableCtrl.readTable(); |
131 | 128 | VOTableData data = voTableCtrl.getVOTableData(); |
132 | 129 | |
133 | 130 | List<Granule> granules; |
134 | - try { | |
135 | - GranuleCtrl gc = new GranuleCtrl(data); | |
136 | - granules = gc.getGranules(); | |
137 | - } catch (ParseException e) { | |
138 | - throw new CantParseVOTableException("Parsing error on a granule.", e); | |
139 | - } | |
131 | + GranuleCtrl gc = new GranuleCtrl(data); | |
132 | + granules = gc.getGranules(); | |
140 | 133 | return granules; |
141 | 134 | } |
142 | 135 | |
... | ... | @@ -150,12 +143,8 @@ public class EpnTapConnection implements EpnTapInterface { |
150 | 143 | Debug.writeObject("data", data); |
151 | 144 | |
152 | 145 | List<Granule> granules; |
153 | - try { | |
154 | - GranuleCtrl gc = new GranuleCtrl(data); | |
155 | - granules = gc.getGranules(); | |
156 | - } catch (ParseException e) { | |
157 | - throw new CantParseVOTableException("Parsing error on a granule.", e); | |
158 | - } | |
146 | + GranuleCtrl gc = new GranuleCtrl(data); | |
147 | + granules = gc.getGranules(); | |
159 | 148 | return granules; |
160 | 149 | } |
161 | 150 | ... | ... |
src/main/java/eu/omp/irap/vespa/epntapclient/granule/GranuleCtrl.java
... | ... | @@ -21,7 +21,6 @@ import java.text.SimpleDateFormat; |
21 | 21 | import java.util.ArrayList; |
22 | 22 | import java.util.Date; |
23 | 23 | import java.util.List; |
24 | -import java.util.logging.Level; | |
25 | 24 | import java.util.logging.Logger; |
26 | 25 | |
27 | 26 | import eu.omp.irap.vespa.votable.controller.VOTableException.CanNotParseDataException; |
... | ... | @@ -56,11 +55,9 @@ public class GranuleCtrl { |
56 | 55 | * |
57 | 56 | * @param rowId the row identified |
58 | 57 | * @return the granule at the position rowId in the VOTable data. |
59 | - * @throws ParseException If an element can not be parsed (ie., a date). | |
60 | 58 | * @throws CanNotParseDataException The column name was not found in the list. |
61 | 59 | */ |
62 | - public Granule getGranuleFromVOTableRow(int rowId) | |
63 | - throws ParseException, CanNotParseDataException { | |
60 | + public Granule getGranuleFromVOTableRow(int rowId) throws CanNotParseDataException { | |
64 | 61 | //@noformat |
65 | 62 | Granule g = new Granule(parseString(rowId, GranuleEnum.GRANULE_UID)); |
66 | 63 | g.setGranuleGid(parseString(rowId, GranuleEnum.GRANULE_GID)); |
... | ... | @@ -86,7 +83,7 @@ public class GranuleCtrl { |
86 | 83 | g.setC2Max(parseDouble(rowId, GranuleEnum.C2MAX)); |
87 | 84 | g.setC3Min(parseDouble(rowId, GranuleEnum.C3MIN)); |
88 | 85 | g.setC3Max(parseDouble(rowId, GranuleEnum.C3MAX)); |
89 | - g.setsRegion(parseString(rowId, GranuleEnum.S_REGION)); | |
86 | + g.setSRegion(parseString(rowId, GranuleEnum.S_REGION)); | |
90 | 87 | g.setC1ResolMin(parseDouble(rowId, GranuleEnum.C1_RESOL_MIN)); |
91 | 88 | g.setC1ResolMax(parseDouble(rowId, GranuleEnum.C1_RESOL_MAX)); |
92 | 89 | g.setC2ResolMin(parseDouble(rowId, GranuleEnum.C2_RESOL_MIN)); |
... | ... | @@ -118,12 +115,12 @@ public class GranuleCtrl { |
118 | 115 | |
119 | 116 | /** |
120 | 117 | * @return A list of Granules from a VOTable, where each Granule is a row of the VOTable data. |
121 | - * @throws ParseException If the granule can not be parsed. | |
122 | - * @throws CanNotParseDataException The column name was not found in the list. | |
118 | + * @throws CanNotParseDataException If the granule can not be parsed or the column name was not | |
119 | + * found in the list. | |
123 | 120 | */ |
124 | - public List<Granule> getGranules() throws ParseException, CanNotParseDataException { | |
121 | + public List<Granule> getGranules() throws CanNotParseDataException { | |
125 | 122 | if (!isV2()) { |
126 | - throw new IllegalArgumentException( | |
123 | + throw new CanNotParseDataException( | |
127 | 124 | "The EPN-CORE is not v2, which is the only suported version"); |
128 | 125 | } |
129 | 126 | List<Granule> granules = new ArrayList<>(); |
... | ... | @@ -149,17 +146,16 @@ public class GranuleCtrl { |
149 | 146 | * @param rowId The row identifier |
150 | 147 | * @param granule The Granule enumeration, representing the column name. |
151 | 148 | * @return The value as Date. |
152 | - * @throws ParseException The date format is not correct. | |
153 | 149 | * @throws CanNotParseDataException The column name was not found in the list. |
154 | 150 | */ |
155 | - private Date parseDate(int rowId, GranuleEnum granule) | |
156 | - throws ParseException, CanNotParseDataException { | |
151 | + private Date parseDate(int rowId, GranuleEnum granule) throws CanNotParseDataException { | |
157 | 152 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd"); |
158 | 153 | Date date = new Date(); |
159 | 154 | try { |
160 | 155 | date = sdf.parse((String) data.getCell(rowId, granule.toString())); |
161 | - } catch (IllegalArgumentException e) { | |
162 | - LOGGER.log(Level.WARNING, String.format(ERROR_MSG, granule, rowId, "empty date"), e); | |
156 | + } catch (ParseException e) { | |
157 | + throw new CanNotParseDataException("The date " + granule + " in the row " + rowId | |
158 | + + " can not be parsed.", e); | |
163 | 159 | } |
164 | 160 | |
165 | 161 | return date; |
... | ... | @@ -178,11 +174,9 @@ public class GranuleCtrl { |
178 | 174 | Object lObj = data.getCell(rowId, granule.toString()); |
179 | 175 | if (lObj instanceof Double) { |
180 | 176 | d = (Double) lObj; |
181 | - } | |
182 | - if (lObj instanceof Float) { | |
177 | + } else if (lObj instanceof Float) { | |
183 | 178 | d = new Double((Float) lObj); |
184 | 179 | } |
185 | - | |
186 | 180 | return d == null ? Double.NaN : d; |
187 | 181 | } |
188 | 182 | |
... | ... | @@ -207,13 +201,7 @@ public class GranuleCtrl { |
207 | 201 | * @throws CanNotParseDataException The column name was not found in the list. |
208 | 202 | */ |
209 | 203 | private String parseString(int rowId, GranuleEnum granule) throws CanNotParseDataException { |
210 | - String res = ""; | |
211 | - try { | |
212 | - res = (String) data.getCell(rowId, granule.toString()); | |
213 | - } catch (IllegalArgumentException e) { | |
214 | - LOGGER.log(Level.WARNING, String.format(ERROR_MSG, granule, rowId, "empty string"), e); | |
215 | - } | |
216 | - return res; | |
204 | + return (String) data.getCell(rowId, granule.toString()); | |
217 | 205 | } |
218 | 206 | |
219 | 207 | } | ... | ... |
src/test/java/eu/omp/irap/vespa/epntapclient/granule/GranuleCtrlTest.java
... | ... | @@ -16,9 +16,11 @@ |
16 | 16 | |
17 | 17 | package eu.omp.irap.vespa.epntapclient.granule; |
18 | 18 | |
19 | +import static org.junit.Assert.assertNotNull; | |
20 | +import static org.junit.Assert.assertTrue; | |
19 | 21 | import static org.junit.Assert.fail; |
20 | 22 | |
21 | -import java.text.ParseException; | |
23 | +import java.util.List; | |
22 | 24 | |
23 | 25 | import org.junit.Test; |
24 | 26 | |
... | ... | @@ -34,19 +36,35 @@ public class GranuleCtrlTest { |
34 | 36 | |
35 | 37 | |
36 | 38 | public GranuleCtrlTest() { |
37 | - granuleCtrl = new GranuleCtrl(VoTableDataTest.createvoTableData()); | |
39 | + granuleCtrl = new GranuleCtrl(VoTableDataTest.createVoTableData()); | |
38 | 40 | } |
39 | 41 | |
40 | 42 | @Test |
41 | - public void parseStringTest() { | |
43 | + public void getGranulesTest() { | |
44 | + List<Granule> granules = null; | |
42 | 45 | try { |
43 | - granuleCtrl.getGranuleFromVOTableRow(0); | |
44 | - granuleCtrl.getGranuleFromVOTableRow(1); | |
45 | - } catch (ParseException e) { | |
46 | - fail("getGranuleFromVOTableRow(int) failed." + e.getMessage()); | |
46 | + granules = granuleCtrl.getGranules(); | |
47 | 47 | } catch (CanNotParseDataException e) { |
48 | - fail("new GranuleCtrl(VOtable) failed: " + e.getMessage()); | |
48 | + fail("Can not parse granule: " + e.getMessage()); | |
49 | + } | |
50 | + assertNotNull(granules); | |
51 | + assertTrue(granules.get(0).equals(GranuleTest.createGranule1())); | |
52 | + assertTrue(granules.get(1).equals(GranuleTest.createGranule2())); | |
53 | + } | |
49 | 54 | |
55 | + @Test | |
56 | + public void parseStringTest() { | |
57 | + Granule g1 = null; | |
58 | + Granule g2 = null; | |
59 | + try { | |
60 | + g1 = granuleCtrl.getGranuleFromVOTableRow(0); | |
61 | + g2 = granuleCtrl.getGranuleFromVOTableRow(1); | |
62 | + } catch (CanNotParseDataException e) { | |
63 | + fail("Can not parse granule: " + e.getMessage()); | |
50 | 64 | } |
65 | + assertNotNull(g1); | |
66 | + assertNotNull(g2); | |
67 | + assertTrue(g1.equals(GranuleTest.createGranule1())); | |
68 | + assertTrue(g2.equals(GranuleTest.createGranule2())); | |
51 | 69 | } |
52 | 70 | } | ... | ... |
src/test/java/eu/omp/irap/vespa/epntapclient/granule/GranuleTest.java
... | ... | @@ -19,7 +19,10 @@ package eu.omp.irap.vespa.epntapclient.granule; |
19 | 19 | import static org.junit.Assert.assertFalse; |
20 | 20 | import static org.junit.Assert.assertTrue; |
21 | 21 | |
22 | +import java.text.ParseException; | |
23 | +import java.text.SimpleDateFormat; | |
22 | 24 | import java.util.Date; |
25 | +import java.util.logging.Logger; | |
23 | 26 | |
24 | 27 | import org.junit.Test; |
25 | 28 | |
... | ... | @@ -28,12 +31,78 @@ import org.junit.Test; |
28 | 31 | */ |
29 | 32 | public class GranuleTest { |
30 | 33 | |
31 | - public static Granule createCompleteGranule() { | |
32 | - Granule completeGranule = new Granule("Mercury"); | |
34 | + /** The logger for the class GranuleTest. */ | |
35 | + private static final Logger LOGGER = Logger.getLogger(GranuleTest.class.getName()); | |
36 | + | |
37 | + | |
38 | + public static Granule createGranule1() { | |
39 | + Date testDate = null; | |
40 | + try { | |
41 | + testDate = new SimpleDateFormat("yyyy-mm-dd").parse("2015-08-20"); | |
42 | + } catch (ParseException e) { | |
43 | + LOGGER.warning("Can not parse the test date."); | |
44 | + } | |
45 | + | |
46 | + Granule completeGranule = new Granule("Uranus"); | |
47 | + completeGranule.setGranuleGid("Planet"); | |
48 | + completeGranule.setObsId("7"); | |
49 | + completeGranule.setDataproductType("ca"); | |
50 | + completeGranule.setTargetName("Uranus"); | |
51 | + completeGranule.setTargetClass("planet"); | |
52 | + completeGranule.setTimeMin(Double.NaN); | |
53 | + completeGranule.setTimeMax(Double.NaN); | |
54 | + completeGranule.setTimeSamplingStepMin(Double.NaN); | |
55 | + completeGranule.setTimeSamplingStepMax(Double.NaN); | |
56 | + completeGranule.setTimeExpMin(Double.NaN); | |
57 | + completeGranule.setTimeExpMax(Double.NaN); | |
58 | + completeGranule.setSpectralRangeMin(Double.NaN); | |
59 | + completeGranule.setSpectralRangeMax(Double.NaN); | |
60 | + completeGranule.setSpectralResolutionMin(Double.NaN); | |
61 | + completeGranule.setSpectralResolutionMax(Double.NaN); | |
62 | + completeGranule.setC1Min(Double.NaN); | |
63 | + completeGranule.setC1Max(Double.NaN); | |
64 | + completeGranule.setC2Min(Double.NaN); | |
65 | + completeGranule.setC2Max(Double.NaN); | |
66 | + completeGranule.setC3Min(Double.NaN); | |
67 | + completeGranule.setC3Max(Double.NaN); | |
68 | + completeGranule.setSRegion(""); | |
69 | + completeGranule.setC1ResolMin(Double.NaN); | |
70 | + completeGranule.setC1ResolMax(Double.NaN); | |
71 | + completeGranule.setC2ResolMin(Double.NaN); | |
72 | + completeGranule.setC2ResolMax(Double.NaN); | |
73 | + completeGranule.setC3ResolMin(Double.NaN); | |
74 | + completeGranule.setC3ResolMax(Double.NaN); | |
75 | + completeGranule.setSpatialFrameType("celestial"); | |
76 | + completeGranule.setIncidenceMin(Double.NaN); | |
77 | + completeGranule.setIncidenceMax(Double.NaN); | |
78 | + completeGranule.setEmergenceMin(Double.NaN); | |
79 | + completeGranule.setEmergenceMax(Double.NaN); | |
80 | + completeGranule.setPhaseMin(Double.NaN); | |
81 | + completeGranule.setPhaseMax(Double.NaN); | |
82 | + completeGranule.setInstrumentHostName(""); | |
83 | + completeGranule.setInstrumentName(""); | |
84 | + completeGranule.setMeasurementType("\"phys.mass\" \"phys.size.radius\""); | |
85 | + completeGranule.setProcessingLevel(5); | |
86 | + completeGranule.setCreationDate(testDate); | |
87 | + completeGranule.setModificationDate(testDate); | |
88 | + completeGranule.setReleaseDate(testDate); | |
89 | + completeGranule.setServiceTitle("Planets"); | |
90 | + return completeGranule; | |
91 | + } | |
92 | + | |
93 | + public static Granule createGranule2() { | |
94 | + Date testDate = null; | |
95 | + try { | |
96 | + testDate = new SimpleDateFormat("yyyy-mm-dd").parse("2015-08-20"); | |
97 | + } catch (ParseException e) { | |
98 | + LOGGER.warning("Can not parse the test date."); | |
99 | + } | |
100 | + | |
101 | + Granule completeGranule = new Granule("Neptune"); | |
33 | 102 | completeGranule.setGranuleGid("Planet"); |
34 | - completeGranule.setObsId("1"); | |
103 | + completeGranule.setObsId("8"); | |
35 | 104 | completeGranule.setDataproductType("ca"); |
36 | - completeGranule.setTargetName("Mercury"); | |
105 | + completeGranule.setTargetName("Neptune"); | |
37 | 106 | completeGranule.setTargetClass("planet"); |
38 | 107 | completeGranule.setTimeMin(Double.NaN); |
39 | 108 | completeGranule.setTimeMax(Double.NaN); |
... | ... | @@ -51,7 +120,7 @@ public class GranuleTest { |
51 | 120 | completeGranule.setC2Max(Double.NaN); |
52 | 121 | completeGranule.setC3Min(Double.NaN); |
53 | 122 | completeGranule.setC3Max(Double.NaN); |
54 | - completeGranule.setsRegion(""); | |
123 | + completeGranule.setSRegion(""); | |
55 | 124 | completeGranule.setC1ResolMin(Double.NaN); |
56 | 125 | completeGranule.setC1ResolMax(Double.NaN); |
57 | 126 | completeGranule.setC2ResolMin(Double.NaN); |
... | ... | @@ -69,9 +138,9 @@ public class GranuleTest { |
69 | 138 | completeGranule.setInstrumentName(""); |
70 | 139 | completeGranule.setMeasurementType("\"phys.mass\" \"phys.size.radius\""); |
71 | 140 | completeGranule.setProcessingLevel(5); |
72 | - completeGranule.setCreationDate(new Date(1421755709)); | |
73 | - completeGranule.setModificationDate(new Date(1421755709)); | |
74 | - completeGranule.setReleaseDate(new Date(1421755709)); | |
141 | + completeGranule.setCreationDate(testDate); | |
142 | + completeGranule.setModificationDate(testDate); | |
143 | + completeGranule.setReleaseDate(testDate); | |
75 | 144 | completeGranule.setServiceTitle("Planets"); |
76 | 145 | return completeGranule; |
77 | 146 | } |
... | ... | @@ -83,13 +152,13 @@ public class GranuleTest { |
83 | 152 | } |
84 | 153 | |
85 | 154 | @Test |
86 | - public static void isNotValidTest() { | |
155 | + public void isNotValidTest() { | |
87 | 156 | assertFalse("The incomplete granule is valid.", createIncompleteGranule().isValid()); |
88 | 157 | } |
89 | 158 | |
90 | 159 | @Test |
91 | - public static void isValidTest() { | |
92 | - assertTrue("The complete granule is not valid.", createCompleteGranule().isValid()); | |
160 | + public void isValidTest() { | |
161 | + assertTrue("The complete granule is not valid.", createGranule1().isValid()); | |
93 | 162 | } |
94 | 163 | |
95 | 164 | } | ... | ... |