Commit b678b06e6ff390d42749dbb26ae0f361621c288d

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

Refactoring and introducing records test limit

ptcrisync-test/resources/test.properties.default
... ... @@ -29,3 +29,7 @@ orcid_access_token = 00000000-0000-0000-0000-000000000000
29 29 # Resources base path
30 30 source_orcid_id = 0000-0000-0000-0000
31 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
32 36 \ No newline at end of file
... ...
ptcrisync-test/src/pt/ptcris/test/ExportTest.java
... ... @@ -14,6 +14,7 @@ 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 16 import org.um.dsi.gavea.orcid.model.work.Work;
  17 +import org.um.dsi.gavea.orcid.model.work.WorkSummary;
17 18  
18 19 import pt.ptcris.ORCIDClient;
19 20 import pt.ptcris.ORCIDClientImpl;
... ... @@ -21,8 +22,9 @@ import pt.ptcris.ORCIDHelper;
21 22 import pt.ptcris.PTCRISync;
22 23 import pt.ptcris.test.dummy.ORCIDClientDummyImpl;
23 24 import pt.ptcris.test.dummy.ProgressHandlerDummyImpl;
24   -import pt.ptcris.test.preparation.ActivitiesSummaryTestPreparation;
25   -import pt.ptcris.test.preparation.WorkTestPreparation;
  25 +import pt.ptcris.test.preparation.ActivitiesSummaryTestPrepare;
  26 +import pt.ptcris.test.preparation.DefaultTestOrcidClient;
  27 +import pt.ptcris.test.preparation.WorkTestPrepare;
