Skip to content

Commit

Permalink
MDEXP-641 Refactored tests
Browse files Browse the repository at this point in the history
  • Loading branch information
obozhko-folio committed Feb 16, 2024
1 parent 92f6792 commit ba37e16
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

import static java.lang.String.format;
Expand Down
118 changes: 4 additions & 114 deletions src/test/java/org/folio/dataexp/service/DataExportAllServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,13 @@
import com.github.jknack.handlebars.internal.Files;
import lombok.SneakyThrows;
import org.codehaus.plexus.util.StringUtils;
import org.folio.dataexp.BaseDataExportInitializer;
import org.folio.dataexp.client.AlternativeTitleTypesClient;
import org.folio.dataexp.client.CallNumberTypesClient;
import org.folio.dataexp.client.ContributorNameTypesClient;
import org.folio.dataexp.client.ElectronicAccessRelationshipsClient;
import org.folio.dataexp.client.HoldingsNoteTypesClient;
import org.folio.dataexp.client.IdentifierTypesClient;
import org.folio.dataexp.client.InstanceFormatsClient;
import org.folio.dataexp.client.InstanceTypesClient;
import org.folio.dataexp.client.IssuanceModesClient;
import org.folio.dataexp.client.ItemNoteTypesClient;
import org.folio.dataexp.client.LoanTypesClient;
import org.folio.dataexp.client.LocationUnitsClient;
import org.folio.dataexp.client.LocationsClient;
import org.folio.dataexp.client.MaterialTypesClient;
import org.folio.dataexp.client.NatureOfContentTermsClient;
import org.folio.dataexp.client.UserClient;
import org.folio.dataexp.domain.dto.AlternativeDataTypes;
import org.folio.dataexp.domain.dto.CallNumberTypes;
import org.folio.dataexp.domain.dto.Campuses;
import org.folio.dataexp.domain.dto.ContributorNameTypes;
import org.folio.dataexp.domain.dto.ElectronicAccessRelationships;
import org.folio.dataexp.domain.dto.ExportAllRequest;
import org.folio.dataexp.domain.dto.HoldingsNoteTypes;
import org.folio.dataexp.domain.dto.IdentifierTypes;
import org.folio.dataexp.domain.dto.InstanceFormats;
import org.folio.dataexp.domain.dto.InstanceTypes;
import org.folio.dataexp.domain.dto.Institutions;
import org.folio.dataexp.domain.dto.IssuanceModes;
import org.folio.dataexp.domain.dto.ItemNoteTypes;
import org.folio.dataexp.domain.dto.JobExecution;
import org.folio.dataexp.domain.dto.JobProfile;
import org.folio.dataexp.domain.dto.Libraries;
import org.folio.dataexp.domain.dto.LoanTypes;
import org.folio.dataexp.domain.dto.Locations;
import org.folio.dataexp.domain.dto.MappingProfile;
import org.folio.dataexp.domain.dto.MaterialTypes;
import org.folio.dataexp.domain.dto.RecordTypes;
import org.folio.dataexp.domain.dto.User;
import org.folio.dataexp.domain.entity.JobProfileEntity;
import org.folio.dataexp.domain.entity.MappingProfileEntity;
import org.folio.dataexp.repository.ErrorLogEntityCqlRepository;
import org.folio.dataexp.repository.JobExecutionEntityCqlRepository;
import org.folio.dataexp.repository.JobExecutionExportFilesEntityRepository;
import org.folio.dataexp.repository.JobProfileEntityRepository;
import org.folio.dataexp.repository.MappingProfileEntityRepository;
Expand All @@ -54,72 +18,24 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;

import java.nio.charset.Charset;
import java.util.List;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

