Skip to content

Commit 1a59cf3

Browse files
committed
Unit tests added
Resolves: #102
1 parent fdfc215 commit 1a59cf3

22 files changed

+1015
-48
lines changed

pom.xml

Lines changed: 67 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<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">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
34
<modelVersion>4.0.0</modelVersion>
45
<groupId>ru.cinimex</groupId>
56
<artifactId>mq-java-exporter</artifactId>
@@ -12,55 +13,11 @@
1213
<snakeyaml.version>1.23</snakeyaml.version>
1314
<prometheus.version>0.6.0</prometheus.version>
1415
<log4j.version>2.11.2</log4j.version>
16+
<junit.jupiter.version>5.4.2</junit.jupiter.version>
17+
<junit.platform.version>1.3.2</junit.platform.version>
1518
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1619
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
1720
</properties>
18-
<dependencies>
19-
<dependency>
20-
<groupId>com.ibm.mq</groupId>
21-
<artifactId>com.ibm.mq.allclient</artifactId>
22-
<version>${mq.allclient.version}</version>
23-
<exclusions>
24-
<exclusion>
25-
<groupId>org.bouncycastle</groupId>
26-
<artifactId>bcprov-jdk15on</artifactId>
27-
</exclusion>
28-
<exclusion>
29-
<groupId>org.bouncycastle</groupId>
30-
<artifactId>bcpkix-jdk15on</artifactId>
31-
</exclusion>
32-
<exclusion>
33-
<groupId>javax.jms</groupId>
34-
<artifactId>javax.jms-api</artifactId>
35-
</exclusion>
36-
</exclusions>
37-
</dependency>
38-
<dependency>
39-
<groupId>org.yaml</groupId>
40-
<artifactId>snakeyaml</artifactId>
41-
<version>${snakeyaml.version}</version>
42-
</dependency>
43-
<dependency>
44-
<groupId>io.prometheus</groupId>
45-
<artifactId>simpleclient</artifactId>
46-
<version>${prometheus.version}</version>
47-
</dependency>
48-
<dependency>
49-
<groupId>io.prometheus</groupId>
50-
<artifactId>simpleclient_common</artifactId>
51-
<version>${prometheus.version}</version>
52-
</dependency>
53-
<dependency>
54-
<groupId>org.apache.logging.log4j</groupId>
55-
<artifactId>log4j-api</artifactId>
56-
<version>${log4j.version}</version>
57-
</dependency>
58-
<dependency>
59-
<groupId>org.apache.logging.log4j</groupId>
60-
<artifactId>log4j-core</artifactId>
61-
<version>${log4j.version}</version>
62-
</dependency>
63-
</dependencies>
6421
<build>
6522
<finalName>mq_exporter</finalName>
6623
<plugins>
@@ -112,6 +69,69 @@
11269
</execution>
11370
</executions>
11471
</plugin>
72+
<plugin>
73+
<groupId>org.apache.maven.plugins</groupId>
74+
<artifactId>maven-surefire-plugin</artifactId>
75+
<version>2.22.1</version>
76+
</plugin>
11577
</plugins>
11678
</build>
79+
<dependencies>
80+
<dependency>
81+
<groupId>com.ibm.mq</groupId>
82+
<artifactId>com.ibm.mq.allclient</artifactId>
83+
<version>${mq.allclient.version}</version>
84+
<exclusions>
85+
<exclusion>
86+
<groupId>org.bouncycastle</groupId>
87+
<artifactId>bcprov-jdk15on</artifactId>
88+
</exclusion>
89+
<exclusion>
90+
<groupId>org.bouncycastle</groupId>
91+
<artifactId>bcpkix-jdk15on</artifactId>
92+
</exclusion>
93+
<exclusion>
94+
<groupId>javax.jms</groupId>
95+
<artifactId>javax.jms-api</artifactId>
96+
</exclusion>
97+
</exclusions>
98+
</dependency>
99+
<dependency>
100+
<groupId>org.yaml</groupId>
101+
<artifactId>snakeyaml</artifactId>
102+
<version>${snakeyaml.version}</version>
103+
</dependency>
104+
<dependency>
105+
<groupId>io.prometheus</groupId>
106+
<artifactId>simpleclient</artifactId>
107+
<version>${prometheus.version}</version>
108+
</dependency>
109+
<dependency>
110+
<groupId>io.prometheus</groupId>
111+
<artifactId>simpleclient_common</artifactId>
112+
<version>${prometheus.version}</version>
113+
</dependency>
114+
<dependency>
115+
<groupId>org.apache.logging.log4j</groupId>
116+
<artifactId>log4j-api</artifactId>
117+
<version>${log4j.version}</version>
118+
</dependency>
119+
<dependency>
120+
<groupId>org.apache.logging.log4j</groupId>
121+
<artifactId>log4j-core</artifactId>
122+
<version>${log4j.version}</version>
123+
</dependency>
124+
<dependency>
125+
<groupId>org.junit.jupiter</groupId>
126+
<artifactId>junit-jupiter-api</artifactId>
127+
<version>${junit.jupiter.version}</version>
128+
<scope>test</scope>
129+
</dependency>
130+
<dependency>
131+
<groupId>org.junit.jupiter</groupId>
132+
<artifactId>junit-jupiter-engine</artifactId>
133+
<version>${junit.jupiter.version}</version>
134+
<scope>test</scope>
135+
</dependency>
136+
</dependencies>
117137
</project>

