DATAES-462 - Polishing.

SimpleElasticsearchPersistentProperty now already checks for the correct type of score properties. Added unit tests for that. Also added unit tests for SimpleElasticsearchPersistentEntity rejecting more than one score property being present.

Additional non-null assertions on components that are required so that we can remove superfluous null checks.

A bit o formatting, Javadoc, missing @since tags and license headers.

Original pull request: #207.
This commit is contained in:
Oliver Gierke
2018-06-13 18:56:12 +02:00
parent d996406113
commit 5ddb46c435
15 changed files with 160 additions and 26 deletions

View File

@@ -46,6 +46,7 @@ import org.springframework.data.elasticsearch.entities.Car;
import org.springframework.data.elasticsearch.entities.SampleEntity;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

View File

@@ -36,7 +36,6 @@ import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.hamcrest.Matchers;
@@ -1516,6 +1515,7 @@ public class ElasticsearchTemplateTests {
@Test // DATAES-462
public void shouldReturnScores() {
// given
List<IndexQuery> indexQueries = new ArrayList<>();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2017 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,10 +15,13 @@
*/
package org.springframework.data.elasticsearch.core.mapping;
import static org.assertj.core.api.Assertions.*;
import java.beans.IntrospectionException;
import org.junit.Test;
import org.springframework.data.annotation.Version;
import org.springframework.data.elasticsearch.annotations.Score;
import org.springframework.data.mapping.MappingException;
import org.springframework.data.mapping.model.Property;
import org.springframework.data.mapping.model.SimpleTypeHolder;
@@ -30,6 +33,7 @@ import org.springframework.util.ReflectionUtils;
* @author Rizwan Idrees
* @author Mohsin Husen
* @author Mark Paluch
* @author Oliver Gierke
*/
public class SimpleElasticsearchPersistentEntityTests {
@@ -62,6 +66,17 @@ public class SimpleElasticsearchPersistentEntityTests {
// when
entity.addPersistentProperty(persistentProperty2);
}
@Test // DATAES-462
public void rejectsMultipleScoreProperties() {
SimpleElasticsearchMappingContext context = new SimpleElasticsearchMappingContext();
assertThatExceptionOfType(MappingException.class) //
.isThrownBy(() -> context.getRequiredPersistentEntity(TwoScoreProperties.class)) //
.withMessageContaining("first") //
.withMessageContaining("second");
}
private static SimpleElasticsearchPersistentProperty createProperty(SimpleElasticsearchPersistentEntity<?> entity,
String field) {
@@ -106,4 +121,12 @@ public class SimpleElasticsearchPersistentEntityTests {
this.version2 = version2;
}
}
// DATAES-462
static class TwoScoreProperties {
@Score float first;
@Score float second;
}
}

View File

@@ -0,0 +1,44 @@
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.elasticsearch.core.mapping;
import static org.assertj.core.api.Assertions.*;
import org.junit.Test;
import org.springframework.data.elasticsearch.annotations.Score;
import org.springframework.data.mapping.MappingException;
/**
* Unit tests for {@link SimpleElasticsearchPersistentProperty}.
*
* @author Oliver Gierke
*/
public class SimpleElasticsearchPersistentPropertyUnitTests {
@Test // DATAES-462
public void rejectsScorePropertyOfTypeOtherthanFloat() {
SimpleElasticsearchMappingContext context = new SimpleElasticsearchMappingContext();
assertThatExceptionOfType(MappingException.class) //
.isThrownBy(() -> context.getRequiredPersistentEntity(InvalidScoreProperty.class)) //
.withMessageContaining("scoreProperty");
}
static class InvalidScoreProperty {
@Score String scoreProperty;
}
}