Skip to content

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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.springframework.data.elasticsearch.config.ElasticsearchConfigurationSupport;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
import org.springframework.data.elasticsearch.junit.junit4.TestNodeResource;

/**
* configuration class for the classic ElasticsearchTemplate. Needs a {@link TestNodeResource} bean that should be set up in
Expand Down
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();
}
}
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);
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.elasticsearch.ElasticsearchVersion;
import org.springframework.data.elasticsearch.ElasticsearchVersionRule;
import org.springframework.data.elasticsearch.junit.junit4.ElasticsearchVersion;
import org.springframework.data.elasticsearch.junit.junit4.ElasticsearchVersionRule;
import org.springframework.data.elasticsearch.TestUtils;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.http.HttpHeaders;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.ElasticsearchVersion;
import org.springframework.data.elasticsearch.ElasticsearchVersionRule;
import org.springframework.data.elasticsearch.junit.junit4.ElasticsearchVersion;
import org.springframework.data.elasticsearch.junit.junit4.ElasticsearchVersionRule;
import org.springframework.data.elasticsearch.TestUtils;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.elasticsearch;
package org.springframework.data.elasticsearch.junit.junit4;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.elasticsearch;
package org.springframework.data.elasticsearch.junit.junit4;

import java.util.concurrent.atomic.AtomicReference;

Expand All @@ -23,6 +23,7 @@
import org.junit.runners.model.Statement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.elasticsearch.TestUtils;
import org.springframework.data.util.Version;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.elasticsearch;
package org.springframework.data.elasticsearch.junit.junit4;

import java.io.IOException;

import org.elasticsearch.client.Client;
import org.elasticsearch.node.Node;
import org.junit.rules.ExternalResource;
import org.springframework.data.elasticsearch.Utils;
import org.springframework.util.Assert;

/**
Expand All @@ -29,7 +30,7 @@
*/
public class TestNodeResource extends ExternalResource {

private static Node node;
private Node node;

@Override
protected void before() throws Throwable {
Expand Down
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;
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 {
Copy link
Member

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.

Copy link
Collaborator Author

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

Copy link
Member

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.

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");
}
}
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);
}
}
Loading