Commit 19b59a898fcdd7f0066cbe7fe797045ec1517762

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

PG: create java folder and move src for a maven project

Showing 29 changed files with 1406 additions and 1619 deletions   Show diff stats
ptcrisync-test/resources/.gitignore
... ... @@ -1,2 +0,0 @@
1   -# special configurations
2   -*.properties
ptcrisync-test/resources/log4j2.xml
... ... @@ -1,19 +0,0 @@
1   -<?xml version="1.0" encoding="UTF-8"?>
2   -<Configuration package="main" status="WARN">
3   -<Appenders>
4   - <Console name="Console" target="SYSTEM_OUT">
5   - <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
6   - </Console>
7   -</Appenders>
8   -<Loggers>
9   - <Logger name="log4j.test.Log4jTest" level="trace">
10   - <AppenderRef ref="Console"/>
11   - </Logger>
12   - <Root level="info">
13   - <AppenderRef ref="Console"/>
14   - </Root>
15   - <!-- Root level="trace">
16   - <AppenderRef ref="Console"/>
17   - </Root-->
18   -</Loggers>
19   -</Configuration>
ptcrisync-test/resources/test.properties.default
... ... @@ -1,41 +0,0 @@
1   -#########################################
2   -# This file is a template
3   -# you should renamed it to:
4   -# test.properties
5   -# and change the settings
6   -
7   -############## URLs #####################
8   -# Request a token URL example
9   -# https://sandbox.orcid.org/oauth/authorize?client_id=0000-0000-0000-0000&response_type=code&scope=/authenticate%20/orcid-profile/read-limited%20/orcid-bio/update%20/activities/update&redirect_uri=https://developers.google.com/oauthplayground
10   -
11   -# Authorization endpoint: https://sandbox.orcid.org/oauth/authorize
12   -# Token endpoint: https://api.sandbox.orcid.org/oauth/token
13   -
14   -
15   -# to access the member api, you should request an access token
16   -orcid_login_uri = https://sandbox.orcid.org
17   -orcid_api_uri = https://api.sandbox.orcid.org
18   -orcid_redirect_uri = https://developers.google.com/oauthplayground
19   -
20   -orcid_client_id = 0000-0000-0000-0000
21   -orcid_client_secret = 00000000-0000-0000-0000-000000000000
22   -
23   -
24   -# You should request a token in orcid sandbox with the following scopes:
25   -# "/authenticate /orcid-profile/read-limited /orcid-bio/update /activities/update"
26   -orcid_id = 0000-0000-0000-0000
27   -orcid_access_token = 00000000-0000-0000-0000-000000000000
28   -
29   -# Resources base path
30   -source_orcid_id = 0000-0000-0000-0000
31   -source_resources_path = resources/0000-0000-0000-0000
32   -
33   -# Set to "true" if you need to ignore the specified limits
34   -test_ignore_limits = true
35   -test_export_limit = 10
36   -test_import_limit = 5
37   -
38   -# Set to "true" if you need to test performance without ORCID API communications
39   -test_dummy_client = false
40   -# Number of records to test
41   -test_performance_limit = 10, 20, 30, 50, 100, 200, 500
42 0 \ No newline at end of file
ptcrisync-test/src/main/MainTester.java
... ... @@ -1,213 +0,0 @@
1   -package main;
2   -
3   -import java.util.LinkedList;
4   -import java.util.List;
5   -import java.util.ArrayList;
6   -import java.util.logging.ConsoleHandler;
7   -import java.util.logging.Level;
8   -import java.util.logging.Logger;
9   -import java.util.logging.SimpleFormatter;
10   -
11   -import org.um.dsi.gavea.orcid.model.common.RelationshipType;
12   -import org.um.dsi.gavea.orcid.model.common.ScopePathType;
13   -import org.um.dsi.gavea.orcid.model.work.ExternalIdentifier;
14   -import org.um.dsi.gavea.orcid.model.work.ExternalIdentifierType;
15   -import org.um.dsi.gavea.orcid.model.work.Work;
16   -import org.um.dsi.gavea.orcid.model.work.WorkExternalIdentifiers;
17   -import org.um.dsi.gavea.orcid.model.work.WorkTitle;
18   -import org.um.dsi.gavea.orcid.model.work.WorkType;
19   -import org.um.dsi.gavea.orcid.client.OrcidAccessToken;
20   -import org.um.dsi.gavea.orcid.model.activities.ActivitiesSummary;
21   -import org.um.dsi.gavea.orcid.model.activities.Identifier;
22   -import org.um.dsi.gavea.orcid.model.activities.WorkGroup;
23   -
24   -//import org.um.dsi.gavea.orcid.model.work.Work;
25   -import org.um.dsi.gavea.orcid.client.OrcidOAuthClient;
26   -import org.um.dsi.gavea.orcid.client.exception.OrcidClientException;
27   -
28   -//import pt.ptcris.PTCRISync;
29   -//import pt.ptcris.handlers.ProgressHandler;
30   -
31   -public class MainTester {
32   - private static Logger logger = Logger.getLogger(MainTester.class.getName());
33   -
34   - private static final String orcidID = "0000-0000-0000-0000";
35   - private static final String accessToken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
36   - private static final String serviceSourceName = "Local CRIS";
37   -
38   - public static void main(String[] args) {
39   - /*ConsoleHandler handler = new ConsoleHandler();
40   - handler.setFormatter(new SimpleFormatter());
41   - handler.setLevel(Level.ALL);
42   - logger.setLevel(Level.ALL);
43   - logger.addHandler(handler);
44   -
45   - List<Work> works = new LinkedList<Work>();
46   - MainTester progressHandler = new MainTester();
47   -
48   - PTCRISync.export(orcidID, accessToken, works, serviceSourceName, progressHandler);
49   - List<Work> worksToImport = PTCRISync.importWorks(orcidID, accessToken, works, progressHandler);
50   - List<Work> worksToUpdate = PTCRISync.importUpdates(orcidID, accessToken, works, progressHandler);
51   - */
52   -
53   - //OAuth flow: Server-side
54   - //OAuth endpoints: Custom
55   - //Authorization endpoint: https://sandbox.orcid.org/oauth/authorize
56   - //Token endpoint: https://api.sandbox.orcid.org/oauth/token
57   - //Scopes: /authenticate /orcid-profile/read-limited /orcid-bio/update /activities/update
58   - //OAuth Client ID: 0000-0002-3788-2479
59   - //OAuth Client secret: 002c639b-ebe9-46cf-867f-c3642a68a0f5
60   -
61   -
62   - String loginUri = "https://sandbox.orcid.org";
63   - String clientSecret = "002c639b-ebe9-46cf-867f-c3642a68a0f5";
64   - String clientId = "0000-0002-3788-2479";
65   - String apiUri = "https://api.sandbox.orcid.org";
66   - String redirectUri = "https://developers.google.com/oauthplayground";
67   -
68   - List<ScopePathType> scopes = new ArrayList<ScopePathType>();
69   - scopes.add(ScopePathType.AUTHENTICATE);
70   - scopes.add(ScopePathType.ORCID_PROFILE_READ_LIMITED);
71   - scopes.add(ScopePathType.ACTIVITIES_UPDATE);
72   - scopes.add(ScopePathType.ORCID_BIO_UPDATE);
73   - scopes.add(ScopePathType.ORCID_WORKS_CREATE);
74   -
75   - //scopes.add(ScopePathType.ORCID_WORKS_CREATE);
76   - //scopes.add(ScopePathType.ORCID_WORKS_READ_LIMITED);
77   - //scopes.add(ScopePathType.ORCID_WORKS_UPDATE);
78   -
79   - OrcidOAuthClient orcidClient = new OrcidOAuthClient(loginUri, apiUri, clientId, clientSecret, redirectUri);
80   -
81   - String orcid = "0000-0002-5216-3501";
82   - String state = "bearer";
83   - OrcidAccessToken orcidToken;
84   - OrcidAccessToken orcidToken2 = new OrcidAccessToken();
85   - String accessToken;
86   -
87   - /* code = zTahts
88   - {
89   - "name": "Marina PTCRIS R. Cunha",
90   - "access_token": "decd27c2-d605-4ea4-ad44-5858ac0172b7",
91   - "expires_in": 3599,
92   - "token_type": "bearer",
93   - "orcid": "0000-0002-6305-4618",
94   - "scope": "/authenticate /orcid-profile/read-limited /orcid-bio/update /activities/update",
95   - "refresh_token": "0f1d5cfa-c756-4325-b59c-831ae28159ae"
96   - }
97   - */
98   -
99   - //https://api.sandbox.orcid.org/v2.0_rc2/#!/Member_API_v2.0_rc1/viewActivities
100   - orcidToken2.setName("Marina PTCRIS R. Cunha");
101   - orcidToken2.setOrcid("0000-0002-6305-4618");
102   - orcidToken2.setAccess_token("b121460e-bddd-403c-9fd3-028a1abbe7f9");
103   - orcidToken2.setScope("/authenticate /activities/update /orcid-profile/read-limited /orcid-bio/update");
104   - //orcidToken2.setScope("/authenticate /funding/create /orcid-bio/update /orcid-bio/read-limited /affiliations/update /affiliations/create /orcid-bio/external-identifiers/create /orcid-works/update /orcid-profile/read-limited /funding/update /read-limited /activities/read-limited /orcid-works/create /orcid-works/read-limited /activities/update /person/read-limited /person/update");
105   - orcidToken2.setRefresh_token("0f1d5cfa-c756-4325-b59c-831ae28159ae");
106   - orcidToken2.setToken_type("bearer");
107   - orcidToken2.setExpires_in(3599);
108   -
109   -
110   -
111   -
112   -
113   - try {
114   - String authorizationURI = orcidClient.getAuthorizeUriLogin(scopes,state,orcid);
115   - System.out.println(authorizationURI);
116   - //TODO: request authorizationURI and get the code from browser URL
117   -
118   - //this authorizationCode is supplied by the google playground, you must have a landingpage to get that code
119   - /*String authorizationCode = "J9VmA8";
120   -
121   - try {
122   - orcidToken = orcidClient.getAccessTokens(authorizationCode);
123   - accessToken = orcidToken.getAccess_token();
124   - accessToken = "95c3fa9f-c0a1-49fe-bf4e-27caf1ed9419";
125   -
126   -
127   -
128   -
129   - } catch (OrcidClientException e) {
130   - // TODO Auto-generated catch block
131   - e.printStackTrace();
132   - }
133   - */
134   - } catch (Exception e) {
135   - // TODO Auto-generated catch block
136   - e.printStackTrace();
137   - }
138   -
139   -
140   - try {
141   - ActivitiesSummary activities = orcidClient.readActivitiesSummary(orcidToken2);
142   - List<WorkGroup> workGroupList = activities.getWorks().getGroup();
143   -
144   - for (WorkGroup workGroup : workGroupList) {
145   - System.out.print("PUTCODE: " + workGroup.getWorkSummary().get(0).getPutCode() + " - ");
146   - System.out.print(" Visibility: " + workGroup.getWorkSummary().get(0).getVisibility().toString() + " - ");
147   - System.out.println(workGroup.getWorkSummary().get(0).getTitle().getTitle());
148   -
149   - List<Identifier> identifiersList = workGroup.getIdentifiers().getIdentifier();
150   - for (Identifier identifier: identifiersList) {
151   - //print ID_TYPE: ID_VALUE
152   - System.out.print(identifier.getExternalIdentifierType() +": " + identifier.getExternalIdentifierId()+"\n");
153   - }
154   -
155   -
156   -
157   - }
158   -
159   - String putCode = "477804";
160   -
161   - Work work = orcidClient.readWork(orcidToken2, putCode);
162   - WorkTitle workTitle = new WorkTitle("Teste de submissão PTCRISSync",null,null);
163   - work.setTitle(workTitle);
164   -
165   - //Save work with changed title
166   - orcidClient.updateWork(orcidToken2, putCode, work);
167   - System.out.println("\n\nSaved with title: " + workTitle.getTitle());
168   -
169   - //Create a new work
170   - Work newWork = new Work();
171   - WorkTitle workTitle2 = new WorkTitle("PTCRISSync, new item",null,null);
172   - newWork.setTitle(workTitle2);
173   - newWork.setType(WorkType.JOURNAL_ARTICLE);
174   -
175   -
176   - ExternalIdentifier internalId = new ExternalIdentifier();
177   - internalId.setExternalIdentifierType(ExternalIdentifierType.SOURCE_WORK_ID);
178   - internalId.setExternalIdentifierId("PTCRIS_01");
179   - internalId.setRelationship(RelationshipType.SELF);
180   -
181   - //List<ExternalIdentifier> workExternalIdentifierList = new ArrayList<ExternalIdentifier>();
182   - //workExternalIdentifierList.add(internalId);
183   -
184   - WorkExternalIdentifiers externalIdentifiers = new WorkExternalIdentifiers();
185   - //externalIdentifiers.setWorkExternalIdentifier(workExternalIdentifierList);
186   - externalIdentifiers.getWorkExternalIdentifier().add(internalId);
187   - newWork.setExternalIdentifiers(externalIdentifiers);
188   -
189   - String newPutCode = orcidClient.addWork(orcidToken2, newWork);
190   - System.out.println("New item with PUTCODE: " + newPutCode);
191   -
192   -
193   - } catch (OrcidClientException e) {
194   - // TODO Auto-generated catch block
195   - e.printStackTrace();
196   - }
197   -
198   -
199   -
200   -
201   - //List<Work> works = new LinkedList<Work>();
202   - //MainTester progressHandler = new MainTester();
203   -
204   - //PTCRISync.export(orcidID, accessToken, works, serviceSourceName, progressHandler);
205   - //List<Work> worksToImport = PTCRISync.importWorks(orcidID, accessToken, works, progressHandler);
206   - //List<Work> worksToUpdate = PTCRISync.importUpdates(orcidID, accessToken, works, progressHandler);
207   -
208   -
209   -
210   -
211   - }
212   -
213   -}
ptcrisync-test/src/main/java/pt/ptcris/test/ExportTest.java
... ... @@ -0,0 +1,173 @@
  1 +package pt.ptcris.test;
  2 +
  3 +import static org.junit.Assert.assertTrue;
  4 +
  5 +import java.math.BigInteger;
  6 +import java.util.List;
  7 +import java.util.Properties;
  8 +
  9 +import org.junit.Test;
  10 +import org.junit.runner.JUnitCore;
  11 +import org.junit.runner.Result;
  12 +import org.junit.runner.notification.Failure;
  13 +
  14 +import org.um.dsi.gavea.orcid.client.exception.OrcidClientException;
  15 +import org.um.dsi.gavea.orcid.model.activities.ActivitiesSummary;
  16 +import org.um.dsi.gavea.orcid.model.work.Work;
  17 +import org.um.dsi.gavea.orcid.model.work.WorkSummary;
  18 +
  19 +import pt.ptcris.ORCIDClientImpl;
  20 +import pt.ptcris.ORCIDHelper;
  21 +import pt.ptcris.PTCRISync;
  22 +import pt.ptcris.test.dummy.ProgressHandlerDummyImpl;
  23 +import pt.ptcris.test.preparation.ActivitiesSummaryTestPrepare;
  24 +import pt.ptcris.test.preparation.DefaultTestOrcidClient;
  25 +import pt.ptcris.test.preparation.WorkTestPrepare;
  26 +import pt.ptcris.test.properties.TestPropertiesLoader;
  27 +
  28 +import javax.xml.bind.JAXBException;
  29 +
  30 +import org.apache.logging.log4j.LogManager;
  31 +import org.apache.logging.log4j.Logger;
  32 +
  33 +public class ExportTest {
  34 + private final Properties properties = TestPropertiesLoader.getInstance().getProperties();
  35 + private static final Logger _log = LogManager.getLogger(ExportTest.class);
  36 + private static ProgressHandlerDummyImpl progressHandler = new ProgressHandlerDummyImpl();
  37 +
  38 + /**
  39 + * Empty ORCID profile and we want to export works from a local app
  40 + */
  41 + @Test
  42 + public void exportToNewProfile() {
  43 + boolean testResult = false;
  44 +
  45 + String orcidId = properties.getProperty("orcid_id");
  46 + boolean ignoreLimits = Boolean.parseBoolean(properties.getProperty("test_ignore_limits"));
  47 +
  48 + try {
  49 + _log.info("Prepare data for: " + orcidId);
  50 + ORCIDClientImpl orcidClient = (ORCIDClientImpl) DefaultTestOrcidClient.getDefaultClient();
  51 +
  52 + ActivitiesSummary activitiesSummary = ActivitiesSummaryTestPrepare.loadFromResources();
  53 + List<BigInteger> putCodesList = ORCIDHelper.getWorkSummaryPutCodes(activitiesSummary);
  54 +
  55 + if (!ignoreLimits) {
  56 + int nRecords = Integer.parseInt(properties.getProperty("test_export_limit"));
  57 + //Limit the numbers to export
  58 + int putCodesListSize = putCodesList.size();
  59 + if ( putCodesListSize > nRecords ) {
  60 + putCodesList.subList(nRecords, putCodesListSize).clear();
  61 + }
  62 + }
  63 + List<Work> localWorks = WorkTestPrepare.loadLocalWorksFromResources(putCodesList);
  64 +
  65 + int numberOfLocalWorks = localWorks.size();
  66 + int numberOfExportedWorks = 0;
  67 +
  68 + try {
  69 + // Check the number of exported works
  70 + ORCIDHelper helper = new ORCIDHelper(orcidClient);
  71 +
  72 + _log.info("Deleting all existing works");
  73 + helper.deleteAllSourcedWorks();
  74 +
  75 + PTCRISync.export(orcidClient, localWorks, progressHandler);
  76 +
  77 + List<WorkSummary> exportedWorkSummary = helper.getSourcedWorkSummaries();
  78 + numberOfExportedWorks = exportedWorkSummary.size();
  79 +
  80 + _log.info("Localworks to export: " + numberOfLocalWorks);
  81 + _log.info("Exported content: " + numberOfExportedWorks);
  82 + testResult = true;
  83 +
  84 + assertTrue("Number of exported that should match: " + numberOfLocalWorks,
  85 + (numberOfExportedWorks == numberOfLocalWorks));
  86 + _log.info("[exportToNewProfile] " + "PASSED");
  87 + } catch (OrcidClientException orcidException) {
  88 + _log.fatal(orcidException.getDeveloperMessage());
  89 + _log.error("[exportToNewProfile] " + "FAILED");
  90 + } catch (Exception e) {
  91 + e.printStackTrace();
  92 + } finally {
  93 +
  94 + assertTrue("Export to new profile", testResult);
  95 + }
  96 +
  97 + } catch (JAXBException e) {
  98 + // TODO Auto-generated catch block
  99 + e.printStackTrace();
  100 + }
  101 + }
  102 +
  103 + /**
  104 + * Filled ORCID profile and we want to export works from local app
  105 + */
  106 + @Test
  107 + public void exportToProfile() {
  108 + boolean testResult = false;
  109 +
  110 + String orcidId = properties.getProperty("orcid_id");
  111 + boolean ignoreLimits = Boolean.parseBoolean(properties.getProperty("test_ignore_limits"));
  112 +
  113 + try {
  114 + _log.info("Prepare data for: " + orcidId);
  115 + ORCIDClientImpl orcidClient = (ORCIDClientImpl) DefaultTestOrcidClient.getDefaultClient();
  116 +
  117 + ActivitiesSummary activitiesSummary = ActivitiesSummaryTestPrepare.loadFromResources();
  118 + List<BigInteger> putCodesList = ORCIDHelper.getWorkSummaryPutCodes(activitiesSummary);
  119 +
  120 + if (!ignoreLimits) {
  121 + int nRecords = (Integer.parseInt(properties.getProperty("test_export_limit")) + 1);
  122 + //Limit the number of works to export
  123 + int putCodesListSize = putCodesList.size();
  124 + if ( putCodesListSize > nRecords ) {
  125 + putCodesList.subList(nRecords, putCodesListSize).clear();
  126 + }
  127 + }
  128 +
  129 + List<Work> localWorks = WorkTestPrepare.loadLocalWorksFromResources(putCodesList);
  130 +
  131 + int numberOfLocalWorks = localWorks.size();
  132 + int numberOfExportedWorks = 0;
  133 +
  134 + try {
  135 + // Check the number of exported works
  136 + ORCIDHelper helper = new ORCIDHelper(orcidClient);
  137 +
  138 + PTCRISync.export(orcidClient, localWorks, progressHandler);
  139 +
  140 +
  141 + List<WorkSummary> exportedWorkSummary = helper.getSourcedWorkSummaries();
  142 + numberOfExportedWorks = exportedWorkSummary.size();
  143 +
  144 + _log.info("Localworks to export: " + numberOfLocalWorks);
  145 + _log.info("Exported content: " + numberOfExportedWorks);
  146 + testResult = true;
  147 +
  148 + assertTrue("Number of exported that should match: " + numberOfLocalWorks,
  149 + (numberOfExportedWorks == numberOfLocalWorks));
  150 + _log.info("[exportToProfile] " + "PASSED");
  151 + } catch (OrcidClientException orcidException) {
  152 + _log.fatal(orcidException.getDeveloperMessage());
  153 + _log.error("[exportToProfile] " + "FAILED");
  154 + } catch (Exception e) {
  155 + e.printStackTrace();
  156 + } finally {
  157 +
  158 + assertTrue("Export to profile", testResult);
  159 + }
  160 +
  161 + } catch (JAXBException e) {
  162 + // TODO Auto-generated catch block
  163 + e.printStackTrace();
  164 + }
  165 + }
  166 +
  167 + public static void main(String[] args) {
  168 + Result result = JUnitCore.runClasses(ExportTest.class);
  169 + for (Failure failure : result.getFailures()) {
  170 + System.out.println(failure.toString());
  171 + }
  172 + }
  173 +}
