Skip to content

Commit 807cfe7

Browse files
committed
DATAES-576 - Fix CustomMethodRepositoryRestTests.
Register custom template bean as workaround to delete the index before repository bootstrapping.
1 parent 1f04e64 commit 807cfe7

File tree

5 files changed

+48
-9
lines changed

5 files changed

+48
-9
lines changed

src/test/java/org/springframework/data/elasticsearch/repositories/CustomMethodRepositoryBaseTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.data.domain.PageRequest;
3131
import org.springframework.data.domain.Sort;
3232
import org.springframework.data.domain.Sort.Order;
33+
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
3334
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
3435
import org.springframework.data.elasticsearch.entities.SampleEntity;
3536
import org.springframework.data.elasticsearch.repositories.custom.SampleCustomMethodRepository;
@@ -46,7 +47,6 @@
4647
* @author Christoph Strobl
4748
* @author Don Wellington
4849
*/
49-
5050
public abstract class CustomMethodRepositoryBaseTests {
5151

5252
@Autowired private SampleCustomMethodRepository repository;

src/test/java/org/springframework/data/elasticsearch/repositories/CustomMethodRepositoryRestTests.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,27 @@
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
15-
*/package org.springframework.data.elasticsearch.repositories;
15+
*/
16+
package org.springframework.data.elasticsearch.repositories;
1617

18+
import org.elasticsearch.client.RestHighLevelClient;
1719
import org.junit.Before;
1820
import org.junit.runner.RunWith;
21+
1922
import org.springframework.beans.factory.annotation.Autowired;
2023
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
2124
import org.springframework.data.elasticsearch.entities.SampleEntity;
2225
import org.springframework.test.context.ContextConfiguration;
2326
import org.springframework.test.context.junit4.SpringRunner;
2427

2528
/**
26-
*
2729
* @author Don Wellington
28-
*
30+
* @author Mark Paluch
2931
*/
3032
@RunWith(SpringRunner.class)
3133
@ContextConfiguration("classpath:custom-method-repository-rest-test.xml")
3234
public class CustomMethodRepositoryRestTests extends CustomMethodRepositoryBaseTests {
35+
3336
@Autowired private ElasticsearchRestTemplate elasticsearchTemplate;
3437

3538
@Before
@@ -39,4 +42,18 @@ public void before() {
3942
elasticsearchTemplate.putMapping(SampleEntity.class);
4043
elasticsearchTemplate.refresh(SampleEntity.class);
4144
}
45+
46+
/**
47+
* Workaround because {@link SampleEntity} is used like everywhere and there's little chance to find the place where
48+
* the mapping is altered so that it becomes incompatible with the one created on repository startup.
49+
* <p>
50+
* TODO: Remove me once the cause is fixed! See: https://jira.spring.io/browse/DATAES-576
51+
*/
52+
public static class CleanupOnStartElasticsearchTemplate extends ElasticsearchRestTemplate {
53+
54+
public CleanupOnStartElasticsearchTemplate(RestHighLevelClient client) {
55+
super(client);
56+
deleteIndex(SampleEntity.class);
57+
}
58+
}
4259
}

src/test/java/org/springframework/data/elasticsearch/repositories/CustomMethodRepositoryTests.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,24 @@
1515
*/
1616
package org.springframework.data.elasticsearch.repositories;
1717

18+
import org.elasticsearch.client.Client;
1819
import org.junit.Before;
1920
import org.junit.runner.RunWith;
21+
2022
import org.springframework.beans.factory.annotation.Autowired;
2123
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
2224
import org.springframework.data.elasticsearch.entities.SampleEntity;
2325
import org.springframework.test.context.ContextConfiguration;
2426
import org.springframework.test.context.junit4.SpringRunner;
2527

2628
/**
27-
*
2829
* @author Don Wellington
29-
*
30+
* @author Mark Paluch
3031
*/
3132
@RunWith(SpringRunner.class)
3233
@ContextConfiguration("classpath:custom-method-repository-test.xml")
3334
public class CustomMethodRepositoryTests extends CustomMethodRepositoryBaseTests {
35+
3436
@Autowired private ElasticsearchTemplate elasticsearchTemplate;
3537

3638
@Before
@@ -40,4 +42,18 @@ public void before() {
4042
elasticsearchTemplate.putMapping(SampleEntity.class);
4143
elasticsearchTemplate.refresh(SampleEntity.class);
4244
}
45+
46+
/**
47+
* Workaround because {@link SampleEntity} is used like everywhere and there's little chance to find the place where
48+
* the mapping is altered so that it becomes incompatible with the one created on repository startup.
49+
* <p>
50+
* TODO: Remove me once the cause is fixed! See: https://jira.spring.io/browse/DATAES-576
51+
*/
52+
static class CleanupOnStartElasticsearchTemplate extends ElasticsearchTemplate {
53+
54+
public CleanupOnStartElasticsearchTemplate(Client client) {
55+
super(client);
56+
deleteIndex(SampleEntity.class);
57+
}
58+
}
4359
}

src/test/resources/custom-method-repository-rest-test.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@
55
xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch https://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd
66
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
77

8-
<import resource="elasticsearch-rest-template-test.xml"/>
8+
<import resource="infrastructure.xml"/>
9+
10+
<bean name="elasticsearchTemplate"
11+
class="org.springframework.data.elasticsearch.repositories.CustomMethodRepositoryRestTests$CleanupOnStartElasticsearchTemplate">
12+
<constructor-arg name="client" ref="restClient"/>
13+
</bean>
14+
15+
<elasticsearch:rest-client id="restClient"/>
916

1017
<elasticsearch:repositories
1118
base-package="org.springframework.data.elasticsearch.repositories.custom"/>

src/test/resources/custom-method-repository-test.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
<import resource="infrastructure.xml"/>
99

1010
<bean name="elasticsearchTemplate"
11-
class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
11+
class="org.springframework.data.elasticsearch.repositories.CustomMethodRepositoryTests$CleanupOnStartElasticsearchTemplate">
1212
<constructor-arg name="client" ref="client"/>
1313
</bean>
1414

15-
1615
<elasticsearch:repositories
1716
base-package="org.springframework.data.elasticsearch.repositories.custom"/>
1817

0 commit comments

Comments
 (0)