DATAES-456 - Polishing.

This commit is contained in:
P.J.Meisch 2019-06-27 08:10:05 +02:00
parent bf4948239e
commit 03567d4281
5 changed files with 40 additions and 78 deletions

View File

@ -47,7 +47,8 @@ public class MappingElasticsearchEntityInformation<T, ID> extends PersistentEnti
this(entity, entity.getIndexName(), entity.getIndexType(), entity.getVersionType());
}
public MappingElasticsearchEntityInformation(ElasticsearchPersistentEntity<T> entity, String indexName, String type, VersionType versionType) {
public MappingElasticsearchEntityInformation(ElasticsearchPersistentEntity<T> entity, String indexName, String type,
VersionType versionType) {
super(entity);
this.entityMetadata = entity;
@ -76,7 +77,8 @@ public class MappingElasticsearchEntityInformation<T, ID> extends PersistentEnti
ElasticsearchPersistentProperty versionProperty = entityMetadata.getVersionProperty();
try {
return versionProperty != null ? (Long) entityMetadata.getPropertyAccessor(entity).getProperty(versionProperty) : null;
return versionProperty != null ? (Long) entityMetadata.getPropertyAccessor(entity).getProperty(versionProperty)
: null;
} catch (Exception e) {
throw new IllegalStateException("failed to load version field", e);
}
@ -92,7 +94,8 @@ public class MappingElasticsearchEntityInformation<T, ID> extends PersistentEnti
ElasticsearchPersistentProperty parentProperty = entityMetadata.getParentIdProperty();
try {
return parentProperty != null ? (String) entityMetadata.getPropertyAccessor(entity).getProperty(parentProperty) : null;
return parentProperty != null ? (String) entityMetadata.getPropertyAccessor(entity).getProperty(parentProperty)
: null;
} catch (Exception e) {
throw new IllegalStateException("failed to load parent ID: " + e, e);
}

View File

@ -1,39 +0,0 @@
/*
* Copyright 2019 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.entities;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
/**
* DynamicIndexEntity
*
* @author Sylvain Laurent
*/
@Document(indexName = "#{@indexNameProvider.getIndexName()}", createIndex = false)
public class DynamicIndexEntity {
@Id private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.elasticsearch;
package org.springframework.data.elasticsearch.repositories.dynamicindex;
import static org.junit.Assert.*;
@ -24,9 +24,10 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.entities.DynamicIndexEntity;
import org.springframework.data.elasticsearch.repositories.dynamicindex.DynamicIndexRepository;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ -34,6 +35,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
* DynamicIndexEntityTests
*
* @author Sylvain Laurent
* @author Peter-Josef Meisch
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ -48,6 +50,7 @@ public class DynamicIndexEntityTests {
@Before
public void init() {
deleteIndexes();
template.createIndex("index1");
template.createIndex("index2");
@ -58,8 +61,15 @@ public class DynamicIndexEntityTests {
deleteIndexes();
}
private void deleteIndexes() {
template.deleteIndex("index1");
template.deleteIndex("index2");
}
@Test // DATAES-456
public void indexNameIsDynamicallyProvided() {
int initialCallsCount = indexNameProvider.callsCount;
indexNameProvider.setIndexName("index1");
@ -69,7 +79,6 @@ public class DynamicIndexEntityTests {
indexNameProvider.setIndexName("index2");
assertEquals(0L, repository.count());
}
@ImportResource(value = "classpath:/dynamic-index-repository-test.xml")
@ -79,13 +88,17 @@ public class DynamicIndexEntityTests {
public IndexNameProvider indexNameProvider() {
return new IndexNameProvider();
}
}
static class IndexNameProvider {
private String indexName;
int callsCount;
public String getIndexName() {
callsCount++;
return indexName;
}
@ -93,11 +106,23 @@ public class DynamicIndexEntityTests {
public void setIndexName(String indexName) {
this.indexName = indexName;
}
}
private void deleteIndexes() {
template.deleteIndex("index1");
template.deleteIndex("index2");
@Document(indexName = "#{@indexNameProvider.getIndexName()}", createIndex = false)
public static class DynamicIndexEntity {
@Id private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
public interface DynamicIndexRepository extends ElasticsearchRepository<DynamicIndexEntity, String> {}
}

View File

@ -1,28 +0,0 @@
/*
* Copyright 2019 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.repositories.dynamicindex;
import org.springframework.data.elasticsearch.entities.DynamicIndexEntity;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
/**
* DynamicIndexRepository
*
* @author Sylvain Laurent
*/
public interface DynamicIndexRepository extends ElasticsearchRepository<DynamicIndexEntity, String> {
}

View File

@ -13,6 +13,7 @@
</bean>
<elasticsearch:repositories
base-package="org.springframework.data.elasticsearch.repositories.dynamicindex"/>
base-package="org.springframework.data.elasticsearch.repositories.dynamicindex"
consider-nested-repositories="true"/>
</beans>