Polishing.
Rename AssumeConnection to EnabledOnElasticsearch following the typical design of JUnit 5 EnabledOn… annotation programming model. Enable tests by default if the test element isn't annotated with EnabledOnElasticsearch as we assume that the extension was activated by the parent. Move EnabledOnElasticsearch into utility project. See #583 Original pull request: #609
This commit is contained in:
@@ -3,7 +3,6 @@
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-data-elasticsearch-reactive-example</artifactId>
|
||||
|
||||
<name>Spring Data Elasticsearch - Reactive Example</name>
|
||||
@@ -18,6 +17,11 @@
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.data.examples</groupId>
|
||||
<artifactId>spring-data-elasticsearch-example-utils</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.projectreactor</groupId>
|
||||
<artifactId>reactor-test</artifactId>
|
||||
|
||||
@@ -15,16 +15,16 @@
|
||||
*/
|
||||
package example.springdata.elasticsearch.conference;
|
||||
|
||||
import example.springdata.elasticsearch.util.AssumeConnection;
|
||||
import reactor.test.StepVerifier;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import example.springdata.elasticsearch.util.EnabledOnElasticsearch;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
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;
|
||||
@@ -38,9 +38,9 @@ import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
|
||||
* @author Christoph Strobl
|
||||
* @author Prakhar Gupta
|
||||
*/
|
||||
@AssumeConnection
|
||||
@EnabledOnElasticsearch
|
||||
@SpringBootTest(classes = ApplicationConfiguration.class)
|
||||
public class ReactiveElasticsearchOperationsTest {
|
||||
class ReactiveElasticsearchOperationsTest {
|
||||
|
||||
private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
||||
|
||||
@@ -48,8 +48,7 @@ public class ReactiveElasticsearchOperationsTest {
|
||||
ReactiveElasticsearchOperations operations;
|
||||
|
||||
@Test
|
||||
@AssumeConnection
|
||||
public void textSearch() {
|
||||
void textSearch() {
|
||||
|
||||
String expectedDate = "2014-10-29";
|
||||
String expectedWord = "java";
|
||||
|
||||
@@ -15,16 +15,16 @@
|
||||
*/
|
||||
package example.springdata.elasticsearch.conference;
|
||||
|
||||
import example.springdata.elasticsearch.util.AssumeConnection;
|
||||
import reactor.test.StepVerifier;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
import example.springdata.elasticsearch.util.EnabledOnElasticsearch;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@@ -34,9 +34,9 @@ import org.springframework.boot.test.context.SpringBootTest;
|
||||
* @author Christoph Strobl
|
||||
* @author Prakhar Gupta
|
||||
*/
|
||||
@AssumeConnection
|
||||
@EnabledOnElasticsearch
|
||||
@SpringBootTest(classes = ApplicationConfiguration.class)
|
||||
public class ReactiveElasticsearchRepositoryTest {
|
||||
class ReactiveElasticsearchRepositoryTest {
|
||||
|
||||
private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
|
||||
|
||||
@@ -44,8 +44,7 @@ public class ReactiveElasticsearchRepositoryTest {
|
||||
ConferenceRepository repository;
|
||||
|
||||
@Test
|
||||
@AssumeConnection
|
||||
public void textSearch() {
|
||||
void textSearch() {
|
||||
|
||||
String expectedDate = "2014-10-29";
|
||||
String expectedWord = "java";
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
/*
|
||||
* 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";
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
* 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 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.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 ExecutionCondition {
|
||||
|
||||
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) {
|
||||
isConnectionAvailable = true;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
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!");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user