|
| 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.client.elc; |
| 17 | + |
| 18 | +import static com.github.tomakehurst.wiremock.client.WireMock.*; |
| 19 | +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.*; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.DisplayName; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 24 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 25 | +import org.springframework.beans.factory.annotation.Autowired; |
| 26 | +import org.springframework.context.annotation.Configuration; |
| 27 | +import org.springframework.data.annotation.Id; |
| 28 | +import org.springframework.data.elasticsearch.annotations.Document; |
| 29 | +import org.springframework.data.elasticsearch.annotations.Field; |
| 30 | +import org.springframework.data.elasticsearch.client.ClientConfiguration; |
| 31 | +import org.springframework.data.elasticsearch.core.ElasticsearchOperations; |
| 32 | +import org.springframework.lang.Nullable; |
| 33 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 34 | + |
| 35 | +import com.github.tomakehurst.wiremock.junit5.WireMockExtension; |
| 36 | + |
| 37 | +/** |
| 38 | + * Tests that need to check the data produced by the Elasticsearch client |
| 39 | + * @author Peter-Josef Meisch |
| 40 | + */ |
| 41 | +@ExtendWith(SpringExtension.class) |
| 42 | +public class ELCWiremockTests { |
| 43 | + |
| 44 | + @RegisterExtension static WireMockExtension wireMock = WireMockExtension.newInstance() |
| 45 | + .options(wireMockConfig() |
| 46 | + .dynamicPort() |
| 47 | + // needed, otherwise Wiremock goes to test/resources/mappings |
| 48 | + .usingFilesUnderDirectory("src/test/resources/wiremock-mappings")) |
| 49 | + .build(); |
| 50 | + |
| 51 | + @Configuration |
| 52 | + static class Config extends ElasticsearchConfiguration { |
| 53 | + @Override |
| 54 | + public ClientConfiguration clientConfiguration() { |
| 55 | + return ClientConfiguration.builder() |
| 56 | + .connectedTo("localhost:" + wireMock.getPort()) |
| 57 | + .build(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Autowired ElasticsearchOperations operations; |
| 62 | + |
| 63 | + @Test // #2839 |
| 64 | + @DisplayName("should store null values if configured") |
| 65 | + void shouldStoreNullValuesIfConfigured() { |
| 66 | + |
| 67 | + wireMock.stubFor(put(urlPathEqualTo("/null-fields/_doc/42")) |
| 68 | + .withRequestBody(equalToJson(""" |
| 69 | + { |
| 70 | + "_class": "org.springframework.data.elasticsearch.client.elc.ELCWiremockTests$EntityWithNullFields", |
| 71 | + "id": "42", |
| 72 | + "field1": null |
| 73 | + } |
| 74 | + """)) |
| 75 | + .willReturn( |
| 76 | + aResponse() |
| 77 | + .withStatus(200) |
| 78 | + .withHeader("X-elastic-product", "Elasticsearch") |
| 79 | + .withHeader("content-type", "application/vnd.elasticsearch+json;compatible-with=8") |
| 80 | + .withBody(""" |
| 81 | + { |
| 82 | + "_index": "null-fields", |
| 83 | + "_id": "42", |
| 84 | + "_version": 1, |
| 85 | + "result": "created", |
| 86 | + "forced_refresh": true, |
| 87 | + "_shards": { |
| 88 | + "total": 2, |
| 89 | + "successful": 1, |
| 90 | + "failed": 0 |
| 91 | + }, |
| 92 | + "_seq_no": 1, |
| 93 | + "_primary_term": 1 |
| 94 | + } |
| 95 | + """))); |
| 96 | + |
| 97 | + var entity = new EntityWithNullFields(); |
| 98 | + entity.setId("42"); |
| 99 | + |
| 100 | + operations.save(entity); |
| 101 | + // no need to assert anything, if the field1:null is not sent, we run into a 404 error |
| 102 | + } |
| 103 | + |
| 104 | + @Document(indexName = "null-fields") |
| 105 | + static class EntityWithNullFields { |
| 106 | + @Nullable |
| 107 | + @Id private String id; |
| 108 | + @Nullable |
| 109 | + @Field(storeNullValue = true) private String field1; |
| 110 | + @Nullable |
| 111 | + @Field private String field2; |
| 112 | + |
| 113 | + @Nullable |
| 114 | + public String getId() { |
| 115 | + return id; |
| 116 | + } |
| 117 | + |
| 118 | + public void setId(@Nullable String id) { |
| 119 | + this.id = id; |
| 120 | + } |
| 121 | + |
| 122 | + @Nullable |
| 123 | + public String getField1() { |
| 124 | + return field1; |
| 125 | + } |
| 126 | + |
| 127 | + public void setField1(@Nullable String field1) { |
| 128 | + this.field1 = field1; |
| 129 | + } |
| 130 | + |
| 131 | + @Nullable |
| 132 | + public String getField2() { |
| 133 | + return field2; |
| 134 | + } |
| 135 | + |
| 136 | + public void setField2(@Nullable String field2) { |
| 137 | + this.field2 = field2; |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments