|
| 1 | +/* |
| 2 | + * Copyright 2024 the original author or authors. |
| 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 | + * https://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 | +package org.springframework.data.elasticsearch.core.sql; |
| 17 | + |
| 18 | +import static org.springframework.data.elasticsearch.core.IndexOperationsAdapter.blocking; |
| 19 | + |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.AfterEach; |
| 23 | +import org.junit.jupiter.api.BeforeEach; |
| 24 | +import org.junit.jupiter.api.DisplayName; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.springframework.beans.factory.annotation.Autowired; |
| 27 | +import org.springframework.context.annotation.Configuration; |
| 28 | +import org.springframework.context.annotation.Import; |
| 29 | +import org.springframework.data.annotation.Id; |
| 30 | +import org.springframework.data.elasticsearch.annotations.Document; |
| 31 | +import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations; |
| 32 | +import org.springframework.data.elasticsearch.core.query.SqlQuery; |
| 33 | +import org.springframework.data.elasticsearch.junit.jupiter.ReactiveElasticsearchTemplateConfiguration; |
| 34 | +import org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest; |
| 35 | +import org.springframework.lang.Nullable; |
| 36 | +import org.springframework.test.context.ContextConfiguration; |
| 37 | + |
| 38 | +import reactor.test.StepVerifier; |
| 39 | + |
| 40 | +/** |
| 41 | + * Testing the reactive querying using SQL syntax. |
| 42 | + * |
| 43 | + * @author Youssef Aouichaoui |
| 44 | + */ |
| 45 | +@SpringIntegrationTest |
| 46 | +@ContextConfiguration(classes = { ReactiveSqlOperationsIntegrationTests.Config.class }) |
| 47 | +@DisplayName("Using Elasticsearch SQL Reactive Client") |
| 48 | +public class ReactiveSqlOperationsIntegrationTests { |
| 49 | + @Autowired ReactiveElasticsearchOperations operations; |
| 50 | + |
| 51 | + @BeforeEach |
| 52 | + void setUp() { |
| 53 | + // create index |
| 54 | + blocking(operations.indexOps(EntityForSQL.class)).createWithMapping(); |
| 55 | + |
| 56 | + // add data |
| 57 | + operations |
| 58 | + .saveAll(List.of(EntityForSQL.builder().withViews(3).build(), EntityForSQL.builder().withViews(0).build()), |
| 59 | + EntityForSQL.class) |
| 60 | + .blockLast(); |
| 61 | + } |
| 62 | + |
| 63 | + @AfterEach |
| 64 | + void tearDown() { |
| 65 | + // delete index |
| 66 | + blocking(operations.indexOps(EntityForSQL.class)).delete(); |
| 67 | + } |
| 68 | + |
| 69 | + // begin configuration region |
| 70 | + @Configuration |
| 71 | + @Import({ ReactiveElasticsearchTemplateConfiguration.class }) |
| 72 | + static class Config {} |
| 73 | + // end region |
| 74 | + |
| 75 | + @Test |
| 76 | + void when_search_with_an_sql_query() { |
| 77 | + // Given |
| 78 | + SqlQuery query = SqlQuery.builder("SELECT * FROM entity_for_sql WHERE views = 0").build(); |
| 79 | + |
| 80 | + // When |
| 81 | + |
| 82 | + // Then |
| 83 | + operations.search(query).as(StepVerifier::create).expectNextCount(1).verifyComplete(); |
| 84 | + } |
| 85 | + |
| 86 | + // begin region |
| 87 | + @Document(indexName = "entity_for_sql") |
| 88 | + static class EntityForSQL { |
| 89 | + @Id private String id; |
| 90 | + private final Integer views; |
| 91 | + |
| 92 | + public EntityForSQL(EntityForSQL.Builder builder) { |
| 93 | + this.views = builder.views; |
| 94 | + } |
| 95 | + |
| 96 | + @Nullable |
| 97 | + public String getId() { |
| 98 | + return id; |
| 99 | + } |
| 100 | + |
| 101 | + public Integer getViews() { |
| 102 | + return views; |
| 103 | + } |
| 104 | + |
| 105 | + public static EntityForSQL.Builder builder() { |
| 106 | + return new EntityForSQL.Builder(); |
| 107 | + } |
| 108 | + |
| 109 | + static class Builder { |
| 110 | + private Integer views = 0; |
| 111 | + |
| 112 | + public EntityForSQL.Builder withViews(Integer views) { |
| 113 | + this.views = views; |
| 114 | + |
| 115 | + return this; |
| 116 | + } |
| 117 | + |
| 118 | + public EntityForSQL build() { |
| 119 | + return new EntityForSQL(this); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + // end region |
| 124 | +} |
0 commit comments