Skip to content

Commit dc4d8e5

Browse files
committed
DATAES-654 - Add Junit5 support.
1 parent 4682636 commit dc4d8e5

23 files changed

+631
-16
lines changed

pom.xml

+12-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,18 @@
242242
</exclusions>
243243
</dependency>
244244

245-
<dependency>
245+
<dependency>
246+
<groupId>org.junit.jupiter</groupId>
247+
<artifactId>junit-jupiter</artifactId>
248+
<scope>test</scope>
249+
</dependency>
250+
<dependency>
251+
<groupId>org.junit.vintage</groupId>
252+
<artifactId>junit-vintage-engine</artifactId>
253+
<scope>test</scope>
254+
</dependency>
255+
256+
<dependency>
246257
<groupId>org.skyscreamer</groupId>
247258
<artifactId>jsonassert</artifactId>
248259
<version>1.5.0</version>

src/test/java/org/springframework/data/elasticsearch/ElasticsearchTestConfiguration.java

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.data.elasticsearch.config.ElasticsearchConfigurationSupport;
2323
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
2424
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
25+
import org.springframework.data.elasticsearch.junit.junit4.TestNodeResource;
2526

2627
/**
2728
* configuration class for the classic ElasticsearchTemplate. Needs a {@link TestNodeResource} bean that should be set up in
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2019 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;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import org.junit.jupiter.api.DisplayName;
21+
import org.junit.jupiter.api.Test;
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
25+
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
26+
import org.springframework.data.elasticsearch.junit.jupiter.ClusterConnectionInfo;
27+
import org.springframework.data.elasticsearch.junit.jupiter.ElasticsearchRestTemplateConfiguration;
28+
import org.springframework.data.elasticsearch.junit.jupiter.SpringDataElasticsearch;
29+
import org.springframework.data.elasticsearch.junit.jupiter.SpringDataElasticsearchTest;
30+
import org.springframework.test.context.ContextConfiguration;
31+
32+
/**
33+
* class demonstrating the setup of a JUnit 5 test in Spring Data Elasticsearch that uses the rest client. The
34+
* ContextConfiguration must include the test class itself (and provide a
35+
* {@link org.springframework.data.elasticsearch.junit.jupiter.ClusterConnectionInfo} bean) and the
36+
* {@link ElasticsearchRestTemplateConfiguration} class
37+
*
38+
* @author Peter-Josef Meisch
39+
*/
40+
@SpringDataElasticsearchTest
41+
@ContextConfiguration(
42+
classes = { JUnit5SampleRestClientBasedTests.class, ElasticsearchRestTemplateConfiguration.class })
43+
@DisplayName(" a sample JUnit 5 test with rest client")
44+
public class JUnit5SampleRestClientBasedTests {
45+
46+
// Test environment setup
47+
// this will be set by the SpringDataElasticsearchExtension, must be static
48+
@SpringDataElasticsearch static private ClusterConnectionInfo clusterConnectionInfo;
49+
50+
// must be provided as bean for the ElasticsearchRestTemplateConfiguration class
51+
@Bean
52+
private ClusterConnectionInfo clusterConnectionInfo() {
53+
return clusterConnectionInfo;
54+
}
55+
56+
// the stuff to test
57+
58+
@Autowired private ElasticsearchOperations elasticsearchOperations;
59+
60+
@Test
61+
@DisplayName("should have a ElasticsearchRestTemplate")
62+
void shouldHaveARestTemplate() {
63+
assertThat(elasticsearchOperations).isNotNull().isInstanceOf(ElasticsearchRestTemplate.class);
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2019 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;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import org.junit.jupiter.api.DisplayName;
21+
import org.junit.jupiter.api.Test;
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
25+
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
26+
import org.springframework.data.elasticsearch.junit.jupiter.ClusterConnectionInfo;
27+
import org.springframework.data.elasticsearch.junit.jupiter.ElasticsearchTemplateConfiguration;
28+
import org.springframework.data.elasticsearch.junit.jupiter.SpringDataElasticsearch;
29+
import org.springframework.data.elasticsearch.junit.jupiter.SpringDataElasticsearchTest;
30+
import org.springframework.test.context.ContextConfiguration;
31+
32+
/**
33+
* class demonstrating the setup of a JUnit 5 test in Spring Data Elasticsearch that uses the tramsport client. The
34+
* ContextConfiguration must include the test class itself (and provide a
35+
* {@link org.springframework.data.elasticsearch.junit.jupiter.ClusterConnectionInfo} bean) and the
36+
* {@link ElasticsearchTemplateConfiguration} class
37+
*
38+
* @author Peter-Josef Meisch
39+
*/
40+
@SpringDataElasticsearchTest
41+
@ContextConfiguration(
42+
classes = { JUnit5SampleTransportClientBasedTests.class, ElasticsearchTemplateConfiguration.class })
43+
@DisplayName(" a sample JUnit 5 test with transport client")
44+
public class JUnit5SampleTransportClientBasedTests {
45+
46+
// Test environment setup
47+
// this will be set by the SpringDataElasticsearchExtension, must be static
48+
@SpringDataElasticsearch static private ClusterConnectionInfo clusterConnectionInfo;
49+
50+
// must be provided as bean for the ElasticsearchTemplateConfiguration class
51+
@Bean
52+
private ClusterConnectionInfo clusterConnectionInfo() {
53+
return clusterConnectionInfo;
54+
}
55+
56+
// the stuff to test
57+
58+
@Autowired private ElasticsearchOperations elasticsearchOperations;
59+
60+
@Test
61+
@DisplayName("should have a ElasticsearchTemplate")
62+
void shouldHaveATemplate() {
63+
assertThat(elasticsearchOperations).isNotNull().isInstanceOf(ElasticsearchTemplate.class);
64+
}
65+
}

src/test/java/org/springframework/data/elasticsearch/client/reactive/ReactiveElasticsearchClientTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@
5353
import org.junit.Rule;
5454
import org.junit.Test;
5555
import org.junit.runner.RunWith;
56-
import org.springframework.data.elasticsearch.ElasticsearchVersion;
57-
import org.springframework.data.elasticsearch.ElasticsearchVersionRule;
56+
import org.springframework.data.elasticsearch.junit.junit4.ElasticsearchVersion;
57+
import org.springframework.data.elasticsearch.junit.junit4.ElasticsearchVersionRule;
5858
import org.springframework.data.elasticsearch.TestUtils;
5959
import org.springframework.data.elasticsearch.client.ClientConfiguration;
6060
import org.springframework.http.HttpHeaders;

src/test/java/org/springframework/data/elasticsearch/core/ReactiveElasticsearchTemplateTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@
5353
import org.springframework.data.domain.PageRequest;
5454
import org.springframework.data.domain.Pageable;
5555
import org.springframework.data.domain.Sort;
56-
import org.springframework.data.elasticsearch.ElasticsearchVersion;
57-
import org.springframework.data.elasticsearch.ElasticsearchVersionRule;
56+
import org.springframework.data.elasticsearch.junit.junit4.ElasticsearchVersion;
57+
import org.springframework.data.elasticsearch.junit.junit4.ElasticsearchVersionRule;
5858
import org.springframework.data.elasticsearch.TestUtils;
5959
import org.springframework.data.elasticsearch.annotations.Document;
6060
import org.springframework.data.elasticsearch.annotations.Field;

src/test/java/org/springframework/data/elasticsearch/ElasticsearchVersion.java renamed to src/test/java/org/springframework/data/elasticsearch/junit/junit4/ElasticsearchVersion.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.data.elasticsearch;
16+
package org.springframework.data.elasticsearch.junit.junit4;
1717

1818
import java.lang.annotation.Documented;
1919
import java.lang.annotation.ElementType;

src/test/java/org/springframework/data/elasticsearch/ElasticsearchVersionRule.java renamed to src/test/java/org/springframework/data/elasticsearch/junit/junit4/ElasticsearchVersionRule.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.data.elasticsearch;
16+
package org.springframework.data.elasticsearch.junit.junit4;
1717

1818
import java.util.concurrent.atomic.AtomicReference;
1919

@@ -23,6 +23,7 @@
2323
import org.junit.runners.model.Statement;
2424
import org.slf4j.Logger;
2525
import org.slf4j.LoggerFactory;
26+
import org.springframework.data.elasticsearch.TestUtils;
2627
import org.springframework.data.util.Version;
2728

2829
/**

src/test/java/org/springframework/data/elasticsearch/TestNodeResource.java renamed to src/test/java/org/springframework/data/elasticsearch/junit/junit4/TestNodeResource.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.data.elasticsearch;
16+
package org.springframework.data.elasticsearch.junit.junit4;
1717

1818
import java.io.IOException;
1919

2020
import org.elasticsearch.client.Client;
2121
import org.elasticsearch.node.Node;
2222
import org.junit.rules.ExternalResource;
23+
import org.springframework.data.elasticsearch.Utils;
2324
import org.springframework.util.Assert;
2425

2526
/**
@@ -29,7 +30,7 @@
2930
*/
3031
public class TestNodeResource extends ExternalResource {
3132

32-
private static Node node;
33+
private Node node;
3334

3435
@Override
3536
protected void before() throws Throwable {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* interfaces, annotations and classes related to JUnit 4 test handling.
3+
*/
4+
@org.springframework.lang.NonNullApi
5+
package org.springframework.data.elasticsearch.junit.junit4;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright 2019 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.junit.jupiter;
17+
18+
import java.io.IOException;
19+
import java.net.URL;
20+
21+
import org.elasticsearch.node.Node;
22+
import org.elasticsearch.node.NodeValidationException;
23+
import org.junit.jupiter.api.extension.ExtensionContext;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
26+
import org.springframework.data.elasticsearch.Utils;
27+
import org.springframework.lang.Nullable;
28+
import org.springframework.util.StringUtils;
29+
30+
/**
31+
* This class manages the connection to an Elasticsearch Cluster, starting a local one if necessary.
32+
*
33+
* @author Peter-Josef Meisch
34+
*/
35+
class ClusterConnection implements ExtensionContext.Store.CloseableResource {
36+
private static final Logger LOGGER = LoggerFactory.getLogger(ClusterConnection.class);
37+
38+
@Nullable private ClusterConnectionInfo clusterConnectionInfo;
39+
private Node node;
40+
41+
/**
42+
* creates the ClusterConnection, starting a local node if necessary.
43+
*
44+
* @param clusterUrl if null or empty a local cluster is tarted
45+
*/
46+
public ClusterConnection(@Nullable String clusterUrl) {
47+
clusterConnectionInfo = StringUtils.isEmpty(clusterUrl) ? startLocalNode() : parseUrl(clusterUrl);
48+
49+
if (clusterConnectionInfo != null) {
50+
LOGGER.debug(clusterConnectionInfo.toString());
51+
} else {
52+
LOGGER.error("could not create ClusterConnectionInfo");
53+
}
54+
}
55+
56+
@Nullable
57+
public ClusterConnectionInfo getClusterConnectionInfo() {
58+
return clusterConnectionInfo;
59+
}
60+
61+
@Nullable
62+
private ClusterConnectionInfo parseUrl(String clusterUrl) {
63+
try {
64+
URL url = new URL(clusterUrl);
65+
66+
if (!url.getProtocol().startsWith("http") || url.getPort() <= 0) {
67+
throw new IllegalArgumentException("invalid url " + clusterUrl);
68+
}
69+
70+
return ClusterConnectionInfo.builder() //
71+
.withHostAndPort(url.getHost(), url.getPort()) //
72+
.useSsl(url.getProtocol().equals("https")) //
73+
.build();
74+
} catch (Exception e) {
75+
LOGGER.warn("could not parse URL {}", clusterUrl, e);
76+
}
77+
78+
return null;
79+
}
80+
81+
private @Nullable ClusterConnectionInfo startLocalNode() {
82+
LOGGER.debug("starting local node");
83+
84+
try {
85+
node = Utils.getNode();
86+
node.start();
87+
return ClusterConnectionInfo.builder() //
88+
.withHostAndPort("localhost", 9200) //
89+
.withClient(node.client()) //
90+
.build();
91+
} catch (NodeValidationException e) {
92+
LOGGER.error("could not start local node", e);
93+
}
94+
95+
return null;
96+
}
97+
98+
@Override
99+
public void close() throws Exception {
100+
101+
if (node != null) {
102+
LOGGER.debug("closing node");
103+
try {
104+
node.close();
105+
} catch (IOException ignored) {}
106+
}
107+
LOGGER.debug("closed");
108+
}
109+
}

0 commit comments

Comments
 (0)