Skip to content

Commit

Permalink
split reindex IT
Browse files Browse the repository at this point in the history
  • Loading branch information
PBobylev committed Nov 28, 2024
1 parent 15c030a commit fce324f
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 173 deletions.
173 changes: 0 additions & 173 deletions src/test/java/org/folio/linked/data/e2e/ReIndexControllerIT.java
Original file line number Diff line number Diff line change
@@ -1,173 +0,0 @@
package org.folio.linked.data.e2e;

import static java.util.Objects.nonNull;
import static org.assertj.core.api.Assertions.assertThat;
import static org.folio.linked.data.domain.dto.ResourceIndexEventType.CREATE;
import static org.folio.linked.data.test.MonographTestUtil.getSampleInstanceResource;
import static org.folio.linked.data.test.MonographTestUtil.getSampleWork;
import static org.folio.linked.data.test.TestUtil.TENANT_ID;
import static org.folio.linked.data.test.TestUtil.awaitAndAssert;
import static org.folio.linked.data.test.TestUtil.cleanResourceTables;
import static org.folio.linked.data.test.TestUtil.defaultHeaders;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.util.Date;
import lombok.SneakyThrows;
import org.folio.linked.data.e2e.base.IntegrationTest;
import org.folio.linked.data.model.entity.Resource;
import org.folio.linked.data.repo.ResourceEdgeRepository;
import org.folio.linked.data.service.tenant.TenantScopedExecutionService;
import org.folio.linked.data.test.ResourceTestRepository;
import org.folio.linked.data.test.kafka.KafkaSearchWorkIndexTopicListener;
import org.folio.spring.tools.kafka.KafkaAdminService;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.web.servlet.MockMvc;

@IntegrationTest
class ReIndexControllerIT {

public static final String INDEX_URL = "/linked-data/reindex";

@Autowired
private MockMvc mockMvc;
@Autowired
private ResourceTestRepository resourceRepo;
@Autowired
private ResourceEdgeRepository resourceEdgeRepository;
@Autowired
private Environment env;
@Autowired
private KafkaSearchWorkIndexTopicListener consumer;
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private TenantScopedExecutionService tenantScopedExecutionService;
@Autowired
private KafkaAdminService kafkaAdminService;

@BeforeAll
static void beforeAll(@Autowired KafkaAdminService kafkaAdminService) {
kafkaAdminService.createTopics(TENANT_ID);
}

@BeforeEach
public void beforeEach() {
kafkaAdminService.restartEventListeners();
}

@BeforeEach
public void afterEach() {
tenantScopedExecutionService.execute(TENANT_ID, () -> {
cleanResourceTables(jdbcTemplate);
});
consumer.getMessages().clear();
}

@Test
void indexResourceWithNoIndexDate_andNotFullIndexRequest() throws Exception {
// given
var work = resourceRepo.save(getSampleWork(null)).setId(1L);
var instance = resourceRepo.save(getSampleInstanceResource(null, work));
var anotherWork = getSampleWork(instance);
anotherWork.setId(2L);
resourceRepo.save(anotherWork);
var resourceEdge = work.getIncomingEdges().iterator().next();
resourceEdge.computeId();
resourceEdgeRepository.save(resourceEdge);

var requestBuilder = put(INDEX_URL)
.contentType(APPLICATION_JSON)
.headers(defaultHeaders(env));

// when
var resultActions = mockMvc.perform(requestBuilder);

// then
resultActions.andExpect(status().isNoContent());
checkKafkaMessageSent(work);
}

@Test
void notIndexResourceWithIndexDate_andNotFullIndexRequest() throws Exception {
// given
var work = resourceRepo.save(getSampleWork(null).setIndexDate(new Date()));
resourceRepo.save(getSampleInstanceResource(null, work));

var requestBuilder = put(INDEX_URL)
.contentType(APPLICATION_JSON)
.headers(defaultHeaders(env));

// when
var resultActions = mockMvc.perform(requestBuilder);

// then
resultActions.andExpect(status().isNoContent());
checkKafkaMessageSent(null);
}

@Test
void indexResourceWithNoIndexDate_andFullIndexRequest() throws Exception {
// given
var work = resourceRepo.save(getSampleWork(null));
resourceRepo.save(getSampleInstanceResource(null, work));
var resourceEdge = work.getIncomingEdges().iterator().next();
resourceEdge.computeId();
resourceEdgeRepository.save(resourceEdge);

var requestBuilder = put(INDEX_URL)
.param("full", "true")
.contentType(APPLICATION_JSON)
.headers(defaultHeaders(env));

// when
var resultActions = mockMvc.perform(requestBuilder);

// then
resultActions.andExpect(status().isNoContent());
checkKafkaMessageSent(work);
}

@Test
void indexResourceWithIndexDate_andFullIndexRequest() throws Exception {
// given
var work = resourceRepo.save(getSampleWork(null).setIndexDate(new Date()));
resourceRepo.save(getSampleInstanceResource(null, work));
var resourceEdge = work.getIncomingEdges().iterator().next();
resourceEdge.computeId();
resourceEdgeRepository.save(resourceEdge);

var requestBuilder = put(INDEX_URL)
.param("full", "true")
.contentType(APPLICATION_JSON)
.headers(defaultHeaders(env));

// when
var resultActions = mockMvc.perform(requestBuilder);

// then
resultActions.andExpect(status().isNoContent());
checkKafkaMessageSent(work);
}

@SneakyThrows
protected void checkKafkaMessageSent(Resource indexed) {
if (nonNull(indexed)) {
awaitAndAssert(() -> assertTrue(consumer.getMessages()
.stream()
.anyMatch(m -> m.contains(indexed.getId().toString()) && m.contains(CREATE.getValue())))
);
assertThat(resourceRepo.isIndexDateNotNull(indexed.getId())).isTrue();
} else {
awaitAndAssert(() -> assertTrue(consumer.getMessages().isEmpty()));
}
}

}
61 changes: 61 additions & 0 deletions src/test/java/org/folio/linked/data/e2e/reindex/ReIndexFullIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.folio.linked.data.e2e.reindex;

