View Javadoc
1   /*
2    * Copyright 2023 Adrian Herscu
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package dev.aherscu.qa.jgiven.elasticsearch.steps;
18  
19  import java.util.function.*;
20  
21  import com.tngtech.jgiven.annotation.*;
22  
23  import co.elastic.clients.elasticsearch.*;
24  import dev.aherscu.qa.jgiven.commons.steps.*;
25  import dev.aherscu.qa.jgiven.elasticsearch.model.*;
26  import lombok.extern.slf4j.*;
27  
28  @Slf4j
29  public class ElasticSearchFixtures<T, TDocument, SELF extends ElasticSearchFixtures<T, TDocument, SELF>>
30      extends GenericFixtures<ElasticSearchScenarioType<TDocument>, SELF> {
31  
32      @ProvidedScenarioState
33      protected final ThreadLocal<String>                 index        =
34          new ThreadLocal<>();
35      @ProvidedScenarioState
36      protected final ThreadLocal<Class<TDocument>>       documentType =
37          new ThreadLocal<>();
38      @ProvidedScenarioState
39      protected final ThreadLocal<Function<TDocument, T>> convertBy    =
40          ThreadLocal.withInitial(() -> tDocument -> (T) tDocument);
41  
42      // see
43      // https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/object-lifecycles.html
44      @ProvidedScenarioState
45      protected ElasticsearchClient                       elasticsearchClient;
46  
47      @Hidden
48      public SELF convertingBy(final Function<TDocument, T> convertBy) {
49          log.debug("setting custom conversion function");
50          this.convertBy.set(convertBy);
51          return self();
52      }
53  
54      public SELF elastic_search(
55          @Hidden final ElasticsearchClient elasticsearchClient) {
56          log.debug("setting client {}", elasticsearchClient);
57          this.elasticsearchClient = elasticsearchClient;
58          return self();
59      }
60  
61      public SELF indexed_by(final String index) {
62          log.debug("setting index {}", index);
63          this.index.set(index);
64          return self();
65      }
66  
67      public SELF storing(final Class<TDocument> documentType) {
68          log.debug("setting document type {}", documentType);
69          this.documentType.set(documentType);
70          return self();
71      }
72  }