Class WithOpenSearchData

java.lang.Object
fr.ght1pc9kc.testy.search.WithOpenSearchData
All Implemented Interfaces:
org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.Extension, org.junit.jupiter.api.extension.ParameterResolver, org.junit.jupiter.api.extension.TestInstantiationAwareExtension

public final class WithOpenSearchData extends Object implements org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.ParameterResolver
Extension resetting OpenSearch indices with data before each test.

Implementation of the data set:

 public class UserDataSet implements OpenSearchDataSet<User> {
     @Override
     public List<User> documents() {
         return List.of(new User("okenobi", "Obiwan", "Kenobi"));
     }

     @Override
     public String identifier() {
         return "login";
     }
 }
 

Use of the extension. Before each test, the index is deleted and contains only the documents of the data set, already refreshed and searchable:

 class UserSearchRepositoryTest {
     private static final WithEmbeddedOpenSearch W_OPENSEARCH = WithEmbeddedOpenSearch.builder().build();
     private static final WithObjectMapper W_OBJECT_MAPPER = WithObjectMapper.builder().build();
     private static final WithOpenSearchData W_OPENSEARCH_DATA = WithOpenSearchData.builder(W_OPENSEARCH)
             .withObjectMapper(W_OBJECT_MAPPER)
             .addDataset("user", new UserDataSet())
             .build();

     @RegisterExtension
     static final ChainedExtension CHAIN = ChainedExtension.outer(W_OPENSEARCH)
             .append(W_OBJECT_MAPPER)
             .append(W_OPENSEARCH_DATA)
             .register();
 }
 

Data Tracker

A test that does not modify the indices can inject the WithOpenSearchData.Tracker to skip the next reload.


 @Test
  void should_read_data(WithOpenSearchData.Tracker tracker) {
      tracker.skipNextSampleLoad();
      ...
  }
 
  • Method Details

    • beforeAll

      public void beforeAll(org.junit.jupiter.api.extension.ExtensionContext context)
      Specified by:
      beforeAll in interface org.junit.jupiter.api.extension.BeforeAllCallback
    • beforeEach

      public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context) throws IOException
      Specified by:
      beforeEach in interface org.junit.jupiter.api.extension.BeforeEachCallback
      Throws:
      IOException
    • supportsParameter

      public boolean supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext context)
      Specified by:
      supportsParameter in interface org.junit.jupiter.api.extension.ParameterResolver
    • resolveParameter

      public Object resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
      Specified by:
      resolveParameter in interface org.junit.jupiter.api.extension.ParameterResolver
    • builder

      Parameters:
      wOpenSearch - Embedded OpenSearch node.
      Returns:
      WithOpenSearchData.WithOpenSearchDataBuilder.