-
Notifications
You must be signed in to change notification settings - Fork 1.3k
DATAES-654 - Add Junit5 support. #329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sothawo
merged 1 commit into
spring-projects:master
from
sothawo:DATAES-654_Add-JUnit5-support
Oct 20, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/test/java/org/springframework/data/elasticsearch/JUnit5ClusterConnectionTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* 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 | ||
* | ||
* https://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; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.data.elasticsearch.junit.jupiter.ClusterConnectionInfo; | ||
import org.springframework.data.elasticsearch.junit.jupiter.IntegrationTest; | ||
import org.springframework.data.elasticsearch.junit.jupiter.SpringDataElasticsearchExtension; | ||
|
||
/** | ||
* Testing the setup and parameter injection of the CusterConnectionInfo. | ||
* | ||
* @author Peter-Josef Meisch | ||
*/ | ||
@IntegrationTest | ||
@DisplayName("a sample JUnit 5 test with a bare cluster connection") | ||
public class JUnit5ClusterConnectionTests { | ||
|
||
@Test | ||
@DisplayName("should have the connection info injected") | ||
void shouldHaveTheConnectionInfoInjected(ClusterConnectionInfo clusterConnectionInfo) { | ||
assertThat(clusterConnectionInfo).isNotNull(); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/test/java/org/springframework/data/elasticsearch/JUnit5SampleRestClientBasedTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* 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 | ||
* | ||
* https://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; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.elasticsearch.core.ElasticsearchOperations; | ||
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; | ||
import org.springframework.data.elasticsearch.junit.jupiter.ElasticsearchRestTemplateConfiguration; | ||
import org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest; | ||
import org.springframework.test.context.ContextConfiguration; | ||
|
||
/** | ||
* class demonstrating the setup of a JUnit 5 test in Spring Data Elasticsearch that uses the rest client. The | ||
* ContextConfiguration must include the {@link ElasticsearchRestTemplateConfiguration} class. | ||
* | ||
* @author Peter-Josef Meisch | ||
*/ | ||
@SpringIntegrationTest | ||
@ContextConfiguration(classes = { ElasticsearchRestTemplateConfiguration.class}) | ||
@DisplayName("a sample JUnit 5 test with rest client") | ||
public class JUnit5SampleRestClientBasedTests { | ||
|
||
@Autowired private ElasticsearchOperations elasticsearchOperations; | ||
|
||
@Test | ||
@DisplayName("should have a ElasticsearchRestTemplate") | ||
void shouldHaveARestTemplate() { | ||
assertThat(elasticsearchOperations).isNotNull().isInstanceOf(ElasticsearchRestTemplate.class); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...st/java/org/springframework/data/elasticsearch/JUnit5SampleTransportClientBasedTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* 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 | ||
* | ||
* https://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; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.elasticsearch.core.ElasticsearchOperations; | ||
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; | ||
import org.springframework.data.elasticsearch.junit.jupiter.ElasticsearchTemplateConfiguration; | ||
import org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest; | ||
import org.springframework.test.context.ContextConfiguration; | ||
|
||
/** | ||
* class demonstrating the setup of a JUnit 5 test in Spring Data Elasticsearch that uses the transport client. The | ||
* ContextConfiguration must include the {@link ElasticsearchTemplateConfiguration} class | ||
* | ||
* @author Peter-Josef Meisch | ||
*/ | ||
@SpringIntegrationTest | ||
@ContextConfiguration(classes = { ElasticsearchTemplateConfiguration.class }) | ||
@DisplayName("a sample JUnit 5 test with transport client") | ||
public class JUnit5SampleTransportClientBasedTests { | ||
|
||
@Autowired private ElasticsearchOperations elasticsearchOperations; | ||
|
||
@Test | ||
@DisplayName("should have a ElasticsearchTemplate") | ||
void shouldHaveATemplate() { | ||
assertThat(elasticsearchOperations).isNotNull().isInstanceOf(ElasticsearchTemplate.class); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
src/test/java/org/springframework/data/elasticsearch/junit/junit4/package-info.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** | ||
* interfaces, annotations and classes related to JUnit 4 test handling. | ||
*/ | ||
@org.springframework.lang.NonNullApi | ||
package org.springframework.data.elasticsearch.junit.junit4; |
125 changes: 125 additions & 0 deletions
125
src/test/java/org/springframework/data/elasticsearch/junit/jupiter/ClusterConnection.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* 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 | ||
* | ||
* https://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.junit.jupiter; | ||
|
||
import java.io.IOException; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
|
||
import org.elasticsearch.node.Node; | ||
import org.elasticsearch.node.NodeValidationException; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.data.elasticsearch.Utils; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.util.StringUtils; | ||
|
||
/** | ||
* This class manages the connection to an Elasticsearch Cluster, starting a local one if necessary. The information | ||
* about the ClusterConnection is stored botha s a varaible in the instance for direct aaces from JUnit 5 and in a | ||
* static ThreadLocal<ClusterConnectionInfo> acessible with the {@link ClusterConnection#clusterConnectionInfo()} method | ||
* to be integrated in the Spring setup | ||
* | ||
* @author Peter-Josef Meisch | ||
*/ | ||
class ClusterConnection implements ExtensionContext.Store.CloseableResource { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(ClusterConnection.class); | ||
|
||
private static final ThreadLocal<ClusterConnectionInfo> clusterConnectionInfoThreadLocal = new ThreadLocal<>(); | ||
|
||
private Node node; | ||
private final ClusterConnectionInfo clusterConnectionInfo; | ||
|
||
/** | ||
* creates the ClusterConnection, starting a local node if necessary. | ||
* | ||
* @param clusterUrl if null or empty a local cluster is tarted | ||
*/ | ||
public ClusterConnection(@Nullable String clusterUrl) { | ||
clusterConnectionInfo = StringUtils.isEmpty(clusterUrl) ? startLocalNode() : parseUrl(clusterUrl); | ||
|
||
if (clusterConnectionInfo != null) { | ||
LOGGER.debug(clusterConnectionInfo.toString()); | ||
clusterConnectionInfoThreadLocal.set(clusterConnectionInfo); | ||
} else { | ||
LOGGER.error("could not create ClusterConnectionInfo"); | ||
} | ||
} | ||
|
||
/** | ||
* @return the {@link ClusterConnectionInfo} from the ThreadLocal storage. | ||
*/ | ||
@Nullable | ||
public static ClusterConnectionInfo clusterConnectionInfo() { | ||
return clusterConnectionInfoThreadLocal.get(); | ||
} | ||
|
||
public ClusterConnectionInfo getClusterConnectionInfo() { | ||
return clusterConnectionInfo; | ||
} | ||
|
||
/** | ||
* @param clusterUrl the URL to parse | ||
* @return the connection information | ||
*/ | ||
private ClusterConnectionInfo parseUrl(String clusterUrl) { | ||
try { | ||
URL url = new URL(clusterUrl); | ||
|
||
if (!url.getProtocol().startsWith("http") || url.getPort() <= 0) { | ||
throw new ClusterConnectionException("invalid url " + clusterUrl); | ||
} | ||
|
||
return ClusterConnectionInfo.builder() // | ||
.withHostAndPort(url.getHost(), url.getPort()) // | ||
.useSsl(url.getProtocol().equals("https")) // | ||
.build(); | ||
} catch (MalformedURLException e) { | ||
throw new ClusterConnectionException(e); | ||
} | ||
|
||
} | ||
|
||
private @Nullable ClusterConnectionInfo startLocalNode() { | ||
LOGGER.debug("starting local node"); | ||
|
||
try { | ||
node = Utils.getNode(); | ||
node.start(); | ||
return ClusterConnectionInfo.builder() // | ||
.withHostAndPort("localhost", 9200) // | ||
.withClient(node.client()) // | ||
.build(); | ||
} catch (NodeValidationException e) { | ||
LOGGER.error("could not start local node", e); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
|
||
if (node != null) { | ||
LOGGER.debug("closing node"); | ||
try { | ||
node.close(); | ||
} catch (IOException ignored) {} | ||
} | ||
LOGGER.debug("closed"); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...java/org/springframework/data/elasticsearch/junit/jupiter/ClusterConnectionException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* 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 | ||
* | ||
* https://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.junit.jupiter; | ||
|
||
/** | ||
* @author Peter-Josef Meisch | ||
*/ | ||
public class ClusterConnectionException extends RuntimeException { | ||
public ClusterConnectionException(String message) { | ||
super(message); | ||
} | ||
|
||
public ClusterConnectionException(Throwable cause) { | ||
super(cause); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would make sense to store both information, the endpoint and the handle to a cluster runner (Node client, Test containers) in the Jupiter context for separation of concerns.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ClusterConnection is stored in the ExtensionContext. Or do you mean having separate objects for the different connection types? I think, we will remove the client part when removing transport client stuff, so we then will only need the host/port/ssl part. Even when adding support for TestContainers (optional test depenedency?), the connection to the ES in a TestContainer will be defined by the URL
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment below in the actual extension.