Commit 0447ea29a00a7a17a505522a5e0b9e54a9c7e0b8

Authored by Paulo Graça
1 parent ad580ed832
Exists in tests

PG: create preparation task and fix import test

ptcrisync-test/pom.xml
... ... @@ -45,7 +45,7 @@
45 45 </resource>
46 46 </resources>
47 47 <plugins>
48   - <plugin>
  48 + <!-- plugin>
49 49 <groupId>org.codehaus.mojo</groupId>
50 50 <artifactId>exec-maven-plugin</artifactId>
51 51 <version>1.1.1</version>
... ... @@ -57,11 +57,10 @@
57 57 </goals>
58 58 <configuration>
59 59 <mainClass>pt.ptcris.test.main.MainTester</mainClass>
60   - <!--arguments> <argument>prepare</argument> </arguments -->
61 60 </configuration>
62 61 </execution>
63 62 </executions>
64   - </plugin>
  63 + </plugin-->
65 64  
66 65 <plugin>
67 66 <groupId>org.apache.maven.plugins</groupId>
... ...
ptcrisync-test/src/main/java/pt/ptcris/test/ImportTest.java
... ... @@ -16,15 +16,20 @@ import org.junit.runner.Result;
16 16 import org.junit.runner.notification.Failure;
17 17 import org.um.dsi.gavea.orcid.client.exception.OrcidClientException;
18 18 import org.um.dsi.gavea.orcid.model.activities.ActivitiesSummary;
  19 +import org.um.dsi.gavea.orcid.model.common.RelationshipType;
  20 +import org.um.dsi.gavea.orcid.model.work.ExternalIdentifier;
  21 +import org.um.dsi.gavea.orcid.model.work.ExternalIdentifierType;
19 22 import org.um.dsi.gavea.orcid.model.work.Work;
20 23  
21 24 import pt.ptcris.PTCRISync;
  25 +import pt.ptcris.ORCIDClient;
22 26 import pt.ptcris.ORCIDClientImpl;
23 27 import pt.ptcris.ORCIDHelper;
24 28 import pt.ptcris.test.dummy.ProgressHandlerDummyImpl;
25 29 import pt.ptcris.test.preparation.ActivitiesSummaryTestPrepare;
26 30 import pt.ptcris.test.preparation.DefaultTestOrcidClient;
27 31 import pt.ptcris.test.preparation.WorkTestPrepare;
  32 +import pt.ptcris.test.preparation.WorkTestPrepareSettings;