import static org.folio.linked.data.test.MonographTestUtil.getSampleInstanceResource;
import static org.folio.linked.data.test.MonographTestUtil.getSampleWork;
import static org.folio.linked.data.test.TestUtil.defaultHeaders;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.util.Date;
import org.folio.linked.data.e2e.base.IntegrationTest;
import org.junit.jupiter.api.Test;

@IntegrationTest
class ReIndexFullIT extends ReindexITBase {

@Test
void indexResourceWithNoIndexDate_andFullIndexRequest() throws Exception {
// given
var work = resourceRepo.save(getSampleWork(null));
resourceRepo.save(getSampleInstanceResource(null, work));
var resourceEdge = work.getIncomingEdges().iterator().next();
resourceEdge.computeId();
resourceEdgeRepository.save(resourceEdge);

var requestBuilder = put(INDEX_URL)
.param("full", "true")
.contentType(APPLICATION_JSON)
.headers(defaultHeaders(env));

// when
var resultActions = mockMvc.perform(requestBuilder);

// then
resultActions.andExpect(status().isNoContent());
checkKafkaMessageSent(work);
}

@Test
void indexResourceWithIndexDate_andFullIndexRequest() throws Exception {
// given
var work = resourceRepo.save(getSampleWork(null).setIndexDate(new Date()));
resourceRepo.save(getSampleInstanceResource(null, work));
var resourceEdge = work.getIncomingEdges().iterator().next();
resourceEdge.computeId();
resourceEdgeRepository.save(resourceEdge);

var requestBuilder = put(INDEX_URL)
.param("full", "true")
.contentType(APPLICATION_JSON)
.headers(defaultHeaders(env));

// when
var resultActions = mockMvc.perform(requestBuilder);

// then
resultActions.andExpect(status().isNoContent());
checkKafkaMessageSent(work);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.folio.linked.data.e2e.reindex;

import static org.folio.linked.data.test.MonographTestUtil.getSampleInstanceResource;
import static org.folio.linked.data.test.MonographTestUtil.getSampleWork;
import static org.folio.linked.data.test.TestUtil.defaultHeaders;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.util.Date;
import org.folio.linked.data.e2e.base.IntegrationTest;
import org.junit.jupiter.api.Test;

@IntegrationTest
class ReIndexNotFullIT extends ReindexITBase {

@Test
void indexResourceWithNoIndexDate_andNotFullIndexRequest() throws Exception {
// given
var work = resourceRepo.save(getSampleWork(null)).setId(1L);
var instance = resourceRepo.save(getSampleInstanceResource(null, work));
var anotherWork = getSampleWork(instance);
anotherWork.setId(2L);
resourceRepo.save(anotherWork);
var resourceEdge = work.getIncomingEdges().iterator().next();
resourceEdge.computeId();
resourceEdgeRepository.save(resourceEdge);

var requestBuilder = put(INDEX_URL)
.contentType(APPLICATION_JSON)
.headers(defaultHeaders(env));

// when
var resultActions = mockMvc.perform(requestBuilder);

// then
resultActions.andExpect(status().isNoContent());
checkKafkaMessageSent(work);
}

@Test
void notIndexResourceWithIndexDate_andNotFullIndexRequest() throws Exception {
// given
var work = resourceRepo.save(getSampleWork(null).setIndexDate(new Date()));
resourceRepo.save(getSampleInstanceResource(null, work));

var requestBuilder = put(INDEX_URL)
.contentType(APPLICATION_JSON)
.headers(defaultHeaders(env));

// when
var resultActions = mockMvc.perform(requestBuilder);

// then
resultActions.andExpect(status().isNoContent());
checkKafkaMessageSent(null);
}

}
79 changes: 79 additions & 0 deletions src/test/java/org/folio/linked/data/e2e/reindex/ReindexITBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.folio.linked.data.e2e.reindex;

import static java.util.Objects.nonNull;
import static org.assertj.core.api.Assertions.assertThat;
import static org.folio.linked.data.domain.dto.ResourceIndexEventType.CREATE;
import static org.folio.linked.data.test.TestUtil.TENANT_ID;
import static org.folio.linked.data.test.TestUtil.awaitAndAssert;
import static org.folio.linked.data.test.TestUtil.cleanResourceTables;
import static org.junit.jupiter.api.Assertions.assertTrue;

import lombok.SneakyThrows;
import org.folio.linked.data.e2e.base.IntegrationTest;
import org.folio.linked.data.model.entity.Resource;
import org.folio.linked.data.repo.ResourceEdgeRepository;
import org.folio.linked.data.service.tenant.TenantScopedExecutionService;
import org.folio.linked.data.test.ResourceTestRepository;
import org.folio.linked.data.test.kafka.KafkaSearchWorkIndexTopicListener;
import org.folio.spring.tools.kafka.KafkaAdminService;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.web.servlet.MockMvc;

@IntegrationTest
abstract class ReindexITBase {

public static final String INDEX_URL = "/reindex";

@Autowired
protected MockMvc mockMvc;
@Autowired
protected ResourceTestRepository resourceRepo;
@Autowired
protected ResourceEdgeRepository resourceEdgeRepository;
@Autowired
protected Environment env;
@Autowired
protected KafkaSearchWorkIndexTopicListener consumer;
@Autowired
protected JdbcTemplate jdbcTemplate;
@Autowired
protected TenantScopedExecutionService tenantScopedExecutionService;
@Autowired
protected KafkaAdminService kafkaAdminService;

@BeforeAll
static void beforeAll(@Autowired KafkaAdminService kafkaAdminService) {
kafkaAdminService.createTopics(TENANT_ID);
}

@BeforeEach
protected void beforeEach() {
kafkaAdminService.restartEventListeners();
}

@BeforeEach
protected void afterEach() {
tenantScopedExecutionService.execute(TENANT_ID, () -> {
cleanResourceTables(jdbcTemplate);
});
consumer.getMessages().clear();
}

@SneakyThrows
protected void checkKafkaMessageSent(Resource indexed) {
if (nonNull(indexed)) {
awaitAndAssert(() -> assertTrue(consumer.getMessages()
.stream()
.anyMatch(m -> m.contains(indexed.getId().toString()) && m.contains(CREATE.getValue())))
);
assertThat(resourceRepo.isIndexDateNotNull(indexed.getId())).isTrue();
} else {
awaitAndAssert(() -> assertTrue(consumer.getMessages().isEmpty()));
}
}

}

0 comments on commit fce324f

Please sign in to comment.