26 28 import pt.ptcris.test.properties.TestPropertiesLoader;
27 29  
28 30 import javax.xml.bind.JAXBException;
... ... @@ -30,12 +32,11 @@ import javax.xml.bind.JAXBException;
30 32 import org.apache.logging.log4j.LogManager;
31 33 import org.apache.logging.log4j.Logger;
32 34  
33   -
34 35 public class ExportTest {
35 36 private final Properties properties = TestPropertiesLoader.getInstance().getProperties();
36 37 private static final Logger _log = LogManager.getLogger(ExportTest.class);
37 38 private static ProgressHandlerDummyImpl progressHandler = new ProgressHandlerDummyImpl();
38   -
  39 +
39 40 /**
40 41 * Empty ORCID profile and we want to export works from a local app
41 42 */
... ... @@ -43,41 +44,55 @@ public class ExportTest {
43 44 public void exportToNewProfile() {
44 45 boolean testResult = false;
45 46  
46   - OrcidAccessToken orcidToken = new OrcidAccessToken();
47 47 String orcidId = properties.getProperty("orcid_id");
48   -
49 48 String sourceOrcidId = properties.getProperty("source_orcid_id");
50 49 String resources_path = properties.getProperty("source_resources_path");
  50 + boolean ignoreExportLimit = Boolean.getBoolean(properties.getProperty("test_ignore_limits"));
51 51  
52   - orcidToken.setAccess_token(properties.getProperty("orcid_access_token"));
53   - orcidToken.setOrcid(orcidId);
  52 + // Load local works - test using local XML files
  53 + String filename = resources_path + "/" + sourceOrcidId + "_activities.xml";
54 54  
55   - //Load local works - test using local XML files
56   - String filename = resources_path + "/" + sourceOrcidId + "_activities.xml";
57   -
58 55 try {
59   - ORCIDClientImpl orcidClient = new ORCIDClientImpl(properties.getProperty("orcid_login_uri"),
60   - properties.getProperty("orcid_api_uri"),
61   - properties.getProperty("orcid_client_id"),
62   - properties.getProperty("orcid_client_secret"),
63   - properties.getProperty("orcid_redirect_uri"), orcidToken);
64   -
  56 + _log.info("Prepare data for: " + orcidId);
  57 + ORCIDClientImpl orcidClient = (ORCIDClientImpl) DefaultTestOrcidClient.getDefaultClient();
  58 +
65 59 _log.debug("Loading resources from filesystem: " + filename);
66   - ActivitiesSummary activitiesSummary = ActivitiesSummaryTestPreparation.loadActivitiesSummaryFromFile(filename);
  60 + ActivitiesSummary activitiesSummary = ActivitiesSummaryTestPrepare.loadActivitiesSummaryFromFile(filename);
67 61 List<String> putCodesList = ORCIDHelper.getWorkSummaryPutCodes(activitiesSummary);
  62 +
  63 + if (!ignoreExportLimit) {
  64 + int nRecords = Integer.parseInt(properties.getProperty("test_export_limit"));
  65 + //Limit the numbers to export
  66 + int putCodesListSize = putCodesList.size();
  67 + if ( putCodesListSize > nRecords ) {
  68 + putCodesList.subList(nRecords, putCodesListSize).clear();
  69 + }
  70 + }
68 71 _log.debug("Loading works from filesystem: " + resources_path);
69   - List<Work> localWorks = WorkTestPreparation.loadLocalTestWorks (putCodesList, resources_path);
  72 + List<Work> localWorks = WorkTestPrepare.loadLocalTestWorks(putCodesList, resources_path);
  73 +
  74 + int numberOfLocalWorks = localWorks.size();
  75 + int numberOfExportedWorks = 0;
70 76  
71 77 try {
72   - //TODO remove temporary client
73   - //ORCIDClient tempClient = ORCIDClientDummyImpl.getDefaultClientInstance();
74   -
75   - _log.info("Exporting content");
76   - //PTCRISync.export(tempClient, localWorks, progressHandler);
77   -
  78 + // TODO remove temporary client
  79 + // ORCIDClient tempClient =
  80 + // ORCIDClientDummyImpl.getDefaultClientInstance();
  81 + // PTCRISync.export(tempClient, localWorks, progressHandler);
  82 + _log.info("Localworks to export: " + numberOfLocalWorks);
  83 +
78 84 PTCRISync.export(orcidClient, localWorks, progressHandler);
79   - _log.info("Exported content");
  85 +
  86 + // Check the number of exported works
  87 + ORCIDHelper helper = new ORCIDHelper(orcidClient);
  88 + List<WorkSummary> exportedWorkSummary = helper.getSourcedWorkSummaries();
  89 + numberOfExportedWorks = exportedWorkSummary.size();
  90 +
  91 + _log.info("Exported content: " + numberOfExportedWorks);
80 92 testResult = true;
  93 +
  94 + assertTrue("Number of exported that should match: " + numberOfLocalWorks,
  95 + (numberOfExportedWorks == numberOfLocalWorks));
81 96 _log.info("[exportToNewProfile] " + "PASSED");
82 97 } catch (OrcidClientException orcidException) {
83 98  
... ... @@ -87,7 +102,80 @@ public class ExportTest {
87 102 e.printStackTrace();
88 103 } finally {
89 104  
90   - assertTrue ("Export to new profile", testResult);
  105 + assertTrue("Export to new profile", testResult);
  106 + }
  107 +
  108 + } catch (JAXBException e) {
  109 + // TODO Auto-generated catch block
  110 + e.printStackTrace();
  111 + }
  112 + }
  113 +
  114 + /**
  115 + * Filled ORCID profile and we want to export works from local app
  116 + */
  117 + @Test
  118 + public void exportToProfile() {
  119 + boolean testResult = false;
  120 +
  121 + String orcidId = properties.getProperty("orcid_id");
  122 + String sourceOrcidId = properties.getProperty("source_orcid_id");
  123 + String resources_path = properties.getProperty("source_resources_path");
  124 + boolean ignoreExportLimit = Boolean.getBoolean(properties.getProperty("test_ignore_limits"));
  125 +
  126 + // Load local works - test using local XML files
  127 + String filename = resources_path + "/" + sourceOrcidId + "_activities.xml";
  128 +
  129 +
  130 + try {
  131 + _log.info("Prepare data for: " + orcidId);
  132 + ORCIDClientImpl orcidClient = (ORCIDClientImpl) DefaultTestOrcidClient.getDefaultClient();
  133 +
  134 + _log.debug("Loading resources from filesystem: " + filename);
  135 + ActivitiesSummary activitiesSummary = ActivitiesSummaryTestPrepare.loadActivitiesSummaryFromFile(filename);
  136 + List<String> putCodesList = ORCIDHelper.getWorkSummaryPutCodes(activitiesSummary);
  137 +
  138 + if (!ignoreExportLimit) {
  139 + int nRecords = (Integer.parseInt(properties.getProperty("test_export_limit")) + 1);
  140 + //Limit the numbers to export
  141 + int putCodesListSize = putCodesList.size();
  142 + if ( putCodesListSize > nRecords ) {
  143 + putCodesList.subList(nRecords, putCodesListSize).clear();
  144 + }
  145 + }
  146 +
  147 + _log.debug("Loading works from filesystem: " + resources_path);
  148 + List<Work> localWorks = WorkTestPrepare.loadLocalTestWorks(putCodesList, resources_path);
  149 +
  150 +
  151 + int numberOfLocalWorks = localWorks.size();
  152 + int numberOfExportedWorks = 0;
  153 +
  154 + try {
  155 + _log.info("Localworks to export: " + numberOfLocalWorks);
  156 +
  157 + PTCRISync.export(orcidClient, localWorks, progressHandler);
  158 +
  159 + // Check the number of exported works
  160 + ORCIDHelper helper = new ORCIDHelper(orcidClient);
  161 + List<WorkSummary> exportedWorkSummary = helper.getSourcedWorkSummaries();
  162 + numberOfExportedWorks = exportedWorkSummary.size();
  163 +
  164 + _log.info("Exported content: " + numberOfExportedWorks);
  165 + testResult = true;
  166 +
  167 + assertTrue("Number of exported that should match: " + numberOfLocalWorks,
  168 + (numberOfExportedWorks == numberOfLocalWorks));
  169 + _log.info("[exportToProfile] " + "PASSED");
  170 + } catch (OrcidClientException orcidException) {
  171 +
  172 + orcidException.printStackTrace();
  173 + _log.error("[exportToProfile] " + "FAILED");
  174 + } catch (Exception e) {
  175 + e.printStackTrace();
  176 + } finally {
  177 +
  178 + assertTrue("Export to profile", testResult);
91 179 }
92 180  
93 181 } catch (JAXBException e) {
... ... @@ -95,12 +183,11 @@ public class ExportTest {
95 183 e.printStackTrace();
96 184 }
97 185 }
98   -
99   -
  186 +
100 187 public static void main(String[] args) {
101   - Result result = JUnitCore.runClasses(ExportTest.class);
102   - for (Failure failure : result.getFailures()) {
103   - System.out.println(failure.toString());
104   - }
105   - }
  188 + Result result = JUnitCore.runClasses(ExportTest.class);
  189 + for (Failure failure : result.getFailures()) {
  190 + System.out.println(failure.toString());
  191 + }
  192 + }
106 193 }
... ...
ptcrisync-test/src/pt/ptcris/test/preparation/AbstractTestPreparation.java
... ... @@ -1,65 +0,0 @@
1   -package pt.ptcris.test.preparation;
2   -
3   -import java.io.File;
4   -import java.lang.reflect.ParameterizedType;
5   -
6   -import javax.xml.bind.JAXBContext;
7   -import javax.xml.bind.JAXBException;
8   -import javax.xml.bind.Marshaller;
9   -import javax.xml.bind.Unmarshaller;
10   -
11   -/**
12   - * @author pgraca
13   - *
14   - * @param <T>
15   - */
16   -public abstract class AbstractTestPreparation<T> {
17   - // T stands for "Type"
18   - final Class<T> typeParameterClass;
19   -
20   - public AbstractTestPreparation(Class<T> typeParameterClass) {
21   - this.typeParameterClass = typeParameterClass;
22   - }
23   -
24   -
25   - @SuppressWarnings({ "unchecked", "rawtypes" })
26   - public AbstractTestPreparation() {
27   - this.typeParameterClass = ((Class) ((ParameterizedType) getClass()
28   - .getGenericSuperclass()).getActualTypeArguments()[0]);
29   - }
30   -
31   -
32   - /**
33   - * Method for unmarshaling an entity from a XML file
34   - * @param filename
35   - * @return
36   - * @throws JAXBException
37   - */
38   - @SuppressWarnings("unchecked")
39   - public T load (String filename) throws JAXBException {
40   - JAXBContext context = JAXBContext.newInstance(typeParameterClass);
41   - Unmarshaller un = context.createUnmarshaller();
42   - return (T) un.unmarshal(new File(filename));
43   - }
44   -
45   - /**
46   - * Method for marshaling an entity into a XML file
47   - * @param entity
48   - * @param filename
49   - * @throws JAXBException
50   - */
51   - public void save (T entity, String filename) throws JAXBException {
52   -
53   - JAXBContext context = JAXBContext.newInstance(typeParameterClass);
54   - Marshaller m = context.createMarshaller();
55   - //for pretty-print XML in JAXB
56   - m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
57   -
58   - // Write to System.out for debugging
59   - // m.marshal(emp, System.out);
60   -
61   - // Write to File
62   - m.marshal(entity, new File(filename));
63   -
64   - }
65   -}
ptcrisync-test/src/pt/ptcris/test/preparation/AbstractTestSerializer.java
... ... @@ -0,0 +1,65 @@
  1 +package pt.ptcris.test.preparation;
  2 +
  3 +import java.io.File;
  4 +import java.lang.reflect.ParameterizedType;
  5 +
  6 +import javax.xml.bind.JAXBContext;
  7 +import javax.xml.bind.JAXBException;
  8 +import javax.xml.bind.Marshaller;
  9 +import javax.xml.bind.Unmarshaller;
  10 +
  11 +/**
  12 + * @author pgraca
  13 + *
  14 + * @param <T>
  15 + */
  16 +public abstract class AbstractTestSerializer<T> {
  17 + // T stands for "Type"
  18 + final Class<T> typeParameterClass;
  19 +
  20 + public AbstractTestSerializer(Class<T> typeParameterClass) {
  21 + this.typeParameterClass = typeParameterClass;
  22 + }
  23 +
  24 +
  25 + @SuppressWarnings({ "unchecked", "rawtypes" })
  26 + public AbstractTestSerializer() {
  27 + this.typeParameterClass = ((Class) ((ParameterizedType) getClass()
  28 + .getGenericSuperclass()).getActualTypeArguments()[0]);
  29 + }
  30 +
  31 +
  32 + /**
  33 + * Method for unmarshaling an entity from a XML file
  34 + * @param filename
  35 + * @return
  36 + * @throws JAXBException
  37 + */
  38 + @SuppressWarnings("unchecked")
  39 + public T load (String filename) throws JAXBException {
  40 + JAXBContext context = JAXBContext.newInstance(typeParameterClass);
  41 + Unmarshaller un = context.createUnmarshaller();
  42 + return (T) un.unmarshal(new File(filename));
  43 + }
  44 +
  45 + /**
  46 + * Method for marshaling an entity into a XML file
  47 + * @param entity
  48 + * @param filename
  49 + * @throws JAXBException
  50 + */
  51 + public void save (T entity, String filename) throws JAXBException {
  52 +
  53 + JAXBContext context = JAXBContext.newInstance(typeParameterClass);
  54 + Marshaller m = context.createMarshaller();
  55 + //for pretty-print XML in JAXB
  56 + m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
  57 +
  58 + // Write to System.out for debugging
  59 + // m.marshal(emp, System.out);
  60 +
  61 + // Write to File
  62 + m.marshal(entity, new File(filename));
  63 +
  64 + }
  65 +}
... ...
ptcrisync-test/src/pt/ptcris/test/preparation/ActivitiesSummaryTestPreparation.java
... ... @@ -1,43 +0,0 @@
1   -package pt.ptcris.test.preparation;
2   -
3   -import java.io.File;
4   -import java.util.LinkedList;
5   -import java.util.List;
6   -import java.util.Properties;
7   -
8   -import javax.xml.bind.JAXBException;
9   -
10   -import org.apache.logging.log4j.LogManager;
11   -import org.apache.logging.log4j.Logger;
12   -import org.um.dsi.gavea.orcid.model.activities.ActivitiesSummary;
13   -import org.um.dsi.gavea.orcid.model.activities.WorkGroup;
14   -import org.um.dsi.gavea.orcid.model.work.WorkSummary;
15   -
16   -import pt.ptcris.test.properties.TestPropertiesLoader;
17   -
18   -
19   -public class ActivitiesSummaryTestPreparation {
20   - private static final Logger _log = LogManager.getLogger(ActivitiesSummaryTestPreparation.class);
21   -
22   - public static void init (String orcid) throws SecurityException {
23   - Properties properties = TestPropertiesLoader.getInstance("public-api-test.properties").getProperties();
24   -
25   - String path = properties.getProperty("source_resources_path");
26   - _log.info("Try creating directory: " + path);
27   - //Try to create the directory
28   - new File(path).mkdirs();
29   - }
30   -
31   - public static ActivitiesSummary loadActivitiesSummaryFromFile (String filename) throws JAXBException {
32   - AbstractTestPreparation<ActivitiesSummary> prepareActivitiesSummary = new AbstractTestPreparation<ActivitiesSummary>(){};
33   -
34   - ActivitiesSummary activitiesSummary = prepareActivitiesSummary.load(filename);
35   - return activitiesSummary;
36   - }
37   -
38   - public static void activitiesSummaryToXMLFile (ActivitiesSummary activitiesSummary, String filename) throws JAXBException {
39   - AbstractTestPreparation<ActivitiesSummary> prepareActivitiesSummary = new AbstractTestPreparation<ActivitiesSummary>(){};
40   - _log.info("saving: " + filename);
41   - prepareActivitiesSummary.save(activitiesSummary, filename);
42   - }
43   -}
ptcrisync-test/src/pt/ptcris/test/preparation/ActivitiesSummaryTestPrepare.java
... ... @@ -0,0 +1,43 @@
  1 +package pt.ptcris.test.preparation;
  2 +
  3 +import java.io.File;
  4 +import java.util.LinkedList;
  5 +import java.util.List;
  6 +import java.util.Properties;
  7 +
  8 +import javax.xml.bind.JAXBException;
  9 +
  10 +import org.apache.logging.log4j.LogManager;
  11 +import org.apache.logging.log4j.Logger;
  12 +import org.um.dsi.gavea.orcid.model.activities.ActivitiesSummary;
  13 +import org.um.dsi.gavea.orcid.model.activities.WorkGroup;
  14 +import org.um.dsi.gavea.orcid.model.work.WorkSummary;
  15 +
  16 +import pt.ptcris.test.properties.TestPropertiesLoader;
  17 +
  18 +
  19 +public class ActivitiesSummaryTestPrepare {
  20 + private static final Logger _log = LogManager.getLogger(ActivitiesSummaryTestPrepare.class);
  21 +
  22 + public static void init (String orcid) throws SecurityException {
  23 + Properties properties = TestPropertiesLoader.getInstance("public-api-test.properties").getProperties();
  24 +
  25 + String path = properties.getProperty("source_resources_path");
  26 + _log.info("Try creating directory: " + path);
  27 + //Try to create the directory
  28 + new File(path).mkdirs();
  29 + }
  30 +
  31 + public static ActivitiesSummary loadActivitiesSummaryFromFile (String filename) throws JAXBException {
  32 + AbstractTestSerializer<ActivitiesSummary> prepareActivitiesSummary = new AbstractTestSerializer<ActivitiesSummary>(){};
  33 +
  34 + ActivitiesSummary activitiesSummary = prepareActivitiesSummary.load(filename);
  35 + return activitiesSummary;
  36 + }
  37 +
  38 + public static void activitiesSummaryToXMLFile (ActivitiesSummary activitiesSummary, String filename) throws JAXBException {
  39 + AbstractTestSerializer<ActivitiesSummary> prepareActivitiesSummary = new AbstractTestSerializer<ActivitiesSummary>(){};
  40 + _log.info("saving: " + filename);
  41 + prepareActivitiesSummary.save(activitiesSummary, filename);
  42 + }
  43 +}
... ...
ptcrisync-test/src/pt/ptcris/test/preparation/WorkTestPreparation.java
... ... @@ -1,129 +0,0 @@
1   -package pt.ptcris.test.preparation;
2   -
3   -import java.math.BigInteger;
4   -import java.util.LinkedList;
5   -import java.util.List;
6   -import java.util.Properties;
7   -
8   -import javax.xml.bind.JAXBException;
9   -import org.apache.logging.log4j.LogManager;
10   -import org.apache.logging.log4j.Logger;
11   -
12   -
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.ORCIDClientImpl;
19   -import pt.ptcris.ORCIDHelper;
20   -import pt.ptcris.test.properties.TestPropertiesLoader;
21   -
22   -public class WorkTestPreparation {
23   - private static final Logger _log = LogManager.getLogger(WorkTestPreparation.class);
24   -
25   - public static void init (String orcid) throws SecurityException {
26   - ActivitiesSummaryTestPreparation.init(orcid);
27   - }
28   -
29   -
30   - public static Work loadWorkFromFile (String filename) throws JAXBException {
31   - AbstractTestPreparation<Work> preparedWork = new AbstractTestPreparation<Work>(){};
32   -
33   - Work work = preparedWork.load(filename);
34   - return work;
35   - }
36   -
37   - public static List<Work> loadWorksFromResources (List<String> putCodesList, String resourcesPath) throws JAXBException {
38   - List<Work> workList = new LinkedList<Work>();
39   -
40   - Work work;
41   - for(String putCode:putCodesList) {
42   - //TODO: FIX the file structure shouln't be here
43   - work = WorkTestPreparation.loadWorkFromFile(resourcesPath + "/" + putCode + ".xml");
44   - workList.add(work);
45   - }
46   -
47   - return workList;
48   - }
49   -
50   - /**
51   - * Clears putCodes from a work list
52   - *
53   - * @return a list of works
54   - * @throws JAXBException
55   - */
56   - public static List<Work> loadLocalTestWorks (List<String> putCodesList, String resources_path) throws JAXBException, NullPointerException {
57   - Work work;
58   -
59   - List<Work> worksList = loadWorksFromResources (putCodesList, resources_path);
60   -
61   - for (int index=0;index<worksList.size();index++) {
62   - work = worksList.get(index);
63   - work.setPutCode(null);
64   - }
65   -
66   - return worksList;
67   - }
68   -
69   -
70   - public static void workToXMLFile (Work work, String filename) throws JAXBException {
71   - AbstractTestPreparation<Work> preparedWork = new AbstractTestPreparation<Work>(){};
72   -
73   - _log.info("saving: " + filename);
74   - preparedWork.save(work, filename);
75   - }
76   -
77   -
78   - /**
79   - * This main class will download and save ORCID xml data prior testing
80   - * @param args
81   - */
82   - public static void main(String[] args) {
83   - Properties properties = TestPropertiesLoader.getInstance("public-api-test.properties").getProperties();
84   -
85   - String orcidId = properties.getProperty("source_orcid_id");
86   - String resources_path = properties.getProperty("source_resources_path");
87   -
88   - OrcidAccessToken orcidToken = new OrcidAccessToken();
89   - orcidToken.setAccess_token(properties.getProperty("orcid_access_token"));
90   - orcidToken.setOrcid(orcidId);
91   -
92   - try {
93   -
94   - WorkTestPreparation.init(orcidId);
95   -
96   - ORCIDClientImpl orcidClient = new ORCIDClientImpl(properties.getProperty("orcid_login_uri"),
97   - properties.getProperty("orcid_api_uri"),
98   - properties.getProperty("orcid_client_id"),
99   - properties.getProperty("orcid_client_secret"),
100   - properties.getProperty("orcid_redirect_uri"), orcidToken);
101   -
102   - ORCIDHelper orcidHelper = new ORCIDHelper(orcidClient);
103   - ActivitiesSummary activitiesSummary = orcidHelper.getActivitiesSummary();
104   -
105   - List<String> putcodesList = ORCIDHelper.getWorkSummaryPutCodes(activitiesSummary);
106   -
107   - String activitiesSummaryPath = resources_path + "/" + orcidId + "_activities.xml";
108   - ActivitiesSummaryTestPreparation.activitiesSummaryToXMLFile(activitiesSummary,activitiesSummaryPath);
109   -
110   - Work work;
111   - String workPath;
112   - for (String putCode : putcodesList) {
113   - work = orcidClient.getWork(putCode);
114   -
115   - workPath = resources_path + "/" + putCode + ".xml";
116   - WorkTestPreparation.workToXMLFile(work,workPath);
117   - }
118   -
119   -
120   - } catch (OrcidClientException e) {
121   - // TODO Auto-generated catch block
122   - e.printStackTrace();
123   - } catch (JAXBException e) {
124   - // TODO Auto-generated catch block
125   - e.printStackTrace();
126   - }
127   -
128   - }
129   -}
ptcrisync-test/src/pt/ptcris/test/preparation/WorkTestPrepare.java
... ... @@ -0,0 +1,129 @@
  1 +package pt.ptcris.test.preparation;
  2 +
  3 +import java.util.LinkedList;
  4 +import java.util.List;
  5 +import java.util.Properties;
  6 +
  7 +import javax.xml.bind.JAXBException;
  8 +import org.apache.logging.log4j.LogManager;
  9 +import org.apache.logging.log4j.Logger;
  10 +
  11 +
  12 +import org.um.dsi.gavea.orcid.client.OrcidAccessToken;
  13 +import org.um.dsi.gavea.orcid.client.exception.OrcidClientException;
  14 +import org.um.dsi.gavea.orcid.model.activities.ActivitiesSummary;
  15 +import org.um.dsi.gavea.orcid.model.work.Work;
  16 +
  17 +import pt.ptcris.ORCIDClientImpl;
  18 +import pt.ptcris.ORCIDHelper;
  19 +import pt.ptcris.test.properties.TestPropertiesLoader;
  20 +
  21 +public class WorkTestPrepare {
  22 + private static final Logger _log = LogManager.getLogger(WorkTestPrepare.class);
  23 +
  24 + public static void init (String orcid) throws SecurityException {
  25 + ActivitiesSummaryTestPrepare.init(orcid);
  26 + }
  27 +
  28 +
  29 + public static Work loadWorkFromFile (String filename) throws JAXBException {
  30 + AbstractTestSerializer<Work> preparedWork = new AbstractTestSerializer<Work>(){};
  31 +
  32 + Work work = preparedWork.load(filename);
  33 + return work;
  34 + }
  35 +
  36 + public static List<Work> loadWorksFromResources (List<String> putCodesList, String resourcesPath) throws JAXBException {
  37 + List<Work> workList = new LinkedList<Work>();
  38 +
  39 + Work work;
  40 + _log.debug("Loading putcodes from filesystem: " + resourcesPath );
  41 + for(String putCode:putCodesList) {
  42 + //TODO: FIX the file structure shouln't be here
  43 + work = WorkTestPrepare.loadWorkFromFile(resourcesPath + "/" + putCode + ".xml");
  44 + workList.add(work);
  45 + }
  46 +
  47 + return workList;
  48 + }
  49 +
  50 + /**
  51 + * Clears putCodes from a work list
  52 + *
  53 + * @return a list of works
  54 + * @throws JAXBException
  55 + */
  56 + public static List<Work> loadLocalTestWorks (List<String> putCodesList, String resources_path) throws JAXBException, NullPointerException {
  57 + Work work;
  58 +
  59 + List<Work> worksList = loadWorksFromResources (putCodesList, resources_path);
  60 +
  61 + for (int index=0;index<worksList.size();index++) {
  62 + work = worksList.get(index);
  63 + work.setPutCode(null);
  64 + }
  65 +
  66 + return worksList;
  67 + }
  68 +
  69 +
  70 + public static void workToXMLFile (Work work, String filename) throws JAXBException {
  71 + AbstractTestSerializer<Work> preparedWork = new AbstractTestSerializer<Work>(){};
  72 +
  73 + _log.info("saving: " + filename);
  74 + preparedWork.save(work, filename);
  75 + }
  76 +
  77 +
  78 + /**
  79 + * This main class will download and save ORCID xml data prior testing
  80 + * @param args
  81 + */
  82 + public static void main(String[] args) {
  83 + Properties properties = TestPropertiesLoader.getInstance("public-api-test.properties").getProperties();
  84 +
  85 + String orcidId = properties.getProperty("source_orcid_id");
  86 + String resources_path = properties.getProperty("source_resources_path");
  87 +
  88 + OrcidAccessToken orcidToken = new OrcidAccessToken();
  89 + orcidToken.setAccess_token(properties.getProperty("orcid_access_token"));
  90 + orcidToken.setOrcid(orcidId);
  91 +
  92 + try {
  93 +
  94 + WorkTestPrepare.init(orcidId);
  95 +
  96 + ORCIDClientImpl orcidClient = new ORCIDClientImpl(properties.getProperty("orcid_login_uri"),
  97 + properties.getProperty("orcid_api_uri"),
  98 + properties.getProperty("orcid_client_id"),
  99 + properties.getProperty("orcid_client_secret"),
  100 + properties.getProperty("orcid_redirect_uri"), orcidToken);
  101 +
  102 + ORCIDHelper orcidHelper = new ORCIDHelper(orcidClient);
  103 + ActivitiesSummary activitiesSummary = orcidHelper.getActivitiesSummary();
  104 +
  105 + List<String> putcodesList = ORCIDHelper.getWorkSummaryPutCodes(activitiesSummary);
  106 +
  107 + String activitiesSummaryPath = resources_path + "/" + orcidId + "_activities.xml";
  108 + ActivitiesSummaryTestPrepare.activitiesSummaryToXMLFile(activitiesSummary,activitiesSummaryPath);
  109 +
  110 + Work work;
  111 + String workPath;
  112 + for (String putCode : putcodesList) {
  113 + work = orcidClient.getWork(putCode);
  114 +
  115 + workPath = resources_path + "/" + putCode + ".xml";
  116 + WorkTestPrepare.workToXMLFile(work,workPath);
  117 + }
  118 +
  119 +
  120 + } catch (OrcidClientException e) {
  121 + // TODO Auto-generated catch block
  122 + e.printStackTrace();
  123 + } catch (JAXBException e) {
  124 + // TODO Auto-generated catch block
  125 + e.printStackTrace();
  126 + }
  127 +
  128 + }
  129 +}
... ...