This commit is contained in:
Ahmed Tawila 2017-11-16 08:54:36 +02:00
commit 76f6e99256
21 changed files with 632 additions and 78 deletions

View File

@ -1,68 +1,87 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>drools</artifactId>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>drools</artifactId>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<properties>
<http-component-version>4.4.6</http-component-version>
<drools-version>7.4.1.Final</drools-version>
<apache-poi-version>3.13</apache-poi-version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>${http-component-version}</version>
</dependency>
<!-- ... -->
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-ci</artifactId>
<version>${drools-version}</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-decisiontables</artifactId>
<version>${drools-version}</version>
</dependency>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>${http-component-version}</version>
</dependency>
<!-- ... -->
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-ci</artifactId>
<version>${drools-version}</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-decisiontables</artifactId>
<version>${drools-version}</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
<version>${drools-version}</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>${drools-version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${apache-poi-version}</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
<version>${drools-version}</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>${drools-version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${apache-poi-version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${apache-poi-version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${apache-poi-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
</dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<forkCount>3</forkCount>
<reuseForks>true</reuseForks>
<excludes>
<exclude>**/*IntegrationTest.java</exclude>
<exclude>**/*LongRunningUnitTest.java</exclude>
<exclude>**/*ManualTest.java</exclude>
<exclude>**/JdbcTest.java</exclude>
<exclude>**/*LiveTest.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -2,16 +2,36 @@ package com.baeldung.templatemethodpattern.model;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.List;
public class Computer {
public abstract class ComputerBuilder {
private Map<String, String> computerParts = new HashMap<>();
protected Map<String, String> computerParts = new HashMap<>();
protected List<String> moterboardSetupStatus = new ArrayList<>();
public Computer(Map<String, String> computerParts) {
this.computerParts = computerParts;
public final Computer buildComputer() {
addMotherboard();
setupMotherboard();
addProcessor();
return getComputer();
}
public abstract void addMotherboard();
public abstract void setupMotherboard();
public abstract void addProcessor();
public List<String> getMotherboardSetupStatus() {
return moterboardSetupStatus;
}
public Map<String, String> getComputerParts() {
return computerParts;
}
private Computer getComputer() {
return new Computer(computerParts);
}
}

View File

@ -8,7 +8,7 @@ import java.util.Map;
public abstract class ComputerBuilder {
protected Map<String, String> computerParts = new HashMap<>();
protected List<String> motherboardSetupStatus = new ArrayList<>();
protected List<String> moterboardSetupStatus = new ArrayList<>();
public final Computer buildComputer() {
addMotherboard();
@ -20,13 +20,13 @@ public abstract class ComputerBuilder {
public abstract void addMotherboard();
public abstract void setupMotherboard();
public abstract void addProcessor();
public List<String> getMotherboardSetupStatus() {
return motherboardSetupStatus;
}
public abstract void addProcessor();
public List<String> getMotherboardSetupStatus() {
return moterboardSetupStatus;
}
public Map<String, String> getComputerParts() {
return computerParts;
}

View File

@ -0,0 +1,26 @@
package com.baeldung.templatemethodpattern.model;
public class HighEndComputer extends Computer {
@Override
public void addMotherboard() {
computerParts.put("Motherboard", "High-end Motherboard");
}
@Override
public void setupMotherboard() {
moterboardSetupStatus.add("Screwing the high-end motherboard to the case.");
moterboardSetupStatus.add("Pluging in the power supply connectors.");
moterboardSetupStatus.forEach(step -> System.out.println(step));
}
@Override
public void addProcessor() {
computerParts.put("Processor", "High-end Processor");
}
@Override
public void addMotherboard() {
computerParts.put("Motherboard", "High End Motherboard");
}
}

View File

@ -9,13 +9,13 @@ public class HighEndComputerBuilder extends ComputerBuilder {
@Override
public void setupMotherboard() {
motherboardSetupStatus.add("Screwing the high-end motherboard to the case.");
motherboardSetupStatus.add("Pluging in the power supply connectors.");
motherboardSetupStatus.forEach(step -> System.out.println(step));
moterboardSetupStatus.add("Screwing the high-end motherboard to the case.");
moterboardSetupStatus.add("Pluging in the power supply connectors.");
moterboardSetupStatus.forEach(step -> System.out.println(step));
}
@Override
public void addProcessor() {
computerParts.put("Processor", "High-end Processor");
computerParts.put("Processor", "High-end Processor");
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.templatemethodpattern.model;
public class StandardComputer extends Computer {
public void addMotherboard() {
computerParts.put("Motherboard", "Standard Motherboard");
}
@Override
public void setupMotherboard() {
moterboardSetupStatus.add("Screwing the standard motherboard to the case.");
moterboardSetupStatus.add("Pluging in the power supply connectors.");
moterboardSetupStatus.forEach(step -> System.out.println(step));
}
@Override
public void addProcessor() {
computerParts.put("Processor", "Standard Processor");
}
@Override
public void addMotherboard() {
computerParts.put("Motherboard", "Standard Motherboard");
}
}

View File

@ -9,9 +9,9 @@ public class StandardComputerBuilder extends ComputerBuilder {
@Override
public void setupMotherboard() {
motherboardSetupStatus.add("Screwing the standard motherboard to the case.");
motherboardSetupStatus.add("Pluging in the power supply connectors.");
motherboardSetupStatus.forEach(step -> System.out.println(step));
moterboardSetupStatus.add("Screwing the standard motherboard to the case.");
moterboardSetupStatus.add("Pluging in the power supply connectors.");
moterboardSetupStatus.forEach(step -> System.out.println(step));
}
@Override

View File

@ -29,8 +29,8 @@ public class TemplateMethodPatternTest {
@Test
public void givenStandardMotherBoard_whenAddingMotherBoard_thenEqualAssertion() {
standardComputerBuilder.addMotherboard();
assertEquals("Standard Motherboard", standardComputerBuilder.getComputerParts().get("Motherboard"));
standardComputer.addMotherboard();
assertEquals("Standard Motherboard", standardComputer.getComputerParts().get("Motherboard"));
}
@Test
@ -77,10 +77,11 @@ public class TemplateMethodPatternTest {
}
@Test
public void givenAllHighEnddParts_whenBuildingComputer_thenTwoParts() {
public void givenAllHighEndParts_whenBuildingComputer_thenTwoParts() {
highEndComputerBuilder.buildComputer();
assertEquals(2, highEndComputerBuilder.getComputerParts().size());
}
@Test
public void givenAllHighEndParts_whenComputerisBuilt_thenComputerInstance() {
assertThat(standardComputerBuilder.buildComputer(), instanceOf(Computer.class));

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.spring.cloud</groupId>
@ -15,8 +15,9 @@
<module>spring-cloud-ribbon-client</module>
<module>spring-cloud-rest</module>
<module>spring-cloud-zookeeper</module>
<module>spring-cloud-gateway</module>
<module>spring-cloud-connectors-heroku</module>
<module>spring-cloud-gateway</module>
<module>spring-cloud-stream</module>
<module>spring-cloud-connectors-heroku</module>
</modules>
<packaging>pom</packaging>
@ -38,6 +39,7 @@
<spring-cloud-starter-eureka.version>1.2.3.RELEASE</spring-cloud-starter-eureka.version>
<spring-cloud-starter-feign.version>1.2.3.RELEASE</spring-cloud-starter-feign.version>
<spring-cloud-starter-hystrix.version>1.2.3.RELEASE</spring-cloud-starter-hystrix.version>
<spring-cloud-stream.version>1.3.0.RELEASE</spring-cloud-stream.version>
<spring-boot-starter-web.version>1.4.2.RELEASE</spring-boot-starter-web.version>
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
<spring-boot-maven-plugin.version>1.4.2.RELEASE</spring-boot-maven-plugin.version>

View File

@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.baeldung</groupId>
<artifactId>spring-cloud-stream</artifactId>
<packaging>pom</packaging>
<name>spring-cloud-stream</name>
<parent>
<groupId>com.baeldung.spring.cloud</groupId>
<artifactId>spring-cloud</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modules>
<module>spring-cloud-stream-rabbit</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
<version>${spring-cloud-stream.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream</artifactId>
<version>${spring-cloud-stream.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-test-support</artifactId>
<version>${spring-cloud-stream.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot-maven-plugin.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-stream-rabbit</artifactId>
<packaging>jar</packaging>
<name>spring-cloud-stream-rabbit</name>
<description>Simple Spring Cloud Stream</description>
<parent>
<groupId>org.baeldung</groupId>
<artifactId>spring-cloud-stream</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-test-support</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,38 @@
package com.baeldung.spring.cloud.stream.rabbit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import com.baeldung.spring.cloud.stream.rabbit.processor.MyProcessor;
@SpringBootApplication
@EnableBinding(MyProcessor.class)
public class MultipleOutputsServiceApplication {
public static void main(String[] args) {
SpringApplication.run(MultipleOutputsServiceApplication.class, args);
}
@Autowired
private MyProcessor processor;
@StreamListener(MyProcessor.INPUT)
public void routeValues(Integer val) {
if (val < 10) {
processor.anOutput()
.send(message(val));
} else {
processor.anotherOutput()
.send(message(val));
}
}
private static final <T> Message<T> message(T val) {
return MessageBuilder.withPayload(val)
.build();
}
}

View File

@ -0,0 +1,39 @@
package com.baeldung.spring.cloud.stream.rabbit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import com.baeldung.spring.cloud.stream.rabbit.processor.MyProcessor;
@SpringBootApplication
@EnableBinding(MyProcessor.class)
public class MultipleOutputsWithConditionsServiceApplication {
public static void main(String[] args) {
SpringApplication.run(MultipleOutputsWithConditionsServiceApplication.class, args);
}
@Autowired
private MyProcessor processor;
@StreamListener(target = MyProcessor.INPUT, condition = "payload < 10")
public void routeValuesToAnOutput(Integer val) {
processor.anOutput()
.send(message(val));
}
@StreamListener(target = MyProcessor.INPUT, condition = "payload >= 10")
public void routeValuesToAnotherOutput(Integer val) {
processor.anotherOutput()
.send(message(val));
}
private static final <T> Message<T> message(T val) {
return MessageBuilder.withPayload(val)
.build();
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.spring.cloud.stream.rabbit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Processor;
import org.springframework.context.annotation.Bean;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.handler.annotation.SendTo;
import com.baeldung.spring.cloud.stream.rabbit.messages.TextPlainMessageConverter;
import com.baeldung.spring.cloud.stream.rabbit.model.LogMessage;
@SpringBootApplication
@EnableBinding(Processor.class)
public class MyLoggerServiceApplication {
public static void main(String[] args) {
SpringApplication.run(MyLoggerServiceApplication.class, args);
}
@Bean
public MessageConverter providesTextPlainMessageConverter() {
return new TextPlainMessageConverter();
}
@StreamListener(Processor.INPUT)
@SendTo(Processor.OUTPUT)
public LogMessage enrichLogMessage(LogMessage log) {
return new LogMessage(String.format("[1]: %s", log.getMessage()));
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.spring.cloud.stream.rabbit.messages;
import org.springframework.messaging.Message;
import org.springframework.messaging.converter.AbstractMessageConverter;
import org.springframework.util.MimeType;
import com.baeldung.spring.cloud.stream.rabbit.model.LogMessage;
public class TextPlainMessageConverter extends AbstractMessageConverter {
public TextPlainMessageConverter() {
super(new MimeType("text", "plain"));
}
@Override
protected boolean supports(Class<?> clazz) {
return (LogMessage.class == clazz);
}
@Override
protected Object convertFromInternal(Message<?> message, Class<?> targetClass, Object conversionHint) {
Object payload = message.getPayload();
String text = payload instanceof String ? (String) payload : new String((byte[]) payload);
return new LogMessage(text);
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.spring.cloud.stream.rabbit.model;
import java.io.Serializable;
public class LogMessage implements Serializable {
private static final long serialVersionUID = -5857383701708275796L;
private String message;
public LogMessage() {
}
public LogMessage(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public String toString() {
return message;
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.spring.cloud.stream.rabbit.processor;
import org.springframework.cloud.stream.annotation.Input;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.SubscribableChannel;
public interface MyProcessor {
String INPUT = "myInput";
@Input
SubscribableChannel myInput();
@Output("myOutput")
MessageChannel anOutput();
@Output
MessageChannel anotherOutput();
}

View File

@ -0,0 +1,28 @@
spring:
cloud:
stream:
bindings:
input:
destination: queue.log.messages
binder: local_rabbit
group: logMessageConsumers
output:
destination: queue.pretty.log.messages
binder: local_rabbit
binders:
local_rabbit:
type: rabbit
environment:
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
virtual-host: /
server:
port: 0
management:
health:
binders:
enabled: true

View File

@ -0,0 +1,52 @@
package com.baeldung.spring.cloud.stream.rabbit;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.test.binder.MessageCollector;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.baeldung.spring.cloud.stream.rabbit.processor.MyProcessor;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MultipleOutputsServiceApplication.class)
@DirtiesContext
public class MultipleOutputsServiceApplicationIntegrationTest {
@Autowired
private MyProcessor pipe;
@Autowired
private MessageCollector messageCollector;
@Test
public void whenSendMessage_thenResponseIsInAOutput() {
whenSendMessage(1);
thenPayloadInChannelIs(pipe.anOutput(), 1);
}
@Test
public void whenSendMessage_thenResponseIsInAnotherOutput() {
whenSendMessage(11);
thenPayloadInChannelIs(pipe.anotherOutput(), 11);
}
private void whenSendMessage(Integer val) {
pipe.myInput()
.send(MessageBuilder.withPayload(val)
.build());
}
private void thenPayloadInChannelIs(MessageChannel channel, Integer expectedValue) {
Object payload = messageCollector.forChannel(channel)
.poll()
.getPayload();
assertEquals(expectedValue, payload);
}
}

View File

@ -0,0 +1,52 @@
package com.baeldung.spring.cloud.stream.rabbit;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.test.binder.MessageCollector;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.baeldung.spring.cloud.stream.rabbit.processor.MyProcessor;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MultipleOutputsWithConditionsServiceApplication.class)
@DirtiesContext
public class MultipleOutputsWithConditionsServiceIntegrationTest {
@Autowired
private MyProcessor pipe;
@Autowired
private MessageCollector messageCollector;
@Test
public void whenSendMessage_thenResponseIsInAOutput() {
whenSendMessage(1);
thenPayloadInChannelIs(pipe.anOutput(), 1);
}
@Test
public void whenSendMessage_thenResponseIsInAnotherOutput() {
whenSendMessage(11);
thenPayloadInChannelIs(pipe.anotherOutput(), 11);
}
private void whenSendMessage(Integer val) {
pipe.myInput()
.send(MessageBuilder.withPayload(val)
.build());
}
private void thenPayloadInChannelIs(MessageChannel channel, Integer expectedValue) {
Object payload = messageCollector.forChannel(channel)
.poll()
.getPayload();
assertEquals(expectedValue, payload);
}
}

View File

@ -0,0 +1,40 @@
package com.baeldung.spring.cloud.stream.rabbit;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.messaging.Processor;
import org.springframework.cloud.stream.test.binder.MessageCollector;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.baeldung.spring.cloud.stream.rabbit.model.LogMessage;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MyLoggerServiceApplication.class)
@DirtiesContext
public class MyLoggerApplicationIntegrationTest {
@Autowired
private Processor pipe;
@Autowired
private MessageCollector messageCollector;
@Test
public void whenSendMessage_thenResponseShouldUpdateText() {
pipe.input()
.send(MessageBuilder.withPayload(new LogMessage("This is my message"))
.build());
Object payload = messageCollector.forChannel(pipe.output())
.poll()
.getPayload();
assertEquals("[1]: This is my message", payload.toString());
}
}