From 33c81291cd6c540f7bc10b1557ee165a77cff4e6 Mon Sep 17 00:00:00 2001 From: RomanChernetskyi <111740564+RomanChernetskyi@users.noreply.github.com> Date: Mon, 30 Sep 2024 16:38:50 +0300 Subject: [PATCH] [MODDICORE-411] Map accepted values from Mapping params (#357) --- pom.xml | 2 +- .../processing/mapping/MappingManager.java | 108 ----- .../reader/record/marc/MarcRecordReader.java | 23 +- .../mapper/util/AcceptedValuesUtil.java | 155 +++++++ .../reader/util/MatchIdProcessorUtil.java | 6 +- .../processing/events/EventManagerTest.java | 8 +- .../publisher/KafkaEventPublisherTest.java | 12 +- .../manager/MappingManagerUnitTest.java | 198 +-------- .../mapping/mapper/HoldingsMapperTest.java | 284 +++++++------ .../reader/AcceptedValuesUtilTest.java | 167 ++++++++ .../reader/MarcRecordReaderUnitTest.java | 397 ++++++++---------- 11 files changed, 700 insertions(+), 660 deletions(-) create mode 100644 src/main/java/org/folio/processing/mapping/mapper/util/AcceptedValuesUtil.java create mode 100644 src/test/java/org/folio/processing/mapping/reader/AcceptedValuesUtilTest.java diff --git a/pom.xml b/pom.xml index cdf7caac..02403bb1 100644 --- a/pom.xml +++ b/pom.xml @@ -170,7 +170,7 @@ net.mguenther.kafka kafka-junit - 2.5.0 + 3.6.0 test diff --git a/src/main/java/org/folio/processing/mapping/MappingManager.java b/src/main/java/org/folio/processing/mapping/MappingManager.java index 62082e15..60505ccc 100644 --- a/src/main/java/org/folio/processing/mapping/MappingManager.java +++ b/src/main/java/org/folio/processing/mapping/MappingManager.java @@ -4,11 +4,8 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.folio.DataImportEventPayload; -import org.folio.Location; import org.folio.MappingProfile; -import org.folio.Organization; import org.folio.processing.exceptions.MappingException; -import org.folio.processing.mapping.defaultmapper.processor.parameters.MappingParameters; import org.folio.processing.mapping.mapper.FactoryRegistry; import org.folio.processing.mapping.mapper.Mapper; import org.folio.processing.mapping.mapper.MappingContext; @@ -17,12 +14,9 @@ import org.folio.processing.mapping.mapper.reader.ReaderFactory; import org.folio.processing.mapping.mapper.writer.Writer; import org.folio.processing.mapping.mapper.writer.WriterFactory; -import org.folio.rest.jaxrs.model.MappingRule; import org.folio.rest.jaxrs.model.ProfileSnapshotWrapper; -import java.util.HashMap; import java.util.Map; -import java.util.stream.Collectors; import static org.folio.rest.jaxrs.model.ProfileType.MAPPING_PROFILE; @@ -38,12 +32,6 @@ public final class MappingManager { private static final Logger LOGGER = LogManager.getLogger(MappingManager.class); private static final FactoryRegistry FACTORY_REGISTRY = new FactoryRegistry(); - public static final String PERMANENT_LOCATION_ID = "permanentLocationId"; - public static final String TEMPORARY_LOCATION_ID = "temporaryLocationId"; - public static final String VENDOR_ID = "vendor"; - private static final String MATERIAL_SUPPLIER = "materialSupplier"; - private static final String ACCESS_PROVIDER = "accessProvider"; - private static final String DONOR_ORGANIZATION_IDS = "donorOrganizationIds"; private MappingManager() { } @@ -71,10 +59,6 @@ public static DataImportEventPayload map(DataImportEventPayload eventPayload, Ma mappingProfile = (MappingProfile) mappingProfileWrapper.getContent(); } - //Fix MODDICORE-128 (The system doesn't update acceptedLocation in mapping profiles after the location list is changed) - updateLocationsInMappingProfile(mappingProfile, mappingContext.getMappingParameters()); - updateOrganizationsInMappingProfile(mappingProfile, mappingContext.getMappingParameters()); - Reader reader = FACTORY_REGISTRY.createReader(mappingProfile.getIncomingRecordType()); Writer writer = FACTORY_REGISTRY.createWriter(mappingProfile.getExistingRecordType()); @@ -86,98 +70,6 @@ public static DataImportEventPayload map(DataImportEventPayload eventPayload, Ma } } - /** - * Fill Permanent and Temporary Locations in MappingProfile from {@code mappingParameters} - * - * @param mappingProfile - MappingProfile - * @param mappingParameters - mapping parameters - */ - private static void updateLocationsInMappingProfile(MappingProfile mappingProfile, MappingParameters mappingParameters) { - if ((mappingProfile.getMappingDetails() != null) && (mappingProfile.getMappingDetails().getMappingFields() != null)) { - HashMap locations = getLocationsFromMappingParameters(mappingParameters); - if (!locations.isEmpty()) { - for (MappingRule mappingRule : mappingProfile.getMappingDetails().getMappingFields()) { - if ((mappingRule.getName() != null) && (mappingRule.getName().equals(PERMANENT_LOCATION_ID) || mappingRule.getName().equals(TEMPORARY_LOCATION_ID))) { - mappingRule.setAcceptedValues(locations); - } - } - } - } - } - - /** - * Fill {@link Organization} accepted values in MappingProfile from {@code mappingParameters} - * - * @param mappingProfile - MappingProfile - * @param mappingParameters - mapping parameters - */ - private static void updateOrganizationsInMappingProfile(MappingProfile mappingProfile, MappingParameters mappingParameters) { - if (mappingParameters.getOrganizations() == null) { - return; - } - - if ((mappingProfile.getMappingDetails() != null) && (mappingProfile.getMappingDetails().getMappingFields() != null)) { - HashMap organizations = getOrganizationsFromMappingParameters(mappingParameters); - HashMap donorOrganizations = getDonorOrganizationsFromMappingParameters(mappingParameters); - if (!organizations.isEmpty()) { - for (MappingRule mappingRule : mappingProfile.getMappingDetails().getMappingFields()) { - if (isOrganizationsAcceptedValuesNeeded(mappingRule)) { - mappingRule.setAcceptedValues(organizations); - } else if (DONOR_ORGANIZATION_IDS.equals(mappingRule.getName())) { - populateDonorOrganizations(mappingRule, donorOrganizations); - } - } - } - } - } - - private static HashMap getLocationsFromMappingParameters(MappingParameters mappingParameters) { - HashMap locations = new HashMap<>(); - for (Location location : mappingParameters.getLocations()) { - StringBuilder locationValue = new StringBuilder() - .append(location.getName()) - .append(" (") - .append(location.getCode()) - .append(")"); - locations.put(location.getId(), String.valueOf(locationValue)); - } - return locations; - } - - private static HashMap getOrganizationsFromMappingParameters(MappingParameters mappingParameters) { - HashMap organizations = new HashMap<>(); - for (Organization organization : mappingParameters.getOrganizations()) { - organizations.put(organization.getId(), formatOrganizationValue(organization)); - } - return organizations; - } - - private static HashMap getDonorOrganizationsFromMappingParameters(MappingParameters mappingParameters) { - return mappingParameters.getOrganizations().stream() - .filter(organization -> Boolean.TRUE.equals(organization.getIsDonor())) - .collect(Collectors.toMap(Organization::getId, MappingManager::formatOrganizationValue, (a, b) -> b, HashMap::new)); - } - - private static String formatOrganizationValue(Organization organization) { - return new StringBuilder() - .append(organization.getCode()) - .append(" (") - .append(organization.getId()) - .append(")") - .toString(); - } - - private static boolean isOrganizationsAcceptedValuesNeeded(MappingRule mappingRule) { - return (mappingRule.getName() != null) && (mappingRule.getName().equals(VENDOR_ID) - || mappingRule.getName().equals(MATERIAL_SUPPLIER) || mappingRule.getName().equals(ACCESS_PROVIDER)); - } - - private static void populateDonorOrganizations(MappingRule mappingRule, HashMap donorOrganizations) { - mappingRule.getSubfields().stream() - .flatMap(subfieldMapping -> subfieldMapping.getFields().stream()) - .forEach(rule -> rule.setAcceptedValues(donorOrganizations)); - } - /** * Registers reader factory * diff --git a/src/main/java/org/folio/processing/mapping/mapper/reader/record/marc/MarcRecordReader.java b/src/main/java/org/folio/processing/mapping/mapper/reader/record/marc/MarcRecordReader.java index cf5c78d3..4ad6d55c 100644 --- a/src/main/java/org/folio/processing/mapping/mapper/reader/record/marc/MarcRecordReader.java +++ b/src/main/java/org/folio/processing/mapping/mapper/reader/record/marc/MarcRecordReader.java @@ -54,7 +54,7 @@ import static org.apache.commons.lang3.StringUtils.EMPTY; import static org.apache.commons.lang3.StringUtils.isNotBlank; import static org.apache.commons.lang3.StringUtils.isNotEmpty; -import static org.folio.processing.mapping.MappingManager.VENDOR_ID; +import static org.folio.processing.mapping.mapper.util.AcceptedValuesUtil.getAcceptedValues; import static org.folio.processing.value.Value.ValueType.LIST; import static org.folio.processing.value.Value.ValueType.MISSING; @@ -85,6 +85,7 @@ public class MarcRecordReader implements Reader { private static final List NEEDS_VALIDATION_BY_ACCEPTED_VALUES = List.of("vendor", "materialSupplier", "accessProvider","relationshipId", "donorOrganizationIds"); private static final String STATISTICAL_CODE_ID_FIELD = "statisticalCodeId"; private static final String BLANK = ""; + private static final String VENDOR_ID = "vendor"; private EntityType entityType; private Map acceptedValuesMatchers; @@ -199,7 +200,7 @@ private void processMARCExpression(boolean arrayValue, boolean isRepeatableField List marcValues = readValuesFromMarcRecord(expressionPart).stream().filter(m -> isNotBlank(m)).collect(Collectors.toList()); if (arrayValue || (isRepeatableField && marcValues.size() > 1)) { if (!resultList.isEmpty() && marcValues.size() == resultList.size()) { - List collectedValues = marcValues.stream().map(value -> getFromAcceptedValues(ruleExpression, value)).collect(Collectors.toList()); + List collectedValues = marcValues.stream().map(value -> getFromMappingParameters(ruleExpression, value)).collect(Collectors.toList()); List tmpResultList = new ArrayList<>(resultList); String concatenator = multipleStringBuilder.toString(); for (int i = 0; i < tmpResultList.size(); i++) { @@ -208,22 +209,24 @@ private void processMARCExpression(boolean arrayValue, boolean isRepeatableField } } else { // TODO This todo for cases where first subfields count not equals second subfields count - List collectedValues = marcValues.stream().map(value -> getFromAcceptedValues(ruleExpression, value)).collect(Collectors.toList()); + List collectedValues = marcValues.stream().map(value -> getFromMappingParameters(ruleExpression, value)).collect(Collectors.toList()); resultList.addAll(collectedValues); } } else { if (!marcValues.isEmpty()) { - sb.append(getFromAcceptedValues(ruleExpression, marcValues.get(0))); + sb.append(getFromMappingParameters(ruleExpression, marcValues.get(0))); } } } - private String getFromAcceptedValues(MappingRule ruleExpression, String value) { + private String getFromMappingParameters(MappingRule ruleExpression, String value) { AcceptedValuesMatcher acceptedValuesMatcher = Objects.isNull(ruleExpression.getName()) ? null : acceptedValuesMatchers.get(ruleExpression.getName()); - if (ruleExpression.getAcceptedValues() != null && !ruleExpression.getAcceptedValues().isEmpty()) { - for (Map.Entry entry : ruleExpression.getAcceptedValues().entrySet()) { + Map acceptedValues = getAcceptedValues(ruleExpression.getName(), mappingParameters); + + if (!acceptedValues.isEmpty()) { + for (Map.Entry entry : acceptedValues.entrySet()) { if ((acceptedValuesMatcher != null && acceptedValuesMatcher.matches(entry.getValue(), value)) || entry.getValue().equalsIgnoreCase(value) || equalsBasedOnBrackets(ruleExpression.getName(), entry.getValue(), value)) { value = entry.getKey(); @@ -232,7 +235,7 @@ private String getFromAcceptedValues(MappingRule ruleExpression, String value) { } boolean needsValidationByAcceptedValues = NEEDS_VALIDATION_BY_ACCEPTED_VALUES.contains(String.valueOf(ruleExpression.getName())); - if (needsValidationByAcceptedValues && !ruleExpression.getAcceptedValues().containsKey(value)) { + if (needsValidationByAcceptedValues && !acceptedValues.containsKey(value)) { return BLANK; } @@ -305,7 +308,7 @@ private int findFirstParenthesesIndex(String input, int startIndex) { */ private StringBuilder processStringExpression(MappingRule ruleExpression, boolean arrayValue, List resultList, StringBuilder sb, StringBuilder multipleStringBuilder, String expressionPart) { String value = expressionPart.replace(EXPRESSIONS_QUOTE, EMPTY); - value = getFromAcceptedValues(ruleExpression, value); + value = getFromMappingParameters(ruleExpression, value); if (isNotEmpty(value)) { if (arrayValue && resultList.isEmpty()) { resultList.add(value); @@ -406,7 +409,7 @@ private void retrieveValuesFromMarcRecord(List repeatableStrings, Mappin private String readRepeatableStringField(MappingRule mappingRule) { String value = mappingRule.getValue().replace(EXPRESSIONS_QUOTE, EMPTY); - return getFromAcceptedValues(mappingRule, value); + return getFromMappingParameters(mappingRule, value); } private boolean shouldCreateItemPerRepeatedMarcField(Value.ValueType valueType, MappingRule mappingRule) { diff --git a/src/main/java/org/folio/processing/mapping/mapper/util/AcceptedValuesUtil.java b/src/main/java/org/folio/processing/mapping/mapper/util/AcceptedValuesUtil.java new file mode 100644 index 00000000..08b0890a --- /dev/null +++ b/src/main/java/org/folio/processing/mapping/mapper/util/AcceptedValuesUtil.java @@ -0,0 +1,155 @@ +package org.folio.processing.mapping.mapper.util; + +import io.vertx.core.json.JsonObject; +import org.folio.Organization; +import org.folio.StatisticalCodeType; +import org.folio.processing.mapping.defaultmapper.processor.parameters.MappingParameters; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.function.Function; + +import static java.util.Map.entry; +import static org.folio.processing.matching.reader.util.MatchIdProcessorUtil.CODE_PROPERTY; +import static org.folio.processing.matching.reader.util.MatchIdProcessorUtil.ID_PROPERTY; +import static org.folio.processing.matching.reader.util.MatchIdProcessorUtil.NAME_PROPERTY; + +/** + * Retrieves accepted values from MappingParameters + */ +public class AcceptedValuesUtil { + private static final String VALUE_PROPERTY = "value"; + private static final String STATISTICAL_CODE_TEMPLATE = "%s: %s - %s"; + + private static final String HOLDINGS_PERMANENT_LOCATION_ID = "permanentLocationId"; + private static final String HOLDINGS_TEMPORARY_LOCATION_ID = "temporaryLocationId"; + private static final String STATUS_ID = "statusId"; + private static final String NATURE_OF_CONTENT_TERM_ID = "natureOfContentTermId"; + private static final String INSTANCE_RELATIONSHIP_TYPE_ID = "instanceRelationshipTypeId"; + private static final String HOLDINGS_TYPE_ID = "holdingsTypeId"; + private static final String CALL_NUMBER_TYPE_ID = "callNumberTypeId"; + private static final String ILL_POLICY_ID = "illPolicyId"; + private static final String STATISTICAL_CODE_ID = "statisticalCodeId"; + private static final String NOTE_TYPE = "noteType"; + private static final String RELATIONSHIP_ID = "relationshipId"; + private static final String MATERIAL_TYPE_ID = "materialType.id"; + private static final String ITEM_CALL_NUMBER_TYPE_ID = "itemLevelCallNumberTypeId"; + private static final String ITEM_NOTE_TYPE_ID = "itemNoteTypeId"; + private static final String PERMANENT_LOAN_TYPE_ID = "permanentLoanType.id"; + private static final String TEMPORARY_LOAN_TYPE_ID = "temporaryLoanType.id"; + private static final String ITEM_PERMANENT_LOCATION_ID = "permanentLocation.id"; + private static final String ITEM_TEMPORARY_LOCATION_ID = "temporaryLocation.id"; + private static final String ITEM_DAMAGED_STATUS_ID = "itemDamagedStatusId"; + private static final String CONTRIBUTOR_NAME_TYPE_ID = "contributorNameTypeId"; + private static final String VENDOR = "vendor"; + private static final String MATERIAL_SUPPLIER = "materialSupplier"; + private static final String ACCESS_PROVIDER = "accessProvider"; + private static final String DONOR_ORGANIZATION_IDS = "donorOrganizationIds"; + private static final String ORDER_LOCATION = "locationId"; + private static final String ORDER_MATERIAL_TYPE = "materialType"; + private static final String ACQUISITION_UNIT_IDS = "acqUnitIds"; + private static final String BILL_TO = "billTo"; + private static final String SHIP_TO = "shipTo"; + private static final String PRODUCT_ID_TYPE = "productIdType"; + private static final String ACQUISITION_METHOD = "acquisitionMethod"; + private static final String FUND_ID = "fundId"; + private static final String EXPENSE_CLASS_ID = "expenseClassId"; + + private static final Map>> ruleNameToMappingParameter = Map.ofEntries( + entry(HOLDINGS_PERMANENT_LOCATION_ID, MappingParameters::getLocations), + entry(HOLDINGS_TEMPORARY_LOCATION_ID, MappingParameters::getLocations), + entry(STATUS_ID, MappingParameters::getInstanceStatuses), + entry(NATURE_OF_CONTENT_TERM_ID, MappingParameters::getNatureOfContentTerms), + entry(INSTANCE_RELATIONSHIP_TYPE_ID, MappingParameters::getInstanceRelationshipTypes), + entry(HOLDINGS_TYPE_ID, MappingParameters::getHoldingsTypes), + entry(CALL_NUMBER_TYPE_ID, MappingParameters::getCallNumberTypes), + entry(ILL_POLICY_ID, MappingParameters::getIllPolicies), + entry(STATISTICAL_CODE_ID, AcceptedValuesUtil::getStatisticalCode), + entry(NOTE_TYPE, MappingParameters::getHoldingsNoteTypes), + entry(RELATIONSHIP_ID, MappingParameters::getElectronicAccessRelationships), + entry(MATERIAL_TYPE_ID, MappingParameters::getMaterialTypes), + entry(ITEM_CALL_NUMBER_TYPE_ID, MappingParameters::getCallNumberTypes), + entry(ITEM_DAMAGED_STATUS_ID, MappingParameters::getItemDamageStatuses), + entry(ITEM_NOTE_TYPE_ID, MappingParameters::getItemNoteTypes), + entry(PERMANENT_LOAN_TYPE_ID, MappingParameters::getLoanTypes), + entry(TEMPORARY_LOAN_TYPE_ID, MappingParameters::getLoanTypes), + entry(ITEM_PERMANENT_LOCATION_ID, MappingParameters::getLocations), + entry(ITEM_TEMPORARY_LOCATION_ID, MappingParameters::getLocations), + entry(CONTRIBUTOR_NAME_TYPE_ID, MappingParameters::getContributorNameTypes), + entry(ORDER_LOCATION, MappingParameters::getLocations), + entry(ORDER_MATERIAL_TYPE, MappingParameters::getMaterialTypes), + entry(VENDOR, MappingParameters::getOrganizations), + entry(MATERIAL_SUPPLIER, MappingParameters::getOrganizations), + entry(ACCESS_PROVIDER, MappingParameters::getOrganizations), + entry(DONOR_ORGANIZATION_IDS, AcceptedValuesUtil::getDonorOrganizationsFromMappingParameters), + entry(ACQUISITION_UNIT_IDS, MappingParameters::getAcquisitionsUnits), + entry(BILL_TO, MappingParameters::getTenantConfigurationAddresses), + entry(SHIP_TO, MappingParameters::getTenantConfigurationAddresses), + entry(PRODUCT_ID_TYPE, MappingParameters::getIdentifierTypes), + entry(ACQUISITION_METHOD, MappingParameters::getAcquisitionMethods), + entry(FUND_ID, MappingParameters::getFunds), + entry(EXPENSE_CLASS_ID, MappingParameters::getExpenseClasses)); + + private AcceptedValuesUtil() {} + + public static Map getAcceptedValues(String ruleName, MappingParameters mappingParameters) { + HashMap acceptedValues = new HashMap<>(); + + if (ruleName == null || !ruleNameToMappingParameter.containsKey(ruleName)) { + return acceptedValues; + } + + List mappingParameter = ruleNameToMappingParameter.get(ruleName).apply(mappingParameters); + + mappingParameter.forEach(parameter -> { + JsonObject jsonObject = parameter instanceof String string ? new JsonObject(string) : JsonObject.mapFrom(parameter); + + String idField = jsonObject.getString(ID_PROPERTY); + String nameField = jsonObject.getString(NAME_PROPERTY); + String valueField = jsonObject.getString(VALUE_PROPERTY); + String codeField = jsonObject.getString(CODE_PROPERTY); + + if (idField != null && (nameField != null || valueField != null)) { + acceptedValues.put(idField, formAcceptedValue(nameField, valueField, codeField)); + } + }); + + return acceptedValues; + } + + private static String formAcceptedValue(String nameField, String valueField, String codeField) { + StringBuilder value = new StringBuilder() + .append(nameField != null ? nameField : valueField); + + if (codeField != null) { + value.append(" (") + .append(codeField) + .append(")"); + } + return String.valueOf(value); + } + + private static List getDonorOrganizationsFromMappingParameters(MappingParameters mappingParameters) { + return mappingParameters.getOrganizations().stream() + .filter(organization -> Boolean.TRUE.equals(organization.getIsDonor())).toList(); + } + + private static List getStatisticalCode(MappingParameters mappingParameters) { + return mappingParameters.getStatisticalCodes().stream() + .map(statCode -> { + Optional statCodeType = mappingParameters + .getStatisticalCodeTypes().stream() + .filter(codeType -> codeType.getId().equals(statCode.getStatisticalCodeTypeId())).findAny(); + if (statCodeType.isPresent()) { + String formattedStatCode = String.format(STATISTICAL_CODE_TEMPLATE, statCodeType.get().getName(), statCode.getCode(), + statCode.getName()); + + return new JsonObject().put(ID_PROPERTY, statCode.getId()).put(VALUE_PROPERTY, formattedStatCode); + } + return null; + }).filter(Objects::nonNull).toList(); + } +} diff --git a/src/main/java/org/folio/processing/matching/reader/util/MatchIdProcessorUtil.java b/src/main/java/org/folio/processing/matching/reader/util/MatchIdProcessorUtil.java index 09eb7785..0e47ac38 100644 --- a/src/main/java/org/folio/processing/matching/reader/util/MatchIdProcessorUtil.java +++ b/src/main/java/org/folio/processing/matching/reader/util/MatchIdProcessorUtil.java @@ -15,9 +15,9 @@ public final class MatchIdProcessorUtil { public static final String MAPPING_PARAMS_KEY = "MAPPING_PARAMS"; public static final String RELATIONS_KEY = "MATCHING_PARAMETERS_RELATIONS"; - private static final String NAME_PROPERTY = "name"; - private static final String ID_PROPERTY = "id"; - private static final String CODE_PROPERTY = "code"; + public static final String NAME_PROPERTY = "name"; + public static final String ID_PROPERTY = "id"; + public static final String CODE_PROPERTY = "code"; private static final String LOCATIONS_PROPERTY = "locations"; private MatchIdProcessorUtil() { diff --git a/src/test/java/org/folio/processing/events/EventManagerTest.java b/src/test/java/org/folio/processing/events/EventManagerTest.java index 1c0557e1..4dd73aff 100644 --- a/src/test/java/org/folio/processing/events/EventManagerTest.java +++ b/src/test/java/org/folio/processing/events/EventManagerTest.java @@ -5,15 +5,14 @@ import io.vertx.ext.unit.junit.RunTestOnContext; import io.vertx.ext.unit.junit.VertxUnitRunner; import net.mguenther.kafka.junit.EmbeddedKafkaCluster; -import net.mguenther.kafka.junit.EmbeddedKafkaClusterConfig; import org.folio.kafka.KafkaConfig; import org.junit.BeforeClass; -import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import static net.mguenther.kafka.junit.EmbeddedKafkaCluster.provisionWith; +import static net.mguenther.kafka.junit.EmbeddedKafkaClusterConfig.defaultClusterConfig; @RunWith(VertxUnitRunner.class) public class EventManagerTest { @@ -21,12 +20,13 @@ public class EventManagerTest { @Rule public RunTestOnContext rule = new RunTestOnContext(); - @ClassRule - public static EmbeddedKafkaCluster kafkaCluster = provisionWith(EmbeddedKafkaClusterConfig.useDefaults()); + public static EmbeddedKafkaCluster kafkaCluster; private static KafkaConfig kafkaConfig; @BeforeClass public static void setUpClass() { + kafkaCluster = provisionWith(defaultClusterConfig()); + kafkaCluster.start(); String[] hostAndPort = kafkaCluster.getBrokerList().split(":"); kafkaConfig = KafkaConfig.builder() .kafkaHost(hostAndPort[0]) diff --git a/src/test/java/org/folio/processing/events/services/publisher/KafkaEventPublisherTest.java b/src/test/java/org/folio/processing/events/services/publisher/KafkaEventPublisherTest.java index 558e4208..c11aedae 100644 --- a/src/test/java/org/folio/processing/events/services/publisher/KafkaEventPublisherTest.java +++ b/src/test/java/org/folio/processing/events/services/publisher/KafkaEventPublisherTest.java @@ -2,18 +2,16 @@ import io.vertx.core.Vertx; import io.vertx.core.json.Json; +import io.vertx.ext.unit.junit.VertxUnitRunner; import net.mguenther.kafka.junit.EmbeddedKafkaCluster; -import net.mguenther.kafka.junit.EmbeddedKafkaClusterConfig; import net.mguenther.kafka.junit.ObserveKeyValues; import org.folio.DataImportEventPayload; import org.folio.kafka.KafkaConfig; import org.folio.kafka.KafkaTopicNameHelper; import org.folio.rest.jaxrs.model.Event; import org.junit.BeforeClass; -import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; import java.util.HashMap; import java.util.List; @@ -23,27 +21,29 @@ import java.util.concurrent.TimeUnit; import static net.mguenther.kafka.junit.EmbeddedKafkaCluster.provisionWith; +import static net.mguenther.kafka.junit.EmbeddedKafkaClusterConfig.defaultClusterConfig; import static org.folio.DataImportEventTypes.DI_COMPLETED; import static org.folio.kafka.KafkaTopicNameHelper.getDefaultNameSpace; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -@RunWith(JUnit4.class) +@RunWith(VertxUnitRunner.class) public class KafkaEventPublisherTest { private static final String KAFKA_ENV = "folio"; private static final String OKAPI_URL = "http://localhost"; private static final String TENANT_ID = "diku"; private static final String TOKEN = "stub-token"; - @ClassRule - public static EmbeddedKafkaCluster kafkaCluster = provisionWith(EmbeddedKafkaClusterConfig.useDefaults()); + public static EmbeddedKafkaCluster kafkaCluster; private static KafkaConfig kafkaConfig; private Vertx vertx = Vertx.vertx(); @BeforeClass public static void setUpClass() { + kafkaCluster = provisionWith(defaultClusterConfig()); + kafkaCluster.start(); String[] hostAndPort = kafkaCluster.getBrokerList().split(":"); kafkaConfig = KafkaConfig.builder() .kafkaHost(hostAndPort[0]) diff --git a/src/test/java/org/folio/processing/mapping/manager/MappingManagerUnitTest.java b/src/test/java/org/folio/processing/mapping/manager/MappingManagerUnitTest.java index 7a6b6bf4..340ef4d4 100644 --- a/src/test/java/org/folio/processing/mapping/manager/MappingManagerUnitTest.java +++ b/src/test/java/org/folio/processing/mapping/manager/MappingManagerUnitTest.java @@ -6,12 +6,11 @@ import org.folio.DataImportEventPayload; import org.folio.Holdings; import org.folio.Instance; -import org.folio.Location; import org.folio.MappingProfile; -import org.folio.Organization; import org.folio.ParsedRecord; import org.folio.Record; import org.folio.StatisticalCode; +import org.folio.StatisticalCodeType; import org.folio.processing.mapping.MappingManager; import org.folio.processing.mapping.defaultmapper.processor.parameters.MappingParameters; import org.folio.processing.mapping.mapper.MappingContext; @@ -40,11 +39,9 @@ import static org.folio.rest.jaxrs.model.EntityType.HOLDINGS; import static org.folio.rest.jaxrs.model.EntityType.INSTANCE; import static org.folio.rest.jaxrs.model.EntityType.MARC_BIBLIOGRAPHIC; -import static org.folio.rest.jaxrs.model.EntityType.ORDER; import static org.folio.rest.jaxrs.model.ProfileType.MAPPING_PROFILE; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; @RunWith(JUnit4.class) public class MappingManagerUnitTest { @@ -91,181 +88,6 @@ public void shouldMap_MarcBibliographicToInstance() throws IOException { assertNotNull(mappedInstance.getIndexTitle()); } - @Test - public void shouldMap_MarcBibliographicToInstance_checkCopyingLocations() throws IOException { - // given - MappingProfile mappingProfile = new MappingProfile() - .withIncomingRecordType(MARC_BIBLIOGRAPHIC) - .withExistingRecordType(INSTANCE) - .withMappingDetails(new MappingDetail() - .withMappingFields(singletonList(new MappingRule().withName("permanentLocationId") - .withPath("indexTitle").withValue("949$l").withEnabled("true")))); - ProfileSnapshotWrapper mappingProfileWrapper = new ProfileSnapshotWrapper(); - mappingProfileWrapper.setContent(mappingProfile); - mappingProfileWrapper.setContentType(MAPPING_PROFILE); - - String givenMarcRecord = "{ \"leader\":\"01314nam 22003851a 4500\", \"fields\":[ { \"001\":\"ybp7406411\" } ] }"; - - String givenInstance = new ObjectMapper().writeValueAsString(new TestInstance(UUID.randomUUID().toString())); - DataImportEventPayload eventPayload = new DataImportEventPayload(); - HashMap context = new HashMap<>(); - context.put(MARC_BIBLIOGRAPHIC.value(), givenMarcRecord); - context.put(INSTANCE.value(), givenInstance); - eventPayload.setContext(context); - eventPayload.setCurrentNode(mappingProfileWrapper); - - String locationId = UUID.randomUUID().toString(); - MappingContext mappingContext = new MappingContext().withMappingParameters(new MappingParameters() - .withLocations(List.of(new Location() - .withId(locationId) - .withCode("CODE")))); - - // when - MappingManager.registerReaderFactory(new TestMarcBibliographicReaderFactory()); - MappingManager.registerWriterFactory(new TestInstanceWriterFactory()); - MappingManager.map(eventPayload, mappingContext); - - // then - assertNotNull(eventPayload.getContext().get(MARC_BIBLIOGRAPHIC.value())); - assertNotNull(eventPayload.getContext().get(INSTANCE.value())); - - assertNotNull(mappingProfile.getMappingDetails().getMappingFields().get(0)); - assertTrue(mappingProfile.getMappingDetails().getMappingFields().get(0).getAcceptedValues().containsKey(locationId)); - - TestInstance mappedInstance = new ObjectMapper().readValue(eventPayload.getContext().get(INSTANCE.value()), TestInstance.class); - assertNotNull(mappedInstance.getId()); - assertNotNull(mappedInstance.getIndexTitle()); - } - - @Test - public void shouldMap_MarcBibliographicToOrder_checkOrganizations() throws IOException { - // given - MappingProfile mappingProfile = new MappingProfile() - .withId(UUID.randomUUID().toString()) - .withIncomingRecordType(MARC_BIBLIOGRAPHIC) - .withExistingRecordType(ORDER) - .withMappingDetails(new MappingDetail() - .withMappingFields(new ArrayList<>(List.of( - new MappingRule().withName("vendor").withPath("order.po.vendor").withValue("\"CODE\"").withEnabled("true"), - new MappingRule().withName("materialSupplier").withPath("order.poLine.physical.materialSupplier").withValue("\"CODE\"").withEnabled("true"), - new MappingRule().withName("accessProvider").withPath("order.poLine.eresource.accessProvider").withValue("\"CODE\"").withEnabled("true") - )))); - - ProfileSnapshotWrapper mappingProfileWrapper = new ProfileSnapshotWrapper(); - mappingProfileWrapper.setContent(mappingProfile); - mappingProfileWrapper.setContentType(MAPPING_PROFILE); - - String givenMarcRecord = "{ \"leader\":\"01314nam 22003851a 4500\", \"fields\":[ { \"001\":\"ybp7406411\" } ] }"; - - String givenOrder = new ObjectMapper().writeValueAsString(new TestOrder(UUID.randomUUID().toString())); - DataImportEventPayload eventPayload = new DataImportEventPayload(); - HashMap context = new HashMap<>(); - context.put(MARC_BIBLIOGRAPHIC.value(), givenMarcRecord); - context.put(ORDER.value(), givenOrder); - eventPayload.setContext(context); - eventPayload.setCurrentNode(mappingProfileWrapper); - - String organizationId = UUID.randomUUID().toString(); - MappingContext mappingContext = new MappingContext().withMappingParameters(new MappingParameters() - .withOrganizations(List.of(new Organization() - .withId(organizationId) - .withName("NAME") - .withCode("CODE") - ))); - - // when - MappingManager.registerReaderFactory(new TestMarcBibliographicReaderFactory()); - MappingManager.registerWriterFactory(new TestOrderWriterFactory()); - MappingManager.map(eventPayload, mappingContext); - - // then - assertNotNull(eventPayload.getContext().get(MARC_BIBLIOGRAPHIC.value())); - assertNotNull(eventPayload.getContext().get(ORDER.value())); - - for (MappingRule mappingRule : mappingProfile.getMappingDetails().getMappingFields()) { - assertNotNull(mappingRule); - assertTrue(mappingRule.getAcceptedValues().containsKey(organizationId)); - String expectedAcceptedValue = String.format("%s (%s)", mappingRule.getValue().replaceAll("\"", ""), organizationId); - assertEquals(expectedAcceptedValue, mappingRule.getAcceptedValues().get(organizationId)); - } - } - - @Test - public void shouldMap_MarcBibliographicToOrder_checkDonorOrganizationsProvision() { - // given - MappingRule donorsMappingRule = new MappingRule() - .withName("donorOrganizationIds") - .withEnabled("true") - .withPath("order.poLine.donorOrganizationIds[]") - .withRepeatableFieldAction(MappingRule.RepeatableFieldAction.EXTEND_EXISTING) - .withSubfields(List.of( - new RepeatableSubfieldMapping() - .withOrder(0) - .withPath("order.poLine.donorOrganizationIds[]") - .withFields(List.of(new MappingRule() - .withName("donorOrganizationIds") - .withEnabled("true") - .withPath("order.poLine.donorOrganizationIds[]") - .withValue("\"CODE-1\""))), - new RepeatableSubfieldMapping() - .withOrder(1) - .withPath("order.poLine.donorOrganizationIds[]") - .withFields(List.of(new MappingRule() - .withName("donorOrganizationIds") - .withEnabled("true") - .withPath("order.poLine.donorOrganizationIds[]") - .withValue("\"CODE-2\""))))); - - MappingProfile mappingProfile = new MappingProfile() - .withId(UUID.randomUUID().toString()) - .withIncomingRecordType(MARC_BIBLIOGRAPHIC) - .withExistingRecordType(ORDER) - .withMappingDetails(new MappingDetail() - .withMappingFields(List.of(donorsMappingRule))); - - ProfileSnapshotWrapper mappingProfileWrapper = new ProfileSnapshotWrapper() - .withContentType(MAPPING_PROFILE) - .withContent(mappingProfile); - - HashMap context = new HashMap<>(); - context.put(MARC_BIBLIOGRAPHIC.value(), Json.encode(new Record())); - context.put(ORDER.value(), new JsonObject().encode()); - DataImportEventPayload eventPayload = new DataImportEventPayload() - .withCurrentNode(mappingProfileWrapper) - .withContext(context); - - String expectedOrganizationId = UUID.randomUUID().toString(); - MappingContext mappingContext = new MappingContext().withMappingParameters(new MappingParameters() - .withOrganizations(List.of( - new Organization() - .withId(expectedOrganizationId) - .withIsDonor(true) - .withName("NAME-1") - .withCode("CODE-1"), - new Organization() - .withId(UUID.randomUUID().toString()) - .withName("NAME-2") - .withCode("CODE-2")) - )); - - // when - MappingManager.registerReaderFactory(new TestMarcBibliographicReaderFactory()); - MappingManager.registerWriterFactory(new TestOrderWriterFactory()); - MappingManager.map(eventPayload, mappingContext); - - // then - assertEquals(2, donorsMappingRule.getSubfields().size()); - - for (RepeatableSubfieldMapping subfieldMapping : donorsMappingRule.getSubfields()) { - assertEquals(1, subfieldMapping.getFields().size()); - MappingRule mappingRule = subfieldMapping.getFields().get(0); - assertEquals(1, mappingRule.getAcceptedValues().size()); - assertTrue(mappingRule.getAcceptedValues().containsKey(expectedOrganizationId)); - String expectedAcceptedValue = String.format("CODE-1 (%s)", expectedOrganizationId); - assertEquals(expectedAcceptedValue, mappingRule.getAcceptedValues().get(expectedOrganizationId)); - } - } - @Test(expected = RuntimeException.class) public void shouldThrowException_ifNoReaderEligible() { // given @@ -371,16 +193,19 @@ private void shouldMap_MarcBibliographicStatisticalCodes( new StatisticalCode() .withId("uuid1") .withCode("abc") - .withName("abd"), + .withName("abd") + .withStatisticalCodeTypeId("uuid1"), new StatisticalCode() .withId("uuid2") .withCode("bbc") - .withName("bbd (bbc)") - ); + .withName("bbd") + .withStatisticalCodeTypeId("uuid1")); + + List statisticalCodeTypes = List.of( + new StatisticalCodeType() + .withId("uuid1") + .withName("TEST (test code type)")); - HashMap acceptedValues = new HashMap<>(Map.of( - "uuid1", "TEST (test code type): abc - abd", - "uuid2", "TEST (test code type): bbc - bbd (bbc)")); MappingProfile mappingProfile = new MappingProfile() .withId(UUID.randomUUID().toString()) @@ -401,7 +226,6 @@ private void shouldMap_MarcBibliographicStatisticalCodes( .withPath("instance.statisticalCodeIds[]") .withValue(value == null ? "971" : value) .withEnabled("true") - .withAcceptedValues(acceptedValues) )) ))) )))); @@ -435,7 +259,7 @@ private void shouldMap_MarcBibliographicStatisticalCodes( eventPayload.setCurrentNode(mappingProfileWrapper); MappingContext mappingContext = new MappingContext().withMappingParameters(new MappingParameters() - .withStatisticalCodes(statisticalCodes)); + .withStatisticalCodes(statisticalCodes).withStatisticalCodeTypes(statisticalCodeTypes)); MappingManager.registerReaderFactory(new MarcBibReaderFactory()); MappingManager.registerWriterFactory(new WriterFactory() { diff --git a/src/test/java/org/folio/processing/mapping/mapper/HoldingsMapperTest.java b/src/test/java/org/folio/processing/mapping/mapper/HoldingsMapperTest.java index a474083a..0af2a24c 100644 --- a/src/test/java/org/folio/processing/mapping/mapper/HoldingsMapperTest.java +++ b/src/test/java/org/folio/processing/mapping/mapper/HoldingsMapperTest.java @@ -46,11 +46,6 @@ public void shouldCreateOneHoldingIfOnlySingleMARCfieldContainsLocation() throws context.put(MARC_BIBLIOGRAPHIC.value(), Json.encodePrettily(record)); eventPayload.setContext(context); - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put("fcd64ce1-6995-48f0-840e-89ffa2288371", "Main Library (KU/CC/DI/M)"); - acceptedValues.put("53cf956f-c1df-410b-8bea-27f712cca7c0", "Annex (KU/CC/DI/A)"); - acceptedValues.put("184aae84-a5bf-4c6a-85ba-4a7c73026cd5", "Online (E)"); - MappingDetail mappingDetails = new MappingDetail() .withName("holdings") .withRecordType(HOLDINGS) @@ -58,8 +53,7 @@ public void shouldCreateOneHoldingIfOnlySingleMARCfieldContainsLocation() throws .withName("permanentLocationId") .withEnabled("true") .withPath("holdings.permanentLocationId") - .withValue("945$h") - .withAcceptedValues(acceptedValues), + .withValue("945$h"), new MappingRule() .withName("statisticalCodeIds") .withEnabled("true") @@ -75,10 +69,19 @@ public void shouldCreateOneHoldingIfOnlySingleMARCfieldContainsLocation() throws MappingContext mappingContext = new MappingContext() .withMappingParameters(new MappingParameters() - .withLocations(Lists.newArrayList(Lists.newArrayList(new Location() - .withId("fcd64ce1-6995-48f0-840e-89ffa2288371") - .withName("Main Library") - .withCode("KU/CC/DI/M"))))); + .withLocations(List.of( + new Location() + .withId("fcd64ce1-6995-48f0-840e-89ffa2288371") + .withName("Main Library") + .withCode("KU/CC/DI/M"), + new Location() + .withId("53cf956f-c1df-410b-8bea-27f712cca7c0") + .withName("Annex") + .withCode("KU/CC/DI/A"), + new Location() + .withId("184aae84-a5bf-4c6a-85ba-4a7c73026cd5") + .withName("Online") + .withCode("E")))); Reader reader = new MarcBibReaderFactory().createReader(); reader.initialize(eventPayload, mappingContext); @@ -111,11 +114,6 @@ public void shouldCreateOneHoldingIfPermanentLocationIsStringValue() throws IOEx context.put(MARC_BIBLIOGRAPHIC.value(), Json.encodePrettily(record)); eventPayload.setContext(context); - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put("fcd64ce1-6995-48f0-840e-89ffa2288371", "Main Library (KU/CC/DI/M)"); - acceptedValues.put("53cf956f-c1df-410b-8bea-27f712cca7c0", "Annex (KU/CC/DI/A)"); - acceptedValues.put("184aae84-a5bf-4c6a-85ba-4a7c73026cd5", "Online (E)"); - MappingDetail mappingDetails = new MappingDetail() .withName("holdings") .withRecordType(HOLDINGS) @@ -123,8 +121,7 @@ public void shouldCreateOneHoldingIfPermanentLocationIsStringValue() throws IOEx .withName("permanentLocationId") .withEnabled("true") .withPath("holdings.permanentLocationId") - .withValue("\"KU/CC/DI/A\"; else 945$h") - .withAcceptedValues(acceptedValues))); + .withValue("\"KU/CC/DI/A\"; else 945$h"))); MappingProfile profile = new MappingProfile() .withId(UUID.randomUUID().toString()) @@ -135,10 +132,19 @@ public void shouldCreateOneHoldingIfPermanentLocationIsStringValue() throws IOEx MappingContext mappingContext = new MappingContext() .withMappingParameters(new MappingParameters() - .withLocations(Lists.newArrayList(Lists.newArrayList(new Location() - .withId("fcd64ce1-6995-48f0-840e-89ffa2288371") - .withName("Main Library") - .withCode("KU/CC/DI/M"))))); + .withLocations(List.of( + new Location() + .withId("fcd64ce1-6995-48f0-840e-89ffa2288371") + .withName("Main Library") + .withCode("KU/CC/DI/M"), + new Location() + .withId("53cf956f-c1df-410b-8bea-27f712cca7c0") + .withName("Annex") + .withCode("KU/CC/DI/A"), + new Location() + .withId("184aae84-a5bf-4c6a-85ba-4a7c73026cd5") + .withName("Online") + .withCode("E")))); Reader reader = new MarcBibReaderFactory().createReader(); reader.initialize(eventPayload, mappingContext); @@ -179,11 +185,6 @@ public void shouldMapMultipleHoldingInExistingHoldings() throws IOException { context.put(MARC_BIBLIOGRAPHIC.value(), Json.encodePrettily(record)); eventPayload.setContext(context); - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put("fcd64ce1-6995-48f0-840e-89ffa2288371", "Main Library (KU/CC/DI/M)"); - acceptedValues.put("53cf956f-c1df-410b-8bea-27f712cca7c0", "Annex (KU/CC/DI/A)"); - acceptedValues.put("184aae84-a5bf-4c6a-85ba-4a7c73026cd5", "Online (E)"); - MappingDetail mappingDetails = new MappingDetail() .withName("holdings") .withRecordType(HOLDINGS) @@ -202,10 +203,19 @@ public void shouldMapMultipleHoldingInExistingHoldings() throws IOException { MappingContext mappingContext = new MappingContext() .withMappingParameters(new MappingParameters() - .withLocations(Lists.newArrayList(Lists.newArrayList(new Location() - .withId("fcd64ce1-6995-48f0-840e-89ffa2288371") - .withName("Main Library") - .withCode("KU/CC/DI/M"))))); + .withLocations(List.of( + new Location() + .withId("fcd64ce1-6995-48f0-840e-89ffa2288371") + .withName("Main Library") + .withCode("KU/CC/DI/M"), + new Location() + .withId("53cf956f-c1df-410b-8bea-27f712cca7c0") + .withName("Annex") + .withCode("KU/CC/DI/A"), + new Location() + .withId("184aae84-a5bf-4c6a-85ba-4a7c73026cd5") + .withName("Online") + .withCode("E")))); Reader reader = new MarcBibReaderFactory().createReader(); reader.initialize(eventPayload, mappingContext); @@ -243,11 +253,6 @@ public void shouldMapOneHoldingInExistingHoldingIfPermanentLocationIsStringValue context.put(MARC_BIBLIOGRAPHIC.value(), Json.encodePrettily(record)); eventPayload.setContext(context); - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put("fcd64ce1-6995-48f0-840e-89ffa2288371", "Main Library (KU/CC/DI/M)"); - acceptedValues.put("53cf956f-c1df-410b-8bea-27f712cca7c0", "Annex (KU/CC/DI/A)"); - acceptedValues.put("184aae84-a5bf-4c6a-85ba-4a7c73026cd5", "Online (E)"); - MappingDetail mappingDetails = new MappingDetail() .withName("holdings") .withRecordType(HOLDINGS) @@ -255,8 +260,7 @@ public void shouldMapOneHoldingInExistingHoldingIfPermanentLocationIsStringValue .withName("permanentLocationId") .withEnabled("true") .withPath("holdings.permanentLocationId") - .withValue("\"KU/CC/DI/A\"; else 945$h") - .withAcceptedValues(acceptedValues))); + .withValue("\"KU/CC/DI/A\"; else 945$h"))); MappingProfile profile = new MappingProfile() .withId(UUID.randomUUID().toString()) @@ -267,10 +271,19 @@ public void shouldMapOneHoldingInExistingHoldingIfPermanentLocationIsStringValue MappingContext mappingContext = new MappingContext() .withMappingParameters(new MappingParameters() - .withLocations(Lists.newArrayList(Lists.newArrayList(new Location() - .withId("fcd64ce1-6995-48f0-840e-89ffa2288371") - .withName("Main Library") - .withCode("KU/CC/DI/M"))))); + .withLocations(List.of( + new Location() + .withId("fcd64ce1-6995-48f0-840e-89ffa2288371") + .withName("Main Library") + .withCode("KU/CC/DI/M"), + new Location() + .withId("53cf956f-c1df-410b-8bea-27f712cca7c0") + .withName("Annex") + .withCode("KU/CC/DI/A"), + new Location() + .withId("184aae84-a5bf-4c6a-85ba-4a7c73026cd5") + .withName("Online") + .withCode("E")))); Reader reader = new MarcBibReaderFactory().createReader(); reader.initialize(eventPayload, mappingContext); @@ -302,11 +315,6 @@ public void shouldCreateMultipleHoldingsButWithoutDuplicatedLocations() throws I context.put(MARC_BIBLIOGRAPHIC.value(), Json.encodePrettily(record)); eventPayload.setContext(context); - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put("fcd64ce1-6995-48f0-840e-89ffa2288371", "Main Library (KU/CC/DI/M)"); - acceptedValues.put("53cf956f-c1df-410b-8bea-27f712cca7c0", "Annex (KU/CC/DI/A)"); - acceptedValues.put("184aae84-a5bf-4c6a-85ba-4a7c73026cd5", "Online (E)"); - MappingDetail mappingDetails = new MappingDetail() .withName("holdings") .withRecordType(HOLDINGS) @@ -314,14 +322,12 @@ public void shouldCreateMultipleHoldingsButWithoutDuplicatedLocations() throws I .withName("permanentLocationId") .withEnabled("true") .withPath("holdings.permanentLocationId") - .withValue("945$h") - .withAcceptedValues(acceptedValues), + .withValue("945$h"), new MappingRule() .withName("temporaryLocationId") .withEnabled("true") .withPath("holdings.temporaryLocationId") - .withValue("945$a") - .withAcceptedValues(acceptedValues), + .withValue("945$a"), new MappingRule() .withName("statisticalCodeIds") .withEnabled("true") @@ -355,10 +361,19 @@ public void shouldCreateMultipleHoldingsButWithoutDuplicatedLocations() throws I MappingContext mappingContext = new MappingContext() .withMappingParameters(new MappingParameters() - .withLocations(Lists.newArrayList(Lists.newArrayList(new Location() - .withId("fcd64ce1-6995-48f0-840e-89ffa2288371") - .withName("Main Library") - .withCode("KU/CC/DI/M"))))); + .withLocations(List.of( + new Location() + .withId("fcd64ce1-6995-48f0-840e-89ffa2288371") + .withName("Main Library") + .withCode("KU/CC/DI/M"), + new Location() + .withId("53cf956f-c1df-410b-8bea-27f712cca7c0") + .withName("Annex") + .withCode("KU/CC/DI/A"), + new Location() + .withId("184aae84-a5bf-4c6a-85ba-4a7c73026cd5") + .withName("Online") + .withCode("E")))); Reader reader = new MarcBibReaderFactory().createReader(); reader.initialize(eventPayload, mappingContext); @@ -403,11 +418,6 @@ public void shouldCreateMultipleHoldingsButIfLocationMappingRuleContainsElseStat context.put(MARC_BIBLIOGRAPHIC.value(), Json.encodePrettily(record)); eventPayload.setContext(context); - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put("fcd64ce1-6995-48f0-840e-89ffa2288371", "Main Library (KU/CC/DI/M)"); - acceptedValues.put("53cf956f-c1df-410b-8bea-27f712cca7c0", "Annex (KU/CC/DI/A)"); - acceptedValues.put("184aae84-a5bf-4c6a-85ba-4a7c73026cd5", "Online (E)"); - MappingDetail mappingDetails = new MappingDetail() .withName("holdings") .withRecordType(HOLDINGS) @@ -415,14 +425,12 @@ public void shouldCreateMultipleHoldingsButIfLocationMappingRuleContainsElseStat .withName("permanentLocationId") .withEnabled("true") .withPath("holdings.permanentLocationId") - .withValue("945$h; else \"KU/CC/DI/A\"") - .withAcceptedValues(acceptedValues), + .withValue("945$h; else \"KU/CC/DI/A\""), new MappingRule() .withName("temporaryLocationId") .withEnabled("true") .withPath("holdings.temporaryLocationId") - .withValue("945$a") - .withAcceptedValues(acceptedValues), + .withValue("945$a"), new MappingRule() .withName("statisticalCodeIds") .withEnabled("true") @@ -456,10 +464,19 @@ public void shouldCreateMultipleHoldingsButIfLocationMappingRuleContainsElseStat MappingContext mappingContext = new MappingContext() .withMappingParameters(new MappingParameters() - .withLocations(Lists.newArrayList(Lists.newArrayList(new Location() - .withId("fcd64ce1-6995-48f0-840e-89ffa2288371") - .withName("Main Library") - .withCode("KU/CC/DI/M"))))); + .withLocations(List.of( + new Location() + .withId("fcd64ce1-6995-48f0-840e-89ffa2288371") + .withName("Main Library") + .withCode("KU/CC/DI/M"), + new Location() + .withId("53cf956f-c1df-410b-8bea-27f712cca7c0") + .withName("Annex") + .withCode("KU/CC/DI/A"), + new Location() + .withId("184aae84-a5bf-4c6a-85ba-4a7c73026cd5") + .withName("Online") + .withCode("E")))); Reader reader = new MarcBibReaderFactory().createReader(); reader.initialize(eventPayload, mappingContext); @@ -506,11 +523,6 @@ public void shouldCreateMultipleHoldingsUsingMappingRuleWithElseStatement() thro context.put(MARC_BIBLIOGRAPHIC.value(), Json.encodePrettily(record)); eventPayload.setContext(context); - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put("fcd64ce1-6995-48f0-840e-89ffa2288371", "Main Library (KU/CC/DI/M)"); - acceptedValues.put("53cf956f-c1df-410b-8bea-27f712cca7c0", "Annex (KU/CC/DI/A)"); - acceptedValues.put("184aae84-a5bf-4c6a-85ba-4a7c73026cd5", "Online (E)"); - MappingDetail mappingDetails = new MappingDetail() .withName("holdings") .withRecordType(HOLDINGS) @@ -518,14 +530,12 @@ public void shouldCreateMultipleHoldingsUsingMappingRuleWithElseStatement() thro .withName("permanentLocationId") .withEnabled("true") .withPath("holdings.permanentLocationId") - .withValue("945$h") - .withAcceptedValues(acceptedValues), + .withValue("945$h"), new MappingRule() .withName("temporaryLocationId") .withEnabled("true") .withPath("holdings.temporaryLocationId") - .withValue("945$a") - .withAcceptedValues(acceptedValues), + .withValue("945$a"), new MappingRule() .withName("statisticalCodeIds") .withEnabled("true") @@ -559,10 +569,19 @@ public void shouldCreateMultipleHoldingsUsingMappingRuleWithElseStatement() thro MappingContext mappingContext = new MappingContext() .withMappingParameters(new MappingParameters() - .withLocations(Lists.newArrayList(Lists.newArrayList(new Location() - .withId("fcd64ce1-6995-48f0-840e-89ffa2288371") - .withName("Main Library") - .withCode("KU/CC/DI/M"))))); + .withLocations(List.of( + new Location() + .withId("fcd64ce1-6995-48f0-840e-89ffa2288371") + .withName("Main Library") + .withCode("KU/CC/DI/M"), + new Location() + .withId("53cf956f-c1df-410b-8bea-27f712cca7c0") + .withName("Annex") + .withCode("KU/CC/DI/A"), + new Location() + .withId("184aae84-a5bf-4c6a-85ba-4a7c73026cd5") + .withName("Online") + .withCode("E")))); Reader reader = new MarcBibReaderFactory().createReader(); reader.initialize(eventPayload, mappingContext); @@ -608,11 +627,6 @@ public void shouldCreateOneHoldingIfNoLocationMarcField() throws IOException { context.put(MARC_BIBLIOGRAPHIC.value(), Json.encodePrettily(record)); eventPayload.setContext(context); - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put("fcd64ce1-6995-48f0-840e-89ffa2288371", "Main Library (KU/CC/DI/M)"); - acceptedValues.put("53cf956f-c1df-410b-8bea-27f712cca7c0", "Annex (KU/CC/DI/A)"); - acceptedValues.put("184aae84-a5bf-4c6a-85ba-4a7c73026cd5", "Online (E)"); - MappingDetail mappingDetails = new MappingDetail() .withName("holdings") .withRecordType(HOLDINGS) @@ -620,14 +634,12 @@ public void shouldCreateOneHoldingIfNoLocationMarcField() throws IOException { .withName("permanentLocationId") .withEnabled("true") .withPath("holdings.permanentLocationId") - .withValue("945$h; else \"Online (E)\"") - .withAcceptedValues(acceptedValues), + .withValue("945$h; else \"Online (E)\""), new MappingRule() .withName("temporaryLocationId") .withEnabled("true") .withPath("holdings.temporaryLocationId") - .withValue("945$a") - .withAcceptedValues(acceptedValues), + .withValue("945$a"), new MappingRule() .withName("statisticalCodeIds") .withEnabled("true") @@ -661,10 +673,19 @@ public void shouldCreateOneHoldingIfNoLocationMarcField() throws IOException { MappingContext mappingContext = new MappingContext() .withMappingParameters(new MappingParameters() - .withLocations(Lists.newArrayList(Lists.newArrayList(new Location() - .withId("fcd64ce1-6995-48f0-840e-89ffa2288371") - .withName("Main Library") - .withCode("KU/CC/DI/M"))))); + .withLocations(List.of( + new Location() + .withId("fcd64ce1-6995-48f0-840e-89ffa2288371") + .withName("Main Library") + .withCode("KU/CC/DI/M"), + new Location() + .withId("53cf956f-c1df-410b-8bea-27f712cca7c0") + .withName("Annex") + .withCode("KU/CC/DI/A"), + new Location() + .withId("184aae84-a5bf-4c6a-85ba-4a7c73026cd5") + .withName("Online") + .withCode("E")))); Reader reader = new MarcBibReaderFactory().createReader(); reader.initialize(eventPayload, mappingContext); @@ -693,11 +714,6 @@ public void shouldCreateSingleHoldingIfLocationsAreTheSame() throws IOException context.put(MARC_BIBLIOGRAPHIC.value(), Json.encodePrettily(record)); eventPayload.setContext(context); - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put("fcd64ce1-6995-48f0-840e-89ffa2288371", "Main Library (KU/CC/DI/M)"); - acceptedValues.put("53cf956f-c1df-410b-8bea-27f712cca7c0", "Annex (KU/CC/DI/A)"); - acceptedValues.put("184aae84-a5bf-4c6a-85ba-4a7c73026cd5", "Online (E)"); - MappingDetail mappingDetails = new MappingDetail() .withName("holdings") .withRecordType(HOLDINGS) @@ -705,8 +721,7 @@ public void shouldCreateSingleHoldingIfLocationsAreTheSame() throws IOException .withName("permanentLocationId") .withEnabled("true") .withPath("holdings.permanentLocationId") - .withValue("945$h") - .withAcceptedValues(acceptedValues))); + .withValue("945$h"))); MappingProfile profile = new MappingProfile() .withId(UUID.randomUUID().toString()) @@ -717,10 +732,19 @@ public void shouldCreateSingleHoldingIfLocationsAreTheSame() throws IOException MappingContext mappingContext = new MappingContext() .withMappingParameters(new MappingParameters() - .withLocations(Lists.newArrayList(Lists.newArrayList(new Location() - .withId("fcd64ce1-6995-48f0-840e-89ffa2288371") - .withName("Main Library") - .withCode("KU/CC/DI/M"))))); + .withLocations(List.of( + new Location() + .withId("fcd64ce1-6995-48f0-840e-89ffa2288371") + .withName("Main Library") + .withCode("KU/CC/DI/M"), + new Location() + .withId("53cf956f-c1df-410b-8bea-27f712cca7c0") + .withName("Annex") + .withCode("KU/CC/DI/A"), + new Location() + .withId("184aae84-a5bf-4c6a-85ba-4a7c73026cd5") + .withName("Online") + .withCode("E")))); Reader reader = new MarcBibReaderFactory().createReader(); reader.initialize(eventPayload, mappingContext); @@ -754,11 +778,6 @@ public void shouldNotCreateOneHoldingsIfProfileIsInvalid() throws IOException { context.put(MARC_BIBLIOGRAPHIC.value(), Json.encodePrettily(record)); eventPayload.setContext(context); - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put("fcd64ce1-6995-48f0-840e-89ffa2288371", "Main Library (KU/CC/DI/M)"); - acceptedValues.put("53cf956f-c1df-410b-8bea-27f712cca7c0", "Annex (KU/CC/DI/A)"); - acceptedValues.put("184aae84-a5bf-4c6a-85ba-4a7c73026cd5", "Online (E)"); - MappingDetail mappingDetails = null; MappingProfile profile = new MappingProfile() @@ -770,10 +789,19 @@ public void shouldNotCreateOneHoldingsIfProfileIsInvalid() throws IOException { MappingContext mappingContext = new MappingContext() .withMappingParameters(new MappingParameters() - .withLocations(Lists.newArrayList(Lists.newArrayList(new Location() - .withId("fcd64ce1-6995-48f0-840e-89ffa2288371") - .withName("Main Library") - .withCode("KU/CC/DI/M"))))); + .withLocations(List.of( + new Location() + .withId("fcd64ce1-6995-48f0-840e-89ffa2288371") + .withName("Main Library") + .withCode("KU/CC/DI/M"), + new Location() + .withId("53cf956f-c1df-410b-8bea-27f712cca7c0") + .withName("Annex") + .withCode("KU/CC/DI/A"), + new Location() + .withId("184aae84-a5bf-4c6a-85ba-4a7c73026cd5") + .withName("Online") + .withCode("E")))); Reader reader = new MarcBibReaderFactory().createReader(); reader.initialize(eventPayload, mappingContext); @@ -831,11 +859,6 @@ public void shouldUpdateSingleHoldingsButWithoutDuplicatedLocations() throws IOE context.put(MARC_BIBLIOGRAPHIC.value(), Json.encodePrettily(record)); eventPayload.setContext(context); - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put("fcd64ce1-6995-48f0-840e-89ffa2288371", "Main Library (KU/CC/DI/M)"); - acceptedValues.put("53cf956f-c1df-410b-8bea-27f712cca7c0", "Annex (KU/CC/DI/A)"); - acceptedValues.put("184aae84-a5bf-4c6a-85ba-4a7c73026cd5", "Online (E)"); - MappingDetail mappingDetails = new MappingDetail() .withName("holdings") .withRecordType(HOLDINGS) @@ -843,14 +866,12 @@ public void shouldUpdateSingleHoldingsButWithoutDuplicatedLocations() throws IOE .withName("permanentLocationId") .withEnabled("true") .withPath("holdings.permanentLocationId") - .withValue("945$h") - .withAcceptedValues(acceptedValues), + .withValue("945$h"), new MappingRule() .withName("temporaryLocationId") .withEnabled("true") .withPath("holdings.temporaryLocationId") - .withValue("945$a") - .withAcceptedValues(acceptedValues), + .withValue("945$a"), new MappingRule() .withName("statisticalCodeIds") .withEnabled("true") @@ -884,10 +905,19 @@ public void shouldUpdateSingleHoldingsButWithoutDuplicatedLocations() throws IOE MappingContext mappingContext = new MappingContext() .withMappingParameters(new MappingParameters() - .withLocations(Lists.newArrayList(Lists.newArrayList(new Location() - .withId("fcd64ce1-6995-48f0-840e-89ffa2288371") - .withName("Main Library") - .withCode("KU/CC/DI/M"))))); + .withLocations(List.of( + new Location() + .withId("fcd64ce1-6995-48f0-840e-89ffa2288371") + .withName("Main Library") + .withCode("KU/CC/DI/M"), + new Location() + .withId("53cf956f-c1df-410b-8bea-27f712cca7c0") + .withName("Annex") + .withCode("KU/CC/DI/A"), + new Location() + .withId("184aae84-a5bf-4c6a-85ba-4a7c73026cd5") + .withName("Online") + .withCode("E")))); Reader reader = new MarcBibReaderFactory().createReader(); reader.initialize(eventPayload, mappingContext); diff --git a/src/test/java/org/folio/processing/mapping/reader/AcceptedValuesUtilTest.java b/src/test/java/org/folio/processing/mapping/reader/AcceptedValuesUtilTest.java new file mode 100644 index 00000000..252da5e8 --- /dev/null +++ b/src/test/java/org/folio/processing/mapping/reader/AcceptedValuesUtilTest.java @@ -0,0 +1,167 @@ +package org.folio.processing.mapping.reader; + +import org.folio.AcquisitionMethod; +import org.folio.AcquisitionsUnit; +import org.folio.CallNumberType; +import org.folio.ContributorNameType; +import org.folio.ElectronicAccessRelationship; +import org.folio.ExpenseClass; +import org.folio.Fund; +import org.folio.HoldingsNoteType; +import org.folio.HoldingsType; +import org.folio.IdentifierType; +import org.folio.IllPolicy; +import org.folio.InstanceRelationshipType; +import org.folio.InstanceStatus; +import org.folio.ItemDamageStatus; +import org.folio.ItemNoteType; +import org.folio.Loantype; +import org.folio.Location; +import org.folio.Mtype; +import org.folio.NatureOfContentTerm; +import org.folio.Organization; +import org.folio.StatisticalCode; +import org.folio.StatisticalCodeType; +import org.folio.processing.mapping.defaultmapper.processor.parameters.MappingParameters; +import org.folio.processing.mapping.mapper.util.AcceptedValuesUtil; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +@RunWith(JUnit4.class) +public class AcceptedValuesUtilTest { + private static final List INSTANCE_ACCEPTED_VALUES_RULES = + List.of("statusId", "natureOfContentTermId", "instanceRelationshipTypeId"); + private static final List HOLDINGS_ACCEPTED_VALUES_RULES = + List.of("holdingsTypeId", "permanentLocationId", "temporaryLocationId", "callNumberTypeId", "illPolicyId", "noteType", "relationshipId"); + private static final List ITEM_ACCEPTED_VALUES_RULES = + List.of("materialType.id", "itemLevelCallNumberTypeId", "itemDamagedStatusId", "itemNoteTypeId", "permanentLoanType.id", + "temporaryLoanType.id", "permanentLocation.id", "temporaryLocation.id"); + private static final List ORDER_ACCEPTED_VALUES_RULES = + List.of("acqUnitIds", "billTo", "shipTo", "contributorNameTypeId", "productIdType", + "acquisitionMethod", "fundId", "expenseClassId", "locationId", "materialType", "accessProvider", "vendor", "materialSupplier", "donorOrganizationIds"); + private static final String TEST_NAME = "testName"; + public static final String TEST_ADDRESS_TEMPLATE = "{\"id\":\"%s\", \"name\":\"%s\",\"address\":\"Test2\"}"; + + @Test + public void testInstanceAcceptedValues() { + String testUUID = UUID.randomUUID().toString(); + + MappingParameters mappingParameters = new MappingParameters() + .withInstanceStatuses(List.of(new InstanceStatus().withId(testUUID + "statusId").withName(TEST_NAME))) + .withNatureOfContentTerms(List.of(new NatureOfContentTerm().withId(testUUID + "natureOfContentTermId").withName(TEST_NAME))) + .withInstanceRelationshipTypes(List.of(new InstanceRelationshipType().withId(testUUID + "instanceRelationshipTypeId").withName(TEST_NAME))); + + testAcceptedValues(INSTANCE_ACCEPTED_VALUES_RULES, mappingParameters, testUUID); + } + + @Test + public void testHoldingsAcceptedValues() { + String testUUID = UUID.randomUUID().toString(); + + MappingParameters mappingParameters = new MappingParameters() + .withHoldingsTypes(List.of(new HoldingsType().withId(testUUID + "holdingsTypeId").withName(TEST_NAME))) + .withLocations(List.of(new Location().withId(testUUID + "permanentLocationId").withName(TEST_NAME), new Location().withId(testUUID + "temporaryLocationId").withName(TEST_NAME))) + .withCallNumberTypes(List.of(new CallNumberType().withId(testUUID + "callNumberTypeId").withName(TEST_NAME))) + .withIllPolicies(List.of(new IllPolicy().withId(testUUID + "illPolicyId").withName(TEST_NAME))) + .withHoldingsNoteTypes(List.of(new HoldingsNoteType().withId(testUUID + "noteType").withName(TEST_NAME))) + .withElectronicAccessRelationships(List.of(new ElectronicAccessRelationship().withId(testUUID + "relationshipId").withName(TEST_NAME))); + + testAcceptedValues(HOLDINGS_ACCEPTED_VALUES_RULES, mappingParameters, testUUID); + } + + @Test + public void testItemAcceptedValues() { + String testUUID = UUID.randomUUID().toString(); + + MappingParameters mappingParameters = new MappingParameters() + .withMaterialTypes(List.of(new Mtype().withId(testUUID + "materialType.id").withName(TEST_NAME))) + .withCallNumberTypes(List.of(new CallNumberType().withId(testUUID + "itemLevelCallNumberTypeId").withName(TEST_NAME))) + .withItemDamagedStatuses(List.of(new ItemDamageStatus().withId(testUUID + "itemDamagedStatusId").withName(TEST_NAME))) + .withItemNoteTypes(List.of(new ItemNoteType().withId(testUUID + "itemNoteTypeId").withName(TEST_NAME))) + .withLoanTypes(List.of(new Loantype().withId(testUUID + "permanentLoanType.id").withName(TEST_NAME), new Loantype().withId(testUUID + "temporaryLoanType.id").withName(TEST_NAME))) + .withLocations(List.of(new Location().withId(testUUID + "permanentLocation.id").withName(TEST_NAME), new Location().withId(testUUID + "temporaryLocation.id").withName(TEST_NAME))); + + testAcceptedValues(ITEM_ACCEPTED_VALUES_RULES, mappingParameters, testUUID); + } + + @Test + public void testOrderAcceptedValues() { + String testUUID = UUID.randomUUID().toString(); + + MappingParameters mappingParameters = new MappingParameters() + .withAcquisitionsUnits(List.of(new AcquisitionsUnit().withId(testUUID + "acqUnitIds").withName(TEST_NAME))) + .withTenantConfigurationAddresses(List.of(String.format(TEST_ADDRESS_TEMPLATE, testUUID + "billTo", TEST_NAME), + String.format(TEST_ADDRESS_TEMPLATE, testUUID + "shipTo", TEST_NAME))) + .withContributorNameTypes(List.of(new ContributorNameType().withId(testUUID + "contributorNameTypeId").withName(TEST_NAME))) + .withIdentifierTypes(List.of(new IdentifierType().withId(testUUID + "productIdType").withName(TEST_NAME))) + .withAcquisitionMethods(List.of(new AcquisitionMethod().withId(testUUID + "acquisitionMethod").withValue(TEST_NAME))) + .withFunds(List.of(new Fund().withId(testUUID + "fundId").withName(TEST_NAME))) + .withExpenseClasses(List.of(new ExpenseClass().withId(testUUID + "expenseClassId").withName(TEST_NAME))) + .withLocations(List.of(new Location().withId(testUUID + "locationId").withName(TEST_NAME))) + .withMaterialTypes(List.of(new Mtype().withId(testUUID + "materialType").withName(TEST_NAME))) + .withOrganizations(List.of(new Organization().withId(testUUID + "accessProvider").withName(TEST_NAME), + new Organization().withId(testUUID + "vendor").withName(TEST_NAME), + new Organization().withId(testUUID + "donorOrganizationIds").withIsDonor(true).withName(TEST_NAME), + new Organization().withId(testUUID + "materialSupplier").withName(TEST_NAME))); + + testAcceptedValues(ORDER_ACCEPTED_VALUES_RULES, mappingParameters, testUUID); + } + + @Test + public void shouldReturnEmptyAcceptedValuesIfIdIsNull() { + Map map = AcceptedValuesUtil.getAcceptedValues("billTo", + new MappingParameters().withTenantConfigurationAddresses(List.of("{\"name\":\"test\",\"address\":\"Test2\"}"))); + + assertTrue(map.isEmpty()); + } + + @Test + public void shouldReturnEmptyAcceptedValuesIfNameIsNull() { + Map map = AcceptedValuesUtil.getAcceptedValues("billTo", + new MappingParameters().withTenantConfigurationAddresses(List.of("{\"id\":\"test\",\"address\":\"Test2\"}"))); + + assertTrue(map.isEmpty()); + } + + @Test + public void shouldReturnEmptyAcceptedValuesIfRuleNameIsNull() { + Map map = AcceptedValuesUtil.getAcceptedValues(null, new MappingParameters()); + + assertTrue(map.isEmpty()); + } + + @Test + public void testStatisticalCodeFormation() { + String statCodeUUID = UUID.randomUUID().toString(); + String statCodeTypeUUID = UUID.randomUUID().toString(); + + MappingParameters mappingParameters = new MappingParameters() + .withStatisticalCodes(List.of(new StatisticalCode().withId(statCodeUUID).withName("Test Code").withCode("test").withStatisticalCodeTypeId(statCodeTypeUUID))) + .withStatisticalCodeTypes(List.of(new StatisticalCodeType().withId(statCodeTypeUUID).withName("TEST (testing)"))); + + Map acceptedValues = AcceptedValuesUtil.getAcceptedValues("statisticalCodeId", mappingParameters); + + assertFalse(acceptedValues.isEmpty()); + assertTrue(acceptedValues.containsKey(statCodeUUID)); + assertEquals("TEST (testing): test - Test Code", acceptedValues.get(statCodeUUID)); + } + + private void testAcceptedValues(List acceptedValuesRules, MappingParameters mappingParameters, String uuid) { + acceptedValuesRules.forEach(rule -> { + Map acceptedValues = AcceptedValuesUtil.getAcceptedValues(rule, mappingParameters); + String key = uuid + rule; + assertFalse(acceptedValues.isEmpty()); + assertTrue(acceptedValues.containsKey(key)); + assertEquals(TEST_NAME, acceptedValues.get(key)); + }); + } +} diff --git a/src/test/java/org/folio/processing/mapping/reader/MarcRecordReaderUnitTest.java b/src/test/java/org/folio/processing/mapping/reader/MarcRecordReaderUnitTest.java index 0cf71624..91922f6e 100644 --- a/src/test/java/org/folio/processing/mapping/reader/MarcRecordReaderUnitTest.java +++ b/src/test/java/org/folio/processing/mapping/reader/MarcRecordReaderUnitTest.java @@ -4,8 +4,14 @@ import io.vertx.core.json.Json; import io.vertx.core.json.JsonArray; import io.vertx.core.json.JsonObject; +import org.folio.AcquisitionsUnit; import org.folio.DataImportEventPayload; +import org.folio.ElectronicAccessRelationship; import org.folio.Holdings; +import org.folio.ItemNoteType; +import org.folio.Location; +import org.folio.NatureOfContentTerm; +import org.folio.Organization; import org.folio.ParsedRecord; import org.folio.Record; import org.folio.processing.mapping.MappingManager; @@ -40,6 +46,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.UUID; import static java.util.Collections.emptyList; import static java.util.Collections.singletonList; @@ -292,16 +299,16 @@ public void shouldRead_AcceptedStrings_FromRules() throws IOException { .withParsedRecord(new ParsedRecord().withContent(RECORD))).encode()); eventPayload.setContext(context); Reader reader = new MarcBibReaderFactory().createReader(); - reader.initialize(eventPayload, mappingContext); - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put("randomUUID", "value"); - acceptedValues.put("randomUUID2", "noValue"); + List locations = List.of( + new Location().withName("value").withId("randomUUID"), + new Location().withName("noValue").withId("randomUUID2")); + reader.initialize(eventPayload, mappingContext.withMappingParameters(new MappingParameters().withLocations(locations))); + // when Value value = reader.read(new MappingRule() - .withName("testField") + .withName("permanentLocationId") .withPath("") - .withValue("\"test\" \" \" \"value\" \" \"") - .withAcceptedValues(acceptedValues)); + .withValue("\"test\" \" \" \"value\" \" \"")); // then assertNotNull(value); assertEquals(ValueType.STRING, value.getType()); @@ -495,12 +502,12 @@ public void shouldReadRepeatableFieldAndCreateFieldItemPerEverySpecifiedField() context.put(MARC_BIBLIOGRAPHIC.value(), JsonObject.mapFrom(new Record() .withParsedRecord(new ParsedRecord().withContent(RECORD_WITH_MULTIPLE_856))).encode()); eventPayload.setContext(context); - Reader reader = new MarcBibReaderFactory().createReader(); - reader.initialize(eventPayload, mappingContext); - String uuid = "f5d0068e-6272-458e-8a81-b85e7b9a14aa"; - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put(uuid, String.format("Resource (%s)", uuid)); + List electronicAccessRelationships = List.of( + new ElectronicAccessRelationship().withId(uuid).withName("Resource")); + + Reader reader = new MarcBibReaderFactory().createReader(); + reader.initialize(eventPayload, mappingContext.withMappingParameters(new MappingParameters().withElectronicAccessRelationships(electronicAccessRelationships))); List listRules = new ArrayList<>(); @@ -508,20 +515,17 @@ public void shouldReadRepeatableFieldAndCreateFieldItemPerEverySpecifiedField() .withName("uri") .withPath("holdings.electronicAccess[].uri") .withEnabled("true") - .withValue("856$u") - .withAcceptedValues(acceptedValues)); + .withValue("856$u")); listRules.add(new MappingRule() .withName("relationshipId") .withPath("holdings.electronicAccess[].relationshipId") .withEnabled("true") - .withValue("\"f5d0068e-6272-458e-8a81-b85e7b9a14aa\"") - .withAcceptedValues(acceptedValues)); + .withValue("\"f5d0068e-6272-458e-8a81-b85e7b9a14aa\"")); listRules.add(new MappingRule() .withName("linkText") .withPath("holdings.electronicAccess[].linkText") .withEnabled("true") - .withValue("856$z") - .withAcceptedValues(acceptedValues)); + .withValue("856$z")); Value value = reader.read(new MappingRule() .withName("electronicAccess") @@ -557,13 +561,12 @@ public void shouldLeftAnEmptyValueIfRelationShipIdIsInvalid() throws IOException context.put(MARC_BIBLIOGRAPHIC.value(), JsonObject.mapFrom(new Record() .withParsedRecord(new ParsedRecord().withContent(RECORD_WITH_MULTIPLE_856))).encode()); eventPayload.setContext(context); - Reader reader = new MarcBibReaderFactory().createReader(); - reader.initialize(eventPayload, mappingContext); - - String uuid = "UUID"; - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put(uuid, String.format("Resource (%s)", uuid)); + List electronicAccessRelationships = List.of( + new ElectronicAccessRelationship().withId(uuid).withName("Resource")); + + Reader reader = new MarcBibReaderFactory().createReader(); + reader.initialize(eventPayload, mappingContext.withMappingParameters(new MappingParameters().withElectronicAccessRelationships(electronicAccessRelationships))); List listRules = new ArrayList<>(); @@ -571,26 +574,22 @@ public void shouldLeftAnEmptyValueIfRelationShipIdIsInvalid() throws IOException .withName("uri") .withPath("holdings.electronicAccess[].uri") .withEnabled("true") - .withValue("856$u") - .withAcceptedValues(acceptedValues)); + .withValue("856$u")); listRules.add(new MappingRule() .withName("relationshipId") .withPath("holdings.electronicAccess[].relationshipId") .withEnabled("true") - .withValue("\"Resourcce\"") - .withAcceptedValues(acceptedValues)); + .withValue("\"Resourcce\"")); listRules.add(new MappingRule() .withName("linkText") .withPath("holdings.electronicAccess[].linkText") .withEnabled("true") - .withValue("856$z") - .withAcceptedValues(acceptedValues)); + .withValue("856$z")); Value value = reader.read(new MappingRule() .withName("electronicAccess") .withPath("holdings") .withRepeatableFieldAction(EXTEND_EXISTING) - .withAcceptedValues(acceptedValues) .withSubfields(singletonList(new RepeatableSubfieldMapping() .withOrder(0) .withPath("holdings.electronicAccess[]") @@ -615,18 +614,17 @@ public void shouldLeftAnEmptyValueIfRelationShipIdIsInvalid() throws IOException } @Test - public void shouldReadRepeatableFieldWithAcceptedValues() throws IOException { + public void shouldReadRepeatableField() throws IOException { DataImportEventPayload eventPayload = new DataImportEventPayload(); HashMap context = new HashMap<>(); context.put(MARC_BIBLIOGRAPHIC.value(), JsonObject.mapFrom(new Record() .withParsedRecord(new ParsedRecord().withContent(RECORD_WITH_MULTIPLE_876))).encode()); eventPayload.setContext(context); + List itemNoteTypes = List.of( + new ItemNoteType().withId("UUID1").withName("Binding"), + new ItemNoteType().withId("UUID2").withName("Electronic bookplate")); Reader reader = new MarcBibReaderFactory().createReader(); - reader.initialize(eventPayload, mappingContext); - - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put("UUID1", "Binding"); - acceptedValues.put("UUID2", "Electronic bookplate"); + reader.initialize(eventPayload, mappingContext.withMappingParameters(new MappingParameters().withItemNoteTypes(itemNoteTypes))); List listRules = new ArrayList<>(); @@ -634,8 +632,7 @@ public void shouldReadRepeatableFieldWithAcceptedValues() throws IOException { .withName("itemNoteTypeId") .withPath("item.notes[].itemNoteTypeId") .withEnabled("true") - .withValue("876$t") - .withAcceptedValues(acceptedValues)); + .withValue("876$t")); listRules.add(new MappingRule() .withName("note") .withPath("item.notes[].note") @@ -755,7 +752,7 @@ public void shouldReturnEmptyRepeatableFieldValueWhenHasNoDataForRequiredFieldSt List.of(new RepeatableSubfieldMapping().withOrder(0).withPath("item.statisticalCodeIds[]") .withFields( List.of(new MappingRule().withName("statisticalCodeId").withEnabled("true").withPath("item.statisticalCodeIds[]") - .withEnabled("true").withValue("949$s").withAcceptedValues(new HashMap<>()))))); + .withEnabled("true").withValue("949$s"))))); Value value = reader.read(mappingRule); assertNotNull(value); @@ -957,26 +954,23 @@ public void shouldRead_MARCFieldsArrayWithRepeatableFieldAction_FromRules() thro context.put(MARC_BIBLIOGRAPHIC.value(), JsonObject.mapFrom(new Record() .withParsedRecord(new ParsedRecord().withContent(RECORD))).encode()); eventPayload.setContext(context); + List natureOfContentTerms = List.of( + new NatureOfContentTerm().withId("UUID1").withName("website"), + new NatureOfContentTerm().withId("UUID2").withName("school program"), + new NatureOfContentTerm().withId("UUID3").withName("literature report")); Reader reader = new MarcBibReaderFactory().createReader(); - reader.initialize(eventPayload, mappingContext); - - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put("UUID1", "website"); - acceptedValues.put("UUID2", "school program"); - acceptedValues.put("UUID3", "literature report"); + reader.initialize(eventPayload, mappingContext.withMappingParameters(new MappingParameters().withNatureOfContentTerms(natureOfContentTerms))); MappingRule fieldRule1 = new MappingRule() - .withName("natureOfContentTermIds") + .withName("natureOfContentTermId") .withPath("instance.natureOfContentTermIds[]") .withEnabled("true") - .withValue("\"school program\"") - .withAcceptedValues(acceptedValues); + .withValue("\"school program\""); MappingRule fieldRule2 = new MappingRule() - .withName("natureOfContentTermIds") + .withName("natureOfContentTermId") .withPath("instance.natureOfContentTermIds[]") .withEnabled("true") - .withValue("\"literature report\"") - .withAcceptedValues(acceptedValues); + .withValue("\"literature report\""); MappingRule mappingRule = new MappingRule() .withPath("instance.natureOfContentTermIds[]") @@ -1004,7 +998,7 @@ public void shouldRead_MARCFieldsArrayWithRepeatableFieldAction_FromRules() thro @Test public void shouldRead_MARCFieldsArrayWithRepeatableFieldWithMARCValue_FromRules() throws IOException { // given - List expectedFields = Arrays.asList("pcc", "UUID3"); + List expectedFields = Arrays.asList("pcc", "literature report"); DataImportEventPayload eventPayload = new DataImportEventPayload(); HashMap context = new HashMap<>(); context.put(MARC_BIBLIOGRAPHIC.value(), JsonObject.mapFrom(new Record() @@ -1013,35 +1007,28 @@ public void shouldRead_MARCFieldsArrayWithRepeatableFieldWithMARCValue_FromRules Reader reader = new MarcBibReaderFactory().createReader(); reader.initialize(eventPayload, mappingContext); - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put("UUID1", "website"); - acceptedValues.put("UUID2", "school program"); - acceptedValues.put("UUID3", "literature report"); - MappingRule fieldRule1 = new MappingRule() .withName("formerIds") - .withPath("instance.formerIds[]") + .withPath("holdings.formerIds[]") .withEnabled("true") - .withValue("042$a") - .withAcceptedValues(acceptedValues); + .withValue("042$a"); MappingRule fieldRule2 = new MappingRule() .withName("formerIds") - .withPath("instance.formerIds[]") + .withPath("holdings.formerIds[]") .withEnabled("true") - .withValue("\"literature report\"") - .withAcceptedValues(acceptedValues); + .withValue("\"literature report\""); MappingRule mappingRule = new MappingRule() - .withPath("instance.formerIds[]") + .withPath("holdings.formerIds[]") .withRepeatableFieldAction(EXTEND_EXISTING) .withSubfields(Arrays.asList( new RepeatableSubfieldMapping() .withOrder(0) - .withPath("instance.formerIds[]") + .withPath("holdings.formerIds[]") .withFields(singletonList(fieldRule1)), new RepeatableSubfieldMapping() .withOrder(0) - .withPath("instance.formerIds[]") + .withPath("holdings.formerIds[]") .withFields(singletonList(fieldRule2)))); // when @@ -1079,20 +1066,20 @@ public void shouldReadSpecificPermanentLocationWithBrackets() throws IOException context.put(MARC_BIBLIOGRAPHIC.value(), JsonObject.mapFrom(new Record() .withParsedRecord(new ParsedRecord().withContent(RECORD_WITH_049))).encode()); eventPayload.setContext(context); - Reader reader = new MarcBibReaderFactory().createReader(); - reader.initialize(eventPayload, mappingContext); String expectedId = "fcd64ce1-6995-48f0-840e-89ffa2288371"; - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put("184aae84-a5bf-4c6a-85ba-4a7c73026cd5", "Online (E)"); - acceptedValues.put(expectedId, "Main Library (KU/CC/DI/M)"); - acceptedValues.put("758258bc-ecc1-41b8-abca-f7b610822ffd", "ORWIG ETHNO CD (KU/CC/DI/O)"); - acceptedValues.put("f34d27c6-a8eb-461b-acd6-5dea81771e70", "SECOND FLOOR (KU/CC/DI/2)"); + List locations = List.of( + new Location().withId("184aae84-a5bf-4c6a-85ba-4a7c73026cd5").withName("Online").withCode("E"), + new Location().withId(expectedId).withName("Main Library").withCode("KU/CC/DI/M"), + new Location().withId("758258bc-ecc1-41b8-abca-f7b610822ffd").withName("ORWIG ETHNO CD").withCode("KU/CC/DI/O"), + new Location().withId("f34d27c6-a8eb-461b-acd6-5dea81771e70").withName("SECOND FLOOR").withCode("KU/CC/DI/2")); + + Reader reader = new MarcBibReaderFactory().createReader(); + reader.initialize(eventPayload, mappingContext.withMappingParameters(new MappingParameters().withLocations(locations))); Value value = reader.read(new MappingRule() .withName("permanentLocationId") .withPath("holdings.permanentLocationId") - .withValue("049$a") - .withAcceptedValues(acceptedValues)); + .withValue("049$a")); assertNotNull(value); assertEquals(ValueType.STRING, value.getType()); @@ -1106,20 +1093,20 @@ public void shouldReadEqualsPermanentLocationWithBracketsIfContainsSameCode() th context.put(MARC_BIBLIOGRAPHIC.value(), JsonObject.mapFrom(new Record() .withParsedRecord(new ParsedRecord().withContent(RECORD_WITH_049))).encode()); eventPayload.setContext(context); - Reader reader = new MarcBibReaderFactory().createReader(); - reader.initialize(eventPayload, mappingContext); String expectedId = "fcd64ce1-6995-48f0-840e-89ffa2288371"; - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put("758258bc-ecc1-41b8-abca-f7b610822ffd", "ORWIG ETHNO CD (KU/CC/DI/MO)"); - acceptedValues.put("184aae84-a5bf-4c6a-85ba-4a7c73026cd5", "Online (KU/CC/DI/MI)"); - acceptedValues.put(expectedId, "Main Library (KU/CC/DI/M)"); - acceptedValues.put("f34d27c6-a8eb-461b-acd6-5dea81771e70", "SECOND FLOOR (KU/CC/DI/MU)"); + List locations = List.of( + new Location().withId("184aae84-a5bf-4c6a-85ba-4a7c73026cd5").withName("Online").withCode("E"), + new Location().withId(expectedId).withName("Main Library").withCode("KU/CC/DI/M"), + new Location().withId("758258bc-ecc1-41b8-abca-f7b610822ffd").withName("ORWIG ETHNO CD").withCode("KU/CC/DI/O"), + new Location().withId("f34d27c6-a8eb-461b-acd6-5dea81771e70").withName("SECOND FLOOR").withCode("KU/CC/DI/2")); + + Reader reader = new MarcBibReaderFactory().createReader(); + reader.initialize(eventPayload, mappingContext.withMappingParameters(new MappingParameters().withLocations(locations))); Value value = reader.read(new MappingRule() .withName("permanentLocationId") .withPath("holdings.permanentLocationId") - .withValue("049$a") - .withAcceptedValues(acceptedValues)); + .withValue("049$a")); assertNotNull(value); assertEquals(ValueType.STRING, value.getType()); @@ -1133,19 +1120,19 @@ public void shouldNotReadPermanentLocationWithBracketsNotEqualsCode() throws IOE context.put(MARC_BIBLIOGRAPHIC.value(), JsonObject.mapFrom(new Record() .withParsedRecord(new ParsedRecord().withContent(RECORD_WITH_049))).encode()); eventPayload.setContext(context); + List locations = List.of( + new Location().withId("184aae84-a5bf-4c6a-85ba-4a7c73026cd5").withName("Online").withCode("KU/CC/DI/MI"), + new Location().withId("fcd64ce1-6995-48f0-840e-89ffa2288371").withName("Main Library").withCode("KU/CC/DI/MK"), + new Location().withId("758258bc-ecc1-41b8-abca-f7b610822ffd").withName("ORWIG ETHNO CD").withCode("KU/CC/DI/MO"), + new Location().withId("f34d27c6-a8eb-461b-acd6-5dea81771e70").withName("SECOND FLOOR").withCode("KU/CC/DI/VU")); + Reader reader = new MarcBibReaderFactory().createReader(); - reader.initialize(eventPayload, mappingContext); - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put("758258bc-ecc1-41b8-abca-f7b610822ffd", "ORWIG ETHNO CD (KU/CC/DI/MO)"); - acceptedValues.put("184aae84-a5bf-4c6a-85ba-4a7c73026cd5", "Online (KU/CC/DI/MI)"); - acceptedValues.put("fcd64ce1-6995-48f0-840e-89ffa2288371", "Main Library (KU/CC/DI/MK)"); - acceptedValues.put("f34d27c6-a8eb-461b-acd6-5dea81771e70", "SECOND FLOOR (KU/CC/DI/VU)"); + reader.initialize(eventPayload, mappingContext.withMappingParameters(new MappingParameters().withLocations(locations))); Value value = reader.read(new MappingRule() .withName("permanentLocationId") .withPath("holdings.permanentLocationId") - .withValue("049$a") - .withAcceptedValues(acceptedValues)); + .withValue("049$a")); assertNotNull(value); assertEquals(ValueType.STRING, value.getType()); @@ -1160,19 +1147,19 @@ public void shouldNotReadPermanentLocationWithoutBrackets() throws IOException { context.put(MARC_BIBLIOGRAPHIC.value(), JsonObject.mapFrom(new Record() .withParsedRecord(new ParsedRecord().withContent(RECORD_WITH_049))).encode()); eventPayload.setContext(context); + List locations = List.of( + new Location().withId("184aae84-a5bf-4c6a-85ba-4a7c73026cd5").withName("Online").withCode("KU/CC/DI/MI"), + new Location().withId("fcd64ce1-6995-48f0-840e-89ffa2288371").withName("Main Library").withCode("KU/CC/DI/MK"), + new Location().withId("758258bc-ecc1-41b8-abca-f7b610822ffd").withName("ORWIG ETHNO CD").withCode("KU/CC/DI/MO"), + new Location().withId("f34d27c6-a8eb-461b-acd6-5dea81771e70").withName("SECOND FLOOR").withCode("KU/CC/DI/VU")); + Reader reader = new MarcBibReaderFactory().createReader(); - reader.initialize(eventPayload, mappingContext); - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put("758258bc-ecc1-41b8-abca-f7b610822ffd", "ORWIG ETHNO CD (KU/CC/DI/MO)"); - acceptedValues.put("184aae84-a5bf-4c6a-85ba-4a7c73026cd5", "Online (KU/CC/DI/MI)"); - acceptedValues.put("fcd64ce1-6995-48f0-840e-89ffa2288371", "Main Library KU/CC/DI/M"); - acceptedValues.put("f34d27c6-a8eb-461b-acd6-5dea81771e70", "SECOND FLOOR (KU/CC/DI/VU)"); + reader.initialize(eventPayload, mappingContext.withMappingParameters(new MappingParameters().withLocations(locations))); Value value = reader.read(new MappingRule() .withName("permanentLocationId") .withPath("holdings.permanentLocationId") - .withValue("049$a") - .withAcceptedValues(acceptedValues)); + .withValue("049$a")); assertNotNull(value); assertEquals(ValueType.STRING, value.getType()); @@ -1186,19 +1173,19 @@ public void shouldReadPermanentLocationIfRecordContainsBrackets() throws IOExcep context.put(MARC_BIBLIOGRAPHIC.value(), JsonObject.mapFrom(new Record() .withParsedRecord(new ParsedRecord().withContent(RECORD_WITH_049_AND_BRACKETS))).encode()); eventPayload.setContext(context); + List locations = List.of( + new Location().withId("184aae84-a5bf-4c6a-85ba-4a7c73026cd5").withName("Online").withCode("KU/CC/DI/MI"), + new Location().withId("fcd64ce1-6995-48f0-840e-89ffa2288371").withName("Main Library").withCode("KU/CC/DI/M"), + new Location().withId("758258bc-ecc1-41b8-abca-f7b610822ffd").withName("ORWIG ETHNO CD").withCode("KU/CC/DI/MO"), + new Location().withId("f34d27c6-a8eb-461b-acd6-5dea81771e70").withName("SECOND FLOOR").withCode("KU/CC/DI/VU")); + Reader reader = new MarcBibReaderFactory().createReader(); - reader.initialize(eventPayload, mappingContext); - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put("758258bc-ecc1-41b8-abca-f7b610822ffd", "ORWIG ETHNO CD (KU/CC/DI/MO)"); - acceptedValues.put("184aae84-a5bf-4c6a-85ba-4a7c73026cd5", "Online (KU/CC/DI/MI)"); - acceptedValues.put("fcd64ce1-6995-48f0-840e-89ffa2288371", "Main Library (KU/CC/DI/M)"); - acceptedValues.put("f34d27c6-a8eb-461b-acd6-5dea81771e70", "SECOND FLOOR (KU/CC/DI/VU)"); + reader.initialize(eventPayload, mappingContext.withMappingParameters(new MappingParameters().withLocations(locations))); Value value = reader.read(new MappingRule() .withName("permanentLocationId") .withPath("holdings.permanentLocationId") - .withValue("049$a") - .withAcceptedValues(acceptedValues)); + .withValue("049$a")); assertNotNull(value); assertEquals(ValueType.STRING, value.getType()); @@ -1212,20 +1199,20 @@ public void shouldReadPermanentLocationFromTheLastBracketsEvenIfThereIsCommonVal context.put(MARC_BIBLIOGRAPHIC.value(), JsonObject.mapFrom(new Record() .withParsedRecord(new ParsedRecord().withContent(RECORD_WITH_049_WITH_OLI_LOCATION))).encode()); eventPayload.setContext(context); + List locations = List.of( + new Location().withId("184aae84-a5bf-4c6a-85ba-4a7c73026cd5").withName("Ils ali (Oli)").withCode("oli,ils"), + new Location().withId("fcd64ce1-6995-48f0-840e-89ffa2288371").withName("Oli als (Oli)").withCode("oli,als"), + new Location().withId("758258bc-ecc1-41b8-abca-f7b610822fff").withName("Oliss").withCode("oliss"), + new Location().withId("758258bc-ecc1-41b8-abca-f7b610822ffd").withName("Oli (Oli)").withCode("oli"), + new Location().withId("f34d27c6-a8eb-461b-acd6-5dea81771e70").withName("SECOND FLOOR").withCode("KU/CC/DI/VU")); + Reader reader = new MarcBibReaderFactory().createReader(); - reader.initialize(eventPayload, mappingContext); - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put("758258bc-ecc1-41b8-abca-f7b610822fff", "Oliss (oliss)"); - acceptedValues.put("f34d27c6-a8eb-461b-acd6-5dea81771e70", "SECOND FLOOR (KU/CC/DI/VU)"); - acceptedValues.put("184aae84-a5bf-4c6a-85ba-4a7c73026cd5", "Ils ali (Oli) (oli,ils)"); - acceptedValues.put("758258bc-ecc1-41b8-abca-f7b610822ffd", "Oli (Oli) (oli)"); - acceptedValues.put("fcd64ce1-6995-48f0-840e-89ffa2288371", "Oli als (Oli)(oli,als)"); + reader.initialize(eventPayload, mappingContext.withMappingParameters(new MappingParameters().withLocations(locations))); Value value = reader.read(new MappingRule() .withName("permanentLocationId") .withPath("holdings.permanentLocationId") - .withValue("049$a") - .withAcceptedValues(acceptedValues)); + .withValue("049$a")); assertNotNull(value); assertEquals(ValueType.STRING, value.getType()); @@ -1239,20 +1226,20 @@ public void shouldReadPermanentLocationFromTheLastBracketsWithSpecificLocation() context.put(MARC_BIBLIOGRAPHIC.value(), JsonObject.mapFrom(new Record() .withParsedRecord(new ParsedRecord().withContent(RECORD_WITH_049_WITH_OLI_ALS_LOCATION))).encode()); eventPayload.setContext(context); + List locations = List.of( + new Location().withId("758258bc-ecc1-41b8-abca-f7b610822fff").withName("Oliss").withCode("oliss"), + new Location().withId("f34d27c6-a8eb-461b-acd6-5dea81771e70").withName("SECOND FLOOR").withCode("KU/CC/DI/VU"), + new Location().withId("184aae84-a5bf-4c6a-85ba-4a7c73026cd5").withName("Ils ali (Oli)").withCode("oli,ils"), + new Location().withId("758258bc-ecc1-41b8-abca-f7b610822ffd").withName("Oli (Oli)").withCode("oli"), + new Location().withId("fcd64ce1-6995-48f0-840e-89ffa2288371").withName("Oli als (Oli)").withCode("oli,als")); + Reader reader = new MarcBibReaderFactory().createReader(); - reader.initialize(eventPayload, mappingContext); - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put("758258bc-ecc1-41b8-abca-f7b610822fff", "Oliss (oliss)"); - acceptedValues.put("f34d27c6-a8eb-461b-acd6-5dea81771e70", "SECOND FLOOR (KU/CC/DI/VU)"); - acceptedValues.put("184aae84-a5bf-4c6a-85ba-4a7c73026cd5", "Ils ali (Oli) (oli,ils)"); - acceptedValues.put("758258bc-ecc1-41b8-abca-f7b610822ffd", "Oli (Oli) (oli)"); - acceptedValues.put("fcd64ce1-6995-48f0-840e-89ffa2288371", "Oli als (Oli) (oli,als)"); + reader.initialize(eventPayload, mappingContext.withMappingParameters(new MappingParameters().withLocations(locations))); Value value = reader.read(new MappingRule() .withPath("holdings.permanentLocationId") .withName("permanentLocationId") - .withValue("049$a") - .withAcceptedValues(acceptedValues)); + .withValue("049$a")); assertNotNull(value); assertEquals(ValueType.STRING, value.getType()); @@ -1266,20 +1253,20 @@ public void shouldReadPermanentLocationFromTheLastBracketsEvenIfThereMoreThan2() context.put(MARC_BIBLIOGRAPHIC.value(), JsonObject.mapFrom(new Record() .withParsedRecord(new ParsedRecord().withContent(RECORD_WITH_049_WITH_OLI_LOCATION))).encode()); eventPayload.setContext(context); + List locations = List.of( + new Location().withId("758258bc-ecc1-41b8-abca-f7b610822fff").withName("Oliss").withCode("oliss"), + new Location().withId("f34d27c6-a8eb-461b-acd6-5dea81771e70").withName("SECOND FLOOR").withCode("KU/CC/DI/VU"), + new Location().withId("184aae84-a5bf-4c6a-85ba-4a7c73026cd5").withName("Ils ali (Oli)").withCode("oli,ils"), + new Location().withId("758258bc-ecc1-41b8-abca-f7b610822ffd").withName("Oli (Oli)").withCode("oli"), + new Location().withId("fcd64ce1-6995-48f0-840e-89ffa2288371").withName("Oli als (Oli)").withCode("oli,als")); + Reader reader = new MarcBibReaderFactory().createReader(); - reader.initialize(eventPayload, mappingContext); - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put("758258bc-ecc1-41b8-abca-f7b610822fff", "Oliss (oliss) (oli) (ollll) (olls)"); - acceptedValues.put("f34d27c6-a8eb-461b-acd6-5dea81771e70", "SECOND FLOOR (KU/CC/DI/VU)"); - acceptedValues.put("184aae84-a5bf-4c6a-85ba-4a7c73026cd5", "Ils ali (Oli) (oli))"); - acceptedValues.put("758258bc-ecc1-41b8-abca-f7b610822ffd", "Oli (Oli) (oli)"); - acceptedValues.put("fcd64ce1-6995-48f0-840e-89ffa2288371", "Oli als (Oli)(oli,als)"); + reader.initialize(eventPayload, mappingContext.withMappingParameters(new MappingParameters().withLocations(locations))); Value value = reader.read(new MappingRule() .withPath("holdings.permanentLocationId") .withName("permanentLocationId") - .withValue("049$a") - .withAcceptedValues(acceptedValues)); + .withValue("049$a")); assertNotNull(value); assertEquals(ValueType.STRING, value.getType()); @@ -1293,20 +1280,20 @@ public void shouldNotReadPermanentLocationEvenIfNameContainsBracketsButNotEquals context.put(MARC_BIBLIOGRAPHIC.value(), JsonObject.mapFrom(new Record() .withParsedRecord(new ParsedRecord().withContent(RECORD_WITH_049_WITH_OL_LOCATION))).encode()); eventPayload.setContext(context); + List locations = List.of( + new Location().withId("758258bc-ecc1-41b8-abca-f7b610822fff").withName("Oliss").withCode("oliss"), + new Location().withId("f34d27c6-a8eb-461b-acd6-5dea81771e70").withName("SECOND FLOOR").withCode("KU/CC/DI/VU"), + new Location().withId("184aae84-a5bf-4c6a-85ba-4a7c73026cd5").withName("Ils ali (Oli) (ol)").withCode("oli,ils"), + new Location().withId("758258bc-ecc1-41b8-abca-f7b610822ffd").withName("Oli (Oli)").withCode("oli"), + new Location().withId("fcd64ce1-6995-48f0-840e-89ffa2288371").withName("Oli als (Oli)").withCode("oli,als")); + Reader reader = new MarcBibReaderFactory().createReader(); - reader.initialize(eventPayload, mappingContext); - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put("758258bc-ecc1-41b8-abca-f7b610822fff", "Oliss (oliss)"); - acceptedValues.put("f34d27c6-a8eb-461b-acd6-5dea81771e70", "SECOND FLOOR (KU/CC/DI/VU)"); - acceptedValues.put("184aae84-a5bf-4c6a-85ba-4a7c73026cd5", "Ils ali (Oli) (ol) (oli,ils)"); - acceptedValues.put("758258bc-ecc1-41b8-abca-f7b610822ffd", "Oli (Oli) (oli)"); - acceptedValues.put("fcd64ce1-6995-48f0-840e-89ffa2288371", "Oli als (Oli) (oli,als)"); + reader.initialize(eventPayload, mappingContext.withMappingParameters(new MappingParameters().withLocations(locations))); Value value = reader.read(new MappingRule() .withPath("holdings.permanentLocationId") .withName("permanentLocationId") - .withValue("049$a") - .withAcceptedValues(acceptedValues)); + .withValue("049$a")); assertNotNull(value); assertEquals(ValueType.STRING, value.getType()); @@ -1320,19 +1307,19 @@ public void shouldNotReadPermanentLocationWhenRecordContainsInvalidBrackets() th context.put(MARC_BIBLIOGRAPHIC.value(), JsonObject.mapFrom(new Record() .withParsedRecord(new ParsedRecord().withContent(RECORD_WITH_049_AND_INVALID_BRACKETS))).encode()); eventPayload.setContext(context); + List locations = List.of( + new Location().withId("184aae84-a5bf-4c6a-85ba-4a7c73026cd5").withName("Online").withCode("KU/CC/DI/MI"), + new Location().withId("fcd64ce1-6995-48f0-840e-89ffa2288371").withName("Main Library").withCode("KU/CC/DI/M"), + new Location().withId("758258bc-ecc1-41b8-abca-f7b610822ffd").withName("ORWIG ETHNO CD").withCode("KU/CC/DI/MO"), + new Location().withId("f34d27c6-a8eb-461b-acd6-5dea81771e70").withName("SECOND FLOOR").withCode("KU/CC/DI/VU")); + Reader reader = new MarcBibReaderFactory().createReader(); - reader.initialize(eventPayload, mappingContext); - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put("758258bc-ecc1-41b8-abca-f7b610822ffd", "ORWIG ETHNO CD (KU/CC/DI/MO)"); - acceptedValues.put("184aae84-a5bf-4c6a-85ba-4a7c73026cd5", "Online (KU/CC/DI/MI)"); - acceptedValues.put("fcd64ce1-6995-48f0-840e-89ffa2288371", "Main Library KU/CC/DI/M"); - acceptedValues.put("f34d27c6-a8eb-461b-acd6-5dea81771e70", "SECOND FLOOR (KU/CC/DI/VU)"); + reader.initialize(eventPayload, mappingContext.withMappingParameters(new MappingParameters().withLocations(locations))); Value value = reader.read(new MappingRule() .withPath("holdings.permanentLocationId") .withName("permanentLocationId") - .withValue("049$a") - .withAcceptedValues(acceptedValues)); + .withValue("049$a")); assertNotNull(value); assertEquals(ValueType.STRING, value.getType()); @@ -1346,24 +1333,22 @@ public void shouldRead_OrderComplexField() throws IOException { HashMap context = new HashMap<>(); context.put(MARC_BIBLIOGRAPHIC.value(), JsonObject.mapFrom(new Record().withParsedRecord(new ParsedRecord().withContent(RECORD))).encode()); eventPayload.setContext(context); + String addressId = UUID.randomUUID().toString(); + String address = String.format("{\"id\":\"%s\", \"name\":\"Test1\",\"address\":\"Test2\"}", addressId); Reader reader = new MarcBibReaderFactory().createReader(); - reader.initialize(eventPayload, mappingContext); + reader.initialize(eventPayload, mappingContext.withMappingParameters(new MappingParameters().withTenantConfigurationAddresses(List.of(address)))); // when - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put("db9f5d17-0ca3-4d14-ae49-16b63c8fc084", "suf"); - acceptedValues.put("db9f5d17-0ca3-4d14-ae49-16b63c8fc083", "pref"); Value value = reader.read(new MappingRule() - .withName("prefix") - .withPath("order.po.poNumberPrefix") + .withName("billTo") + .withPath("order.po.billTo") .withEnabled("true") - .withValue("\"pref\"") - .withAcceptedValues(acceptedValues)); + .withValue("\"Test1\"")); // then assertNotNull(value); assertEquals(ValueType.STRING, value.getType()); - assertEquals("db9f5d17-0ca3-4d14-ae49-16b63c8fc083", value.getValue()); + assertEquals(addressId, value.getValue()); } @Test @@ -1373,19 +1358,18 @@ public void shouldRead_OrderArrayNonRepeatableField() throws IOException { HashMap context = new HashMap<>(); context.put(MARC_BIBLIOGRAPHIC.value(), JsonObject.mapFrom(new Record().withParsedRecord(new ParsedRecord().withContent(RECORD))).encode()); eventPayload.setContext(context); + List acqUnits = List.of( + new AcquisitionsUnit().withId("0ebb1f7d-983f-3026-8a4c-5318e0ebc042").withName("online"), + new AcquisitionsUnit().withId("0ebb1f7d-983f-3026-8a4c-5318e0ebc041").withName("main")); + Reader reader = new MarcBibReaderFactory().createReader(); - reader.initialize(eventPayload, mappingContext); + reader.initialize(eventPayload, mappingContext.withMappingParameters(new MappingParameters().withAcquisitionsUnits(acqUnits))); // when - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put("0ebb1f7d-983f-3026-8a4c-5318e0ebc042", "online"); - acceptedValues.put("0ebb1f7d-983f-3026-8a4c-5318e0ebc041", "main"); - Value value = reader.read(new MappingRule() .withName("acqUnitIds") .withPath("order.po.acqUnitIds[]") .withEnabled("true") - .withValue("\"main\"") - .withAcceptedValues(acceptedValues)); + .withValue("\"main\"")); // then assertNotNull(value); @@ -1416,7 +1400,7 @@ public void shouldRead_OrderLineComplexField() throws IOException { } @Test - public void shouldRead_MARCFieldAsMissingValueIfMappingRulesNeedsToBeValidByAcceptedValuesAndIsNotValid() throws IOException { + public void shouldRead_MARCFieldAsMissingValueIfMappingRulesNeedsToBeValidByMappingParametersAndIsNotValid() throws IOException { // given DataImportEventPayload eventPayload = new DataImportEventPayload(); HashMap context = new HashMap<>(); @@ -1424,30 +1408,26 @@ public void shouldRead_MARCFieldAsMissingValueIfMappingRulesNeedsToBeValidByAcce .withParsedRecord(new ParsedRecord().withContent(RECORD))).encode()); eventPayload.setContext(context); - Reader reader = new MarcBibReaderFactory().createReader(); - reader.initialize(eventPayload, mappingContext); String uuid = "UUID"; + List organizations = List.of(new Organization().withId(uuid).withName("(CODE)").withCode(uuid)); + Reader reader = new MarcBibReaderFactory().createReader(); + reader.initialize(eventPayload, mappingContext.withMappingParameters(new MappingParameters().withOrganizations(organizations))); - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put(uuid, String.format("CODE (%s)", uuid)); MappingRule vendorRule = new MappingRule() .withName("vendor") .withPath("order.po.vendor") .withEnabled("true") - .withValue("\"RANDOM\"") - .withAcceptedValues(acceptedValues); + .withValue("\"RANDOM\""); MappingRule materialSupplierRule = new MappingRule() .withName("materialSupplier") .withPath("order.poLine.physical.materialSupplier") .withEnabled("true") - .withValue("\"RANDOM\"") - .withAcceptedValues(acceptedValues); + .withValue("\"RANDOM\""); MappingRule accessProviderRule = new MappingRule() .withName("accessProvider") .withPath("order.poLine.eresource.accessProvider") .withEnabled("true") - .withValue("\"RANDOM\"") - .withAcceptedValues(acceptedValues); + .withValue("\"RANDOM\""); // when Value valueVendor = reader.read(vendorRule); @@ -1466,7 +1446,7 @@ public void shouldRead_MARCFieldAsMissingValueIfMappingRulesNeedsToBeValidByAcce } @Test - public void shouldRead_MARCFieldIfMappingRulesNeedsToBeValidByAcceptedValuesAndIsValid() throws IOException { + public void shouldRead_MARCFieldIfMappingRulesNeedsToBeValidByMappingParametersAndIsValid() throws IOException { // given DataImportEventPayload eventPayload = new DataImportEventPayload(); HashMap context = new HashMap<>(); @@ -1474,30 +1454,26 @@ public void shouldRead_MARCFieldIfMappingRulesNeedsToBeValidByAcceptedValuesAndI .withParsedRecord(new ParsedRecord().withContent(RECORD))).encode()); eventPayload.setContext(context); - Reader reader = new MarcBibReaderFactory().createReader(); - reader.initialize(eventPayload, mappingContext); String uuid = "UUID"; + List organizations = List.of(new Organization().withId(uuid).withName("(CODE)").withCode(uuid)); + Reader reader = new MarcBibReaderFactory().createReader(); + reader.initialize(eventPayload, mappingContext.withMappingParameters(new MappingParameters().withOrganizations(organizations))); - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put(uuid, String.format("CODE (%s)", uuid)); MappingRule vendorRule = new MappingRule() .withName("vendor") .withPath("order.po.vendor") .withEnabled("true") - .withValue("\"CODE\"") - .withAcceptedValues(acceptedValues); + .withValue("\"CODE\""); MappingRule materialSupplierRule = new MappingRule() .withName("materialSupplier") .withPath("order.poLine.physical.materialSupplier") .withEnabled("true") - .withValue("\"CODE\"") - .withAcceptedValues(acceptedValues); + .withValue("\"CODE\""); MappingRule accessProviderRule = new MappingRule() .withName("accessProvider") .withPath("order.poLine.eresource.accessProvider") .withEnabled("true") - .withValue("\"CODE\"") - .withAcceptedValues(acceptedValues); + .withValue("\"CODE\""); // when Value valueVendor = reader.read(vendorRule); @@ -1523,19 +1499,16 @@ public void shouldRead_IfVendorCodeContainsParenthesis() throws IOException { context.put(MARC_BIBLIOGRAPHIC.value(), JsonObject.mapFrom(new Record() .withParsedRecord(new ParsedRecord().withContent(RECORD))).encode()); eventPayload.setContext(context); - - Reader reader = new MarcBibReaderFactory().createReader(); - reader.initialize(eventPayload, mappingContext); String uuid = "UUID"; - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put(uuid, String.format("(CODE) (%s)", uuid)); + List organizations = List.of(new Organization().withId(uuid).withName("(CODE)").withCode(uuid)); + Reader reader = new MarcBibReaderFactory().createReader(); + reader.initialize(eventPayload, mappingContext.withMappingParameters(new MappingParameters().withOrganizations(organizations))); MappingRule vendorRule = new MappingRule() .withName("vendor") .withPath("order.po.vendor") .withEnabled("true") - .withValue("\"CODE)\"") - .withAcceptedValues(acceptedValues); + .withValue("\"CODE)\""); // when Value valueVendor = reader.read(vendorRule); @@ -2004,14 +1977,14 @@ public void shouldReturnListValueWithMultipleDonorsIdsIfMarcFieldMappingSpecifie .withParsedRecord(new ParsedRecord().withContent(RECORD_WITH_900_FIELD_DONORS_CODES)))); DataImportEventPayload eventPayload = new DataImportEventPayload().withContext(context); - Reader reader = new MarcBibReaderFactory().createReader(); - reader.initialize(eventPayload, mappingContext); - String expectedId1 = "UUID1"; String expectedId2 = "UUID2"; - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put(expectedId1, "(CODE-1) " + expectedId1); - acceptedValues.put(expectedId2, "(CODE-2) " + expectedId2); + List organizations = List.of( + new Organization().withId(expectedId1).withCode("CODE-1").withName(expectedId1).withIsDonor(true), + new Organization().withId(expectedId2).withCode("CODE-2").withName(expectedId2).withIsDonor(true)); + + Reader reader = new MarcBibReaderFactory().createReader(); + reader.initialize(eventPayload, mappingContext.withMappingParameters(new MappingParameters().withOrganizations(organizations))); MappingRule donorsMappingRule = new MappingRule() .withName("donorOrganizationIds") @@ -2026,8 +1999,7 @@ public void shouldReturnListValueWithMultipleDonorsIdsIfMarcFieldMappingSpecifie .withName("donorOrganizationIds") .withEnabled("true") .withPath("order.poLine.donorOrganizationIds[]") - .withValue("900$a") - .withAcceptedValues(acceptedValues))), + .withValue("900$a"))), new RepeatableSubfieldMapping() .withOrder(1) .withPath("order.poLine.donorOrganizationIds[]") @@ -2035,8 +2007,7 @@ public void shouldReturnListValueWithMultipleDonorsIdsIfMarcFieldMappingSpecifie .withName("donorOrganizationIds") .withEnabled("true") .withPath("order.poLine.donorOrganizationIds[]") - .withValue("900$b") - .withAcceptedValues(acceptedValues))) + .withValue("900$b"))) )); Value value = reader.read(donorsMappingRule); @@ -2056,11 +2027,10 @@ public void shouldReturnEmptyRepeatableFieldValueIfMappingValueDoesNotMatchAccep context.put(MARC_BIBLIOGRAPHIC.value(), Json.encode(new Record() .withParsedRecord(new ParsedRecord().withContent(RECORD_WITH_900_FIELD_DONORS_CODES)))); eventPayload.setContext(context); - Reader reader = new MarcBibReaderFactory().createReader(); - reader.initialize(eventPayload, mappingContext); + List organizations = List.of(new Organization().withId("UUID3").withCode("CODE-3").withName("UUID3").withIsDonor(true)); - HashMap acceptedValues = new HashMap<>(); - acceptedValues.put("UUID3", "(CODE-3) UUID3"); + Reader reader = new MarcBibReaderFactory().createReader(); + reader.initialize(eventPayload, mappingContext.withMappingParameters(new MappingParameters().withOrganizations(organizations))); MappingRule donorsMappingRule = new MappingRule() .withName("donorOrganizationIds") @@ -2074,8 +2044,7 @@ public void shouldReturnEmptyRepeatableFieldValueIfMappingValueDoesNotMatchAccep .withName("donorOrganizationIds") .withEnabled("true") .withPath("order.poLine.donorOrganizationIds[]") - .withValue("900$a") - .withAcceptedValues(acceptedValues))))); + .withValue("900$a"))))); Value value = reader.read(donorsMappingRule);