class DataExportAllServiceTest extends BaseDataExportInitializer {
class DataExportAllServiceTest extends ServiceInitializer {

@Autowired
private DataExportAllService dataExportAllService;

@Autowired
protected JobExecutionEntityCqlRepository jobExecutionEntityCqlRepository;

@Autowired
private JobProfileEntityRepository jobProfileEntityRepository;

@Autowired
private MappingProfileEntityRepository mappingProfileEntityRepository;

@Autowired
protected DataExportTenantService dataExportTenantService;

@Autowired
protected ErrorLogEntityCqlRepository errorLogEntityCqlRepository;

private JobExecutionExportFilesEntityRepository jobExecutionExportFilesEntityRepository;
@Autowired
protected JobExecutionExportFilesEntityRepository jobExecutionExportFilesEntityRepository;

@MockBean
private UserClient userClient;
@MockBean
private AlternativeTitleTypesClient alternativeTitleTypesClient;
@MockBean
private CallNumberTypesClient callNumberTypesClient;
@MockBean
private ContributorNameTypesClient contributorNameTypesClient;
@MockBean
private ElectronicAccessRelationshipsClient electronicAccessRelationshipsClient;
@MockBean
private HoldingsNoteTypesClient holdingsNoteTypesClient;
@MockBean
private IdentifierTypesClient identifierTypesClient;
@MockBean
private InstanceFormatsClient instanceFormatsClient;
@MockBean
private InstanceTypesClient instanceTypesClient;
@MockBean
private ItemNoteTypesClient itemNoteTypesClient;
@MockBean
private LoanTypesClient loanTypesClient;
@MockBean
private LocationsClient locationsClient;
@MockBean
private LocationUnitsClient locationUnitsClient;
@MockBean
private MaterialTypesClient materialTypesClient;
@MockBean
private NatureOfContentTermsClient natureOfContentTermsClient;
@MockBean
private IssuanceModesClient issuanceModesClient;
private JobProfileEntityRepository jobProfileEntityRepository;

private static final UUID CUSTOM_INSTANCE_MAPPING_PROFILE_ID = UUID.randomUUID();
private static final UUID CUSTOM_HOLDINGS_MAPPING_PROFILE_ID = UUID.randomUUID();
Expand Down Expand Up @@ -995,32 +911,6 @@ void exportAllAuthorityNonDeletedNoErrorsTest(boolean suppressedFromDiscovery) {
}
}

protected void handleReferenceData() {
var user = new User();
var personal = new User.Personal();
personal.setFirstName("testuser");
personal.setLastName("testuser");
user.setPersonal(personal);
when(userClient.getUserById(any(String.class))).thenReturn(user);
when(alternativeTitleTypesClient.getAlternativeTitleTypes(any(Long.class))).thenReturn(new AlternativeDataTypes());
when(callNumberTypesClient.getCallNumberTypes(any(Long.class))).thenReturn(new CallNumberTypes());
when(contributorNameTypesClient.getContributorNameTypes(any(Long.class))).thenReturn(new ContributorNameTypes());
when(electronicAccessRelationshipsClient.getElectronicAccessRelationships(any(Long.class))).thenReturn(new ElectronicAccessRelationships());
when(holdingsNoteTypesClient.getHoldingsNoteTypes(any(Long.class))).thenReturn(new HoldingsNoteTypes());
when(identifierTypesClient.getIdentifierTypes(any(Long.class))).thenReturn(new IdentifierTypes());
when(instanceFormatsClient.getInstanceFormats(any(Long.class))).thenReturn(new InstanceFormats());
when(instanceTypesClient.getInstanceTypes(any(Long.class))).thenReturn(new InstanceTypes());
when(itemNoteTypesClient.getItemNoteTypes(any(Long.class))).thenReturn(new ItemNoteTypes());
when(loanTypesClient.getLoanTypes(any(Long.class))).thenReturn(new LoanTypes());
when(locationsClient.getLocations(any(Long.class))).thenReturn(new Locations());
when(locationUnitsClient.getCampuses(any(Long.class))).thenReturn(new Campuses());
when(locationUnitsClient.getLibraries(any(Long.class))).thenReturn(new Libraries());
when(locationUnitsClient.getInstitutions(any(Long.class))).thenReturn(new Institutions());
when(materialTypesClient.getMaterialTypes(any(Long.class))).thenReturn(new MaterialTypes());
when(natureOfContentTermsClient.getNatureOfContentTerms(any(Long.class))).thenReturn(new org.folio.dataexp.domain.dto.NatureOfContentTerms());
when(issuanceModesClient.getIssuanceModes(any(Long.class))).thenReturn(new IssuanceModes());
}

private void createCustomInstanceJobProfile() {
var customInstanceMappingProfile = new MappingProfile().id(CUSTOM_INSTANCE_MAPPING_PROFILE_ID).name("Custom Instance Mapping Profile")
._default(false).recordTypes(List.of(RecordTypes.INSTANCE));
Expand All @@ -1035,7 +925,7 @@ private void createCustomInstanceJobProfile() {

private void createCustomHoldingsJobProfile() {
var customHoldingsMappingProfile = new MappingProfile().id(CUSTOM_HOLDINGS_MAPPING_PROFILE_ID).name("Custom Holdings Mapping Profile")
._default(false).recordTypes(List.of(RecordTypes.HOLDINGS));
._default(false).recordTypes(List.of(org.folio.dataexp.domain.dto.RecordTypes.HOLDINGS));
mappingProfileEntityRepository.save(new MappingProfileEntity().withMappingProfile(customHoldingsMappingProfile)
.withId(customHoldingsMappingProfile.getId()).withName(customHoldingsMappingProfile.getName()));
var customHoldingsJobProfile = new JobProfile().id(CUSTOM_HOLDINGS_JOB_PROFILE_ID).name("Custom Holdings Job Profile")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.github.jknack.handlebars.internal.Files;
import lombok.SneakyThrows;
import org.codehaus.plexus.util.StringUtils;
import org.folio.dataexp.domain.dto.JobExecution;
import org.folio.dataexp.domain.dto.QuickExportRequest;
import org.folio.dataexp.exception.export.DataExportRequestValidationException;
Expand All @@ -22,7 +23,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when;

class QuickExportServiceTest extends DataExportAllServiceTest {
class QuickExportServiceTest extends ServiceInitializer {

@Autowired
private QuickExportService quickExportService;
Expand Down Expand Up @@ -59,6 +60,7 @@ void quickExportNoErrorsTest(String recordType, String expectedId) {
String outputMrcFile = Files.read(s3Client.read(fileLocation), Charset.defaultCharset());

assertThat(outputMrcFile).containsOnlyOnce(expectedId);
assertThat(StringUtils.countMatches(outputMrcFile, "999")).isEqualTo(1);
}
}

Expand Down
98 changes: 98 additions & 0 deletions src/test/java/org/folio/dataexp/service/ServiceInitializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package org.folio.dataexp.service;

import org.folio.dataexp.BaseDataExportInitializer;
import org.folio.dataexp.client.*;
import org.folio.dataexp.domain.dto.*;
import org.folio.dataexp.domain.dto.AlternativeDataTypes;
import org.folio.dataexp.domain.dto.Campuses;
import org.folio.dataexp.domain.dto.ContributorNameTypes;
import org.folio.dataexp.domain.dto.ElectronicAccessRelationships;
import org.folio.dataexp.domain.dto.IdentifierTypes;
import org.folio.dataexp.domain.dto.InstanceFormats;
import org.folio.dataexp.domain.dto.InstanceTypes;
import org.folio.dataexp.domain.dto.Institutions;
import org.folio.dataexp.domain.dto.IssuanceModes;
import org.folio.dataexp.domain.dto.ItemNoteTypes;
import org.folio.dataexp.domain.dto.Libraries;
import org.folio.dataexp.domain.dto.LoanTypes;
import org.folio.dataexp.domain.dto.Locations;
import org.folio.dataexp.domain.dto.MaterialTypes;
import org.folio.dataexp.repository.ErrorLogEntityCqlRepository;
import org.folio.dataexp.repository.JobExecutionEntityCqlRepository;
import org.folio.dataexp.repository.JobExecutionExportFilesEntityRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

abstract class ServiceInitializer extends BaseDataExportInitializer {

@Autowired
protected DataExportTenantService dataExportTenantService;
@Autowired
protected ErrorLogEntityCqlRepository errorLogEntityCqlRepository;
@Autowired
protected JobExecutionEntityCqlRepository jobExecutionEntityCqlRepository;
@Autowired
protected JobExecutionExportFilesEntityRepository jobExecutionExportFilesEntityRepository;

@MockBean
private UserClient userClient;
@MockBean
private AlternativeTitleTypesClient alternativeTitleTypesClient;
@MockBean
private CallNumberTypesClient callNumberTypesClient;
@MockBean
private ContributorNameTypesClient contributorNameTypesClient;
@MockBean
private ElectronicAccessRelationshipsClient electronicAccessRelationshipsClient;
@MockBean
private HoldingsNoteTypesClient holdingsNoteTypesClient;
@MockBean
private IdentifierTypesClient identifierTypesClient;
@MockBean
private InstanceFormatsClient instanceFormatsClient;
@MockBean
private InstanceTypesClient instanceTypesClient;
@MockBean
private ItemNoteTypesClient itemNoteTypesClient;
@MockBean
private LoanTypesClient loanTypesClient;
@MockBean
private LocationsClient locationsClient;
@MockBean
private LocationUnitsClient locationUnitsClient;
@MockBean
private MaterialTypesClient materialTypesClient;
@MockBean
private NatureOfContentTermsClient natureOfContentTermsClient;
@MockBean
private IssuanceModesClient issuanceModesClient;

protected void handleReferenceData() {
var user = new User();
var personal = new User.Personal();
personal.setFirstName("testuser");
personal.setLastName("testuser");
user.setPersonal(personal);
when(userClient.getUserById(any(String.class))).thenReturn(user);
when(alternativeTitleTypesClient.getAlternativeTitleTypes(any(Long.class))).thenReturn(new AlternativeDataTypes());
when(callNumberTypesClient.getCallNumberTypes(any(Long.class))).thenReturn(new org.folio.dataexp.domain.dto.CallNumberTypes());
when(contributorNameTypesClient.getContributorNameTypes(any(Long.class))).thenReturn(new ContributorNameTypes());
when(electronicAccessRelationshipsClient.getElectronicAccessRelationships(any(Long.class))).thenReturn(new ElectronicAccessRelationships());
when(holdingsNoteTypesClient.getHoldingsNoteTypes(any(Long.class))).thenReturn(new org.folio.dataexp.domain.dto.HoldingsNoteTypes());
when(identifierTypesClient.getIdentifierTypes(any(Long.class))).thenReturn(new IdentifierTypes());
when(instanceFormatsClient.getInstanceFormats(any(Long.class))).thenReturn(new InstanceFormats());
when(instanceTypesClient.getInstanceTypes(any(Long.class))).thenReturn(new InstanceTypes());
when(itemNoteTypesClient.getItemNoteTypes(any(Long.class))).thenReturn(new ItemNoteTypes());
when(loanTypesClient.getLoanTypes(any(Long.class))).thenReturn(new LoanTypes());
when(locationsClient.getLocations(any(Long.class))).thenReturn(new Locations());
when(locationUnitsClient.getCampuses(any(Long.class))).thenReturn(new Campuses());
when(locationUnitsClient.getLibraries(any(Long.class))).thenReturn(new Libraries());
when(locationUnitsClient.getInstitutions(any(Long.class))).thenReturn(new Institutions());
when(materialTypesClient.getMaterialTypes(any(Long.class))).thenReturn(new MaterialTypes());
when(natureOfContentTermsClient.getNatureOfContentTerms(any(Long.class))).thenReturn(new org.folio.dataexp.domain.dto.NatureOfContentTerms());
when(issuanceModesClient.getIssuanceModes(any(Long.class))).thenReturn(new IssuanceModes());
}
}

0 comments on commit ba37e16

Please sign in to comment.