|
| 1 | +package org.folio.dew.batch.eholdings; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 6 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 7 | +import java.util.Collections; |
| 8 | +import java.util.List; |
| 9 | +import java.util.stream.Stream; |
| 10 | +import lombok.SneakyThrows; |
| 11 | +import org.folio.de.entity.EHoldingsPackage; |
| 12 | +import org.folio.dew.domain.dto.eholdings.ContentTypeEnum; |
| 13 | +import org.folio.dew.domain.dto.eholdings.Coverage; |
| 14 | +import org.folio.dew.domain.dto.eholdings.EHoldingsResourceDTO; |
| 15 | +import org.folio.dew.domain.dto.eholdings.EPackage; |
| 16 | +import org.folio.dew.domain.dto.eholdings.EmbargoPeriod; |
| 17 | +import org.folio.dew.domain.dto.eholdings.PackageAltName; |
| 18 | +import org.folio.dew.domain.dto.eholdings.PackageAttributes; |
| 19 | +import org.folio.dew.domain.dto.eholdings.PackageData; |
| 20 | +import org.folio.dew.domain.dto.eholdings.PackageVisibility; |
| 21 | +import org.folio.dew.domain.dto.eholdings.PackageVisibility.CategoryEnum; |
| 22 | +import org.folio.dew.domain.dto.eholdings.Proxy; |
| 23 | +import org.folio.dew.domain.dto.eholdings.PublicationType; |
| 24 | +import org.folio.dew.domain.dto.eholdings.ResourcesAttributes; |
| 25 | +import org.folio.dew.domain.dto.eholdings.ResourcesData; |
| 26 | +import org.folio.dew.domain.dto.eholdings.VisibilityData; |
| 27 | +import org.jspecify.annotations.NonNull; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.junit.jupiter.params.ParameterizedTest; |
| 30 | +import org.junit.jupiter.params.provider.MethodSource; |
| 31 | +import org.junit.jupiter.params.provider.NullAndEmptySource; |
| 32 | +import org.junit.jupiter.params.provider.NullSource; |
| 33 | + |
| 34 | +class EHoldingsToExportFormatMapperTest { |
| 35 | + |
| 36 | + private final EHoldingsToExportFormatMapper mapper = new EHoldingsToExportFormatMapper(); |
| 37 | + private static final ObjectMapper objectMapper = new ObjectMapper() |
| 38 | + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 39 | + |
| 40 | + @Test |
| 41 | + void shouldMapPackageAccessToPublicWhenFreeAccess() { |
| 42 | + var result = mapper.convertToExportFormat(buildPackage(true, null, null, null)); |
| 43 | + assertThat(result.getPackageAccess()).isEqualTo("Public"); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void shouldMapPackageAccessToControlledWhenNotFreeAccess() { |
| 48 | + var result = mapper.convertToExportFormat(buildPackage(false, null, null, null)); |
| 49 | + assertThat(result.getPackageAccess()).isEqualTo("Controlled"); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void shouldMapAltNamesJoinedWithSemicolon() { |
| 54 | + var alt1 = new PackageAltName(); |
| 55 | + alt1.setAltName("Alt 1"); |
| 56 | + var alt2 = new PackageAltName(); |
| 57 | + alt2.setAltName("Alt 2"); |
| 58 | + var alt3 = new PackageAltName(); |
| 59 | + alt3.setAltName("Alt 3"); |
| 60 | + var alt4 = new PackageAltName(); |
| 61 | + alt4.setAltName("Alt 4"); |
| 62 | + |
| 63 | + var result = mapper.convertToExportFormat(buildPackage(null, List.of(alt1, alt2), List.of(alt3, alt4), null)); |
| 64 | + |
| 65 | + assertThat(result.getCustomAlternativeNames()).isEqualTo("Alt 1; Alt 2"); |
| 66 | + assertThat(result.getManagedAlternativeNames()).isEqualTo("Alt 3; Alt 4"); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void shouldMapVisibilityByCategoryWithReasonAppended() { |
| 71 | + var visibility = List.of( |
| 72 | + buildVisibility(CategoryEnum.PF, true, "Set by System"), |
| 73 | + buildVisibility(CategoryEnum.FTF, false, null), |
| 74 | + buildVisibility(CategoryEnum.MARC, true, "because") |
| 75 | + ); |
| 76 | + |
| 77 | + var result = mapper.convertToExportFormat(buildPackage(null, null, null, visibility)); |
| 78 | + |
| 79 | + assertThat(result.getHideInPublicationFinder()).isEqualTo("Yes (Set by System)"); |
| 80 | + assertThat(result.getHideInFullTextFinder()).isEqualTo("No"); |
| 81 | + assertThat(result.getExcludeFromMARCExport()).isEqualTo("Yes (because)"); |
| 82 | + } |
| 83 | + |
| 84 | + @ParameterizedTest |
| 85 | + @NullAndEmptySource |
| 86 | + @SuppressWarnings("unchecked") |
| 87 | + void shouldReturnEmptyForNullOrEmptyInput(List<?> list) { |
| 88 | + var result = mapper.convertToExportFormat( |
| 89 | + buildPackage(null, (List<PackageAltName>) list, (List<PackageAltName>) list, (List<PackageVisibility>) list)); |
| 90 | + |
| 91 | + assertThat(result.getManagedAlternativeNames()).isEmpty(); |
| 92 | + assertThat(result.getCustomAlternativeNames()).isEmpty(); |
| 93 | + assertThat(result.getPackageAccess()).isEmpty(); |
| 94 | + assertThat(result.getHideInPublicationFinder()).isEmpty(); |
| 95 | + assertThat(result.getHideInFullTextFinder()).isEmpty(); |
| 96 | + assertThat(result.getExcludeFromMARCExport()).isEmpty(); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void shouldMapProxiedUrl() { |
| 101 | + var proxy = new Proxy(); |
| 102 | + proxy.setId("ezproxy"); |
| 103 | + proxy.setProxiedUrl("https://proxy.example.com"); |
| 104 | + |
| 105 | + var result = mapper.convertToExportFormat(buildMinimalResource(proxy)); |
| 106 | + |
| 107 | + assertThat(result.getTitleProxiedUrl()).isEqualTo("https://proxy.example.com"); |
| 108 | + } |
| 109 | + |
| 110 | + @ParameterizedTest |
| 111 | + @NullSource |
| 112 | + @MethodSource("nullProxiedUrlProxy") |
| 113 | + void shouldReturnEmptyProxiedUrlForNullOrMissingUrl(Proxy proxy) { |
| 114 | + assertThat(mapper.convertToExportFormat(buildMinimalResource(proxy)).getTitleProxiedUrl()).isEmpty(); |
| 115 | + } |
| 116 | + |
| 117 | + private static Stream<Proxy> nullProxiedUrlProxy() { |
| 118 | + return Stream.of(new Proxy()); // proxy present but proxiedUrl is null |
| 119 | + } |
| 120 | + |
| 121 | + private EHoldingsResourceDTO buildMinimalResource(Proxy proxy) { |
| 122 | + var embargo = new EmbargoPeriod(); |
| 123 | + embargo.setEmbargoValue(0); |
| 124 | + |
| 125 | + var visibility = new VisibilityData(); |
| 126 | + visibility.setIsHidden(false); |
| 127 | + |
| 128 | + var attrs = getResourcesAttributes(proxy, embargo, visibility); |
| 129 | + |
| 130 | + var data = new ResourcesData(); |
| 131 | + data.setAttributes(attrs); |
| 132 | + data.setIncluded(Collections.emptyList()); |
| 133 | + |
| 134 | + return EHoldingsResourceDTO.builder() |
| 135 | + .resourcesData(data) |
| 136 | + .notes(Collections.emptyList()) |
| 137 | + .agreements(Collections.emptyList()) |
| 138 | + .build(); |
| 139 | + } |
| 140 | + |
| 141 | + private static @NonNull ResourcesAttributes getResourcesAttributes(Proxy proxy, EmbargoPeriod embargo, |
| 142 | + VisibilityData visibility) { |
| 143 | + var attrs = new ResourcesAttributes(); |
| 144 | + attrs.setTitleId(1); |
| 145 | + attrs.setIsSelected(false); |
| 146 | + attrs.setIsTitleCustom(false); |
| 147 | + attrs.setPublicationType(PublicationType.BOOK); |
| 148 | + attrs.setAlternateTitles(Collections.emptyList()); |
| 149 | + attrs.setContributors(Collections.emptyList()); |
| 150 | + attrs.setIdentifiers(Collections.emptyList()); |
| 151 | + attrs.setManagedCoverages(Collections.emptyList()); |
| 152 | + attrs.setCustomCoverages(Collections.emptyList()); |
| 153 | + attrs.setManagedEmbargoPeriod(embargo); |
| 154 | + attrs.setCustomEmbargoPeriod(embargo); |
| 155 | + attrs.setVisibilityData(visibility); |
| 156 | + attrs.setSubjects(Collections.emptyList()); |
| 157 | + attrs.setProxy(proxy); |
| 158 | + return attrs; |
| 159 | + } |
| 160 | + |
| 161 | + private PackageVisibility buildVisibility(CategoryEnum category, boolean hidden, String reason) { |
| 162 | + var vis = new PackageVisibility(); |
| 163 | + vis.setCategory(category); |
| 164 | + vis.setHidden(hidden); |
| 165 | + vis.setReason(reason); |
| 166 | + return vis; |
| 167 | + } |
| 168 | + |
| 169 | + @SneakyThrows |
| 170 | + private EHoldingsPackage buildPackage(Boolean isFreeAccess, |
| 171 | + List<PackageAltName> customAltNames, |
| 172 | + List<PackageAltName> managedAltNames, |
| 173 | + List<PackageVisibility> visibility) { |
| 174 | + var attrs = getPackageAttributes(isFreeAccess, customAltNames, managedAltNames, visibility); |
| 175 | + |
| 176 | + var packageData = new PackageData(); |
| 177 | + packageData.setId("1-22"); |
| 178 | + packageData.setType("packages"); |
| 179 | + packageData.setAttributes(attrs); |
| 180 | + |
| 181 | + var ePackage = new EPackage(); |
| 182 | + ePackage.setData(packageData); |
| 183 | + ePackage.setIncluded(Collections.emptyList()); |
| 184 | + |
| 185 | + var entity = new EHoldingsPackage(); |
| 186 | + entity.setId("1-22"); |
| 187 | + entity.setJobExecutionId(1L); |
| 188 | + entity.setEPackage(objectMapper.writeValueAsString(ePackage)); |
| 189 | + entity.setEProvider("null"); |
| 190 | + entity.setAgreements("[]"); |
| 191 | + entity.setNotes("[]"); |
| 192 | + return entity; |
| 193 | + } |
| 194 | + |
| 195 | + private static @NonNull PackageAttributes getPackageAttributes(Boolean isFreeAccess, List<PackageAltName> customAltNames, |
| 196 | + List<PackageAltName> managedAltNames, List<PackageVisibility> visibility) { |
| 197 | + var coverage = new Coverage(); |
| 198 | + coverage.setBeginCoverage(""); |
| 199 | + coverage.setEndCoverage(""); |
| 200 | + |
| 201 | + var proxy = new Proxy(); |
| 202 | + proxy.setId("<n>"); |
| 203 | + |
| 204 | + var attrs = new PackageAttributes(); |
| 205 | + attrs.setContentType(ContentTypeEnum.UNKNOWN); |
| 206 | + attrs.setCustomCoverage(coverage); |
| 207 | + attrs.setProxy(proxy); |
| 208 | + attrs.setIsSelected(false); |
| 209 | + attrs.setIsFreeAccess(isFreeAccess); |
| 210 | + attrs.setManagedAltNames(managedAltNames); |
| 211 | + attrs.setCustomAltNames(customAltNames); |
| 212 | + attrs.setVisibility(visibility); |
| 213 | + return attrs; |
| 214 | + } |
| 215 | +} |
0 commit comments