|
| 1 | +package org.folio.rest.converter.packages; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 6 | + |
| 7 | +import java.util.List; |
| 8 | +import org.folio.holdingsiq.model.CoverageDates; |
| 9 | +import org.folio.holdingsiq.model.PackageData; |
| 10 | +import org.folio.holdingsiq.model.SubjectAssociation; |
| 11 | +import org.folio.rest.jaxrs.model.PackageCollectionItem; |
| 12 | +import org.folio.spring.config.TestConfig; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 15 | +import org.springframework.beans.factory.annotation.Autowired; |
| 16 | +import org.springframework.core.convert.ConversionService; |
| 17 | +import org.springframework.test.context.ContextConfiguration; |
| 18 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 19 | + |
| 20 | +@ExtendWith(SpringExtension.class) |
| 21 | +@ContextConfiguration(classes = TestConfig.class) |
| 22 | +class PackageCollectionItemConverterTest { |
| 23 | + |
| 24 | + @Autowired |
| 25 | + private ConversionService conversionService; |
| 26 | + |
| 27 | + @Test |
| 28 | + void shouldKeepManagedAndExplicitCustomSubjectsAndDropCustomPlaceholders() { |
| 29 | + var packageData = PackageData.builder() |
| 30 | + .packageId(1) |
| 31 | + .vendorId(2) |
| 32 | + .contentType("EJournal") |
| 33 | + .customCoverage(CoverageDates.builder().beginCoverage("").endCoverage("").build()) |
| 34 | + .subjectAssociations(List.of( |
| 35 | + new SubjectAssociation(1, 3, "Science", false, false, "Science", null), |
| 36 | + new SubjectAssociation(2, 3, "Biology", true, false, "Science -- Biology", 1), |
| 37 | + new SubjectAssociation(3, 3, "Genetics", true, true, "Science -- Biology -- Genetics", 2), |
| 38 | + new SubjectAssociation(4, 3, "Botany", true, true, "Science -- Botany", 1))) |
| 39 | + .build(); |
| 40 | + |
| 41 | + var subjects = convert(packageData).getAttributes().getSubjects(); |
| 42 | + |
| 43 | + assertNotNull(subjects); |
| 44 | + assertEquals(List.of("Science", "Genetics", "Botany"), |
| 45 | + subjects.stream().map(s -> s.getName()).toList()); |
| 46 | + var genetics = subjects.get(1); |
| 47 | + assertEquals(3, genetics.getId()); |
| 48 | + assertEquals(3, genetics.getSchemaId()); |
| 49 | + assertEquals(2, genetics.getParentId()); |
| 50 | + assertEquals(true, genetics.getIsCustom()); |
| 51 | + assertEquals("Science -- Biology -- Genetics", genetics.getSubjectHierarchy()); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void shouldTreatNullIsCustomAsManaged() { |
| 56 | + var packageData = PackageData.builder() |
| 57 | + .packageId(1) |
| 58 | + .vendorId(2) |
| 59 | + .contentType("EJournal") |
| 60 | + .customCoverage(CoverageDates.builder().beginCoverage("").endCoverage("").build()) |
| 61 | + .subjectAssociations(List.of(new SubjectAssociation(1, 3, "Science", null, null, null, null))) |
| 62 | + .build(); |
| 63 | + |
| 64 | + var subjects = convert(packageData).getAttributes().getSubjects(); |
| 65 | + |
| 66 | + assertEquals(1, subjects.size()); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void shouldReturnNullSubjectsWhenNoSubjectAssociations() { |
| 71 | + var packageData = PackageData.builder().packageId(1).vendorId(2) |
| 72 | + .contentType("EJournal") |
| 73 | + .customCoverage(CoverageDates.builder().beginCoverage("").endCoverage("").build()).build(); |
| 74 | + |
| 75 | + assertNull(convert(packageData).getAttributes().getSubjects()); |
| 76 | + } |
| 77 | + |
| 78 | + private PackageCollectionItem convert(PackageData packageData) { |
| 79 | + var item = conversionService.convert(packageData, PackageCollectionItem.class); |
| 80 | + assertNotNull(item); |
| 81 | + return item; |
| 82 | + } |
| 83 | +} |
0 commit comments