Skip to content

Commit

Permalink
Added and updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RuslanLavrov committed Oct 7, 2024
1 parent 46f6e6c commit ec0a97c
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ public String apply(RuleExecutionContext context) {
}

return subjectSources.stream()
.filter(subjectSource -> subjectSource.getCode() != null && subjectSource.getCode().equalsIgnoreCase(sourceCode))
.filter(subjectSource -> sourceCode.equalsIgnoreCase(subjectSource.getCode()))
.findFirst()
.map(SubjectSource::getId)
.orElse(StringUtils.EMPTY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
Expand All @@ -31,7 +33,6 @@
import org.folio.InstanceType;
import org.folio.Subject;
import org.folio.SubjectSource;
import org.folio.SubjectSources;
import org.folio.SubjectType;
import org.folio.processing.TestUtil;
import org.folio.processing.mapping.defaultmapper.RecordMapper;
Expand Down Expand Up @@ -69,6 +70,7 @@ public class InstanceMappingTest {
private static final String BIB_WITH_720_FIELDS = "src/test/resources/org/folio/processing/mapping/instance/720_fields_samples.mrc";
private static final String BIB_WITH_FIELDS_FOR_ALTERNATIVE_MAPPING = "src/test/resources/org/folio/processing/mapping/instance/fields_for_alternative_mapping_samples.mrc";
private static final String BIB_WITH_FIELDS_FOR_ALTERNATIVE_MAPPING_WITH_PUNCTUATIONS = "src/test/resources/org/folio/processing/mapping/instance/fields_for_alternative_mapping_samples_with_punctuations.mrc";
public static final String BIB_WITH_SUBJECT_SOURCES_CODE_IN_2_SUBFIELD = "src/test/resources/org/folio/processing/mapping/instance/subject_source_codes_in_2_subfield.mrc";
private static final String CLASSIFICATIONS_TEST = "src/test/resources/org/folio/processing/mapping/instance/classificationsTest.mrc";
private static final String INSTANCES_CLASSIFICATIONS_PATH = "src/test/resources/org/folio/processing/mapping/instance/classificationsTestInstance.json";
private static final String DEFAULT_MAPPING_RULES_PATH = "src/test/resources/org/folio/processing/mapping/instance/rules.json";
Expand Down Expand Up @@ -481,7 +483,6 @@ public void testMarcToInstanceWithRepeatableSubjects() throws IOException {
final String FIFTH_LIBRARY_SOURCE_ID = "e894d0dc-621d-4b1d-98f6-6f7120eb0d44";
final String SIXTH_LIBRARY_SOURCE_ID = "e894d0dc-621d-4b1d-98f6-6f7120eb0d45";
final String SEVENTH_LIBRARY_SOURCE_ID = "e894d0dc-621d-4b1d-98f6-6f7120eb0d46";
final String EIGHTH_LIBRARY_SOURCE_ID = "e894d0dc-621d-4b1d-98f6-6f7120eb0d47";

final String FIRST_SUBJECT_TYPE_ID = "d6488f88-1e74-40ce-81b5-b19a928ff5b1";
final String SECOND_SUBJECT_TYPE_ID = "d6488f88-1e74-40ce-81b5-b19a928ff5b2";
Expand All @@ -503,8 +504,8 @@ public void testMarcToInstanceWithRepeatableSubjects() throws IOException {
new Subject().withValue("Test 648 subject").withSourceId(SIXTH_LIBRARY_SOURCE_ID).withTypeId(SIXTH_SUBJECT_TYPE_ID),
new Subject().withValue("Test 650 subject").withSourceId(SEVENTH_LIBRARY_SOURCE_ID).withTypeId(SEVENTH_SUBJECT_TYPE_ID),
new Subject().withValue("Test 651 subject").withSourceId(SECOND_LIBRARY_SOURCE_ID).withTypeId(EIGHTH_SUBJECT_TYPE_ID),
new Subject().withValue("Test 655 subject").withSourceId(EIGHTH_LIBRARY_SOURCE_ID).withTypeId(NINTH_SUBJECT_TYPE_ID)
);
new Subject().withValue("Test 655 subject").withSourceId(SECOND_LIBRARY_SOURCE_ID).withTypeId(NINTH_SUBJECT_TYPE_ID)
);

MarcReader reader = new MarcStreamReader(new ByteArrayInputStream(TestUtil.readFileFromPath(BIB_WITH_REPEATED_600_SUBFIELDS).getBytes(StandardCharsets.UTF_8)));
JsonObject mappingRules = new JsonObject(TestUtil.readFileFromPath(DEFAULT_MAPPING_RULES_PATH));
Expand Down Expand Up @@ -594,6 +595,49 @@ public void testMarcToInstanceWithRepeatableSubjectsButWithoutIndicators() throw
});
}

