From d35657a5b7512b204b4973f44a3c59580c570d2e Mon Sep 17 00:00:00 2001 From: Daniel Frey Date: Fri, 21 Oct 2022 12:40:47 -0400 Subject: [PATCH] Move xmpp testcontainer to function-test-support --- common/function-test-support/pom.xml | 5 +- .../fn/test/support/xmpp/XmppContainer.java | 46 +++++++++++ .../xmpp/XmppTestContainerSupport.java | 72 ++++++++++++++++++ .../src/main/resources/logback-test.xml | 12 +++ .../xmpp}/conf/available-plugins.xml | 0 .../resources/xmpp}/conf/crowd.properties | 0 .../xmpp}/conf/openfire-demoboot.xml | 0 .../main/resources/xmpp}/conf/openfire.xml | 0 .../main/resources/xmpp}/conf/security.xml | 0 .../xmpp}/conf/security/archive/readme.txt | 0 .../xmpp}/conf/security/client.truststore | Bin .../resources/xmpp}/conf/security/keystore | Bin .../resources/xmpp}/conf/security/truststore | Bin .../resources/xmpp}/conf/server-update.xml | 0 consumer/xmpp-consumer/pom.xml | 16 ++-- .../docker-compose/xmpp/docker-compose.yml | 10 --- .../xmpp/XmppConsumerConfigurationTests.java | 55 ++++++------- 17 files changed, 166 insertions(+), 50 deletions(-) create mode 100644 common/function-test-support/src/main/java/org/springframework/cloud/fn/test/support/xmpp/XmppContainer.java create mode 100644 common/function-test-support/src/main/java/org/springframework/cloud/fn/test/support/xmpp/XmppTestContainerSupport.java create mode 100644 common/function-test-support/src/main/resources/logback-test.xml rename {consumer/xmpp-consumer/src/test/docker-compose/xmpp/_data/openfire => common/function-test-support/src/main/resources/xmpp}/conf/available-plugins.xml (100%) rename {consumer/xmpp-consumer/src/test/docker-compose/xmpp/_data/openfire => common/function-test-support/src/main/resources/xmpp}/conf/crowd.properties (100%) rename {consumer/xmpp-consumer/src/test/docker-compose/xmpp/_data/openfire => common/function-test-support/src/main/resources/xmpp}/conf/openfire-demoboot.xml (100%) rename {consumer/xmpp-consumer/src/test/docker-compose/xmpp/_data/openfire => common/function-test-support/src/main/resources/xmpp}/conf/openfire.xml (100%) rename {consumer/xmpp-consumer/src/test/docker-compose/xmpp/_data/openfire => common/function-test-support/src/main/resources/xmpp}/conf/security.xml (100%) rename {consumer/xmpp-consumer/src/test/docker-compose/xmpp/_data/openfire => common/function-test-support/src/main/resources/xmpp}/conf/security/archive/readme.txt (100%) rename {consumer/xmpp-consumer/src/test/docker-compose/xmpp/_data/openfire => common/function-test-support/src/main/resources/xmpp}/conf/security/client.truststore (100%) rename {consumer/xmpp-consumer/src/test/docker-compose/xmpp/_data/openfire => common/function-test-support/src/main/resources/xmpp}/conf/security/keystore (100%) rename {consumer/xmpp-consumer/src/test/docker-compose/xmpp/_data/openfire => common/function-test-support/src/main/resources/xmpp}/conf/security/truststore (100%) rename {consumer/xmpp-consumer/src/test/docker-compose/xmpp/_data/openfire => common/function-test-support/src/main/resources/xmpp}/conf/server-update.xml (100%) delete mode 100644 consumer/xmpp-consumer/src/test/docker-compose/xmpp/docker-compose.yml diff --git a/common/function-test-support/pom.xml b/common/function-test-support/pom.xml index cc4086d2..bca7a1f7 100644 --- a/common/function-test-support/pom.xml +++ b/common/function-test-support/pom.xml @@ -47,7 +47,10 @@ org.testcontainers testcontainers - ${test-containers.version} + + + org.testcontainers + junit-jupiter org.awaitility diff --git a/common/function-test-support/src/main/java/org/springframework/cloud/fn/test/support/xmpp/XmppContainer.java b/common/function-test-support/src/main/java/org/springframework/cloud/fn/test/support/xmpp/XmppContainer.java new file mode 100644 index 00000000..093be73c --- /dev/null +++ b/common/function-test-support/src/main/java/org/springframework/cloud/fn/test/support/xmpp/XmppContainer.java @@ -0,0 +1,46 @@ +/* + * Copyright 2014-2022 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.cloud.fn.test.support.xmpp; + +import org.testcontainers.containers.BindMode; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.wait.strategy.Wait; +import org.testcontainers.utility.DockerImageName; + +/** + * @author Chris Bono + */ +public class XmppContainer extends GenericContainer { + + private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("fishbowler/openfire:v4.7.0"); + + private static final int XMPP_INTERNAL_PORT = 5222; + + public XmppContainer() { + this(DEFAULT_IMAGE_NAME); + } + + public XmppContainer(DockerImageName dockerImageName) { + super(dockerImageName); + dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); + withExposedPorts(XMPP_INTERNAL_PORT); + withClasspathResourceMapping("xmpp/conf", "/var/lib/openfire/conf", BindMode.READ_ONLY); + withCommand("-demoboot"); + setWaitStrategy(Wait.defaultWaitStrategy()); + } + +} diff --git a/common/function-test-support/src/main/java/org/springframework/cloud/fn/test/support/xmpp/XmppTestContainerSupport.java b/common/function-test-support/src/main/java/org/springframework/cloud/fn/test/support/xmpp/XmppTestContainerSupport.java new file mode 100644 index 00000000..bef7d290 --- /dev/null +++ b/common/function-test-support/src/main/java/org/springframework/cloud/fn/test/support/xmpp/XmppTestContainerSupport.java @@ -0,0 +1,72 @@ +/* + * Copyright 2014-2022 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.cloud.fn.test.support.xmpp; + +import org.junit.jupiter.api.BeforeAll; +import org.testcontainers.junit.jupiter.Testcontainers; + +/** + * @author Chris Bono + */ +@Testcontainers(disabledWithoutDocker = true) +public interface XmppTestContainerSupport { + + /** + * Default XMPP Host. + */ + String XMPP_HOST = "localhost"; + + /** + * Sample John User setup by config. + */ + String JOHN_USER = "john"; + + /** + * Sample Jane User setup by config. + */ + String JANE_USER = "jane"; + + + /** + * Password for sample users. + */ + String USER_PW = "secret"; + + /** + * Default Service Name. + */ + String SERVICE_NAME = "localhost"; + + /** + * The container. + */ + XmppContainer XMPP_CONTAINER = new XmppContainer(); + + @BeforeAll + static void startContainer() { + XMPP_CONTAINER.start(); + } + + static String getXmppHost() { + return XMPP_HOST; + } + + static Integer getXmppMappedPort() { + return XMPP_CONTAINER.getFirstMappedPort(); + } + +} diff --git a/common/function-test-support/src/main/resources/logback-test.xml b/common/function-test-support/src/main/resources/logback-test.xml new file mode 100644 index 00000000..554cd1bd --- /dev/null +++ b/common/function-test-support/src/main/resources/logback-test.xml @@ -0,0 +1,12 @@ + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n + + + + + + + + diff --git a/consumer/xmpp-consumer/src/test/docker-compose/xmpp/_data/openfire/conf/available-plugins.xml b/common/function-test-support/src/main/resources/xmpp/conf/available-plugins.xml similarity index 100% rename from consumer/xmpp-consumer/src/test/docker-compose/xmpp/_data/openfire/conf/available-plugins.xml rename to common/function-test-support/src/main/resources/xmpp/conf/available-plugins.xml diff --git a/consumer/xmpp-consumer/src/test/docker-compose/xmpp/_data/openfire/conf/crowd.properties b/common/function-test-support/src/main/resources/xmpp/conf/crowd.properties similarity index 100% rename from consumer/xmpp-consumer/src/test/docker-compose/xmpp/_data/openfire/conf/crowd.properties rename to common/function-test-support/src/main/resources/xmpp/conf/crowd.properties diff --git a/consumer/xmpp-consumer/src/test/docker-compose/xmpp/_data/openfire/conf/openfire-demoboot.xml b/common/function-test-support/src/main/resources/xmpp/conf/openfire-demoboot.xml similarity index 100% rename from consumer/xmpp-consumer/src/test/docker-compose/xmpp/_data/openfire/conf/openfire-demoboot.xml rename to common/function-test-support/src/main/resources/xmpp/conf/openfire-demoboot.xml diff --git a/consumer/xmpp-consumer/src/test/docker-compose/xmpp/_data/openfire/conf/openfire.xml b/common/function-test-support/src/main/resources/xmpp/conf/openfire.xml similarity index 100% rename from consumer/xmpp-consumer/src/test/docker-compose/xmpp/_data/openfire/conf/openfire.xml rename to common/function-test-support/src/main/resources/xmpp/conf/openfire.xml diff --git a/consumer/xmpp-consumer/src/test/docker-compose/xmpp/_data/openfire/conf/security.xml b/common/function-test-support/src/main/resources/xmpp/conf/security.xml similarity index 100% rename from consumer/xmpp-consumer/src/test/docker-compose/xmpp/_data/openfire/conf/security.xml rename to common/function-test-support/src/main/resources/xmpp/conf/security.xml diff --git a/consumer/xmpp-consumer/src/test/docker-compose/xmpp/_data/openfire/conf/security/archive/readme.txt b/common/function-test-support/src/main/resources/xmpp/conf/security/archive/readme.txt similarity index 100% rename from consumer/xmpp-consumer/src/test/docker-compose/xmpp/_data/openfire/conf/security/archive/readme.txt rename to common/function-test-support/src/main/resources/xmpp/conf/security/archive/readme.txt diff --git a/consumer/xmpp-consumer/src/test/docker-compose/xmpp/_data/openfire/conf/security/client.truststore b/common/function-test-support/src/main/resources/xmpp/conf/security/client.truststore similarity index 100% rename from consumer/xmpp-consumer/src/test/docker-compose/xmpp/_data/openfire/conf/security/client.truststore rename to common/function-test-support/src/main/resources/xmpp/conf/security/client.truststore diff --git a/consumer/xmpp-consumer/src/test/docker-compose/xmpp/_data/openfire/conf/security/keystore b/common/function-test-support/src/main/resources/xmpp/conf/security/keystore similarity index 100% rename from consumer/xmpp-consumer/src/test/docker-compose/xmpp/_data/openfire/conf/security/keystore rename to common/function-test-support/src/main/resources/xmpp/conf/security/keystore diff --git a/consumer/xmpp-consumer/src/test/docker-compose/xmpp/_data/openfire/conf/security/truststore b/common/function-test-support/src/main/resources/xmpp/conf/security/truststore similarity index 100% rename from consumer/xmpp-consumer/src/test/docker-compose/xmpp/_data/openfire/conf/security/truststore rename to common/function-test-support/src/main/resources/xmpp/conf/security/truststore diff --git a/consumer/xmpp-consumer/src/test/docker-compose/xmpp/_data/openfire/conf/server-update.xml b/common/function-test-support/src/main/resources/xmpp/conf/server-update.xml similarity index 100% rename from consumer/xmpp-consumer/src/test/docker-compose/xmpp/_data/openfire/conf/server-update.xml rename to common/function-test-support/src/main/resources/xmpp/conf/server-update.xml diff --git a/consumer/xmpp-consumer/pom.xml b/consumer/xmpp-consumer/pom.xml index 91204ed0..e83dca06 100644 --- a/consumer/xmpp-consumer/pom.xml +++ b/consumer/xmpp-consumer/pom.xml @@ -22,13 +22,17 @@ ${project.version} - - - - - - + + org.springframework.cloud.fn + function-test-support + ${project.version} + test + + + + + diff --git a/consumer/xmpp-consumer/src/test/docker-compose/xmpp/docker-compose.yml b/consumer/xmpp-consumer/src/test/docker-compose/xmpp/docker-compose.yml deleted file mode 100644 index d7e38c47..00000000 --- a/consumer/xmpp-consumer/src/test/docker-compose/xmpp/docker-compose.yml +++ /dev/null @@ -1,10 +0,0 @@ -version: "3.7" -services: - Openfire: - image: fishbowler/openfire:latest - ports: - - "5222:5222" - volumes: - - ./_data/openfire/conf:/var/lib/openfire/conf - command: - - -demoboot diff --git a/consumer/xmpp-consumer/src/test/java/org/springframework/cloud/fn/consumer/xmpp/XmppConsumerConfigurationTests.java b/consumer/xmpp-consumer/src/test/java/org/springframework/cloud/fn/consumer/xmpp/XmppConsumerConfigurationTests.java index fab99faa..9e3eedd1 100644 --- a/consumer/xmpp-consumer/src/test/java/org/springframework/cloud/fn/consumer/xmpp/XmppConsumerConfigurationTests.java +++ b/consumer/xmpp-consumer/src/test/java/org/springframework/cloud/fn/consumer/xmpp/XmppConsumerConfigurationTests.java @@ -16,7 +16,6 @@ package org.springframework.cloud.fn.consumer.xmpp; -import java.io.File; import java.io.IOException; import java.time.Duration; import java.util.function.Consumer; @@ -33,52 +32,42 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.jxmpp.stringprep.XmppStringprepException; -import org.testcontainers.containers.DockerComposeContainer; -import org.testcontainers.junit.jupiter.Container; -import org.testcontainers.junit.jupiter.Testcontainers; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.fn.test.support.xmpp.XmppTestContainerSupport; import org.springframework.context.annotation.Import; import org.springframework.integration.xmpp.XmppHeaders; import org.springframework.messaging.Message; import org.springframework.messaging.support.MessageBuilder; import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; import static org.assertj.core.api.Assertions.assertThat; import static org.awaitility.Awaitility.await; /** * @author Daniel Frey + * @author Chris Bono * * @since 4.0.0 */ -@SpringBootTest( - properties = { - "xmpp.factory.user=john", - "xmpp.factory.password=secret", - "xmpp.factory.host=localhost", - "xmpp.factory.service-name=localhost", - "xmpp.factory.security-mode=disabled" - } -) +@SpringBootTest @DirtiesContext -@Testcontainers -public class XmppConsumerConfigurationTests { +public class XmppConsumerConfigurationTests implements XmppTestContainerSupport { - private static final String XMPP_HOST = "localhost"; - private static final int XMPP_PORT = 5222; - private static final String FROM = "john"; - private static final String TO = "jane"; - private static final String TO_PW = "secret"; - private static final String SERVICE_NAME = "localhost"; - - @Container - private static final DockerComposeContainer XMPP_CONTAINER = - new DockerComposeContainer(new File("src/test/docker-compose/xmpp/docker-compose.yml")) - .withExposedService("Openfire", XMPP_PORT); + @DynamicPropertySource + static void registerConfigurationProperties(DynamicPropertyRegistry registry) { + registry.add("xmpp.factory.user", () -> JOHN_USER); + registry.add("xmpp.factory.password", () -> USER_PW); + registry.add("xmpp.factory.host", () -> XmppTestContainerSupport.getXmppHost()); + registry.add("xmpp.factory.port", () -> XmppTestContainerSupport.getXmppMappedPort()); + registry.add("xmpp.factory.service-name", () -> SERVICE_NAME); + registry.add("xmpp.factory.security-mode", () -> "disabled"); + } @Autowired private Consumer> xmppConsumer; @@ -92,10 +81,10 @@ public class XmppConsumerConfigurationTests { XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder(); builder.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled); - builder.setHost(XMPP_HOST); - builder.setPort(XMPP_PORT); + builder.setHost(XmppTestContainerSupport.getXmppHost()); + builder.setPort(XmppTestContainerSupport.getXmppMappedPort()); builder.setResource(SERVICE_NAME); - builder.setUsernameAndPassword(TO, TO_PW) + builder.setUsernameAndPassword(JANE_USER, USER_PW) .setXmppDomain(SERVICE_NAME); this.clientConnection = new XMPPTCPConnection(builder.build()); @@ -119,7 +108,7 @@ public class XmppConsumerConfigurationTests { Message testMessage = MessageBuilder.withPayload("test") - .setHeader(XmppHeaders.TO, TO + "@" + SERVICE_NAME) + .setHeader(XmppHeaders.TO, JANE_USER + "@" + SERVICE_NAME) .build(); await().atMost(Duration.ofSeconds(20)).pollDelay(Duration.ofMillis(100)) @@ -141,7 +130,7 @@ public class XmppConsumerConfigurationTests { = this.clientConnection.createStanzaCollector(StanzaTypeFilter.MESSAGE); Message testMessage = - MessageBuilder.withPayload(org.jivesoftware.smack.packet.MessageBuilder.buildMessage().addBody("en_us", "test").to(TO + "@" + SERVICE_NAME).build()) + MessageBuilder.withPayload(org.jivesoftware.smack.packet.MessageBuilder.buildMessage().addBody("en_us", "test").to(JANE_USER + "@" + SERVICE_NAME).build()) .build(); await().atMost(Duration.ofSeconds(20)).pollDelay(Duration.ofMillis(100)) @@ -163,13 +152,13 @@ public class XmppConsumerConfigurationTests { private void assertTo(Stanza stanza) { - assertThat(stanza.getTo().asBareJid().asUnescapedString()).isEqualTo(TO + "@" + SERVICE_NAME); + assertThat(stanza.getTo().asBareJid().asUnescapedString()).isEqualTo(JANE_USER + "@" + SERVICE_NAME); } private void assertFrom(Stanza stanza) { - assertThat(stanza.getFrom().asBareJid().asUnescapedString()).isEqualTo(FROM + "@" + SERVICE_NAME); + assertThat(stanza.getFrom().asBareJid().asUnescapedString()).isEqualTo(JOHN_USER + "@" + SERVICE_NAME); }