Skip to content
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

Apply spotless and checkstyle to examples #6061

Merged
merged 1 commit into from Oct 25, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 1 addition & 23 deletions build.gradle
Expand Up @@ -32,7 +32,7 @@ subprojects {
apply plugin: 'idea'
apply plugin: 'io.franzbecker.gradle-lombok'
apply from: "$rootDir/gradle/shading.gradle"
apply plugin: 'com.diffplug.spotless'
apply from: "$rootDir/gradle/spotless.gradle"
apply plugin: 'checkstyle'
apply plugin: 'org.gradle.test-retry'

Expand Down Expand Up @@ -128,28 +128,6 @@ subprojects {
testImplementation 'ch.qos.logback:logback-classic:1.3.3'
}

spotless {
java {
toggleOffOn()
removeUnusedImports()
trimTrailingWhitespace()
endWithNewline()

prettier(['prettier': '2.5.1', 'prettier-plugin-java': '1.6.1'])
.config([
'parser' : 'java',
'tabWidth' : 4,
'printWidth': 120
])

importOrder('', 'java', 'javax', '\\#')
}
groovyGradle {
target '**/*.groovy'
greclipse('4.19.0')
}
}

checkstyle {
toolVersion = "9.3"
configFile = rootProject.file('config/checkstyle/checkstyle.xml')
Expand Down
11 changes: 11 additions & 0 deletions examples/build.gradle
@@ -1,10 +1,21 @@
// empty build.gradle for dependabot
plugins {
id 'com.diffplug.spotless' version '6.8.0' apply false
}

apply from: "$rootDir/../gradle/ci-support.gradle"

subprojects {
apply plugin:"java"
apply from: "$rootDir/../gradle/spotless.gradle"
apply plugin: 'checkstyle'

repositories {
mavenCentral()
}

checkstyle {
toolVersion = "9.3"
configFile = rootProject.file('../config/checkstyle/checkstyle.xml')
}
}
1 change: 1 addition & 0 deletions examples/cucumber/build.gradle
Expand Up @@ -13,4 +13,5 @@ dependencies {
testImplementation 'io.cucumber:cucumber-java:7.8.0'
testImplementation 'io.cucumber:cucumber-junit:7.8.0'
testImplementation 'org.testcontainers:selenium'
testImplementation 'org.assertj:assertj-core:3.23.1'
}
Expand Up @@ -5,8 +5,5 @@
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(plugin = {"pretty"})
public class CucumberTest {


}
@CucumberOptions(plugin = { "pretty" })
public class CucumberTest {}
Expand Up @@ -10,22 +10,23 @@
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testcontainers.containers.BrowserWebDriverContainer;
import org.testcontainers.containers.BrowserWebDriverContainer.VncRecordingMode;
import org.testcontainers.lifecycle.TestDescription;

import java.io.File;
import java.util.List;
import java.util.Optional;

import static junit.framework.TestCase.assertEquals;
import static org.testcontainers.containers.BrowserWebDriverContainer.VncRecordingMode.RECORD_ALL;
import static org.assertj.core.api.Assertions.assertThat;

public class Stepdefs {

private BrowserWebDriverContainer container = new BrowserWebDriverContainer()
.withCapabilities(new ChromeOptions())
.withRecordingMode(RECORD_ALL, new File("build"));
.withCapabilities(new ChromeOptions())
.withRecordingMode(VncRecordingMode.RECORD_ALL, new File("build"));

private String location;

private String answer;

@Before
Expand All @@ -35,17 +36,20 @@ public void beforeScenario() {

@After
public void afterScenario(Scenario scenario) {
container.afterTest(new TestDescription() {
@Override
public String getTestId() {
return scenario.getId();
}
container.afterTest(
new TestDescription() {
@Override
public String getTestId() {
return scenario.getId();
}

@Override
public String getFilesystemFriendlyName() {
return scenario.getName();
}
}, Optional.of(scenario).filter(Scenario::isFailed).map(__ -> new RuntimeException()));
@Override
public String getFilesystemFriendlyName() {
return scenario.getName();
}
},
Optional.of(scenario).filter(Scenario::isFailed).map(__ -> new RuntimeException())
);
}

@Given("^location is \"([^\"]*)\"$")
Expand All @@ -63,8 +67,6 @@ public void iAskIsItPossibleToSearchHere() throws Exception {

@Then("^I should be told \"([^\"]*)\"$")
public void iShouldBeTold(String expected) throws Exception {
assertEquals(expected, answer);
assertThat(answer).isEqualTo(expected);
}


}
20 changes: 13 additions & 7 deletions examples/immudb/src/test/java/ImmuDbTest.java
Expand Up @@ -8,6 +8,7 @@
import org.junit.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;

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

/**
Expand All @@ -17,28 +18,33 @@ public class ImmuDbTest {

// Default port for the ImmuDb server
private static final int IMMUDB_PORT = 3322;

// Default username for the ImmuDb server
private final String IMMUDB_USER = "immudb";

// Default password for the ImmuDb server
private final String IMMUDB_PASSWORD = "immudb";

// Default database name for the ImmuDb server
private final String IMMUDB_DATABASE = "defaultdb";

// Test container for the ImmuDb database, with the latest version of the image and exposed port
@ClassRule
public static final GenericContainer<?> immuDbContainer = new GenericContainer<>("codenotary/immudb:1.3")
.withExposedPorts(IMMUDB_PORT).waitingFor(
Wait.forLogMessage(".*Web API server enabled.*", 1)
);
.withExposedPorts(IMMUDB_PORT)
.waitingFor(Wait.forLogMessage(".*Web API server enabled.*", 1));

// ImmuClient used to interact with the DB
private ImmuClient immuClient;

@Before
public void setUp() {
this.immuClient = ImmuClient.newBuilder()
.withServerUrl(immuDbContainer.getHost())
.withServerPort(immuDbContainer.getMappedPort(IMMUDB_PORT))
.build();
this.immuClient =
ImmuClient
.newBuilder()
.withServerUrl(immuDbContainer.getHost())
.withServerPort(immuDbContainer.getMappedPort(IMMUDB_PORT))
.build();
this.immuClient.login(IMMUDB_USER, IMMUDB_PASSWORD);
this.immuClient.useDatabase(IMMUDB_DATABASE);
}
Expand Down
Expand Up @@ -4,10 +4,9 @@
import org.rnorth.ducttape.unreliables.Unreliables;
import org.testcontainers.containers.Container;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.KafkaContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.lifecycle.Startable;
import org.testcontainers.lifecycle.Startables;
import org.testcontainers.containers.KafkaContainer;
import org.testcontainers.utility.DockerImageName;

import java.time.Duration;
Expand All @@ -17,67 +16,69 @@
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.util.concurrent.TimeUnit.SECONDS;

/**
* Provides an easy way to launch a Kafka cluster with multiple brokers.
*/
public class KafkaContainerCluster implements Startable {

private final int brokersNum;

private final Network network;

private final GenericContainer<?> zookeeper;

private final Collection<KafkaContainer> brokers;

public KafkaContainerCluster(String confluentPlatformVersion, int brokersNum, int internalTopicsRf) {
if (brokersNum < 0) {
throw new IllegalArgumentException("brokersNum '" + brokersNum + "' must be greater than 0");
}
if (internalTopicsRf < 0 || internalTopicsRf > brokersNum) {
throw new IllegalArgumentException("internalTopicsRf '" + internalTopicsRf + "' must be less than brokersNum and greater than 0");
throw new IllegalArgumentException(
"internalTopicsRf '" + internalTopicsRf + "' must be less than brokersNum and greater than 0"
);
}

this.brokersNum = brokersNum;
this.network = Network.newNetwork();

this.zookeeper = new GenericContainer<>(DockerImageName.parse("confluentinc/cp-zookeeper").withTag(confluentPlatformVersion))
.withNetwork(network)
.withNetworkAliases("zookeeper")
.withEnv("ZOOKEEPER_CLIENT_PORT", String.valueOf(KafkaContainer.ZOOKEEPER_PORT));

this.brokers = IntStream
.range(0, this.brokersNum)
.mapToObj(brokerNum -> {
return new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka").withTag(confluentPlatformVersion))
.withNetwork(this.network)
.withNetworkAliases("broker-" + brokerNum)
.dependsOn(this.zookeeper)
.withExternalZookeeper("zookeeper:" + KafkaContainer.ZOOKEEPER_PORT)
.withEnv("KAFKA_BROKER_ID", brokerNum + "")
.withEnv("KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR", internalTopicsRf + "")
.withEnv("KAFKA_OFFSETS_TOPIC_NUM_PARTITIONS", internalTopicsRf + "")
.withEnv("KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR", internalTopicsRf + "")
.withEnv("KAFKA_TRANSACTION_STATE_LOG_MIN_ISR", internalTopicsRf + "")
.withStartupTimeout(Duration.ofMinutes(1));
})
.collect(Collectors.toList());
this.zookeeper =
new GenericContainer<>(DockerImageName.parse("confluentinc/cp-zookeeper").withTag(confluentPlatformVersion))
.withNetwork(network)
.withNetworkAliases("zookeeper")
.withEnv("ZOOKEEPER_CLIENT_PORT", String.valueOf(KafkaContainer.ZOOKEEPER_PORT));

this.brokers =
IntStream
.range(0, this.brokersNum)
.mapToObj(brokerNum -> {
return new KafkaContainer(
DockerImageName.parse("confluentinc/cp-kafka").withTag(confluentPlatformVersion)
)
.withNetwork(this.network)
.withNetworkAliases("broker-" + brokerNum)
.dependsOn(this.zookeeper)
.withExternalZookeeper("zookeeper:" + KafkaContainer.ZOOKEEPER_PORT)
.withEnv("KAFKA_BROKER_ID", brokerNum + "")
.withEnv("KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR", internalTopicsRf + "")
.withEnv("KAFKA_OFFSETS_TOPIC_NUM_PARTITIONS", internalTopicsRf + "")
.withEnv("KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR", internalTopicsRf + "")
.withEnv("KAFKA_TRANSACTION_STATE_LOG_MIN_ISR", internalTopicsRf + "")
.withStartupTimeout(Duration.ofMinutes(1));
})
.collect(Collectors.toList());
}

public Collection<KafkaContainer> getBrokers() {
return this.brokers;
}

public String getBootstrapServers() {
return brokers.stream()
.map(KafkaContainer::getBootstrapServers)
.collect(Collectors.joining(","));
return brokers.stream().map(KafkaContainer::getBootstrapServers).collect(Collectors.joining(","));
}

private Stream<GenericContainer<?>> allContainers() {
return Stream.concat(
this.brokers.stream(),
Stream.of(this.zookeeper)
);
return Stream.concat(this.brokers.stream(), Stream.of(this.zookeeper));
}

@Override
Expand All @@ -86,15 +87,23 @@ public void start() {
// sequential start to avoid resource contention on CI systems with weaker hardware
brokers.forEach(GenericContainer::start);

Unreliables.retryUntilTrue(30, TimeUnit.SECONDS, () -> {
Container.ExecResult result = this.zookeeper.execInContainer(
"sh", "-c",
"zookeeper-shell zookeeper:" + KafkaContainer.ZOOKEEPER_PORT + " ls /brokers/ids | tail -n 1"
);
String brokers = result.getStdout();

return brokers != null && brokers.split(",").length == this.brokersNum;
});
Unreliables.retryUntilTrue(
30,
TimeUnit.SECONDS,
() -> {
Container.ExecResult result =
this.zookeeper.execInContainer(
"sh",
"-c",
"zookeeper-shell zookeeper:" +
KafkaContainer.ZOOKEEPER_PORT +
" ls /brokers/ids | tail -n 1"
);
String brokers = result.getStdout();

return brokers != null && brokers.split(",").length == this.brokersNum;
}
);
}

@Override
Expand Down