|
| 1 | +/* |
| 2 | + * Copyright 2021 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.convert; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | +import static org.junit.jupiter.params.provider.Arguments.*; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.List; |
| 24 | +import java.util.stream.Stream; |
| 25 | + |
| 26 | +import org.junit.jupiter.api.DisplayName; |
| 27 | +import org.junit.jupiter.api.Named; |
| 28 | +import org.junit.jupiter.params.ParameterizedTest; |
| 29 | +import org.junit.jupiter.params.provider.Arguments; |
| 30 | +import org.junit.jupiter.params.provider.MethodSource; |
| 31 | +import org.springframework.data.elasticsearch.annotations.DateFormat; |
| 32 | +import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty; |
| 33 | +import org.springframework.data.elasticsearch.core.mapping.PropertyValueConverter; |
| 34 | +import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext; |
| 35 | +import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchPersistentEntity; |
| 36 | +import org.springframework.lang.Nullable; |
| 37 | + |
| 38 | +/** |
| 39 | + * @author Peter-Josef Meisch |
| 40 | + */ |
| 41 | +public class PropertyValueConvertersUnitTests { |
| 42 | + |
| 43 | + @ParameterizedTest(name = "{0}") // #2018 |
| 44 | + @MethodSource("propertyValueConverters") |
| 45 | + @DisplayName("should return original object on write if it cannot be converted") |
| 46 | + void shouldReturnOriginalObjectOnWriteIfItCannotBeConverted(PropertyValueConverter converter) { |
| 47 | + |
| 48 | + NoConverterForThisClass value = new NoConverterForThisClass(); |
| 49 | + |
| 50 | + Object written = converter.write(value); |
| 51 | + |
| 52 | + assertThat(written).isEqualTo(value.toString()); |
| 53 | + } |
| 54 | + |
| 55 | + static Stream<Arguments> propertyValueConverters() { |
| 56 | + |
| 57 | + SimpleElasticsearchMappingContext context = new SimpleElasticsearchMappingContext(); |
| 58 | + SimpleElasticsearchPersistentEntity<?> persistentEntity = context |
| 59 | + .getRequiredPersistentEntity(NoConverterForThisClass.class); |
| 60 | + ElasticsearchPersistentProperty persistentProperty = persistentEntity.getRequiredPersistentProperty("property"); |
| 61 | + |
| 62 | + List<PropertyValueConverter> converters = new ArrayList<>(); |
| 63 | + |
| 64 | + converters.add(new DatePropertyValueConverter(persistentProperty, |
| 65 | + Collections.singletonList(ElasticsearchDateConverter.of(DateFormat.basic_date)))); |
| 66 | + converters.add(new DateRangePropertyValueConverter(persistentProperty, |
| 67 | + Collections.singletonList(ElasticsearchDateConverter.of(DateFormat.basic_date)))); |
| 68 | + converters.add(new NumberRangePropertyValueConverter(persistentProperty)); |
| 69 | + converters.add(new TemporalPropertyValueConverter(persistentProperty, |
| 70 | + Collections.singletonList(ElasticsearchDateConverter.of(DateFormat.basic_date)))); |
| 71 | + converters.add(new TemporalRangePropertyValueConverter(persistentProperty, |
| 72 | + Collections.singletonList(ElasticsearchDateConverter.of(DateFormat.basic_date)))); |
| 73 | + |
| 74 | + return converters.stream().map(propertyValueConverter -> arguments( |
| 75 | + Named.of(propertyValueConverter.getClass().getSimpleName(), propertyValueConverter))); |
| 76 | + } |
| 77 | + |
| 78 | + static class NoConverterForThisClass { |
| 79 | + @SuppressWarnings("unused") |
| 80 | + @Nullable Long property; |
| 81 | + } |
| 82 | +} |
0 commit comments