@Test
public void testMarcToSubjectSourceIdMappingByCodeFrom2Subfield() throws IOException {
MarcReader reader = new MarcStreamReader(new ByteArrayInputStream(
TestUtil.readFileFromPath(BIB_WITH_SUBJECT_SOURCES_CODE_IN_2_SUBFIELD).getBytes(StandardCharsets.UTF_8)));

JsonObject mappingRules = new JsonObject(TestUtil.readFileFromPath(DEFAULT_MAPPING_RULES_PATH));
List<SubjectSource> subjectSources = new ObjectMapper()
.readValue(new File(DEFAULT_SUBJECT_SOURCES_PATH), new TypeReference<>() {});

String firstSourceId = "e894d0dc-621d-4b1d-98f6-6f7120eb0d40";
String secondSourceId = "e894d0dc-621d-4b1d-98f6-6f7120eb0d41";
String thirdSourceId = "e894d0dc-621d-4b1d-98f6-6f7120eb0d42";
String fourthSourceId = "e894d0dc-621d-4b1d-98f6-6f7120eb0d45";
String fifthSourceId = "e894d0dc-621d-4b1d-98f6-6f7120eb0d46";

Map<String, String> subjectValueToSourceId = Map.of(
"Subject heading 600", firstSourceId,
"Subject heading 610", secondSourceId,
"Subject heading 611", thirdSourceId,
"Subject heading 630", fourthSourceId,
"Subject heading 647", fifthSourceId,
"Subject heading 648", firstSourceId,
"Subject heading 650", secondSourceId,
"Subject heading 651", thirdSourceId,
"Subject heading 655", fourthSourceId
);

assertTrue(reader.hasNext());
ByteArrayOutputStream os = new ByteArrayOutputStream();
MarcJsonWriter writer = new MarcJsonWriter(os);
Record record = reader.next();
writer.write(record);
JsonObject marc = new JsonObject(os.toString());
Instance instance = mapper.mapRecord(marc, new MappingParameters().withSubjectSources(subjectSources), mappingRules);

assertNotNull(instance.getSubjects());
assertEquals(9, instance.getSubjects().size());
instance.getSubjects().forEach(subject -> {
assertNotNull(subject.getValue());
assertEquals(subjectValueToSourceId.get(subject.getValue()), subject.getSourceId());
});
}

