Revision 31971
Added by Alessia Bardi about 10 years ago
modules/dnet-openaire-http-api-client/trunk/src/test/java/eu/dnetlib/api/client/OpenaireApiClientTest.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.api.client; |
2 | 2 |
|
3 | 3 |
import static org.junit.Assert.assertEquals; |
4 |
import static org.junit.Assert.assertNull; |
|
4 | 5 |
|
6 |
import java.net.URI; |
|
7 |
import java.net.URISyntaxException; |
|
8 |
import java.util.List; |
|
9 |
|
|
5 | 10 |
import javax.annotation.Resource; |
6 | 11 |
|
12 |
import org.apache.http.client.utils.URIBuilder; |
|
7 | 13 |
import org.dom4j.Document; |
8 | 14 |
import org.dom4j.DocumentException; |
9 | 15 |
import org.dom4j.DocumentHelper; |
16 |
import org.junit.Test; |
|
10 | 17 |
import org.junit.runner.RunWith; |
11 | 18 |
import org.springframework.test.context.ContextConfiguration; |
12 | 19 |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
13 | 20 |
|
21 |
import com.google.common.collect.Lists; |
|
22 |
|
|
14 | 23 |
@RunWith(SpringJUnit4ClassRunner.class) |
15 | 24 |
@ContextConfiguration(locations = { "/eu/dnetlib/api/client/applicationContext-dnet-openaire-http-api-client.xml" }) |
16 | 25 |
public class OpenaireApiClientTest { |
... | ... | |
25 | 34 |
public static String publicationsQuery = "((deletedbyinference = false) AND (oaftype exact result)) and (resulttypeid exact publication)"; |
26 | 35 |
public static String datasetsQuery = "((deletedbyinference = false) AND (oaftype exact result)) and (resulttypeid exact dataset)"; |
27 | 36 |
public static int defaultPageSize = 10; |
37 |
public static List<String> commonParameters = Lists.newArrayList("model", "keywords", "page", "size", "format", "sortBy", "hasECFunding", "hasUKFunding", |
|
38 |
"funder", "fundingStream", "FP7scientificArea"); |
|
39 |
public static List<String> publicationParameters = Lists.newArrayList("author", "doi", "FP7ProjectID", "fromDateAccepted", "hasProject", "OA", |
|
40 |
"openaireAuthorID", "openaireProjectID", "openaireProviderID", "openairePublicationID", "projectID", "title", "toDateAccepted"); |
|
41 |
public static List<String> datasetParameters = Lists.newArrayList("author", "doi", "FP7ProjectID", "fromDateCollected", "hasProject", "OA", |
|
42 |
"openaireAuthorID", "openaireProjectID", "openaireProviderID", "openaireDatasetID", "projectID", "title", "toDateCollected"); |
|
43 |
public static List<String> projectParameters = Lists.newArrayList("name", "acronym", "grantID", "startYear", "participantCountries", "participantAcronyms", |
|
44 |
"endYear"); |
|
28 | 45 |
|
29 | 46 |
public void checkException(final String apiResponse, final String expectedException) throws DocumentException { |
30 | 47 |
Document doc = DocumentHelper.parseText(apiResponse); |
... | ... | |
32 | 49 |
assertEquals(expectedException, exception); |
33 | 50 |
} |
34 | 51 |
|
52 |
public void checkNotException(final String apiResponse) throws DocumentException { |
|
53 |
Document doc = DocumentHelper.parseText(apiResponse); |
|
54 |
assertNull(doc.selectSingleNode("//exception")); |
|
55 |
} |
|
56 |
|
|
57 |
@Test |
|
58 |
public void testPublicationParameters() throws URISyntaxException, DocumentException { |
|
59 |
for (String param : commonParameters) { |
|
60 |
if (!param.equals("model") && !param.equals("format") && !param.equals("page") && !param.equals("size") && !param.equals("sortBy")) { |
|
61 |
URIBuilder builder = new URIBuilder(requestBaseURL).setPath(publicationsPath).addParameter(param, "test"); |
|
62 |
URI uri = builder.build(); |
|
63 |
System.out.println(uri); |
|
64 |
String results = getApiClient().doRequest(EntityType.publication, ApiModel.openaire, uri); |
|
65 |
checkNotException(results); |
|
66 |
} |
|
67 |
} |
|
68 |
for (String param : publicationParameters) { |
|
69 |
if (!param.equals("fromDateAccepted") && !param.equals("toDateAccepted")) { |
|
70 |
URIBuilder builder = new URIBuilder(requestBaseURL).setPath(publicationsPath).addParameter(param, "test"); |
|
71 |
URI uri = builder.build(); |
|
72 |
System.out.println(uri); |
|
73 |
String results = getApiClient().doRequest(EntityType.publication, ApiModel.openaire, uri); |
|
74 |
checkNotException(results); |
|
75 |
} |
|
76 |
} |
|
77 |
} |
|
78 |
|
|
79 |
@Test |
|
80 |
public void testDatasetParameters() throws URISyntaxException, DocumentException { |
|
81 |
for (String param : commonParameters) { |
|
82 |
if (!param.equals("model") && !param.equals("format") && !param.equals("page") && !param.equals("size") && !param.equals("sortBy")) { |
|
83 |
URIBuilder builder = new URIBuilder(requestBaseURL).setPath(datasetsPath).addParameter(param, "test"); |
|
84 |
URI uri = builder.build(); |
|
85 |
System.out.println(uri); |
|
86 |
String results = getApiClient().doRequest(EntityType.dataset, ApiModel.openaire, uri); |
|
87 |
checkNotException(results); |
|
88 |
} |
|
89 |
} |
|
90 |
for (String param : datasetParameters) { |
|
91 |
if (!param.equals("fromDateCollected") && !param.equals("toDateCollected")) { |
|
92 |
URIBuilder builder = new URIBuilder(requestBaseURL).setPath(datasetsPath).addParameter(param, "test"); |
|
93 |
URI uri = builder.build(); |
|
94 |
System.out.println(uri); |
|
95 |
String results = getApiClient().doRequest(EntityType.dataset, ApiModel.openaire, uri); |
|
96 |
checkNotException(results); |
|
97 |
} |
|
98 |
} |
|
99 |
} |
|
100 |
|
|
101 |
@Test |
|
102 |
public void testProjectParameters() throws URISyntaxException, DocumentException { |
|
103 |
for (String param : commonParameters) { |
|
104 |
if (!param.equals("model") && !param.equals("format") && !param.equals("page") && !param.equals("size") && !param.equals("sortBy")) { |
|
105 |
URIBuilder builder = new URIBuilder(requestBaseURL).setPath(projectsPath).addParameter(param, "test"); |
|
106 |
URI uri = builder.build(); |
|
107 |
System.out.println(uri); |
|
108 |
String results = getApiClient().doRequest(EntityType.project, ApiModel.openaire, uri); |
|
109 |
checkNotException(results); |
|
110 |
} |
|
111 |
} |
|
112 |
for (String param : projectParameters) { |
|
113 |
if (!param.equals("startYear") && !param.equals("endYear")) { |
|
114 |
URIBuilder builder = new URIBuilder(requestBaseURL).setPath(projectsPath).addParameter(param, "test"); |
|
115 |
URI uri = builder.build(); |
|
116 |
System.out.println(uri); |
|
117 |
String results = getApiClient().doRequest(EntityType.project, ApiModel.openaire, uri); |
|
118 |
checkNotException(results); |
|
119 |
} |
|
120 |
} |
|
121 |
} |
|
122 |
|
|
35 | 123 |
public ApiClient getApiClient() { |
36 | 124 |
return apiClient; |
37 | 125 |
} |
Also available in: Unified diff
Tests for list of available parameters