Project

General

Profile

1
package eu.dnetlib.api.client;
2

    
3
import java.net.URI;
4
import java.net.URISyntaxException;
5
import java.util.List;
6
import javax.annotation.Resource;
7

    
8
import com.google.common.collect.Lists;
9
import org.apache.http.client.utils.URIBuilder;
10
import org.dom4j.Document;
11
import org.dom4j.DocumentException;
12
import org.dom4j.DocumentHelper;
13
import org.dom4j.Node;
14
import org.junit.runner.RunWith;
15
import org.springframework.test.context.ContextConfiguration;
16
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
17

    
18
import static org.junit.Assert.assertEquals;
19
import static org.junit.Assert.assertNull;
20

    
21
@RunWith(SpringJUnit4ClassRunner.class)
22
@ContextConfiguration(locations = { "/eu/dnetlib/api/client/applicationContext-dnet-openaire-http-api-client.xml" })
23
public class OpenaireApiClientTest {
24

    
25
	@Resource
26
	private ApiClient apiClient;
27
	// public static String requestBaseURL = "http://api.openaire.eu";
28
	// public static String basePath = "/search/";
29
	public static String requestBaseURL = "http://beta.services.openaire.eu:8480";
30
	public static String basePath = "/search/api/";
31
	// public static String requestBaseURL = "http://rudie.di.uoa.gr:8080";
32
	// public static String basePath = "/dnet-functionality-services-1.0.0-SNAPSHOT/api/";
33
	public static String publicationsPath = basePath + "publications";
34
	public static String datasetsPath = basePath + "datasets";
35
	public static String projectsPath = basePath + "projects";
36
	public static String publicationsQuery = "(oaftype exact result) and (resulttypeid exact publication)";
37
	public static String datasetsQuery = "(oaftype exact result) and (resulttypeid exact dataset)";
38
	public static int defaultPageSize = 10;
39

    
40
	//TODO: re-introduce "FP7scientificArea" when the index with relfundinglevelX_name is ready
41
	public static List<String> commonParameters = Lists.newArrayList("model", "keywords", "page", "size", "format", "sortBy", "hasECFunding", "hasWTFunding",
42
			"funder");
43
	//TODO: re-introduce "fundingStream" when the index with relfundinglevelX_name is ready
44
	public static List<String> publicationParameters = Lists.newArrayList("author", "doi", "FP7ProjectID", "fromDateAccepted", "hasProject", "OA",
45
			"openaireAuthorID", "openaireProjectID", "openaireProviderID", "openairePublicationID", "projectID", "title", "toDateAccepted");
46
	//TODO: re-introduce "fundingStream" when the index with relfundinglevelX_name is ready
47
	public static List<String> datasetParameters = Lists.newArrayList("author", "doi", "FP7ProjectID", "fromDateAccepted",
48
			"funder", "hasProject", "OA",
49
			"openaireAuthorID", "openaireProjectID", "openaireProviderID", "openaireDatasetID", "projectID", "title", "toDateAccepted");
50

    
51
	public static List<String> projectParameters = Lists.newArrayList("name", "acronym", "grantID", "callID", "startYear", "participantCountries",
52
			"participantAcronyms", "endYear");
53
	//TODO Current not existing: resultstoragedate, to be removed from the docs for datasets and publications
54
	public static List<String> sortByParams_results = Lists.newArrayList("dateofcollection", "resultembargoenddate",
55
			"resultembargoendyear", "resultdateofacceptance", "resultacceptanceyear");
56
	public static List<String> sortByParams_projects = Lists.newArrayList("projectstartdate", "projectstartyear", "projectenddate", "projectendyear",
57
			"projectduration");
58

    
59
	public static int MAX_NUM_RESULTS = 10000;
60
	public static int MAX_PAGE_SIZE = 10000;
61

    
62
	public void checkException(final String apiResponse, final String expectedException) throws DocumentException {
63
		Document doc = DocumentHelper.parseText(apiResponse);
64
		String exception = doc.selectSingleNode("//exception").getText();
65
		assertEquals(expectedException, exception);
66
	}
67

    
68
	public void checkNotException(final String apiResponse) throws DocumentException {
69
		Document doc = DocumentHelper.parseText(apiResponse);
70
		Node n = doc.selectSingleNode("//exception");
71
		if (n != null) {
72
			System.out.println(apiResponse);
73
		}
74
		assertNull(n);
75

    
76
	}
77

    
78
	public void checkSortByParamaters(final EntityType entityType) throws URISyntaxException, DocumentException {
79
		String path = "";
80
		List<String> paramList = null;
81
		ApiModel model = ApiModel.openaire;
82
		switch (entityType) {
83
		case dataset:
84
			path = datasetsPath;
85
			paramList = sortByParams_results;
86
			break;
87
		case project:
88
			path = projectsPath;
89
			paramList = sortByParams_projects;
90
			model = ApiModel.none;
91
			break;
92
		case publication:
93
			path = publicationsPath;
94
			paramList = sortByParams_results;
95
			break;
96
		default:
97
			throw new IllegalArgumentException("entity type " + entityType + " is not handled");
98
		}
99
		for (String sortBy : paramList) {
100
			URIBuilder builder = new URIBuilder(requestBaseURL).setPath(path).setParameter("sortBy", sortBy + ",descending");
101
			URI url = builder.build();
102
			System.out.println(url);
103
			String results = getApiClient().doRequest(entityType, model, url);
104
			// System.out.println(results);
105
			checkNotException(results);
106
		}
107
	}
108

    
109
	public ApiClient getApiClient() {
110
		return apiClient;
111
	}
112

    
113
	public void setApiClient(final ApiClient apiClient) {
114
		this.apiClient = apiClient;
115
	}
116

    
117
}
(1-1/5)