@Test
public void testMarc720ToInstanceContributors() throws IOException {
MarcReader reader = new MarcStreamReader(new ByteArrayInputStream(TestUtil.readFileFromPath(BIB_WITH_720_FIELDS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,32 @@ public void SET_SUBJECT_SOURCE_ID_shouldReturnEmptyIfNoMatchingMappingSpecified(
assertEquals(EMPTY_STRING, actualSubjectSource);
}

@Test
public void SET_SUBJECT_SOURCE_ID_BY_CODE_shouldReturnEmptyStringIfMappingParametersHasNoSubjectSources() {
// given
var context = new RuleExecutionContext();
context.setMappingParameters(new MappingParameters());
// when
var actualSubjectSourceId = runFunction("set_subject_source_id_by_code", context);
//then
assertEquals(EMPTY_STRING, actualSubjectSourceId);
}

@Test
public void SET_SUBJECT_SOURCE_ID_BY_CODE_shouldReturnEmptyStringIfSubfieldValueDoesNotMatchNoSubjectSourceCode() {
// given
var subjectSource = new SubjectSource()
.withId(UUID.randomUUID().toString())
.withCode("lcsh");
var context = new RuleExecutionContext();
context.setSubFieldValue("nonExistingCode");
context.setMappingParameters(new MappingParameters().withSubjectSources(List.of(subjectSource)));
// when
var actualSubjectSourceId = runFunction("set_subject_source_id_by_code", context);
//then
assertEquals(EMPTY_STRING, actualSubjectSourceId);
}

@Test
public void SET_SUBJECT_TYPE_ID_shouldReturnExpectedResult() {
// given
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
01697cam 2200433Ma 4500001001200000003000600012005001700018006001900035007001500054008004100069040009800110050002300208072002500231082001800256100002400274245006500298260003100363300005800394336002600452337002600478338003600504490009600540500004000636504006900676588002600745600004600771600002300817610002100840611002100861630002100882647002100903648002100924650002100945651002100966655002100987710002301008776013401031830009801165ocm85820197OCoLC20160514041104.1m o d cr |||||||||||901214s1991 nyua ob 001 0 eng d aDG1bengepncDG1dDG1dOCLCQdE7BdOCLCFdOCLCOdOCLCQdYDXCPdN$TdIDEBKdOCLCQdCOOdOCLCQ 4aQA278.5b.J27 1991 7aMATx0290202bisacsh04a519.5/3542201 aJackson, J. Edward.12aA user's guide to principal components /cJ. Edward Jackson. aNew York :bWiley,c?1991. a1 online resource (xvii, 569 pages) :billustrations. atextbtxt2rdacontent acomputerbc2rdamedia aonline resourcebcr2rdacarrier1 aWiley series in probability and mathematical statistics. Applied probability and statistics a"A Wiley-Interscience publication." aIncludes bibliographical references (pages 497-550) and indexes.0 aPrint version record. 0aTesting 600 subjectbTesting 600b subject 4fTest 600.2 subject 2fTest 610 subject 3fTest 611 subject 4aTest 630 subject 5aTest 647 subject 5aTest 648 subject 6aTest 650 subject 1aTest 651 subject 7aTest 655 subject2 aJohn Wiley & Sons.08iPrint version:aJackson, J. Edward.tUser's guide to principal components.dNew York : Wiley, ?1991z0471622672w(DLC) 90028108 0aWiley series in probability and mathematical statistics.pApplied probability and statistics.
01697cam 2200433Ma 4500001001200000003000600012005001700018006001900035007001500054008004100069040009800110050002300208072002500231082001800256100002400274245006500298260003100363300005800394336002600452337002600478338003600504490009600540500004000636504006900676588002600745600004600771600002300817610002100840611002100861630002100882647002100903648002100924650002100945651002100966655002100987710002301008776013401031830009801165ocm85820197OCoLC20160514041104.1m o d cr |||||||||||901214s1991 nyua ob 001 0 eng d aDG1bengepncDG1dDG1dOCLCQdE7BdOCLCFdOCLCOdOCLCQdYDXCPdN$TdIDEBKdOCLCQdCOOdOCLCQ 4aQA278.5b.J27 1991 7aMATx0290202bisacsh04a519.5/3542201 aJackson, J. Edward.12aA user's guide to principal components /cJ. Edward Jackson. aNew York :bWiley,c?1991. a1 online resource (xvii, 569 pages) :billustrations. atextbtxt2rdacontent acomputerbc2rdamedia aonline resourcebcr2rdacarrier1 aWiley series in probability and mathematical statistics. Applied probability and statistics a"A Wiley-Interscience publication." aIncludes bibliographical references (pages 497-550) and indexes.0 aPrint version record. 0aTesting 600 subjectbTesting 600b subject 4fTest 600.2 subject 2fTest 610 subject 3fTest 611 subject 4aTest 630 subject 5aTest 647 subject 5aTest 648 subject 6aTest 650 subject 1aTest 651 subject 1aTest 655 subject2 aJohn Wiley & Sons.08iPrint version:aJackson, J. Edward.tUser's guide to principal components.dNew York : Wiley, ?1991z0471622672w(DLC) 90028108 0aWiley series in probability and mathematical statistics.pApplied probability and statistics.
Loading

0 comments on commit ec0a97c

Please sign in to comment.