... ...
ptcrisync-test/src/main/java/pt/ptcris/test/ImportTest.java
... ... @@ -0,0 +1,159 @@
  1 +package pt.ptcris.test;
  2 +
  3 +import static org.junit.Assert.assertTrue;
  4 +//import static org.junit.Assert.fail;
  5 +
  6 +import java.math.BigInteger;
  7 +import java.util.LinkedList;
  8 +import java.util.List;
  9 +import java.util.Properties;
  10 +
  11 +import javax.xml.bind.JAXBException;
  12 +
  13 +import org.junit.Test;
  14 +import org.junit.runner.JUnitCore;
  15 +import org.junit.runner.Result;
  16 +import org.junit.runner.notification.Failure;
  17 +import org.um.dsi.gavea.orcid.client.exception.OrcidClientException;
  18 +import org.um.dsi.gavea.orcid.model.activities.ActivitiesSummary;
  19 +import org.um.dsi.gavea.orcid.model.work.Work;
  20 +
  21 +import pt.ptcris.PTCRISync;
  22 +import pt.ptcris.ORCIDClientImpl;
  23 +import pt.ptcris.ORCIDHelper;
  24 +import pt.ptcris.test.dummy.ProgressHandlerDummyImpl;
  25 +import pt.ptcris.test.preparation.ActivitiesSummaryTestPrepare;
  26 +import pt.ptcris.test.preparation.DefaultTestOrcidClient;
  27 +import pt.ptcris.test.preparation.WorkTestPrepare;
  28 +import pt.ptcris.test.properties.TestPropertiesLoader;
  29 +
  30 +import org.apache.logging.log4j.LogManager;
  31 +import org.apache.logging.log4j.Logger;
  32 +
  33 +public class ImportTest {
  34 + private final Properties properties = TestPropertiesLoader.getInstance().getProperties();
  35 + private static final Logger _log = LogManager.getLogger(ImportTest.class);
  36 + private static ProgressHandlerDummyImpl progressHandler = new ProgressHandlerDummyImpl();
  37 +
  38 + /**
  39 + * Empty Local APP and we want to import all records from ORCID
  40 + */
  41 + @Test
  42 + public void importNewItems() {
  43 + boolean testResult = false;
  44 + String orcidId = properties.getProperty("orcid_id");
  45 +
  46 + _log.info("Prepare data for: " + orcidId);
  47 + ORCIDClientImpl orcidClient = (ORCIDClientImpl) DefaultTestOrcidClient.getDefaultClient();
  48 +
  49 + // Empty list of local works
  50 + List<Work> localWorks = new LinkedList<Work>();
  51 +
  52 + int numberOfWorks = 0;
  53 + int numberOfImportedWorks = 0;
  54 +
  55 + try {
  56 + // Check the number of exported works
  57 + ORCIDHelper helper = new ORCIDHelper(orcidClient);
  58 +
  59 + numberOfWorks = helper.getAllWorkSummaries().size();
  60 +
  61 + _log.info("Import works: " + numberOfWorks);
  62 + List<Work> remoteWorks = PTCRISync.importWorks(orcidClient, localWorks, progressHandler);
  63 + numberOfImportedWorks = remoteWorks.size();
  64 +
  65 + _log.info("Import works: " + numberOfWorks);
  66 + _log.info("Number of imported works: " + numberOfImportedWorks);
  67 + testResult = true;
  68 +
  69 + assertTrue("Number of exported that should match: " + numberOfWorks,
  70 + (numberOfImportedWorks == numberOfWorks));
  71 + _log.info("[exportToNewProfile] " + "PASSED");
  72 + } catch (OrcidClientException orcidException) {
  73 + _log.fatal(orcidException.getDeveloperMessage());
  74 + _log.error("[exportToNewProfile] " + "FAILED");
  75 + } catch (Exception e) {
  76 + e.printStackTrace();
  77 + } finally {
  78 +
  79 + assertTrue("Import new works", testResult);
  80 + }
  81 +
  82 + }
  83 +
  84 + /**
  85 + * Local APP profile with Works and we want to import new records from ORCID
  86 + */
  87 + @Test
  88 + public void importfromOrcid() {
  89 + boolean testResult = false;
  90 + String orcidId = properties.getProperty("orcid_id");
  91 + boolean ignoreLimits = Boolean.parseBoolean(properties.getProperty("test_ignore_limits"));
  92 +
  93 + _log.info("Prepare data for: " + orcidId);
  94 + ORCIDClientImpl orcidClient = (ORCIDClientImpl) DefaultTestOrcidClient.getDefaultClient();
  95 +
  96 + int numberOfWorks = 0;
  97 + int numberOfImportedWorks = 0;
  98 +
  99 + ActivitiesSummary activitiesSummary;
  100 + try {
  101 + //Load from local file activities
  102 + activitiesSummary = ActivitiesSummaryTestPrepare.loadFromResources();
  103 + List<BigInteger> putCodesList = ORCIDHelper.getWorkSummaryPutCodes(activitiesSummary);
  104 +
  105 +
  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 + }
  113 +
  114 + //Will delete existing and import new works to ORCID
  115 + List<Work> localWorks = WorkTestPrepare.createAndLoadWorks(orcidClient,putCodesList);
  116 +
  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 + }
  149 +
  150 + }
  151 +
  152 + public static void main(String[] args) {
  153 + Result result = JUnitCore.runClasses(ImportTest.class);
  154 + for (Failure failure : result.getFailures()) {
  155 + System.out.println(failure.toString());
  156 + }
  157 + }
  158 +
  159 +}
