Skip to content

Commit

Permalink
Merged in DSC-809 (pull request DSpace#2168)
Browse files Browse the repository at this point in the history
[DSC-809] - fix integration tests related to openAIRE in WorkspaceItemRestRepositoryIT

Approved-by: Francesco Pio Scognamiglio
  • Loading branch information
NikitaKr1vonosov authored and francescopioscognamiglio committed May 16, 2024
2 parents d1a9478 + adcf0f5 commit c4a995e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.apache.commons.io.Charsets;
Expand Down Expand Up @@ -58,6 +59,8 @@ public class OpenAireProjectImportMetadataSourceServiceImpl extends AbstractImpo

private int timeout = 1000;

private Supplier<HttpClientBuilder> httpClientBuilderSupplier = HttpClients::custom;

@Autowired
private ConfigurationService configurationService;

Expand Down Expand Up @@ -156,7 +159,7 @@ public List<ImportRecord> call() throws Exception {
String proxyPort = configurationService.getProperty("http.proxy.port");
HttpGet method = null;
try {
HttpClientBuilder hcBuilder = HttpClients.custom();
HttpClientBuilder hcBuilder = httpClientBuilderSupplier.get();
Builder requestConfigBuilder = RequestConfig.custom();
requestConfigBuilder.setConnectionRequestTimeout(timeout);

Expand Down Expand Up @@ -213,7 +216,7 @@ public List<ImportRecord> call() throws Exception {
String proxyPort = configurationService.getProperty("http.proxy.port");
HttpGet method = null;
try {
HttpClientBuilder hcBuilder = HttpClients.custom();
HttpClientBuilder hcBuilder = httpClientBuilderSupplier.get();
Builder requestConfigBuilder = RequestConfig.custom();
requestConfigBuilder.setConnectionRequestTimeout(timeout);

Expand Down Expand Up @@ -249,6 +252,10 @@ public List<ImportRecord> call() throws Exception {
}
}

public void setHttpClientBuilderSupplier(Supplier<HttpClientBuilder> httpClientBuilderSupplier) {
this.httpClientBuilderSupplier = httpClientBuilderSupplier;
}

private List<Element> splitToRecords(String recordsSrc) {
try {
SAXBuilder saxBuilder = new SAXBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicStatusLine;
import org.dspace.app.rest.matcher.CollectionMatcher;
import org.dspace.app.rest.matcher.ItemMatcher;
import org.dspace.app.rest.matcher.MetadataMatcher;
Expand Down Expand Up @@ -108,6 +117,7 @@
import org.dspace.eperson.Group;
import org.dspace.eperson.factory.EPersonServiceFactory;
import org.dspace.eperson.service.GroupService;
import org.dspace.importer.external.openaire.service.OpenAireProjectImportMetadataSourceServiceImpl;
import org.dspace.services.ConfigurationService;
import org.dspace.services.factory.DSpaceServicesFactory;
import org.dspace.supervision.SupervisionOrder;
Expand All @@ -119,6 +129,7 @@
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.MvcResult;
Expand Down Expand Up @@ -153,6 +164,9 @@ public class WorkspaceItemRestRepositoryIT extends AbstractControllerIntegration
@Autowired
private WorkspaceItemRestRepository workspaceItemRestRepository;

@Autowired
private OpenAireProjectImportMetadataSourceServiceImpl openAireService;

@Autowired
private SubmissionService submissionService;

Expand Down Expand Up @@ -7351,8 +7365,22 @@ public void createWorkspaceItemFromExternalSourceOpenAIRE_Test() throws Exceptio

context.restoreAuthSystemState();

HttpClientBuilder httpClientBuilder = Mockito.mock(HttpClientBuilder.class);
CloseableHttpClient httpClient = Mockito.mock(CloseableHttpClient.class);
Mockito.when(httpClientBuilder.build()).thenReturn(httpClient);
InputStream openaireResponseStream = getClass().getResourceAsStream("openaire-response.xml");

BasicStatusLine statusLine = new BasicStatusLine(new HttpVersion(1, 1), HttpStatus.SC_OK, "OK");
HttpEntity httpEntity = Mockito.mock(HttpEntity.class);
CloseableHttpResponse response = Mockito.mock(CloseableHttpResponse.class);
Mockito.when(response.getStatusLine()).thenReturn(statusLine);
Mockito.when(response.getEntity()).thenReturn(httpEntity);
Mockito.when(httpEntity.getContent()).thenReturn(openaireResponseStream);
Mockito.when(httpClient.execute(Mockito.any(HttpGet.class))).thenReturn(response);

Integer workspaceItemId = null;
try {
openAireService.setHttpClientBuilderSupplier(() -> httpClientBuilder);
ObjectMapper mapper = new ObjectMapper();
// You have to be an admin to create an Item from an ExternalDataObject
String token = getAuthToken(admin.getEmail(), password);
Expand Down Expand Up @@ -7390,6 +7418,7 @@ public void createWorkspaceItemFromExternalSourceOpenAIRE_Test() throws Exceptio
)))))
));
} finally {
openAireService.setHttpClientBuilderSupplier(HttpClients::custom);
WorkspaceItemBuilder.deleteWorkspaceItem(workspaceItemId);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<root>
<results>
<result>
<code>777541</code>
<title>OpenAIRE Advancing Open Scholarship</title>
<acronym>OpenAIRE-Advance</acronym>
<startdate>2018-01-01</startdate>
<enddate>2021-02-28</enddate>
<oamandatepublications>true</oamandatepublications>
<callidentifier>H2020-EINFRA-2017</callidentifier>
<summary>OpenAIRE-Advance continues the mission of OpenAIRE to support</summary>
</result>
</results>
</root>

0 comments on commit c4a995e

Please sign in to comment.