Skip to content

Commit

Permalink
MODSOURCE-752: test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
PBobylev committed Jul 26, 2024
1 parent 429e989 commit 6fbbd2c
Show file tree
Hide file tree
Showing 17 changed files with 308 additions and 139 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.folio.consumers;

import static org.folio.okapi.common.XOkapiHeaders.TENANT;
import static org.folio.rest.jaxrs.model.Record.RecordType.MARC_AUTHORITY;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.vertx.core.json.Json;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
Expand All @@ -13,6 +13,7 @@
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.common.header.internals.RecordHeader;
Expand All @@ -31,20 +32,24 @@
import org.folio.services.AbstractLBServiceTest;
import org.folio.services.RecordService;
import org.folio.services.RecordServiceImpl;
import org.folio.services.domainevent.RecordDomainEventPublisher;
import org.jetbrains.annotations.NotNull;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

@RunWith(VertxUnitRunner.class)
public class AuthorityDomainKafkaHandlerTest extends AbstractLBServiceTest {

private static final String recordId = UUID.randomUUID().toString();
private static RawRecord rawRecord;
private static ParsedRecord parsedRecord;

@Mock
private RecordDomainEventPublisher recordDomainEventPublisher;
private RecordDao recordDao;
private RecordService recordService;
private Record record;
Expand All @@ -61,7 +66,8 @@ public static void setUpClass() throws IOException {

@Before
public void setUp(TestContext context) {
recordDao = new RecordDaoImpl(postgresClientFactory);
MockitoAnnotations.openMocks(this);
recordDao = new RecordDaoImpl(postgresClientFactory, recordDomainEventPublisher);
recordService = new RecordServiceImpl(recordDao);
handler = new AuthorityDomainKafkaHandler(recordService);
Async async = context.async();
Expand All @@ -78,8 +84,9 @@ record = new Record()
.withRecordType(MARC_AUTHORITY)
.withRawRecord(rawRecord)
.withParsedRecord(parsedRecord);
var okapiHeaders = Map.of(TENANT, TENANT_ID);
SnapshotDaoUtil.save(postgresClientFactory.getQueryExecutor(TENANT_ID), snapshot)
.compose(savedSnapshot -> recordService.saveRecord(record, TENANT_ID))
.compose(savedSnapshot -> recordService.saveRecord(record, okapiHeaders))
.onSuccess(ar -> async.complete())
.onFailure(context::fail);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import java.util.Map;
import org.folio.TestMocks;
import org.folio.TestUtil;
import org.folio.dao.util.AdvisoryLockUtil;
Expand All @@ -19,12 +20,15 @@
import org.folio.rest.jaxrs.model.Record;
import org.folio.rest.jaxrs.model.Snapshot;
import org.folio.services.AbstractLBServiceTest;
import org.folio.services.domainevent.RecordDomainEventPublisher;
import org.folio.services.util.TypeConnection;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.test.util.ReflectionTestUtils;

import java.io.IOException;
Expand All @@ -33,6 +37,7 @@
import java.util.UUID;

import static org.folio.dao.RecordDaoImpl.INDEXERS_DELETION_LOCK_NAMESPACE_ID;
import static org.folio.okapi.common.XOkapiHeaders.TENANT;
import static org.folio.rest.jaxrs.model.Record.State.ACTUAL;
import static org.folio.rest.jaxrs.model.Record.State.DELETED;
import static org.folio.rest.jooq.Tables.MARC_RECORDS_TRACKING;
Expand All @@ -41,16 +46,18 @@
public class RecordDaoImplTest extends AbstractLBServiceTest {

private static final String ENABLE_FALLBACK_QUERY_FIELD = "enableFallbackQuery";

@Mock
private RecordDomainEventPublisher recordDomainEventPublisher;
private RecordDao recordDao;
private Record record;
private Record deletedRecord;
private String deletedRecordId;

@Before
public void setUp(TestContext context) throws IOException {
MockitoAnnotations.openMocks(this);
Async async = context.async();
recordDao = new RecordDaoImpl(postgresClientFactory);
recordDao = new RecordDaoImpl(postgresClientFactory, recordDomainEventPublisher);
RawRecord rawRecord = new RawRecord()
.withContent(new ObjectMapper().readValue(TestUtil.readFileFromPath(RAW_MARC_RECORD_CONTENT_SAMPLE_PATH), String.class));
ParsedRecord marcRecord = new ParsedRecord()
Expand Down Expand Up @@ -85,9 +92,10 @@ public void setUp(TestContext context) throws IOException {
.withExternalIdsHolder(new ExternalIdsHolder()
.withInstanceId(UUID.randomUUID().toString()));

var okapiHeaders = Map.of(TENANT, TENANT_ID);
SnapshotDaoUtil.save(postgresClientFactory.getQueryExecutor(TENANT_ID), snapshot)
.compose(savedSnapshot -> recordDao.saveRecord(record, TENANT_ID))
.compose(savedSnapshot -> recordDao.saveRecord(deletedRecord, TENANT_ID))
.compose(savedSnapshot -> recordDao.saveRecord(record, okapiHeaders))
.compose(savedSnapshot -> recordDao.saveRecord(deletedRecord, okapiHeaders))
.onComplete(save -> {
if (save.failed()) {
context.fail(save.cause());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import static org.folio.EntityLinksKafkaTopic.INSTANCE_AUTHORITY;
import static org.folio.EntityLinksKafkaTopic.LINKS_STATS;
import static org.folio.RecordStorageKafkaTopic.MARC_BIB;
import static org.folio.okapi.common.XOkapiHeaders.TENANT;
import static org.folio.rest.jaxrs.model.LinkUpdateReport.Status.FAIL;

import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.json.Json;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
Expand All @@ -34,7 +34,6 @@
import org.folio.TestUtil;
import org.folio.dao.RecordDao;
import org.folio.dao.RecordDaoImpl;
import org.folio.dao.util.ParsedRecordDaoUtil;
import org.folio.dao.util.SnapshotDaoUtil;
import org.folio.kafka.services.KafkaTopic;
import org.folio.okapi.common.XOkapiHeaders;
Expand All @@ -52,11 +51,13 @@
import org.folio.rest.jaxrs.model.Subfield;
import org.folio.rest.jaxrs.model.SubfieldsChange;
import org.folio.rest.jaxrs.model.UpdateTarget;
import org.folio.services.domainevent.RecordDomainEventPublisher;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper;

@RunWith(VertxUnitRunner.class)
Expand Down Expand Up @@ -90,7 +91,8 @@ public class AuthorityLinkChunkKafkaHandlerTest extends AbstractLBServiceTest {
);
private final RawRecord rawRecord = new RawRecord().withId(RECORD_ID)
.withContent("test content");

@Mock
private RecordDomainEventPublisher recordDomainEventPublisher;
private RecordDao recordDao;
private RecordService recordService;
private Record record;
Expand All @@ -99,7 +101,8 @@ public class AuthorityLinkChunkKafkaHandlerTest extends AbstractLBServiceTest {

@Before
public void setUp(TestContext context) throws IOException {
recordDao = new RecordDaoImpl(postgresClientFactory);
MockitoAnnotations.openMocks(this);
recordDao = new RecordDaoImpl(postgresClientFactory, recordDomainEventPublisher);
recordService = new RecordServiceImpl(recordDao);

var async = context.async();
Expand Down Expand Up @@ -142,10 +145,11 @@ record = new Record()
.withSnapshotId(snapshot.getJobExecutionId())
.withExternalIdsHolder(new ExternalIdsHolder().withInstanceId(ERROR_INSTANCE_ID));

var okapiHeaders = Map.of(TENANT, TENANT_ID);
SnapshotDaoUtil.save(postgresClientFactory.getQueryExecutor(TENANT_ID), snapshot)
.compose(savedSnapshot -> recordService.saveRecord(record, TENANT_ID))
.compose(savedRecord -> recordService.saveRecord(secondRecord, TENANT_ID))
.compose(savedRecord -> recordService.saveRecord(errorRecord, TENANT_ID))
.compose(savedSnapshot -> recordService.saveRecord(record, okapiHeaders))
.compose(savedRecord -> recordService.saveRecord(secondRecord, okapiHeaders))
.compose(savedRecord -> recordService.saveRecord(errorRecord, okapiHeaders))
.onSuccess(ar -> async.complete())
.onFailure(context::fail);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import java.util.Map;
import org.folio.ActionProfile;
import org.folio.DataImportEventPayload;
import org.folio.dao.RecordDaoImpl;
Expand All @@ -16,6 +17,7 @@
import org.folio.rest.jaxrs.model.RawRecord;
import org.folio.rest.jaxrs.model.Record;
import org.folio.rest.jaxrs.model.Snapshot;
import org.folio.services.domainevent.RecordDomainEventPublisher;
import org.folio.services.handlers.actions.MarcAuthorityDeleteEventHandler;
import org.junit.Assert;
import org.junit.Before;
Expand All @@ -27,9 +29,12 @@
import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static org.folio.ActionProfile.Action.DELETE;
import static org.folio.ActionProfile.Action.UPDATE;
import static org.folio.okapi.common.XOkapiHeaders.TENANT;
import static org.folio.rest.jaxrs.model.DataImportEventTypes.DI_SRS_MARC_AUTHORITY_RECORD_DELETED;
import static org.folio.rest.jaxrs.model.ProfileType.ACTION_PROFILE;
import static org.folio.rest.jaxrs.model.Record.RecordType.MARC_AUTHORITY;
Expand All @@ -39,12 +44,17 @@ public class MarcAuthorityDeleteEventHandlerTest extends AbstractLBServiceTest {

private static final String PARSED_CONTENT =
"{\"leader\":\"01314nam 22003851a 4500\",\"fields\":[{\"001\":\"ybp7406411\"},{\"856\":{\"subfields\":[{\"u\":\"example.com\"}],\"ind1\":\" \",\"ind2\":\" \"}}]}";
private final RecordService recordService = new RecordServiceImpl(new RecordDaoImpl(postgresClientFactory));
private final EventHandler eventHandler = new MarcAuthorityDeleteEventHandler(recordService);
@Mock
private RecordDomainEventPublisher recordDomainEventPublisher;
private RecordService recordService;
private EventHandler eventHandler;
private Record record;

@Before
public void before(TestContext testContext) throws IOException {
MockitoAnnotations.openMocks(this);
recordService = new RecordServiceImpl(new RecordDaoImpl(postgresClientFactory, recordDomainEventPublisher));
eventHandler = new MarcAuthorityDeleteEventHandler(recordService);
Snapshot snapshot = new Snapshot()
.withJobExecutionId(UUID.randomUUID().toString())
.withProcessingStartedDate(new Date())
Expand Down Expand Up @@ -86,7 +96,8 @@ public void shouldDeleteRecord(TestContext context) {
.withFolioRecord(ActionProfile.FolioRecord.MARC_AUTHORITY)
)
);
recordService.saveRecord(record, TENANT_ID)
var okapiHeaders = Map.of(TENANT, TENANT_ID);
recordService.saveRecord(record, okapiHeaders)
// when
.onSuccess(ar -> eventHandler.handle(dataImportEventPayload)
// then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import java.util.Map;
import org.folio.DataImportEventPayload;
import org.folio.MappingProfile;
import org.folio.MatchDetail;
Expand All @@ -25,13 +26,15 @@
import org.folio.rest.jaxrs.model.RawRecord;
import org.folio.rest.jaxrs.model.Record;
import org.folio.rest.jaxrs.model.Snapshot;
import org.folio.services.domainevent.RecordDomainEventPublisher;
import org.folio.services.handlers.match.MarcAuthorityMatchEventHandler;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.io.IOException;
Expand All @@ -43,6 +46,7 @@

import static java.util.Collections.singletonList;
import static org.folio.MatchDetail.MatchCriterion.EXACTLY_MATCHES;
import static org.folio.okapi.common.XOkapiHeaders.TENANT;
import static org.folio.rest.jaxrs.model.DataImportEventTypes.DI_SRS_MARC_AUTHORITY_RECORD_CREATED;
import static org.folio.rest.jaxrs.model.DataImportEventTypes.DI_SRS_MARC_AUTHORITY_RECORD_MATCHED;
import static org.folio.rest.jaxrs.model.DataImportEventTypes.DI_SRS_MARC_AUTHORITY_RECORD_NOT_MATCHED;
Expand All @@ -58,6 +62,8 @@ public class MarcAuthorityMatchEventHandlerTest extends AbstractLBServiceTest {
private static final String MATCHED_MARC_KEY = "MATCHED_MARC_AUTHORITY";
private static final String existingRecordId = "b90cb1bc-601f-45d7-b99e-b11efd281dcd";
private static String rawRecordContent;
@Mock
private RecordDomainEventPublisher recordDomainEventPublisher;
private RecordDao recordDao;
private Record existingRecord;
private Record incomingRecord;
Expand All @@ -72,7 +78,7 @@ public static void setUpClass() throws IOException {
public void setUp(TestContext context) {
MockitoAnnotations.initMocks(this);

recordDao = new RecordDaoImpl(postgresClientFactory);
recordDao = new RecordDaoImpl(postgresClientFactory, recordDomainEventPublisher);
handler = new MarcAuthorityMatchEventHandler(recordDao, null, vertx);
Async async = context.async();

Expand Down Expand Up @@ -169,7 +175,8 @@ public void shouldMatchBy999ffsField(TestContext context) {
)))
))));

recordDao.saveRecord(existingRecord, TENANT_ID)
var okapiHeaders = Map.of(TENANT, TENANT_ID);
recordDao.saveRecord(existingRecord, okapiHeaders)
.onComplete(context.asyncAssertSuccess())
.onSuccess(existingSavedRecord -> handler.handle(dataImportEventPayload)
.whenComplete((updatedEventPayload, throwable) -> {
Expand Down Expand Up @@ -218,7 +225,8 @@ public void shouldMatchBy001Field(TestContext context) {
new Field().withLabel("recordSubfield").withValue("")
)))))));

recordDao.saveRecord(existingRecord, TENANT_ID)
var okapiHeaders = Map.of(TENANT, TENANT_ID);
recordDao.saveRecord(existingRecord, okapiHeaders)
.onComplete(context.asyncAssertSuccess())
.onSuccess(existingSavedRecord -> handler.handle(dataImportEventPayload)
.whenComplete((updatedEventPayload, throwable) -> {
Expand Down Expand Up @@ -268,7 +276,8 @@ public void shouldMatchBy010aField(TestContext context) {
)))
))));

recordDao.saveRecord(existingRecord, TENANT_ID)
var okapiHeaders = Map.of(TENANT, TENANT_ID);
recordDao.saveRecord(existingRecord, okapiHeaders)
.onComplete(context.asyncAssertSuccess())
.onSuccess(existingSavedRecord -> handler.handle(dataImportEventPayload)
.whenComplete((updatedEventPayload, throwable) -> {
Expand Down Expand Up @@ -315,7 +324,8 @@ public void shouldNotMatchBy999ffsField(TestContext context) {
new Field().withLabel("indicator2").withValue(""),
new Field().withLabel("recordSubfield").withValue("a"))))))));

recordDao.saveRecord(existingRecord, TENANT_ID)
var okapiHeaders = Map.of(TENANT, TENANT_ID);
recordDao.saveRecord(existingRecord, okapiHeaders)
.onComplete(context.asyncAssertSuccess())
.onSuccess(existingSavedRecord -> handler.handle(dataImportEventPayload)
.whenComplete((updatedEventPayload, throwable) -> {
Expand Down Expand Up @@ -360,7 +370,8 @@ public void shouldNotMatchBy001Field(TestContext context) {
new Field().withLabel("indicator2").withValue(""),
new Field().withLabel("recordSubfield").withValue("a"))))))));

recordDao.saveRecord(existingRecord, TENANT_ID)
var okapiHeaders = Map.of(TENANT, TENANT_ID);
recordDao.saveRecord(existingRecord, okapiHeaders)
.onComplete(context.asyncAssertSuccess())
.onSuccess(record -> handler.handle(dataImportEventPayload)
.whenComplete((updatedEventPayload, throwable) -> {
Expand Down
Loading

0 comments on commit 6fbbd2c

Please sign in to comment.