Skip to content

Commit

Permalink
[DSC-1319] Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaGiamminonni committed Nov 6, 2023
1 parent 9f7e3ee commit c45e7d8
Show file tree
Hide file tree
Showing 5 changed files with 2,680 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.List;
import java.util.Optional;

import org.dspace.content.MetadataFieldName;
import org.dspace.importer.external.metadatamapping.MetadatumDTO;

/**
Expand Down Expand Up @@ -95,6 +96,11 @@ public Collection<MetadatumDTO> getValue(String schema, String element, String q
return values;
}

public Optional<String> getSingleValue(String field) {
MetadataFieldName metadataFieldName = new MetadataFieldName(field);
return getSingleValue(metadataFieldName.schema, metadataFieldName.element, metadataFieldName.qualifier);
}

public Optional<String> getSingleValue(String schema, String element, String qualifier) {
return getValue(schema, element, qualifier).stream()
.map(MetadatumDTO::getValue)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.app.rest;

import static org.dspace.app.matcher.LambdaMatcher.matches;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.mockito.Mockito.when;

import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Optional;

import org.apache.commons.io.IOUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.impl.client.CloseableHttpClient;
import org.dspace.importer.external.datamodel.ImportRecord;
import org.dspace.importer.external.liveimportclient.service.LiveImportClientImpl;
import org.dspace.importer.external.ror.service.RorImportMetadataSourceServiceImpl;
import org.hamcrest.Matcher;
import org.junit.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;

public class RorImportMetadataSourceServiceIT extends AbstractLiveImportIntegrationTest {

@Autowired
private LiveImportClientImpl liveImportClient;

@Autowired
private RorImportMetadataSourceServiceImpl rorServiceImpl;

@Test
public void tesGetRecords() throws Exception {
context.turnOffAuthorisationSystem();
CloseableHttpClient originalHttpClient = liveImportClient.getHttpClient();
CloseableHttpClient httpClient = Mockito.mock(CloseableHttpClient.class);

try (InputStream file = getClass().getResourceAsStream("ror-records.json")) {

String jsonResponse = IOUtils.toString(file, Charset.defaultCharset());

liveImportClient.setHttpClient(httpClient);
CloseableHttpResponse response = mockResponse(jsonResponse, 200, "OK");
when(httpClient.execute(ArgumentMatchers.any())).thenReturn(response);

context.restoreAuthSystemState();
Collection<ImportRecord> recordsImported = rorServiceImpl.getRecords("test query", 0, 2);
assertThat(recordsImported, hasSize(10));

ImportRecord record = recordsImported.iterator().next();

assertThat(record.getValueList(), hasSize(11));

assertThat(record.getSingleValue("dc.title"), is("The University of Texas"));
assertThat(record.getSingleValue("organization.identifier.ror"), is("https://ror.org/02f6dcw23"));
assertThat(record.getSingleValue("oairecerif.acronym"), is("UTHSCSA"));
assertThat(record.getSingleValue("oairecerif.identifier.url"), is("http://www.uthscsa.edu/"));
assertThat(record.getSingleValue("dc.type"), is("Education"));
assertThat(record.getSingleValue("organization.address.addressCountry"), is("US"));
assertThat(record.getSingleValue("organization.foundingDate"), is("1959"));
assertThat(record.getValue("organization", "identifier", "crossrefid"), hasSize(2));
assertThat(record.getSingleValue("organization.identifier.isni"), is("0000 0001 0629 5880"));
assertThat(record.getSingleValue("organization.parentOrganization"), is("The University of Texas System"));

} finally {
liveImportClient.setHttpClient(originalHttpClient);
}
}

@Test
public void tesCount() throws Exception {
context.turnOffAuthorisationSystem();
CloseableHttpClient originalHttpClient = liveImportClient.getHttpClient();
CloseableHttpClient httpClient = Mockito.mock(CloseableHttpClient.class);

try (InputStream file = getClass().getResourceAsStream("ror-records.json")) {

String jsonResponse = IOUtils.toString(file, Charset.defaultCharset());

liveImportClient.setHttpClient(httpClient);
CloseableHttpResponse response = mockResponse(jsonResponse, 200, "OK");
when(httpClient.execute(ArgumentMatchers.any())).thenReturn(response);

context.restoreAuthSystemState();
Integer count = rorServiceImpl.count("test");
assertThat(count, equalTo(200));
} finally {
liveImportClient.setHttpClient(originalHttpClient);
}
}

@Test
public void tesGetRecord() throws Exception {
context.turnOffAuthorisationSystem();
CloseableHttpClient originalHttpClient = liveImportClient.getHttpClient();
CloseableHttpClient httpClient = Mockito.mock(CloseableHttpClient.class);

try (InputStream file = getClass().getResourceAsStream("ror-record.json")) {

String jsonResponse = IOUtils.toString(file, Charset.defaultCharset());

liveImportClient.setHttpClient(httpClient);
CloseableHttpResponse response = mockResponse(jsonResponse, 200, "OK");
when(httpClient.execute(ArgumentMatchers.any())).thenReturn(response);

context.restoreAuthSystemState();
ImportRecord record = rorServiceImpl.getRecord("https://ror.org/01sps7q28");
assertThat(record.getValueList(), hasSize(9));
assertThat(record.getSingleValue("dc.title"), is("The University of Texas Health Science Center at Tyler"));
assertThat(record.getSingleValue("organization.identifier.ror"), is("https://ror.org/01sps7q28"));
assertThat(record.getSingleValue("oairecerif.acronym"), is("UTHSCT"));
assertThat(record.getSingleValue("oairecerif.identifier.url"),
is("https://www.utsystem.edu/institutions/university-texas-health-science-center-tyler"));
assertThat(record.getSingleValue("dc.type"), is("Healthcare"));
assertThat(record.getSingleValue("organization.address.addressCountry"), is("US"));
assertThat(record.getSingleValue("organization.foundingDate"), is("1947"));
assertThat(record.getSingleValue("organization.identifier.isni"), is("0000 0000 9704 5790"));
assertThat(record.getSingleValue("organization.parentOrganization"), is("The University of Texas System"));

} finally {
liveImportClient.setHttpClient(originalHttpClient);
}
}

private Matcher<Optional<String>> is(String value) {
return matches(optionalValue -> optionalValue.isPresent() && optionalValue.get().equals(value));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.app.rest;

import static org.dspace.app.rest.matcher.ItemAuthorityMatcher.matchItemAuthorityWithOtherInformations;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize;
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 java.util.HashMap;
import java.util.Map;

import org.dspace.app.rest.test.AbstractControllerIntegrationTest;
import org.junit.Test;

public class RorOrgUnitAuthorityIT extends AbstractControllerIntegrationTest {

@Test
public void testAuthority() throws Exception {

Map<String, String> expectedExtras = new HashMap<>();
expectedExtras.put("data-ror_orgunit_id", "https://ror.org/02z02cv32");
expectedExtras.put("ror_orgunit_id", "https://ror.org/02z02cv32");
expectedExtras.put("data-ror_orgunit_type", "Nonprofit");
expectedExtras.put("ror_orgunit_type", "Nonprofit");
expectedExtras.put("data-ror_orgunit_acronym", "WEICan, IEEC");
expectedExtras.put("ror_orgunit_acronym", "WEICan, IEEC");

String token = getAuthToken(eperson.getEmail(), password);
getClient(token).perform(get("/api/submission/vocabularies/OrgUnitAuthority/entries")
.param("filter", "test"))
.andExpect(status().isOk())
.andExpect(jsonPath("$._embedded.entries", hasSize(10)))
.andExpect(jsonPath("$._embedded.entries",
hasItem(matchItemAuthorityWithOtherInformations("will be referenced::ROR-ID::https://ror.org/02z02cv32",
"Wind Energy Institute of Canada", "Wind Energy Institute of Canada", "vocabularyEntry",
expectedExtras))));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
{
"id": "https://ror.org/01sps7q28",
"name": "The University of Texas Health Science Center at Tyler",
"email_address": null,
"ip_addresses": [

],
"established": 1947,
"types": [
"Healthcare"
],
"relationships": [
{
"label": "The University of Texas System",
"type": "Parent",
"id": "https://ror.org/01gek1696"
}
],
"addresses": [
{
"lat": 32.426014,
"lng": -95.212728,
"state": "Texas",
"state_code": "US-TX",
"city": "Tyler",
"geonames_city": {
"id": 4738214,
"city": "Tyler",
"geonames_admin1": {
"name": "Texas",
"id": 4736286,
"ascii_name": "Texas",
"code": "US.TX"
},
"geonames_admin2": {
"name": "Smith County",
"id": 4729130,
"ascii_name": "Smith County",
"code": "US.TX.423"
},
"license": {
"attribution": "Data from geonames.org under a CC-BY 3.0 license",
"license": "http://creativecommons.org/licenses/by/3.0/"
},
"nuts_level1": {
"name": null,
"code": null
},
"nuts_level2": {
"name": null,
"code": null
},
"nuts_level3": {
"name": null,
"code": null
}
},
"postcode": null,
"primary": false,
"line": null,
"country_geonames_id": 6252001
}
],
"links": [
"https://www.utsystem.edu/institutions/university-texas-health-science-center-tyler"
],
"aliases": [
"East Texas Tuberculosis Sanitarium",
"UT Health Northeast"
],
"acronyms": [
"UTHSCT"
],
"status": "active",
"wikipedia_url": "https://en.wikipedia.org/wiki/University_of_Texas_Health_Science_Center_at_Tyler",
"labels": [

],
"country": {
"country_name": "United States",
"country_code": "US"
},
"external_ids": {
"ISNI": {
"preferred": null,
"all": [
"0000 0000 9704 5790"
]
},
"OrgRef": {
"preferred": null,
"all": [
"3446655"
]
},
"Wikidata": {
"preferred": null,
"all": [
"Q7896437"
]
},
"GRID": {
"preferred": "grid.267310.1",
"all": "grid.267310.1"
}
}
}
Loading

0 comments on commit c45e7d8

Please sign in to comment.