Skip to content

Commit

Permalink
tests: add test case for fetching orcid info
Browse files Browse the repository at this point in the history
  • Loading branch information
rekt-hard committed Jan 22, 2024
1 parent 9dfaafa commit 5c3d324
Show file tree
Hide file tree
Showing 15 changed files with 7,940 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import at.ac.tuwien.damap.enums.EIdentifierType;
import at.ac.tuwien.damap.rest.dmp.domain.ContributorDO;
import at.ac.tuwien.damap.rest.dmp.domain.IdentifierDO;
import at.ac.tuwien.damap.rest.persons.orcid.models.ORCIDExpandedSearchPerson;
import at.ac.tuwien.damap.rest.persons.orcid.models.ORCIDExpandedSearchResult;
import at.ac.tuwien.damap.rest.persons.orcid.models.ORCIDRecord;
import at.ac.tuwien.damap.rest.persons.orcid.models.base.ORCIDAffiliation;
import at.ac.tuwien.damap.rest.persons.orcid.models.base.ORCIDDate;
Expand Down Expand Up @@ -78,6 +80,19 @@ public ContributorDO mapRecordEntityToPersonDO(ORCIDRecord orcidRecord,
return contributorDO;
}

public List<ContributorDO> mapExpandedSearchResultEntityToDOList(ORCIDExpandedSearchResult expandedSearchResult) {
List<ContributorDO> contributors = List.of();

if (expandedSearchResult.getNumFound() > 0 && expandedSearchResult.getPersons() != null) {
contributors = expandedSearchResult.getPersons().stream().map(o -> {
var c = new ContributorDO();
ORCIDMapper.mapExpandedSearchPersonEntityToDO(o, c);
return c;
}).collect(Collectors.toList());
}
return contributors;
}

