2 * Copyright (C) 2019 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
9 package eu
.etaxonomy
.cdm
.io
.pesi
.erms
.validation
;
11 import java
.sql
.ResultSet
;
12 import java
.sql
.SQLException
;
14 import org
.apache
.log4j
.Logger
;
16 import eu
.etaxonomy
.cdm
.app
.pesi
.PesiDestinations
;
17 import eu
.etaxonomy
.cdm
.app
.pesi
.PesiSources
;
18 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
19 import eu
.etaxonomy
.cdm
.io
.common
.Source
;
22 * Tests the ERMS -> PESI pipeline by comparing the source DB with destination PESI DB.
27 public class PesiErmsValidator
{
29 private static final Logger logger
= Logger
.getLogger(PesiErmsValidator
.class);
31 private static final Source defaultSource
= PesiSources
.PESI2019_ERMS();
32 private static final Source defaultDestination
= PesiDestinations
.pesi_test_local_CDM_ERMS2PESI();
34 private Source source
= defaultSource
;
35 private Source destination
= defaultDestination
;
37 public void invoke(Source source
, Source destination
){
38 boolean success
= true;
40 this.destination
= destination
;
41 success
&= testReferences();
42 success
&= testTaxa();
43 success
&= testTaxonRelations();
45 System
.out
.println("end validation " + (success?
"":"NOT ") + "successful.");
48 private boolean testTaxonRelations() {
49 boolean success
= true;
53 private boolean testTaxa() {
54 boolean success
= testTaxaCount();
58 private boolean testTaxaCount() {
59 int countSrc
= source
.getUniqueInteger("SELECT count(*) FROM tu ");
60 int countDest
= destination
.getUniqueInteger("SELECT count(*) FROM Taxon ");
61 return equals("Taxon count ", countSrc
, countDest
);
64 private boolean testReferences() {
65 boolean success
= testReferenceCount();
68 success
&= testSingleReferences();
69 } catch (SQLException e
) {
76 private boolean testSingleReferences() throws SQLException
{
77 boolean success
= true;
78 ResultSet srcRS
= source
.getResultSet("SELECT s.* FROM sources s ORDER BY s.id ");
79 ResultSet destRS
= destination
.getResultSet("SELECT s.* FROM Source s "
80 + " WHERE s.OriginalDB = 'erms' ORDER BY s.RefIdInSource"); // +1 for the source reference "erms" but this has no OriginalDB
81 while (srcRS
.next() && destRS
.next()){
82 success
&= testSingleReference(srcRS
, destRS
);
87 private boolean testSingleReference(ResultSet srcRS
, ResultSet destRS
) throws SQLException
{
89 boolean success
= equals("Reference ID ", srcRS
.getInt("id"), destRS
.getInt("RefIdInSource"));
90 success
&= equals("Reference name ", srcRS
.getString("source_name"), destRS
.getString("Name"));
91 success
&= equals("Reference note ", srcRS
.getString("source_note"), destRS
.getString("Notes"));
92 success
&= equals("Reference link ", srcRS
.getString("source_link"), destRS
.getString("Link"));
93 success
&= equals("Reference year ", srcRS
.getString("source_year"), destRS
.getString("RefYear"));
98 private boolean testReferenceCount() {
99 int countSrc
= source
.getUniqueInteger("SELECT count(*) FROM sources ");
100 int countDest
= destination
.getUniqueInteger("SELECT count(*) FROM Source s WHERE s.OriginalDB = 'erms'"); // +1 for the source reference "erms" but this has no OriginalDB
101 boolean success
= equals("Reference count ", countSrc
, countDest
);
105 private boolean equals(String messageStart
, int nSrc
, int nDest
) {
107 String message
= messageStart
+ " must be equal, but was not.\n Source: "+ nSrc
+ "; Destination: " + nDest
;
108 logger
.warn(message
);
111 logger
.info(messageStart
+ " were equal: " + nSrc
);
116 private boolean equals(String messageStart
, String strSrc
, String strDest
) {
117 if (!CdmUtils
.nullSafeEqual(strSrc
, strDest
)){
118 int index
= diffIndex(strSrc
, strDest
);
119 String message
= messageStart
+ " must be equal, but was not at "+index
+".\n Source: "+ strSrc
+ "\n Destination: " + strDest
;
120 logger
.warn(message
);
123 logger
.info(messageStart
+ " were equal: " + strSrc
);
132 private int diffIndex(String strSrc
, String strDest
) {
133 if (strSrc
== null || strDest
== null){
137 for (i
= 0; i
<strSrc
.length() && i
<strDest
.length() ;i
++) {
138 if (strSrc
.charAt(i
)!= strDest
.charAt(i
)){
142 if(strSrc
.length()!=strDest
.length()){
143 return Math
.max(strSrc
.length(), strDest
.length());
148 //** ************* MAIN ********************************************/
152 public static void main(String
[] args
){
153 PesiErmsValidator validator
= new PesiErmsValidator();
154 validator
.invoke(defaultSource
, defaultDestination
);