Skip to content

Add data-elasticsearch smoke test #31

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions data/data-elasticsearch/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Tests if Data Elasticsearch with REST client is working.
22 changes: 22 additions & 0 deletions data/data-elasticsearch/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
plugins {
id "java"
id "org.springframework.boot"
id "org.springframework.cr.smoke-test"
}

dependencies {
implementation(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES))
implementation("org.springframework.boot:spring-boot-starter-data-elasticsearch")

implementation("org.crac:crac:$cracVersion")
implementation(project(":cr-listener"))

testImplementation("org.springframework.boot:spring-boot-starter-test")

appTestImplementation(project(":cr-smoke-test-support"))
appTestImplementation("org.awaitility:awaitility:4.2.0")
}

crSmokeTest {
webApplication = false
}
11 changes: 11 additions & 0 deletions data/data-elasticsearch/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '3.1'
services:
elasticsearch:
image: 'elasticsearch:8.8.0'
container_name: 'elasticsearch'
ports:
- '9200'
- '9300'
environment:
- discovery.type=single-node
- xpack.security.enabled=false
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2023 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 com.example.data.elasticsearch;

import static org.assertj.core.api.Assertions.*;

import java.time.Duration;

import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;
import org.springframework.cr.smoketest.support.assertj.AssertableOutput;
import org.springframework.cr.smoketest.support.junit.ApplicationTest;

@ApplicationTest
public class DataElasticsearchApplicationTests {

@Test
void connectionTest(AssertableOutput output) {
Awaitility.await().atMost(Duration.ofSeconds(10)).untilAsserted(() -> {
assertThat(output).hasLineMatching("Starting ElasticsearchReader: was (initialized|stopped)");
assertThat(output).hasLineContaining("You Know, for Search");
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2023 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 com.example.data.elasticsearch;

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;

@Document(indexName = "smoke-test-index")
class ApplicationState {

@Id
private String applicationId;

private String state;

public ApplicationState() {
}

public ApplicationState(String id, String state) {
this.applicationId = id;
this.state = state;
}

public String getApplicationId() {
return applicationId;
}

public void setApplicationId(String applicationId) {
this.applicationId = applicationId;
}

public String getState() {
return state;
}

public void setName(String state) {
this.state = state;
}

ApplicationState newState(String state) {
return new ApplicationState(this.applicationId, state);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2023 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 com.example.data.elasticsearch;

import org.springframework.data.repository.CrudRepository;

interface ApplicationStateRepository extends CrudRepository<ApplicationState, String> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2023 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 com.example.data.elasticsearch;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DataElasticsearchApplication {

public static void main(String[] args) {
SpringApplication.run(DataElasticsearchApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2023 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 com.example.data.elasticsearch;

import java.util.concurrent.atomic.AtomicBoolean;

import org.springframework.context.SmartLifecycle;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ClassUtils;

@Component
@EnableScheduling
public class ElasticsearchReader implements SmartLifecycle {

private final ApplicationStateRepository applicationStateRepository;

private final ElasticsearchTemplate elasticsearchOperations;

final AtomicBoolean running = new AtomicBoolean(false);

public ElasticsearchReader(ApplicationStateRepository applicationStateRepository,
ElasticsearchTemplate elasticsearchOperations) {

this.applicationStateRepository = applicationStateRepository;
this.applicationStateRepository
.save(new ApplicationState(ClassUtils.getShortName(ElasticsearchReader.class), "initialized"));
this.elasticsearchOperations = elasticsearchOperations;
}

@Override
public void start() {
if (running.compareAndSet(false, true)) {
ApplicationState previousState = getSetState("started");
System.out.println("Starting ElasticsearchReader: was %s".formatted(previousState.getState()));
}
}

@Scheduled(fixedDelay = 1000)
public void scheduled() {

if (isRunning()) {
String tagline = elasticsearchOperations.execute(client -> client.info().tagline());
System.out.println(tagline);
}
}

@Override
public void stop() {
if (running.compareAndSet(true, false)) {
ApplicationState previousState = getSetState("stopped");
System.out.println("Stopping ElasticsearchReader: was %s".formatted(previousState.getState()));
}
}

@Override
public boolean isRunning() {
return running.get();
}

ApplicationState getSetState(String newState) {

ApplicationState current = applicationStateRepository
.findById(ClassUtils.getShortName(ElasticsearchReader.class))
.orElseThrow(() -> new IllegalStateException("Expected ElasticsearchReader to be initialized"));
applicationStateRepository.save(current.newState(newState));
return current;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.elasticsearch.uris=http://${ELASTICSEARCH_HOST:localhost}:${ELASTICSEARCH_PORT_9200:9200}