mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-07 01:54:22 +00:00
added mockito test to prove functionality of OrcidExternalSources
This commit is contained in:
@@ -9,6 +9,7 @@ package org.dspace.external;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Scanner;
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.HttpResponse;
|
||||
@@ -32,6 +33,13 @@ public class OrcidRestConnector {
|
||||
|
||||
private String url;
|
||||
|
||||
private HttpClient httpClient;
|
||||
|
||||
@PostConstruct
|
||||
private void setup() {
|
||||
this.httpClient = HttpClientBuilder.create().build();
|
||||
}
|
||||
|
||||
public OrcidRestConnector(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
@@ -47,7 +55,6 @@ public class OrcidRestConnector {
|
||||
httpGet.addHeader("Authorization","Bearer " + accessToken);
|
||||
}
|
||||
try {
|
||||
HttpClient httpClient = HttpClientBuilder.create().build();
|
||||
HttpResponse getResponse = httpClient.execute(httpGet);
|
||||
//do not close this httpClient
|
||||
result = getResponse.getEntity().getContent();
|
||||
@@ -77,5 +84,11 @@ public class OrcidRestConnector {
|
||||
return s.hasNext() ? s.next() : "";
|
||||
}
|
||||
|
||||
public HttpClient getHttpClient() {
|
||||
return httpClient;
|
||||
}
|
||||
|
||||
public void setHttpClient(HttpClient httpClient) {
|
||||
this.httpClient = httpClient;
|
||||
}
|
||||
}
|
||||
|
@@ -17,6 +17,7 @@ import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.HttpResponse;
|
||||
@@ -56,8 +57,15 @@ public class OrcidV3AuthorDataProvider implements ExternalDataProvider {
|
||||
|
||||
private String orcidUrl;
|
||||
|
||||
private XMLtoBio converter;
|
||||
|
||||
public static final String ORCID_ID_SYNTAX = "\\d{4}-\\d{4}-\\d{4}-(\\d{3}X|\\d{4})";
|
||||
|
||||
@PostConstruct
|
||||
private void setup() {
|
||||
this.converter = new XMLtoBio();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSourceIdentifier() {
|
||||
return sourceIdentifier;
|
||||
@@ -102,14 +110,6 @@ public class OrcidV3AuthorDataProvider implements ExternalDataProvider {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes an instance of the Orcidv3 class based on the provided parameters.
|
||||
* This constructor is called through the spring bean initialization
|
||||
*/
|
||||
private OrcidV3AuthorDataProvider(String url) {
|
||||
this.orcidRestConnector = new OrcidRestConnector(url);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ExternalDataObject> getExternalDataObject(String id) {
|
||||
Person person = getBio(id);
|
||||
@@ -161,7 +161,6 @@ public class OrcidV3AuthorDataProvider implements ExternalDataProvider {
|
||||
return null;
|
||||
}
|
||||
InputStream bioDocument = orcidRestConnector.get(id + ((id.endsWith("/person")) ? "" : "/person"), accessToken);
|
||||
XMLtoBio converter = new XMLtoBio();
|
||||
Person person = converter.convertSinglePerson(bioDocument);
|
||||
try {
|
||||
bioDocument.close();
|
||||
@@ -189,7 +188,6 @@ public class OrcidV3AuthorDataProvider implements ExternalDataProvider {
|
||||
String searchPath = "search?q=" + URLEncoder.encode(query) + "&start=" + start + "&rows=" + limit;
|
||||
log.debug("queryBio searchPath=" + searchPath + " accessToken=" + accessToken);
|
||||
InputStream bioDocument = orcidRestConnector.get(searchPath, accessToken);
|
||||
XMLtoBio converter = new XMLtoBio();
|
||||
List<Result> results = converter.convert(bioDocument);
|
||||
List<Person> bios = new LinkedList<>();
|
||||
for (Result result : results) {
|
||||
@@ -225,7 +223,6 @@ public class OrcidV3AuthorDataProvider implements ExternalDataProvider {
|
||||
String searchPath = "search?q=" + URLEncoder.encode(query) + "&start=" + 0 + "&rows=" + 0;
|
||||
log.debug("queryBio searchPath=" + searchPath + " accessToken=" + accessToken);
|
||||
InputStream bioDocument = orcidRestConnector.get(searchPath, accessToken);
|
||||
XMLtoBio converter = new XMLtoBio();
|
||||
return converter.getNumberOfResultsFromXml(bioDocument);
|
||||
}
|
||||
|
||||
@@ -279,4 +276,20 @@ public class OrcidV3AuthorDataProvider implements ExternalDataProvider {
|
||||
public void setClientSecret(String clientSecret) {
|
||||
this.clientSecret = clientSecret;
|
||||
}
|
||||
|
||||
public OrcidRestConnector getOrcidRestConnector() {
|
||||
return orcidRestConnector;
|
||||
}
|
||||
|
||||
public void setOrcidRestConnector(OrcidRestConnector orcidRestConnector) {
|
||||
this.orcidRestConnector = orcidRestConnector;
|
||||
}
|
||||
|
||||
public XMLtoBio getConverter() {
|
||||
return converter;
|
||||
}
|
||||
|
||||
public void setConverter(XMLtoBio converter) {
|
||||
this.converter = converter;
|
||||
}
|
||||
}
|
||||
|
@@ -9,17 +9,30 @@ package org.dspace.app.rest;
|
||||
|
||||
import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import com.amazonaws.util.StringInputStream;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.ProtocolVersion;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.entity.BasicHttpEntity;
|
||||
import org.apache.http.message.BasicHttpResponse;
|
||||
import org.dspace.app.rest.test.AbstractControllerIntegrationTest;
|
||||
import org.dspace.external.OrcidRestConnector;
|
||||
import org.dspace.external.provider.impl.OrcidV3AuthorDataProvider;
|
||||
import org.dspace.external.provider.orcid.xml.XMLtoBio;
|
||||
import org.dspace.services.ConfigurationService;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Assume;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentMatchers;
|
||||
import org.mockito.Mockito;
|
||||
import org.orcid.jaxb.model.record_v3.NameType;
|
||||
import org.orcid.jaxb.model.record_v3.NameType.FamilyName;
|
||||
import org.orcid.jaxb.model.record_v3.Person;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
/**
|
||||
@@ -34,7 +47,12 @@ public class OrcidExternalSourcesIT extends AbstractControllerIntegrationTest {
|
||||
@Autowired
|
||||
ConfigurationService configurationService;
|
||||
|
||||
@Before
|
||||
@Autowired
|
||||
private OrcidRestConnector orcidRestConnector;
|
||||
|
||||
@Autowired
|
||||
private OrcidV3AuthorDataProvider orcidV3AuthorDataProvider;
|
||||
|
||||
public void onlyRunIfConfigExists() {
|
||||
if (StringUtils.isBlank(configurationService.getProperty("orcid.clientid"))) {
|
||||
Assume.assumeNoException(new IllegalStateException("Missing ORCID credentials"));
|
||||
@@ -43,6 +61,7 @@ public class OrcidExternalSourcesIT extends AbstractControllerIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void findOneExternalSourcesExistingSources() throws Exception {
|
||||
onlyRunIfConfigExists();
|
||||
getClient().perform(get("/api/integration/externalsources/orcid"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$", Matchers.allOf(
|
||||
@@ -55,6 +74,7 @@ public class OrcidExternalSourcesIT extends AbstractControllerIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void findOneExternalSourcesExistingSourcesWithentryValueTest() throws Exception {
|
||||
onlyRunIfConfigExists();
|
||||
String entry = "0000-0002-9029-1854";
|
||||
getClient().perform(get("/api/integration/externalsources/orcid/entryValues/" + entry))
|
||||
.andExpect(status().isOk())
|
||||
@@ -73,6 +93,7 @@ public class OrcidExternalSourcesIT extends AbstractControllerIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void findOneExternalSourceEntriesApplicableQuery() throws Exception {
|
||||
onlyRunIfConfigExists();
|
||||
String q = "orcid:0000-0002-9029-1854";
|
||||
getClient().perform(get("/api/integration/externalsources/orcid/entries")
|
||||
.param("query", q))
|
||||
@@ -96,6 +117,7 @@ public class OrcidExternalSourcesIT extends AbstractControllerIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void findOneExternalSourceEntriesApplicableQueryFamilyNameAndGivenNamesTest() throws Exception {
|
||||
onlyRunIfConfigExists();
|
||||
String q = "family-name:bollini AND given-names:andrea";
|
||||
getClient().perform(get("/api/integration/externalsources/orcid/entries")
|
||||
.param("query", q))
|
||||
@@ -116,4 +138,47 @@ public class OrcidExternalSourcesIT extends AbstractControllerIntegrationTest {
|
||||
.andExpect(jsonPath("$._embedded.externalSourceEntries[0].metadata['person.identifier.orcid'][0].value",
|
||||
is("0000-0002-9029-1854")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findOneExternalSourcesMockitoTest() throws Exception {
|
||||
|
||||
HttpClient httpClient = Mockito.mock(HttpClient.class);
|
||||
XMLtoBio converter = Mockito.mock(XMLtoBio.class);
|
||||
orcidV3AuthorDataProvider.setConverter(converter);
|
||||
orcidRestConnector.setHttpClient(httpClient);
|
||||
|
||||
BasicHttpResponse basicHttpResponse = new BasicHttpResponse(new ProtocolVersion("http", 1, 1), 200, "OK");
|
||||
basicHttpResponse.setEntity(new BasicHttpEntity());
|
||||
StringInputStream is = new StringInputStream("HttpResponseProxy{HTTP/1.1 200 OK "
|
||||
+ "[Server: nginx/1.16.1, Cache-Control: no-cache, no-store, max-age=0, must-revalidate,"
|
||||
+ " Content-Type: application/vnd.orcid+xml; qs=5;charset=UTF-8,"
|
||||
+ " Expires: 0, Pragma: no-cache, X-XSS-Protection: 1; mode=block, Transfer-Encoding: chunked,"
|
||||
+ " Connection: keep-alive, path=/, X-Frame-Options: DENY]}");
|
||||
BasicHttpEntity bhe = new BasicHttpEntity();
|
||||
basicHttpResponse.setEntity(bhe);
|
||||
bhe.setChunked(true);
|
||||
bhe.setContent(is);
|
||||
|
||||
String entry = "0000-0002-9029-1854";
|
||||
|
||||
NameType name = new NameType();
|
||||
name.setFamilyName(new FamilyName("Bollini, Andrea"));
|
||||
|
||||
Person person = new Person();
|
||||
person.setName(name);
|
||||
name.setPath(entry);
|
||||
|
||||
when(httpClient.execute(ArgumentMatchers.any())).thenReturn(basicHttpResponse);
|
||||
when(converter.convertSinglePerson(is)).thenReturn(person);
|
||||
|
||||
getClient().perform(get("/api/integration/externalsources/orcid/entryValues/" + entry))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$", Matchers.allOf(
|
||||
hasJsonPath("$.id", is(entry)),
|
||||
hasJsonPath("$.display", is("Bollini, Andrea")),
|
||||
hasJsonPath("$.value", is("Bollini, Andrea")),
|
||||
hasJsonPath("$.externalSource", is("orcid")),
|
||||
hasJsonPath("$.type", is("externalSourceEntry"))
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
@@ -21,12 +21,16 @@
|
||||
|
||||
</bean>
|
||||
<bean class="org.dspace.external.provider.impl.OrcidV3AuthorDataProvider" init-method="init">
|
||||
<constructor-arg value="${orcid.api.url}"/>
|
||||
<property name="sourceIdentifier" value="orcid"/>
|
||||
<property name="orcidUrl" value="${orcid.url}" />
|
||||
<property name="clientId" value="${orcid.clientid}" />
|
||||
<property name="clientSecret" value="${orcid.clientsecret}" />
|
||||
<property name="OAUTHUrl" value="${orcid.oauth.url}" />
|
||||
<property name="orcidRestConnector" ref="orcidRestConnector"/>
|
||||
</bean>
|
||||
|
||||
<bean id="orcidRestConnector" class="org.dspace.external.OrcidRestConnector">
|
||||
<constructor-arg value="${orcid.api.url}"/>
|
||||
</bean>
|
||||
|
||||
<bean class="org.dspace.external.provider.impl.LCNameDataProvider">
|
||||
|
Reference in New Issue
Block a user