This is an automated email from the ASF dual-hosted git repository. ahuber pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/causeway.git
commit 2f0364af66c7f5abe221eb4d5d1349d617869b08 Author: Andi Huber <[email protected]> AuthorDate: Wed Jan 24 12:52:17 2024 +0100 CAUSEWAY-3668: adds EmailServiceDefault IntegTest --- regressiontests/stable-config/pom.xml | 14 +++ .../config/EmailServiceDefault_IntegTest.java | 122 +++++++++++++++++++++ .../resources/application-config-test.properties | 9 ++ 3 files changed, 145 insertions(+) diff --git a/regressiontests/stable-config/pom.xml b/regressiontests/stable-config/pom.xml index a0da351865..3ab5ff5fe4 100644 --- a/regressiontests/stable-config/pom.xml +++ b/regressiontests/stable-config/pom.xml @@ -41,11 +41,25 @@ </properties> <dependencies> + <dependency> <groupId>org.apache.causeway.regressiontests</groupId> <artifactId>causeway-regressiontests-stable</artifactId> <scope>test</scope> </dependency> + + <dependency> + <groupId>org.apache.causeway.core</groupId> + <artifactId>causeway-core-runtimeservices</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>com.icegreen</groupId> + <artifactId>greenmail-junit5</artifactId> + <version>1.6.15</version> + <scope>test</scope> + </dependency> </dependencies> diff --git a/regressiontests/stable-config/src/test/java/org/apache/causeway/testdomain/config/EmailServiceDefault_IntegTest.java b/regressiontests/stable-config/src/test/java/org/apache/causeway/testdomain/config/EmailServiceDefault_IntegTest.java new file mode 100644 index 0000000000..1ea2fc7fe6 --- /dev/null +++ b/regressiontests/stable-config/src/test/java/org/apache/causeway/testdomain/config/EmailServiceDefault_IntegTest.java @@ -0,0 +1,122 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * http://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.apache.causeway.testdomain.config; + +import java.util.Arrays; +import java.util.Collections; +import java.util.Properties; + +import javax.mail.MessagingException; +import javax.mail.internet.MimeMessage; + +import com.icegreen.greenmail.configuration.GreenMailConfiguration; +import com.icegreen.greenmail.junit5.GreenMailExtension; +import com.icegreen.greenmail.util.GreenMailUtil; +import com.icegreen.greenmail.util.ServerSetupTest; + +import org.json.JSONException; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.mail.javamail.JavaMailSenderImpl; +import org.springframework.test.context.TestPropertySource; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.apache.causeway.commons.collections.Can; +import org.apache.causeway.commons.io.TextUtils; +import org.apache.causeway.core.config.presets.CausewayPresets; +import org.apache.causeway.core.runtimeservices.CausewayModuleCoreRuntimeServices; +import org.apache.causeway.core.runtimeservices.email.EmailServiceDefault; +import org.apache.causeway.testdomain.conf.Configuration_headless; + +@SpringBootTest( + classes = { + Configuration_headless.class, + CausewayModuleCoreRuntimeServices.class, + //JavaMailSenderImpl.class, + EmailServiceDefault_IntegTest.MailSenderProvider.class + } +) +@TestPropertySource({ + "classpath:/application-config-test.properties", + CausewayPresets.UseLog4j2Test +}) +class EmailServiceDefault_IntegTest { + + @RegisterExtension + static GreenMailExtension greenMail = new GreenMailExtension(ServerSetupTest.SMTP) + .withConfiguration(GreenMailConfiguration.aConfig().withUser("user", "admin")) + .withPerMethodLifecycle(false); + + @Autowired(required = true) + private EmailServiceDefault emailService; + + @Configuration + static class MailSenderProvider { + @Bean + public JavaMailSender getJavaMailSender() { + JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); + mailSender.setHost("127.0.0.1"); + mailSender.setPort(3025); + + mailSender.setUsername("user"); + mailSender.setPassword("admin"); + + Properties props = mailSender.getJavaMailProperties(); + props.put("mail.transport.protocol", "smtp"); + props.put("mail.smtp.auth", "true"); + props.put("mail.smtp.starttls.enable", "true"); + props.put("mail.debug", "true"); + + return mailSender; + } + } + + @Test + void should_send_email_to_user_with_green_mail_extension() throws JSONException, MessagingException { + + boolean sent = emailService.send( + Arrays.asList("[email protected]"), + Collections.emptyList(), + Collections.emptyList(), + "Message from Java Mail Sender", + "Hello this is a simple email message"); + + assertTrue(sent); + + MimeMessage receivedMessage = greenMail.getReceivedMessages()[0]; + + assertEquals(1, receivedMessage.getAllRecipients().length); + assertEquals("[email protected]", receivedMessage.getAllRecipients()[0].toString()); + assertEquals("[email protected]", receivedMessage.getFrom()[0].toString()); + assertEquals("Message from Java Mail Sender", receivedMessage.getSubject()); + + // extract payload from multiple parts + var bodyLines = TextUtils.readLines(GreenMailUtil.getBody(receivedMessage)); + var filteredLines = bodyLines.filter(line->line.startsWith("Hello")); + assertEquals(Can.of("Hello this is a simple email message"), filteredLines); + } + +} diff --git a/regressiontests/stable-config/src/test/resources/application-config-test.properties b/regressiontests/stable-config/src/test/resources/application-config-test.properties index 132c8d5170..2c9a384f61 100644 --- a/regressiontests/stable-config/src/test/resources/application-config-test.properties +++ b/regressiontests/stable-config/src/test/resources/application-config-test.properties @@ -23,3 +23,12 @@ causeway.core.meta-model.introspector.policy=ANNOTATION_REQUIRED +# spring powered mail, but does not seem to be picked up for testing +spring.mail.host=127.0.0.1 +spring.mail.port=3025 +spring.mail.username=user +spring.mail.password=admin +spring.mail.properties.mail.smtp.auth=true +spring.mail.properties.mail.smtp.starttls.enable=true + +causeway.core.runtime-services.email.sender.address=test.sen...@hotmail.com \ No newline at end of file