... ...
ptcrisync-test/src/main/java/pt/ptcris/test/IntegrationTest.java
... ... @@ -0,0 +1,111 @@
  1 +package pt.ptcris.test;
  2 +import static org.junit.Assert.assertTrue;
  3 +//import static org.junit.Assert.fail;
  4 +
  5 +import java.util.LinkedList;
  6 +import java.util.List;
  7 +import java.util.Properties;
  8 +
  9 +import org.junit.Test;
  10 +import org.junit.runner.JUnitCore;
  11 +import org.junit.runner.Result;
  12 +import org.junit.runner.notification.Failure;
  13 +import org.um.dsi.gavea.orcid.client.OrcidAccessToken;
  14 +import org.um.dsi.gavea.orcid.client.exception.OrcidClientException;
  15 +import org.um.dsi.gavea.orcid.model.activities.ActivitiesSummary;
  16 +import org.um.dsi.gavea.orcid.model.work.Work;
  17 +
  18 +import pt.ptcris.PTCRISync;
  19 +import pt.ptcris.ORCIDClientImpl;
  20 +import pt.ptcris.test.dummy.ProgressHandlerDummyImpl;
  21 +import pt.ptcris.test.properties.TestPropertiesLoader;
  22 +
  23 +import org.apache.logging.log4j.LogManager;
  24 +import org.apache.logging.log4j.Logger;
  25 +
  26 +public class IntegrationTest {
  27 + private final Properties properties = TestPropertiesLoader.getInstance().getProperties();
  28 + private static final Logger _log = LogManager.getLogger(IntegrationTest.class);
  29 + private static ProgressHandlerDummyImpl progressHandler = new ProgressHandlerDummyImpl();
  30 +
  31 +
  32 + /**
  33 + * The ORCID API is currently unavailable we want to acknowledge the behavior of the framework in this case
  34 + */
  35 + @Test
  36 + public void pingTest() {
  37 + boolean testResult = false;
  38 +
  39 + OrcidAccessToken orcidToken = new OrcidAccessToken();
  40 + orcidToken.setAccess_token(properties.getProperty("orcid_access_token"));
  41 + orcidToken.setOrcid(properties.getProperty("orcid_id"));
  42 +
  43 +
  44 + ORCIDClientImpl orcidClient = new ORCIDClientImpl(properties.getProperty("orcid_login_uri"),
  45 + properties.getProperty("orcid_api_uri"),
  46 + properties.getProperty("orcid_client_id"),
  47 + properties.getProperty("orcid_client_secret"),
  48 + properties.getProperty("orcid_redirect_uri"), orcidToken);
  49 +
  50 + try {
  51 + @SuppressWarnings("unused")
  52 + ActivitiesSummary activitiesSummary = orcidClient.getActivitiesSummary();
  53 + testResult = true;
  54 + _log.info("[pingTest] " + "PASSED");
  55 + } catch (OrcidClientException orcidException) {
  56 + _log.error("[pingTest] " + orcidException.getMessage());
  57 + _log.error("[pingTest] " + "FAILED");
  58 + } catch (Exception e) {
  59 + e.printStackTrace();
  60 + } finally {
  61 +
  62 + assertTrue ("Ping test", testResult);
  63 + }
  64 +
  65 + }
  66 +
  67 + /**
  68 + * The token is rather invalid or has expired we want to acknowledge the behavior of the framework in this cases
  69 + */
  70 + @Test
  71 + public void tokenTest() {
  72 + boolean testResult = false;
  73 + String orcidInvalidToken = "Invalid-Token-Test";
  74 +
  75 + OrcidAccessToken orcidToken = new OrcidAccessToken();
  76 + //Invalid token to Test
  77 + orcidToken.setAccess_token(orcidInvalidToken);
  78 + orcidToken.setOrcid(properties.getProperty("orcid_id"));
  79 +
  80 + ORCIDClientImpl orcidClient = new ORCIDClientImpl(properties.getProperty("orcid_login_uri"),
  81 + properties.getProperty("orcid_api_uri"),
  82 + properties.getProperty("orcid_client_id"),
  83 + properties.getProperty("orcid_client_secret"),
  84 + properties.getProperty("orcid_redirect_uri"), orcidToken);
  85 +
  86 + //Empty list of works
  87 + List<Work> localWorks = new LinkedList<Work>();
  88 +
  89 + try {
  90 + PTCRISync.importWorks(orcidClient, localWorks, progressHandler);
  91 +
  92 + _log.debug("[tokenTest] " + "Unknown behavior");
  93 + } catch (OrcidClientException orcidException) {
  94 + testResult = true;
  95 + _log.info("[tokenTest] " + "PASSED");
  96 + } finally {
  97 +
  98 + assertTrue ("Token test", testResult);
  99 + }
  100 +
  101 + }
  102 +
  103 +
  104 + public static void main(String[] args) {
  105 + Result result = JUnitCore.runClasses(IntegrationTest.class);
  106 + for (Failure failure : result.getFailures()) {
  107 + System.out.println(failure.toString());
  108 + }
  109 + }
  110 +
  111 +}