src/main/java/ru/cinimex/exporter/mq/pcf/PCFElementRow.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ru.cinimex.exporter.mq.pcf;
22

3+
import com.ibm.mq.constants.MQConstants;
4+
35
/**
46
* This class represents part of the PCF message, which could be received from
57
* $SYS/MQ/INFO/QMGR/{QMGR_NAME}/Monitor/{CLASS}/{TYPE} MQ topic.
@@ -45,4 +47,9 @@ public int getRowDatatype() {
4547
public String getRowDesc() {
4648
return rowDesc;
4749
}
50+
51+
@Override
52+
public String toString() {
53+
return "PCFElementRow{rowId='" + rowId + "', rowDatatype='" + MQConstants.lookup(rowDatatype, "MQIAMO_MONITOR.*") + "', rowDesc='" + rowDesc + "'}";
54+
}
4855
}

src/main/java/ru/cinimex/exporter/prometheus/metrics/MetricManagerUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ private static Double defaultConversion(List<Double> params) {
9797

9898
/**
9999
* @param parsedQuery - parameter, needed for getting metric in special family metrics
100-
* @param updatedMetricName - metric name, than will be updated
100+
* @param updatedMetricName - metric name, that will be updated
101101
* @return map with label list as key and double parameter list used for conversion function as value
102102
*/
103103
public static Map<List<String>, List<Double>> getMetricsUsedToUpdate(Set<String> parsedQuery, String updatedMetricName) {
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package ru.cinimex.exporter;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
import ru.cinimex.exporter.mq.MQSecurityProperties;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
13+
public class ConfigTest {
14+
private Config config;
15+
16+
@BeforeEach
17+
void initConfigFile() {
18+
config = new Config("src/test/resources/valid_config.yaml");
19+
}
20+
21+
@Test
22+
void testValidConfigFile() {
23+
List<String> queues = new ArrayList<>();
24+
queues.add("QUEUE1");
25+
queues.add("QUEUE2");
26+
27+
List<String> listeners = new ArrayList<>();
28+
listeners.add("LISTENER01");
29+
30+
List<String> channels = new ArrayList<>();
31+
channels.add("MANAGEMENT.CHANNEL");
32+
33+
Assertions.assertAll(
34+
35+
//Asserts for MQ connection params
36+
37+
() -> assertEquals("QM", config.getQmgrName()),
38+
() -> assertEquals("hostname", config.getQmgrHost()),
39+
() -> assertEquals(1414, config.getQmgrPort()),
40+
() -> assertEquals("SYSTEM.DEF.SVRCONN", config.getQmgrChannel()),
41+
() -> assertEquals("mqm", config.getUser()),
42+
() -> assertEquals("mqmpass", config.getPassword()),
43+
() -> assertEquals(true, config.useMqscp()),
44+
() -> assertEquals(12000, config.getConnTimeout()),
45+
46+
//Asserts for prometheus endpoint params
47+
48+
() -> assertEquals("/metrics", config.getEndpURL()),
49+
() -> assertEquals(8080, config.getEndpPort()),
50+
51+
//Asserts for PCFParameters
52+
53+
() -> assertEquals(true, config.sendPCFCommands()),
54+
() -> assertEquals(true, config.usePCFWildcards()),
55+
() -> assertEquals(10, config.getScrapeInterval()),
56+
57+
//Asserts for monitored objects
58+
59+
() -> assertEquals(config.getQueues(), queues),
60+
() -> assertEquals(config.getListeners(), listeners),
61+
() -> assertEquals(config.getChannels(), channels)
62+
);
63+
64+
}
65+
66+
@Test
67+
void getMqSecurityProperties() {
68+
MQSecurityProperties props = config.getMqSecurityProperties();
69+
Assertions.assertAll(
70+
() -> assertEquals(true, props.isUseTLS()),
71+
() -> assertEquals("src/test/resources/keystores/keystore.jks", props.getKeystorePath()),
72+
() -> assertEquals("testpass2", props.getKeystorePassword()),
73+
() -> assertEquals("src/test/resources/keystores/truststore.jks", props.getTruststorePath()),
74+
() -> assertEquals("testpass2", props.getTruststorePassword()),
75+
() -> assertEquals("TLSv1.2", props.getSslProtocol()),
76+
() -> assertEquals("TLS_RSA_WITH_AES_256_CBC_SHA256", props.getCipherSuite())
77+
);
78+
79+
}
80+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package ru.cinimex.exporter.mq;
2+
3+
import com.ibm.mq.MQException;
4+
import com.ibm.mq.constants.MQConstants;
5+
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
import ru.cinimex.exporter.Config;
9+
10+
import java.util.Hashtable;
11+
import java.util.Map;
12+
13+
import static org.junit.jupiter.api.Assertions.assertEquals;
14+
15+
/**
16+
* Although we do not have the ability to mock IBM MQ, we'll just check exceptions correctness.
17+
*/
18+
19+
class MQConnectionTest {
20+
private MQConnection connection;
21+
private Config config;
22+
private String host;
23+
private int port;
24+
private String channel;
25+
private String qmName;
26+
private String user;
27+
private String password;
28+
private boolean useMQCSP;
29+
30+
31+
@BeforeEach
32+
void setConnection() {
33+
connection = new MQConnection();
34+
config = new Config("src/test/resources/valid_config.yaml");
35+
qmName = config.getQmgrName();
36+
host = config.getQmgrHost();
37+
port = config.getQmgrPort();
38+
channel = config.getQmgrChannel();
39+
user = config.getUser();
40+
password = config.getPassword();
41+
useMQCSP = config.useMqscp();
42+
}
43+
44+
@Test
45+
void createMQConnectionParams() {
46+
Map<String, Object> params = MQConnection.createMQConnectionParams(config);
47+
Assertions.assertAll(
48+
() -> assertEquals(MQConstants.TRANSPORT_MQSERIES_CLIENT, params.get(MQConstants.TRANSPORT_PROPERTY)),
49+
() -> assertEquals(host, params.get(MQConstants.HOST_NAME_PROPERTY)),
50+
() -> assertEquals(port, params.get(MQConstants.PORT_PROPERTY)),
51+
() -> assertEquals(channel, params.get(MQConstants.CHANNEL_PROPERTY)),
52+
() -> assertEquals(user, params.get(MQConstants.USER_ID_PROPERTY)),
53+
() -> assertEquals(password, params.get(MQConstants.PASSWORD_PROPERTY)),
54+
() -> assertEquals(useMQCSP, params.get(MQConstants.USE_MQCSP_AUTHENTICATION_PROPERTY))
55+
);
56+
}
57+
58+
@Test
59+
void establish() {
60+
Map<String, Object> params = MQConnection.createMQConnectionParams(config);
61+
Assertions.assertThrows(MQException.class, () -> connection.establish(qmName, params));
62+
}
63+
64+
@Test
65+
void establish1() {
66+
Map<String, Object> params = MQConnection.createMQConnectionParams(config);
67+
Assertions.assertThrows(MQException.class, () -> connection.establish(qmName, params));
68+
}
69+
70+
71+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package ru.cinimex.exporter.mq;
2+
3+
import com.ibm.mq.constants.MQConstants;
4+
import com.ibm.mq.pcf.PCFMessage;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
class MQObjectTest {
11+
private MQObject queue;
12+
private MQObject channel;
13+
private MQObject listener;
14+
15+
@BeforeEach
16+
void initObjects() {
17+
queue = new MQObject("queue", MQObject.MQType.QUEUE);
18+
channel = new MQObject("channel", MQObject.MQType.CHANNEL);
19+
listener = new MQObject("listener", MQObject.MQType.LISTENER);
20+
}
21+
22+
@Test
23+
void objectNameCode() {
24+
assertEquals(MQConstants.MQCA_Q_NAME, MQObject.objectNameCode(MQObject.MQType.QUEUE));
25+
assertEquals(MQConstants.MQCACH_LISTENER_NAME, MQObject.objectNameCode(MQObject.MQType.LISTENER));
26+
assertEquals(MQConstants.MQCACH_CHANNEL_NAME, MQObject.objectNameCode(MQObject.MQType.CHANNEL));
27+
}
28+
29+
@Test
30+
void getName() {
31+
assertEquals("queue", queue.getName());
32+
assertEquals("channel", channel.getName());
33+
assertEquals("listener", listener.getName());
34+
}
35+
36+
@Test
37+
void getPCFHeader() {
38+
assertEquals(MQConstants.MQIA_MAX_Q_DEPTH, queue.getPCFHeader());
39+
assertEquals(MQConstants.MQIACH_CHANNEL_STATUS, channel.getPCFHeader());
40+
assertEquals(MQConstants.MQIACH_LISTENER_STATUS, listener.getPCFHeader());
41+
}
42+
43+
@Test
44+
void getType() {
45+
assertEquals(MQObject.MQType.QUEUE, queue.getType());
46+
assertEquals(MQObject.MQType.CHANNEL, channel.getType());
47+
assertEquals(MQObject.MQType.LISTENER, listener.getType());
48+
}
49+
50+
@Test
51+
void getPcfCmd() {
52+
PCFMessage pcfCmd = new PCFMessage(MQConstants.MQCMD_INQUIRE_Q);
53+
pcfCmd.addParameter(MQConstants.MQCA_Q_NAME, queue.getName());
54+
pcfCmd.addParameter(MQConstants.MQIA_Q_TYPE, MQConstants.MQQT_LOCAL);
55+
assertEquals(pcfCmd, queue.getPcfCmd());
56+
57+
pcfCmd = new PCFMessage(MQConstants.MQCMD_INQUIRE_CHANNEL_STATUS);
58+
pcfCmd.addParameter(MQConstants.MQCACH_CHANNEL_NAME, channel.getName());
59+
assertEquals(MQObject.MQType.CHANNEL, channel.getType());
60+
61+
pcfCmd = new PCFMessage(MQConstants.MQCMD_INQUIRE_LISTENER_STATUS);
62+
pcfCmd.addParameter(MQConstants.MQCACH_LISTENER_NAME, listener.getName());
63+
assertEquals(MQObject.MQType.LISTENER, listener.getType());
64+
}
65+
}

0 commit comments

Comments
 (0)