Skip to content

Commit

Permalink
Merge pull request #13 from ayeshLK/ibmmq-tests
Browse files Browse the repository at this point in the history
Add IBM MQ queue related test cases
  • Loading branch information
ayeshLK authored Oct 31, 2023
2 parents 3c854f4 + 2625d46 commit c3ad93a
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 13 deletions.
48 changes: 35 additions & 13 deletions ballerina/tests/pub_sub_tests.bal
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
// under the License.
import ballerina/test;

@test:Config {}
@test:Config {
groups: ["ibmmqTopic"]
}
function basicPublisherSubscriberTest() returns error? {
QueueManager queueManager = check new QueueManager(name = "QM1", host = "localhost", channel = "DEV.APP.SVRCONN");
Topic subscriber = check queueManager.accessTopic("dev", "DEV.BASE.TOPIC", OPEN_AS_SUBSCRIPTION, MQSO_CREATE);
Expand All @@ -34,7 +36,9 @@ function basicPublisherSubscriberTest() returns error? {
check queueManager.disconnect();
}

@test:Config {}
@test:Config {
groups: ["ibmmqTopic"]
}
function pubSubMultipleMessagesInOrderTest() returns error? {
QueueManager queueManager = check new QueueManager(name = "QM1", host = "localhost", channel = "DEV.APP.SVRCONN");
Topic subscriber = check queueManager.accessTopic("dev", "DEV.BASE.TOPIC", OPEN_AS_SUBSCRIPTION, MQSO_CREATE);
Expand All @@ -45,7 +49,7 @@ function pubSubMultipleMessagesInOrderTest() returns error? {
});
}
foreach int i in 0 ... 4 {
Message? message = check subscriber->get(waitInterval = 2);
Message? message = check subscriber->get(options = MQGMO_WAIT, waitInterval = 5);
if message !is () {
test:assertEquals(string:fromBytes(message.payload), i.toString());
} else {
Expand All @@ -57,15 +61,17 @@ function pubSubMultipleMessagesInOrderTest() returns error? {
check queueManager.disconnect();
}

@test:Config {}
@test:Config {
groups: ["ibmmqTopic"]
}
function subscribeWithFiniteTimeoutTest() returns error? {
QueueManager queueManager = check new QueueManager(name = "QM1", host = "localhost", channel = "DEV.APP.SVRCONN");
Topic subscriber = check queueManager.accessTopic("dev", "DEV.BASE.TOPIC", OPEN_AS_SUBSCRIPTION, MQSO_CREATE);
Topic publisher = check queueManager.accessTopic("dev", "DEV.BASE.TOPIC", OPEN_AS_PUBLICATION, MQOO_OUTPUT);
check publisher->put({
payload: "Hello World".toBytes()
});
Message? message = check subscriber->get(waitInterval = 5);
Message? message = check subscriber->get(options = MQGMO_WAIT, waitInterval = 5);
if message !is () {
test:assertEquals(string:fromBytes(message.payload), "Hello World");
} else {
Expand All @@ -76,7 +82,9 @@ function subscribeWithFiniteTimeoutTest() returns error? {
check queueManager.disconnect();
}

@test:Config {}
@test:Config {
groups: ["ibmmqTopic"]
}
function subscribeWithoutPublishTest() returns error? {
QueueManager queueManager = check new QueueManager(name = "QM1", host = "localhost", channel = "DEV.APP.SVRCONN");
Topic subscriber = check queueManager.accessTopic("dev", "DEV.BASE.TOPIC", OPEN_AS_SUBSCRIPTION, MQSO_CREATE);
Expand All @@ -86,7 +94,9 @@ function subscribeWithoutPublishTest() returns error? {
check queueManager.disconnect();
}

@test:Config {}
@test:Config {
groups: ["ibmmqTopic"]
}
function publishToNonExistingTopicTest() returns error? {
QueueManager queueManager = check new QueueManager(name = "QM1", host = "localhost", channel = "DEV.APP.SVRCONN");
Topic|Error result = queueManager.accessTopic("dev", "NON.EXISTING.TOPIC", OPEN_AS_PUBLICATION, MQOO_OUTPUT);
Expand All @@ -100,7 +110,9 @@ function publishToNonExistingTopicTest() returns error? {
check queueManager.disconnect();
}

@test:Config {}
@test:Config {
groups: ["ibmmqTopic"]
}
function subscribeToNonExistingTopicTest() returns error? {
QueueManager queueManager = check new QueueManager(name = "QM1", host = "localhost", channel = "DEV.APP.SVRCONN");
Topic|Error result = queueManager.accessTopic("dev", "NON.EXISTING.TOPIC", OPEN_AS_SUBSCRIPTION, MQSO_CREATE);
Expand All @@ -114,7 +126,9 @@ function subscribeToNonExistingTopicTest() returns error? {
check queueManager.disconnect();
}

@test:Config {}
@test:Config {
groups: ["ibmmqTopic"]
}
function subscribeWithInvalidTopicNameTest() returns error? {
QueueManager queueManager = check new QueueManager(name = "QM1", host = "localhost", channel = "DEV.APP.SVRCONN");
Topic|Error result = queueManager.accessTopic("dev", "INVALID TOPIC", OPEN_AS_SUBSCRIPTION, MQSO_CREATE);
Expand All @@ -128,7 +142,9 @@ function subscribeWithInvalidTopicNameTest() returns error? {
check queueManager.disconnect();
}

@test:Config {}
@test:Config {
groups: ["ibmmqTopic"]
}
function publishWithInvalidTopicNameTest() returns error? {
QueueManager queueManager = check new QueueManager(name = "QM1", host = "localhost", channel = "DEV.APP.SVRCONN");
Topic|Error result = queueManager.accessTopic("dev", "INVALID TOPIC", OPEN_AS_PUBLICATION, MQOO_OUTPUT);
Expand All @@ -142,7 +158,9 @@ function publishWithInvalidTopicNameTest() returns error? {
check queueManager.disconnect();
}

@test:Config {}
@test:Config {
groups: ["ibmmqTopic"]
}
function accessTopicAfterQMDisconnectTest() returns error? {
QueueManager queueManager = check new QueueManager(name = "QM1", host = "localhost", channel = "DEV.APP.SVRCONN");
check queueManager.disconnect();
Expand All @@ -157,7 +175,9 @@ function accessTopicAfterQMDisconnectTest() returns error? {
}
}

@test:Config {}
@test:Config {
groups: ["ibmmqTopic"]
}
function putToTopicAfterTopicCloseTest() returns error? {
QueueManager queueManager = check new QueueManager(name = "QM1", host = "localhost", channel = "DEV.APP.SVRCONN");
Topic publisher = check queueManager.accessTopic("dev", "DEV.BASE.TOPIC", OPEN_AS_PUBLICATION, MQOO_OUTPUT);
Expand All @@ -176,7 +196,9 @@ function putToTopicAfterTopicCloseTest() returns error? {
check queueManager.disconnect();
}

@test:Config {}
@test:Config {
groups: ["ibmmqTopic"]
}
function putToTopicAfterQMDisconnectTest() returns error? {
QueueManager queueManager = check new QueueManager(name = "QM1", host = "localhost", channel = "DEV.APP.SVRCONN");
Topic publisher = check queueManager.accessTopic("dev", "DEV.BASE.TOPIC", OPEN_AS_PUBLICATION, MQOO_OUTPUT);
Expand Down
139 changes: 139 additions & 0 deletions ballerina/tests/queue_producer_consumer_tests.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// Copyright (c) 2023, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
//
// WSO2 LLC. 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.

import ballerina/test;

@test:Config {
groups: ["ibmmqQueue"]
}
function basicQueueProducerConsumerTest() returns error? {
QueueManager queueManager = check new QueueManager(name = "QM1", host = "localhost", channel = "DEV.APP.SVRCONN");
Queue producer = check queueManager.accessQueue("DEV.QUEUE.1", MQOO_OUTPUT);
Queue consumer = check queueManager.accessQueue("DEV.QUEUE.1", MQOO_INPUT_AS_Q_DEF);
check producer->put({
payload: "Hello World".toBytes()
});
Message? message = check consumer->get();
if message !is () {
test:assertEquals(string:fromBytes(message.payload), "Hello World");
} else {
test:assertFail("Expected a value for message");
}
check producer->close();
check consumer->close();
check queueManager.disconnect();
}

@test:Config {
groups: ["ibmmqQueue"]
}
function pubSubMultipleMessagesQueueProducerConsumerTest() returns error? {
QueueManager queueManager = check new QueueManager(name = "QM1", host = "localhost", channel = "DEV.APP.SVRCONN");
Queue producer = check queueManager.accessQueue("DEV.QUEUE.1", MQOO_OUTPUT);
Queue consumer = check queueManager.accessQueue("DEV.QUEUE.1", MQOO_INPUT_AS_Q_DEF);
foreach int i in 0 ... 4 {
check producer->put({
payload: i.toString().toBytes()
});
}
foreach int i in 0 ... 4 {
Message? message = check consumer->get(options = MQGMO_WAIT, waitInterval = 2);
if message !is () {
test:assertEquals(string:fromBytes(message.payload), i.toString());
} else {
test:assertFail("Expected a value for message");
}
}
check producer->close();
check consumer->close();
check queueManager.disconnect();
}

@test:Config {
groups: ["ibmmqQueue"]
}
function consumerWithoutProducingMessageTest() returns error? {
QueueManager queueManager = check new QueueManager(name = "QM1", host = "localhost", channel = "DEV.APP.SVRCONN");
Queue consumer = check queueManager.accessQueue("DEV.QUEUE.1", MQOO_INPUT_AS_Q_DEF);
Message|Error? result = consumer->get(options = MQGMO_WAIT, waitInterval = 5);
test:assertTrue(result is ());
check consumer->close();
check queueManager.disconnect();
}

@test:Config {
groups: ["ibmmqQueue"]
}
function produceToNonExistingQueueTest() returns error? {
QueueManager queueManager = check new QueueManager(name = "QM1", host = "localhost", channel = "DEV.APP.SVRCONN");
Queue|Error result = queueManager.accessQueue("NON.EXISTING.QUEUE", MQOO_OUTPUT);
if result is Error {
test:assertEquals(result.message(), "Error occurred while accessing queue: MQJE001: Completion Code '2', Reason '2085'.");
test:assertEquals(result.detail().reasonCode, 2085);
test:assertEquals(result.detail().completionCode, 2);
} else {
test:assertFail("Expected an error");
}
check queueManager.disconnect();
}

@test:Config {
groups: ["ibmmqQueue"]
}
function consumerConnectToNonExistingQueueTest() returns error? {
QueueManager queueManager = check new QueueManager(name = "QM1", host = "localhost", channel = "DEV.APP.SVRCONN");
Queue|Error result = queueManager.accessQueue("NON.EXISTING.QUEUE", MQOO_INPUT_AS_Q_DEF);
if result is Error {
test:assertEquals(result.message(), "Error occurred while accessing queue: MQJE001: Completion Code '2', Reason '2085'.");
test:assertEquals(result.detail().reasonCode, 2085);
test:assertEquals(result.detail().completionCode, 2);
} else {
test:assertFail("Expected an error");
}
check queueManager.disconnect();
}

@test:Config {
groups: ["ibmmqQueue"]
}
function produceWithInvalidQueueNameTest() returns error? {
QueueManager queueManager = check new QueueManager(name = "QM1", host = "localhost", channel = "DEV.APP.SVRCONN");
Queue|Error result = queueManager.accessQueue("INVALID QUEUE", MQOO_INPUT_AS_Q_DEF);
if result is Error {
test:assertEquals(result.message(), "Error occurred while accessing queue: MQJE001: Completion Code '2', Reason '2085'.");
test:assertEquals(result.detail().reasonCode, 2085);
test:assertEquals(result.detail().completionCode, 2);
} else {
test:assertFail("Expected an error");
}
check queueManager.disconnect();
}

@test:Config {
groups: ["ibmmqQueue"]
}
function consumeFromAnInvalidQueueNameTest() returns error? {
QueueManager queueManager = check new QueueManager(name = "QM1", host = "localhost", channel = "DEV.APP.SVRCONN");
Queue|Error result = queueManager.accessQueue("INVALID QUEUE", MQOO_INPUT_AS_Q_DEF);
if result is Error {
test:assertEquals(result.message(), "Error occurred while accessing queue: MQJE001: Completion Code '2', Reason '2085'.");
test:assertEquals(result.detail().reasonCode, 2085);
test:assertEquals(result.detail().completionCode, 2);
} else {
test:assertFail("Expected an error");
}
check queueManager.disconnect();
}

0 comments on commit c3ad93a

Please sign in to comment.