From 74cbf43dfafafc5653ccdb4b09b71a9d55adca0a Mon Sep 17 00:00:00 2001 From: Usman Ali Date: Fri, 10 Jan 2020 02:39:43 +0500 Subject: [PATCH 01/33] ignore tests if ORC certificate url not available --- .../domain/orc/TestORCCertificateImporterJSON.java | 5 +++-- .../domain/orc/TestORCCertificateImporterRMS.java | 5 +++-- .../sailing/domain/orc/TestORCPerformanceCurve.java | 4 +++- .../orc/TestORCPublicCertificateDatabase.java | 13 +++++++------ 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java index 59dfc372fee..0f4b4788e59 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java @@ -9,7 +9,7 @@ import java.io.IOException; import java.net.URL; import org.json.simple.parser.ParseException; -import org.junit.Ignore; +import org.junit.Assume; import org.junit.Test; import com.sap.sailing.domain.common.orc.ORCCertificate; @@ -27,10 +27,11 @@ public class TestORCCertificateImporterJSON { assertNotNull(milan); } - @Ignore("Certificate used for testing no longer valid after 2019") +// @Ignore("Certificate used for testing no longer valid after 2019") @Test public void testSimpleOnlineJSONFileRead() throws IOException, ParseException { ORCCertificatesCollection importer = ORCCertificatesImporter.INSTANCE.read(new URL("https://data.orc.org/public/WPub.dll?action=DownRMS&CountryId=GER&ext=json").openStream()); + Assume.assumeTrue(importer.getCertificateIds().iterator().hasNext()); ORCCertificate swan = importer.getCertificateById("GER140849GER5335"); ORCCertificate moana = importer.getCertificateById("GER140772GER5549"); assertNotNull(swan); diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java index 04fb90f60d6..bdb042e01b9 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java @@ -10,7 +10,7 @@ import java.net.URL; import java.util.Collection; import org.json.simple.parser.ParseException; -import org.junit.Ignore; +import org.junit.Assume; import org.junit.Test; import com.sap.sailing.domain.common.orc.ORCCertificate; @@ -27,10 +27,11 @@ public class TestORCCertificateImporterRMS { assertNotNull(milan); } - @Ignore("Certificate used for testing no longer valid after 2019") +// @Ignore("Certificate used for testing no longer valid after 2019") @Test public void testSimpleOnlineRMSFileRead() throws IOException, ParseException { ORCCertificatesCollection certificates = ORCCertificatesImporter.INSTANCE.read(new URL("https://data.orc.org/public/WPub.dll?action=DownRMS&CountryId=GER&ext=rms").openStream()); + Assume.assumeTrue(certificates.getCertificateIds().iterator().hasNext()); ORCCertificate swan = certificates.getCertificateById("GER140849GER5335"); ORCCertificate moana = certificates.getCertificateById("GER140772GER5549"); assertNotNull(swan); diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java index adb7bfc2944..ea96e17baf5 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java @@ -16,6 +16,7 @@ import org.apache.commons.math.FunctionEvaluationException; import org.apache.commons.math.MaxIterationsExceededException; import org.json.simple.parser.ParseException; import org.junit.Assert; +import org.junit.Assume; import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Rule; @@ -238,9 +239,10 @@ public class TestORCPerformanceCurve { /** * Tests to make sure, that the structure of the certificate files didn't change and performance curves can be built */ - @Ignore("Certificate used for testing no longer valid after 2019") +// @Ignore("Certificate used for testing no longer valid after 2019") @Test public void testOnlineImport() throws FunctionEvaluationException { + Assume.assumeTrue(importerOnline.getCertificateIds().iterator().hasNext()); assertFalse(Util.isEmpty(importerOnline.getCertificateIds())); for (final ORCCertificate certificate : importerOnline.getCertificates()) { new ORCPerformanceCurveImpl(certificate, alturaCourse); diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java index 03180de4dfa..f984b56965c 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java @@ -12,8 +12,8 @@ import java.util.UUID; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; +import org.junit.Assume; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import com.sap.sailing.domain.base.impl.BoatClassImpl; @@ -86,18 +86,19 @@ public class TestORCPublicCertificateDatabase { assertEquals(handle.getSailNumber(), result.getSailNumber()); } - @Ignore("Certificate used for testing no longer valid after 2019") +// @Ignore("Certificate used for testing no longer valid after 2019") @Test public void testParallelFuzzySearch() throws InterruptedException, ExecutionException { final Future> soulmateCertificatesFuture = db.search("Soulmate", "DEN13", new BoatClassImpl("ORC", BoatClassMasterdata.ORC)); final Future> amarettoCertificatesFuture = db.search("Amaretto", "NED 6101", new BoatClassImpl("Beneteau First 40.7", /* starts upwind */ true)); final Set soulmateCertificates = soulmateCertificatesFuture.get(); final Set amarettoCertificates = amarettoCertificatesFuture.get(); - assertFoundYear(soulmateCertificates, 2019); - assertFoundYear(amarettoCertificates, 2019); + boolean isYearFound = assertFoundYear(soulmateCertificates, 2019) || assertFoundYear(amarettoCertificates, 2019); + Assume.assumeTrue(isYearFound); + assertTrue(isYearFound); } - private void assertFoundYear(final Set certificates, int year) { + private boolean assertFoundYear(final Set certificates, int year) { boolean foundYear = false; for (final ORCCertificate certificate : certificates) { if (certificate.getIssueDate() != null) { @@ -110,7 +111,7 @@ public class TestORCPublicCertificateDatabase { } } } - assertTrue(foundYear); + return foundYear; } private void assertSoulmate(final String referenceNumber, CertificateHandle handle) { From 65027c81345a9d650a89dcd52ce01e35428122af Mon Sep 17 00:00:00 2001 From: Usman Ali Date: Sat, 11 Jan 2020 18:48:27 +0500 Subject: [PATCH 02/33] Adding custom rules in test cases to ignore if ORC certificate not exists --- .../sap/sailing/domain/orc/CustomIgnore.java | 14 ++++++ .../sailing/domain/orc/CustomIgnoreRule.java | 49 +++++++++++++++++++ .../orc/TestORCCertificateImporterJSON.java | 8 ++- .../orc/TestORCCertificateImporterRMS.java | 9 ++-- .../domain/orc/TestORCPerformanceCurve.java | 6 +-- .../orc/TestORCPublicCertificateDatabase.java | 6 ++- 6 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/CustomIgnore.java create mode 100644 java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/CustomIgnoreRule.java diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/CustomIgnore.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/CustomIgnore.java new file mode 100644 index 00000000000..d748fb72be7 --- /dev/null +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/CustomIgnore.java @@ -0,0 +1,14 @@ +package com.sap.sailing.domain.orc; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE, ElementType.METHOD}) +@Inherited +public @interface CustomIgnore { + +} \ No newline at end of file diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/CustomIgnoreRule.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/CustomIgnoreRule.java new file mode 100644 index 00000000000..fca8631aafc --- /dev/null +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/CustomIgnoreRule.java @@ -0,0 +1,49 @@ +package com.sap.sailing.domain.orc; + +import java.net.URL; +import java.util.Iterator; + +import org.junit.Assume; +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +import com.sap.sailing.domain.common.orc.ORCCertificate; + +public class CustomIgnoreRule implements TestRule { + + @Override + public Statement apply(Statement base, Description description) { + return new IgnorableStatement(base, description); + } + private class IgnorableStatement extends Statement { + private final Statement base; + private final Description description; + public IgnorableStatement(Statement base, Description description) { + this.base = base; + this.description = description; + } + @Override + public void evaluate() throws Throwable { + boolean certificateExists = true; + CustomIgnore annotation = description.getAnnotation(CustomIgnore.class); + if(annotation != null) { + ORCCertificatesCollection importer = ORCCertificatesImporter.INSTANCE.read(new URL("https://data.orc.org/public/WPub.dll?action=DownRMS&CountryId=GER&ext=json").openStream()); + certificateExists= importer.getCertificates()!=null && importer.getCertificates().iterator().hasNext(); + if(certificateExists) { + Iterator iterator = importer.getCertificates().iterator(); + while(iterator.hasNext()) { + ORCCertificate certificate = iterator.next(); + if(certificate.getCDL()<1 || certificate.getGPHInSecondsToTheMile() <1) { + certificateExists = false; + break; + } + } + + } + } + Assume.assumeTrue("Test is ignored!", certificateExists); + base.evaluate(); + } + } +} \ No newline at end of file diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java index 0f4b4788e59..51860e53906 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java @@ -9,7 +9,7 @@ import java.io.IOException; import java.net.URL; import org.json.simple.parser.ParseException; -import org.junit.Assume; +import org.junit.Rule; import org.junit.Test; import com.sap.sailing.domain.common.orc.ORCCertificate; @@ -19,6 +19,10 @@ import com.sap.sailing.domain.common.orc.impl.ORCCertificateImpl; public class TestORCCertificateImporterJSON { private static final String RESOURCES = "resources/orc/"; + @Rule + public CustomIgnoreRule customIgnoreRule = new CustomIgnoreRule(); + + @Test public void testSimpleLocalJSONFileRead() throws IOException, ParseException { File fileGER = new File(RESOURCES + "GER2019.json"); @@ -28,10 +32,10 @@ public class TestORCCertificateImporterJSON { } // @Ignore("Certificate used for testing no longer valid after 2019") + @CustomIgnore @Test public void testSimpleOnlineJSONFileRead() throws IOException, ParseException { ORCCertificatesCollection importer = ORCCertificatesImporter.INSTANCE.read(new URL("https://data.orc.org/public/WPub.dll?action=DownRMS&CountryId=GER&ext=json").openStream()); - Assume.assumeTrue(importer.getCertificateIds().iterator().hasNext()); ORCCertificate swan = importer.getCertificateById("GER140849GER5335"); ORCCertificate moana = importer.getCertificateById("GER140772GER5549"); assertNotNull(swan); diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java index bdb042e01b9..92332f2b991 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java @@ -10,7 +10,7 @@ import java.net.URL; import java.util.Collection; import org.json.simple.parser.ParseException; -import org.junit.Assume; +import org.junit.Rule; import org.junit.Test; import com.sap.sailing.domain.common.orc.ORCCertificate; @@ -18,7 +18,10 @@ import com.sap.sailing.domain.common.orc.impl.ORCCertificateImpl; public class TestORCCertificateImporterRMS { private static final String RESOURCES = "resources/orc/"; - + @Rule + public CustomIgnoreRule customIgnoreRule = new CustomIgnoreRule(); + + @Test public void testSimpleLocalRMSFileRead() throws IOException, ParseException { File fileGER = new File(RESOURCES + "GER2019.rms"); @@ -28,10 +31,10 @@ public class TestORCCertificateImporterRMS { } // @Ignore("Certificate used for testing no longer valid after 2019") + @CustomIgnore @Test public void testSimpleOnlineRMSFileRead() throws IOException, ParseException { ORCCertificatesCollection certificates = ORCCertificatesImporter.INSTANCE.read(new URL("https://data.orc.org/public/WPub.dll?action=DownRMS&CountryId=GER&ext=rms").openStream()); - Assume.assumeTrue(certificates.getCertificateIds().iterator().hasNext()); ORCCertificate swan = certificates.getCertificateById("GER140849GER5335"); ORCCertificate moana = certificates.getCertificateById("GER140772GER5549"); assertNotNull(swan); diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java index ea96e17baf5..2de66da3342 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java @@ -16,9 +16,7 @@ import org.apache.commons.math.FunctionEvaluationException; import org.apache.commons.math.MaxIterationsExceededException; import org.json.simple.parser.ParseException; import org.junit.Assert; -import org.junit.Assume; import org.junit.BeforeClass; -import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ErrorCollector; @@ -53,6 +51,8 @@ public class TestORCPerformanceCurve { private static ORCCertificatesCollection importerOnline; private static final String RESOURCES = "resources/orc/"; + @Rule + public CustomIgnoreRule customIgnoreRule = new CustomIgnoreRule(); @Rule public ErrorCollector collector = new ErrorCollector(); @@ -240,9 +240,9 @@ public class TestORCPerformanceCurve { * Tests to make sure, that the structure of the certificate files didn't change and performance curves can be built */ // @Ignore("Certificate used for testing no longer valid after 2019") + @CustomIgnore @Test public void testOnlineImport() throws FunctionEvaluationException { - Assume.assumeTrue(importerOnline.getCertificateIds().iterator().hasNext()); assertFalse(Util.isEmpty(importerOnline.getCertificateIds())); for (final ORCCertificate certificate : importerOnline.getCertificates()) { new ORCPerformanceCurveImpl(certificate, alturaCourse); diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java index f984b56965c..41bd8d85435 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java @@ -14,6 +14,7 @@ import java.util.concurrent.Future; import org.junit.Assume; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import com.sap.sailing.domain.base.impl.BoatClassImpl; @@ -25,6 +26,9 @@ import com.sap.sse.common.Util; public class TestORCPublicCertificateDatabase { private ORCPublicCertificateDatabase db; + @Rule + public CustomIgnoreRule customIgnoreRule = new CustomIgnoreRule(); + @Before public void setUp() { @@ -87,6 +91,7 @@ public class TestORCPublicCertificateDatabase { } // @Ignore("Certificate used for testing no longer valid after 2019") + @CustomIgnore @Test public void testParallelFuzzySearch() throws InterruptedException, ExecutionException { final Future> soulmateCertificatesFuture = db.search("Soulmate", "DEN13", new BoatClassImpl("ORC", BoatClassMasterdata.ORC)); @@ -94,7 +99,6 @@ public class TestORCPublicCertificateDatabase { final Set soulmateCertificates = soulmateCertificatesFuture.get(); final Set amarettoCertificates = amarettoCertificatesFuture.get(); boolean isYearFound = assertFoundYear(soulmateCertificates, 2019) || assertFoundYear(amarettoCertificates, 2019); - Assume.assumeTrue(isYearFound); assertTrue(isYearFound); } From 025388b78342167fae1f0599eec1c952486da2a6 Mon Sep 17 00:00:00 2001 From: Usman Ali Date: Wed, 15 Jan 2020 00:49:57 +0500 Subject: [PATCH 03/33] changing parseDate method to handle all timezone date string --- .../ORCPublicCertificateDatabaseImpl.java | 56 +++++++++++++++---- 1 file changed, 46 insertions(+), 10 deletions(-) diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java index 94e7934e358..e38ecb1b9b0 100755 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java @@ -16,6 +16,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import java.util.TimeZone; import java.util.UUID; import java.util.concurrent.Future; import java.util.concurrent.FutureTask; @@ -72,6 +73,8 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat private static final String ROOT_ELEMENT = "ROOT"; private static final String DATA_ELEMENT = "DATA"; private static final String ROW_ELEMENT = "ROW"; + private static final String[] dateFormatsWithTimeZone = new String[] {"yyyy-MM-dd'T'HH:mm:ssX","yyyy-MM-dd'T'HH:mm:ss.SSSX"}; + private static final String[] dateFormatsWithoutTimeZone = new String[] {"yyyy-MM-dd'T'HH:mm:ss.SSS","yyyy-MM-dd'T'HH:mm:ss"}; private static final DateFormat isoTimestampFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); /** @@ -351,18 +354,51 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat sailNumber, boatClassName, designer, builder, yearBuilt, issueDate, certType, isOneDesign, isProvisional); } - private Date parseDate(String dateString) throws ParseException { - StringBuilder stringBuilder = new StringBuilder(dateString); - char timeZoneCharacter = stringBuilder.charAt(stringBuilder.length()-1); - if(timeZoneCharacter== 'z' || timeZoneCharacter == 'Z'|| timeZoneCharacter == 'X') { - stringBuilder.deleteCharAt(stringBuilder.length()-1); + public Date parseDate(final String dateString) throws ParseException { + DateFormat dateFormat= null; + if(dateString.contains("GMT")) { + try { + return timeAsGmt(dateString); + } catch(Exception ex) { + + } } - if(false == stringBuilder.toString().contains("+")) { - stringBuilder.append("+0000"); - } - - return isoTimestampFormat.parse(stringBuilder.toString()); + for(String df: dateFormatsWithTimeZone) { + try { + dateFormat = new SimpleDateFormat(df); + return dateFormat.parse(dateString); + } catch(Exception ex) { + + } + } + for(String df: dateFormatsWithoutTimeZone) { + try { + TimeZone timeZone = TimeZone.getTimeZone("UTC"); + dateFormat= new SimpleDateFormat(df); + dateFormat.setTimeZone(timeZone); + return dateFormat.parse(dateString); + } catch(Exception ex) { + + } + } + + return null; } + + private Date timeAsGmt(String dateTimeWithGMT) throws ParseException { + final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); + String []dateSplit = dateTimeWithGMT.split(" "); + if( dateSplit.length <2 ) { + dateSplit = dateTimeWithGMT.split("GMT"); + sdf.setTimeZone(TimeZone.getTimeZone("GMT"+dateSplit[1])); + return sdf.parse(dateSplit[0]); + } else { + sdf.setTimeZone(TimeZone.getTimeZone(dateSplit[1])); + return sdf.parse(dateSplit[0]); + } + + + } @Override public ORCCertificate getCertificate(String referenceNumber) throws Exception { From ec716e5477c2a096aa6ee1a0acd95151d8a6331b Mon Sep 17 00:00:00 2001 From: Usman Ali Date: Wed, 15 Jan 2020 00:51:26 +0500 Subject: [PATCH 04/33] formatting the parse date method --- .../ORCPublicCertificateDatabaseImpl.java | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java index e38ecb1b9b0..db665a4b792 100755 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java @@ -355,50 +355,49 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat } public Date parseDate(final String dateString) throws ParseException { - DateFormat dateFormat= null; - if(dateString.contains("GMT")) { + DateFormat dateFormat = null; + if (dateString.contains("GMT")) { try { return timeAsGmt(dateString); - } catch(Exception ex) { - + } catch (Exception ex) { + } } - for(String df: dateFormatsWithTimeZone) { + for (String df : dateFormatsWithTimeZone) { try { dateFormat = new SimpleDateFormat(df); return dateFormat.parse(dateString); - } catch(Exception ex) { - + } catch (Exception ex) { + } } - for(String df: dateFormatsWithoutTimeZone) { + for (String df : dateFormatsWithoutTimeZone) { try { - TimeZone timeZone = TimeZone.getTimeZone("UTC"); - dateFormat= new SimpleDateFormat(df); + TimeZone timeZone = TimeZone.getTimeZone("UTC"); + dateFormat = new SimpleDateFormat(df); dateFormat.setTimeZone(timeZone); return dateFormat.parse(dateString); - } catch(Exception ex) { - + } catch (Exception ex) { + } } - + return null; } private Date timeAsGmt(String dateTimeWithGMT) throws ParseException { final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); - String []dateSplit = dateTimeWithGMT.split(" "); - if( dateSplit.length <2 ) { + String[] dateSplit = dateTimeWithGMT.split(" "); + if (dateSplit.length < 2) { dateSplit = dateTimeWithGMT.split("GMT"); - sdf.setTimeZone(TimeZone.getTimeZone("GMT"+dateSplit[1])); + sdf.setTimeZone(TimeZone.getTimeZone("GMT" + dateSplit[1])); return sdf.parse(dateSplit[0]); } else { sdf.setTimeZone(TimeZone.getTimeZone(dateSplit[1])); return sdf.parse(dateSplit[0]); } - - - } + + } @Override public ORCCertificate getCertificate(String referenceNumber) throws Exception { From efdd7eaf0db605fdd4e23488608a0563c7d7f3cc Mon Sep 17 00:00:00 2001 From: Usman Ali Date: Wed, 15 Jan 2020 12:24:19 +0500 Subject: [PATCH 05/33] adding test cases along with some logging --- .../orc/TestORCPublicCertificateDatabase.java | 26 +++++++++++++++++++ .../orc/ORCPublicCertificateDatabase.java | 4 +++ .../ORCPublicCertificateDatabaseImpl.java | 11 ++++---- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java index 03180de4dfa..69dee2f8fd1 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java @@ -12,6 +12,7 @@ import java.util.UUID; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; +import org.junit.Assert; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; @@ -112,6 +113,31 @@ public class TestORCPublicCertificateDatabase { } assertTrue(foundYear); } + + @Test + public void testParseDateSuccessCases() { + Assert.assertNotNull(db.parseDate("2019-02-21T10:38:00Z")); + Assert.assertNotNull(db.parseDate("2019-02-21T10:38:00.000GMT+2")); + Assert.assertNotNull(db.parseDate("2019-02-21T10:38:00.000 GMT+2")); + Assert.assertNotNull(db.parseDate("2019-02-21T10:38:00.000+0800")); + Assert.assertNotNull(db.parseDate("2019-02-21T10:38:00.000+08:00")); + Assert.assertNotNull(db.parseDate("2019-02-21T10:38:00.000-08")); + Assert.assertNotNull(db.parseDate("2019-02-21T10:38:00.000Z")); + Assert.assertNotNull(db.parseDate("2019-02-21T10:38:00.000z")); + Assert.assertNotNull(db.parseDate("2019-02-21T10:38:00z")); + } + + @Test + public void testParseDateFailureCases() { + Assert.assertNull(db.parseDate("2019-02-21")); + Assert.assertNull(db.parseDate("2019-02-21T10:38GMT+2")); + Assert.assertNull(db.parseDate("2019-02-21T10:38+0800")); + Assert.assertNull(db.parseDate("2019-02-21T10:38+08:00")); + Assert.assertNull(db.parseDate("2019-02-21T10:38-08")); + Assert.assertNull(db.parseDate("2019-02-21T10:38Z")); + Assert.assertNull(db.parseDate("2019-02-21T10z")); + Assert.assertNull(db.parseDate("2019-02-21T10:38z")); + } private void assertSoulmate(final String referenceNumber, CertificateHandle handle) { assertEquals("SOULMATE", handle.getYachtName()); diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/ORCPublicCertificateDatabase.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/ORCPublicCertificateDatabase.java index dd717b8b94e..4019f1632af 100755 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/ORCPublicCertificateDatabase.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/ORCPublicCertificateDatabase.java @@ -1,6 +1,8 @@ package com.sap.sailing.domain.orc; +import java.text.ParseException; import java.util.Arrays; +import java.util.Date; import java.util.Set; import java.util.UUID; import java.util.concurrent.Future; @@ -166,4 +168,6 @@ public interface ORCPublicCertificateDatabase { * parameters. */ Future> search(String yachtName, String sailNumber, BoatClass boatClass); + + Date parseDate(final String dateString); } diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java index db665a4b792..7ddd98c362b 100755 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java @@ -354,13 +354,14 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat sailNumber, boatClassName, designer, builder, yearBuilt, issueDate, certType, isOneDesign, isProvisional); } - public Date parseDate(final String dateString) throws ParseException { + @Override + public Date parseDate(final String dateString) { DateFormat dateFormat = null; if (dateString.contains("GMT")) { try { return timeAsGmt(dateString); } catch (Exception ex) { - + logger.fine("Date is not in GMT format :"+dateString); } } for (String df : dateFormatsWithTimeZone) { @@ -368,7 +369,7 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat dateFormat = new SimpleDateFormat(df); return dateFormat.parse(dateString); } catch (Exception ex) { - + logger.fine("Date is not parsable by "+df+" format :"+dateString); } } for (String df : dateFormatsWithoutTimeZone) { @@ -378,10 +379,10 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat dateFormat.setTimeZone(timeZone); return dateFormat.parse(dateString); } catch (Exception ex) { - + logger.fine("Date is not parsable by "+df+" format :"+dateString); } } - + logger.fine("Date is not parsable by any of the format :"+dateString); return null; } From 7d875ea874472a2627ae42bbcfb8c9aafe3282a4 Mon Sep 17 00:00:00 2001 From: Usman Ali Date: Thu, 16 Jan 2020 04:06:05 +0500 Subject: [PATCH 06/33] test cases completed for parseDate --- .../orc/TestORCPublicCertificateDatabase.java | 39 +++++++++++++------ .../ORCPublicCertificateDatabaseImpl.java | 8 ++-- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java index 69dee2f8fd1..a0cf4ab8586 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java @@ -4,8 +4,14 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.Calendar; +import java.util.Date; import java.util.GregorianCalendar; +import java.util.LinkedHashMap; +import java.util.Map; import java.util.Set; import java.util.TimeZone; import java.util.UUID; @@ -26,10 +32,21 @@ import com.sap.sse.common.Util; public class TestORCPublicCertificateDatabase { private ORCPublicCertificateDatabase db; - + Map dateComparisonMap = new LinkedHashMap(); @Before public void setUp() { db = new ORCPublicCertificateDatabaseImpl(); + dateComparisonMap.put("2019-02-21T10:38:59Z", "2019-02-21 10:38:59"); + dateComparisonMap.put("2019-02-21T12:00:00.000GMT+2", "2019-02-21 10:00:00"); + dateComparisonMap.put("2019-02-21T15:00:00.000 GMT+2", "2019-02-21 13:00:00"); + dateComparisonMap.put("2019-02-21T10:38:55.000-0800", "2019-02-21 18:38:55"); + dateComparisonMap.put("2019-02-21T10:38:32.000+08:00", "2019-02-21 02:38:32"); + dateComparisonMap.put("2019-02-21T10:38:09.000-06", "2019-02-21 16:38:09"); + dateComparisonMap.put("2019-02-21T10:38:22.000Z", "2019-02-21 10:38:22"); + dateComparisonMap.put("2019-02-21T10:38:00.000z", "2019-02-21 10:38:00"); + dateComparisonMap.put("2019-02-21T10:38:46z", "2019-02-21 10:38:46"); + dateComparisonMap.put("2019-02-21T10:38:17", "2019-02-21 10:38:17"); + dateComparisonMap.put("2019-02-21T10:38:33.000", "2019-02-21 10:38:33"); } @Test @@ -115,21 +132,19 @@ public class TestORCPublicCertificateDatabase { } @Test - public void testParseDateSuccessCases() { - Assert.assertNotNull(db.parseDate("2019-02-21T10:38:00Z")); - Assert.assertNotNull(db.parseDate("2019-02-21T10:38:00.000GMT+2")); - Assert.assertNotNull(db.parseDate("2019-02-21T10:38:00.000 GMT+2")); - Assert.assertNotNull(db.parseDate("2019-02-21T10:38:00.000+0800")); - Assert.assertNotNull(db.parseDate("2019-02-21T10:38:00.000+08:00")); - Assert.assertNotNull(db.parseDate("2019-02-21T10:38:00.000-08")); - Assert.assertNotNull(db.parseDate("2019-02-21T10:38:00.000Z")); - Assert.assertNotNull(db.parseDate("2019-02-21T10:38:00.000z")); - Assert.assertNotNull(db.parseDate("2019-02-21T10:38:00z")); + public void testParseDateSuccessCases() throws ParseException { + DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); + dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); + for (String date : dateComparisonMap.keySet()) { + Date convertedDate = db.parseDate(date); + Assert.assertEquals(convertedDate.getTime() / 1000, + dateFormat.parse(dateComparisonMap.get(date)).getTime() / 1000); + } } @Test public void testParseDateFailureCases() { - Assert.assertNull(db.parseDate("2019-02-21")); + Assert.assertNull(db.parseDate("2019-02-21T20:38")); Assert.assertNull(db.parseDate("2019-02-21T10:38GMT+2")); Assert.assertNull(db.parseDate("2019-02-21T10:38+0800")); Assert.assertNull(db.parseDate("2019-02-21T10:38+08:00")); diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java index 7ddd98c362b..fcbe9fe7e4b 100755 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java @@ -361,7 +361,7 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat try { return timeAsGmt(dateString); } catch (Exception ex) { - logger.fine("Date is not in GMT format :"+dateString); + logger.fine("Date is not in GMT format :" + dateString); } } for (String df : dateFormatsWithTimeZone) { @@ -369,7 +369,7 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat dateFormat = new SimpleDateFormat(df); return dateFormat.parse(dateString); } catch (Exception ex) { - logger.fine("Date is not parsable by "+df+" format :"+dateString); + logger.fine("Date is not parsable by " + df + " format :" + dateString); } } for (String df : dateFormatsWithoutTimeZone) { @@ -379,10 +379,10 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat dateFormat.setTimeZone(timeZone); return dateFormat.parse(dateString); } catch (Exception ex) { - logger.fine("Date is not parsable by "+df+" format :"+dateString); + logger.fine("Date is not parsable by " + df + " format :" + dateString); } } - logger.fine("Date is not parsable by any of the format :"+dateString); + logger.fine("Date is not parsable by any of the format :" + dateString); return null; } From f47bf7423ebdab5a13464cab879093dc4cc01fab Mon Sep 17 00:00:00 2001 From: Usman Ali Date: Sun, 19 Jan 2020 03:08:02 +0500 Subject: [PATCH 07/33] throwing parse exception and handling it in test cases --- .../orc/TestORCPublicCertificateDatabase.java | 28 +++++++++++++------ .../orc/ORCPublicCertificateDatabase.java | 2 +- .../ORCPublicCertificateDatabaseImpl.java | 4 +-- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java index a0cf4ab8586..7bdb6c166e3 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java @@ -7,10 +7,13 @@ import static org.junit.Assert.assertTrue; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.TimeZone; @@ -33,6 +36,8 @@ import com.sap.sse.common.Util; public class TestORCPublicCertificateDatabase { private ORCPublicCertificateDatabase db; Map dateComparisonMap = new LinkedHashMap(); + List dateFailureCases =null; + @Before public void setUp() { db = new ORCPublicCertificateDatabaseImpl(); @@ -47,6 +52,9 @@ public class TestORCPublicCertificateDatabase { dateComparisonMap.put("2019-02-21T10:38:46z", "2019-02-21 10:38:46"); dateComparisonMap.put("2019-02-21T10:38:17", "2019-02-21 10:38:17"); dateComparisonMap.put("2019-02-21T10:38:33.000", "2019-02-21 10:38:33"); + dateFailureCases = Arrays.asList("2019-02-21T10:44GMT+2","2019-02-21T10:38+0800","2019-02-21T10:38+08:00", + "2019-02-21T10:38-08","2019-02-21T10:38Z","2019-02-21T10z","2019-02-21T10:38z"); + } @Test @@ -143,15 +151,17 @@ public class TestORCPublicCertificateDatabase { } @Test - public void testParseDateFailureCases() { - Assert.assertNull(db.parseDate("2019-02-21T20:38")); - Assert.assertNull(db.parseDate("2019-02-21T10:38GMT+2")); - Assert.assertNull(db.parseDate("2019-02-21T10:38+0800")); - Assert.assertNull(db.parseDate("2019-02-21T10:38+08:00")); - Assert.assertNull(db.parseDate("2019-02-21T10:38-08")); - Assert.assertNull(db.parseDate("2019-02-21T10:38Z")); - Assert.assertNull(db.parseDate("2019-02-21T10z")); - Assert.assertNull(db.parseDate("2019-02-21T10:38z")); + public void testParseDateFailureCases() throws ParseException { + for(String dateString: dateFailureCases) { + try { + db.parseDate(dateString); + Assert.fail(dateString+" is parsable"); // as date must not be parsable + } catch(ParseException e) { + // ignoring exception as date must fail. + } + } + Assert.assertTrue(true); + } private void assertSoulmate(final String referenceNumber, CertificateHandle handle) { diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/ORCPublicCertificateDatabase.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/ORCPublicCertificateDatabase.java index 4019f1632af..02ed2d8ef72 100755 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/ORCPublicCertificateDatabase.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/ORCPublicCertificateDatabase.java @@ -169,5 +169,5 @@ public interface ORCPublicCertificateDatabase { */ Future> search(String yachtName, String sailNumber, BoatClass boatClass); - Date parseDate(final String dateString); + Date parseDate(final String dateString) throws ParseException; } diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java index fcbe9fe7e4b..64b4a67b71e 100755 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java @@ -355,7 +355,7 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat } @Override - public Date parseDate(final String dateString) { + public Date parseDate(final String dateString) throws ParseException { DateFormat dateFormat = null; if (dateString.contains("GMT")) { try { @@ -383,7 +383,7 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat } } logger.fine("Date is not parsable by any of the format :" + dateString); - return null; + throw new ParseException("Date is not parsable by any of the format :" + dateString,0); } private Date timeAsGmt(String dateTimeWithGMT) throws ParseException { From e96e77bb7e95ae744d0fdb74bd89596630db75cd Mon Sep 17 00:00:00 2001 From: Usman Ali Date: Tue, 21 Jan 2020 23:51:23 +0500 Subject: [PATCH 08/33] modifying rule name for orc ignore cases --- .../{CustomIgnore.java => IgnoreInvalidOrcCerticates.java} | 2 +- ...tomIgnoreRule.java => IgnoreInvalidOrcCerticatesRule.java} | 4 ++-- .../sailing/domain/orc/TestORCCertificateImporterJSON.java | 4 ++-- .../sap/sailing/domain/orc/TestORCCertificateImporterRMS.java | 4 ++-- .../com/sap/sailing/domain/orc/TestORCPerformanceCurve.java | 4 ++-- .../sailing/domain/orc/TestORCPublicCertificateDatabase.java | 4 ++-- 6 files changed, 11 insertions(+), 11 deletions(-) rename java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/{CustomIgnore.java => IgnoreInvalidOrcCerticates.java} (87%) rename java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/{CustomIgnoreRule.java => IgnoreInvalidOrcCerticatesRule.java} (91%) diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/CustomIgnore.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCerticates.java similarity index 87% rename from java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/CustomIgnore.java rename to java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCerticates.java index d748fb72be7..be853e4e995 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/CustomIgnore.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCerticates.java @@ -9,6 +9,6 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.METHOD}) @Inherited -public @interface CustomIgnore { +public @interface IgnoreInvalidOrcCerticates { } \ No newline at end of file diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/CustomIgnoreRule.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCerticatesRule.java similarity index 91% rename from java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/CustomIgnoreRule.java rename to java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCerticatesRule.java index fca8631aafc..3af0b061db2 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/CustomIgnoreRule.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCerticatesRule.java @@ -10,7 +10,7 @@ import org.junit.runners.model.Statement; import com.sap.sailing.domain.common.orc.ORCCertificate; -public class CustomIgnoreRule implements TestRule { +public class IgnoreInvalidOrcCerticatesRule implements TestRule { @Override public Statement apply(Statement base, Description description) { @@ -26,7 +26,7 @@ public class CustomIgnoreRule implements TestRule { @Override public void evaluate() throws Throwable { boolean certificateExists = true; - CustomIgnore annotation = description.getAnnotation(CustomIgnore.class); + IgnoreInvalidOrcCerticates annotation = description.getAnnotation(IgnoreInvalidOrcCerticates.class); if(annotation != null) { ORCCertificatesCollection importer = ORCCertificatesImporter.INSTANCE.read(new URL("https://data.orc.org/public/WPub.dll?action=DownRMS&CountryId=GER&ext=json").openStream()); certificateExists= importer.getCertificates()!=null && importer.getCertificates().iterator().hasNext(); diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java index 51860e53906..a234dbeebcb 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java @@ -20,7 +20,7 @@ public class TestORCCertificateImporterJSON { private static final String RESOURCES = "resources/orc/"; @Rule - public CustomIgnoreRule customIgnoreRule = new CustomIgnoreRule(); + public IgnoreInvalidOrcCerticatesRule customIgnoreRule = new IgnoreInvalidOrcCerticatesRule(); @Test @@ -32,7 +32,7 @@ public class TestORCCertificateImporterJSON { } // @Ignore("Certificate used for testing no longer valid after 2019") - @CustomIgnore + @IgnoreInvalidOrcCerticates @Test public void testSimpleOnlineJSONFileRead() throws IOException, ParseException { ORCCertificatesCollection importer = ORCCertificatesImporter.INSTANCE.read(new URL("https://data.orc.org/public/WPub.dll?action=DownRMS&CountryId=GER&ext=json").openStream()); diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java index 92332f2b991..27191913dbf 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java @@ -19,7 +19,7 @@ import com.sap.sailing.domain.common.orc.impl.ORCCertificateImpl; public class TestORCCertificateImporterRMS { private static final String RESOURCES = "resources/orc/"; @Rule - public CustomIgnoreRule customIgnoreRule = new CustomIgnoreRule(); + public IgnoreInvalidOrcCerticatesRule customIgnoreRule = new IgnoreInvalidOrcCerticatesRule(); @Test @@ -31,7 +31,7 @@ public class TestORCCertificateImporterRMS { } // @Ignore("Certificate used for testing no longer valid after 2019") - @CustomIgnore + @IgnoreInvalidOrcCerticates @Test public void testSimpleOnlineRMSFileRead() throws IOException, ParseException { ORCCertificatesCollection certificates = ORCCertificatesImporter.INSTANCE.read(new URL("https://data.orc.org/public/WPub.dll?action=DownRMS&CountryId=GER&ext=rms").openStream()); diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java index 2de66da3342..1d270f7fac5 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java @@ -52,7 +52,7 @@ public class TestORCPerformanceCurve { private static final String RESOURCES = "resources/orc/"; @Rule - public CustomIgnoreRule customIgnoreRule = new CustomIgnoreRule(); + public IgnoreInvalidOrcCerticatesRule customIgnoreRule = new IgnoreInvalidOrcCerticatesRule(); @Rule public ErrorCollector collector = new ErrorCollector(); @@ -240,7 +240,7 @@ public class TestORCPerformanceCurve { * Tests to make sure, that the structure of the certificate files didn't change and performance curves can be built */ // @Ignore("Certificate used for testing no longer valid after 2019") - @CustomIgnore + @IgnoreInvalidOrcCerticates @Test public void testOnlineImport() throws FunctionEvaluationException { assertFalse(Util.isEmpty(importerOnline.getCertificateIds())); diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java index 41bd8d85435..ec9e941b3d8 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java @@ -27,7 +27,7 @@ import com.sap.sse.common.Util; public class TestORCPublicCertificateDatabase { private ORCPublicCertificateDatabase db; @Rule - public CustomIgnoreRule customIgnoreRule = new CustomIgnoreRule(); + public IgnoreInvalidOrcCerticatesRule customIgnoreRule = new IgnoreInvalidOrcCerticatesRule(); @Before @@ -91,7 +91,7 @@ public class TestORCPublicCertificateDatabase { } // @Ignore("Certificate used for testing no longer valid after 2019") - @CustomIgnore + @IgnoreInvalidOrcCerticates @Test public void testParallelFuzzySearch() throws InterruptedException, ExecutionException { final Future> soulmateCertificatesFuture = db.search("Soulmate", "DEN13", new BoatClassImpl("ORC", BoatClassMasterdata.ORC)); From c2745597f4b0d8e637f058ecba594479845e4b79 Mon Sep 17 00:00:00 2001 From: Usman Ali Date: Mon, 27 Jan 2020 13:40:29 +0500 Subject: [PATCH 09/33] Testing dynamically db search --- .../orc/IgnoreInvalidOrcCerticatesRule.java | 40 +++++++++++------ .../orc/TestORCCertificateImporterJSON.java | 1 - .../orc/TestORCCertificateImporterRMS.java | 1 - .../domain/orc/TestORCPerformanceCurve.java | 1 - .../orc/TestORCPublicCertificateDatabase.java | 43 ++++++++++++++++--- 5 files changed, 64 insertions(+), 22 deletions(-) diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCerticatesRule.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCerticatesRule.java index 3af0b061db2..e5c6fcb31f6 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCerticatesRule.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCerticatesRule.java @@ -1,6 +1,6 @@ package com.sap.sailing.domain.orc; -import java.net.URL; +import java.util.Calendar; import java.util.Iterator; import org.junit.Assume; @@ -9,39 +9,55 @@ import org.junit.runner.Description; import org.junit.runners.model.Statement; import com.sap.sailing.domain.common.orc.ORCCertificate; +import com.sap.sailing.domain.orc.ORCPublicCertificateDatabase.CertificateHandle; +import com.sap.sailing.domain.orc.impl.ORCPublicCertificateDatabaseImpl; +import com.sap.sse.common.CountryCode; +import com.sap.sse.common.CountryCodeFactory; public class IgnoreInvalidOrcCerticatesRule implements TestRule { + private ORCPublicCertificateDatabase db =new ORCPublicCertificateDatabaseImpl(); @Override public Statement apply(Statement base, Description description) { return new IgnorableStatement(base, description); } + private class IgnorableStatement extends Statement { private final Statement base; private final Description description; + public IgnorableStatement(Statement base, Description description) { this.base = base; this.description = description; } + @Override public void evaluate() throws Throwable { boolean certificateExists = true; IgnoreInvalidOrcCerticates annotation = description.getAnnotation(IgnoreInvalidOrcCerticates.class); - if(annotation != null) { - ORCCertificatesCollection importer = ORCCertificatesImporter.INSTANCE.read(new URL("https://data.orc.org/public/WPub.dll?action=DownRMS&CountryId=GER&ext=json").openStream()); - certificateExists= importer.getCertificates()!=null && importer.getCertificates().iterator().hasNext(); - if(certificateExists) { - Iterator iterator = importer.getCertificates().iterator(); - while(iterator.hasNext()) { - ORCCertificate certificate = iterator.next(); - if(certificate.getCDL()<1 || certificate.getGPHInSecondsToTheMile() <1) { - certificateExists = false; - break; + if (annotation != null) { + Iterator iterator = CountryCodeFactory.INSTANCE.getAll().iterator(); + int year = Calendar.getInstance().get(Calendar.YEAR); + certificateExists = false; + while (iterator.hasNext()) { + CountryCode cc = iterator.next(); + try { + Iterable certificateHandles = db.search(cc, year, null, null, null, null); + if (certificateHandles.iterator().hasNext()) { + Iterable orcCertificates = db.getCertificates(certificateHandles); + if (orcCertificates.iterator().hasNext()) { + certificateExists = true; + break; + } } + } catch (Exception ex) { + // Exceptions are ignored because we are searching for any countries orc certificate's + // availability. } - } + } + Assume.assumeTrue("Test is ignored!", certificateExists); base.evaluate(); } diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java index a234dbeebcb..16dc7aeb760 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java @@ -31,7 +31,6 @@ public class TestORCCertificateImporterJSON { assertNotNull(milan); } -// @Ignore("Certificate used for testing no longer valid after 2019") @IgnoreInvalidOrcCerticates @Test public void testSimpleOnlineJSONFileRead() throws IOException, ParseException { diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java index 27191913dbf..c21e0a1984a 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java @@ -30,7 +30,6 @@ public class TestORCCertificateImporterRMS { assertNotNull(milan); } -// @Ignore("Certificate used for testing no longer valid after 2019") @IgnoreInvalidOrcCerticates @Test public void testSimpleOnlineRMSFileRead() throws IOException, ParseException { diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java index 1d270f7fac5..ace83a8f419 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java @@ -239,7 +239,6 @@ public class TestORCPerformanceCurve { /** * Tests to make sure, that the structure of the certificate files didn't change and performance curves can be built */ -// @Ignore("Certificate used for testing no longer valid after 2019") @IgnoreInvalidOrcCerticates @Test public void testOnlineImport() throws FunctionEvaluationException { diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java index ec9e941b3d8..8dd3f858289 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java @@ -4,24 +4,27 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import java.util.ArrayList; import java.util.Calendar; import java.util.GregorianCalendar; +import java.util.Iterator; +import java.util.List; import java.util.Set; import java.util.TimeZone; import java.util.UUID; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; -import org.junit.Assume; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import com.sap.sailing.domain.base.impl.BoatClassImpl; -import com.sap.sailing.domain.common.BoatClassMasterdata; import com.sap.sailing.domain.common.orc.ORCCertificate; import com.sap.sailing.domain.orc.ORCPublicCertificateDatabase.CertificateHandle; import com.sap.sailing.domain.orc.impl.ORCPublicCertificateDatabaseImpl; +import com.sap.sse.common.CountryCode; +import com.sap.sse.common.CountryCodeFactory; import com.sap.sse.common.Util; public class TestORCPublicCertificateDatabase { @@ -94,11 +97,37 @@ public class TestORCPublicCertificateDatabase { @IgnoreInvalidOrcCerticates @Test public void testParallelFuzzySearch() throws InterruptedException, ExecutionException { - final Future> soulmateCertificatesFuture = db.search("Soulmate", "DEN13", new BoatClassImpl("ORC", BoatClassMasterdata.ORC)); - final Future> amarettoCertificatesFuture = db.search("Amaretto", "NED 6101", new BoatClassImpl("Beneteau First 40.7", /* starts upwind */ true)); - final Set soulmateCertificates = soulmateCertificatesFuture.get(); - final Set amarettoCertificates = amarettoCertificatesFuture.get(); - boolean isYearFound = assertFoundYear(soulmateCertificates, 2019) || assertFoundYear(amarettoCertificates, 2019); + Iterator iterator = CountryCodeFactory.INSTANCE.getAll().iterator(); + int year = Calendar.getInstance().get(Calendar.YEAR); + List certificates = new ArrayList(); + while (iterator.hasNext()) { + CountryCode cc = iterator.next(); + try { + Iterable certificateHandles = db.search(cc, year, null, null, null, null); + if (certificateHandles.iterator().hasNext()) { + Iterable orcCertificates = db.getCertificates(certificateHandles); + if (orcCertificates.iterator().hasNext()) { + if (certificates.size() == 2) {// certificates size set to two as we are trying to test the + // parallel search functionality + break; + } + ORCCertificate orcCertificate = orcCertificates.iterator().next(); + certificates.add(orcCertificate); + } + } + } catch (Exception ex) { + // Exceptions are ignored because we are searching for any countries orc certificate's availability. + } + } + List>> futures = new ArrayList>>(); + for (ORCCertificate orcCertificate : certificates) { + futures.add(db.search(orcCertificate.getBoatName(), orcCertificate.getSailNumber(), + new BoatClassImpl(orcCertificate.getBoatClassName(), true))); + } + boolean isYearFound = false; + for (Future> futureResult : futures) { + isYearFound = isYearFound || assertFoundYear(futureResult.get(), year); + } assertTrue(isYearFound); } From 8104e91274deb1d50a36e1f3d4860c8eb201b8a2 Mon Sep 17 00:00:00 2001 From: Usman Ali Date: Mon, 27 Jan 2020 14:40:12 +0500 Subject: [PATCH 10/33] removing comments --- .../sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java | 1 - 1 file changed, 1 deletion(-) diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java index 8dd3f858289..68188c03577 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java @@ -93,7 +93,6 @@ public class TestORCPublicCertificateDatabase { assertEquals(handle.getSailNumber(), result.getSailNumber()); } -// @Ignore("Certificate used for testing no longer valid after 2019") @IgnoreInvalidOrcCerticates @Test public void testParallelFuzzySearch() throws InterruptedException, ExecutionException { From 239bc369044dc42590cb4f61bc54e04d903a9e77 Mon Sep 17 00:00:00 2001 From: Axel Uhl Date: Thu, 13 Feb 2020 10:10:21 +0100 Subject: [PATCH 11/33] formatting --- .../orc/IgnoreInvalidOrcCerticatesRule.java | 4 +--- .../orc/TestORCCertificateImporterJSON.java | 1 - .../orc/TestORCCertificateImporterRMS.java | 2 +- .../domain/orc/TestORCPerformanceCurve.java | 2 +- .../orc/TestORCPerformanceCurveCourse.java | 18 ------------------ .../orc/TestORCPublicCertificateDatabase.java | 2 +- .../impl/ORCPublicCertificateDatabaseImpl.java | 15 +++++++-------- 7 files changed, 11 insertions(+), 33 deletions(-) diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCerticatesRule.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCerticatesRule.java index e5c6fcb31f6..75b7c46663d 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCerticatesRule.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCerticatesRule.java @@ -15,7 +15,7 @@ import com.sap.sse.common.CountryCode; import com.sap.sse.common.CountryCodeFactory; public class IgnoreInvalidOrcCerticatesRule implements TestRule { - private ORCPublicCertificateDatabase db =new ORCPublicCertificateDatabaseImpl(); + private ORCPublicCertificateDatabase db = new ORCPublicCertificateDatabaseImpl(); @Override public Statement apply(Statement base, Description description) { @@ -55,9 +55,7 @@ public class IgnoreInvalidOrcCerticatesRule implements TestRule { // availability. } } - } - Assume.assumeTrue("Test is ignored!", certificateExists); base.evaluate(); } diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java index cae6f493275..2642663ce2a 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java @@ -22,7 +22,6 @@ public class TestORCCertificateImporterJSON { @Rule public IgnoreInvalidOrcCerticatesRule customIgnoreRule = new IgnoreInvalidOrcCerticatesRule(); - @Test public void testSimpleLocalJSONFileRead() throws IOException, ParseException { File fileGER = new File(RESOURCES + "GER2019.json"); diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java index bc2e3d6b38f..3019f0c798c 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java @@ -18,10 +18,10 @@ import com.sap.sailing.domain.common.orc.impl.ORCCertificateImpl; public class TestORCCertificateImporterRMS { private static final String RESOURCES = "resources/orc/"; + @Rule public IgnoreInvalidOrcCerticatesRule customIgnoreRule = new IgnoreInvalidOrcCerticatesRule(); - @Test public void testSimpleLocalRMSFileRead() throws IOException, ParseException { File fileGER = new File(RESOURCES + "GER2019.rms"); diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java index 33381fd4e65..bf790101037 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java @@ -45,7 +45,6 @@ import com.sap.sse.common.impl.DegreeBearingImpl; * */ public class TestORCPerformanceCurve { - // set true to see all the differences i private final boolean collectErrors = true; @@ -55,6 +54,7 @@ public class TestORCPerformanceCurve { private static ORCCertificatesCollection importerOnline; private static final String RESOURCES = "resources/orc/"; + @Rule public IgnoreInvalidOrcCerticatesRule customIgnoreRule = new IgnoreInvalidOrcCerticatesRule(); diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurveCourse.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurveCourse.java index d79d62eb7c8..6bb1d30f9ac 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurveCourse.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurveCourse.java @@ -25,7 +25,6 @@ import com.sap.sse.common.Util; import com.sap.sse.common.impl.DegreeBearingImpl; public class TestORCPerformanceCurveCourse { - @Test public void testSubcourseOfSimpleORCCourse() { double accuracy = 0.000000001; @@ -36,23 +35,18 @@ public class TestORCPerformanceCurveCourse { legs.add(new ORCPerformanceCurveLegImpl(new NauticalMileDistance(1), new DegreeBearingImpl(180))); legs.add(new ORCPerformanceCurveLegImpl(new NauticalMileDistance(1), new DegreeBearingImpl(0))); ORCPerformanceCurveCourse course = new ORCPerformanceCurveCourseImpl(legs); - // case 0: no leg finished, 40.0% of current leg ORCPerformanceCurveCourse subcourse0 = course.subcourse(0, 0.4); assertEquals(0.4, subcourse0.getTotalLength().getNauticalMiles(), accuracy); - // case 1: first leg finished, 0.0% of current leg ORCPerformanceCurveCourse subcourse1 = course.subcourse(1, 0); assertEquals(1, subcourse1.getTotalLength().getNauticalMiles(), accuracy); - // case 2: first leg finished, 12.5% of current leg ORCPerformanceCurveCourse subcourse2 = course.subcourse(1, 0.125); assertEquals(1.125, subcourse2.getTotalLength().getNauticalMiles(), accuracy); - // special case: didn't start, equals to no legs finished and 0.0% of current leg ORCPerformanceCurveCourse subcourseSpecial1 = course.subcourse(0, 0); assertEquals(0, subcourseSpecial1.getTotalLength().getNauticalMiles(), accuracy); - // special case: number of finished legs is higher then number of actual legs ORCPerformanceCurveCourse subcourseSpecial2 = course.subcourse(10,0); assertEquals(5, subcourseSpecial2.getTotalLength().getNauticalMiles(), accuracy); @@ -68,23 +62,18 @@ public class TestORCPerformanceCurveCourse { legs.add(new ORCPerformanceCurveLegImpl(new NauticalMileDistance(10.23), new DegreeBearingImpl(180))); legs.add(new ORCPerformanceCurveLegImpl(new NauticalMileDistance(2.57), new DegreeBearingImpl(0))); ORCPerformanceCurveCourse course = new ORCPerformanceCurveCourseImpl(legs); - // case 0: no leg finished, 40.0% of current leg ORCPerformanceCurveCourse subcourse0 = course.subcourse(0, 0.4); assertEquals(1.512, subcourse0.getTotalLength().getNauticalMiles(), accuracy); - // case 1: first leg finished, 0.0% of current leg ORCPerformanceCurveCourse subcourse1 = course.subcourse(1, 0); assertEquals(3.78, subcourse1.getTotalLength().getNauticalMiles(), accuracy); - // case 2: first leg finished, 12.5% of current leg ORCPerformanceCurveCourse subcourse2 = course.subcourse(1, 0.125); assertEquals(3.96125, subcourse2.getTotalLength().getNauticalMiles(), accuracy); - // special case: didn't start, equals to no legs finished and 0.0% of current leg ORCPerformanceCurveCourse subcourseSpecial1 = course.subcourse(0, 0); assertEquals(0, subcourseSpecial1.getTotalLength().getNauticalMiles(), accuracy); - // special case: number of finished legs is higher then number of actual legs ORCPerformanceCurveCourse subcourseSpecial2 = course.subcourse(10,0); assertEquals(23.03, subcourseSpecial2.getTotalLength().getNauticalMiles(), accuracy); @@ -99,26 +88,21 @@ public class TestORCPerformanceCurveCourse { legs.add(new ORCPerformanceCurveLegImpl(new NauticalMileDistance(3), ORCPerformanceCurveLegTypes.CIRCULAR_RANDOM)); legs.add(new ORCPerformanceCurveLegImpl(new NauticalMileDistance(4), new DegreeBearingImpl(0))); ORCPerformanceCurveCourse course = new ORCPerformanceCurveCourseImpl(legs); - // case 0: no leg finished, 40.0% of current leg ORCPerformanceCurveCourse subcourse0 = course.subcourse(0, 0.4); assertEquals(0.4, subcourse0.getTotalLength().getNauticalMiles(), accuracy); - // case 1: first leg finished, 0.0% of current leg ORCPerformanceCurveCourse subcourse1 = course.subcourse(1, 0); assertEquals(1, subcourse1.getTotalLength().getNauticalMiles(), accuracy); - // case 2: first leg finished, 12.5% of current leg ORCPerformanceCurveCourse subcourse2 = course.subcourse(1, 0.125); assertEquals(1.25, subcourse2.getTotalLength().getNauticalMiles(), accuracy); assertEquals(ORCPerformanceCurveLegTypes.LONG_DISTANCE, Util.last(subcourse2.getLegs()).getType()); assertEquals(0, subcourse2.getLegs().iterator().next().getTwa().getDegrees(), accuracy); - // special case: didn't start, equals to no legs finished and 0.0% of current leg ORCPerformanceCurveCourse subcourseSpecial1 = course.subcourse(0, 0); assertEquals(0, subcourseSpecial1.getTotalLength().getNauticalMiles(), accuracy); assertEquals(1, Util.size(subcourseSpecial1.getLegs())); - // special case: number of finished legs is higher then number of actual legs ORCPerformanceCurveCourse subcourseSpecial2 = course.subcourse(10, 0); assertEquals(10, subcourseSpecial2.getTotalLength().getNauticalMiles(), accuracy); @@ -135,11 +119,9 @@ public class TestORCPerformanceCurveCourse { legs.add(new ORCPerformanceCurveLegImpl(new NauticalMileDistance(1), new DegreeBearingImpl(0))); legs.add(new ORCPerformanceCurveLegImpl(new NauticalMileDistance(2), new DegreeBearingImpl(0))); ORCPerformanceCurveCourse course = new ORCPerformanceCurveCourseImpl(legs); - // case 0: no leg finished, 40.0% of current leg ORCPerformanceCurveCourse subcourse0 = course.subcourse(0, 0.4); assertEquals(0.4, subcourse0.getTotalLength().getNauticalMiles(), accuracy); - // case 1: first leg finished, 0.0% of current leg ORCPerformanceCurveCourse subcourse1 = subcourse0.subcourse(0, 0.1); assertEquals(0.04, subcourse1.getTotalLength().getNauticalMiles(), accuracy); diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java index 68188c03577..2abdde7fe58 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java @@ -29,10 +29,10 @@ import com.sap.sse.common.Util; public class TestORCPublicCertificateDatabase { private ORCPublicCertificateDatabase db; + @Rule public IgnoreInvalidOrcCerticatesRule customIgnoreRule = new IgnoreInvalidOrcCerticatesRule(); - @Before public void setUp() { db = new ORCPublicCertificateDatabaseImpl(); diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java index 94e7934e358..784f202a493 100755 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java @@ -352,15 +352,14 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat } private Date parseDate(String dateString) throws ParseException { - StringBuilder stringBuilder = new StringBuilder(dateString); - char timeZoneCharacter = stringBuilder.charAt(stringBuilder.length()-1); - if(timeZoneCharacter== 'z' || timeZoneCharacter == 'Z'|| timeZoneCharacter == 'X') { - stringBuilder.deleteCharAt(stringBuilder.length()-1); + final StringBuilder stringBuilder = new StringBuilder(dateString); + final char timeZoneCharacter = stringBuilder.charAt(stringBuilder.length() - 1); + if (timeZoneCharacter == 'z' || timeZoneCharacter == 'Z' || timeZoneCharacter == 'X') { + stringBuilder.deleteCharAt(stringBuilder.length() - 1); + } + if (stringBuilder.indexOf("+") == -1) { + stringBuilder.append("+0000"); } - if(false == stringBuilder.toString().contains("+")) { - stringBuilder.append("+0000"); - } - return isoTimestampFormat.parse(stringBuilder.toString()); } From d84b09652b48ec30303db23c4f0d704f8c7982d2 Mon Sep 17 00:00:00 2001 From: Usman Ali Date: Sat, 15 Feb 2020 17:38:49 +0500 Subject: [PATCH 12/33] adding java docs for IgnoreInvalidOrcCertificatesRule --- .../orc/IgnoreInvalidOrcCerticates.java | 14 ----------- .../orc/IgnoreInvalidOrcCertificates.java | 23 +++++++++++++++++ ... => IgnoreInvalidOrcCertificatesRule.java} | 25 ++++++++++++++++--- .../orc/TestORCCertificateImporterJSON.java | 4 +-- .../orc/TestORCCertificateImporterRMS.java | 4 +-- .../domain/orc/TestORCPerformanceCurve.java | 4 +-- .../orc/TestORCPublicCertificateDatabase.java | 4 +-- 7 files changed, 53 insertions(+), 25 deletions(-) delete mode 100644 java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCerticates.java create mode 100644 java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCertificates.java rename java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/{IgnoreInvalidOrcCerticatesRule.java => IgnoreInvalidOrcCertificatesRule.java} (64%) diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCerticates.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCerticates.java deleted file mode 100644 index be853e4e995..00000000000 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCerticates.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.sap.sailing.domain.orc; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Inherited; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.TYPE, ElementType.METHOD}) -@Inherited -public @interface IgnoreInvalidOrcCerticates { - -} \ No newline at end of file diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCertificates.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCertificates.java new file mode 100644 index 00000000000..9bc6b7f9210 --- /dev/null +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCertificates.java @@ -0,0 +1,23 @@ +package com.sap.sailing.domain.orc; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/*** + * A IgnoreInvalidOrcCerticates is a annotation which is used to identify whether we need to execute the + * {@link IgnoreInvalidOrcCertificatesRule} for that particular test case. i.e. you can find + * the usage of {@link IgnoreInvalidOrcCertificates} in + * {@link TestORCPublicCertificateDatabase} class. + * + * @author Usman Ali + * + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.TYPE, ElementType.METHOD }) +@Inherited +public @interface IgnoreInvalidOrcCertificates { + +} \ No newline at end of file diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCerticatesRule.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCertificatesRule.java similarity index 64% rename from java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCerticatesRule.java rename to java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCertificatesRule.java index 75b7c46663d..50e8aa7a342 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCerticatesRule.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCertificatesRule.java @@ -14,9 +14,23 @@ import com.sap.sailing.domain.orc.impl.ORCPublicCertificateDatabaseImpl; import com.sap.sse.common.CountryCode; import com.sap.sse.common.CountryCodeFactory; -public class IgnoreInvalidOrcCerticatesRule implements TestRule { +/*** + * A IgnoreInvalidOrcCerticatesRule is an implementation of TestRule. This class execution depends on + * {@link IgnoreInvalidOrcCertificates} annotation on any method in a test class containing + * {@link org.junit.rules.TestRule} annotation with current class implementation. When any test class added + * {@link IgnoreInvalidOrcCertificatesRule} rule then before executing all of it's test method, Junit will execute the + * evaluate() method of {@link IgnoreInvalidOrcCertificatesRule} class. This method first check whether the current test + * method contains the {@link IgnoreInvalidOrcCertificates} annotation, if yes then it will check for any certificate + * available to parse on ORC Website. If certificate available then this method continue execution of the current test + * method otherwise it will ignore it. + * + * @author Usman Ali + * + */ + +public class IgnoreInvalidOrcCertificatesRule implements TestRule { private ORCPublicCertificateDatabase db = new ORCPublicCertificateDatabaseImpl(); - + @Override public Statement apply(Statement base, Description description) { return new IgnorableStatement(base, description); @@ -31,10 +45,15 @@ public class IgnoreInvalidOrcCerticatesRule implements TestRule { this.description = description; } + /*** + * This method executes for every test case having {@link TestRule} annotation of + * {@link IgnoreInvalidOrcCertificatesRule} class. Assume statement at the end of this method evaluates whether + * the test method will execute or ignored. + */ @Override public void evaluate() throws Throwable { boolean certificateExists = true; - IgnoreInvalidOrcCerticates annotation = description.getAnnotation(IgnoreInvalidOrcCerticates.class); + IgnoreInvalidOrcCertificates annotation = description.getAnnotation(IgnoreInvalidOrcCertificates.class); if (annotation != null) { Iterator iterator = CountryCodeFactory.INSTANCE.getAll().iterator(); int year = Calendar.getInstance().get(Calendar.YEAR); diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java index 2642663ce2a..9bd94475c6c 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java @@ -20,7 +20,7 @@ public class TestORCCertificateImporterJSON { private static final String RESOURCES = "resources/orc/"; @Rule - public IgnoreInvalidOrcCerticatesRule customIgnoreRule = new IgnoreInvalidOrcCerticatesRule(); + public IgnoreInvalidOrcCertificatesRule customIgnoreRule = new IgnoreInvalidOrcCertificatesRule(); @Test public void testSimpleLocalJSONFileRead() throws IOException, ParseException { @@ -30,7 +30,7 @@ public class TestORCCertificateImporterJSON { assertNotNull(milan); } - @IgnoreInvalidOrcCerticates + @IgnoreInvalidOrcCertificates @Test public void testSimpleOnlineJSONFileRead() throws IOException, ParseException { ORCCertificatesCollection importer = ORCCertificatesImporter.INSTANCE.read(new URL("https://data.orc.org/public/WPub.dll?action=DownRMS&CountryId=GER&ext=json").openStream()); diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java index 3019f0c798c..e8b7b2d0dca 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java @@ -20,7 +20,7 @@ public class TestORCCertificateImporterRMS { private static final String RESOURCES = "resources/orc/"; @Rule - public IgnoreInvalidOrcCerticatesRule customIgnoreRule = new IgnoreInvalidOrcCerticatesRule(); + public IgnoreInvalidOrcCertificatesRule customIgnoreRule = new IgnoreInvalidOrcCertificatesRule(); @Test public void testSimpleLocalRMSFileRead() throws IOException, ParseException { @@ -30,7 +30,7 @@ public class TestORCCertificateImporterRMS { assertNotNull(milan); } - @IgnoreInvalidOrcCerticates + @IgnoreInvalidOrcCertificates @Test public void testSimpleOnlineRMSFileRead() throws IOException, ParseException { ORCCertificatesCollection certificates = ORCCertificatesImporter.INSTANCE.read(new URL("https://data.orc.org/public/WPub.dll?action=DownRMS&CountryId=GER&ext=rms").openStream()); diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java index bf790101037..81e67f3d013 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java @@ -56,7 +56,7 @@ public class TestORCPerformanceCurve { private static final String RESOURCES = "resources/orc/"; @Rule - public IgnoreInvalidOrcCerticatesRule customIgnoreRule = new IgnoreInvalidOrcCerticatesRule(); + public IgnoreInvalidOrcCertificatesRule customIgnoreRule = new IgnoreInvalidOrcCertificatesRule(); @Rule public ErrorCollector collector = new ErrorCollector(); @@ -254,7 +254,7 @@ public class TestORCPerformanceCurve { /** * Tests to make sure, that the structure of the certificate files didn't change and performance curves can be built */ - @IgnoreInvalidOrcCerticates + @IgnoreInvalidOrcCertificates @Test public void testOnlineImport() throws FunctionEvaluationException { assertFalse(Util.isEmpty(importerOnline.getCertificateIds())); diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java index 2abdde7fe58..374ceb0faf5 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java @@ -31,7 +31,7 @@ public class TestORCPublicCertificateDatabase { private ORCPublicCertificateDatabase db; @Rule - public IgnoreInvalidOrcCerticatesRule customIgnoreRule = new IgnoreInvalidOrcCerticatesRule(); + public IgnoreInvalidOrcCertificatesRule customIgnoreRule = new IgnoreInvalidOrcCertificatesRule(); @Before public void setUp() { @@ -93,7 +93,7 @@ public class TestORCPublicCertificateDatabase { assertEquals(handle.getSailNumber(), result.getSailNumber()); } - @IgnoreInvalidOrcCerticates + @IgnoreInvalidOrcCertificates @Test public void testParallelFuzzySearch() throws InterruptedException, ExecutionException { Iterator iterator = CountryCodeFactory.INSTANCE.getAll().iterator(); From 5ad027bcc65c06cc9cd65f7b972e9d40ab878d35 Mon Sep 17 00:00:00 2001 From: Usman Ali Date: Mon, 17 Feb 2020 00:22:18 +0500 Subject: [PATCH 13/33] refactoring the testParseDateSuccessCases by using GregorianCalendar --- .../orc/TestORCPublicCertificateDatabase.java | 44 +++++++++---------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java index 7bdb6c166e3..bcd73569ab0 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java @@ -4,10 +4,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import java.text.DateFormat; import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; import java.util.Date; @@ -35,23 +32,27 @@ import com.sap.sse.common.Util; public class TestORCPublicCertificateDatabase { private ORCPublicCertificateDatabase db; - Map dateComparisonMap = new LinkedHashMap(); + Map dateComparisonMap = new LinkedHashMap(); List dateFailureCases =null; @Before public void setUp() { db = new ORCPublicCertificateDatabaseImpl(); - dateComparisonMap.put("2019-02-21T10:38:59Z", "2019-02-21 10:38:59"); - dateComparisonMap.put("2019-02-21T12:00:00.000GMT+2", "2019-02-21 10:00:00"); - dateComparisonMap.put("2019-02-21T15:00:00.000 GMT+2", "2019-02-21 13:00:00"); - dateComparisonMap.put("2019-02-21T10:38:55.000-0800", "2019-02-21 18:38:55"); - dateComparisonMap.put("2019-02-21T10:38:32.000+08:00", "2019-02-21 02:38:32"); - dateComparisonMap.put("2019-02-21T10:38:09.000-06", "2019-02-21 16:38:09"); - dateComparisonMap.put("2019-02-21T10:38:22.000Z", "2019-02-21 10:38:22"); - dateComparisonMap.put("2019-02-21T10:38:00.000z", "2019-02-21 10:38:00"); - dateComparisonMap.put("2019-02-21T10:38:46z", "2019-02-21 10:38:46"); - dateComparisonMap.put("2019-02-21T10:38:17", "2019-02-21 10:38:17"); - dateComparisonMap.put("2019-02-21T10:38:33.000", "2019-02-21 10:38:33"); + /** + * By default GregorianCalendar month starts from 0 as January and so on. In below cases 1 specifies the date + * month as February. + */ + dateComparisonMap.put("2019-02-21T10:38:59Z", new GregorianCalendar(2019, 1, 21, 15, 38, 59)); + dateComparisonMap.put("2019-02-21T12:00:00.000GMT+2", new GregorianCalendar(2019, 1, 21, 15, 00, 00)); + dateComparisonMap.put("2019-02-21T15:00:00.000 GMT-2", new GregorianCalendar(2019, 1, 21, 22, 00, 00)); + dateComparisonMap.put("2019-02-21T10:38:55.000-0800", new GregorianCalendar(2019, 1, 21, 23, 38, 55)); + dateComparisonMap.put("2019-02-21T10:38:32.000+08:00", new GregorianCalendar(2019, 1, 21, 07, 38, 32)); + dateComparisonMap.put("2019-02-21T10:38:09.000-06", new GregorianCalendar(2019, 1, 21, 21, 38, 9)); + dateComparisonMap.put("2019-02-21T10:38:22.000Z", new GregorianCalendar(2019, 1, 21, 15, 38, 22)); + dateComparisonMap.put("2019-02-21T10:38:00.000z", new GregorianCalendar(2019, 1, 21, 15, 38, 00)); + dateComparisonMap.put("2019-02-21T10:38:46z", new GregorianCalendar(2019, 1, 21, 15, 38, 46)); + dateComparisonMap.put("2019-02-21T10:38:17", new GregorianCalendar(2019, 1, 21, 15, 38, 17)); + dateComparisonMap.put("2019-02-21T10:38:33.000", new GregorianCalendar(2019, 1, 21, 15, 38, 33)); dateFailureCases = Arrays.asList("2019-02-21T10:44GMT+2","2019-02-21T10:38+0800","2019-02-21T10:38+08:00", "2019-02-21T10:38-08","2019-02-21T10:38Z","2019-02-21T10z","2019-02-21T10:38z"); @@ -141,27 +142,24 @@ public class TestORCPublicCertificateDatabase { @Test public void testParseDateSuccessCases() throws ParseException { - DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); - dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); for (String date : dateComparisonMap.keySet()) { Date convertedDate = db.parseDate(date); - Assert.assertEquals(convertedDate.getTime() / 1000, - dateFormat.parse(dateComparisonMap.get(date)).getTime() / 1000); + assertEquals(convertedDate.getTime(), dateComparisonMap.get(date).getTimeInMillis()); } } @Test public void testParseDateFailureCases() throws ParseException { - for(String dateString: dateFailureCases) { + for (String dateString : dateFailureCases) { try { db.parseDate(dateString); - Assert.fail(dateString+" is parsable"); // as date must not be parsable - } catch(ParseException e) { + Assert.fail(dateString + " is parsable"); // as date must not be parsable + } catch (ParseException e) { // ignoring exception as date must fail. } } Assert.assertTrue(true); - + } private void assertSoulmate(final String referenceNumber, CertificateHandle handle) { From 6dcd2994cd81209625fc33cc6b2920f30e6c4799 Mon Sep 17 00:00:00 2001 From: Aleksandr Vanteev Date: Thu, 27 Feb 2020 18:00:03 +0300 Subject: [PATCH 14/33] Fix comments, revert changes, some refactoring --- .../orc/IgnoreInvalidOrcCertificatesRule.java | 24 +++---- .../orc/TestORCPublicCertificateDatabase.java | 70 +++++-------------- 2 files changed, 27 insertions(+), 67 deletions(-) diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCertificatesRule.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCertificatesRule.java index 50e8aa7a342..dd53a2bc47d 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCertificatesRule.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCertificatesRule.java @@ -1,9 +1,7 @@ package com.sap.sailing.domain.orc; -import java.util.Calendar; -import java.util.Iterator; - -import org.junit.Assume; +import java.time.LocalDate; +import org.junit.Assert; import org.junit.rules.TestRule; import org.junit.runner.Description; import org.junit.runners.model.Statement; @@ -55,19 +53,15 @@ public class IgnoreInvalidOrcCertificatesRule implements TestRule { boolean certificateExists = true; IgnoreInvalidOrcCertificates annotation = description.getAnnotation(IgnoreInvalidOrcCertificates.class); if (annotation != null) { - Iterator iterator = CountryCodeFactory.INSTANCE.getAll().iterator(); - int year = Calendar.getInstance().get(Calendar.YEAR); + int year = LocalDate.now().getYear(); certificateExists = false; - while (iterator.hasNext()) { - CountryCode cc = iterator.next(); + for (CountryCode cc : CountryCodeFactory.INSTANCE.getAll()) { try { Iterable certificateHandles = db.search(cc, year, null, null, null, null); - if (certificateHandles.iterator().hasNext()) { - Iterable orcCertificates = db.getCertificates(certificateHandles); - if (orcCertificates.iterator().hasNext()) { - certificateExists = true; - break; - } + Iterable orcCertificates = db.getCertificates(certificateHandles); + if (orcCertificates.iterator().hasNext()) { + certificateExists = true; + break; } } catch (Exception ex) { // Exceptions are ignored because we are searching for any countries orc certificate's @@ -75,7 +69,7 @@ public class IgnoreInvalidOrcCertificatesRule implements TestRule { } } } - Assume.assumeTrue("Test is ignored!", certificateExists); + Assert.assertTrue(certificateExists); base.evaluate(); } } diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java index 374ceb0faf5..ec06f01c709 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java @@ -4,13 +4,10 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.GregorianCalendar; -import java.util.Iterator; -import java.util.List; +import java.time.Instant; +import java.time.LocalDate; +import java.time.ZoneOffset; import java.util.Set; -import java.util.TimeZone; import java.util.UUID; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; @@ -20,11 +17,10 @@ import org.junit.Rule; import org.junit.Test; import com.sap.sailing.domain.base.impl.BoatClassImpl; +import com.sap.sailing.domain.common.BoatClassMasterdata; import com.sap.sailing.domain.common.orc.ORCCertificate; import com.sap.sailing.domain.orc.ORCPublicCertificateDatabase.CertificateHandle; import com.sap.sailing.domain.orc.impl.ORCPublicCertificateDatabaseImpl; -import com.sap.sse.common.CountryCode; -import com.sap.sse.common.CountryCodeFactory; import com.sap.sse.common.Util; public class TestORCPublicCertificateDatabase { @@ -96,54 +92,24 @@ public class TestORCPublicCertificateDatabase { @IgnoreInvalidOrcCertificates @Test public void testParallelFuzzySearch() throws InterruptedException, ExecutionException { - Iterator iterator = CountryCodeFactory.INSTANCE.getAll().iterator(); - int year = Calendar.getInstance().get(Calendar.YEAR); - List certificates = new ArrayList(); - while (iterator.hasNext()) { - CountryCode cc = iterator.next(); - try { - Iterable certificateHandles = db.search(cc, year, null, null, null, null); - if (certificateHandles.iterator().hasNext()) { - Iterable orcCertificates = db.getCertificates(certificateHandles); - if (orcCertificates.iterator().hasNext()) { - if (certificates.size() == 2) {// certificates size set to two as we are trying to test the - // parallel search functionality - break; - } - ORCCertificate orcCertificate = orcCertificates.iterator().next(); - certificates.add(orcCertificate); - } - } - } catch (Exception ex) { - // Exceptions are ignored because we are searching for any countries orc certificate's availability. - } - } - List>> futures = new ArrayList>>(); - for (ORCCertificate orcCertificate : certificates) { - futures.add(db.search(orcCertificate.getBoatName(), orcCertificate.getSailNumber(), - new BoatClassImpl(orcCertificate.getBoatClassName(), true))); - } - boolean isYearFound = false; - for (Future> futureResult : futures) { - isYearFound = isYearFound || assertFoundYear(futureResult.get(), year); - } + final Future> soulmateCertificatesFuture = db.search("Soulmate", "DEN13", + new BoatClassImpl("ORC", BoatClassMasterdata.ORC)); + final Future> amarettoCertificatesFuture = db.search("Amaretto", "NED 6101", + new BoatClassImpl("Beneteau First 40.7", /* starts upwind */ true)); + final Set soulmateCertificates = soulmateCertificatesFuture.get(); + final Set amarettoCertificates = amarettoCertificatesFuture.get(); + int year = LocalDate.now().getYear(); + boolean isYearFound = assertFoundYear(soulmateCertificates, year) + || assertFoundYear(amarettoCertificates, year); assertTrue(isYearFound); } private boolean assertFoundYear(final Set certificates, int year) { - boolean foundYear = false; - for (final ORCCertificate certificate : certificates) { - if (certificate.getIssueDate() != null) { - final GregorianCalendar cal = new GregorianCalendar(); - cal.setTimeZone(TimeZone.getTimeZone("UTC")); - cal.setTimeInMillis(certificate.getIssueDate().asMillis()); - if (cal.get(Calendar.YEAR) == year) { - foundYear = true; - break; - } - } - } - return foundYear; + return certificates.stream().map(cert -> { + LocalDate certDate = Instant.ofEpochMilli(cert.getIssueDate().asMillis()).atOffset(ZoneOffset.UTC) + .toLocalDate(); + return certDate.getYear(); + }).anyMatch(cy -> cy == year); } private void assertSoulmate(final String referenceNumber, CertificateHandle handle) { From a8587213137dd552e60bddf761cd73e2ef1ad666 Mon Sep 17 00:00:00 2001 From: Aleksadnr Vanteev Date: Sun, 1 Mar 2020 22:14:36 +0300 Subject: [PATCH 15/33] Revert test --- .../orc/TestORCPublicCertificateDatabase.java | 35 ++++++++++++++----- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java index ec06f01c709..3615c18bd03 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java @@ -7,6 +7,7 @@ import static org.junit.Assert.assertTrue; import java.time.Instant; import java.time.LocalDate; import java.time.ZoneOffset; +import java.util.ArrayList; import java.util.Set; import java.util.UUID; import java.util.concurrent.ExecutionException; @@ -17,10 +18,11 @@ import org.junit.Rule; import org.junit.Test; import com.sap.sailing.domain.base.impl.BoatClassImpl; -import com.sap.sailing.domain.common.BoatClassMasterdata; import com.sap.sailing.domain.common.orc.ORCCertificate; import com.sap.sailing.domain.orc.ORCPublicCertificateDatabase.CertificateHandle; import com.sap.sailing.domain.orc.impl.ORCPublicCertificateDatabaseImpl; +import com.sap.sse.common.CountryCode; +import com.sap.sse.common.CountryCodeFactory; import com.sap.sse.common.Util; public class TestORCPublicCertificateDatabase { @@ -92,15 +94,30 @@ public class TestORCPublicCertificateDatabase { @IgnoreInvalidOrcCertificates @Test public void testParallelFuzzySearch() throws InterruptedException, ExecutionException { - final Future> soulmateCertificatesFuture = db.search("Soulmate", "DEN13", - new BoatClassImpl("ORC", BoatClassMasterdata.ORC)); - final Future> amarettoCertificatesFuture = db.search("Amaretto", "NED 6101", - new BoatClassImpl("Beneteau First 40.7", /* starts upwind */ true)); - final Set soulmateCertificates = soulmateCertificatesFuture.get(); - final Set amarettoCertificates = amarettoCertificatesFuture.get(); int year = LocalDate.now().getYear(); - boolean isYearFound = assertFoundYear(soulmateCertificates, year) - || assertFoundYear(amarettoCertificates, year); + ArrayList certificates = new ArrayList(); + for (CountryCode cc : CountryCodeFactory.INSTANCE.getAll()) { + try { + Iterable certificateHandles = db.search(cc, year, null, null, null, null); + Iterable orcCertificates = db.getCertificates(certificateHandles); + if (certificates.size() == 2) {// certificates size set to two as we are trying to test the + // parallel search functionality + break; + } + orcCertificates.forEach(certificates::add); + } catch (Exception ex) { + // Exceptions are ignored because we are searching for any countries orc certificate's availability. + } + } + ArrayList>> futures = new ArrayList>>(); + for (ORCCertificate orcCertificate : certificates) { + futures.add(db.search(orcCertificate.getBoatName(), orcCertificate.getSailNumber(), + new BoatClassImpl(orcCertificate.getBoatClassName(), true))); + } + boolean isYearFound = false; + for (Future> futureResult : futures) { + isYearFound = isYearFound || assertFoundYear(futureResult.get(), year); + } assertTrue(isYearFound); } From 2e0bce361c49c07faa703b371e30714d7db892c3 Mon Sep 17 00:00:00 2001 From: Aleksadnr Vanteev Date: Wed, 4 Mar 2020 17:20:40 +0300 Subject: [PATCH 16/33] Refactor of parsing dates from different formats --- .../orc/TestORCPublicCertificateDatabase.java | 17 ++-- .../orc/ORCPublicCertificateDatabase.java | 4 +- .../ORCPublicCertificateDatabaseImpl.java | 85 ++++++++----------- 3 files changed, 42 insertions(+), 64 deletions(-) diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java index 4a5a8bcf3ab..53d77d0dbb3 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java @@ -4,40 +4,36 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import java.text.ParseException; import java.util.Arrays; import java.time.Instant; import java.time.LocalDate; import java.time.ZoneOffset; import java.util.ArrayList; -import java.util.Calendar; -import java.util.Date; import java.util.GregorianCalendar; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.TimeZone; import java.util.UUID; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; -import org.junit.Assert; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import com.sap.sailing.domain.base.impl.BoatClassImpl; -import com.sap.sailing.domain.common.BoatClassMasterdata; import com.sap.sailing.domain.common.orc.ORCCertificate; import com.sap.sailing.domain.orc.ORCPublicCertificateDatabase.CertificateHandle; import com.sap.sailing.domain.orc.impl.ORCPublicCertificateDatabaseImpl; +import com.sap.sse.common.CountryCode; +import com.sap.sse.common.CountryCodeFactory; import com.sap.sse.common.Util; public class TestORCPublicCertificateDatabase { private ORCPublicCertificateDatabase db; - Map dateComparisonMap = new LinkedHashMap(); - List dateFailureCases =null; + private Map dateComparisonMap = new LinkedHashMap(); + private List dateFailureCases = Arrays.asList("2019-02-21T10:44GMT+2","2019-02-21T10:38+0800","2019-02-21T10:38+08:00", + "2019-02-21T10:38-08","2019-02-21T10:38Z","2019-02-21T10z","2019-02-21T10:38z"); @Before public void setUp() { @@ -57,9 +53,6 @@ public class TestORCPublicCertificateDatabase { dateComparisonMap.put("2019-02-21T10:38:46z", new GregorianCalendar(2019, 1, 21, 15, 38, 46)); dateComparisonMap.put("2019-02-21T10:38:17", new GregorianCalendar(2019, 1, 21, 15, 38, 17)); dateComparisonMap.put("2019-02-21T10:38:33.000", new GregorianCalendar(2019, 1, 21, 15, 38, 33)); - dateFailureCases = Arrays.asList("2019-02-21T10:44GMT+2","2019-02-21T10:38+0800","2019-02-21T10:38+08:00", - "2019-02-21T10:38-08","2019-02-21T10:38Z","2019-02-21T10z","2019-02-21T10:38z"); - } @Test diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/ORCPublicCertificateDatabase.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/ORCPublicCertificateDatabase.java index 02ed2d8ef72..058ebfb7915 100755 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/ORCPublicCertificateDatabase.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/ORCPublicCertificateDatabase.java @@ -1,6 +1,6 @@ package com.sap.sailing.domain.orc; -import java.text.ParseException; +import java.time.format.DateTimeParseException; import java.util.Arrays; import java.util.Date; import java.util.Set; @@ -169,5 +169,5 @@ public interface ORCPublicCertificateDatabase { */ Future> search(String yachtName, String sailNumber, BoatClass boatClass); - Date parseDate(final String dateString) throws ParseException; + Date parseDate(final String dateString) throws DateTimeParseException; } diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java index cf7177b6db6..f18749d68d0 100755 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java @@ -4,7 +4,13 @@ import java.io.InputStream; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Base64; import java.util.Calendar; import java.util.Date; @@ -15,7 +21,9 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Optional; import java.util.Set; +import java.util.TimeZone; import java.util.UUID; import java.util.concurrent.Future; import java.util.concurrent.FutureTask; @@ -72,8 +80,20 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat private static final String ROOT_ELEMENT = "ROOT"; private static final String DATA_ELEMENT = "DATA"; private static final String ROW_ELEMENT = "ROW"; - private static final String[] dateFormatsWithTimeZone = new String[] {"yyyy-MM-dd'T'HH:mm:ssX","yyyy-MM-dd'T'HH:mm:ss.SSSX"}; + private static final String[] dateFormatsWithTimeZone = new String[] {"yyyy-MM-dd'T'HH:mm:ssX","yyyy-MM-dd'T'HH:mm:ss.SSSX", "yyyy-MM-dd'T'HH:mm:ss.SSS","yyyy-MM-dd'T'HH:mm:ss"}; private static final String[] dateFormatsWithoutTimeZone = new String[] {"yyyy-MM-dd'T'HH:mm:ss.SSS","yyyy-MM-dd'T'HH:mm:ss"}; + private static final DateTimeFormatter[] DATE_FORMATTERS = + new DateTimeFormatter[] { + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"), + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS") + }; + private static final DateTimeFormatter[] DATE_FORMATTERS_WITH_ZONE = + new DateTimeFormatter[] { + DateTimeFormatter.ISO_DATE_TIME, + DateTimeFormatter.ISO_INSTANT, + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssX"), + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX") + }; private static final DateFormat isoTimestampFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); /** @@ -352,60 +372,25 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat return new CertificateHandleImpl(issuingCountry, gph, sssid, datInGID, referenceNumber, yachtName, sailNumber, boatClassName, designer, builder, yearBuilt, issueDate, certType, isOneDesign, isProvisional); } - - private Date parseDate(String dateString) throws ParseException { -// final StringBuilder stringBuilder = new StringBuilder(dateString); -// final char timeZoneCharacter = stringBuilder.charAt(stringBuilder.length() - 1); -// if (timeZoneCharacter == 'z' || timeZoneCharacter == 'Z' || timeZoneCharacter == 'X') { -// stringBuilder.deleteCharAt(stringBuilder.length() - 1); -// } -// if (stringBuilder.indexOf("+") == -1) { -// stringBuilder.append("+0000"); - } @Override - public Date parseDate(final String dateString) throws ParseException { - DateFormat dateFormat = null; - if (dateString.contains("GMT")) { - try { - return timeAsGmt(dateString); - } catch (Exception ex) { - logger.fine("Date is not in GMT format :" + dateString); - } + public Date parseDate(final String dateString) throws DateTimeParseException { + Optional parsedDateWithoutZone = Arrays.asList(DATE_FORMATTERS).stream() + .map(f -> LocalDateTime.parse(dateString, f)) + .findAny(); + Optional parsedDateWithZone = Arrays.asList(DATE_FORMATTERS_WITH_ZONE).stream() + .map(f -> ZonedDateTime.parse(dateString, f)) + .findAny(); + if (parsedDateWithoutZone.isPresent()) { + LocalDateTime localDateTime = parsedDateWithoutZone.get(); + return Date.from(localDateTime.toInstant(ZoneOffset.UTC)); } - for (String df : dateFormatsWithTimeZone) { - try { - dateFormat = new SimpleDateFormat(df); - return dateFormat.parse(dateString); - } catch (Exception ex) { - logger.fine("Date is not parsable by " + df + " format :" + dateString); - } - } - for (String df : dateFormatsWithoutTimeZone) { - try { - TimeZone timeZone = TimeZone.getTimeZone("UTC"); - dateFormat = new SimpleDateFormat(df); - dateFormat.setTimeZone(timeZone); - return dateFormat.parse(dateString); - } catch (Exception ex) { - logger.fine("Date is not parsable by " + df + " format :" + dateString); - } + if (parsedDateWithZone.isPresent()) { + ZonedDateTime zonedDateTime = parsedDateWithZone.get(); + return Date.from(zonedDateTime.toInstant()); } logger.fine("Date is not parsable by any of the format :" + dateString); - throw new ParseException("Date is not parsable by any of the format :" + dateString,0); - } - - private Date timeAsGmt(String dateTimeWithGMT) throws ParseException { - final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); - String[] dateSplit = dateTimeWithGMT.split(" "); - if (dateSplit.length < 2) { - dateSplit = dateTimeWithGMT.split("GMT"); - sdf.setTimeZone(TimeZone.getTimeZone("GMT" + dateSplit[1])); - return sdf.parse(dateSplit[0]); - } else { - sdf.setTimeZone(TimeZone.getTimeZone(dateSplit[1])); - return sdf.parse(dateSplit[0]); - } + throw new DateTimeParseException("Date is not parsable by any of the format", dateString, 0); } @Override From ce8b470afdd5ede6026e8e94e3f3df4f68ce939a Mon Sep 17 00:00:00 2001 From: Aleksadnr Vanteev Date: Wed, 4 Mar 2020 23:52:48 +0300 Subject: [PATCH 17/33] Refactor, move to java.time(need to work more on offsets) --- .../orc/TestORCPublicCertificateDatabase.java | 50 ++++++++++++++----- .../ORCPublicCertificateDatabaseImpl.java | 28 ++++++++--- 2 files changed, 57 insertions(+), 21 deletions(-) diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java index 53d77d0dbb3..abab1cb4440 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java @@ -5,11 +5,16 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.util.Arrays; +import java.util.Date; import java.time.Instant; import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.ZoneId; import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; import java.util.ArrayList; -import java.util.GregorianCalendar; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -18,6 +23,7 @@ import java.util.UUID; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -31,7 +37,7 @@ import com.sap.sse.common.Util; public class TestORCPublicCertificateDatabase { private ORCPublicCertificateDatabase db; - private Map dateComparisonMap = new LinkedHashMap(); + private Map dateComparisonMap = new LinkedHashMap(); private List dateFailureCases = Arrays.asList("2019-02-21T10:44GMT+2","2019-02-21T10:38+0800","2019-02-21T10:38+08:00", "2019-02-21T10:38-08","2019-02-21T10:38Z","2019-02-21T10z","2019-02-21T10:38z"); @@ -42,17 +48,19 @@ public class TestORCPublicCertificateDatabase { * By default GregorianCalendar month starts from 0 as January and so on. In below cases 1 specifies the date * month as February. */ - dateComparisonMap.put("2019-02-21T10:38:59Z", new GregorianCalendar(2019, 1, 21, 15, 38, 59)); - dateComparisonMap.put("2019-02-21T12:00:00.000GMT+2", new GregorianCalendar(2019, 1, 21, 15, 00, 00)); - dateComparisonMap.put("2019-02-21T15:00:00.000 GMT-2", new GregorianCalendar(2019, 1, 21, 22, 00, 00)); - dateComparisonMap.put("2019-02-21T10:38:55.000-0800", new GregorianCalendar(2019, 1, 21, 23, 38, 55)); - dateComparisonMap.put("2019-02-21T10:38:32.000+08:00", new GregorianCalendar(2019, 1, 21, 07, 38, 32)); - dateComparisonMap.put("2019-02-21T10:38:09.000-06", new GregorianCalendar(2019, 1, 21, 21, 38, 9)); - dateComparisonMap.put("2019-02-21T10:38:22.000Z", new GregorianCalendar(2019, 1, 21, 15, 38, 22)); - dateComparisonMap.put("2019-02-21T10:38:00.000z", new GregorianCalendar(2019, 1, 21, 15, 38, 00)); - dateComparisonMap.put("2019-02-21T10:38:46z", new GregorianCalendar(2019, 1, 21, 15, 38, 46)); - dateComparisonMap.put("2019-02-21T10:38:17", new GregorianCalendar(2019, 1, 21, 15, 38, 17)); - dateComparisonMap.put("2019-02-21T10:38:33.000", new GregorianCalendar(2019, 1, 21, 15, 38, 33)); + dateComparisonMap.put("2019-02-21T10:38:59Z", Date.from(ZonedDateTime.parse("2019-02-21T10:38:59Z").toInstant())); + // dateComparisonMap.put("2019-02-21T12:00:00.000GMT+2", Date.from(ZonedDateTime.of(2019, 2, 21, 10, 38, 59, 0, ZoneId.systemDefault()).toInstant())); + // dateComparisonMap.put("2019-02-21T15:00:00.000 GMT-2", Date.from(ZonedDateTime.of(2019, 2, 21, 10, 38, 59, 0, ZoneId.systemDefault()).toInstant())); + // dateComparisonMap.put("2019-02-21T10:38:55.000-0800", + // Date.from(ZonedDateTime.parse("2019-02-21T10:38:55.000-0800", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")).toInstant()) + // ); + // dateComparisonMap.put("2019-02-21T10:38:32.000+08:00", Date.from(ZonedDateTime.parse("2019-02-21T10:38:59Z").toInstant())); + // dateComparisonMap.put("2019-02-21T10:38:09.000-06", new GregorianCalendar(2019, 1, 21, 21, 38, 9)); + // dateComparisonMap.put("2019-02-21T10:38:22.000Z", new GregorianCalendar(2019, 1, 21, 15, 38, 22)); + // dateComparisonMap.put("2019-02-21T10:38:00.000z", new GregorianCalendar(2019, 1, 21, 15, 38, 00)); + // dateComparisonMap.put("2019-02-21T10:38:46z", new GregorianCalendar(2019, 1, 21, 15, 38, 46)); + // dateComparisonMap.put("2019-02-21T10:38:17", new GregorianCalendar(2019, 1, 21, 15, 38, 17)); +// dateComparisonMap.put("2019-02-21T10:38:33.000", new GregorianCalendar(2019, 1, 21, 15, 38, 33)); } @Test @@ -139,6 +147,22 @@ public class TestORCPublicCertificateDatabase { } assertTrue(isYearFound); } + + @Test + public void testParseDateSuccessCases() { + for (String date : dateComparisonMap.keySet()) { + Date convertedDate = db.parseDate(date); + assertEquals(convertedDate, dateComparisonMap.get(date)); + } + } + + @Test(expected = DateTimeParseException.class) + public void testShould() throws Exception { + for (String dateString : dateFailureCases) { + db.parseDate(dateString); + Assert.fail(dateString + " is parsable"); + } + } private boolean assertFoundYear(final Set certificates, int year) { return certificates.stream().map(cert -> { diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java index f18749d68d0..26110003787 100755 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java @@ -23,7 +23,6 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Optional; import java.util.Set; -import java.util.TimeZone; import java.util.UUID; import java.util.concurrent.Future; import java.util.concurrent.FutureTask; @@ -80,21 +79,20 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat private static final String ROOT_ELEMENT = "ROOT"; private static final String DATA_ELEMENT = "DATA"; private static final String ROW_ELEMENT = "ROW"; - private static final String[] dateFormatsWithTimeZone = new String[] {"yyyy-MM-dd'T'HH:mm:ssX","yyyy-MM-dd'T'HH:mm:ss.SSSX", "yyyy-MM-dd'T'HH:mm:ss.SSS","yyyy-MM-dd'T'HH:mm:ss"}; - private static final String[] dateFormatsWithoutTimeZone = new String[] {"yyyy-MM-dd'T'HH:mm:ss.SSS","yyyy-MM-dd'T'HH:mm:ss"}; + private static final DateTimeFormatter[] DATE_FORMATTERS = new DateTimeFormatter[] { DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"), - DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS") + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS"), + DateTimeFormatter.ISO_INSTANT }; private static final DateTimeFormatter[] DATE_FORMATTERS_WITH_ZONE = new DateTimeFormatter[] { DateTimeFormatter.ISO_DATE_TIME, - DateTimeFormatter.ISO_INSTANT, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssX"), + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"), DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX") }; - private static final DateFormat isoTimestampFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); /** * Hash code and equality as based on {@link #getReferenceNumber() the reference number field} only. @@ -376,10 +374,24 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat @Override public Date parseDate(final String dateString) throws DateTimeParseException { Optional parsedDateWithoutZone = Arrays.asList(DATE_FORMATTERS).stream() - .map(f -> LocalDateTime.parse(dateString, f)) + .map(f -> { + try { + return LocalDateTime.parse(dateString, f); + } catch (DateTimeParseException e) { + return null; + } + }) + .filter(out -> out != null) .findAny(); Optional parsedDateWithZone = Arrays.asList(DATE_FORMATTERS_WITH_ZONE).stream() - .map(f -> ZonedDateTime.parse(dateString, f)) + .map(f -> { + try { + return ZonedDateTime.parse(dateString, f); + } catch (DateTimeParseException e) { + return null; + } + }) + .filter(out -> out != null) .findAny(); if (parsedDateWithoutZone.isPresent()) { LocalDateTime localDateTime = parsedDateWithoutZone.get(); From 0f8756fc97ec62bca85919cad1586f3685eee8ee Mon Sep 17 00:00:00 2001 From: Aleksadnr Vanteev Date: Thu, 5 Mar 2020 18:34:09 +0300 Subject: [PATCH 18/33] Add test cases for time parsing --- .../orc/TestORCPublicCertificateDatabase.java | 45 ++++++++++++++----- .../ORCPublicCertificateDatabaseImpl.java | 4 +- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java index abab1cb4440..1b5676562bc 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java @@ -49,18 +49,39 @@ public class TestORCPublicCertificateDatabase { * month as February. */ dateComparisonMap.put("2019-02-21T10:38:59Z", Date.from(ZonedDateTime.parse("2019-02-21T10:38:59Z").toInstant())); - // dateComparisonMap.put("2019-02-21T12:00:00.000GMT+2", Date.from(ZonedDateTime.of(2019, 2, 21, 10, 38, 59, 0, ZoneId.systemDefault()).toInstant())); - // dateComparisonMap.put("2019-02-21T15:00:00.000 GMT-2", Date.from(ZonedDateTime.of(2019, 2, 21, 10, 38, 59, 0, ZoneId.systemDefault()).toInstant())); - // dateComparisonMap.put("2019-02-21T10:38:55.000-0800", - // Date.from(ZonedDateTime.parse("2019-02-21T10:38:55.000-0800", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")).toInstant()) - // ); - // dateComparisonMap.put("2019-02-21T10:38:32.000+08:00", Date.from(ZonedDateTime.parse("2019-02-21T10:38:59Z").toInstant())); - // dateComparisonMap.put("2019-02-21T10:38:09.000-06", new GregorianCalendar(2019, 1, 21, 21, 38, 9)); - // dateComparisonMap.put("2019-02-21T10:38:22.000Z", new GregorianCalendar(2019, 1, 21, 15, 38, 22)); - // dateComparisonMap.put("2019-02-21T10:38:00.000z", new GregorianCalendar(2019, 1, 21, 15, 38, 00)); - // dateComparisonMap.put("2019-02-21T10:38:46z", new GregorianCalendar(2019, 1, 21, 15, 38, 46)); - // dateComparisonMap.put("2019-02-21T10:38:17", new GregorianCalendar(2019, 1, 21, 15, 38, 17)); -// dateComparisonMap.put("2019-02-21T10:38:33.000", new GregorianCalendar(2019, 1, 21, 15, 38, 33)); + // dateComparisonMap.put("2019-02-21T12:00:00.000GMT+2", Date.from(ZonedDateTime.of(2019, 2, 21, 10, 38, 59, 0, ZoneId.systemDefault()).toInstant())); + // // dateComparisonMap.put("2019-02-21T15:00:00.000 GMT-2", Date.from(ZonedDateTime.of(2019, 2, 21, 10, 38, 59, 0, ZoneId.systemDefault()).toInstant())); + dateComparisonMap.put("2019-02-21T10:38:55.000-0800", + Date.from(ZonedDateTime.parse("2019-02-21T10:38:55.000-0800", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")).toInstant()) + ); + dateComparisonMap.put( + "2019-02-21T10:38:32.000+08:00", + Date.from(ZonedDateTime.parse("2019-02-21T10:38:32.000+08:00").toInstant()) + ); + dateComparisonMap.put( + "2019-02-21T10:38:09.000-06", + Date.from(ZonedDateTime.parse("2019-02-21T10:38:09.000-06", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSx")).toInstant()) + ); + dateComparisonMap.put( + "2019-02-21T10:38:22.000Z", + Date.from(ZonedDateTime.parse("2019-02-21T10:38:22.000Z").toInstant()) + ); + dateComparisonMap.put( + "2019-02-21T10:38:00.000z", + Date.from(ZonedDateTime.parse("2019-02-21T10:38:00.000z").toInstant()) + ); + dateComparisonMap.put( + "2019-02-21T10:38:46z", + Date.from(ZonedDateTime.parse("2019-02-21T10:38:46z").toInstant()) + ); + dateComparisonMap.put( + "2019-02-21T10:38:17", + Date.from(LocalDateTime.parse("2019-02-21T10:38:17").toInstant(ZoneOffset.UTC)) + ); + dateComparisonMap.put( + "2019-02-21T10:38:33.000", + Date.from(LocalDateTime.parse("2019-02-21T10:38:33.000").toInstant(ZoneOffset.UTC)) + ); } @Test diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java index 26110003787..d1cfd798aa4 100755 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java @@ -83,11 +83,11 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat private static final DateTimeFormatter[] DATE_FORMATTERS = new DateTimeFormatter[] { DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"), - DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS"), - DateTimeFormatter.ISO_INSTANT + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS") }; private static final DateTimeFormatter[] DATE_FORMATTERS_WITH_ZONE = new DateTimeFormatter[] { + DateTimeFormatter.ISO_INSTANT, DateTimeFormatter.ISO_DATE_TIME, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssX"), DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"), From 703214147f95ad3e813c6bb2291575f8bec76b0d Mon Sep 17 00:00:00 2001 From: Aleksadnr Vanteev Date: Fri, 6 Mar 2020 14:17:40 +0300 Subject: [PATCH 19/33] Fix tests, make dynamically using values --- .../orc/IgnoreInvalidOrcCertificatesRule.java | 15 +++++++ .../orc/TestORCCertificateImporterJSON.java | 41 ++++++++++++++----- .../orc/TestORCCertificateImporterRMS.java | 36 +++++++++++----- .../orc/TestORCPublicCertificateDatabase.java | 39 ++++++++---------- .../ORCPublicCertificateDatabaseImpl.java | 4 +- 5 files changed, 89 insertions(+), 46 deletions(-) diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCertificatesRule.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCertificatesRule.java index dd53a2bc47d..d97ead9077f 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCertificatesRule.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCertificatesRule.java @@ -1,6 +1,11 @@ package com.sap.sailing.domain.orc; import java.time.LocalDate; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + import org.junit.Assert; import org.junit.rules.TestRule; import org.junit.runner.Description; @@ -28,6 +33,12 @@ import com.sap.sse.common.CountryCodeFactory; public class IgnoreInvalidOrcCertificatesRule implements TestRule { private ORCPublicCertificateDatabase db = new ORCPublicCertificateDatabaseImpl(); + + private List availableCerts; + + public Collection getAvailableCerts() { + return Collections.unmodifiableCollection(availableCerts); + } @Override public Statement apply(Statement base, Description description) { @@ -41,6 +52,7 @@ public class IgnoreInvalidOrcCertificatesRule implements TestRule { public IgnorableStatement(Statement base, Description description) { this.base = base; this.description = description; + availableCerts = new ArrayList<>(); } /*** @@ -59,6 +71,7 @@ public class IgnoreInvalidOrcCertificatesRule implements TestRule { try { Iterable certificateHandles = db.search(cc, year, null, null, null, null); Iterable orcCertificates = db.getCertificates(certificateHandles); + orcCertificates.forEach(availableCerts::add); if (orcCertificates.iterator().hasNext()) { certificateExists = true; break; @@ -72,5 +85,7 @@ public class IgnoreInvalidOrcCertificatesRule implements TestRule { Assert.assertTrue(certificateExists); base.evaluate(); } + + } } \ No newline at end of file diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java index 9bd94475c6c..52ee59193b6 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java @@ -1,12 +1,18 @@ package com.sap.sailing.domain.orc; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.URL; +import java.util.Collection; +import java.util.List; +import java.util.stream.Collector; +import java.util.stream.Collectors; import org.json.simple.parser.ParseException; import org.junit.Rule; @@ -33,16 +39,31 @@ public class TestORCCertificateImporterJSON { @IgnoreInvalidOrcCertificates @Test public void testSimpleOnlineJSONFileRead() throws IOException, ParseException { - ORCCertificatesCollection importer = ORCCertificatesImporter.INSTANCE.read(new URL("https://data.orc.org/public/WPub.dll?action=DownRMS&CountryId=GER&ext=json").openStream()); - ORCCertificate swan = importer.getCertificateById("GER140849GER5335"); - ORCCertificate moana = importer.getCertificateById("GER140772GER5549"); + Collection certificates = customIgnoreRule.getAvailableCerts(); + final ORCCertificate cert = certificates.stream().findFirst().get(); + String countryCode = cert.getId().substring(0, 3); + String url = String.format("https://data.orc.org/public/WPub.dll?action=DownRMS&CountryId=%s&ext=json", countryCode); + ORCCertificatesCollection importer = ORCCertificatesImporter.INSTANCE.read(new URL(url).openStream()); + + ORCCertificate swan = null, refernceCert = null; + for (ORCCertificate orcCertificate : certificates) { + swan = importer.getCertificateById(orcCertificate.getId()); + if (swan != null) { + refernceCert = orcCertificate; + break; + } + } + assertNotNull(swan); - assertNotNull(moana); - assertEquals(539.1, swan .getGPHInSecondsToTheMile(), 0.0000001); - assertEquals(490.4, moana.getGPHInSecondsToTheMile(), 0.0000001); - assertEquals(862.2, swan .getWindwardLeewardSpeedPrediction().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds(), 0.1); - assertEquals(788.2, moana.getWindwardLeewardSpeedPrediction().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds(), 0.1); - assertEquals(861.0, swan .getLongDistanceSpeedPredictions().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds(), 0.1); - assertEquals(787.2, moana.getLongDistanceSpeedPredictions().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds(), 0.1); + assertNotNull(refernceCert); + assertEquals(refernceCert.getGPHInSecondsToTheMile(), swan .getGPHInSecondsToTheMile(), 0.0000001); +// assertEquals( +// refernceCert.getWindwardLeewardSpeedPrediction().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds(), +// swan.getWindwardLeewardSpeedPrediction().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds(), +// 0.1); +// assertEquals( +// refernceCert.getLongDistanceSpeedPredictions().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds(), +// swan.getLongDistanceSpeedPredictions().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds(), +// 0.1); } } diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java index e8b7b2d0dca..044e1cf640f 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java @@ -14,7 +14,6 @@ import org.junit.Rule; import org.junit.Test; import com.sap.sailing.domain.common.orc.ORCCertificate; -import com.sap.sailing.domain.common.orc.impl.ORCCertificateImpl; public class TestORCCertificateImporterRMS { private static final String RESOURCES = "resources/orc/"; @@ -33,17 +32,32 @@ public class TestORCCertificateImporterRMS { @IgnoreInvalidOrcCertificates @Test public void testSimpleOnlineRMSFileRead() throws IOException, ParseException { - ORCCertificatesCollection certificates = ORCCertificatesImporter.INSTANCE.read(new URL("https://data.orc.org/public/WPub.dll?action=DownRMS&CountryId=GER&ext=rms").openStream()); - ORCCertificate swan = certificates.getCertificateById("GER140849GER5335"); - ORCCertificate moana = certificates.getCertificateById("GER140772GER5549"); + Collection certificates = customIgnoreRule.getAvailableCerts(); + final ORCCertificate cert = certificates.stream().findFirst().get(); + String countryCode = cert.getId().substring(0, 3); + String url = String.format("https://data.orc.org/public/WPub.dll?action=DownRMS&CountryId=%s&ext=json", countryCode); + ORCCertificatesCollection importer = ORCCertificatesImporter.INSTANCE.read(new URL(url).openStream()); + + ORCCertificate swan = null, refernceCert = null; + for (ORCCertificate orcCertificate : certificates) { + swan = importer.getCertificateById(orcCertificate.getId()); + if (swan != null) { + refernceCert = orcCertificate; + break; + } + } + assertNotNull(swan); - assertNotNull(moana); - assertEquals(539.1, swan .getGPHInSecondsToTheMile(), 0.0000001); - assertEquals(490.4, moana.getGPHInSecondsToTheMile(), 0.0000001); - assertEquals(862.2, swan .getWindwardLeewardSpeedPrediction().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds(), 0.1); - assertEquals(788.2, moana.getWindwardLeewardSpeedPrediction().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds(), 0.1); - assertEquals(861.0, swan .getLongDistanceSpeedPredictions().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds(), 0.1); - assertEquals(787.2, moana.getLongDistanceSpeedPredictions().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds(), 0.1); + assertNotNull(refernceCert); + assertEquals(refernceCert.getGPHInSecondsToTheMile(), swan .getGPHInSecondsToTheMile(), 0.0000001); +// assertEquals( +// refernceCert.getWindwardLeewardSpeedPrediction().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds(), +// swan.getWindwardLeewardSpeedPrediction().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds(), +// 0.1); +// assertEquals( +// refernceCert.getLongDistanceSpeedPredictions().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds(), +// swan.getLongDistanceSpeedPredictions().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds(), +// 0.1); } @Test diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java index 1b5676562bc..e8731e98f3f 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java @@ -5,11 +5,11 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.util.Arrays; +import java.util.Collection; import java.util.Date; import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; -import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; @@ -18,6 +18,7 @@ import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.UUID; import java.util.concurrent.ExecutionException; @@ -25,14 +26,13 @@ import java.util.concurrent.Future; import org.junit.Assert; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import com.sap.sailing.domain.base.impl.BoatClassImpl; import com.sap.sailing.domain.common.orc.ORCCertificate; import com.sap.sailing.domain.orc.ORCPublicCertificateDatabase.CertificateHandle; import com.sap.sailing.domain.orc.impl.ORCPublicCertificateDatabaseImpl; -import com.sap.sse.common.CountryCode; -import com.sap.sse.common.CountryCodeFactory; import com.sap.sse.common.Util; public class TestORCPublicCertificateDatabase { @@ -41,6 +41,9 @@ public class TestORCPublicCertificateDatabase { private List dateFailureCases = Arrays.asList("2019-02-21T10:44GMT+2","2019-02-21T10:38+0800","2019-02-21T10:38+08:00", "2019-02-21T10:38-08","2019-02-21T10:38Z","2019-02-21T10z","2019-02-21T10:38z"); + @Rule + public IgnoreInvalidOrcCertificatesRule customIgnoreRule = new IgnoreInvalidOrcCertificatesRule(); + @Before public void setUp() { db = new ORCPublicCertificateDatabaseImpl(); @@ -129,13 +132,19 @@ public class TestORCPublicCertificateDatabase { } // TODO this test will probably break 2020 when 2019 certificates will no longer be returned as valid... + @IgnoreInvalidOrcCertificates @Test - public void testGetCertificate() throws Exception { - final String referenceNumber = "FRA00013881"; + public void testGetCertificate() throws Exception { + Collection certificates = customIgnoreRule.getAvailableCerts(); + final ORCCertificate cert = certificates.stream().findFirst().get(); + Iterable certHandles = db.search(null, LocalDate.now().getYear(), null, cert.getBoatName(), cert.getSailNumber(), cert.getBoatClassName()); + Optional certificateHandle = Optional.ofNullable(certHandles.iterator().hasNext() ? certHandles.iterator().next() : null); + assertTrue(certificateHandle.isPresent()); + final String referenceNumber = certificateHandle.get().getReferenceNumber(); final CertificateHandle handle = db.getCertificateHandle(referenceNumber); final ORCCertificate result = db.getCertificate(referenceNumber); assertEquals(handle.getGPH(), result.getGPH().asSeconds(), 0.00001); - assertEquals(handle.getIssueDate(), result.getIssueDate()); + assertEquals(handle.getIssueDate().asMillis(), result.getIssueDate().asMillis(), 1000.0); assertEquals(handle.getSailNumber(), result.getSailNumber()); } @@ -143,26 +152,12 @@ public class TestORCPublicCertificateDatabase { @Test public void testParallelFuzzySearch() throws InterruptedException, ExecutionException { int year = LocalDate.now().getYear(); - ArrayList certificates = new ArrayList(); - for (CountryCode cc : CountryCodeFactory.INSTANCE.getAll()) { - try { - Iterable certificateHandles = db.search(cc, year, null, null, null, null); - Iterable orcCertificates = db.getCertificates(certificateHandles); - if (certificates.size() == 2) {// certificates size set to two as we are trying to test the - // parallel search functionality - break; - } - orcCertificates.forEach(certificates::add); - } catch (Exception ex) { - // Exceptions are ignored because we are searching for any countries orc certificate's availability. - } - } ArrayList>> futures = new ArrayList>>(); - for (ORCCertificate orcCertificate : certificates) { + boolean isYearFound = false; + for (ORCCertificate orcCertificate : customIgnoreRule.getAvailableCerts()) { futures.add(db.search(orcCertificate.getBoatName(), orcCertificate.getSailNumber(), new BoatClassImpl(orcCertificate.getBoatClassName(), true))); } - boolean isYearFound = false; for (Future> futureResult : futures) { isYearFound = isYearFound || assertFoundYear(futureResult.get(), year); } diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java index d1cfd798aa4..bf8cb03c4f3 100755 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java @@ -1,9 +1,7 @@ package com.sap.sailing.domain.orc.impl; import java.io.InputStream; -import java.text.DateFormat; import java.text.ParseException; -import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.ZoneOffset; import java.time.ZonedDateTime; @@ -83,7 +81,7 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat private static final DateTimeFormatter[] DATE_FORMATTERS = new DateTimeFormatter[] { DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"), - DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS") + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS") }; private static final DateTimeFormatter[] DATE_FORMATTERS_WITH_ZONE = new DateTimeFormatter[] { From 64fd423702eca0f9c6dc5d38c50ced0f9ea6bf2f Mon Sep 17 00:00:00 2001 From: Aleksadnr Vanteev Date: Mon, 9 Mar 2020 13:02:13 +0300 Subject: [PATCH 20/33] Fix unstage changes. --- .../domain/orc/TestORCCertificateImporterJSON.java | 8 -------- 1 file changed, 8 deletions(-) diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java index 52ee59193b6..caec53b3529 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java @@ -1,25 +1,17 @@ package com.sap.sailing.domain.orc; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.URL; import java.util.Collection; -import java.util.List; -import java.util.stream.Collector; -import java.util.stream.Collectors; - import org.json.simple.parser.ParseException; import org.junit.Rule; import org.junit.Test; import com.sap.sailing.domain.common.orc.ORCCertificate; -import com.sap.sailing.domain.common.orc.impl.ORCCertificateImpl; public class TestORCCertificateImporterJSON { From 028569778e65333b9f1e9377f30f9f6df2df7cc2 Mon Sep 17 00:00:00 2001 From: Axel Uhl Date: Mon, 9 Mar 2020 23:19:11 +0100 Subject: [PATCH 21/33] adjusted comment of ORC test rule --- .../domain/orc/IgnoreInvalidOrcCertificatesRule.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCertificatesRule.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCertificatesRule.java index d97ead9077f..42d38471477 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCertificatesRule.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCertificatesRule.java @@ -24,8 +24,10 @@ import com.sap.sse.common.CountryCodeFactory; * {@link IgnoreInvalidOrcCertificatesRule} rule then before executing all of it's test method, Junit will execute the * evaluate() method of {@link IgnoreInvalidOrcCertificatesRule} class. This method first check whether the current test * method contains the {@link IgnoreInvalidOrcCertificates} annotation, if yes then it will check for any certificate - * available to parse on ORC Website. If certificate available then this method continue execution of the current test - * method otherwise it will ignore it. + * available to parse on ORC Website. If at least one certificate is available then this method continues execution of + * the current test method; otherwise it will let the test fail because the searching for certificates may be broken, or + * the unlikely corner case applies where temporarily at the beginning of a new calendar year no valid certificate + * exists at all in the ORC database. * * @author Usman Ali * @@ -58,7 +60,7 @@ public class IgnoreInvalidOrcCertificatesRule implements TestRule { /*** * This method executes for every test case having {@link TestRule} annotation of * {@link IgnoreInvalidOrcCertificatesRule} class. Assume statement at the end of this method evaluates whether - * the test method will execute or ignored. + * the test method will execute or be ignored. */ @Override public void evaluate() throws Throwable { From 11e3921ef75e0f6ac26295869c036ab95cef6619 Mon Sep 17 00:00:00 2001 From: Axel Uhl Date: Mon, 9 Mar 2020 23:22:15 +0100 Subject: [PATCH 22/33] renamed ORC test rule for the case of no valid certificate being found in DB --- ...atesRule.java => FailIfNoValidOrcCertificateRule.java} | 8 ++++---- .../sailing/domain/orc/IgnoreInvalidOrcCertificates.java | 2 +- .../domain/orc/TestORCCertificateImporterJSON.java | 2 +- .../sailing/domain/orc/TestORCCertificateImporterRMS.java | 2 +- .../sap/sailing/domain/orc/TestORCPerformanceCurve.java | 2 +- .../domain/orc/TestORCPublicCertificateDatabase.java | 2 +- 6 files changed, 9 insertions(+), 9 deletions(-) rename java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/{IgnoreInvalidOrcCertificatesRule.java => FailIfNoValidOrcCertificateRule.java} (89%) diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCertificatesRule.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/FailIfNoValidOrcCertificateRule.java similarity index 89% rename from java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCertificatesRule.java rename to java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/FailIfNoValidOrcCertificateRule.java index 42d38471477..28bc6a53c2d 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCertificatesRule.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/FailIfNoValidOrcCertificateRule.java @@ -21,8 +21,8 @@ import com.sap.sse.common.CountryCodeFactory; * A IgnoreInvalidOrcCerticatesRule is an implementation of TestRule. This class execution depends on * {@link IgnoreInvalidOrcCertificates} annotation on any method in a test class containing * {@link org.junit.rules.TestRule} annotation with current class implementation. When any test class added - * {@link IgnoreInvalidOrcCertificatesRule} rule then before executing all of it's test method, Junit will execute the - * evaluate() method of {@link IgnoreInvalidOrcCertificatesRule} class. This method first check whether the current test + * {@link FailIfNoValidOrcCertificateRule} rule then before executing all of it's test method, Junit will execute the + * evaluate() method of {@link FailIfNoValidOrcCertificateRule} class. This method first check whether the current test * method contains the {@link IgnoreInvalidOrcCertificates} annotation, if yes then it will check for any certificate * available to parse on ORC Website. If at least one certificate is available then this method continues execution of * the current test method; otherwise it will let the test fail because the searching for certificates may be broken, or @@ -33,7 +33,7 @@ import com.sap.sse.common.CountryCodeFactory; * */ -public class IgnoreInvalidOrcCertificatesRule implements TestRule { +public class FailIfNoValidOrcCertificateRule implements TestRule { private ORCPublicCertificateDatabase db = new ORCPublicCertificateDatabaseImpl(); private List availableCerts; @@ -59,7 +59,7 @@ public class IgnoreInvalidOrcCertificatesRule implements TestRule { /*** * This method executes for every test case having {@link TestRule} annotation of - * {@link IgnoreInvalidOrcCertificatesRule} class. Assume statement at the end of this method evaluates whether + * {@link FailIfNoValidOrcCertificateRule} class. Assume statement at the end of this method evaluates whether * the test method will execute or be ignored. */ @Override diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCertificates.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCertificates.java index 9bc6b7f9210..721b27730e5 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCertificates.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCertificates.java @@ -8,7 +8,7 @@ import java.lang.annotation.Target; /*** * A IgnoreInvalidOrcCerticates is a annotation which is used to identify whether we need to execute the - * {@link IgnoreInvalidOrcCertificatesRule} for that particular test case. i.e. you can find + * {@link FailIfNoValidOrcCertificateRule} for that particular test case. i.e. you can find * the usage of {@link IgnoreInvalidOrcCertificates} in * {@link TestORCPublicCertificateDatabase} class. * diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java index caec53b3529..e5d69283856 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java @@ -18,7 +18,7 @@ public class TestORCCertificateImporterJSON { private static final String RESOURCES = "resources/orc/"; @Rule - public IgnoreInvalidOrcCertificatesRule customIgnoreRule = new IgnoreInvalidOrcCertificatesRule(); + public FailIfNoValidOrcCertificateRule customIgnoreRule = new FailIfNoValidOrcCertificateRule(); @Test public void testSimpleLocalJSONFileRead() throws IOException, ParseException { diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java index 044e1cf640f..c1c939e4320 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java @@ -19,7 +19,7 @@ public class TestORCCertificateImporterRMS { private static final String RESOURCES = "resources/orc/"; @Rule - public IgnoreInvalidOrcCertificatesRule customIgnoreRule = new IgnoreInvalidOrcCertificatesRule(); + public FailIfNoValidOrcCertificateRule customIgnoreRule = new FailIfNoValidOrcCertificateRule(); @Test public void testSimpleLocalRMSFileRead() throws IOException, ParseException { diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java index 81e67f3d013..3587612d734 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java @@ -56,7 +56,7 @@ public class TestORCPerformanceCurve { private static final String RESOURCES = "resources/orc/"; @Rule - public IgnoreInvalidOrcCertificatesRule customIgnoreRule = new IgnoreInvalidOrcCertificatesRule(); + public FailIfNoValidOrcCertificateRule customIgnoreRule = new FailIfNoValidOrcCertificateRule(); @Rule public ErrorCollector collector = new ErrorCollector(); diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java index a77581c7f2e..d09c28e4707 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java @@ -42,7 +42,7 @@ public class TestORCPublicCertificateDatabase { "2019-02-21T10:38-08","2019-02-21T10:38Z","2019-02-21T10z","2019-02-21T10:38z"); @Rule - public IgnoreInvalidOrcCertificatesRule customIgnoreRule = new IgnoreInvalidOrcCertificatesRule(); + public FailIfNoValidOrcCertificateRule customIgnoreRule = new FailIfNoValidOrcCertificateRule(); @Before public void setUp() { From 4ae024ac3df0a88d100c9c6fa3224538c2e9193f Mon Sep 17 00:00:00 2001 From: Axel Uhl Date: Tue, 10 Mar 2020 00:29:00 +0100 Subject: [PATCH 23/33] in ORCPublicCertificateDatabaseImpl use JSON certificates instead of RMS to align IDs --- .../orc/TestORCCertificateImporterJSON.java | 36 +++++++++---------- .../ORCPublicCertificateDatabaseImpl.java | 4 +-- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java index e5d69283856..66a3819d76d 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java @@ -2,16 +2,21 @@ package com.sap.sailing.domain.orc; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.URL; import java.util.Collection; + import org.json.simple.parser.ParseException; import org.junit.Rule; import org.junit.Test; import com.sap.sailing.domain.common.orc.ORCCertificate; +import com.sap.sailing.domain.common.orc.impl.ORCCertificateImpl; +import com.sap.sse.common.Util; public class TestORCCertificateImporterJSON { @@ -30,32 +35,25 @@ public class TestORCCertificateImporterJSON { @IgnoreInvalidOrcCertificates @Test - public void testSimpleOnlineJSONFileRead() throws IOException, ParseException { + public void testSimpleOnlineJSONFileRead() throws IOException, ParseException, InterruptedException { Collection certificates = customIgnoreRule.getAvailableCerts(); final ORCCertificate cert = certificates.stream().findFirst().get(); String countryCode = cert.getId().substring(0, 3); - String url = String.format("https://data.orc.org/public/WPub.dll?action=DownRMS&CountryId=%s&ext=json", countryCode); + String url = String.format("https://data.orc.org/public/WPub.dll?action=DownBoatRMS&CountryId=%s&ext=json", countryCode); ORCCertificatesCollection importer = ORCCertificatesImporter.INSTANCE.read(new URL(url).openStream()); - - ORCCertificate swan = null, refernceCert = null; + ORCCertificate someValidCert = null, referenceCert = null; for (ORCCertificate orcCertificate : certificates) { - swan = importer.getCertificateById(orcCertificate.getId()); - if (swan != null) { - refernceCert = orcCertificate; + someValidCert = importer.getCertificateById(orcCertificate.getId()); + if (someValidCert != null) { + referenceCert = orcCertificate; break; } } - - assertNotNull(swan); - assertNotNull(refernceCert); - assertEquals(refernceCert.getGPHInSecondsToTheMile(), swan .getGPHInSecondsToTheMile(), 0.0000001); -// assertEquals( -// refernceCert.getWindwardLeewardSpeedPrediction().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds(), -// swan.getWindwardLeewardSpeedPrediction().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds(), -// 0.1); -// assertEquals( -// refernceCert.getLongDistanceSpeedPredictions().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds(), -// swan.getLongDistanceSpeedPredictions().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds(), -// 0.1); + assertNotNull("None of the certificates "+String.join(", ", Util.map(certificates, c->c.toString()))+ + " was found when importing the country document from "+url, someValidCert); + assertNotNull(referenceCert); + assertEquals(referenceCert.getGPHInSecondsToTheMile(), someValidCert.getGPHInSecondsToTheMile(), 0.0000001); + assertTrue(referenceCert.getWindwardLeewardSpeedPrediction().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds() > 10); + assertTrue(referenceCert.getLongDistanceSpeedPredictions().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds() > 10); } } diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java index bf8cb03c4f3..ac642fc5e6f 100755 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java @@ -406,10 +406,10 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat @Override public ORCCertificate getCertificate(String referenceNumber) throws Exception { final HttpClient client = new SystemDefaultHttpClient(); - final HttpGet getRequest = new HttpGet("http://data.orc.org/public/WPub.dll?action=DownBoatRMS&RefNo="+referenceNumber); + final HttpGet getRequest = new HttpGet("http://data.orc.org/public/WPub.dll?action=DownBoatRMS&ext=json&RefNo="+referenceNumber); addAuthorizationHeader(getRequest); logger.fine("Obtaining certificate for reference number "+referenceNumber); - final Iterable certificates = new ORCCertificatesRmsImporter().read(client.execute(getRequest).getEntity().getContent()) + final Iterable certificates = new ORCCertificatesJsonImporter().read(client.execute(getRequest).getEntity().getContent()) .getCertificates(); final ORCCertificate result; if (certificates.iterator().hasNext()) { From 8aee21e37c9a0c089b0b8dcd7ca85d66fe96f58c Mon Sep 17 00:00:00 2001 From: Axel Uhl Date: Tue, 10 Mar 2020 01:06:20 +0100 Subject: [PATCH 24/33] ignore boat class name for test case ORC certificate matching --- .../orc/TestORCCertificateImporterRMS.java | 30 ++++++++----------- .../orc/TestORCPublicCertificateDatabase.java | 10 +++++-- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java index c1c939e4320..6736d07ad59 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java @@ -2,6 +2,7 @@ package com.sap.sailing.domain.orc; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import java.io.File; import java.io.FileInputStream; @@ -14,6 +15,7 @@ import org.junit.Rule; import org.junit.Test; import com.sap.sailing.domain.common.orc.ORCCertificate; +import com.sap.sailing.domain.common.orc.impl.ORCCertificateImpl; public class TestORCCertificateImporterRMS { private static final String RESOURCES = "resources/orc/"; @@ -35,29 +37,21 @@ public class TestORCCertificateImporterRMS { Collection certificates = customIgnoreRule.getAvailableCerts(); final ORCCertificate cert = certificates.stream().findFirst().get(); String countryCode = cert.getId().substring(0, 3); - String url = String.format("https://data.orc.org/public/WPub.dll?action=DownRMS&CountryId=%s&ext=json", countryCode); + String url = String.format("https://data.orc.org/public/WPub.dll?action=DownBoatRMS&CountryId=%s&ext=json", countryCode); ORCCertificatesCollection importer = ORCCertificatesImporter.INSTANCE.read(new URL(url).openStream()); - - ORCCertificate swan = null, refernceCert = null; + ORCCertificate anyValidCertificate = null, referenceCert = null; for (ORCCertificate orcCertificate : certificates) { - swan = importer.getCertificateById(orcCertificate.getId()); - if (swan != null) { - refernceCert = orcCertificate; + anyValidCertificate = importer.getCertificateById(orcCertificate.getId()); + if (anyValidCertificate != null) { + referenceCert = orcCertificate; break; } } - - assertNotNull(swan); - assertNotNull(refernceCert); - assertEquals(refernceCert.getGPHInSecondsToTheMile(), swan .getGPHInSecondsToTheMile(), 0.0000001); -// assertEquals( -// refernceCert.getWindwardLeewardSpeedPrediction().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds(), -// swan.getWindwardLeewardSpeedPrediction().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds(), -// 0.1); -// assertEquals( -// refernceCert.getLongDistanceSpeedPredictions().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds(), -// swan.getLongDistanceSpeedPredictions().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds(), -// 0.1); + assertNotNull(anyValidCertificate); + assertNotNull(referenceCert); + assertEquals(referenceCert.getGPHInSecondsToTheMile(), anyValidCertificate .getGPHInSecondsToTheMile(), 0.0000001); + assertTrue(referenceCert.getWindwardLeewardSpeedPrediction().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds() > 10); + assertTrue(referenceCert.getLongDistanceSpeedPredictions().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds() > 10); } @Test diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java index d09c28e4707..aae46927c7e 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java @@ -131,15 +131,19 @@ public class TestORCPublicCertificateDatabase { assertSoulmate(referenceNumber, result); } - // TODO this test will probably break 2021 when 2020 certificates will no longer be returned as valid... @IgnoreInvalidOrcCertificates @Test public void testGetCertificate() throws Exception { Collection certificates = customIgnoreRule.getAvailableCerts(); final ORCCertificate cert = certificates.stream().findFirst().get(); - Iterable certHandles = db.search(null, LocalDate.now().getYear(), null, cert.getBoatName(), cert.getSailNumber(), cert.getBoatClassName()); + Iterable certHandles = db.search(null, LocalDate.now().getYear(), null, cert.getBoatName(), + cert.getSailNumber(), /* + * boat class name; could be set to cert.getBoatClassName() but there are + * deviations in ORC DBs and query API, so leaving null: + */ null); Optional certificateHandle = Optional.ofNullable(certHandles.iterator().hasNext() ? certHandles.iterator().next() : null); - assertTrue(certificateHandle.isPresent()); + assertTrue("No certificate found for handle "+certificateHandle+ + " extracted from certificates "+certificates, certificateHandle.isPresent()); final String referenceNumber = certificateHandle.get().getReferenceNumber(); final CertificateHandle handle = db.getCertificateHandle(referenceNumber); final ORCCertificate result = db.getCertificate(referenceNumber); From 66e1c87954e38687ea3802222d985b153b1a9c7b Mon Sep 17 00:00:00 2001 From: Axel Uhl Date: Tue, 10 Mar 2020 10:32:15 +0100 Subject: [PATCH 25/33] renamed annotation for letting tests fail if no valid ORC certificates are found --- .../sailing/domain/orc/FailIfNoValidOrcCertificateRule.java | 6 +++--- ...cCertificates.java => FailIfNoValidOrcCertificates.java} | 4 ++-- .../sailing/domain/orc/TestORCCertificateImporterJSON.java | 2 +- .../sailing/domain/orc/TestORCCertificateImporterRMS.java | 2 +- .../com/sap/sailing/domain/orc/TestORCPerformanceCurve.java | 2 +- .../domain/orc/TestORCPublicCertificateDatabase.java | 4 ++-- 6 files changed, 10 insertions(+), 10 deletions(-) rename java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/{IgnoreInvalidOrcCertificates.java => FailIfNoValidOrcCertificates.java} (85%) diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/FailIfNoValidOrcCertificateRule.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/FailIfNoValidOrcCertificateRule.java index 28bc6a53c2d..3a07f191d65 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/FailIfNoValidOrcCertificateRule.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/FailIfNoValidOrcCertificateRule.java @@ -19,11 +19,11 @@ import com.sap.sse.common.CountryCodeFactory; /*** * A IgnoreInvalidOrcCerticatesRule is an implementation of TestRule. This class execution depends on - * {@link IgnoreInvalidOrcCertificates} annotation on any method in a test class containing + * {@link FailIfNoValidOrcCertificates} annotation on any method in a test class containing * {@link org.junit.rules.TestRule} annotation with current class implementation. When any test class added * {@link FailIfNoValidOrcCertificateRule} rule then before executing all of it's test method, Junit will execute the * evaluate() method of {@link FailIfNoValidOrcCertificateRule} class. This method first check whether the current test - * method contains the {@link IgnoreInvalidOrcCertificates} annotation, if yes then it will check for any certificate + * method contains the {@link FailIfNoValidOrcCertificates} annotation, if yes then it will check for any certificate * available to parse on ORC Website. If at least one certificate is available then this method continues execution of * the current test method; otherwise it will let the test fail because the searching for certificates may be broken, or * the unlikely corner case applies where temporarily at the beginning of a new calendar year no valid certificate @@ -65,7 +65,7 @@ public class FailIfNoValidOrcCertificateRule implements TestRule { @Override public void evaluate() throws Throwable { boolean certificateExists = true; - IgnoreInvalidOrcCertificates annotation = description.getAnnotation(IgnoreInvalidOrcCertificates.class); + FailIfNoValidOrcCertificates annotation = description.getAnnotation(FailIfNoValidOrcCertificates.class); if (annotation != null) { int year = LocalDate.now().getYear(); certificateExists = false; diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCertificates.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/FailIfNoValidOrcCertificates.java similarity index 85% rename from java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCertificates.java rename to java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/FailIfNoValidOrcCertificates.java index 721b27730e5..01400ab60d0 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/IgnoreInvalidOrcCertificates.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/FailIfNoValidOrcCertificates.java @@ -9,7 +9,7 @@ import java.lang.annotation.Target; /*** * A IgnoreInvalidOrcCerticates is a annotation which is used to identify whether we need to execute the * {@link FailIfNoValidOrcCertificateRule} for that particular test case. i.e. you can find - * the usage of {@link IgnoreInvalidOrcCertificates} in + * the usage of {@link FailIfNoValidOrcCertificates} in * {@link TestORCPublicCertificateDatabase} class. * * @author Usman Ali @@ -18,6 +18,6 @@ import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.TYPE, ElementType.METHOD }) @Inherited -public @interface IgnoreInvalidOrcCertificates { +public @interface FailIfNoValidOrcCertificates { } \ No newline at end of file diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java index 66a3819d76d..fcbfe42e8ec 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java @@ -33,7 +33,7 @@ public class TestORCCertificateImporterJSON { assertNotNull(milan); } - @IgnoreInvalidOrcCertificates + @FailIfNoValidOrcCertificates @Test public void testSimpleOnlineJSONFileRead() throws IOException, ParseException, InterruptedException { Collection certificates = customIgnoreRule.getAvailableCerts(); diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java index 6736d07ad59..d8414250228 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java @@ -31,7 +31,7 @@ public class TestORCCertificateImporterRMS { assertNotNull(milan); } - @IgnoreInvalidOrcCertificates + @FailIfNoValidOrcCertificates @Test public void testSimpleOnlineRMSFileRead() throws IOException, ParseException { Collection certificates = customIgnoreRule.getAvailableCerts(); diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java index 3587612d734..99bc89c4f2d 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java @@ -254,7 +254,7 @@ public class TestORCPerformanceCurve { /** * Tests to make sure, that the structure of the certificate files didn't change and performance curves can be built */ - @IgnoreInvalidOrcCertificates + @FailIfNoValidOrcCertificates @Test public void testOnlineImport() throws FunctionEvaluationException { assertFalse(Util.isEmpty(importerOnline.getCertificateIds())); diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java index aae46927c7e..0d9b83317fa 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java @@ -131,7 +131,7 @@ public class TestORCPublicCertificateDatabase { assertSoulmate(referenceNumber, result); } - @IgnoreInvalidOrcCertificates + @FailIfNoValidOrcCertificates @Test public void testGetCertificate() throws Exception { Collection certificates = customIgnoreRule.getAvailableCerts(); @@ -152,7 +152,7 @@ public class TestORCPublicCertificateDatabase { assertEquals(handle.getSailNumber(), result.getSailNumber()); } - @IgnoreInvalidOrcCertificates + @FailIfNoValidOrcCertificates @Test public void testParallelFuzzySearch() throws InterruptedException, ExecutionException { int year = LocalDate.now().getYear(); From b7e5f4d0067259166975414b81b23c92903d6e37 Mon Sep 17 00:00:00 2001 From: Axel Uhl Date: Tue, 10 Mar 2020 10:48:31 +0100 Subject: [PATCH 26/33] refactored JSON/RMS tests to reduce code duplication --- .../AbstractORCCertificateImporterTest.java | 56 ++++++++ .../orc/TestORCCertificateImporterJSON.java | 44 +----- .../orc/TestORCCertificateImporterRMS.java | 40 +----- .../impl/ORCCertificatesCollectionRMS.java | 128 +++++++++--------- 4 files changed, 131 insertions(+), 137 deletions(-) create mode 100755 java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/AbstractORCCertificateImporterTest.java diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/AbstractORCCertificateImporterTest.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/AbstractORCCertificateImporterTest.java new file mode 100755 index 00000000000..6c817d76770 --- /dev/null +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/AbstractORCCertificateImporterTest.java @@ -0,0 +1,56 @@ +package com.sap.sailing.domain.orc; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.net.URL; +import java.util.Collection; + +import org.json.simple.parser.ParseException; +import org.junit.Rule; + +import com.sap.sailing.domain.common.orc.ORCCertificate; +import com.sap.sailing.domain.common.orc.impl.ORCCertificateImpl; +import com.sap.sse.common.Util; + + +public abstract class AbstractORCCertificateImporterTest { + protected static final String RESOURCES = "resources/orc/"; + + @Rule + public FailIfNoValidOrcCertificateRule customIgnoreRule = new FailIfNoValidOrcCertificateRule(); + + protected void testSimpleLocalFileRead(String fileName, String expectedCertificateId) throws IOException, ParseException { + File fileGER = new File(RESOURCES + fileName); + ORCCertificatesCollection importer = ORCCertificatesImporter.INSTANCE.read(new FileInputStream(fileGER)); + ORCCertificate milan = importer.getCertificateById(expectedCertificateId); + assertNotNull(milan); + } + + @FailIfNoValidOrcCertificates + protected void testSimpleOnlineFileRead(String rmsOrJsonFormat) throws IOException, ParseException, InterruptedException { + Collection certificates = customIgnoreRule.getAvailableCerts(); + final ORCCertificate cert = certificates.stream().findFirst().get(); + String countryCode = cert.getId().substring(0, 3); + String url = String.format("https://data.orc.org/public/WPub.dll?action=DownBoatRMS&CountryId=%s&ext="+rmsOrJsonFormat, countryCode); + ORCCertificatesCollection importer = ORCCertificatesImporter.INSTANCE.read(new URL(url).openStream()); + ORCCertificate someValidCert = null, referenceCert = null; + for (ORCCertificate orcCertificate : certificates) { + someValidCert = importer.getCertificateById(orcCertificate.getId()); + if (someValidCert != null) { + referenceCert = orcCertificate; + break; + } + } + assertNotNull("None of the certificates "+String.join(", ", Util.map(certificates, c->c.toString()))+ + " was found when importing the country document from "+url, someValidCert); + assertNotNull(referenceCert); + assertEquals(referenceCert.getGPHInSecondsToTheMile(), someValidCert.getGPHInSecondsToTheMile(), 0.0000001); + assertTrue(referenceCert.getWindwardLeewardSpeedPrediction().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds() > 10); + assertTrue(referenceCert.getLongDistanceSpeedPredictions().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds() > 10); + } +} diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java index fcbfe42e8ec..bab74c77be2 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java @@ -1,59 +1,21 @@ package com.sap.sailing.domain.orc; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import java.io.File; -import java.io.FileInputStream; import java.io.IOException; -import java.net.URL; -import java.util.Collection; import org.json.simple.parser.ParseException; -import org.junit.Rule; import org.junit.Test; -import com.sap.sailing.domain.common.orc.ORCCertificate; -import com.sap.sailing.domain.common.orc.impl.ORCCertificateImpl; -import com.sap.sse.common.Util; - -public class TestORCCertificateImporterJSON { - private static final String RESOURCES = "resources/orc/"; - - @Rule - public FailIfNoValidOrcCertificateRule customIgnoreRule = new FailIfNoValidOrcCertificateRule(); +public class TestORCCertificateImporterJSON extends AbstractORCCertificateImporterTest { @Test public void testSimpleLocalJSONFileRead() throws IOException, ParseException { - File fileGER = new File(RESOURCES + "GER2019.json"); - ORCCertificatesCollection importer = ORCCertificatesImporter.INSTANCE.read(new FileInputStream(fileGER)); - ORCCertificate milan = importer.getCertificateById("GER166844GER7323"); - assertNotNull(milan); + testSimpleLocalFileRead("GER2019.json", "GER166844GER7323"); } @FailIfNoValidOrcCertificates @Test public void testSimpleOnlineJSONFileRead() throws IOException, ParseException, InterruptedException { - Collection certificates = customIgnoreRule.getAvailableCerts(); - final ORCCertificate cert = certificates.stream().findFirst().get(); - String countryCode = cert.getId().substring(0, 3); - String url = String.format("https://data.orc.org/public/WPub.dll?action=DownBoatRMS&CountryId=%s&ext=json", countryCode); - ORCCertificatesCollection importer = ORCCertificatesImporter.INSTANCE.read(new URL(url).openStream()); - ORCCertificate someValidCert = null, referenceCert = null; - for (ORCCertificate orcCertificate : certificates) { - someValidCert = importer.getCertificateById(orcCertificate.getId()); - if (someValidCert != null) { - referenceCert = orcCertificate; - break; - } - } - assertNotNull("None of the certificates "+String.join(", ", Util.map(certificates, c->c.toString()))+ - " was found when importing the country document from "+url, someValidCert); - assertNotNull(referenceCert); - assertEquals(referenceCert.getGPHInSecondsToTheMile(), someValidCert.getGPHInSecondsToTheMile(), 0.0000001); - assertTrue(referenceCert.getWindwardLeewardSpeedPrediction().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds() > 10); - assertTrue(referenceCert.getLongDistanceSpeedPredictions().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds() > 10); + testSimpleOnlineFileRead("json"); } } diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java index d8414250228..73d499052d8 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java @@ -1,62 +1,32 @@ package com.sap.sailing.domain.orc; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; import java.io.File; import java.io.FileInputStream; import java.io.IOException; -import java.net.URL; import java.util.Collection; import org.json.simple.parser.ParseException; -import org.junit.Rule; import org.junit.Test; -import com.sap.sailing.domain.common.orc.ORCCertificate; -import com.sap.sailing.domain.common.orc.impl.ORCCertificateImpl; - -public class TestORCCertificateImporterRMS { +public class TestORCCertificateImporterRMS extends AbstractORCCertificateImporterTest { private static final String RESOURCES = "resources/orc/"; - @Rule - public FailIfNoValidOrcCertificateRule customIgnoreRule = new FailIfNoValidOrcCertificateRule(); - @Test public void testSimpleLocalRMSFileRead() throws IOException, ParseException { - File fileGER = new File(RESOURCES + "GER2019.rms"); - ORCCertificatesCollection certificates = ORCCertificatesImporter.INSTANCE.read(new FileInputStream(fileGER)); - ORCCertificate milan = certificates.getCertificateById("GER166844GER7323"); - assertNotNull(milan); + testSimpleLocalFileRead("GER2019.rms", "GER166844GER7323"); } @FailIfNoValidOrcCertificates @Test - public void testSimpleOnlineRMSFileRead() throws IOException, ParseException { - Collection certificates = customIgnoreRule.getAvailableCerts(); - final ORCCertificate cert = certificates.stream().findFirst().get(); - String countryCode = cert.getId().substring(0, 3); - String url = String.format("https://data.orc.org/public/WPub.dll?action=DownBoatRMS&CountryId=%s&ext=json", countryCode); - ORCCertificatesCollection importer = ORCCertificatesImporter.INSTANCE.read(new URL(url).openStream()); - ORCCertificate anyValidCertificate = null, referenceCert = null; - for (ORCCertificate orcCertificate : certificates) { - anyValidCertificate = importer.getCertificateById(orcCertificate.getId()); - if (anyValidCertificate != null) { - referenceCert = orcCertificate; - break; - } - } - assertNotNull(anyValidCertificate); - assertNotNull(referenceCert); - assertEquals(referenceCert.getGPHInSecondsToTheMile(), anyValidCertificate .getGPHInSecondsToTheMile(), 0.0000001); - assertTrue(referenceCert.getWindwardLeewardSpeedPrediction().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds() > 10); - assertTrue(referenceCert.getLongDistanceSpeedPredictions().get(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS[0]).getDuration(ORCCertificateImpl.NAUTICAL_MILE).asSeconds() > 10); + public void testSimpleOnlineRMSFileRead() throws IOException, ParseException, InterruptedException { + testSimpleOnlineFileRead("rms"); } @Test public void testLocalRMSFileReadWithIdenticalSailnumbers() throws IOException, ParseException { - //additional test to ensure certificates with same sailnumbers but different ids are equally parsed and saved + // additional test to ensure certificates with same sailnumbers but different ids are equally parsed and saved File file = new File(RESOURCES + "multipleIdenticalSailnumbers.rms"); ORCCertificatesCollection certificates = ORCCertificatesImporter.INSTANCE.read(new FileInputStream(file)); assertEquals(14, ((Collection) (certificates.getCertificateIds())).size()); diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCCertificatesCollectionRMS.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCCertificatesCollectionRMS.java index e16306b04b5..c3023550d1a 100755 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCCertificatesCollectionRMS.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCCertificatesCollectionRMS.java @@ -96,69 +96,75 @@ public class ORCCertificatesCollectionRMS extends AbstractORCCertificatesCollect @Override public ORCCertificate getCertificateById(String certificateId) { - ORCCertificateValues certificateValues = getValuesForCertificateId(certificateId); - final String sailNumber = certificateValues.getValue(SAILNUMBER); - final String boatclass = certificateValues.getValue(BOATCLASS); - final String boatName = certificateValues.getValue(BOATNAME); - final Distance length = new MeterDistance(Double.parseDouble(certificateValues.getValue(LENGTH))); - final Duration gph = new SecondsDurationImpl(Double.parseDouble(certificateValues.getValue(GPH))); - final Double cdl = Double.parseDouble(certificateValues.getValue(CDL)); - String dateString = certificateValues.getValue(ISSUEDATE); - String timeString = certificateValues.getValue(ISSUETIME); - TimePoint issueDate; - try { - issueDate = new MillisecondsTimePoint(timestampFormat.parse(dateString+" "+timeString+"+0000")); // assume UTC - } catch (ParseException e) { - issueDate = null; - } - final Map> velocityPredictionsPerTrueWindSpeedAndAngle = new HashMap<>(); - final Map beatAngles = new HashMap<>(); - final Map beatVMGPredictionPerTrueWindSpeed = new HashMap<>(); - final Map beatAllowancePerTrueWindSpeed = new HashMap<>(); - final Map runAngles = new HashMap<>(); - final Map runVMGPredictionPerTrueWindSpeed = new HashMap<>(); - final Map runAllowancePerTrueWindSpeed = new HashMap<>(); - final Map windwardLeewardSpeedPredictionPerTrueWindSpeed = new HashMap<>(); - final Map longDistanceSpeedPredictionPerTrueWindSpeed = new HashMap<>(); - final Map circularRandomSpeedPredictionPerTrueWindSpeed = new HashMap<>(); - final Map nonSpinnakerSpeedPredictionPerTrueWindSpeed = new HashMap<>(); - for (Speed tws : ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS) { - String windSpeed = Integer.toString((int) tws.getKnots()); - String beatAngleKey = BEAT_ANGLE + windSpeed; - String beatAllowanceKey = BEAT_ALLOWANCE + windSpeed; - String runAngleKey = RUN_ANGLE + windSpeed; - String runAllowanceKey = RUN_ALLOWANCE + windSpeed; - String windwardLeewardKey = WINDWARD_LEEWARD + windSpeed; - String longDistanceKey = LONG_DISTANCE + windSpeed; - String circularRandomKey = CIRCULAR_RANDOM + windSpeed; - String nonSpinnakerKey = NON_SPINNAKER + windSpeed; - beatAngles.put(tws, new DegreeBearingImpl(Double.parseDouble(certificateValues.getValue(beatAngleKey)))); - beatAllowancePerTrueWindSpeed.put(tws, new SecondsDurationImpl(Double.parseDouble(certificateValues.getValue(beatAllowanceKey)))); - beatVMGPredictionPerTrueWindSpeed.put(tws, ORCCertificateImpl.NAUTICAL_MILE.inTime(beatAllowancePerTrueWindSpeed.get(tws))); - runAngles.put(tws, new DegreeBearingImpl(Double.parseDouble(certificateValues.getValue(runAngleKey)))); - runAllowancePerTrueWindSpeed.put(tws, new SecondsDurationImpl(Double.parseDouble(certificateValues.getValue(runAllowanceKey)))); - runVMGPredictionPerTrueWindSpeed.put(tws, ORCCertificateImpl.NAUTICAL_MILE.inTime(runAllowancePerTrueWindSpeed.get(tws))); - windwardLeewardSpeedPredictionPerTrueWindSpeed.put(tws, ORCCertificateImpl.NAUTICAL_MILE.inTime( - new SecondsDurationImpl(Double.parseDouble(certificateValues.getValue(windwardLeewardKey))))); - longDistanceSpeedPredictionPerTrueWindSpeed.put(tws, ORCCertificateImpl.NAUTICAL_MILE.inTime( - new SecondsDurationImpl(Double.parseDouble(certificateValues.getValue(longDistanceKey))))); - circularRandomSpeedPredictionPerTrueWindSpeed.put(tws, ORCCertificateImpl.NAUTICAL_MILE.inTime( - new SecondsDurationImpl(Double.parseDouble(certificateValues.getValue(circularRandomKey))))); - nonSpinnakerSpeedPredictionPerTrueWindSpeed.put(tws, ORCCertificateImpl.NAUTICAL_MILE.inTime( - new SecondsDurationImpl(Double.parseDouble(certificateValues.getValue(nonSpinnakerKey))))); - Map velocityPredictionPerTrueWindAngle = new HashMap<>(); - for (Bearing twa : ORCCertificate.ALLOWANCES_TRUE_WIND_ANGLES ) { - String twaCoursesKey = TWA_COURSES + Integer.toString((int) twa.getDegrees()) + windSpeed; - velocityPredictionPerTrueWindAngle.put(twa, ORCCertificateImpl.NAUTICAL_MILE.inTime( - new SecondsDurationImpl(Double.parseDouble(certificateValues.getValue(twaCoursesKey))))); + final ORCCertificateValues certificateValues = getValuesForCertificateId(certificateId); + final ORCCertificate result; + if (certificateValues == null) { + result = null; + } else { + final String sailNumber = certificateValues.getValue(SAILNUMBER); + final String boatclass = certificateValues.getValue(BOATCLASS); + final String boatName = certificateValues.getValue(BOATNAME); + final Distance length = new MeterDistance(Double.parseDouble(certificateValues.getValue(LENGTH))); + final Duration gph = new SecondsDurationImpl(Double.parseDouble(certificateValues.getValue(GPH))); + final Double cdl = Double.parseDouble(certificateValues.getValue(CDL)); + String dateString = certificateValues.getValue(ISSUEDATE); + String timeString = certificateValues.getValue(ISSUETIME); + TimePoint issueDate; + try { + issueDate = new MillisecondsTimePoint(timestampFormat.parse(dateString+" "+timeString+"+0000")); // assume UTC + } catch (ParseException e) { + issueDate = null; } - velocityPredictionsPerTrueWindSpeedAndAngle.put(tws, velocityPredictionPerTrueWindAngle); + final Map> velocityPredictionsPerTrueWindSpeedAndAngle = new HashMap<>(); + final Map beatAngles = new HashMap<>(); + final Map beatVMGPredictionPerTrueWindSpeed = new HashMap<>(); + final Map beatAllowancePerTrueWindSpeed = new HashMap<>(); + final Map runAngles = new HashMap<>(); + final Map runVMGPredictionPerTrueWindSpeed = new HashMap<>(); + final Map runAllowancePerTrueWindSpeed = new HashMap<>(); + final Map windwardLeewardSpeedPredictionPerTrueWindSpeed = new HashMap<>(); + final Map longDistanceSpeedPredictionPerTrueWindSpeed = new HashMap<>(); + final Map circularRandomSpeedPredictionPerTrueWindSpeed = new HashMap<>(); + final Map nonSpinnakerSpeedPredictionPerTrueWindSpeed = new HashMap<>(); + for (Speed tws : ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS) { + String windSpeed = Integer.toString((int) tws.getKnots()); + String beatAngleKey = BEAT_ANGLE + windSpeed; + String beatAllowanceKey = BEAT_ALLOWANCE + windSpeed; + String runAngleKey = RUN_ANGLE + windSpeed; + String runAllowanceKey = RUN_ALLOWANCE + windSpeed; + String windwardLeewardKey = WINDWARD_LEEWARD + windSpeed; + String longDistanceKey = LONG_DISTANCE + windSpeed; + String circularRandomKey = CIRCULAR_RANDOM + windSpeed; + String nonSpinnakerKey = NON_SPINNAKER + windSpeed; + beatAngles.put(tws, new DegreeBearingImpl(Double.parseDouble(certificateValues.getValue(beatAngleKey)))); + beatAllowancePerTrueWindSpeed.put(tws, new SecondsDurationImpl(Double.parseDouble(certificateValues.getValue(beatAllowanceKey)))); + beatVMGPredictionPerTrueWindSpeed.put(tws, ORCCertificateImpl.NAUTICAL_MILE.inTime(beatAllowancePerTrueWindSpeed.get(tws))); + runAngles.put(tws, new DegreeBearingImpl(Double.parseDouble(certificateValues.getValue(runAngleKey)))); + runAllowancePerTrueWindSpeed.put(tws, new SecondsDurationImpl(Double.parseDouble(certificateValues.getValue(runAllowanceKey)))); + runVMGPredictionPerTrueWindSpeed.put(tws, ORCCertificateImpl.NAUTICAL_MILE.inTime(runAllowancePerTrueWindSpeed.get(tws))); + windwardLeewardSpeedPredictionPerTrueWindSpeed.put(tws, ORCCertificateImpl.NAUTICAL_MILE.inTime( + new SecondsDurationImpl(Double.parseDouble(certificateValues.getValue(windwardLeewardKey))))); + longDistanceSpeedPredictionPerTrueWindSpeed.put(tws, ORCCertificateImpl.NAUTICAL_MILE.inTime( + new SecondsDurationImpl(Double.parseDouble(certificateValues.getValue(longDistanceKey))))); + circularRandomSpeedPredictionPerTrueWindSpeed.put(tws, ORCCertificateImpl.NAUTICAL_MILE.inTime( + new SecondsDurationImpl(Double.parseDouble(certificateValues.getValue(circularRandomKey))))); + nonSpinnakerSpeedPredictionPerTrueWindSpeed.put(tws, ORCCertificateImpl.NAUTICAL_MILE.inTime( + new SecondsDurationImpl(Double.parseDouble(certificateValues.getValue(nonSpinnakerKey))))); + Map velocityPredictionPerTrueWindAngle = new HashMap<>(); + for (Bearing twa : ORCCertificate.ALLOWANCES_TRUE_WIND_ANGLES ) { + String twaCoursesKey = TWA_COURSES + Integer.toString((int) twa.getDegrees()) + windSpeed; + velocityPredictionPerTrueWindAngle.put(twa, ORCCertificateImpl.NAUTICAL_MILE.inTime( + new SecondsDurationImpl(Double.parseDouble(certificateValues.getValue(twaCoursesKey))))); + } + velocityPredictionsPerTrueWindSpeedAndAngle.put(tws, velocityPredictionPerTrueWindAngle); + } + result = new ORCCertificateImpl(certificateValues.getValue(NATCERTN_FILE_ID), sailNumber, boatName, boatclass, + length, gph, cdl, issueDate, velocityPredictionsPerTrueWindSpeedAndAngle, beatAngles, beatVMGPredictionPerTrueWindSpeed, + beatAllowancePerTrueWindSpeed, runAngles, runVMGPredictionPerTrueWindSpeed, + runAllowancePerTrueWindSpeed, windwardLeewardSpeedPredictionPerTrueWindSpeed, + longDistanceSpeedPredictionPerTrueWindSpeed, circularRandomSpeedPredictionPerTrueWindSpeed, nonSpinnakerSpeedPredictionPerTrueWindSpeed); } - return new ORCCertificateImpl(certificateValues.getValue(NATCERTN_FILE_ID), sailNumber, boatName, boatclass, - length, gph, cdl, issueDate, velocityPredictionsPerTrueWindSpeedAndAngle, beatAngles, beatVMGPredictionPerTrueWindSpeed, - beatAllowancePerTrueWindSpeed, runAngles, runVMGPredictionPerTrueWindSpeed, - runAllowancePerTrueWindSpeed, windwardLeewardSpeedPredictionPerTrueWindSpeed, - longDistanceSpeedPredictionPerTrueWindSpeed, circularRandomSpeedPredictionPerTrueWindSpeed, nonSpinnakerSpeedPredictionPerTrueWindSpeed); + return result; } @Override From a320ff053473afeccad0dc8838c6e8498594aa4d Mon Sep 17 00:00:00 2001 From: Axel Uhl Date: Tue, 10 Mar 2020 11:14:07 +0100 Subject: [PATCH 27/33] increased issue date tolerance to 10s as we saw 5s already --- .../sap/sailing/domain/orc/TestORCPerformanceCurve.java | 4 ++-- .../domain/orc/TestORCPublicCertificateDatabase.java | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java index 99bc89c4f2d..71860b23c6d 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java @@ -56,7 +56,7 @@ public class TestORCPerformanceCurve { private static final String RESOURCES = "resources/orc/"; @Rule - public FailIfNoValidOrcCertificateRule customIgnoreRule = new FailIfNoValidOrcCertificateRule(); + public FailIfNoValidOrcCertificateRule customFailRule = new FailIfNoValidOrcCertificateRule(); @Rule public ErrorCollector collector = new ErrorCollector(); @@ -98,7 +98,7 @@ public class TestORCPerformanceCurve { File fileWithSpecificBins = new File(RESOURCES + "newFormatCertificate.json"); importerWithSpecificBins = new ORCCertificatesJsonImporter().read(new FileInputStream(fileWithSpecificBins)); // Online File: - importerOnline = new ORCCertificatesJsonImporter().read(new URL("https://data.orc.org/public/WPub.dll?action=DownRMS&CountryId=GER&ext=json").openStream()); + importerOnline = new ORCCertificatesJsonImporter().read(new URL("https://data.orc.org/public/WPub.dll?action=DownBoatRMS&CountryId=GER&ext=json").openStream()); } @Test diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java index 0d9b83317fa..263175161ac 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java @@ -148,7 +148,12 @@ public class TestORCPublicCertificateDatabase { final CertificateHandle handle = db.getCertificateHandle(referenceNumber); final ORCCertificate result = db.getCertificate(referenceNumber); assertEquals(handle.getGPH(), result.getGPH().asSeconds(), 0.00001); - assertEquals(handle.getIssueDate().asMillis(), result.getIssueDate().asMillis(), 1000.0); + // Use some tolerance as we found differences as much as 5s between the dxtDate in the handle coming from the XML search result + // and the IssueDate field in the JSON. Both suggest to report millisecond accuracy, but dxtDate always seems to have the + // milliseconds as "000" explaining many sub-second differences. But in some cases differences were significantly bigger. + assertEquals("Issue dates of certificate with reference number "+referenceNumber+ + " varies between current year result handle ("+handle.getIssueDate()+") and certificate ("+ + result.getIssueDate()+").", handle.getIssueDate().asMillis(), result.getIssueDate().asMillis(), 10000.0); assertEquals(handle.getSailNumber(), result.getSailNumber()); } From 91471c5d45f0faad241102857157701037039615 Mon Sep 17 00:00:00 2001 From: Axel Uhl Date: Tue, 10 Mar 2020 15:53:30 +0100 Subject: [PATCH 28/33] canonicalize certificate IDs internally to ignore whitespace when resolving --- .../AbstractORCCertificateImporterTest.java | 2 +- .../orc/FailIfNoValidOrcCertificateRule.java | 2 - .../AbstractORCCertificatesCollection.java | 4 + .../impl/ORCCertificatesCollectionJSON.java | 296 +++++++++--------- .../impl/ORCCertificatesCollectionRMS.java | 13 +- 5 files changed, 168 insertions(+), 149 deletions(-) diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/AbstractORCCertificateImporterTest.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/AbstractORCCertificateImporterTest.java index 6c817d76770..8b794c4b3d0 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/AbstractORCCertificateImporterTest.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/AbstractORCCertificateImporterTest.java @@ -36,7 +36,7 @@ public abstract class AbstractORCCertificateImporterTest { Collection certificates = customIgnoreRule.getAvailableCerts(); final ORCCertificate cert = certificates.stream().findFirst().get(); String countryCode = cert.getId().substring(0, 3); - String url = String.format("https://data.orc.org/public/WPub.dll?action=DownBoatRMS&CountryId=%s&ext="+rmsOrJsonFormat, countryCode); + String url = String.format("https://data.orc.org/public/WPub.dll?action=DownBoatRMS&CountryId=%s&ext=%s", countryCode, rmsOrJsonFormat); ORCCertificatesCollection importer = ORCCertificatesImporter.INSTANCE.read(new URL(url).openStream()); ORCCertificate someValidCert = null, referenceCert = null; for (ORCCertificate orcCertificate : certificates) { diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/FailIfNoValidOrcCertificateRule.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/FailIfNoValidOrcCertificateRule.java index 3a07f191d65..e447138f6f8 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/FailIfNoValidOrcCertificateRule.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/FailIfNoValidOrcCertificateRule.java @@ -87,7 +87,5 @@ public class FailIfNoValidOrcCertificateRule implements TestRule { Assert.assertTrue(certificateExists); base.evaluate(); } - - } } \ No newline at end of file diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/AbstractORCCertificatesCollection.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/AbstractORCCertificatesCollection.java index 55036c0e5c0..0b55f443368 100755 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/AbstractORCCertificatesCollection.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/AbstractORCCertificatesCollection.java @@ -24,6 +24,10 @@ public abstract class AbstractORCCertificatesCollection implements ORCCertificat } return null; } + + protected String canonicalizeId(String certificateId) { + return certificateId.replaceAll(" ", "").replaceAll("\t", ""); + } @Override public Iterable getCertificates() { diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCCertificatesCollectionJSON.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCCertificatesCollectionJSON.java index 800fb6cd15c..df040a0d5db 100755 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCCertificatesCollectionJSON.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCCertificatesCollectionJSON.java @@ -70,6 +70,9 @@ public class ORCCertificatesCollectionJSON extends AbstractORCCertificatesCollec */ private static final Pattern twaPattern = Pattern.compile("R(\\d+(\\.\\d+)?)"); + /** + * Keys are canonicalized through {@link #canonicalizeId(String)}). + */ private Map certificateJsonObjectsByCertificateId; /** @@ -79,7 +82,7 @@ public class ORCCertificatesCollectionJSON extends AbstractORCCertificatesCollec public ORCCertificatesCollectionJSON(Iterable data) { this.certificateJsonObjectsByCertificateId = new HashMap<>(); for (final JSONObject o : data) { - if (this.certificateJsonObjectsByCertificateId.put(getId(o), o) != null) { + if (this.certificateJsonObjectsByCertificateId.put(canonicalizeId(getId(o)), o) != null) { throw new IllegalArgumentException("Certificate ID in collection not unique: " + getId(o)); } } @@ -88,6 +91,11 @@ public class ORCCertificatesCollectionJSON extends AbstractORCCertificatesCollec /** * Returns an {@link ORCCertificateImpl} object for a given {@link String} key. If there is no map value for the * given key, the method returns {@link null}. + * + * @param certificateId + * internally, the ID is "canonicalized" by removing all whitespace from it, so, e.g., "FIN11826L11826" + * is considered equivalent to "FIN11826 L11826". This helps iron out some subtle differences in how RMS + * files, JSON files and XML-based search results represent certificate IDs. */ @Override public ORCCertificate getCertificateById(String certificateId) { @@ -105,155 +113,157 @@ public class ORCCertificatesCollectionJSON extends AbstractORCCertificatesCollec final Map trueWindSpeedMap = new TreeMap(); // Using TreeMap implementation to save the TrueWindSpeed // so that it first sorts the entries as well as avoids duplicates final Map trueWindAngleMap = new TreeMap(); // Same like trueWindSpeedMap - final JSONObject object = certificateJsonObjectsByCertificateId.get(certificateId); + final JSONObject object = certificateJsonObjectsByCertificateId.get(canonicalizeId(certificateId)); + final ORCCertificate result; if (object == null) { - // TODO Throw Exception for certificate by id not found. InvalidArgumentException? - return null; - } - String sailNumber = null; - for (Entry entry : object.entrySet()) { - switch ((String) entry.getKey()) { - case SAIL_NO: - sailNumber = entry.getValue() == null ? null : entry.getValue().toString(); - break; - case LOA: - length = new MeterDistance(((Number) entry.getValue()).doubleValue()); - break; - case YACHT_NAME: - boatName = entry.getValue() == null ? null : entry.getValue().toString(); - break; - case CLASS: - boatclass = (String) entry.getValue(); - break; - case GPH2: - gph = new SecondsDurationImpl(((Number) entry.getValue()).doubleValue()); - break; - case CDL2: - cdl = ((Number) entry.getValue()).doubleValue(); - break; - case ISSUE_DATE: - Date date = DatatypeConverter.parseDateTime((String) entry.getValue()).getTime(); - issueDate = new MillisecondsTimePoint(date); - break; - case ALLOWANCES: - final JSONObject allowances = (JSONObject) object.get(ALLOWANCES); - // calculating the TWA and TWS before calculating the other dependent values in allowances object - final Object windSpeedsObject = allowances.get(WIND_SPEEDS); - if (windSpeedsObject != null) { - final JSONArray twsArray = (JSONArray) windSpeedsObject; - for (final Object twsObject : twsArray) { - Double twsValue = ((Number) twsObject).doubleValue(); - trueWindSpeedMap.put(twsValue, new KnotSpeedImpl(twsValue)); + result = null; + } else { + String sailNumber = null; + for (Entry entry : object.entrySet()) { + switch ((String) entry.getKey()) { + case SAIL_NO: + sailNumber = entry.getValue() == null ? null : entry.getValue().toString(); + break; + case LOA: + length = new MeterDistance(((Number) entry.getValue()).doubleValue()); + break; + case YACHT_NAME: + boatName = entry.getValue() == null ? null : entry.getValue().toString(); + break; + case CLASS: + boatclass = (String) entry.getValue(); + break; + case GPH2: + gph = new SecondsDurationImpl(((Number) entry.getValue()).doubleValue()); + break; + case CDL2: + cdl = ((Number) entry.getValue()).doubleValue(); + break; + case ISSUE_DATE: + Date date = DatatypeConverter.parseDateTime((String) entry.getValue()).getTime(); + issueDate = new MillisecondsTimePoint(date); + break; + case ALLOWANCES: + final JSONObject allowances = (JSONObject) object.get(ALLOWANCES); + // calculating the TWA and TWS before calculating the other dependent values in allowances object + final Object windSpeedsObject = allowances.get(WIND_SPEEDS); + if (windSpeedsObject != null) { + final JSONArray twsArray = (JSONArray) windSpeedsObject; + for (final Object twsObject : twsArray) { + Double twsValue = ((Number) twsObject).doubleValue(); + trueWindSpeedMap.put(twsValue, new KnotSpeedImpl(twsValue)); + } + } else { + for (Speed speed : ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS) { + trueWindSpeedMap.put(speed.getKnots(), speed); + } } - } else { - for (Speed speed : ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS) { - trueWindSpeedMap.put(speed.getKnots(), speed); + final Double [] trueWindSpeedArray = new Double[trueWindSpeedMap.size()]; + trueWindSpeedMap.keySet().toArray(trueWindSpeedArray); + for (final Object aKey : allowances.keySet()) { + String keyString = (String) aKey; + final Matcher matcher = twaPattern.matcher(keyString); + if (matcher.lookingAt()) { + final JSONArray array = (JSONArray) allowances.get(keyString); + final Map twsMap = new HashMap<>(); + for (int i = 0; i < array.size(); i++) { + final Double allowanceValue = ((Number) array.get(i)).doubleValue(); + twsMap.put(trueWindSpeedMap.get(trueWindSpeedArray[i]), new SecondsDurationImpl(allowanceValue)); + } + final Double trueWindAngleValue = Double.parseDouble(matcher.group(1)); + trueWindAngleMap.put(trueWindAngleValue, new DegreeBearingImpl(trueWindAngleValue)); + allowanceDurationsPerTrueWindAngleAndSpeed.put(trueWindAngleMap.get(trueWindAngleValue), twsMap); + } } + // Now read the various allowances, assuming we find values for the TWS "bins" identified above + for (final Object aKey : allowances.keySet()) { + final JSONArray array = (JSONArray) allowances.get(aKey); + if (((String) aKey).equals(BEAT_ANGLE)) { + for (int i = 0; i < array.size(); i++) { + beatAngles.put(new KnotSpeedImpl(trueWindSpeedArray[i]), + new DegreeBearingImpl(((Number) array.get(i)).doubleValue())); + } + continue; + } + if (((String) aKey).equals(GYBE_ANGLE)) { + for (int i = 0; i < array.size(); i++) { + gybeAngles.put(new KnotSpeedImpl(trueWindSpeedArray[i]), + new DegreeBearingImpl(((Number) array.get(i)).doubleValue())); + } + continue; + } + Map twsMap = new HashMap<>(); + // ignore true wind angles key + if (!twaPattern.matcher((String) aKey).lookingAt()) { + for (int i = 0; i < array.size(); i++) { + twsMap.put(new KnotSpeedImpl(trueWindSpeedArray[i]), + new SecondsDurationImpl(((Number) array.get(i)).doubleValue())); + } + } + switch ((String) aKey) { + case BEAT: + case RUN: + predefinedAllowanceDurationsPerTrueWindSpeed.put((String) aKey, twsMap); + break; + default: + predefinedAllowanceDurationsPerTrueWindSpeed.put((String) aKey, twsMap); + break; + } + } + break; + default: + break; } - final Double [] trueWindSpeedArray = new Double[trueWindSpeedMap.size()]; - trueWindSpeedMap.keySet().toArray(trueWindSpeedArray); - for (final Object aKey : allowances.keySet()) { - String keyString = (String) aKey; - final Matcher matcher = twaPattern.matcher(keyString); - if (matcher.lookingAt()) { - final JSONArray array = (JSONArray) allowances.get(keyString); - final Map twsMap = new HashMap<>(); - for (int i = 0; i < array.size(); i++) { - final Double allowanceValue = ((Number) array.get(i)).doubleValue(); - twsMap.put(trueWindSpeedMap.get(trueWindSpeedArray[i]), new SecondsDurationImpl(allowanceValue)); - } - final Double trueWindAngleValue = Double.parseDouble(matcher.group(1)); - trueWindAngleMap.put(trueWindAngleValue, new DegreeBearingImpl(trueWindAngleValue)); - allowanceDurationsPerTrueWindAngleAndSpeed.put(trueWindAngleMap.get(trueWindAngleValue), twsMap); - } - } - // Now read the various allowances, assuming we find values for the TWS "bins" identified above - for (final Object aKey : allowances.keySet()) { - final JSONArray array = (JSONArray) allowances.get(aKey); - if (((String) aKey).equals(BEAT_ANGLE)) { - for (int i = 0; i < array.size(); i++) { - beatAngles.put(new KnotSpeedImpl(trueWindSpeedArray[i]), - new DegreeBearingImpl(((Number) array.get(i)).doubleValue())); - } - continue; - } - if (((String) aKey).equals(GYBE_ANGLE)) { - for (int i = 0; i < array.size(); i++) { - gybeAngles.put(new KnotSpeedImpl(trueWindSpeedArray[i]), - new DegreeBearingImpl(((Number) array.get(i)).doubleValue())); - } - continue; - } - Map twsMap = new HashMap<>(); - //ignore true wind angles key - if (!twaPattern.matcher((String) aKey).lookingAt()) { - for (int i = 0; i < array.size(); i++) { - twsMap.put(new KnotSpeedImpl(trueWindSpeedArray[i]), - new SecondsDurationImpl(((Number) array.get(i)).doubleValue())); - } - } - switch ((String) aKey) { - case BEAT: - case RUN: - predefinedAllowanceDurationsPerTrueWindSpeed.put((String) aKey, twsMap); - break; - default: - predefinedAllowanceDurationsPerTrueWindSpeed.put((String) aKey, twsMap); - break; - } - } - break; - default: - break; } - } - trueWindSpeedMap.values().forEach(tws -> velocityPredictionPerTrueWindSpeedAndAngle.put(tws, new HashMap<>())); - for (final Bearing keyTWA : allowanceDurationsPerTrueWindAngleAndSpeed.keySet()) { - for (final Speed keyTWS : allowanceDurationsPerTrueWindAngleAndSpeed.get(keyTWA).keySet()) { - velocityPredictionPerTrueWindSpeedAndAngle.get(keyTWS).put(keyTWA, ORCCertificateImpl.NAUTICAL_MILE - .inTime(allowanceDurationsPerTrueWindAngleAndSpeed.get(keyTWA).get(keyTWS))); + trueWindSpeedMap.values().forEach(tws -> velocityPredictionPerTrueWindSpeedAndAngle.put(tws, new HashMap<>())); + for (final Bearing keyTWA : allowanceDurationsPerTrueWindAngleAndSpeed.keySet()) { + for (final Speed keyTWS : allowanceDurationsPerTrueWindAngleAndSpeed.get(keyTWA).keySet()) { + velocityPredictionPerTrueWindSpeedAndAngle.get(keyTWS).put(keyTWA, ORCCertificateImpl.NAUTICAL_MILE + .inTime(allowanceDurationsPerTrueWindAngleAndSpeed.get(keyTWA).get(keyTWS))); + } } + final Map beatVMGPredictionPerTrueWindSpeed = new HashMap<>(); + final Map beatAllowancePerTrueWindSpeed = new HashMap<>(); + final Map runVMGPredictionPerTrueWindSpeed = new HashMap<>(); + final Map runAllowancePerTrueWindSpeed = new HashMap<>(); + final Map windwardLeewardSpeedPredictionPerTrueWindSpeed = new HashMap<>(); + final Map longDistanceSpeedPredictionPerTrueWindSpeed = new HashMap<>(); + final Map circularRandomSpeedPredictionPerTrueWindSpeed = new HashMap<>(); + final Map nonSpinnakerSpeedPredictionPerTrueWindSpeed = new HashMap<>(); + for (final Speed tws : velocityPredictionPerTrueWindSpeedAndAngle.keySet()) { + beatVMGPredictionPerTrueWindSpeed.put(tws, ORCCertificateImpl.NAUTICAL_MILE + .inTime(predefinedAllowanceDurationsPerTrueWindSpeed.get(BEAT).get(tws))); + beatAllowancePerTrueWindSpeed.put(tws, predefinedAllowanceDurationsPerTrueWindSpeed.get(BEAT).get(tws)); + runVMGPredictionPerTrueWindSpeed.put(tws, ORCCertificateImpl.NAUTICAL_MILE + .inTime(predefinedAllowanceDurationsPerTrueWindSpeed.get(RUN).get(tws))); + runAllowancePerTrueWindSpeed.put(tws, predefinedAllowanceDurationsPerTrueWindSpeed.get(RUN).get(tws)); + windwardLeewardSpeedPredictionPerTrueWindSpeed.put(tws, ORCCertificateImpl.NAUTICAL_MILE + .inTime(predefinedAllowanceDurationsPerTrueWindSpeed.get(WINDWARD_LEEWARD).get(tws))); + longDistanceSpeedPredictionPerTrueWindSpeed.put(tws, ORCCertificateImpl.NAUTICAL_MILE + .inTime(predefinedAllowanceDurationsPerTrueWindSpeed.get(LONG_DISTANCE).get(tws))); + circularRandomSpeedPredictionPerTrueWindSpeed.put(tws, ORCCertificateImpl.NAUTICAL_MILE + .inTime(predefinedAllowanceDurationsPerTrueWindSpeed.get(CIRCULAR_RANDOM).get(tws))); + nonSpinnakerSpeedPredictionPerTrueWindSpeed.put(tws, ORCCertificateImpl.NAUTICAL_MILE + .inTime(predefinedAllowanceDurationsPerTrueWindSpeed.get(NON_SPINNAKER).get(tws))); + } + final Bearing[] dynamicAllowancesTrueWindAngles = trueWindAngleMap.values().toArray(new Bearing[trueWindAngleMap.values().size()]); + final Speed[] dynamicAllowancesTrueWindSpeeds = trueWindSpeedMap.values().toArray(new Speed[trueWindSpeedMap.values().size()]); + // replace array by the default arrays if they match; this will save the space for the dynamically-parsed arrays + result = new ORCCertificateImpl( + Arrays.equals(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS, dynamicAllowancesTrueWindSpeeds) + ? ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS + : dynamicAllowancesTrueWindSpeeds, + Arrays.equals(ORCCertificate.ALLOWANCES_TRUE_WIND_ANGLES, dynamicAllowancesTrueWindAngles) + ? ORCCertificate.ALLOWANCES_TRUE_WIND_ANGLES + : dynamicAllowancesTrueWindAngles, + getId(object), sailNumber, boatName, boatclass, length, gph, cdl, issueDate, + velocityPredictionPerTrueWindSpeedAndAngle, beatAngles, beatVMGPredictionPerTrueWindSpeed, + beatAllowancePerTrueWindSpeed, gybeAngles, runVMGPredictionPerTrueWindSpeed, + runAllowancePerTrueWindSpeed, windwardLeewardSpeedPredictionPerTrueWindSpeed, + longDistanceSpeedPredictionPerTrueWindSpeed, circularRandomSpeedPredictionPerTrueWindSpeed, + nonSpinnakerSpeedPredictionPerTrueWindSpeed); } - final Map beatVMGPredictionPerTrueWindSpeed = new HashMap<>(); - final Map beatAllowancePerTrueWindSpeed = new HashMap<>(); - final Map runVMGPredictionPerTrueWindSpeed = new HashMap<>(); - final Map runAllowancePerTrueWindSpeed = new HashMap<>(); - final Map windwardLeewardSpeedPredictionPerTrueWindSpeed = new HashMap<>(); - final Map longDistanceSpeedPredictionPerTrueWindSpeed = new HashMap<>(); - final Map circularRandomSpeedPredictionPerTrueWindSpeed = new HashMap<>(); - final Map nonSpinnakerSpeedPredictionPerTrueWindSpeed = new HashMap<>(); - for (final Speed tws : velocityPredictionPerTrueWindSpeedAndAngle.keySet()) { - beatVMGPredictionPerTrueWindSpeed.put(tws, ORCCertificateImpl.NAUTICAL_MILE - .inTime(predefinedAllowanceDurationsPerTrueWindSpeed.get(BEAT).get(tws))); - beatAllowancePerTrueWindSpeed.put(tws, predefinedAllowanceDurationsPerTrueWindSpeed.get(BEAT).get(tws)); - runVMGPredictionPerTrueWindSpeed.put(tws, ORCCertificateImpl.NAUTICAL_MILE - .inTime(predefinedAllowanceDurationsPerTrueWindSpeed.get(RUN).get(tws))); - runAllowancePerTrueWindSpeed.put(tws, predefinedAllowanceDurationsPerTrueWindSpeed.get(RUN).get(tws)); - windwardLeewardSpeedPredictionPerTrueWindSpeed.put(tws, ORCCertificateImpl.NAUTICAL_MILE - .inTime(predefinedAllowanceDurationsPerTrueWindSpeed.get(WINDWARD_LEEWARD).get(tws))); - longDistanceSpeedPredictionPerTrueWindSpeed.put(tws, ORCCertificateImpl.NAUTICAL_MILE - .inTime(predefinedAllowanceDurationsPerTrueWindSpeed.get(LONG_DISTANCE).get(tws))); - circularRandomSpeedPredictionPerTrueWindSpeed.put(tws, ORCCertificateImpl.NAUTICAL_MILE - .inTime(predefinedAllowanceDurationsPerTrueWindSpeed.get(CIRCULAR_RANDOM).get(tws))); - nonSpinnakerSpeedPredictionPerTrueWindSpeed.put(tws, ORCCertificateImpl.NAUTICAL_MILE - .inTime(predefinedAllowanceDurationsPerTrueWindSpeed.get(NON_SPINNAKER).get(tws))); - } - final Bearing[] dynamicAllowancesTrueWindAngles = trueWindAngleMap.values().toArray(new Bearing[trueWindAngleMap.values().size()]); - final Speed[] dynamicAllowancesTrueWindSpeeds = trueWindSpeedMap.values().toArray(new Speed[trueWindSpeedMap.values().size()]); - // replace array by the default arrays if they match; this will save the space for the dynamically-parsed arrays - return new ORCCertificateImpl( - Arrays.equals(ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS, dynamicAllowancesTrueWindSpeeds) - ? ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS - : dynamicAllowancesTrueWindSpeeds, - Arrays.equals(ORCCertificate.ALLOWANCES_TRUE_WIND_ANGLES, dynamicAllowancesTrueWindAngles) - ? ORCCertificate.ALLOWANCES_TRUE_WIND_ANGLES - : dynamicAllowancesTrueWindAngles, - getId(object), sailNumber, boatName, boatclass, length, gph, cdl, issueDate, - velocityPredictionPerTrueWindSpeedAndAngle, beatAngles, beatVMGPredictionPerTrueWindSpeed, - beatAllowancePerTrueWindSpeed, gybeAngles, runVMGPredictionPerTrueWindSpeed, - runAllowancePerTrueWindSpeed, windwardLeewardSpeedPredictionPerTrueWindSpeed, - longDistanceSpeedPredictionPerTrueWindSpeed, circularRandomSpeedPredictionPerTrueWindSpeed, - nonSpinnakerSpeedPredictionPerTrueWindSpeed); + return result; } private String getId(JSONObject certificateAsJson) { diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCCertificatesCollectionRMS.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCCertificatesCollectionRMS.java index c3023550d1a..7864e8e75e0 100755 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCCertificatesCollectionRMS.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCCertificatesCollectionRMS.java @@ -63,15 +63,22 @@ public class ORCCertificatesCollectionRMS extends AbstractORCCertificatesCollect private static final String NON_SPINNAKER = "NSP"; private static final String NATCERTN_FILE_ID = "NATCERTN.FILE_ID"; private static final DateFormat timestampFormat = new SimpleDateFormat("dd MM yyyy HH:mm:ssZ"); + + /** + * Keys are canonicalized through {@link #canonicalizeId(String)}. + */ private final Map> certificateValuesByCertificateId; public class ORCCertificateValues { + /** + * Canonicalized through {@link AbstractORCCertificatesCollection#canonicalizeId(String)} + */ private final String certificateId; public ORCCertificateValues(String certificateId) { super(); - this.certificateId = certificateId; + this.certificateId = canonicalizeId(certificateId); } public String getValue(String columnName) { @@ -82,7 +89,7 @@ public class ORCCertificatesCollectionRMS extends AbstractORCCertificatesCollect public ORCCertificatesCollectionRMS(Map> certificateValuesByCertificateId) throws IOException { this.certificateValuesByCertificateId = new HashMap<>(); for (final Entry> e : certificateValuesByCertificateId.entrySet()) { - this.certificateValuesByCertificateId.put(e.getKey(), e.getValue()); + this.certificateValuesByCertificateId.put(canonicalizeId(e.getKey()), e.getValue()); } } @@ -91,7 +98,7 @@ public class ORCCertificatesCollectionRMS extends AbstractORCCertificatesCollect } private ORCCertificateValues getValuesForCertificateId(String certificateId) { - return certificateValuesByCertificateId.containsKey(certificateId) ? new ORCCertificateValues(certificateId) : null; + return certificateValuesByCertificateId.containsKey(canonicalizeId(certificateId)) ? new ORCCertificateValues(certificateId) : null; } @Override From b0ecf948805fe7900640e2efbce2d3e4bb8ff9fc Mon Sep 17 00:00:00 2001 From: Axel Uhl Date: Tue, 10 Mar 2020 18:35:58 +0100 Subject: [PATCH 29/33] added TODO for CanSelect for filtering for active certificates only --- .../domain/orc/impl/ORCPublicCertificateDatabaseImpl.java | 1 + 1 file changed, 1 insertion(+) diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java index ac642fc5e6f..33c2c8a06ea 100755 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java @@ -285,6 +285,7 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat for (int rowNodeIndex=0; rowNodeIndexTrue CERTIFICATE_IS_ACTIVE result.add(parseHandle(rowNode)); } } From ffc00ddbf09667dae8beb2f795c42344f1c48466 Mon Sep 17 00:00:00 2001 From: Axel Uhl Date: Wed, 11 Mar 2020 00:13:24 +0100 Subject: [PATCH 30/33] use RefNo as ID for ORC certificates; distinguish valid/invalid in certificate search --- .../domain/common/orc/ORCCertificate.java | 39 ++++-- .../common/orc/impl/ORCCertificateImpl.java | 54 ++++++--- .../AbstractORCCertificateImporterTest.java | 4 +- .../orc/FailIfNoValidOrcCertificateRule.java | 2 +- .../orc/ORCPerformanceCurveRankingTest.java | 2 +- .../orc/TestORCCertificateImporterJSON.java | 2 +- .../orc/TestORCCertificateImporterRMS.java | 2 +- .../domain/orc/TestORCPerformanceCurve.java | 68 +++++------ .../orc/TestORCPublicCertificateDatabase.java | 10 +- .../orc/ORCPublicCertificateDatabase.java | 21 +++- .../impl/ORCCertificatesCollectionJSON.java | 27 +++-- .../impl/ORCCertificatesCollectionRMS.java | 30 ++++- .../orc/impl/ORCCertificatesRmsImporter.java | 112 ++++++++++++------ .../ORCPublicCertificateDatabaseImpl.java | 53 +++++++-- ...CertificateImpl_CustomFieldSerializer.java | 19 +-- .../gwt/ui/server/SailingServiceImpl.java | 2 +- .../impl/ORCCertificateJsonDeserializer.java | 20 ++-- .../impl/ORCCertificateJsonSerializer.java | 6 +- 18 files changed, 322 insertions(+), 151 deletions(-) diff --git a/java/com.sap.sailing.domain.common/src/com/sap/sailing/domain/common/orc/ORCCertificate.java b/java/com.sap.sailing.domain.common/src/com/sap/sailing/domain/common/orc/ORCCertificate.java index 61320a5419b..76c215bde8e 100755 --- a/java/com.sap.sailing.domain.common/src/com/sap/sailing/domain/common/orc/ORCCertificate.java +++ b/java/com.sap.sailing.domain.common/src/com/sap/sailing/domain/common/orc/ORCCertificate.java @@ -5,10 +5,12 @@ import java.util.Map; import com.sap.sailing.domain.common.impl.KnotSpeedImpl; import com.sap.sse.common.Bearing; +import com.sap.sse.common.CountryCode; import com.sap.sse.common.Distance; import com.sap.sse.common.Duration; import com.sap.sse.common.Speed; import com.sap.sse.common.TimePoint; +import com.sap.sse.common.Util.Pair; import com.sap.sse.common.WithID; import com.sap.sse.common.impl.DegreeBearingImpl; @@ -24,10 +26,9 @@ import com.sap.sse.common.impl.DegreeBearingImpl; * {@link RankingMetric}. *

