Migrate Elasticsearch examples to JUnit 5.

See #583
Original pull request: #609
This commit is contained in:
Prakhar Gupta
2021-03-23 21:15:35 +05:30
committed by Mark Paluch
parent 60385ee4b4
commit 75b2a08d60
8 changed files with 172 additions and 117 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2020 the original author or authors.
* Copyright 2021-2021 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.
@@ -15,14 +15,12 @@
*/
package example.springdata.elasticsearch.conference;
import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
@@ -32,7 +30,6 @@ import org.springframework.data.elasticsearch.core.geo.GeoPoint;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Test case to show Spring Data Elasticsearch functionality.
@@ -40,14 +37,15 @@ import org.springframework.test.context.junit4.SpringRunner;
* @author Artur Konczak
* @author Oliver Gierke
* @author Christoph Strobl
* @author Prakhar Gupta
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ApplicationConfiguration.class)
public class ElasticsearchOperationsTest {
private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
@Autowired ElasticsearchOperations operations;
@Autowired
ElasticsearchOperations operations;
@Test
public void textSearch() throws ParseException {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2020 the original author or authors.
* Copyright 2020-2021 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.
@@ -15,42 +15,40 @@
*/
package example.springdata.elasticsearch.conference;
import static org.assertj.core.api.Assertions.*;
import example.springdata.elasticsearch.util.ElasticsearchAvailable;
import example.springdata.elasticsearch.util.AssumeConnection;
import reactor.test.StepVerifier;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchOperations;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Test case to show Spring Data Elasticsearch functionality.
*
* @author Christoph Strobl
* @author Prakhar Gupta
*/
@RunWith(SpringRunner.class)
@AssumeConnection
@SpringBootTest(classes = ApplicationConfiguration.class)
public class ReactiveElasticsearchOperationsTest {
public static @ClassRule ElasticsearchAvailable elasticsearchAvailable = ElasticsearchAvailable.onLocalhost();
private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
@Autowired ReactiveElasticsearchOperations operations;
@Autowired
ReactiveElasticsearchOperations operations;
@Test
@AssumeConnection
public void textSearch() {
String expectedDate = "2014-10-29";
@@ -66,7 +64,7 @@ public class ReactiveElasticsearchOperationsTest {
.verifyComplete();
}
void verify(SearchHit<Conference> hit, String expectedWord, String expectedDate) {
private void verify(SearchHit<Conference> hit, String expectedWord, String expectedDate) {
assertThat(hit.getContent().getKeywords()).contains(expectedWord);
try {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2019 the original author or authors.
* Copyright 2019-2021 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.
@@ -15,38 +15,36 @@
*/
package example.springdata.elasticsearch.conference;
import static org.assertj.core.api.Assertions.*;
import example.springdata.elasticsearch.util.ElasticsearchAvailable;
import example.springdata.elasticsearch.util.AssumeConnection;
import reactor.test.StepVerifier;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Test case to show reactive Spring Data Elasticsearch repository functionality.
*
* @author Christoph Strobl
* @author Prakhar Gupta
*/
@RunWith(SpringRunner.class)
@AssumeConnection
@SpringBootTest(classes = ApplicationConfiguration.class)
public class ReactiveElasticsearchRepositoryTest {
public static @ClassRule ElasticsearchAvailable elasticsearchAvailable = ElasticsearchAvailable.onLocalhost();
private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
@Autowired ConferenceRepository repository;
@Autowired
ConferenceRepository repository;
@Test
@AssumeConnection
public void textSearch() {
String expectedDate = "2014-10-29";
@@ -60,7 +58,7 @@ public class ReactiveElasticsearchRepositoryTest {
.verifyComplete();
}
void verify(Conference it, String expectedWord, String expectedDate) {
private void verify(Conference it, String expectedWord, String expectedDate) {
assertThat(it.getKeywords()).contains(expectedWord);
try {

View File

@@ -0,0 +1,33 @@
/*
* Copyright 2021 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 example.springdata.elasticsearch.util;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.extension.ExtendWith;
/**
* @author Prakhar Gupta
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@ExtendWith(ElasticsearchAvailable.class)
public @interface AssumeConnection {
String url() default "http://localhost:9200";
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2019 the original author or authors.
* Copyright 2019-2021 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.
@@ -15,56 +15,55 @@
*/
package example.springdata.elasticsearch.util;
import static org.junit.platform.commons.support.AnnotationSupport.findAnnotation;
import java.io.IOException;
import java.util.Optional;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.assertj.core.api.Assumptions;
import org.junit.AssumptionViolatedException;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;
/**
* @author Christoph Strobl
* @author Prakhar Gupta
*/
public class ElasticsearchAvailable implements TestRule {
public class ElasticsearchAvailable implements ExecutionCondition {
private final String url;
private ElasticsearchAvailable(String url) {
this.url = url;
}
public static ElasticsearchAvailable onLocalhost() {
return new ElasticsearchAvailable("http://localhost:9200");
}
@Override
public Statement apply(Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
checkServerRunning();
base.evaluate();
}
};
}
private void checkServerRunning() {
private boolean checkServerRunning(String url) {
boolean isConnectionAvailable = false;
try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
CloseableHttpResponse response = client.execute(new HttpHead(url));
if (response != null && response.getStatusLine() != null) {
Assumptions.assumeThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
isConnectionAvailable = true;
}
} catch (IOException e) {
throw new AssumptionViolatedException(String.format("Elasticsearch Server seems to be down. %s", e.getMessage()));
return isConnectionAvailable;
}
return isConnectionAvailable;
}
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext extensionContext) {
Optional<AssumeConnection> annotation = findAnnotation(extensionContext.getElement(), AssumeConnection.class);
if (annotation.isPresent()) {
String url = annotation.get().url();
if (checkServerRunning(url)) {
return ConditionEvaluationResult.enabled("Successfully connected to Elasticsearch server. Continuing test!");
} else {
return ConditionEvaluationResult.disabled("Elasticsearch Server seems to be down. Skipping test!");
}
}
return ConditionEvaluationResult.disabled("No connection specified. Skipping test!");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2019 the original author or authors.
* Copyright 2019-2021 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.
@@ -15,17 +15,14 @@
*/
package example.springdata.elasticsearch.conference;
import static org.assertj.core.api.Assertions.*;
import example.springdata.elasticsearch.util.AssumeConnection;
import example.springdata.elasticsearch.util.ElasticsearchAvailable;
import static org.assertj.core.api.Assertions.assertThat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
@@ -34,26 +31,25 @@ import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Test case to show Spring Data Elasticsearch functionality.
*
* @author Christoph Strobl
* @author Prakhar Gupta
*/
@RunWith(SpringRunner.class)
@AssumeConnection
@SpringBootTest(classes = ApplicationConfiguration.class)
public class ElasticsearchOperationsTest {
public static @ClassRule ElasticsearchAvailable elasticsearchAvailable = ElasticsearchAvailable.onLocalhost();
private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
@Autowired ElasticsearchOperations operations;
@Autowired
ElasticsearchOperations operations;
@Test
@AssumeConnection
public void textSearch() throws ParseException {
String expectedDate = "2014-10-29";
String expectedWord = "java";
CriteriaQuery query = new CriteriaQuery(

View File

@@ -0,0 +1,33 @@
/*
* Copyright 2021 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 example.springdata.elasticsearch.util;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.extension.ExtendWith;
/**
* @author Prakhar Gupta
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@ExtendWith(ElasticsearchAvailable.class)
public @interface AssumeConnection {
String url() default "http://localhost:9200";
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2019 the original author or authors.
* Copyright 2019-2021 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.
@@ -15,56 +15,56 @@
*/
package example.springdata.elasticsearch.util;
import static org.junit.platform.commons.support.AnnotationSupport.findAnnotation;
import java.io.IOException;
import java.util.Optional;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.assertj.core.api.Assumptions;
import org.junit.AssumptionViolatedException;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;
/**
* @author Christoph Strobl
* @author Prakhar Gupta
*/
public class ElasticsearchAvailable implements TestRule {
public class ElasticsearchAvailable implements ExecutionCondition {
private final String url;
private ElasticsearchAvailable(String url) {
this.url = url;
}
public static ElasticsearchAvailable onLocalhost() {
return new ElasticsearchAvailable("http://localhost:9200");
}
@Override
public Statement apply(Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
checkServerRunning();
base.evaluate();
}
};
}
private void checkServerRunning() {
private boolean checkServerRunning(String url) {
boolean isConnectionAvailable = false;
try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
CloseableHttpResponse response = client.execute(new HttpHead(url));
if (response != null && response.getStatusLine() != null) {
Assumptions.assumeThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
isConnectionAvailable = true;
}
} catch (IOException e) {
throw new AssumptionViolatedException(String.format("Elasticsearch Server seems to be down. %s", e.getMessage()));
return isConnectionAvailable;
}
return isConnectionAvailable;
}
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext extensionContext) {
Optional<AssumeConnection> annotation = findAnnotation(extensionContext.getElement(), AssumeConnection.class);
if (annotation.isPresent()) {
String url = annotation.get().url();
if (checkServerRunning(url)) {
return ConditionEvaluationResult
.enabled("Successfully connected to Elasticsearch server. Continuing test!");
} else {
return ConditionEvaluationResult.disabled("Elasticsearch Server seems to be down. Skipping test!");
}
}
return ConditionEvaluationResult.disabled("No connection specified. Skipping test!");
}
}