Skip to content

Commit 58920af

Browse files
authored
DATAES-654 - Add Junit5 support.
Original PR: #329
1 parent 4682636 commit 58920af

25 files changed

+716
-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,125 @@
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.MalformedURLException;
20+
import java.net.URL;
21+
22+
import org.elasticsearch.node.Node;
23+
import org.elasticsearch.node.NodeValidationException;
24+
import org.junit.jupiter.api.extension.ExtensionContext;
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
27+
import org.springframework.data.elasticsearch.Utils;
28+
import org.springframework.lang.Nullable;
29+
import org.springframework.util.StringUtils;
30+
31+
/**
32+
* This class manages the connection to an Elasticsearch Cluster, starting a local one if necessary. The information
33+
* about the ClusterConnection is stored botha s a varaible in the instance for direct aaces from JUnit 5 and in a
34+
* static ThreadLocal<ClusterConnectionInfo> acessible with the {@link ClusterConnection#clusterConnectionInfo()} method
35+
* to be integrated in the Spring setup
36+
*
37+
* @author Peter-Josef Meisch
38+
*/
39+
class ClusterConnection implements ExtensionContext.Store.CloseableResource {
40+
private static final Logger LOGGER = LoggerFactory.getLogger(ClusterConnection.class);
41+
42+
private static final ThreadLocal<ClusterConnectionInfo> clusterConnectionInfoThreadLocal = new ThreadLocal<>();
43+
44+
private Node node;
45+
private final ClusterConnectionInfo clusterConnectionInfo;
46+
47+
/**
48+
* creates the ClusterConnection, starting a local node if necessary.
49+
*
50+
* @param clusterUrl if null or empty a local cluster is tarted
51+
*/
52+
public ClusterConnection(@Nullable String clusterUrl) {
53+
clusterConnectionInfo = StringUtils.isEmpty(clusterUrl) ? startLocalNode() : parseUrl(clusterUrl);
54+
55+
if (clusterConnectionInfo != null) {
56+
LOGGER.debug(clusterConnectionInfo.toString());
57+
clusterConnectionInfoThreadLocal.set(clusterConnectionInfo);
58+
} else {
59+
LOGGER.error("could not create ClusterConnectionInfo");
60+
}
61+
}
62+
63+
/**
64+
* @return the {@link ClusterConnectionInfo} from the ThreadLocal storage.
65+
*/
66+
@Nullable
67+
public static ClusterConnectionInfo clusterConnectionInfo() {
68+
return clusterConnectionInfoThreadLocal.get();
69+
}
70+
71+
public ClusterConnectionInfo getClusterConnectionInfo() {
72+
return clusterConnectionInfo;
73+
}
74+
75+
/**
76+
* @param clusterUrl the URL to parse
77+
* @return the connection information
78+
*/
79+
private ClusterConnectionInfo parseUrl(String clusterUrl) {
80+
try {
81+
URL url = new URL(clusterUrl);
82+
83+
if (!url.getProtocol().startsWith("http") || url.getPort() <= 0) {
84+
throw new ClusterConnectionException("invalid url " + clusterUrl);
85+
}
86+
87+
return ClusterConnectionInfo.builder() //
88+
.withHostAndPort(url.getHost(), url.getPort()) //
89+
.useSsl(url.getProtocol().equals("https")) //
90+
.build();
91+
} catch (MalformedURLException e) {
92+
throw new ClusterConnectionException(e);
93+
}
94+
95+
}
96+
97+
private @Nullable ClusterConnectionInfo startLocalNode() {
98+
LOGGER.debug("starting local node");
99+
100+
try {
101+
node = Utils.getNode();
102+
node.start();
103+
return ClusterConnectionInfo.builder() //
104+
.withHostAndPort("localhost", 9200) //
105+
.withClient(node.client()) //
106+
.build();
107+
} catch (NodeValidationException e) {
108+
LOGGER.error("could not start local node", e);
109+
}
110+
111+
return null;
112+
}
113+
114+
@Override
115+
public void close() throws Exception {
116+
117+
if (node != null) {
118+
LOGGER.debug("closing node");
119+
try {
120+
node.close();
121+
} catch (IOException ignored) {}
122+
}
123+
LOGGER.debug("closed");
124+
}
125+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
/**
19+
* @author Peter-Josef Meisch
20+
*/
21+
public class ClusterConnectionException extends RuntimeException {
22+
public ClusterConnectionException(String message) {
23+
super(message);
24+
}
25+
26+
public ClusterConnectionException(Throwable cause) {
27+
super(cause);
28+
}
29+
}

0 commit comments

Comments
 (0)