... ...
ptcrisync-test/src/main/java/pt/ptcris/test/PerformanceTest.java
... ... @@ -0,0 +1,217 @@
  1 +package pt.ptcris.test;
  2 +
  3 +import org.junit.Test;
  4 +import org.junit.runner.JUnitCore;
  5 +import org.junit.runner.Result;
  6 +import org.junit.runner.notification.Failure;
  7 +import org.um.dsi.gavea.orcid.client.exception.OrcidClientException;
  8 +import org.um.dsi.gavea.orcid.model.activities.ActivitiesSummary;
  9 +import org.um.dsi.gavea.orcid.model.work.Work;
  10 +import org.um.dsi.gavea.orcid.model.work.WorkSummary;
  11 +
  12 +import pt.ptcris.ORCIDClient;
  13 +import pt.ptcris.ORCIDHelper;
  14 +import pt.ptcris.PTCRISync;
  15 +import pt.ptcris.test.dummy.ORCIDClientDummyImpl;
  16 +import pt.ptcris.test.dummy.ProgressHandlerDummyImpl;
  17 +import pt.ptcris.test.preparation.ActivitiesSummaryTestPrepare;
  18 +import pt.ptcris.test.preparation.DefaultTestOrcidClient;
  19 +import pt.ptcris.test.preparation.WorkTestPrepare;
  20 +import pt.ptcris.test.properties.TestPropertiesLoader;
  21 +
  22 +import static org.junit.Assert.assertTrue;
  23 +
  24 +import java.math.BigInteger;
  25 +import java.text.DateFormat;
  26 +import java.text.SimpleDateFormat;
  27 +import java.util.Date;
  28 +import java.util.LinkedList;
  29 +import java.util.List;
  30 +import java.util.Properties;
  31 +
  32 +import javax.xml.bind.JAXBException;
  33 +
  34 +import org.apache.logging.log4j.LogManager;
  35 +import org.apache.logging.log4j.Logger;
  36 +
  37 +public class PerformanceTest {
  38 + private final Properties properties = TestPropertiesLoader.getInstance().getProperties();
  39 + private static final Logger _log = LogManager.getLogger(PerformanceTest.class);
  40 + private static DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
  41 + private static ProgressHandlerDummyImpl progressHandler = new ProgressHandlerDummyImpl();
  42 +
  43 + /**
  44 + * Empty ORCID profile and we want to measure the time of the export N works
  45 + * process from local app to ORCID
  46 + */
  47 + @Test
  48 + public void exportTimetoOrcid() {
  49 + boolean testResult = false;
  50 + String orcidId = properties.getProperty("orcid_id");
  51 + boolean isTestClient = Boolean.parseBoolean(properties.getProperty("test_dummy_client"));
  52 + String[] nTestRecords = properties.getProperty("test_performance_limits").split(",");
  53 +
  54 + _log.info("Prepare data for: " + orcidId);
  55 +
  56 + ORCIDClient orcidClient;
  57 + // To test the With and W/O the API
  58 + if (isTestClient) {
  59 + orcidClient = ORCIDClientDummyImpl.getDefaultClientInstance();
  60 + } else {
  61 + orcidClient = DefaultTestOrcidClient.getDefaultClient();
  62 + }
  63 +
  64 + Date date = new Date();
  65 + _log.info("Date: " + dateFormat.format(date));
  66 + _log.info("Version: " + PTCRISync.class.getPackage().getImplementationVersion());
  67 + try {
  68 + // Test cycle - perform several configured tests
  69 + ActivitiesSummary activitiesSummary = ActivitiesSummaryTestPrepare.loadFromResources();
  70 + for (int i = 0; i < nTestRecords.length; i++) {
  71 + List<BigInteger> putCodesList = ORCIDHelper.getWorkSummaryPutCodes(activitiesSummary);
  72 +
  73 + int nRecords = Integer.parseInt(nTestRecords[i].trim());
  74 + int nPutCodes = nRecords;
  75 +
  76 + //Limit the number of load
  77 + if (nPutCodes > putCodesList.size() ) {
  78 + nPutCodes = putCodesList.size();
  79 + }
  80 +
  81 + List<Work> localWorks = WorkTestPrepare.loadLocalWorksFromResources(putCodesList.subList(0, nPutCodes));
  82 +
  83 + int numberOfLocalWorks = localWorks.size();
  84 + int numberOfExportedWorks = 0;
  85 +
  86 + // Check the number of exported works
  87 + ORCIDHelper helper = new ORCIDHelper(orcidClient);
  88 +
  89 + _log.info("Deleting all existing works");
  90 + helper.deleteAllSourcedWorks();
  91 +
  92 + try {
  93 +
  94 + long start = System.nanoTime();
  95 + _log.info("Start testing: " + orcidId);
  96 + PTCRISync.export(orcidClient, localWorks, progressHandler);
  97 + _log.info("End testing: " + (((double) (System.nanoTime() - start)) / 1E9) + " sec");
  98 +
  99 + List<WorkSummary> exportedWorkSummary = helper.getSourcedWorkSummaries();
  100 + numberOfExportedWorks = exportedWorkSummary.size();
  101 +
  102 + _log.info("Localworks to export: " + numberOfLocalWorks);
  103 + _log.info("Exported content: " + numberOfExportedWorks);
  104 + testResult = true;
  105 +
  106 + assertTrue("Number of exported that should match: " +
  107 + numberOfLocalWorks,
  108 + (numberOfExportedWorks == numberOfLocalWorks));
  109 + _log.info("[exportTimetoOrcid] " + "PASSED");
  110 + } catch (OrcidClientException orcidException) {
  111 + testResult = false;
  112 + _log.fatal(orcidException.getDeveloperMessage());
  113 + _log.error("[exportTimetoOrcid] " + "FAILED");
  114 + } catch (AssertionError e) {
  115 + testResult = false;
  116 + _log.error("[exportTimetoOrcid] " + e.getMessage());
  117 + } catch (Exception e) {
  118 + testResult = false;
  119 + e.printStackTrace();
  120 + } finally {
  121 + System.out.println(progressHandler.getReport());
  122 + assertTrue("Export to new profile", testResult);
  123 + }
  124 + } // For loop end
  125 + } catch (JAXBException e) {
  126 + // TODO Auto-generated catch block
  127 + e.printStackTrace();
  128 + } catch (OrcidClientException e1) {
  129 + e1.printStackTrace();
  130 + }
  131 +
  132 + }
  133 +
  134 + /**
  135 + * Empty Local APP and we want to measure the time of the import M works
  136 + * process from ORCID to an empty local app
  137 + */
  138 + @Test
  139 + public void importTimetoOrcid() {
  140 + boolean testResult = false;
  141 + String orcidId = properties.getProperty("orcid_id");
  142 + boolean isTestClient = Boolean.parseBoolean(properties.getProperty("test_dummy_client"));
  143 + String[] nTestRecords = properties.getProperty("test_performance_limits").split(",");
  144 +
  145 + _log.info("Prepare data for: " + orcidId);
  146 + ORCIDClient orcidClient;
  147 + // To test the With and W/O the API
  148 + if (isTestClient) {
  149 + orcidClient = ORCIDClientDummyImpl.getDefaultClientInstance();
  150 + } else {
  151 + orcidClient = DefaultTestOrcidClient.getDefaultClient();
  152 + }
  153 +
  154 + // Empty list of local works
  155 + List<Work> localWorks = new LinkedList<Work>();
  156 +
  157 + int numberOfWorks = 0;
  158 + int numberOfImportedWorks = 0;
  159 +
  160 + try {
  161 + ActivitiesSummary activitiesSummary = ActivitiesSummaryTestPrepare.loadFromResources();
  162 + List<BigInteger> putCodesList = ORCIDHelper.getWorkSummaryPutCodes(activitiesSummary);
  163 +
  164 + ORCIDHelper helper = new ORCIDHelper(orcidClient);
  165 +
  166 + // Test cycle - perform several configured tests
  167 + for (int i = 0; i < nTestRecords.length; i++) {
  168 + int nRecords = Integer.parseInt(nTestRecords[i].trim());
  169 + int nPutCodes = nRecords;
  170 +
  171 + //Limit the number of load
  172 + if (nPutCodes > putCodesList.size() ) {
  173 + nPutCodes = putCodesList.size();
  174 + }
  175 +
  176 + //Will delete existing and import new works to ORCID, it will take a while
  177 + List<Work> uploadedWorks = WorkTestPrepare.createAndLoadWorks(orcidClient,putCodesList.subList(0, nPutCodes));
  178 + numberOfWorks = uploadedWorks.size();
  179 +
  180 + long start = System.nanoTime();
  181 + _log.info("Start testing: " + orcidId);
  182 + PTCRISync.importWorks(orcidClient, localWorks, progressHandler);
  183 + _log.info("End testing: " + (((double) (System.nanoTime() - start)) / 1E9) + " sec");
  184 + List<WorkSummary> importedWorkSummary = helper.getSourcedWorkSummaries();
  185 + numberOfImportedWorks = importedWorkSummary.size();
  186 +
  187 + _log.info("Import works: " + numberOfWorks);
  188 + _log.info("Number of imported works: " + numberOfImportedWorks);
  189 + testResult = true;
  190 +
  191 + assertTrue("Number of exported that should match: " + numberOfWorks,
  192 + (numberOfImportedWorks == numberOfWorks));
  193 + _log.info("[importTimetoOrcid] " + "PASSED");
  194 +
  195 + } //End of For loop
  196 +
  197 + } catch (OrcidClientException orcidException) {
  198 + _log.fatal(orcidException.getDeveloperMessage());
  199 + _log.error("[importTimetoOrcid] " + "FAILED");
  200 + } catch (AssertionError e) {
  201 + testResult = false;
  202 + _log.error("[importTimetoOrcid] " + e.getMessage());
  203 + } catch (Exception e) {
  204 + e.printStackTrace();
  205 + } finally {
  206 + System.out.println(progressHandler.getReport());
  207 + assertTrue("Import new works", testResult);
  208 + }
  209 + }
  210 +
  211 + public static void main(String[] args) {
  212 + Result result = JUnitCore.runClasses(PerformanceTest.class);
  213 + for (Failure failure : result.getFailures()) {
  214 + System.out.println(failure.toString());
  215 + }
  216 + }
  217 +}