* - * The {@link WithID} interface is to be implemented such that a {@link String} is produced as the ID that contains the - * concatenation (without intermediate white space) of the {@code NatAuth}, the {@code CertNo} and the {@code BIN} - * fields, as provided as single fields in the JSON representation, and as provided in the concatenated form in the - * {@code NATCERTN.FILE_ID} column in the RMS format. + * The {@link WithID} interface is to be implemented based on the {@link #getReferenceNumber()} which is unique across the + * ORC world-wide certificate database. Note that the {@link #getBoatKey()} consisting of the {@link #getFileId()} and the + * {@link #getIssuingCountry()} field values is a unique key for the boat to which the certificate belongs. *

* * The certificate contains time allowances (or, conversely, speed predictions) for a combination of true wind speeds @@ -64,14 +65,38 @@ public interface ORCCertificate extends WithID, Serializable { Bearing[] ALLOWANCES_TRUE_WIND_ANGLES = { new DegreeBearingImpl(52), new DegreeBearingImpl(60), new DegreeBearingImpl(75), new DegreeBearingImpl(90), new DegreeBearingImpl(110), new DegreeBearingImpl(120), new DegreeBearingImpl(135), new DegreeBearingImpl(150) }; + /** - * As the ID of an ORC certificate we use the concatenation (without intermediate white space) of the - * {@code NatAuth}, the {@code CertNo} and the {@code BIN} fields, as provided as single fields in the JSON - * representation + * We use the {@link #getReferenceNumber() reference number ("RefNo")} as the ID of a certificate document. + * It is unique for all of ORC's certificates issued world-wide. */ @Override String getId(); + + String getReferenceNumber(); + /** + * Technically, at ORC a certificate's boat is identified by a "file ID" as returned by this method, together + * with the {@link #getIssuingCountry() issuing country} which together makes this a unique boat key. For each + * such boat key there is at most one valid certificate at a time. + */ + String getFileId(); + + /** + * The country whose authority issued this certificate. Together with the {@link #getFileId() file ID} this makes for a + * unique key of the boat to which this certifiate belongs. + */ + CountryCode getIssuingCountry(); + + /** + * A unique key of the boat to which this certificate belongs; it consists of the {@link #getFileId() file ID} and the + * {@link #getIssuingCountry() issuing country}. For any such key the ORC database delivers at most one valid certificate + * at any given time. + */ + default Pair getBoatKey() { + return new Pair<>(getFileId(), getIssuingCountry()); + } + /** * Returns the sailnumber of the {@link Competitor} which this certificate belongs to. * diff --git a/java/com.sap.sailing.domain.common/src/com/sap/sailing/domain/common/orc/impl/ORCCertificateImpl.java b/java/com.sap.sailing.domain.common/src/com/sap/sailing/domain/common/orc/impl/ORCCertificateImpl.java index 8f6f857b189..fbd04432a79 100755 --- a/java/com.sap.sailing.domain.common/src/com/sap/sailing/domain/common/orc/impl/ORCCertificateImpl.java +++ b/java/com.sap.sailing.domain.common/src/com/sap/sailing/domain/common/orc/impl/ORCCertificateImpl.java @@ -6,6 +6,7 @@ import java.util.Map; import com.sap.sailing.domain.common.impl.NauticalMileDistance; import com.sap.sailing.domain.common.orc.ORCCertificate; import com.sap.sse.common.Bearing; +import com.sap.sse.common.CountryCode; import com.sap.sse.common.Distance; import com.sap.sse.common.Duration; import com.sap.sse.common.Speed; @@ -24,7 +25,7 @@ import com.sap.sse.common.Util; public class ORCCertificateImpl implements ORCCertificate { private static final long serialVersionUID = 8725162998514202782L; - private final String idConsistingOfNatAuthCertNoAndBIN; + private final String referenceNumber; public final static Distance NAUTICAL_MILE = new NauticalMileDistance(1); @@ -35,7 +36,9 @@ public class ORCCertificateImpl implements ORCCertificate { private final Duration gph; private final Double cdl; private final TimePoint issueDate; - + private final String fileId; + private final CountryCode issuingCountry; + /** * The "core" of the certificate for performance curve scoring; the values in the map tell how fast the boat * described by this certificate is expected to sail for the true wind speed (TWS) provided by the key, and the true @@ -111,11 +114,13 @@ public class ORCCertificateImpl implements ORCCertificate { * {@link ORCCertificate#ALLOWANCES_TRUE_WIND_ANGLES}. */ public ORCCertificateImpl(Speed[] allowancesTrueWindSpeeds, Bearing[] allowancesTrueWindAngles, - String idConsistingOfNatAuthCertNoAndBIN, String sailnumber, String boatName, String boatClassName, - Distance length, Duration gph, Double cdl, TimePoint issueDate, + String referenceNumber, String fileId, String sailnumber, String boatName, + String boatClassName, Distance length, Duration gph, Double cdl, + TimePoint issueDate, CountryCode issuingCountry, Map> velocityPredictionsPerTrueWindSpeedAndAngle, Map beatAngles, Map beatVMGPredictionPerTrueWindSpeed, Map beatAllowancePerTrueWindSpeed, - Map runAngles, Map runVMGPredictionPerTrueWindSpeed, + Map runAngles, + Map runVMGPredictionPerTrueWindSpeed, Map runAllowancePerTrueWindSpeed, Map windwardLeewardSpeedPredictionsPerTrueWindSpeed, Map longDistanceSpeedPredictionsPerTrueWindSpeed, @@ -123,7 +128,9 @@ public class ORCCertificateImpl implements ORCCertificate { Map nonSpinnakerSpeedPredictionsPerTrueWindSpeed) { this.allowancesTrueWindAngles = allowancesTrueWindAngles; this.allowancesTrueWindSpeeds = allowancesTrueWindSpeeds; - this.idConsistingOfNatAuthCertNoAndBIN = idConsistingOfNatAuthCertNoAndBIN; + this.referenceNumber = referenceNumber; + this.fileId = fileId; + this.issuingCountry = issuingCountry; this.sailNumber = sailnumber; this.boatName = boatName; this.boatClassName = boatClassName; @@ -150,21 +157,23 @@ public class ORCCertificateImpl implements ORCCertificate { * {@link ORCCertificate#ALLOWANCES_TRUE_WIND_ANGLES}, respectively, for the matrix of time allowances * or, conversely, the speed predictions. */ - public ORCCertificateImpl(String idConsistingOfNatAuthCertNoAndBIN, - String sailnumber, String boatName, String boatClassName, - Distance length, Duration gph, - Double cdl, TimePoint issueDate, - Map> velocityPredictionsPerTrueWindSpeedAndAngle, + public ORCCertificateImpl(String referenceNumber, + String fileId, String sailnumber, String boatName, + String boatClassName, Distance length, + Duration gph, Double cdl, + TimePoint issueDate, + CountryCode issuingCountry, Map> velocityPredictionsPerTrueWindSpeedAndAngle, Map beatAngles, Map beatVMGPredictionPerTrueWindSpeed, - Map beatAllowancePerTrueWindSpeed, Map runAngles, + Map beatAllowancePerTrueWindSpeed, + Map runAngles, Map runVMGPredictionPerTrueWindSpeed, Map runAllowancePerTrueWindSpeed, Map windwardLeewardSpeedPredictionsPerTrueWindSpeed, Map longDistanceSpeedPredictionsPerTrueWindSpeed, Map circularRandomSpeedPredictionsPerTrueWindSpeed, Map nonSpinnakerSpeedPredictionsPerTrueWindSpeed) { - this(ALLOWANCES_TRUE_WIND_SPEEDS, ALLOWANCES_TRUE_WIND_ANGLES, idConsistingOfNatAuthCertNoAndBIN, sailnumber, - boatName, boatClassName, length, gph, cdl, issueDate, velocityPredictionsPerTrueWindSpeedAndAngle, + this(ALLOWANCES_TRUE_WIND_SPEEDS, ALLOWANCES_TRUE_WIND_ANGLES, referenceNumber, fileId, sailnumber, boatName, + boatClassName, length, gph, cdl, issueDate, issuingCountry, velocityPredictionsPerTrueWindSpeedAndAngle, beatAngles, beatVMGPredictionPerTrueWindSpeed, beatAllowancePerTrueWindSpeed, runAngles, runVMGPredictionPerTrueWindSpeed, runAllowancePerTrueWindSpeed, windwardLeewardSpeedPredictionsPerTrueWindSpeed, longDistanceSpeedPredictionsPerTrueWindSpeed, @@ -173,7 +182,22 @@ public class ORCCertificateImpl implements ORCCertificate { @Override public String getId() { - return idConsistingOfNatAuthCertNoAndBIN; + return getReferenceNumber(); + } + + @Override + public String getReferenceNumber() { + return referenceNumber; + } + + @Override + public String getFileId() { + return fileId; + } + + @Override + public CountryCode getIssuingCountry() { + return issuingCountry; } @Override diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/AbstractORCCertificateImporterTest.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/AbstractORCCertificateImporterTest.java index 8b794c4b3d0..7038ac4cade 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/AbstractORCCertificateImporterTest.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/AbstractORCCertificateImporterTest.java @@ -35,8 +35,8 @@ public abstract class AbstractORCCertificateImporterTest { protected void testSimpleOnlineFileRead(String rmsOrJsonFormat) throws IOException, ParseException, InterruptedException { Collection certificates = customIgnoreRule.getAvailableCerts(); final ORCCertificate cert = certificates.stream().findFirst().get(); - String countryCode = cert.getId().substring(0, 3); - String url = String.format("https://data.orc.org/public/WPub.dll?action=DownBoatRMS&CountryId=%s&ext=%s", countryCode, rmsOrJsonFormat); + String countryCode = cert.getIssuingCountry().getThreeLetterIOCCode(); + String url = String.format("https://data.orc.org/public/WPub.dll?action=DownRMS&CountryId=%s&ext=%s", countryCode, rmsOrJsonFormat); ORCCertificatesCollection importer = ORCCertificatesImporter.INSTANCE.read(new URL(url).openStream()); ORCCertificate someValidCert = null, referenceCert = null; for (ORCCertificate orcCertificate : certificates) { diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/FailIfNoValidOrcCertificateRule.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/FailIfNoValidOrcCertificateRule.java index e447138f6f8..3c0ab7f42fa 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/FailIfNoValidOrcCertificateRule.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/FailIfNoValidOrcCertificateRule.java @@ -71,7 +71,7 @@ public class FailIfNoValidOrcCertificateRule implements TestRule { certificateExists = false; for (CountryCode cc : CountryCodeFactory.INSTANCE.getAll()) { try { - Iterable certificateHandles = db.search(cc, year, null, null, null, null); + Iterable certificateHandles = db.search(cc, year, null, null, null, null, /* includeInvalid */ false); Iterable orcCertificates = db.getCertificates(certificateHandles); orcCertificates.forEach(availableCerts::add); if (orcCertificates.iterator().hasNext()) { diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/ORCPerformanceCurveRankingTest.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/ORCPerformanceCurveRankingTest.java index c3b91e2c6a0..454511b92fe 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/ORCPerformanceCurveRankingTest.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/ORCPerformanceCurveRankingTest.java @@ -128,7 +128,7 @@ public class ORCPerformanceCurveRankingTest extends OnlineTracTracBasedTest { final Boat boat = getTrackedRace().getRace().getBoatOfCompetitor(competitor); ORCCertificate certificate; if (boat.getSailID().equals("Air is blue")) { - certificate = certificates.getCertificateById("ITA52521I5252"); + certificate = certificates.getCertificateById("ITA00052727"); } else { certificate = certificates.getCertificateById(boat.getSailID()); if (certificate == null) { diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java index bab74c77be2..224447d1736 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterJSON.java @@ -10,7 +10,7 @@ public class TestORCCertificateImporterJSON extends AbstractORCCertificateImport @Test public void testSimpleLocalJSONFileRead() throws IOException, ParseException { - testSimpleLocalFileRead("GER2019.json", "GER166844GER7323"); + testSimpleLocalFileRead("GER2019.json", "GER20041179"); } @FailIfNoValidOrcCertificates diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java index 73d499052d8..6eadd5ddb2b 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCCertificateImporterRMS.java @@ -15,7 +15,7 @@ public class TestORCCertificateImporterRMS extends AbstractORCCertificateImporte @Test public void testSimpleLocalRMSFileRead() throws IOException, ParseException { - testSimpleLocalFileRead("GER2019.rms", "GER166844GER7323"); + testSimpleLocalFileRead("GER2019.rms", "GER20041179"); } @FailIfNoValidOrcCertificates diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java index 71860b23c6d..0a40971425b 100644 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPerformanceCurve.java @@ -103,31 +103,31 @@ public class TestORCPerformanceCurve { @Test public void testLagrangeInterpolation60() throws FunctionEvaluationException { - ORCCertificate certificate = importerLocal.getCertificateById("GER140772GER5549"); + ORCCertificate certificate = importerLocal.getCertificateById("GER20040783"); testAllowancePerLeg(certificate, 60.0, 498.4); } @Test public void testLagrangeInterpolation62_5() throws FunctionEvaluationException { - ORCCertificate certificate = importerLocal.getCertificateById("GER140772GER5549"); + ORCCertificate certificate = importerLocal.getCertificateById("GER20040783"); testAllowancePerLeg(certificate, 62.5, 492.2, 425.0, 403.6, 393.1, 386.7, 382.6, 371.4); } @Test public void testLagrangeInterpolation98_3() throws FunctionEvaluationException { - ORCCertificate certificate = importerLocal.getCertificateById("GER140772GER5549"); + ORCCertificate certificate = importerLocal.getCertificateById("GER20040783"); testAllowancePerLeg(certificate, 98.3, 483.6, 418.7, 394.8, 377.9, 360.2, 345.0, 321.7); } @Test public void testLagrangeInterpolation120() throws FunctionEvaluationException { - ORCCertificate certificate = importerLocal.getCertificateById("GER140772GER5549"); + ORCCertificate certificate = importerLocal.getCertificateById("GER20040783"); testAllowancePerLeg(certificate, 120.0, 506); } @Test public void testLagrangeInterpolation138_7() throws FunctionEvaluationException { - ORCCertificate certificate = importerLocal.getCertificateById("GER140772GER5549"); + ORCCertificate certificate = importerLocal.getCertificateById("GER20040783"); testAllowancePerLeg(certificate, 138.7, 588.1, 468.4, 413.8, 382.6, 355.1, 326.4, 275.4); } @@ -142,7 +142,7 @@ public class TestORCPerformanceCurve { @Test public void testSimpleConstructedCourse() throws FunctionEvaluationException { - ORCCertificate certificate = (ORCCertificate) importerLocal.getCertificateById("GER140772GER5549"); + ORCCertificate certificate = (ORCCertificate) importerLocal.getCertificateById("GER20040783"); List legs = new ArrayList<>(); legs.add(new ORCPerformanceCurveLegImpl(new NauticalMileDistance(1.0), new DegreeBearingImpl(0))); legs.add(new ORCPerformanceCurveLegImpl(new NauticalMileDistance(1.0), new DegreeBearingImpl(30))); @@ -165,7 +165,7 @@ public class TestORCPerformanceCurve { @Test public void testPerformanceCurveInversion() throws MaxIterationsExceededException, FunctionEvaluationException { Double accuracy = 0.1; - ORCCertificate certificate = (ORCCertificate) importerLocal.getCertificateById("GER140772GER5549"); + ORCCertificate certificate = (ORCCertificate) importerLocal.getCertificateById("GER20040783"); ORCPerformanceCurveImpl performanceCurve = (ORCPerformanceCurveImpl) new ORCPerformanceCurveImpl(certificate, alturaCourse); testBackwardForward(accuracy, performanceCurve, 11.5); testBackwardForward(accuracy, performanceCurve, 17.23); @@ -194,12 +194,12 @@ public class TestORCPerformanceCurve { @Test public void testImpliedWind() throws MaxIterationsExceededException, FunctionEvaluationException { double accuracy = 0.0001; - ORCCertificate certificateMoana = importerLocal.getCertificateById("GER140772GER5549"); - ORCCertificate certificateMilan = importerLocal.getCertificateById("GER166844GER7323"); - ORCCertificate certificateTutima = importerLocal.getCertificateById("GER140618GER5609"); - ORCCertificate certificateBank = importerLocal.getCertificateById("GER140755GER5555"); - ORCCertificate certificateHaspa = importerLocal.getCertificateById("GER141411GER6300"); - ORCCertificate certificateHalbtrocken = importerLocal.getCertificateById("GER141432GER5564"); + ORCCertificate certificateMoana = importerLocal.getCertificateById("GER20040783"); + ORCCertificate certificateMilan = importerLocal.getCertificateById("GER20041179"); + ORCCertificate certificateTutima = importerLocal.getCertificateById("GER30010194"); + ORCCertificate certificateBank = importerLocal.getCertificateById("GER20040647"); + ORCCertificate certificateHaspa = importerLocal.getCertificateById("GER30010167"); + ORCCertificate certificateHalbtrocken = importerLocal.getCertificateById("GER20040632"); ORCPerformanceCurve performanceCurveMoana = new ORCPerformanceCurveImpl(certificateMoana, alturaCourse); ORCPerformanceCurve performanceCurveMilan = new ORCPerformanceCurveImpl(certificateMilan, alturaCourse); ORCPerformanceCurve performanceCurveTutima = new ORCPerformanceCurveImpl(certificateTutima, alturaCourse); @@ -231,12 +231,12 @@ public class TestORCPerformanceCurve { @Test public void testAllowances() throws FunctionEvaluationException { double accuracy = 0.00001; - ORCCertificate certificateMoana = importerLocal.getCertificateById("GER140772GER5549"); - ORCCertificate certificateMilan = importerLocal.getCertificateById("GER166844GER7323"); - ORCCertificate certificateTutima = importerLocal.getCertificateById("GER140618GER5609"); - ORCCertificate certificateBank = importerLocal.getCertificateById("GER140755GER5555"); - ORCCertificate certificateHaspa = importerLocal.getCertificateById("GER141411GER6300"); - ORCCertificate certificateHalbtrocken = importerLocal.getCertificateById("GER141432GER5564"); + ORCCertificate certificateMoana = importerLocal.getCertificateById("GER20040783"); + ORCCertificate certificateMilan = importerLocal.getCertificateById("GER20041179"); + ORCCertificate certificateTutima = importerLocal.getCertificateById("GER30010194"); + ORCCertificate certificateBank = importerLocal.getCertificateById("GER20040647"); + ORCCertificate certificateHaspa = importerLocal.getCertificateById("GER30010167"); + ORCCertificate certificateHalbtrocken = importerLocal.getCertificateById("GER20040632"); ORCPerformanceCurve performanceCurveMoana = new ORCPerformanceCurveImpl(certificateMoana, alturaCourse); ORCPerformanceCurve performanceCurveMilan = new ORCPerformanceCurveImpl(certificateMilan, alturaCourse); ORCPerformanceCurve performanceCurveTutima = new ORCPerformanceCurveImpl(certificateTutima, alturaCourse); @@ -268,7 +268,7 @@ public class TestORCPerformanceCurve { final double highAccuracy = 0.01; final double allowanceAccuracy = 0.1; final Distance ONE_NAUTICAL_MILE = new NauticalMileDistance(1.0); - final ORCCertificate certificateWithSpecificBins = importerWithSpecificBins.getCertificateById("GRA00073GR317"); + final ORCCertificate certificateWithSpecificBins = importerWithSpecificBins.getCertificateById("N/A"); for (final Bearing twa : certificateWithSpecificBins.getTrueWindAngles()) { final ORCPerformanceCurveCourse singleLegOneMileCourseWithTwa = createSingleLegCourseWithTwa(twa); final ORCPerformanceCurve performanceCurveSpecificBins = new ORCPerformanceCurveImpl(certificateWithSpecificBins, singleLegOneMileCourseWithTwa); @@ -299,13 +299,13 @@ public class TestORCPerformanceCurve { ORCPerformanceCurveCourse complexCourse = new ORCPerformanceCurveCourseImpl(list); final double accuracy = 0.0001; - ORCCertificate certificateMoana = importerLocal.getCertificateById("GER140772GER5549"); - ORCCertificate certificateMilan = importerLocal.getCertificateById("GER166844GER7323"); - ORCCertificate certificateTutima = importerLocal.getCertificateById("GER140618GER5609"); - ORCCertificate certificateBank = importerLocal.getCertificateById("GER140755GER5555"); - ORCCertificate certificateHaspa = importerLocal.getCertificateById("GER141411GER6300"); - ORCCertificate certificateHalbtrocken = importerLocal.getCertificateById("GER141432GER5564"); - ORCCertificate certificateWithSpecificBins=importerWithSpecificBins.getCertificateById("GRA00073GR317"); + ORCCertificate certificateMoana = importerLocal.getCertificateById("GER20040783"); + ORCCertificate certificateMilan = importerLocal.getCertificateById("GER20041179"); + ORCCertificate certificateTutima = importerLocal.getCertificateById("GER30010194"); + ORCCertificate certificateBank = importerLocal.getCertificateById("GER20040647"); + ORCCertificate certificateHaspa = importerLocal.getCertificateById("GER30010167"); + ORCCertificate certificateHalbtrocken = importerLocal.getCertificateById("GER20040632"); + ORCCertificate certificateWithSpecificBins=importerWithSpecificBins.getCertificateById("N/A"); ORCPerformanceCurve performanceCurveMoana = new ORCPerformanceCurveImpl(certificateMoana, complexCourse); ORCPerformanceCurve performanceCurveMilan = new ORCPerformanceCurveImpl(certificateMilan, complexCourse); ORCPerformanceCurve performanceCurveTutima = new ORCPerformanceCurveImpl(certificateTutima, complexCourse); @@ -334,13 +334,13 @@ public class TestORCPerformanceCurve { ORCPerformanceCurveCourse complexCourse = new ORCPerformanceCurveCourseImpl(list); double accuracy = 0.00001; - ORCCertificate certificateMoana = importerLocal.getCertificateById("GER140772GER5549"); - ORCCertificate certificateMilan = importerLocal.getCertificateById("GER166844GER7323"); - ORCCertificate certificateTutima = importerLocal.getCertificateById("GER140618GER5609"); - ORCCertificate certificateBank = importerLocal.getCertificateById("GER140755GER5555"); - ORCCertificate certificateHaspa = importerLocal.getCertificateById("GER141411GER6300"); - ORCCertificate certificateHalbtrocken = importerLocal.getCertificateById("GER141432GER5564"); - ORCCertificate certificateWithSpecificBins=importerWithSpecificBins.getCertificateById("GRA00073GR317"); + ORCCertificate certificateMoana = importerLocal.getCertificateById("GER20040783"); + ORCCertificate certificateMilan = importerLocal.getCertificateById("GER20041179"); + ORCCertificate certificateTutima = importerLocal.getCertificateById("GER30010194"); + ORCCertificate certificateBank = importerLocal.getCertificateById("GER20040647"); + ORCCertificate certificateHaspa = importerLocal.getCertificateById("GER30010167"); + ORCCertificate certificateHalbtrocken = importerLocal.getCertificateById("GER20040632"); + ORCCertificate certificateWithSpecificBins=importerWithSpecificBins.getCertificateById("N/A"); ORCPerformanceCurve performanceCurveMoana = new ORCPerformanceCurveImpl(certificateMoana, complexCourse); ORCPerformanceCurve performanceCurveMilan = new ORCPerformanceCurveImpl(certificateMilan, complexCourse); ORCPerformanceCurve performanceCurveTutima = new ORCPerformanceCurveImpl(certificateTutima, complexCourse); diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java index 263175161ac..2026f7f3bc6 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java @@ -2,6 +2,7 @@ package com.sap.sailing.domain.orc; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.util.Arrays; @@ -92,7 +93,7 @@ public class TestORCPublicCertificateDatabase { final String referenceNumber = "FRA00013881"; final Iterable result = db.search(/* country */ null, /* yearOfIssuance */ null, /* referenceNumber */ referenceNumber, /* yachtName */ null, /* sailNumber */ null, - /* boatClassName */ null); + /* boatClassName */ null, /* includeInvalid */ true); assertEquals(1, Util.size(result)); assertSoulmate(referenceNumber, result.iterator().next()); } @@ -102,7 +103,7 @@ public class TestORCPublicCertificateDatabase { final String referenceNumber = "FRA00013881"; final Iterable result = db.search(/* country */ null, /* yearOfIssuance */ 2019, /* referenceNumber */ null, /* yachtName */ "Soulmate", /* sailNumber */ "DEN- 13", - /* boatClassName */ null); + /* boatClassName */ null, /* includeInvalid */ true); assertEquals(1, Util.size(result)); assertSoulmate(referenceNumber, result.iterator().next()); } @@ -112,7 +113,7 @@ public class TestORCPublicCertificateDatabase { final String referenceNumber = "FRA00013881"; final Iterable result = db.search(/* country */ null, /* yearOfIssuance */ null, /* referenceNumber */ null, /* yachtName */ "Soulmate", /* sailNumber */ null, - /* boatClassName */ null); + /* boatClassName */ null, /* includeInvalid */ true); assertTrue(Util.size(result)>1); boolean found = false; for (final CertificateHandle handle : result) { @@ -140,13 +141,14 @@ public class TestORCPublicCertificateDatabase { cert.getSailNumber(), /* * boat class name; could be set to cert.getBoatClassName() but there are * deviations in ORC DBs and query API, so leaving null: - */ null); + */ null, /* includeInvalid */ false); Optional certificateHandle = Optional.ofNullable(certHandles.iterator().hasNext() ? certHandles.iterator().next() : null); assertTrue("No certificate found for handle "+certificateHandle+ " extracted from certificates "+certificates, certificateHandle.isPresent()); final String referenceNumber = certificateHandle.get().getReferenceNumber(); final CertificateHandle handle = db.getCertificateHandle(referenceNumber); final ORCCertificate result = db.getCertificate(referenceNumber); + assertNotNull("Unable to load certificate for reference number "+referenceNumber+" from handle "+certificateHandle); assertEquals(handle.getGPH(), result.getGPH().asSeconds(), 0.00001); // Use some tolerance as we found differences as much as 5s between the dxtDate in the handle coming from the XML search result // and the IssueDate field in the JSON. Both suggest to report millisecond accuracy, but dxtDate always seems to have the diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/ORCPublicCertificateDatabase.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/ORCPublicCertificateDatabase.java index 058ebfb7915..ed292e5ceb5 100755 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/ORCPublicCertificateDatabase.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/ORCPublicCertificateDatabase.java @@ -17,8 +17,14 @@ import com.sap.sse.common.Util; /** * An interface to {@code data.orc.org}, mostly the search functionality at * {@code https://data.orc.org/public/WPub.dll?action=SrchCert&xslp=scert.php}. It can search for certificates by a - * number of criteria, among them the issuing country, sail number, ORC reference number, or boat name. - * <p> + * number of criteria, among them the issuing country, sail number, ORC reference number, or boat name. Those search + * results are represented as {@link CertificateHandle} objects. Such handles can also be obtained for certificates that + * are not currently valid. See the {@code includeInvalid} parameter of the + * {@link #search(CountryCode, Integer, String, String, String, String, boolean)} method. However, handles for + * {@link CertificateHandle#isValid() invalid} certificates cannot be resolved into {@link ORCCertificate} objects + * through {@link #getCertificates(Iterable)} or {@link #getCertificates(CertificateHandle...)} because the database + * returns full certificates only if they are valid. + *

* * As the web site emits XML documents as search results, those can be parsed and turned into handles to certificates. * An example output (excerpt): @@ -80,8 +86,8 @@ import com.sap.sse.common.Util; </DATA> * * - * This also shows that some query results do not provide a valid link to a downloadable copy of the certificate. Those are - * automatically removed from the results here. + * This also shows that some query results do not provide a valid link to a downloadable copy of the certificate. Those + * are automatically removed from the results here. * * @author Axel Uhl (D043530) * @@ -97,6 +103,7 @@ public interface ORCPublicCertificateDatabase { */ public interface CertificateHandle { CountryCode getIssuingCountry(); + String getFileId(); Double getGPH(); String getSSSID(); UUID getDatInGID(); @@ -111,6 +118,7 @@ public interface ORCPublicCertificateDatabase { Integer getCertType(); Boolean isOd(); Boolean isProvisional(); + Boolean isValid(); } /** @@ -118,11 +126,12 @@ public interface ORCPublicCertificateDatabase { * results based on that criterion. You can use "%" as wildcards in the {@code yachtName}, {@code sailNumber} and * {@code boatClassName} parameters. *

+ * @param includeInvalid TODO * * @return an always valid, never {@code null} object which may be {@link Util#isEmpty() empty}. */ Iterable search(CountryCode country, Integer yearOfIssuance, String referenceNumber, - String yachtName, String sailNumber, String boatClassName) throws Exception; + String yachtName, String sailNumber, String boatClassName, boolean includeInvalid) throws Exception; default Iterable getCertificates(Iterable handles) throws Exception { return Util.map(handles, handle->{ @@ -139,7 +148,7 @@ public interface ORCPublicCertificateDatabase { } default CertificateHandle getCertificateHandle(String referenceNumber) throws Exception { - return search(null, null, referenceNumber, null, null, null).iterator().next(); + return search(null, null, referenceNumber, null, null, null, /* includeInvalid */ true).iterator().next(); } ORCCertificate getCertificate(String referenceNumber) throws Exception; diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCCertificatesCollectionJSON.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCCertificatesCollectionJSON.java index df040a0d5db..cb6422916b4 100755 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCCertificatesCollectionJSON.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCCertificatesCollectionJSON.java @@ -21,6 +21,7 @@ import com.sap.sailing.domain.common.impl.MeterDistance; import com.sap.sailing.domain.common.orc.ORCCertificate; import com.sap.sailing.domain.common.orc.impl.ORCCertificateImpl; import com.sap.sse.common.Bearing; +import com.sap.sse.common.CountryCodeFactory; import com.sap.sse.common.Distance; import com.sap.sse.common.Duration; import com.sap.sse.common.Speed; @@ -52,16 +53,15 @@ public class ORCCertificatesCollectionJSON extends AbstractORCCertificatesCollec private static final String YACHT_NAME = "YachtName"; private static final String LOA = "LOA"; private static final String SAIL_NO = "SailNo"; - private static final String CERT_NO = "CertNo"; private static final String BIN = "BIN"; private static final String NAT_AUTH = "NatAuth"; + private static final String REFERENCE_NUMBER = "RefNo"; private static final String RUN = "Run"; private static final String BEAT = "Beat"; private static final String WINDWARD_LEEWARD = "WL"; private static final String LONG_DISTANCE = "OC"; private static final String CIRCULAR_RANDOM = "CR"; private static final String NON_SPINNAKER = "NS"; - private static final String EMPTY_CERT_NO = " "; private static final String WIND_SPEEDS = "WindSpeeds"; /** @@ -99,6 +99,9 @@ public class ORCCertificatesCollectionJSON extends AbstractORCCertificatesCollec */ @Override public ORCCertificate getCertificateById(String certificateId) { + String refNo = null; + String natAuth = null; + String fileId = null; String boatName = null; String boatclass = null; Distance length = null; @@ -121,6 +124,15 @@ public class ORCCertificatesCollectionJSON extends AbstractORCCertificatesCollec String sailNumber = null; for (Entry entry : object.entrySet()) { switch ((String) entry.getKey()) { + case REFERENCE_NUMBER: + refNo = entry.getValue() == null ? null : entry.getValue().toString(); + break; + case NAT_AUTH: + natAuth = entry.getValue() == null ? null : entry.getValue().toString(); + break; + case BIN: + fileId = entry.getValue() == null ? null : entry.getValue().toString(); + break; case SAIL_NO: sailNumber = entry.getValue() == null ? null : entry.getValue().toString(); break; @@ -256,7 +268,8 @@ public class ORCCertificatesCollectionJSON extends AbstractORCCertificatesCollec Arrays.equals(ORCCertificate.ALLOWANCES_TRUE_WIND_ANGLES, dynamicAllowancesTrueWindAngles) ? ORCCertificate.ALLOWANCES_TRUE_WIND_ANGLES : dynamicAllowancesTrueWindAngles, - getId(object), sailNumber, boatName, boatclass, length, gph, cdl, issueDate, + refNo, fileId, sailNumber, boatName, boatclass, length, gph, cdl, + issueDate, CountryCodeFactory.INSTANCE.getFromThreeLetterIOCName(natAuth), velocityPredictionPerTrueWindSpeedAndAngle, beatAngles, beatVMGPredictionPerTrueWindSpeed, beatAllowancePerTrueWindSpeed, gybeAngles, runVMGPredictionPerTrueWindSpeed, runAllowancePerTrueWindSpeed, windwardLeewardSpeedPredictionPerTrueWindSpeed, @@ -267,13 +280,7 @@ public class ORCCertificatesCollectionJSON extends AbstractORCCertificatesCollec } private String getId(JSONObject certificateAsJson) { - return getIdFromFields(certificateAsJson.get(NAT_AUTH).toString(), - certificateAsJson.get(CERT_NO) == null ? EMPTY_CERT_NO : certificateAsJson.get(CERT_NO).toString(), - certificateAsJson.get(BIN).toString()); - } - - private String getIdFromFields(final String natAuth, final String certNo, final String bin) { - return natAuth + certNo + bin; + return certificateAsJson.get(REFERENCE_NUMBER).toString(); } @Override diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCCertificatesCollectionRMS.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCCertificatesCollectionRMS.java index 7864e8e75e0..c44dbadd8f2 100755 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCCertificatesCollectionRMS.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCCertificatesCollectionRMS.java @@ -14,10 +14,13 @@ import com.sap.sailing.domain.common.impl.MeterDistance; import com.sap.sailing.domain.common.orc.ORCCertificate; import com.sap.sailing.domain.common.orc.impl.ORCCertificateImpl; import com.sap.sse.common.Bearing; +import com.sap.sse.common.CountryCode; +import com.sap.sse.common.CountryCodeFactory; import com.sap.sse.common.Distance; import com.sap.sse.common.Duration; import com.sap.sse.common.Speed; import com.sap.sse.common.TimePoint; +import com.sap.sse.common.Util.Triple; import com.sap.sse.common.impl.DegreeBearingImpl; import com.sap.sse.common.impl.MillisecondsTimePoint; import com.sap.sse.common.impl.SecondsDurationImpl; @@ -52,6 +55,7 @@ public class ORCCertificatesCollectionRMS extends AbstractORCCertificatesCollect private static final String SAILNUMBER = "SAILNUMB"; private static final String BOATCLASS = "TYPE"; static final String ISSUEDATE = "DD_MM_yyYY"; + static final String LOA = "LOA"; private static final String ISSUETIME = "HH:MM:SS"; private static final String RUN_ALLOWANCE = "D"; private static final String RUN_ANGLE = "DA"; @@ -61,7 +65,12 @@ public class ORCCertificatesCollectionRMS extends AbstractORCCertificatesCollect private static final String LONG_DISTANCE = "OC"; private static final String CIRCULAR_RANDOM = "CR"; private static final String NON_SPINNAKER = "NSP"; - private static final String NATCERTN_FILE_ID = "NATCERTN.FILE_ID"; + private static final String ISSUING_NATIONAL_AUTHORITY = "NAT"; + private static final String CERTIFICATE_NUMBER = "CERTN"; + private static final String NATCERTN = ISSUING_NATIONAL_AUTHORITY+CERTIFICATE_NUMBER; + private static final String FILE_ID = "FILE_ID"; + private static final String NATCERTN_FILE_ID = NATCERTN+"."+FILE_ID; + static final String REFERENCE_NUMBER = "ReferenceNo"; private static final DateFormat timestampFormat = new SimpleDateFormat("dd MM yyyy HH:mm:ssZ"); /** @@ -108,6 +117,7 @@ public class ORCCertificatesCollectionRMS extends AbstractORCCertificatesCollect if (certificateValues == null) { result = null; } else { + final String refNo = certificateValues.getValue(REFERENCE_NUMBER); final String sailNumber = certificateValues.getValue(SAILNUMBER); final String boatclass = certificateValues.getValue(BOATCLASS); final String boatName = certificateValues.getValue(BOATNAME); @@ -165,15 +175,25 @@ public class ORCCertificatesCollectionRMS extends AbstractORCCertificatesCollect } velocityPredictionsPerTrueWindSpeedAndAngle.put(tws, velocityPredictionPerTrueWindAngle); } - result = new ORCCertificateImpl(certificateValues.getValue(NATCERTN_FILE_ID), sailNumber, boatName, boatclass, - length, gph, cdl, issueDate, velocityPredictionsPerTrueWindSpeedAndAngle, beatAngles, beatVMGPredictionPerTrueWindSpeed, - beatAllowancePerTrueWindSpeed, runAngles, runVMGPredictionPerTrueWindSpeed, + final Triple natCertNoFileId = getIssuingNationalityCertificateNumberAndFileId(certificateValues.getValue(NATCERTN_FILE_ID)); + result = new ORCCertificateImpl(refNo, natCertNoFileId.getC(), sailNumber, boatName, boatclass, length, gph, + cdl, issueDate, natCertNoFileId.getA(), velocityPredictionsPerTrueWindSpeedAndAngle, beatAngles, + beatVMGPredictionPerTrueWindSpeed, beatAllowancePerTrueWindSpeed, runAngles, runVMGPredictionPerTrueWindSpeed, runAllowancePerTrueWindSpeed, windwardLeewardSpeedPredictionPerTrueWindSpeed, - longDistanceSpeedPredictionPerTrueWindSpeed, circularRandomSpeedPredictionPerTrueWindSpeed, nonSpinnakerSpeedPredictionPerTrueWindSpeed); + longDistanceSpeedPredictionPerTrueWindSpeed, circularRandomSpeedPredictionPerTrueWindSpeed, + nonSpinnakerSpeedPredictionPerTrueWindSpeed); } return result; } + private Triple getIssuingNationalityCertificateNumberAndFileId(String natCertFileId) { + final CountryCode nationalityIOCCode = CountryCodeFactory.INSTANCE + .getFromThreeLetterIOCName(natCertFileId.substring(0, ISSUING_NATIONAL_AUTHORITY.length())); + final String certNo = natCertFileId.substring(ISSUING_NATIONAL_AUTHORITY.length(), ISSUING_NATIONAL_AUTHORITY.length()+CERTIFICATE_NUMBER.length()).trim(); + final String fileId = natCertFileId.substring(NATCERTN.length()+".".length()).trim(); + return new Triple<>(nationalityIOCCode, certNo, fileId); + } + @Override public Iterable getCertificateIds() { return Collections.unmodifiableCollection(certificateValuesByCertificateId.keySet()); diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCCertificatesRmsImporter.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCCertificatesRmsImporter.java index 577b5cab0c5..3266d3659d9 100755 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCCertificatesRmsImporter.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCCertificatesRmsImporter.java @@ -5,7 +5,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.Reader; import java.util.HashMap; -import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; @@ -13,16 +12,27 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import com.sap.sailing.domain.common.orc.ORCCertificate; +import com.sap.sse.common.Util.Pair; /** - * Represents a file in format {@code .rms} which is a simple ASCII file format, column-based, with fixed-width columns, - * defined by a header line that defines the names and for the first columns up to the column labeled - * {@link #NAME_OF_LAST_LEFT_ALIGNED_COLUMN_HEADER} also the width of the columns. The header uses column names that do - * not contain spaces, separated by one or more spaces. For the columns up to and including the column - * {@link #NAME_OF_LAST_LEFT_ALIGNED_COLUMN_HEADER} the column names are formatted left-aligned. For subsequent columns - * which contain all numeric values without spaces the column names may also be centered. For those columns parsing can - * simply split by space characters. - *

+ * Represents a file in format {@code .rms} which is a strange ASCII file format, column-based, with fixed-width columns, + * "defined" by a header line that gives the names and for the first columns up to the column labeled + * {@link #NAME_OF_COLUMN_HEADER_WHERE_THINGS_GET_WEIRD} also the width of the columns. Starting with the column following + * {@link #NAME_OF_COLUMN_HEADER_WHERE_THINGS_GET_WEIRD}, field names may be "centered" over their character columns; for example, + * the "LOA" field is represented as " LOA " with the last space marking the separating space to the next column, + * and the values are placed somewhere within those first six "payload" columns; they may appear centered, left-aligned + * or right-aligned, and there doesn't seem to be a recognizable pattern to it.

+ * + * To make matters worse, some columns do not contain values at all. In this case, no alternation between space and non-space + * characters can be used to determine field boundaries. At least, after the {@link #NAME_OF_COLUMN_HEADER_WHERE_THINGS_GET_WEIRD} no + * columns can contain space characters unless they are entirely empty. And in all cases do those column values after + * {@link #NAME_OF_COLUMN_HEADER_WHERE_THINGS_GET_WEIRD} have a non-space character in at least one of the columns used by the corresponding + * field name in the header, unless the field value is left blank.

+ * + * Therefore, a good strategy seems to look for non-space characters in the columns used for the field name in the header. + * If none are found, the field is assumed to be left empty. Otherwise, from the first non-blank character found in the columns + * used by the field name in the header, all characters to the left and to the right that are no space are added to the + * field value.

* * The result of successfully parsing a {@code .rms} file is a map keyed by the sailnumber, with values being * equal-sized maps from the column names to the {@link String} values. Additionally, the column names corresponding to @@ -35,19 +45,18 @@ import com.sap.sailing.domain.common.orc.ORCCertificate; */ public class ORCCertificatesRmsImporter extends AbstractORCCertificatesImporter { - private static final String NAME_OF_LAST_LEFT_ALIGNED_COLUMN_HEADER = ORCCertificatesCollectionRMS.ISSUEDATE; + private static final String NAME_OF_COLUMN_HEADER_WHERE_THINGS_GET_WEIRD = ORCCertificatesCollectionRMS.ISSUEDATE; public ORCCertificatesCollectionRMS read(Reader reader) throws IOException { final BufferedReader br = new BufferedReader(reader); - final LinkedHashMap columnNamesAndWidths = readColumnWidthsFromFirstLine(br.readLine()); - final Iterator i = columnNamesAndWidths.keySet().iterator(); - final String certificateIdColumnName = i.next(); + final String header = br.readLine(); + final LinkedHashMap> columnNamesAndFieldNameStartInHeader = readColumnHeaderFieldNameStartColumnsAndLengthFromFirstLine(header); final Map> certificateValuesByCertificateId = new HashMap<>(); String line; while ((line = br.readLine()) != null) { if (!line.trim().isEmpty()) { - final Map parsedLine = parseLine(line, columnNamesAndWidths); - certificateValuesByCertificateId.put(parsedLine.get(certificateIdColumnName), parsedLine); + final Map parsedLine = parseLine(line, columnNamesAndFieldNameStartInHeader); + certificateValuesByCertificateId.put(parsedLine.get(ORCCertificatesCollectionRMS.REFERENCE_NUMBER), parsedLine); } } return new ORCCertificatesCollectionRMS(certificateValuesByCertificateId); @@ -57,41 +66,70 @@ public class ORCCertificatesRmsImporter extends AbstractORCCertificatesImporter return read(getReaderForInputStream(in)); } - private Map parseLine(final String line, LinkedHashMap columnNamesAndWidths) { - assert columnNamesAndWidths != null; + private Map parseLine(final String line, LinkedHashMap> columnNamesAndFieldNameStartAndLengthInHeader) { + assert columnNamesAndFieldNameStartAndLengthInHeader != null; final Map result = new LinkedHashMap<>(); - int start=0; - boolean splitBySpaceMode = false; // start using column names; switch to splitting by spaces when NAME_OF_LAST_LEFT_ALIGNED_COLUMN_HEADER has been found - for (final Entry columnNameAndWidth : columnNamesAndWidths.entrySet()) { + boolean leftAligned = true; + for (final Entry> columnNameAndFieldNameStartInHeader : columnNamesAndFieldNameStartAndLengthInHeader.entrySet()) { + final int start = columnNameAndFieldNameStartInHeader.getValue().getA(); final int end; - if (splitBySpaceMode) { - boolean foundSpace = false; - int i; - for (i=start; i readColumnWidthsFromFirstLine(final String readLine) { - final LinkedHashMap result = new LinkedHashMap<>(); + /** + * If only space characters are found in {@code line} starting at index {@code start} and ending before index {@code end}, + * the result is {@code null}. Otherwise, starting at any non-space character in the range, expansion to the left and right is + * performed, collecting all contiguous non-space characters. + */ + private String expandFromNonSpaceCharacterInRange(String line, int start, int end) { + final String result; + if (start >= line.length()) { + result = null; + } else { + int left = start; + // search for first non-whitespace from start looking right: + while (left= end) { + result = null; + } else { + int right = left+1; + // expand left: + while (left > 0 && Character.isWhitespace(line.charAt(left-1))) { + left--; + } + // expand right: + while (right < line.length() && !Character.isWhitespace(line.charAt(right))) { + right++; + } + result = line.substring(left, right); + } + } + return result; + } + + private LinkedHashMap> readColumnHeaderFieldNameStartColumnsAndLengthFromFirstLine(final String readLine) { + final LinkedHashMap> result = new LinkedHashMap<>(); final Pattern p = Pattern.compile("([^ ]+ *)"); final Matcher m = p.matcher(readLine); int start = 0; while (m.find(start)) { - result.put(m.group(1).trim(), m.group(1).length()); + final String fieldNameTrimmed = m.group(1).trim(); + result.put(fieldNameTrimmed, new Pair<>(m.start(1), m.group(1).length())); start += m.group(1).length(); } return result; diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java index 33c2c8a06ea..bde7bc2d5fd 100755 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java @@ -77,6 +77,7 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat private static final String ROOT_ELEMENT = "ROOT"; private static final String DATA_ELEMENT = "DATA"; private static final String ROW_ELEMENT = "ROW"; + private static final String DXT_SUFFIX = ".dxt"; private static final DateTimeFormatter[] DATE_FORMATTERS = new DateTimeFormatter[] { @@ -100,6 +101,7 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat */ private static class CertificateHandleImpl implements CertificateHandle { private final CountryCode issuingCountry; + private final String fileId; private final String sssid; private final Double gph; private final UUID datInGID; @@ -114,12 +116,15 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat private final Integer certType; private final Boolean isOneDesign; private final Boolean isProvisional; + private final Boolean isValid; - public CertificateHandleImpl(CountryCode issuingCountry, Double gph, String sssid, UUID datInGID, - String referenceNumber, String yachtName, String sailNumber, String boatClassName, String designer, - String builder, Integer yearBuilt, TimePoint issueDate, Integer certType, Boolean oneDesign, Boolean isProvisional) { + public CertificateHandleImpl(CountryCode issuingCountry, String fileId, Double gph, String sssid, + UUID datInGID, String referenceNumber, String yachtName, String sailNumber, String boatClassName, + String designer, String builder, Integer yearBuilt, TimePoint issueDate, Integer certType, + Boolean oneDesign, Boolean isProvisional, Boolean isValid) { super(); this.issuingCountry = issuingCountry; + this.fileId = fileId; this.gph = gph; this.sssid = sssid; this.datInGID = datInGID; @@ -134,6 +139,7 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat this.certType = certType; this.isOneDesign = oneDesign; this.isProvisional = isProvisional; + this.isValid = isValid; } @Override @@ -166,6 +172,11 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat return issuingCountry; } + @Override + public String getFileId() { + return fileId; + } + @Override public Double getGPH() { return gph; @@ -235,6 +246,11 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat public Boolean isOd() { return isOneDesign; } + + @Override + public Boolean isValid() { + return isValid; + } @Override public String toString() { @@ -242,13 +258,14 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat + ", datInGID=" + datInGID + ", referenceNumber=" + referenceNumber + ", yachtName=" + yachtName + ", sailNumber=" + sailNumber + ", boatClassName=" + boatClassName + ", designer=" + designer + ", builder=" + builder + ", yearBuilt=" + yearBuilt + ", issueDate=" + issueDate + ", certType=" - + certType + ", isOneDesign=" + isOneDesign + ", isProvisional=" + isProvisional + "]"; + + certType + ", isOneDesign=" + isOneDesign + ", isProvisional=" + isProvisional + + ", isValid=" + isValid + "]"; } } @Override public Iterable search(CountryCode issuingCountry, Integer yearOfIssuance, String referenceNumber, - String yachtName, String sailNumber, String boatClassName) throws Exception { + String yachtName, String sailNumber, String boatClassName, boolean includeInvalid) throws Exception { final Set result = new HashSet<>(); final HttpClient client = new SystemDefaultHttpClient(); final List params = new ArrayList<>(); @@ -285,8 +302,11 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat for (int rowNodeIndex=0; rowNodeIndexTrue CERTIFICATE_IS_ACTIVE - result.add(parseHandle(rowNode)); + final CertificateHandle certificateHandle = parseHandle(rowNode); + // certificates can only be obtained if they are currently valid, so ignore expired ones + if (includeInvalid || certificateHandle.isValid()) { + result.add(certificateHandle); + } } } } @@ -302,6 +322,7 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat private CertificateHandle parseHandle(Node rowNode) throws DOMException, ParseException { assert rowNode.getNodeName().equals(ROW_ELEMENT); CountryCode issuingCountry = null; + String fileId = null; String sssid = null; Double gph = null; UUID datInGID = null; @@ -316,6 +337,7 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat TimePoint issueDate = null; Boolean isOneDesign = null; Boolean isProvisional = null; + Boolean isValid = null; for (int i=0; i fuzzySearchVaryingBoatClassName(final String yachtName, final String sailNumber, final BoatClass boatClass) throws Exception { String successfulBoatClassName = boatClass==null?null:boatClass.getName(); - Iterable certificateHandles = search(/* issuingCountry */ null, /* yearOfIssuance */ null, /* referenceNumber */ null, yachtName, sailNumber, successfulBoatClassName); + Iterable certificateHandles = search(/* issuingCountry */ null, /* yearOfIssuance */ null, /* referenceNumber */ null, yachtName, sailNumber, successfulBoatClassName, /* includeInvalid */ false); if (!containsHandleForCurrentYear(certificateHandles) && successfulBoatClassName != null) { if (yachtName != null || sailNumber != null) { logger.fine(()->"Nothing found; removing boat class name restriction "+boatClass.getName()); // try without boat class restriction successfulBoatClassName = null; - certificateHandles = search(/* issuingCountry */ null, /* yearOfIssuance */ null, /* referenceNumber */ null, yachtName, sailNumber, successfulBoatClassName); + certificateHandles = search(/* issuingCountry */ null, /* yearOfIssuance */ null, /* referenceNumber */ null, yachtName, sailNumber, successfulBoatClassName, /* includeInvalid */ false); } else { logger.fine(()->"No current certificates found for boat class "+boatClass.getName()+ " but yacht name and sail number are not specified either; giving up."); diff --git a/java/com.sap.sailing.gwt.ui/src/main/java/com/google/gwt/user/client/rpc/core/com/sap/sailing/domain/common/orc/impl/ORCCertificateImpl_CustomFieldSerializer.java b/java/com.sap.sailing.gwt.ui/src/main/java/com/google/gwt/user/client/rpc/core/com/sap/sailing/domain/common/orc/impl/ORCCertificateImpl_CustomFieldSerializer.java index 4de5a6bd7f7..9b9ea3185c0 100755 --- a/java/com.sap.sailing.gwt.ui/src/main/java/com/google/gwt/user/client/rpc/core/com/sap/sailing/domain/common/orc/impl/ORCCertificateImpl_CustomFieldSerializer.java +++ b/java/com.sap.sailing.gwt.ui/src/main/java/com/google/gwt/user/client/rpc/core/com/sap/sailing/domain/common/orc/impl/ORCCertificateImpl_CustomFieldSerializer.java @@ -12,6 +12,7 @@ import com.google.gwt.user.client.rpc.SerializationStreamWriter; import com.sap.sailing.domain.common.orc.ORCCertificate; import com.sap.sailing.domain.common.orc.impl.ORCCertificateImpl; import com.sap.sse.common.Bearing; +import com.sap.sse.common.CountryCode; import com.sap.sse.common.Distance; import com.sap.sse.common.Duration; import com.sap.sse.common.Speed; @@ -27,7 +28,9 @@ public class ORCCertificateImpl_CustomFieldSerializer extends CustomFieldSeriali public static void serialize(SerializationStreamWriter streamWriter, ORCCertificateImpl instance) throws SerializationException { - streamWriter.writeString(instance.getId()); + streamWriter.writeString(instance.getReferenceNumber()); + streamWriter.writeString(instance.getFileId()); + streamWriter.writeObject(instance.getIssuingCountry()); streamWriter.writeString(instance.getSailNumber()); streamWriter.writeString(instance.getBoatName()); streamWriter.writeString(instance.getBoatClassName()); @@ -65,7 +68,9 @@ public class ORCCertificateImpl_CustomFieldSerializer extends CustomFieldSeriali public static ORCCertificateImpl instantiate(SerializationStreamReader streamReader) throws SerializationException { - final String idConsistingOfNatAuthCertNoAndBIN = streamReader.readString(); + final String referenceNumber = streamReader.readString(); + final String fileId = streamReader.readString(); + final CountryCode issuingCountry = (CountryCode) streamReader.readObject(); final String sailnumber = streamReader.readString(); final String boatName = streamReader.readString(); final String boatclass = streamReader.readString(); @@ -101,12 +106,12 @@ public class ORCCertificateImpl_CustomFieldSerializer extends CustomFieldSeriali @SuppressWarnings("unchecked") final List readTWSs = (List) streamReader.readObject(); final Speed[] trueWindSpeeds = readTWSs == null ? ORCCertificate.ALLOWANCES_TRUE_WIND_SPEEDS : readTWSs.toArray(new Speed[readTWSs.size()]); - return new ORCCertificateImpl(trueWindSpeeds,trueWindAngles,idConsistingOfNatAuthCertNoAndBIN, sailnumber, boatName, boatclass, length, gph, - cdl, issueDate, velocityPredictionsPerTrueWindSpeedAndAngle, beatAngles, - beatVMGPredictionPerTrueWindSpeed, beatAllowancePerTrueWindSpeed, runAngles, + return new ORCCertificateImpl(trueWindSpeeds,trueWindAngles,referenceNumber, fileId, sailnumber, boatName, boatclass, length, + gph, cdl, issueDate, issuingCountry, + velocityPredictionsPerTrueWindSpeedAndAngle, beatAngles, beatVMGPredictionPerTrueWindSpeed, + beatAllowancePerTrueWindSpeed, runAngles, runVMGPredictionPerTrueWindSpeed, runAllowancePerTrueWindSpeed, - windwardLeewardSpeedPredictionsPerTrueWindSpeed, longDistanceSpeedPredictionsPerTrueWindSpeed, - circularRandomSpeedPredictionsPerTrueWindSpeed, nonSpinnakerSpeedPredictionsPerTrueWindSpeed); + windwardLeewardSpeedPredictionsPerTrueWindSpeed, longDistanceSpeedPredictionsPerTrueWindSpeed, circularRandomSpeedPredictionsPerTrueWindSpeed, nonSpinnakerSpeedPredictionsPerTrueWindSpeed); } @Override diff --git a/java/com.sap.sailing.gwt.ui/src/main/java/com/sap/sailing/gwt/ui/server/SailingServiceImpl.java b/java/com.sap.sailing.gwt.ui/src/main/java/com/sap/sailing/gwt/ui/server/SailingServiceImpl.java index b42fb1d1836..223ab575167 100755 --- a/java/com.sap.sailing.gwt.ui/src/main/java/com/sap/sailing/gwt/ui/server/SailingServiceImpl.java +++ b/java/com.sap.sailing.gwt.ui/src/main/java/com/sap/sailing/gwt/ui/server/SailingServiceImpl.java @@ -9981,7 +9981,7 @@ public class SailingServiceImpl extends ResultCachingProxiedRemoteServiceServlet String referenceNumber, String yachtName, String sailNumber, String boatClassName) throws Exception { final ORCPublicCertificateDatabase db = ORCPublicCertificateDatabase.INSTANCE; final Set result = new HashSet<>(); - final Iterable searchResult = db.search(country, yearOfIssuance, referenceNumber, yachtName, sailNumber, boatClassName); + final Iterable searchResult = db.search(country, yearOfIssuance, referenceNumber, yachtName, sailNumber, boatClassName, /* includeInvalid */ false); Util.addAll( db.getCertificates(searchResult), result); diff --git a/java/com.sap.sailing.server.gateway.serialization.shared.android/src/com/sap/sailing/server/gateway/deserialization/racelog/impl/ORCCertificateJsonDeserializer.java b/java/com.sap.sailing.server.gateway.serialization.shared.android/src/com/sap/sailing/server/gateway/deserialization/racelog/impl/ORCCertificateJsonDeserializer.java index a87ef6a3a58..aaa9dd3c48a 100755 --- a/java/com.sap.sailing.server.gateway.serialization.shared.android/src/com/sap/sailing/server/gateway/deserialization/racelog/impl/ORCCertificateJsonDeserializer.java +++ b/java/com.sap.sailing.server.gateway.serialization.shared.android/src/com/sap/sailing/server/gateway/deserialization/racelog/impl/ORCCertificateJsonDeserializer.java @@ -17,6 +17,8 @@ import com.sap.sailing.server.gateway.deserialization.JsonDeserializationExcepti import com.sap.sailing.server.gateway.deserialization.JsonDeserializer; import com.sap.sailing.server.gateway.serialization.racelog.impl.ORCCertificateJsonSerializer; import com.sap.sse.common.Bearing; +import com.sap.sse.common.CountryCode; +import com.sap.sse.common.CountryCodeFactory; import com.sap.sse.common.Distance; import com.sap.sse.common.Duration; import com.sap.sse.common.Speed; @@ -29,6 +31,9 @@ public class ORCCertificateJsonDeserializer implements JsonDeserializer Date: Wed, 11 Mar 2020 00:57:19 +0100 Subject: [PATCH 31/33] introduced certificate update feature in ORCPublicCertificateDatabase --- .../domain/common/orc/ORCCertificate.java | 10 ---- .../orc/TestORCPublicCertificateDatabase.java | 24 +++++++-- .../orc/ORCPublicCertificateDatabase.java | 15 +++++- .../ORCPublicCertificateDatabaseImpl.java | 52 +++++++++++++++++-- 4 files changed, 82 insertions(+), 19 deletions(-) diff --git a/java/com.sap.sailing.domain.common/src/com/sap/sailing/domain/common/orc/ORCCertificate.java b/java/com.sap.sailing.domain.common/src/com/sap/sailing/domain/common/orc/ORCCertificate.java index 76c215bde8e..6d8da19f561 100755 --- a/java/com.sap.sailing.domain.common/src/com/sap/sailing/domain/common/orc/ORCCertificate.java +++ b/java/com.sap.sailing.domain.common/src/com/sap/sailing/domain/common/orc/ORCCertificate.java @@ -10,7 +10,6 @@ import com.sap.sse.common.Distance; import com.sap.sse.common.Duration; import com.sap.sse.common.Speed; import com.sap.sse.common.TimePoint; -import com.sap.sse.common.Util.Pair; import com.sap.sse.common.WithID; import com.sap.sse.common.impl.DegreeBearingImpl; @@ -88,15 +87,6 @@ public interface ORCCertificate extends WithID, Serializable { */ CountryCode getIssuingCountry(); - /** - * A unique key of the boat to which this certificate belongs; it consists of the {@link #getFileId() file ID} and the - * {@link #getIssuingCountry() issuing country}. For any such key the ORC database delivers at most one valid certificate - * at any given time. - */ - default Pair getBoatKey() { - return new Pair<>(getFileId(), getIssuingCountry()); - } - /** * Returns the sailnumber of the {@link Competitor} which this certificate belongs to. * diff --git a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java index 2026f7f3bc6..4290490fe5f 100755 --- a/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java +++ b/java/com.sap.sailing.domain.test/src/com/sap/sailing/domain/orc/TestORCPublicCertificateDatabase.java @@ -5,9 +5,6 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import java.util.Arrays; -import java.util.Collection; -import java.util.Date; import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; @@ -16,6 +13,9 @@ import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Date; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -24,6 +24,7 @@ import java.util.Set; import java.util.UUID; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; +import java.util.logging.Logger; import org.junit.Assert; import org.junit.Before; @@ -37,6 +38,8 @@ import com.sap.sailing.domain.orc.impl.ORCPublicCertificateDatabaseImpl; import com.sap.sse.common.Util; public class TestORCPublicCertificateDatabase { + private static final Logger logger = Logger.getLogger(TestORCPublicCertificateDatabase.class.getName()); + private ORCPublicCertificateDatabase db; private Map dateComparisonMap = new LinkedHashMap(); private List dateFailureCases = Arrays.asList("2019-02-21T10:44GMT+2","2019-02-21T10:38+0800","2019-02-21T10:38+08:00", @@ -132,6 +135,21 @@ public class TestORCPublicCertificateDatabase { assertSoulmate(referenceNumber, result); } + @Test + public void testCertificateUpdate() throws Exception { + final String referenceNumber = "FRA00013881"; + final CertificateHandle oldHandle = db.getCertificateHandle(referenceNumber); + final ORCCertificate result = db.searchForUpdate(oldHandle); + if (result != null) { + assertEquals(oldHandle.getFileId(), result.getFileId()); + assertEquals(oldHandle.getIssuingCountry(), result.getIssuingCountry()); + assertFalse(oldHandle.getIssueDate().after(result.getIssueDate())); + } else { + logger.warning("Couldn't find an update to certificate with reference number "+referenceNumber+ + " anymore. Consider updating this test case to using a different certificate that still has a valid update."); + } + } + @FailIfNoValidOrcCertificates @Test public void testGetCertificate() throws Exception { diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/ORCPublicCertificateDatabase.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/ORCPublicCertificateDatabase.java index ed292e5ceb5..cc77e5fde0a 100755 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/ORCPublicCertificateDatabase.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/ORCPublicCertificateDatabase.java @@ -126,9 +126,16 @@ public interface ORCPublicCertificateDatabase { * results based on that criterion. You can use "%" as wildcards in the {@code yachtName}, {@code sailNumber} and * {@code boatClassName} parameters. *

- * @param includeInvalid TODO * - * @return an always valid, never {@code null} object which may be {@link Util#isEmpty() empty}. + * @param includeInvalid + * If {@code true}, handles may have {@link CertificateHandle#isValid()}{@code == false}, meaning that + * such a handle cannot be resolved to a {@link ORCCertificate} using any of + * {@link #getCertificate(String)}, {@link #getCertificates(Iterable)} and + * {@link #getCertificates(CertificateHandle...)}. If {@code false}, only valid certificate handles + * will be returned that should all be resolvable into an {@link ORCCertificate} with any of the methods + * listed above. + * + * @return never {@code null}; an object which may be {@link Util#isEmpty() empty}. */ Iterable search(CountryCode country, Integer yearOfIssuance, String referenceNumber, String yachtName, String sailNumber, String boatClassName, boolean includeInvalid) throws Exception; @@ -151,6 +158,10 @@ public interface ORCPublicCertificateDatabase { return search(null, null, referenceNumber, null, null, null, /* includeInvalid */ true).iterator().next(); } + ORCCertificate searchForUpdate(ORCCertificate certificate) throws Exception; + + ORCCertificate searchForUpdate(CertificateHandle certificate) throws Exception; + ORCCertificate getCertificate(String referenceNumber) throws Exception; /** diff --git a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java index bde7bc2d5fd..7a39f5e315f 100755 --- a/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java +++ b/java/com.sap.sailing.domain/src/com/sap/sailing/domain/orc/impl/ORCPublicCertificateDatabaseImpl.java @@ -1,5 +1,6 @@ package com.sap.sailing.domain.orc.impl; +import java.io.IOException; import java.io.InputStream; import java.text.ParseException; import java.time.LocalDateTime; @@ -34,6 +35,7 @@ import javax.xml.parsers.DocumentBuilderFactory; import org.apache.http.HttpMessage; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; +import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; @@ -74,6 +76,7 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat private static final NameValuePair ACTION_PARAM = new BasicNameValuePair(ACTION_PARAM_NAME, ACTION_PARAM_VALUE_LIST_CERT); private static final NameValuePair XSLP_PARAM = new BasicNameValuePair(XSLP_PARAM_NAME, XSLP_PARAM_VALUE_LIST_CERT); private static final String SEARCH_URL = "http://data.orc.org/public/WPub.dll"; + private static final String SINGLE_CERTIFICATE_DOWNLOAD_URL = "http://data.orc.org/public/WPub.dll?action=DownBoatRMS"; private static final String ROOT_ELEMENT = "ROOT"; private static final String DATA_ELEMENT = "DATA"; private static final String ROW_ELEMENT = "ROW"; @@ -435,12 +438,54 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat throw new DateTimeParseException("Date is not parsable by any of the format", dateString, 0); } + @Override + public ORCCertificate searchForUpdate(CertificateHandle certificateHandle) + throws ClientProtocolException, IOException, org.json.simple.parser.ParseException { + return searchForUpdate(certificateHandle.getIssuingCountry(), certificateHandle.getFileId(), certificateHandle.getReferenceNumber()); + } + + @Override + public ORCCertificate searchForUpdate(ORCCertificate certificate) + throws ClientProtocolException, IOException, org.json.simple.parser.ParseException { + return searchForUpdate(certificate.getIssuingCountry(), certificate.getFileId(), certificate.getReferenceNumber()); + } + + private ORCCertificate searchForUpdate(final CountryCode issuingCountry, final String fileId, + final String existingCertificateRefNo) + throws IOException, org.json.simple.parser.ParseException, ClientProtocolException { + logger.fine("Looking for update of certificate with reference number "+existingCertificateRefNo); + final String queryParameters = "ext=json&CountryId=" + issuingCountry.getThreeLetterIOCCode()+ + "&dxtName="+fileId+".dxt"; + final ORCCertificate result = getSingleCertificate(queryParameters); + if (result == null) { + logger.info("Couldn't find any valid certificate for that boat"); + } else { + if (result.getReferenceNumber().equals(existingCertificateRefNo)) { + logger.info("Found no new certificate; the one with reference number "+existingCertificateRefNo+ + " is still valid."); + } else { + logger.info("Found update for "+existingCertificateRefNo+": "+result); + } + } + return result; + } + @Override public ORCCertificate getCertificate(String referenceNumber) throws Exception { - final HttpClient client = new SystemDefaultHttpClient(); - final HttpGet getRequest = new HttpGet("http://data.orc.org/public/WPub.dll?action=DownBoatRMS&ext=json&RefNo="+referenceNumber); - addAuthorizationHeader(getRequest); + final String queryParameters = "ext=json&RefNo="+referenceNumber; logger.fine("Obtaining certificate for reference number "+referenceNumber); + final ORCCertificate result = getSingleCertificate(queryParameters); + if (result == null) { + logger.info("Couldn't find ORC certificate with reference number "+referenceNumber); + } + return result; + } + + private ORCCertificate getSingleCertificate(final String queryParameters) + throws IOException, org.json.simple.parser.ParseException, ClientProtocolException { + final HttpGet getRequest = new HttpGet(SINGLE_CERTIFICATE_DOWNLOAD_URL+"&"+queryParameters); + final HttpClient client = new SystemDefaultHttpClient(); + addAuthorizationHeader(getRequest); final Iterable certificates = new ORCCertificatesJsonImporter().read(client.execute(getRequest).getEntity().getContent()) .getCertificates(); final ORCCertificate result; @@ -448,7 +493,6 @@ public class ORCPublicCertificateDatabaseImpl implements ORCPublicCertificateDat result = certificates.iterator().next(); } else { result = null; - logger.info("Couldn't find ORC certificate with reference number "+referenceNumber); } return result; } From ce8ca7e5ed156bdcc9163e92a4ad121588c305e2 Mon Sep 17 00:00:00 2001 From: Axel Uhl Date: Wed, 11 Mar 2020 09:09:58 +0100 Subject: [PATCH 32/33] fixed ORC certificate-related test, adjusting to ReferenceNumber as key --- ...oadingRegattaORCCertificateAssignmentEvent.java | 2 +- .../racelog/impl/ORCCertificateJsonSerializer.java | 2 +- .../resources/rms/newFormatCertificate.json | 2 +- .../src/com/sap/sse/common/CountryCodeFactory.java | 14 ++++++++++++++ .../sse/common/impl/CountryCodeFactoryImpl.java | 10 +++++----- 5 files changed, 22 insertions(+), 8 deletions(-) diff --git a/java/com.sap.sailing.mongodb.test/src/com/sap/sailing/mongodb/test/TestStoringAndLoadingRegattaORCCertificateAssignmentEvent.java b/java/com.sap.sailing.mongodb.test/src/com/sap/sailing/mongodb/test/TestStoringAndLoadingRegattaORCCertificateAssignmentEvent.java index 29619e5f407..0a662ba4029 100644 --- a/java/com.sap.sailing.mongodb.test/src/com/sap/sailing/mongodb/test/TestStoringAndLoadingRegattaORCCertificateAssignmentEvent.java +++ b/java/com.sap.sailing.mongodb.test/src/com/sap/sailing/mongodb/test/TestStoringAndLoadingRegattaORCCertificateAssignmentEvent.java @@ -83,7 +83,7 @@ public class TestStoringAndLoadingRegattaORCCertificateAssignmentEvent extends A @Test public void test() throws JsonDeserializationException, ParseException { RaceLogORCCertificateAssignmentEvent expectedEvent = new RaceLogORCCertificateAssignmentEventImpl(MillisecondsTimePoint.now(), expectedEventTime, author, - expectedId, expectedPassId, importer.getCertificateById("GER140772GER5549"), competitor); + expectedId, expectedPassId, importer.getCertificateById("GER20040783"), competitor); Document dbObject = mongoFactory.storeRaceLogEntry(logIdentifier, expectedEvent); RaceLogORCCertificateAssignmentEvent actualEvent = loadEvent(dbObject); diff --git a/java/com.sap.sailing.server.gateway.serialization.shared.android/src/com/sap/sailing/server/gateway/serialization/racelog/impl/ORCCertificateJsonSerializer.java b/java/com.sap.sailing.server.gateway.serialization.shared.android/src/com/sap/sailing/server/gateway/serialization/racelog/impl/ORCCertificateJsonSerializer.java index e47b2ba5a68..9b43cfc8451 100755 --- a/java/com.sap.sailing.server.gateway.serialization.shared.android/src/com/sap/sailing/server/gateway/serialization/racelog/impl/ORCCertificateJsonSerializer.java +++ b/java/com.sap.sailing.server.gateway.serialization.shared.android/src/com/sap/sailing/server/gateway/serialization/racelog/impl/ORCCertificateJsonSerializer.java @@ -58,7 +58,7 @@ public class ORCCertificateJsonSerializer implements JsonSerializer getAll(); + /** + * @param threeLetterIOCName if {@code null} then {@code null} will be returned + */ CountryCode getFromThreeLetterIOCName(String threeLetterIOCName); + /** + * @param twoLetterISOName if {@code null} then {@code null} will be returned + */ CountryCode getFromTwoLetterISOName(String twoLetterISOName); /** @@ -16,11 +22,19 @@ public interface CountryCodeFactory { *

      *          getFromThreeLetterISOName(Locale.GERMANY.getISO3Country().toUpperCase())
      * 
+ * + * @param threeLetterISOName if {@code null} then {@code null} will be returned */ CountryCode getFromThreeLetterISOName(String threeLetterISOName); + /** + * @param ianaInternet if {@code null} then {@code null} will be returned + */ CountryCode getFromIANAInternet(String ianaInternet); + /** + * @param unVehicle if {@code null} then {@code null} will be returned + */ CountryCode getFromUNVehicle(String unVehicle); } diff --git a/java/com.sap.sse.common/src/com/sap/sse/common/impl/CountryCodeFactoryImpl.java b/java/com.sap.sse.common/src/com/sap/sse/common/impl/CountryCodeFactoryImpl.java index cfd3b733bf7..376ac6e8e96 100755 --- a/java/com.sap.sse.common/src/com/sap/sse/common/impl/CountryCodeFactoryImpl.java +++ b/java/com.sap.sse.common/src/com/sap/sse/common/impl/CountryCodeFactoryImpl.java @@ -306,27 +306,27 @@ public class CountryCodeFactoryImpl implements CountryCodeFactory { @Override public CountryCode getFromIANAInternet(String ianaInternet) { - return byIanaInternet.get(ianaInternet.toLowerCase()); + return ianaInternet==null?null:byIanaInternet.get(ianaInternet.toLowerCase()); } @Override public CountryCode getFromUNVehicle(String unVehicle) { - return byUNVehicle.get(unVehicle.toUpperCase()); + return unVehicle==null?null:byUNVehicle.get(unVehicle.toUpperCase()); } @Override public CountryCode getFromThreeLetterISOName(String threeLetterISOName) { - return byThreeLetterISOName.get(threeLetterISOName.toUpperCase()); + return threeLetterISOName==null?null:byThreeLetterISOName.get(threeLetterISOName.toUpperCase()); } @Override public CountryCode getFromThreeLetterIOCName(String threeLetterIOCName) { - return byThreeLetterIOCName.get(threeLetterIOCName.toUpperCase()); + return threeLetterIOCName==null?null:byThreeLetterIOCName.get(threeLetterIOCName.toUpperCase()); } @Override public CountryCode getFromTwoLetterISOName(String twoLetterISOName) { - return byTwoLetterISOName.get(twoLetterISOName.toUpperCase()); + return twoLetterISOName==null?null:byTwoLetterISOName.get(twoLetterISOName.toUpperCase()); } @Override From 692191f27022af4e657c82b7eafd442623e2c657 Mon Sep 17 00:00:00 2001 From: Axel Uhl Date: Wed, 11 Mar 2020 09:50:26 +0100 Subject: [PATCH 33/33] added TODO --- .../sailing/gwt/ui/adminconsole/CertificatesTableWrapper.java | 1 + 1 file changed, 1 insertion(+) diff --git a/java/com.sap.sailing.gwt.ui/src/main/java/com/sap/sailing/gwt/ui/adminconsole/CertificatesTableWrapper.java b/java/com.sap.sailing.gwt.ui/src/main/java/com/sap/sailing/gwt/ui/adminconsole/CertificatesTableWrapper.java index b74bc15b7b3..8620514f1db 100644 --- a/java/com.sap.sailing.gwt.ui/src/main/java/com/sap/sailing/gwt/ui/adminconsole/CertificatesTableWrapper.java +++ b/java/com.sap.sailing.gwt.ui/src/main/java/com/sap/sailing/gwt/ui/adminconsole/CertificatesTableWrapper.java @@ -175,6 +175,7 @@ public class CertificatesTableWrapper to link to the certificate PDF Window.confirm("Certificate: "+certificate); // TODO Implement CertificatesTableWrapper.showCertificate(...) }