Skip to content

Commit 9d354a4

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

24 files changed

+663
-23
lines changed

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,41 @@
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.junit.jupiter.api.extension.ExtendWith;
23+
import org.springframework.data.elasticsearch.junit.jupiter.ClusterConnectionInfo;
24+
import org.springframework.data.elasticsearch.junit.jupiter.IntegrationTest;
25+
import org.springframework.data.elasticsearch.junit.jupiter.SpringDataElasticsearchExtension;
26+
27+
/**
28+
* Testing the setup and parameter injection of the CusterConnectionInfo.
29+
*
30+
* @author Peter-Josef Meisch
31+
*/
32+
@IntegrationTest
33+
@DisplayName("a sample JUnit 5 test with a bare cluster connection")
34+
public class JUnit5ClusterConnectionTests {
35+
36+
@Test
37+
@DisplayName("should have the connection info injected")
38+
void shouldHaveTheConnectionInfoInjected(ClusterConnectionInfo clusterConnectionInfo) {
39+
assertThat(clusterConnectionInfo).isNotNull();
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.data.elasticsearch.core.ElasticsearchOperations;
24+
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
25+
import org.springframework.data.elasticsearch.junit.jupiter.ElasticsearchRestTemplateConfiguration;
26+
import org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest;
27+
import org.springframework.test.context.ContextConfiguration;
28+
29+
/**
30+
* class demonstrating the setup of a JUnit 5 test in Spring Data Elasticsearch that uses the rest client. The
31+
* ContextConfiguration must include the {@link ElasticsearchRestTemplateConfiguration} class.
32+
*
33+
* @author Peter-Josef Meisch
34+
*/
35+
@SpringIntegrationTest
36+
@ContextConfiguration(classes = { ElasticsearchRestTemplateConfiguration.class})
37+
@DisplayName("a sample JUnit 5 test with rest client")
38+
public class JUnit5SampleRestClientBasedTests {
39+
40+
@Autowired private ElasticsearchOperations elasticsearchOperations;
41+
42+
@Test
43+
@DisplayName("should have a ElasticsearchRestTemplate")
44+
void shouldHaveARestTemplate() {
45+
assertThat(elasticsearchOperations).isNotNull().isInstanceOf(ElasticsearchRestTemplate.class);
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.data.elasticsearch.core.ElasticsearchOperations;
24+
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
25+
import org.springframework.data.elasticsearch.junit.jupiter.ElasticsearchTemplateConfiguration;
26+
import org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest;
27+
import org.springframework.test.context.ContextConfiguration;
28+
29+
/**
30+
* class demonstrating the setup of a JUnit 5 test in Spring Data Elasticsearch that uses the transport client. The
31+
* ContextConfiguration must include the {@link ElasticsearchTemplateConfiguration} class
32+
*
33+
* @author Peter-Josef Meisch
34+
*/
35+
@SpringIntegrationTest
36+
@ContextConfiguration(classes = { ElasticsearchTemplateConfiguration.class })
37+
@DisplayName("a sample JUnit 5 test with transport client")
38+
public class JUnit5SampleTransportClientBasedTests {
39+
40+
@Autowired private ElasticsearchOperations elasticsearchOperations;
41+
42+
@Test
43+
@DisplayName("should have a ElasticsearchTemplate")
44+
void shouldHaveATemplate() {
45+
assertThat(elasticsearchOperations).isNotNull().isInstanceOf(ElasticsearchTemplate.class);
46+
}
47+
}

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,113 @@
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. The information
32+
* about the ClusterConnection is stored in a static ThreadLocal<ClusterConnectionInfo> and can be accessed with the
33+
* {@link ClusterConnection#getClusterConnectionInfo()} method.
34+
*
35+
* @author Peter-Josef Meisch
36+
*/
37+
class ClusterConnection implements ExtensionContext.Store.CloseableResource {
38+
private static final Logger LOGGER = LoggerFactory.getLogger(ClusterConnection.class);
39+
40+
private static final ThreadLocal<ClusterConnectionInfo> clusterConnectionInfo = new ThreadLocal<>();
41+
42+
private Node node;
43+
44+
/**
45+
* creates the ClusterConnection, starting a local node if necessary.
46+
*
47+
* @param clusterUrl if null or empty a local cluster is tarted
48+
*/
49+
public ClusterConnection(@Nullable String clusterUrl) {
50+
ClusterConnectionInfo cci = StringUtils.isEmpty(clusterUrl) ? startLocalNode() : parseUrl(clusterUrl);
51+
52+
if (cci != null) {
53+
LOGGER.debug(cci.toString());
54+
clusterConnectionInfo.set(cci);
55+
} else {
56+
LOGGER.error("could not create ClusterConnectionInfo");
57+
}
58+
}
59+
60+
@Nullable
61+
public static ClusterConnectionInfo getClusterConnectionInfo() {
62+
return clusterConnectionInfo.get();
63+
}
64+
65+
@Nullable
66+
private ClusterConnectionInfo parseUrl(String clusterUrl) {
67+
try {
68+
URL url = new URL(clusterUrl);
69+
70+
if (!url.getProtocol().startsWith("http") || url.getPort() <= 0) {
71+
throw new IllegalArgumentException("invalid url " + clusterUrl);
72+
}
73+
74+
return ClusterConnectionInfo.builder() //
75+
.withHostAndPort(url.getHost(), url.getPort()) //
76+
.useSsl(url.getProtocol().equals("https")) //
77+
.build();
78+
} catch (Exception e) {
79+
LOGGER.warn("could not parse URL {}", clusterUrl, e);
80+
}
81+
82+
return null;
83+
}
84+
85+
private @Nullable ClusterConnectionInfo startLocalNode() {
86+
LOGGER.debug("starting local node");
87+
88+
try {
89+
node = Utils.getNode();
90+
node.start();
91+
return ClusterConnectionInfo.builder() //
92+
.withHostAndPort("localhost", 9200) //
93+
.withClient(node.client()) //
94+
.build();
95+
} catch (NodeValidationException e) {
96+
LOGGER.error("could not start local node", e);
97+
}
98+
99+
return null;
100+
}
101+
102+
@Override
103+
public void close() throws Exception {
104+
105+
if (node != null) {
106+
LOGGER.debug("closing node");
107+
try {
108+
node.close();
109+
} catch (IOException ignored) {}
110+
}
111+
LOGGER.debug("closed");
112+
}
113+
}

0 commit comments

Comments
 (0)