... ...
ptcrisync-test/src/main/java/pt/ptcris/test/dummy/ORCIDClientDummyImpl.java
... ... @@ -0,0 +1,168 @@
  1 +package pt.ptcris.test.dummy;
  2 +
  3 +import org.um.dsi.gavea.orcid.client.exception.OrcidClientException;
  4 +
  5 +import java.math.BigInteger;
  6 +import java.util.HashMap;
  7 +import java.util.Iterator;
  8 +import java.util.LinkedList;
  9 +import java.util.List;
  10 +import java.util.Map;
  11 +import java.util.Properties;
  12 +import java.util.Random;
  13 +
  14 +import javax.xml.bind.JAXBException;
  15 +
  16 +import org.um.dsi.gavea.orcid.client.OrcidAccessToken;
  17 +import org.um.dsi.gavea.orcid.model.work.ExternalIdentifier;
  18 +import org.um.dsi.gavea.orcid.model.work.Work;
  19 +import org.um.dsi.gavea.orcid.model.work.WorkExternalIdentifiers;
  20 +import org.um.dsi.gavea.orcid.model.work.WorkSummary;
  21 +
  22 +import pt.ptcris.ORCIDClient;
  23 +import pt.ptcris.ORCIDHelper;
  24 +import pt.ptcris.test.preparation.ActivitiesSummaryTestPrepare;
  25 +import pt.ptcris.test.preparation.WorkTestPrepare;
  26 +import pt.ptcris.test.properties.TestPropertiesLoader;
  27 +
  28 +import org.um.dsi.gavea.orcid.model.activities.ActivitiesSummary;
  29 +import org.um.dsi.gavea.orcid.model.activities.ActivitiesSummary.Works;
  30 +import org.um.dsi.gavea.orcid.model.activities.Identifier;
  31 +import org.um.dsi.gavea.orcid.model.activities.Identifiers;
  32 +import org.um.dsi.gavea.orcid.model.activities.WorkGroup;
  33 +import org.um.dsi.gavea.orcid.model.common.ClientId;
  34 +import org.um.dsi.gavea.orcid.model.common.OrcidId;
  35 +import org.um.dsi.gavea.orcid.model.common.SourceName;
  36 +import org.um.dsi.gavea.orcid.model.common.SourceType;
  37 +
  38 +/**
  39 + * // TODO remove temporary client // ORCIDClient tempClient = //
  40 + * ORCIDClientDummyImpl.getDefaultClientInstance(); //
  41 + * PTCRISync.export(tempClient, localWorks, progressHandler);
  42 + *
  43 + * @author pgraca
  44 + *
  45 + */
  46 +public class ORCIDClientDummyImpl implements ORCIDClient {
  47 +
  48 + private OrcidAccessToken orcidToken;
  49 + private String clientId;
  50 + private static ORCIDClient orcidClient = null;
  51 + private HashMap<BigInteger, Work> dummyWorks = new HashMap<BigInteger, Work>();
  52 +
  53 + public ORCIDClientDummyImpl(String loginUri, String apiUri, String clientId, String clientSecret,
  54 + String redirectUri, OrcidAccessToken orcidToken) {
  55 + this.orcidToken = orcidToken;
  56 + this.clientId = clientId;
  57 + }
  58 +
  59 + public static ORCIDClient getDefaultClientInstance() {
  60 + if (orcidClient == null) {
  61 +
  62 + Properties properties = TestPropertiesLoader.getInstance().getProperties();
  63 +
  64 + OrcidAccessToken orcidToken = new OrcidAccessToken();
  65 +
  66 + orcidToken.setAccess_token(properties.getProperty("orcid_access_token"));
  67 + orcidToken.setOrcid(properties.getProperty("orcid_id"));
  68 +
  69 + orcidClient = new ORCIDClientDummyImpl(properties.getProperty("orcid_login_uri"),
  70 + properties.getProperty("orcid_api_uri"), properties.getProperty("orcid_client_id"),
  71 + properties.getProperty("orcid_client_secret"), properties.getProperty("orcid_redirect_uri"),
  72 + orcidToken);
  73 + }
  74 +
  75 + return orcidClient;
  76 + }
  77 +
  78 + public Work getWork(BigInteger putCode) throws OrcidClientException {
  79 + if (dummyWorks.containsKey(putCode)) {
  80 + return dummyWorks.get(putCode);
  81 + } else {
  82 + List<BigInteger> putCodesList = new LinkedList<>();
  83 + putCodesList.add(putCode);
  84 + try {
  85 + List<Work> workList = WorkTestPrepare.loadLocalWorksFromResources(putCodesList);
  86 + dummyWorks.put(putCode,workList.get(0));
  87 + return workList.get(0);
  88 + } catch (JAXBException e) {
  89 + // TODO Auto-generated catch block
  90 + e.printStackTrace();
  91 + }
  92 + }