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 Mar 4, 2024
1 parent ed0f2de commit 8316c75
Show file tree
Hide file tree
Showing 12 changed files with 7,690 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,13 @@ private void fetchORCIDContributorInfo(Dmp dmp) {
var identifier = contributor.getPersonIdentifier();
if (identifier != null
&& identifier.getIdentifierType().equals(EIdentifierType.ORCID)) {
ContributorDOMapper.mapDOtoEntity(orcidPersonService.read(identifier.getIdentifier()), contributor);
try {
ContributorDOMapper.mapDOtoEntity(orcidPersonService.read(identifier.getIdentifier()), contributor);
} catch (Exception e) {
log.warn(String.format(
"Could not fetch ORCID or map contributor info for identifier '%s'.%nDetail error message: %s",
identifier.getIdentifier(), e));
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,14 @@ public ContributorDO mapRecordEntityToPersonDO(ORCIDRecord orcidRecord,
contributorDO.setFirstName(orcidRecord.getPerson().getName().getGivenNames().getValue());
contributorDO.setLastName(orcidRecord.getPerson().getName().getFamilyName().getValue());

var primaryMail = orcidRecord.getPerson().getEmails().getEmail().stream().filter(ORCIDEmail::isPrimary)
.findFirst();
contributorDO.setMbox(primaryMail.isPresent() ? primaryMail.get().getEmail() : null);
var emails = orcidRecord.getPerson().getEmails().getEmail();
if (!emails.isEmpty()) {
var primaryMail = emails.stream().filter(ORCIDEmail::isPrimary)
.findFirst();

String mail = primaryMail.isPresent() ? primaryMail.get().getEmail() : emails.get(0).getEmail();
contributorDO.setMbox(mail);
}

List<ORCIDAffiliation> affiliations = new ArrayList<>();

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
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(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 determinteDatasetIDsTest() {
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 8316c75

Please sign in to comment.