Project

General

Profile

1
package eu.dnetlib.api.client;
2

    
3
import java.io.File;
4
import java.io.IOException;
5
import java.net.URI;
6
import java.net.URISyntaxException;
7
import java.net.URL;
8
import javax.xml.XMLConstants;
9
import javax.xml.transform.Source;
10
import javax.xml.validation.Schema;
11
import javax.xml.validation.SchemaFactory;
12
import javax.xml.validation.Validator;
13

    
14
import org.apache.http.client.utils.URIBuilder;
15
import org.dom4j.Document;
16
import org.dom4j.DocumentException;
17
import org.dom4j.DocumentHelper;
18
import org.dom4j.io.DocumentSource;
19
import org.junit.Before;
20
import org.junit.Ignore;
21
import org.junit.Test;
22
import org.xml.sax.SAXException;
23

    
24
import static org.junit.Assert.assertTrue;
25

    
26
@Ignore
27
public class SygmaApiClientTest extends OpenaireApiClientTest {
28

    
29
	private SchemaFactory schemaFactory;
30
	private Schema sygmaSchema;
31
	private Validator validator;
32

    
33
	@Before
34
	public void setup() throws URISyntaxException, SAXException {
35
		schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
36
		URL schemaUrl = this.getClass().getResource("/eu/dnetlib/oaf_sygma_v2.1.xsd");
37
		File schemaFile = new File(schemaUrl.toURI());
38
		sygmaSchema = schemaFactory.newSchema(schemaFile);
39
		validator = sygmaSchema.newValidator();
40
	}
41

    
42
	private void validate(final Source response) throws SAXException, IOException {
43
		validator.validate(response);
44
	}
45

    
46
	private void validate(final Document doc) throws SAXException, IOException {
47
		Source source = new DocumentSource(doc);
48
		validate(source);
49
	}
50

    
51
	@Test
52
	public void testPublicationsSygma() throws URISyntaxException, SAXException, IOException {
53
		URIBuilder builder = new URIBuilder(requestBaseURL);
54
		builder.setPath(publicationsPath);
55
		URI testURI = builder.build();
56
		System.out.println(testURI);
57
		// String results = apiClient.doRequest(EntityType.publication, ApiModel.sygma, testURI);
58
		// System.out.println(results.toString());
59
		validate(getApiClient().doRequestAsSource(EntityType.publication, ApiModel.sygma, testURI));
60
	}
61

    
62
	@Test
63
	public void testPublicationsSygmaLongPage() throws URISyntaxException, SAXException, IOException, DocumentException {
64
		int pageSizeMax = 1000;
65
		URIBuilder builder = new URIBuilder(requestBaseURL).setPath(publicationsPath).addParameter("size", "" + pageSizeMax);
66
		URI testURI = builder.build();
67
		System.out.println(testURI);
68
		validate(getApiClient().doRequestAsSource(EntityType.publication, ApiModel.sygma, testURI));
69
	}
70

    
71
	/*
72
	 * <code>237920</code> <acronym>UNILHC</acronym> <startdate>2009-10-01</startdate> <enddate>2013-09-30</enddate>
73
	 * <callidentifier>FP7-PEOPLE-ITN-2008</callidentifier><objIdentifier>corda_______::c2be0603909ad6004c5ab217e130b532</objIdentifier>
74
	 * UNILHC is currently the most referenced project with about 1.000 publications
75
	 */
76
	@Test
77
	public void testPublicationsByProject() throws URISyntaxException, SAXException, IOException, DocumentException {
78
		int pageSizeMax = 10000;
79
		String grantID = "237920";
80
		String acronym = "UNILHC";
81

    
82
		URIBuilder builder = new URIBuilder(requestBaseURL).setPath(publicationsPath).addParameter("projectID", grantID).addParameter("size", pageSizeMax + "");
83
		URI testURI = builder.build();
84
		System.out.println(testURI);
85
		String results = getApiClient().doRequest(EntityType.publication, ApiModel.sygma, testURI);
86
		Document doc = DocumentHelper.parseText(results);
87
		int pageSize = Integer.parseInt(doc.selectSingleNode("//*[local-name()='header']/*[local-name()='size']").getText());
88
		int total = Integer.parseInt(doc.selectSingleNode("//*[local-name()='header']/*[local-name()='total']").getText());
89
		assertTrue(total <= pageSizeMax);
90
		assertTrue(total <= pageSize);
91
		System.out.println("Number of pubs for project " + grantID + ", acro " + acronym + ": " + total);
92
		validate(doc);
93
	}
94

    
95
	@Test
96
	public void testPublicationsByProjectAndSort() throws URISyntaxException, SAXException, IOException, DocumentException {
97
		int pageSizeMax = 10000;
98
		String grantID = "237920";
99

    
100
		URIBuilder builder = new URIBuilder(requestBaseURL).setPath(publicationsPath).addParameter("projectID", grantID).addParameter("size", pageSizeMax + "")
101
				.addParameter("sortBy", "resultdateofacceptance,ascending");
102
		URI testURI = builder.build();
103
		System.out.println(testURI);
104
		String results = getApiClient().doRequest(EntityType.publication, ApiModel.sygma, testURI);
105
		Document doc = DocumentHelper.parseText(results);
106
		validate(doc);
107
	}
108

    
109
	@Test
110
	public void testPublicationsByProjectAndSortAndFromDate() throws URISyntaxException, SAXException, IOException, DocumentException {
111
		int pageSizeMax = 10000;
112
		String grantID = "237920";
113

    
114
		URIBuilder builder = new URIBuilder(requestBaseURL).setPath(publicationsPath).addParameter("projectID", grantID).addParameter("size", pageSizeMax + "")
115
				.addParameter("sortBy", "resultdateofacceptance,ascending").addParameter("fromDateAccepted", "2010-01-01");
116
		URI testURI = builder.build();
117
		System.out.println(testURI);
118
		String results = getApiClient().doRequest(EntityType.publication, ApiModel.sygma, testURI);
119
		Document doc = DocumentHelper.parseText(results);
120
		validate(doc);
121
	}
122

    
123
	@Test
124
	public void testDatasetsSygma() throws URISyntaxException, SAXException, IOException {
125
		URIBuilder builder = new URIBuilder(requestBaseURL);
126
		builder.setPath(datasetsPath);
127
		URI testURI = builder.build();
128
		System.out.println(testURI);
129
		// String results = apiClient.doRequest(EntityType.dataset, ApiModel.sygma, testURI);
130
		// System.out.println(results);
131
		validate(getApiClient().doRequestAsSource(EntityType.dataset, ApiModel.sygma, testURI));
132
	}
133

    
134
	@Test
135
	public void testDatasetsSygmaPage2() throws URISyntaxException, SAXException, IOException {
136
		URIBuilder builder = new URIBuilder(requestBaseURL);
137
		builder.setPath(datasetsPath);
138
		builder.setParameter("page", "2");
139
		URI testURI = builder.build();
140
		System.out.println(testURI);
141
		// String results = apiClient.doRequest(EntityType.dataset, ApiModel.sygma, testURI);
142
		// System.out.println(results);
143
		validate(getApiClient().doRequestAsSource(EntityType.dataset, ApiModel.sygma, testURI));
144
	}
145

    
146
	@Test
147
	public void testDatasetsSygmaLongPage() throws URISyntaxException, SAXException, IOException, DocumentException {
148
		int pageSizeMax = 1000;
149
		URIBuilder builder = new URIBuilder(requestBaseURL).setPath(datasetsPath).addParameter("size", "" + pageSizeMax);
150
		URI testURI = builder.build();
151
		System.out.println(testURI);
152
		validate(getApiClient().doRequestAsSource(EntityType.dataset, ApiModel.sygma, testURI));
153
	}
154

    
155
	/*
156
	 * <objIdentifier>corda_______::477201c91d06478cb2828771acea1655</objIdentifier> <code>211089</code> <acronym>OLFPERCEPT</acronym>
157
	 * <callidentifier>ERC-2007-StG</callidentifier> OLFPERCEPT is currently the most referenced project by datasets with 4 dataset :(
158
	 */
159
	@Test
160
	public void testDatasetsByProject() throws URISyntaxException, SAXException, IOException, DocumentException {
161
		int pageSizeMax = 10000;
162
		String grantID = "211089";
163
		String acronym = "OLFPERCEPT";
164

    
165
		URIBuilder builder = new URIBuilder(requestBaseURL).setPath(datasetsPath).addParameter("projectID", grantID).addParameter("size", pageSizeMax + "");
166
		URI testURI = builder.build();
167
		System.out.println(testURI);
168
		String results = getApiClient().doRequest(EntityType.dataset, ApiModel.sygma, testURI);
169
		Document doc = DocumentHelper.parseText(results);
170
		int pageSize = Integer.parseInt(doc.selectSingleNode("//*[local-name()='header']/*[local-name()='size']").getText());
171
		int total = Integer.parseInt(doc.selectSingleNode("//*[local-name()='header']/*[local-name()='total']").getText());
172
		assertTrue(total <= pageSizeMax);
173
		assertTrue(total <= pageSize);
174
		System.out.println("Number of dataset for project " + grantID + ", acro " + acronym + ": " + total);
175
		validate(doc);
176
	}
177

    
178
	@Test
179
	public void testDatasetsByProjectAndSort() throws URISyntaxException, SAXException, IOException, DocumentException {
180
		int pageSizeMax = 10000;
181
		String grantID = "211089";
182

    
183
		URIBuilder builder = new URIBuilder(requestBaseURL).setPath(datasetsPath).addParameter("projectID", grantID).addParameter("size", pageSizeMax + "")
184
				.addParameter("sortBy", "resultdateofacceptance,ascending");
185
		URI testURI = builder.build();
186
		System.out.println(testURI);
187
		String results = getApiClient().doRequest(EntityType.publication, ApiModel.sygma, testURI);
188
		Document doc = DocumentHelper.parseText(results);
189
		validate(doc);
190
	}
191

    
192
	@Test
193
	public void testDatasetsByProjectAndSortByCollectiondateAndFromDate() throws URISyntaxException, SAXException, IOException, DocumentException {
194
		int pageSizeMax = 10000;
195
		String grantID = "237920";
196

    
197
		URIBuilder builder = new URIBuilder(requestBaseURL).setPath(publicationsPath).addParameter("projectID", grantID).addParameter("size", pageSizeMax + "")
198
				.addParameter("sortBy", "dateofcollection,ascending").addParameter("fromDateAccepted", "2010-01-01");
199
		URI testURI = builder.build();
200
		System.out.println(testURI);
201
		String results = getApiClient().doRequest(EntityType.publication, ApiModel.sygma, testURI);
202
		Document doc = DocumentHelper.parseText(results);
203
		validate(doc);
204
	}
205

    
206
}
(5-5/5)