28 33 import pt.ptcris.test.properties.TestPropertiesLoader;
29 34  
30 35 import org.apache.logging.log4j.LogManager;
... ... @@ -45,6 +50,13 @@ public class ImportTest {
45 50  
46 51 _log.info("Prepare data for: " + orcidId);
47 52 ORCIDClientImpl orcidClient = (ORCIDClientImpl) DefaultTestOrcidClient.getDefaultClient();
  53 +
  54 + WorkTestPrepareSettings settings = new WorkTestPrepareSettings();
  55 + settings.setClient(orcidClient);
  56 + settings.setIgnoreLimits(Boolean.parseBoolean(properties.getProperty("test_ignore_limits")));
  57 + settings.setTestImportLimit(Integer.parseInt(properties.getProperty("test_import_limit")));
  58 +
  59 + WorkTestPrepare.prepareImport(settings);
48 60  
49 61 // Empty list of local works
50 62 List<Work> localWorks = new LinkedList<Work>();
... ... @@ -52,10 +64,11 @@ public class ImportTest {
52 64 int numberOfWorks = 0;
53 65 int numberOfImportedWorks = 0;
54 66  
  67 +
55 68 try {
56   - // Check the number of exported works
  69 + // Check the number of imported works
57 70 ORCIDHelper helper = new ORCIDHelper(orcidClient);
58   -
  71 +
59 72 numberOfWorks = helper.getAllWorkSummaries().size();
60 73  
61 74 _log.info("Import works: " + numberOfWorks);
... ... @@ -88,66 +101,116 @@ public class ImportTest {
88 101 public void importfromOrcid() {
89 102 boolean testResult = false;
90 103 String orcidId = properties.getProperty("orcid_id");
91   - boolean ignoreLimits = Boolean.parseBoolean(properties.getProperty("test_ignore_limits"));
92 104  
93 105 _log.info("Prepare data for: " + orcidId);
94 106 ORCIDClientImpl orcidClient = (ORCIDClientImpl) DefaultTestOrcidClient.getDefaultClient();
95 107  
  108 + WorkTestPrepareSettings settings = new WorkTestPrepareSettings();
  109 + settings.setClient(orcidClient);
  110 + settings.setIgnoreLimits(Boolean.parseBoolean(properties.getProperty("test_ignore_limits")));
  111 + settings.setTestImportLimit(Integer.parseInt(properties.getProperty("test_import_limit")));
  112 +
  113 + List<Work> localWorks = WorkTestPrepare.prepareImport(settings);
  114 + localWorks.remove(localWorks.size()-1);
  115 +
96 116 int numberOfWorks = 0;
  117 + int numberOfLocalWorks = localWorks.size();
97 118 int numberOfImportedWorks = 0;
98 119  
99   - ActivitiesSummary activitiesSummary;
  120 +
100 121 try {
101   - //Load from local file activities
102   - activitiesSummary = ActivitiesSummaryTestPrepare.loadFromResources();
103   - List<BigInteger> putCodesList = ORCIDHelper.getSummaryPutCodes(activitiesSummary);
104   -
105 122  
106   - if (!ignoreLimits) {
107   - int nRecordsImport = Integer.parseInt(properties.getProperty("test_import_limit"));
108   - // Limit the numbers to import
109   - if (nRecordsImport < putCodesList.size()) {
110   - putCodesList = putCodesList.subList(0, nRecordsImport);
111   - }
112   - }
  123 + // Check the number of exported works
  124 + ORCIDHelper helper = new ORCIDHelper(orcidClient);
  125 +
  126 + numberOfWorks = helper.getSourcedWorkSummaries().size();
  127 +
  128 + List<Work> remoteWorks = PTCRISync.importWorks(orcidClient, localWorks, progressHandler);
  129 + numberOfImportedWorks = remoteWorks.size();
  130 +
  131 + _log.info("Import works: " + numberOfWorks);
  132 + _log.info("Number of imported works: " + numberOfImportedWorks);
  133 + _log.info("Number of Local works: " + numberOfLocalWorks);
  134 + testResult = true;
  135 +
  136 + assertTrue("Number of exported that should match: " + numberOfWorks,
  137 + ((numberOfImportedWorks+numberOfLocalWorks) == numberOfWorks));
  138 + _log.info("[exportToNewProfile] " + "PASSED");
  139 + } catch (OrcidClientException orcidException) {
  140 + _log.fatal(orcidException.getDeveloperMessage());
  141 + _log.error("[exportToNewProfile] " + "FAILED");
  142 + } catch (Exception e) {
  143 + e.printStackTrace();
  144 + } finally {
  145 +
  146 + assertTrue("Import new works", testResult);
  147 + }
  148 +
  149 +
  150 + }
  151 +
  152 + /**
  153 + * Local APP profile with Works and we want to import new records updates from ORCID
  154 + */
  155 + @Test
  156 + public void importUpdatesfromOrcid() {
  157 + boolean testResult = false;
  158 + String orcidId = properties.getProperty("orcid_id");
  159 +
  160 + _log.info("Prepare data for: " + orcidId);
  161 + ORCIDClientImpl orcidClient = (ORCIDClientImpl) DefaultTestOrcidClient.getDefaultClient();
  162 +
  163 + WorkTestPrepareSettings settings = new WorkTestPrepareSettings();
  164 + settings.setClient(orcidClient);
  165 + settings.setIgnoreLimits(Boolean.parseBoolean(properties.getProperty("test_ignore_limits")));
  166 + settings.setTestImportLimit(Integer.parseInt(properties.getProperty("test_import_limit")));
  167 +
  168 + List<Work> localWorks = WorkTestPrepare.prepareImport(settings);
  169 +
  170 +
  171 + int numberOfWorks = 0;
  172 + int numberOfImportedWorks = 0;
  173 +
  174 +
  175 + try {
  176 +
  177 + // Add extra identifier
  178 + ExternalIdentifier internalId = new ExternalIdentifier();
  179 + internalId.setExternalIdentifierType(ExternalIdentifierType.SOURCE_WORK_ID);
  180 + internalId.setExternalIdentifierId("PTCRIS_01");
  181 + // You must indicate the Relationship or the External URL
  182 + internalId.setRelationship(RelationshipType.SELF);
  183 + WorkTestPrepare.updateSetExternalIdentifier (orcidClient,localWorks.get(0),internalId);
113 184  
114   - //Will delete existing and import new works to ORCID
115   - List<Work> localWorks = WorkTestPrepare.createAndLoadWorks(orcidClient,putCodesList);
116 185  
117   - try {
118   -
119   - // Check the number of exported works
120   - ORCIDHelper helper = new ORCIDHelper(orcidClient);
121   -
122   - numberOfWorks = helper.getSourcedWorkSummaries().size();
123   -
124   - List<Work> remoteWorks = PTCRISync.importWorks(orcidClient, localWorks, progressHandler);
125   - numberOfImportedWorks = remoteWorks.size();
126   -
127   - _log.info("Import works: " + numberOfWorks);
128   - _log.info("Number of imported works: " + numberOfImportedWorks);
129   - testResult = true;
130   -
131   - assertTrue("Number of exported that should match: " + numberOfWorks,
132   - (numberOfImportedWorks == numberOfWorks));
133   - _log.info("[exportToNewProfile] " + "PASSED");
134   - } catch (OrcidClientException orcidException) {
135   - _log.fatal(orcidException.getDeveloperMessage());
136   - _log.error("[exportToNewProfile] " + "FAILED");
137   - } catch (Exception e) {
138   - e.printStackTrace();
139   - } finally {
140   -
141   - assertTrue("Import new works", testResult);
142   - }
143   - } catch (JAXBException e1) {
144   - // TODO Auto-generated catch block
145   - e1.printStackTrace();
146   - } catch (OrcidClientException e2) {
147   - e2.printStackTrace();
148   - }
  186 + // Check the number of exported works
  187 + ORCIDHelper helper = new ORCIDHelper(orcidClient);
149 188  
150   - }
  189 + numberOfWorks = helper.getSourcedWorkSummaries().size();
  190 +
  191 + List<Work> remoteWorks = PTCRISync.importUpdates(orcidClient, localWorks, progressHandler);
  192 + numberOfImportedWorks = remoteWorks.size();
  193 +
  194 + _log.info("Import works: " + numberOfWorks);
  195 + _log.info("Number of imported works: " + numberOfImportedWorks);
  196 + testResult = true;
  197 +
  198 + assertTrue("Number of exported that should match: " + 1,
  199 + (numberOfImportedWorks == 1));
  200 + _log.info("[exportToNewProfile] " + "PASSED");
  201 + } catch (OrcidClientException orcidException) {
  202 + _log.fatal(orcidException.getDeveloperMessage());
  203 + _log.error("[exportToNewProfile] " + "FAILED");
  204 + } catch (Exception e) {
  205 + e.printStackTrace();
  206 + } finally {
  207 +
  208 + assertTrue("Import new works", testResult);
  209 + }
  210 +
  211 +
  212 +
  213 + }
151 214  
152 215 public static void main(String[] args) {
153 216 Result result = JUnitCore.runClasses(ImportTest.class);
... ...
ptcrisync-test/src/main/java/pt/ptcris/test/PerformanceTest.java
... ... @@ -117,10 +117,6 @@ public class PerformanceTest {
117 117 } catch (AssertionError ae) {
118 118 testResult = false;
119 119 _log.error("[exportTimetoOrcid] " + ae.getMessage());
120   - } catch (InterruptedException ie) {
121   - testResult = false;
122   - _log.error("[exportTimetoOrcid] Thread Exception");
123   - ie.printStackTrace();
124 120 } catch (Exception e) {
125 121 testResult = false;
126 122 _log.error("[exportTimetoOrcid] Exception" + e.getMessage());
... ... @@ -209,10 +205,6 @@ public class PerformanceTest {
209 205 } catch (AssertionError e) {
210 206 testResult = false;
211 207 _log.error("[importTimetoOrcid] " + e.getMessage());
212   - } catch (InterruptedException ie) {
213   - testResult = false;
214   - _log.error("[exportTimetoOrcid] Thread Exception");
215   - ie.printStackTrace();
216 208 } catch (Exception e) {
217 209 e.printStackTrace();
218 210 } finally {
... ...
ptcrisync-test/src/main/java/pt/ptcris/test/dummy/ProgressHandlerDummyImpl.java
... ... @@ -115,6 +115,8 @@ public class ProgressHandlerDummyImpl implements ProgressHandler {
115 115  
116 116 private Long calculateMax () {
117 117 Long[] data = timeEachExecution.toArray(new Long[0]);
  118 + if (data.length <= 3) return 0l;
  119 +
118 120 // Finding the largest element
119 121 long max = (data[1]-data[0]);
120 122 for (int i = 2; i < data.length-1; i++) {
... ... @@ -125,6 +127,8 @@ public class ProgressHandlerDummyImpl implements ProgressHandler {
125 127  
126 128 private double calculateAverage() {
127 129 Long[] data = (Long[]) timeEachExecution.toArray(new Long[0]);
  130 + if (data.length < 3) return 0.0d;
  131 +
128 132 // calculate sum
129 133 long sum = 0;
130 134 //It starts on 1 instead of 0 because 0 is the init process
... ...
ptcrisync-test/src/main/java/pt/ptcris/test/preparation/WorkTestPrepare.java
... ... @@ -13,7 +13,9 @@ import org.apache.logging.log4j.Logger;
13 13 import org.um.dsi.gavea.orcid.client.OrcidAccessToken;
14 14 import org.um.dsi.gavea.orcid.client.exception.OrcidClientException;
15 15 import org.um.dsi.gavea.orcid.model.activities.ActivitiesSummary;
  16 +import org.um.dsi.gavea.orcid.model.work.ExternalIdentifier;
16 17 import org.um.dsi.gavea.orcid.model.work.Work;
  18 +import org.um.dsi.gavea.orcid.model.work.WorkExternalIdentifiers;
17 19  
18 20 import pt.ptcris.ORCIDClient;
19 21 import pt.ptcris.ORCIDClientImpl;
... ... @@ -93,6 +95,53 @@ public class WorkTestPrepare {
93 95 preparedWork.save(work, filename);
94 96 }
95 97  
  98 + @SuppressWarnings("restriction")
  99 + public static List<Work> prepareImport (WorkTestPrepareSettings settings) {
  100 + //Load from local file activities
  101 + ORCIDClientImpl orcidClient = (ORCIDClientImpl) settings.getClient();
  102 +
  103 + ActivitiesSummary activitiesSummary;
  104 + List<BigInteger> putCodesList;
  105 + try {
  106 + activitiesSummary = ActivitiesSummaryTestPrepare.loadFromResources();
  107 + putCodesList = ORCIDHelper.getSummaryPutCodes(activitiesSummary);
  108 +
  109 + if (!settings.getIgnoreLimits()) {
  110 + int nRecordsImport = settings.getTestImportLimit();
  111 + // Limit the numbers to import
  112 + if (nRecordsImport < putCodesList.size()) {
  113 + putCodesList = putCodesList.subList(0, nRecordsImport);
  114 + }
  115 + }
  116 +
  117 + //Will delete existing and import new works to ORCID
  118 + return createAndLoadWorks(orcidClient,putCodesList);
  119 + } catch (JAXBException e) {
  120 + // TODO Auto-generated catch block
  121 + e.printStackTrace();
  122 + } catch (OrcidClientException e) {
  123 + // TODO Auto-generated catch block
  124 + e.printStackTrace();
  125 + }
  126 +
  127 + return new LinkedList<Work>();
  128 + }
  129 +
  130 +
  131 + public static void updateSetExternalIdentifier (ORCIDClient client, Work work,ExternalIdentifier externalId) throws OrcidClientException {
  132 + //This solution will not interfere with work object
  133 + Work temporaryWork = ORCIDHelper.clone(work);
  134 + // Set the External Identifiers
  135 + WorkExternalIdentifiers externalIdentifiers = new WorkExternalIdentifiers();
  136 + //Add add external Identifiers
  137 + for (ExternalIdentifier externaIdentifier:work.getExternalIdentifiers().getWorkExternalIdentifier()) {
  138 + externalIdentifiers.getWorkExternalIdentifier().add(externaIdentifier);
  139 + }
  140 + externalIdentifiers.getWorkExternalIdentifier().add(externalId);
  141 + temporaryWork.setExternalIdentifiers(externalIdentifiers);
  142 +
  143 + client.updateWork(temporaryWork.getPutCode(), temporaryWork);
  144 + }
96 145  
97 146 /**
98 147 * This main method will facilitate the transfer of content on both ORCID (public to sandbox)
... ...
ptcrisync-test/src/main/java/pt/ptcris/test/preparation/WorkTestPrepareSettings.java
... ... @@ -0,0 +1,34 @@
  1 +package pt.ptcris.test.preparation;
  2 +
  3 +import pt.ptcris.ORCIDClient;
  4 +
  5 +public class WorkTestPrepareSettings {
  6 + boolean ignoreLimits;
  7 + int nRecordsImport;
  8 + int testImportLimit;
  9 + ORCIDClient orcidClient;
  10 +
  11 + public ORCIDClient getClient () {
  12 + return this.orcidClient;
  13 + }
  14 +
  15 + public void setClient (ORCIDClient orcidClient) {
  16 + this.orcidClient = orcidClient;
  17 + }
  18 +
  19 + public boolean getIgnoreLimits () {
  20 + return this.ignoreLimits;
  21 + }
  22 +
  23 + public void setIgnoreLimits (boolean ignoreLimits) {
  24 + this.ignoreLimits = ignoreLimits;
  25 + }
  26 +
  27 + public int getTestImportLimit () {
  28 + return this.testImportLimit;
  29 + }
  30 +
  31 + public void setTestImportLimit (int testImportLimit) {
  32 + this.testImportLimit = testImportLimit;
  33 + }
  34 +}
... ...
ptcrisync-test/src/main/resources/log4j2.xml
... ... @@ -12,8 +12,8 @@
12 12 <Root level="error">
13 13 <AppenderRef ref="Console"/>
14 14 </Root>
15   - <!--Root level="trace">
  15 + <Root level="trace">
16 16 <AppenderRef ref="Console"/>
17   - </Root-->
  17 + </Root>
18 18 </Loggers>
19 19 </Configuration>
... ...