private final Comparator<? super ORCIDAffiliation> sortByCurrentStartDate = ((ORCIDAffiliation a,
ORCIDAffiliation b) -> {
ORCIDDate aEndDate = a.getEndDate();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package at.ac.tuwien.damap.rest.persons.orcid;

import java.util.List;
import java.util.stream.Collectors;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
Expand Down Expand Up @@ -35,14 +34,8 @@ public ResultList<ContributorDO> search(MultivaluedMap<String, String> queryPara
List<ContributorDO> contributors = null;
try {
var orcidSearch = orcidRestClient.getAll(search.getQuery(), search.getPagination().getPerPage());
contributors = ORCIDMapper.mapExpandedSearchResultEntityToDOList(orcidSearch);

if (orcidSearch.getNumFound() > 0 && orcidSearch.getPersons() != null) {
contributors = orcidSearch.getPersons().stream().map(o -> {
var c = new ContributorDO();
ORCIDMapper.mapExpandedSearchPersonEntityToDO(o, c);
return c;
}).collect(Collectors.toList());
}
} catch (Exception e) {
log.error("Issue searching ORCID persons", e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package at.ac.tuwien.damap.rest.persons.orcid;

import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
Expand All @@ -13,6 +14,7 @@
import at.ac.tuwien.damap.rest.persons.orcid.models.ORCIDExpandedSearchResult;
import at.ac.tuwien.damap.rest.persons.orcid.models.ORCIDRecord;

@ApplicationScoped
@RegisterRestClient(configKey = "rest.orcid.search")
@Path("/v3.0")
@Produces(MediaType.APPLICATION_JSON)
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/at/ac/tuwien/damap/TestSetup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package at.ac.tuwien.damap;

import static org.mockito.ArgumentMatchers.any;

import javax.inject.Inject;

import org.junit.jupiter.api.BeforeEach;
import org.mockito.Mockito;

import at.ac.tuwien.damap.rest.dmp.domain.DmpDO;
import at.ac.tuwien.damap.rest.persons.MockUniversityPersonServiceImpl;
import at.ac.tuwien.damap.rest.persons.orcid.ORCIDMapper;
import at.ac.tuwien.damap.rest.persons.orcid.ORCIDPersonServiceImpl;
import at.ac.tuwien.damap.security.SecurityService;
import at.ac.tuwien.damap.util.TestDOFactory;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.mockito.InjectMock;

// Common config for test case setup
@QuarkusTest
public class TestSetup {
@Inject
protected TestDOFactory testDOFactory;

@InjectMock
protected SecurityService securityService;

@InjectMock
MockUniversityPersonServiceImpl personService;

@InjectMock
protected ORCIDPersonServiceImpl orcidPersonServiceImpl;

protected DmpDO dmpDO;

@BeforeEach
public void setup() {
Mockito.when(securityService.getUserId()).thenReturn("012345");
Mockito.when(securityService.getUserName()).thenReturn("testUser");
Mockito.when(personService.read(any(String.class))).thenReturn(testDOFactory.getTestContributorDO());
Mockito.when(orcidPersonServiceImpl.read(any(String.class)))
.thenReturn(
ORCIDMapper.mapRecordEntityToPersonDO(testDOFactory.getORCIDTestRecord(),
testDOFactory.getTestContributorDO()));

dmpDO = testDOFactory.getOrCreateTestDmpDO();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package at.ac.tuwien.damap.conversion;

import at.ac.tuwien.damap.rest.dmp.domain.DmpDO;
import at.ac.tuwien.damap.rest.persons.orcid.ORCIDPersonServiceImpl;
import at.ac.tuwien.damap.rest.projects.MockProjectServiceImpl;
import at.ac.tuwien.damap.util.TestDOFactory;
import io.quarkus.test.junit.QuarkusTest;
Expand All @@ -13,6 +14,7 @@

import javax.inject.Inject;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;

@QuarkusTest
Expand All @@ -25,13 +27,17 @@ class AbstractTemplateExportScienceEuropeComponentsTest extends AbstractTemplate
@InjectMock
MockProjectServiceImpl mockProjectService;

@InjectMock
ORCIDPersonServiceImpl orcidPersonServiceImpl;

@BeforeEach
public void setup() {
Mockito.when(mockProjectService.read(anyString())).thenReturn(testDOFactory.getTestProjectDO());
Mockito.when(orcidPersonServiceImpl.read(any(String.class))).thenReturn(testDOFactory.getTestContributorDO());
}

@Test
void determinteDatasetIDsTest(){
void determineDatasetIDsTest() {
DmpDO dmpDO = testDOFactory.getOrCreateTestDmpDO();
exportSetup(dmpDO.getId());
Assertions.assertEquals(datasetTableIDs.size(), datasets.size(), dmpDO.getDatasets().size());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package at.ac.tuwien.damap.conversion;

import javax.inject.Inject;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import at.ac.tuwien.damap.TestSetup;
import at.ac.tuwien.damap.rest.dmp.domain.DmpDO;
import at.ac.tuwien.damap.rest.projects.MockProjectServiceImpl;
import at.ac.tuwien.damap.util.TestDOFactory;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.mockito.InjectMock;
import io.quarkus.test.security.TestSecurity;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import javax.inject.Inject;

@QuarkusTest
class ExportFWFTemplateTest {
class ExportFWFTemplateTest extends TestSetup {

@Inject
ExportFWFTemplate exportFWFTemplate;
Expand All @@ -26,13 +26,6 @@ class ExportFWFTemplateTest {
@InjectMock
MockProjectServiceImpl mockProjectService;

String projectId = "-1";

@BeforeEach
public void setup() {
Mockito.when(mockProjectService.read(projectId)).thenReturn(testDOFactory.getTestProjectDO());
}

@Test
@TestSecurity(authorizationEnabled = false)
void testFWFTemplateDmp() {
Expand All @@ -52,7 +45,7 @@ void testFWFTemplateDmp() {
void testEmptyFWFTemplateDmp() {
final DmpDO emptyDmpDO = testDOFactory.getOrCreateTestDmpDOEmpty();

//testing the export document return not a null document
// testing the export document return not a null document
XWPFDocument document = null;
try {
document = exportFWFTemplate.exportTemplate(emptyDmpDO.getId());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package at.ac.tuwien.damap.conversion;

import at.ac.tuwien.damap.TestSetup;
import at.ac.tuwien.damap.rest.dmp.domain.DmpDO;
import at.ac.tuwien.damap.rest.projects.MockProjectServiceImpl;
import at.ac.tuwien.damap.util.TestDOFactory;
Expand All @@ -8,14 +9,12 @@
import io.quarkus.test.security.TestSecurity;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import javax.inject.Inject;

@QuarkusTest
class ExportHorizonEuropeTemplateTest {
class ExportHorizonEuropeTemplateTest extends TestSetup {

@Inject
ExportHorizonEuropeTemplate exportHorizonEuropeTemplate;
Expand All @@ -26,13 +25,6 @@ class ExportHorizonEuropeTemplateTest {
@InjectMock
MockProjectServiceImpl mockProjectService;

String projectId = "-1";

@BeforeEach
public void setup() {
Mockito.when(mockProjectService.read(projectId)).thenReturn(testDOFactory.getTestProjectDO());
}

@Test
@TestSecurity(authorizationEnabled = false)
void testTemplateDmp() {
Expand All @@ -52,7 +44,7 @@ void testTemplateDmp() {
void testEmptyTemplateDmp() {
final DmpDO emptyDmpDO = testDOFactory.getOrCreateTestDmpDOEmpty();

//testing the export document return not a null document
// testing the export document return not a null document
XWPFDocument document = null;
try {
document = exportHorizonEuropeTemplate.exportTemplate(emptyDmpDO.getId());
Expand Down
31 changes: 2 additions & 29 deletions src/test/java/at/ac/tuwien/damap/rest/AccessResourceTest.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
package at.ac.tuwien.damap.rest;

import at.ac.tuwien.damap.TestSetup;
import at.ac.tuwien.damap.enums.EFunctionRole;
import at.ac.tuwien.damap.rest.access.domain.AccessDO;
import at.ac.tuwien.damap.rest.access.service.AccessService;
import at.ac.tuwien.damap.rest.dmp.domain.ContributorDO;
import at.ac.tuwien.damap.rest.dmp.domain.DmpDO;
import at.ac.tuwien.damap.rest.persons.MockUniversityPersonServiceImpl;
import at.ac.tuwien.damap.rest.persons.PersonService;
import at.ac.tuwien.damap.security.SecurityService;
import at.ac.tuwien.damap.util.TestDOFactory;
import io.quarkus.test.common.http.TestHTTPEndpoint;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.mockito.InjectMock;
import io.quarkus.test.security.TestSecurity;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

Expand All @@ -22,34 +16,13 @@

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.mockito.ArgumentMatchers.any;

@QuarkusTest
@TestHTTPEndpoint(AccessResource.class)
class AccessResourceTest {

@Inject
TestDOFactory testDOFactory;
class AccessResourceTest extends TestSetup {
@Inject
AccessService accessService;

@InjectMock
SecurityService securityService;

@InjectMock
MockUniversityPersonServiceImpl personService;


DmpDO dmpDO;

@BeforeEach
public void setup() {
Mockito.when(securityService.getUserId()).thenReturn("012345");
Mockito.when(securityService.getUserName()).thenReturn("testUser");
Mockito.when(personService.read(any(String.class))).thenReturn(testDOFactory.getTestContributorDO());
dmpDO = testDOFactory.getOrCreateTestDmpDO();
}

@Test
@TestSecurity(user = "userJwt", roles = "user")
void testGetAccessList_Valid() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,32 @@
package at.ac.tuwien.damap.rest;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.is;

import javax.inject.Inject;
import javax.ws.rs.core.MediaType;

import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import at.ac.tuwien.damap.TestSetup;
import at.ac.tuwien.damap.rest.dmp.domain.DmpDO;
import at.ac.tuwien.damap.rest.version.VersionService;
import at.ac.tuwien.damap.security.SecurityService;
import at.ac.tuwien.damap.util.TestDOFactory;
import io.quarkus.test.common.http.TestHTTPEndpoint;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.mockito.InjectMock;
import io.quarkus.test.security.TestSecurity;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import javax.inject.Inject;
import javax.ws.rs.core.MediaType;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.is;

@QuarkusTest
@TestHTTPEndpoint(DataManagementPlanResource.class)
class DataManagementPlanResourceTest {
class DataManagementPlanResourceTest extends TestSetup {

@Inject
TestDOFactory testDOFactory;

@Inject
VersionService versionService;

@InjectMock
SecurityService securityService;

@BeforeEach
public void setup() {
Mockito.when(securityService.getUserId()).thenReturn("012345");
Mockito.when(securityService.getUserName()).thenReturn("testUser");
}

@Test
void testGetAllPlansEndpoint_Invalid() {
given()
Expand Down Expand Up @@ -216,7 +205,9 @@ void testGetDmpByIdAndRevisionlanEndpoint_Valid() {
testDOFactory.getOrCreateTestVersionDO();
given()
.contentType(MediaType.APPLICATION_JSON)
.when().get("/" + dmpDO.getId() + "/" + versionService.getDmpVersions(dmpDO.getId()).get(0).getRevisionNumber())
.when()
.get("/" + dmpDO.getId() + "/"
+ versionService.getDmpVersions(dmpDO.getId()).get(0).getRevisionNumber())
.then()
.statusCode(200);
}
Expand Down
Loading

0 comments on commit 5c3d324

Please sign in to comment.