ref #8508 add some more columns to Reference validation
[cdmlib-apps.git] / cdm-pesi / src / main / java / eu / etaxonomy / cdm / io / pesi / erms / validation / PesiErmsValidator.java
1 /**
2 * Copyright (C) 2019 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9 package eu.etaxonomy.cdm.io.pesi.erms.validation;
10
11 import java.sql.ResultSet;
12 import java.sql.SQLException;
13
14 import org.apache.log4j.Logger;
15
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;
20
21 /**
22 * Tests the ERMS -> PESI pipeline by comparing the source DB with destination PESI DB.
23 *
24 * @author a.mueller
25 * @since 01.09.2019
26 */
27 public class PesiErmsValidator {
28
29 private static final Logger logger = Logger.getLogger(PesiErmsValidator.class);
30
31 private static final Source defaultSource = PesiSources.PESI2019_ERMS();
32 private static final Source defaultDestination = PesiDestinations.pesi_test_local_CDM_ERMS2PESI();
33
34 private Source source = defaultSource;
35 private Source destination = defaultDestination;
36
37 public void invoke(Source source, Source destination){
38 boolean success = true;
39 this.source = source;
40 this.destination = destination;
41 success &= testReferences();
42 success &= testTaxa();
43 success &= testTaxonRelations();
44 //TBC
45 System.out.println("end validation " + (success? "":"NOT ") + "successful.");
46 }
47
48 private boolean testTaxonRelations() {
49 boolean success = true;
50 return success;
51 }
52
53 private boolean testTaxa() {
54 boolean success = testTaxaCount();
55 return success;
56 }
57
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);
62 }
63
64 private boolean testReferences() {
65 boolean success = testReferenceCount();
66 if (success){
67 try {
68 success &= testSingleReferences();
69 } catch (SQLException e) {
70 e.printStackTrace();
71 }
72 }
73 return success;
74 }
75
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);
83 }
84 return success;
85 }
86
87 private boolean testSingleReference(ResultSet srcRS, ResultSet destRS) throws SQLException {
88 //id, RefIdInSource
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"));
94 //TODO TBC
95 return success;
96 }
97
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);
102 return success;
103 }
104
105 private boolean equals(String messageStart, int nSrc, int nDest) {
106 if (nSrc != nDest){
107 String message = messageStart + " must be equal, but was not.\n Source: "+ nSrc + "; Destination: " + nDest;
108 logger.warn(message);
109 return false;
110 }else{
111 logger.info(messageStart + " were equal: " + nSrc);
112 return true;
113 }
114 }
115
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);
121 return false;
122 }else{
123 logger.info(messageStart + " were equal: " + strSrc);
124 return true;
125 }
126 }
127 /**
128 * @param strSrc
129 * @param strDest
130 * @return
131 */
132 private int diffIndex(String strSrc, String strDest) {
133 if (strSrc == null || strDest == null){
134 return 0;
135 }
136 int i;
137 for (i = 0; i<strSrc.length() && i<strDest.length() ;i++) {
138 if (strSrc.charAt(i)!= strDest.charAt(i)){
139 return i;
140 }
141 }
142 if(strSrc.length()!=strDest.length()){
143 return Math.max(strSrc.length(), strDest.length());
144 }
145 return i;
146 }
147
148 //** ************* MAIN ********************************************/
149
150
151
152 public static void main(String[] args){
153 PesiErmsValidator validator = new PesiErmsValidator();
154 validator.invoke(defaultSource, defaultDestination);
155 System.exit(0);
156 }
157 }