1414 */
1515package org .eclipse .jnosql .databases .elasticsearch .communication ;
1616
17+ import co .elastic .clients .elasticsearch .ElasticsearchClient ;
18+ import co .elastic .clients .elasticsearch ._types .ElasticsearchException ;
19+ import co .elastic .clients .elasticsearch .core .IndexRequest ;
20+ import co .elastic .clients .elasticsearch .core .IndexResponse ;
21+ import co .elastic .clients .elasticsearch .indices .CreateIndexRequest ;
22+ import co .elastic .clients .elasticsearch .indices .CreateIndexResponse ;
23+ import co .elastic .clients .elasticsearch .indices .DeleteIndexRequest ;
24+ import co .elastic .clients .elasticsearch .indices .PutMappingRequest ;
25+ import co .elastic .clients .elasticsearch .indices .PutMappingResponse ;
26+ import co .elastic .clients .json .JsonData ;
27+ import co .elastic .clients .util .ObjectBuilder ;
1728import org .eclipse .jnosql .communication .Settings ;
29+ import org .jetbrains .annotations .NotNull ;
1830import org .testcontainers .containers .GenericContainer ;
1931import org .testcontainers .containers .wait .strategy .Wait ;
2032
33+ import java .io .IOException ;
2134import java .util .HashMap ;
2235import java .util .Map ;
36+ import java .util .Optional ;
37+ import java .util .function .Consumer ;
38+ import java .util .function .Function ;
2339import java .util .function .Supplier ;
2440
41+ import static org .assertj .core .api .SoftAssertions .assertSoftly ;
42+
2543public enum DocumentDatabase implements Supplier <ElasticsearchDocumentManagerFactory > {
2644
2745 INSTANCE ;
@@ -31,25 +49,113 @@ public enum DocumentDatabase implements Supplier<ElasticsearchDocumentManagerFac
3149 .withReuse (true )
3250 .withExposedPorts (9200 , 9300 )
3351 .withEnv ("discovery.type" , "single-node" )
34- .withEnv ("ES_JAVA_OPTS" ,"-Xms1g -Xmx1g" )
35- .withEnv ("xpack.security.enabled" ,"false" )
52+ .withEnv ("ES_JAVA_OPTS" , "-Xms1g -Xmx1g" )
53+ .withEnv ("xpack.security.enabled" , "false" )
3654 .waitingFor (Wait .forHttp ("/" )
3755 .forPort (9200 )
3856 .forStatusCode (200 ));
57+
3958 {
4059 es .start ();
4160 }
4261
62+ public static void clearDatabase (String index ) {
63+ try (var elasticsearch = INSTANCE .newElasticsearchClient ()) {
64+ var response = elasticsearch .client ().indices ().delete (DeleteIndexRequest .of (d ->
65+ d .index (index )));
66+ assertSoftly (softly -> {
67+ softly .assertThat (response .acknowledged ())
68+ .isTrue ();
69+ });
70+ } catch (Exception e ) {
71+ if ( e instanceof ElasticsearchException ){
72+ e .printStackTrace ();
73+ return ;
74+ }
75+ throw new RuntimeException (e );
76+ }
77+ }
78+
79+ public static void createDatabase (String index ) {
80+ try (var elasticsearch = INSTANCE .newElasticsearchClient ()) {
81+ CreateIndexResponse response = elasticsearch .client ().indices ().create (
82+ CreateIndexRequest .of (b -> b .index (index )));
83+ assertSoftly (softly -> {
84+ softly .assertThat (response .acknowledged ())
85+ .isTrue ();
86+ });
87+ } catch (Exception e ) {
88+ throw new RuntimeException (e );
89+ }
90+ }
91+
92+ public static void updateMapping (String index , Function <PutMappingRequest .Builder , ObjectBuilder <PutMappingRequest >> fn ) {
93+ try (var elasticsearch = INSTANCE .newElasticsearchClient ()) {
94+ PutMappingResponse response = elasticsearch .client ().indices ().putMapping (fn );
95+ assertSoftly (softly -> {
96+ softly .assertThat (response .acknowledged ())
97+ .isTrue ();
98+ });
99+ } catch (Exception e ) {
100+ throw new RuntimeException (e );
101+ }
102+ }
103+
104+ public static void insertData (String index , Map <String , Object > map ) {
105+ try (var elasticsearch = INSTANCE .newElasticsearchClient ()) {
106+ var _id = Optional .ofNullable (map .remove ("_id" ));
107+
108+ var indexRequest = _id
109+ .map (String .class ::cast )
110+ .map (id -> IndexRequest .of (b ->
111+ b .index (index )
112+ .id (id ).document (JsonData .of (map ))))
113+ .orElseGet (() -> IndexRequest .of (b ->
114+ b .index (index ).document (JsonData .of (map ))));
115+
116+ IndexResponse response = elasticsearch .client ().index (indexRequest );
117+
118+ assertSoftly (softly -> {
119+ softly .assertThat (response .result ().jsonValue ())
120+ .isEqualTo ("created" );
121+ });
122+ _id .ifPresent (id -> map .put ("_id" , id ));
123+ } catch (Exception e ) {
124+ throw new RuntimeException (e );
125+ }
126+
127+ }
128+
43129
44130 @ Override
45131 public ElasticsearchDocumentManagerFactory get () {
46132 ElasticsearchDocumentConfiguration configuration = new ElasticsearchDocumentConfiguration ();
133+ Settings settings1 = getSettings ();
134+ return configuration .apply (settings1 );
135+ }
136+
137+ @ NotNull
138+ public Settings getSettings () {
47139 Map <String , Object > settings = new HashMap <>();
48- settings .put (ElasticsearchConfigurations .HOST .get ()+".1" , host ());
49- return configuration .apply (Settings .of (settings ));
140+ settings .put (ElasticsearchConfigurations .HOST .get () + ".1" , host ());
141+ return Settings .of (settings );
142+ }
143+
144+ public ElasticsearchClientAutoClosable newElasticsearchClient () {
145+ ElasticsearchDocumentConfiguration configuration = new ElasticsearchDocumentConfiguration ();
146+ return new ElasticsearchClientAutoClosable (configuration .buildElasticsearchClient (getSettings ()));
147+ }
148+
149+ public static record ElasticsearchClientAutoClosable (
150+ ElasticsearchClient client ) implements AutoCloseable {
151+
152+ @ Override
153+ public void close () throws Exception {
154+ this .client ._transport ().close ();
155+ }
50156 }
51157
52- public ElasticsearchDocumentManager get (String database ){
158+ public ElasticsearchDocumentManager get (String database ) {
53159 ElasticsearchDocumentManagerFactory managerFactory = get ();
54160 return managerFactory .apply (database );
55161 }
0 commit comments