Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Add ActiveMq examples #11

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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 .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
allure-results/
target/
ActiveMQData/
.idea/
*.iml
73 changes: 71 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
<?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"
<?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>
<groupId>ru.sbtqa.tag</groupId>
<artifactId>page-factory-2-example</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>page-factory-2-example</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<pf2.version>9.0.0</pf2.version>
<pf2.version>9.0-SNAPSHOT</pf2.version>
<aspectj.version>1.8.10</aspectj.version>
<activemq.version>5.13.0</activemq.version>
<spring.version>4.2.4.RELEASE</spring.version>
</properties>

<dependencies>
<dependency>
<groupId>ru.sbtqa.tag.pagefactory</groupId>
Expand All @@ -37,6 +42,31 @@
<version>${pf2.version}</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>ru.sbtqa.tag.pagefactory</groupId>
<artifactId>mq-plugin</artifactId>
<version>${pf2.version}</version>
<type>jar</type>
</dependency>

<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>${activemq.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand All @@ -58,6 +88,45 @@
</dependency>
</dependencies>
</plugin>

<!-- Plugins for ActiveMQ -->
<plugin>
<groupId>org.apache.activemq.tooling</groupId>
<artifactId>activemq-maven-plugin</artifactId>
<version>${activemq.version}</version>
<configuration>
<configUri>xbean:file:src/test/resources/config/activemq.xml</configUri>
<fork>true</fork>
<systemProperties>
<property>
<name>org.apache.activemq.default.directory.prefix</name>
<value>./target/</value>
</property>
</systemProperties>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-spring</artifactId>
<version>${activemq.version}</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-leveldb-store</artifactId>
<version>${activemq.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>start-activemq</id>
<goals>
<goal>run</goal>
</goals>
<phase>process-test-resources</phase>
</execution>
</executions>
</plugin>
<!-- End ActiveMQ plugins -->
</plugins>
</build>
</project>
130 changes: 130 additions & 0 deletions src/test/java/ru/sbtqa/tag/pagefactory2example/mq/ActiveMqTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package ru.sbtqa.tag.pagefactory2example.mq;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.sbtqa.tag.mqfactory.MqFactory;
import ru.sbtqa.tag.mqfactory.exception.JmsException;
import ru.sbtqa.tag.mqfactory.exception.MqException;
import ru.sbtqa.tag.mqfactory.interfaces.Jms;

import javax.jms.JMSException;
import javax.jms.QueueConnection;
import javax.jms.TextMessage;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;


/**
* To start tests type in command line:
* mvn -Dtest=ActiveMqTest test
*/
public class ActiveMqTest {
private static final Logger LOG = LoggerFactory.getLogger(ActiveMqTest.class);
public static final String AMQ_BROKER_URL = "tcp://localhost:61616";
public static final String QUEUE_NAME = "testQueue";
public static final int MESSAGE_COUNT = 3;

private QueueConnection mQueueConnection;
private Jms<TextMessage> mqService;

@Before
public void beforeTest() {
mQueueConnection = null;
mqService = createConnection();
}

@After
public void afterTest() throws MqException, JMSException {
mqService.removeAllMessages(QUEUE_NAME);
if (mQueueConnection != null) {
mQueueConnection.close();
}
}

@Test
public void browseMessages() throws MqException {
LOG.info("");
LOG.info("----- Start sending messages -----");
for (int i = 1; i <= MESSAGE_COUNT; i++) {
sendMQ(i, "Correlation_" + i);
}
LOG.info("----- End sending messages -----");

LOG.info("");
LOG.info("----- Start browsing messages -----");
List<TextMessage> listAfterAdd = mqService.browseAllMessages(QUEUE_NAME);
for (TextMessage message : listAfterAdd) {
LOG.info(message.toString());
}
LOG.info("----- End browsing messages -----");
}

@Test
public void getMessagesById() throws MqException {
LOG.info("");
LOG.info("----- Start sending messages -----");
List<String> messageIdList = new ArrayList<>();
for (int i = 1; i <= MESSAGE_COUNT; i++) {
messageIdList.add(sendMQ(i, "Correlation_" + i));
}
LOG.info("----- End sending messages -----");

LOG.info("");
LOG.info("----- Start searching messages by ID -----");
for (String messId : messageIdList) {
TextMessage message = mqService.getMessageById(QUEUE_NAME, messId);
LOG.info(message.toString());
}
LOG.info("----- End searching messages by ID -----");
}

@Test
public void getMessageByParam() throws MqException {
sendMQ(100500, "Correlation_100500");
LOG.info("");
LOG.info("----- Start getting message by param -----");
List<TextMessage> messByCorrelList = mqService.getMessagesByParam(QUEUE_NAME, "JMSCorrelationID", "ID:Correlation_100500");
LOG.info("List of JMSMessageID:");
for (TextMessage message : messByCorrelList) {
try {
LOG.info("---> Message ID: {}; Message text: {}", message.getJMSMessageID(), message.getText());
} catch (JMSException e) {
LOG.error("", e);
}
}
LOG.info("----- End getting message by param -----");
}

private String sendMQ(final int idx, final String correlationId) throws MqException {
return mqService.sendRequest(QUEUE_NAME, message -> {
try {
message.setText("Test MQ " + idx);
message.setJMSCorrelationID("ID:" + correlationId);
} catch (JMSException e) {
throw new JmsException("Can't set text to message");
}
return message;
});
}

private Jms<TextMessage> createConnection() {
QueueConnection queueConnection = null;
try {
ActiveMQConnectionFactory mqCF = new ActiveMQConnectionFactory(AMQ_BROKER_URL);
queueConnection = mqCF.createQueueConnection();
} catch (JMSException ex) {
LOG.error("", ex);
}

Properties connProps = new Properties();
connProps.put(MqFactory.MQ_TYPE, "activeMq");
connProps.put(MqFactory.JMS_CONNECTION, queueConnection);
mQueueConnection = queueConnection;
return MqFactory.getMq(connProps);
}
}
17 changes: 17 additions & 0 deletions src/test/resources/config/activemq.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">

<broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="ActiveMQData"
useShutdownHook="false">
<persistenceAdapter>
<kahaDB directory="ActiveMQData/activemqdb"/>
</persistenceAdapter>

<transportConnectors>
<transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=100"/>
</transportConnectors>
</broker>
</beans>
7 changes: 6 additions & 1 deletion src/test/resources/config/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ fragments.path = src/test/resources/fragments
#path to page objects
api.endpoint.package = ru.sbtqa.tag.pagefactory2example.rest
api.baseURI = https://api.github.com
api.ssl.relaxed = true
api.ssl.relaxed = true

#MQ wait timeout in milliseconds. Default value is 1000 ms
mq.timeout=300
#Kafka wait timeout in milliseconds. Default value is 500 ms
kafka.timeout=100