Skip to content

Commit 2413e0a

Browse files
committed
Functional tests
1 parent 304c2b2 commit 2413e0a

9 files changed

+1160
-0
lines changed

connectionfactory_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright (c) IBM Corporation 2019
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*/
10+
package main
11+
12+
import (
13+
"github.com/ibm-messaging/mq-golang-jms20/mqjms"
14+
"github.com/stretchr/testify/assert"
15+
"log"
16+
"os"
17+
"testing"
18+
)
19+
20+
/*
21+
* Test the ability to populate a ConnectionFactory from contents of the two
22+
* JSON files stored on the file system.
23+
*/
24+
func TestLoadCFFromJSON(t *testing.T) {
25+
26+
// Loads CF parameters from connection_info.json and apiKey.json in the Downloads directory
27+
cf, err := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
28+
29+
if err != nil {
30+
log.Fatal(err)
31+
}
32+
33+
// Creates a connection to the queue manager, using defer to close it automatically
34+
// at the end of the function (if it was created successfully)
35+
context, errCtx := cf.CreateContext()
36+
if context != nil {
37+
defer context.Close()
38+
}
39+
40+
if errCtx != nil {
41+
log.Fatal(errCtx)
42+
}
43+
44+
// Equivalent to a JNDI lookup or other declarative definition
45+
queue := context.CreateQueue("DEV.QUEUE.1")
46+
47+
// Send a message!
48+
context.CreateProducer().SendString(queue, "My first message")
49+
50+
// Clean up the message to avoid problems with other tests.
51+
consumer, errCons := context.CreateConsumer(queue)
52+
53+
if errCons != nil {
54+
log.Fatal(errCons)
55+
}
56+
57+
consumer.ReceiveStringBodyNoWait()
58+
consumer.Close()
59+
60+
}
61+
62+
/*
63+
* Illustrate the ability to populate a ConnectionFactory object using code
64+
* of the application developer's choosing, which could be extended to implement
65+
* other helper methods for creating ConnectionFactory objects.
66+
*/
67+
func TestProgrammaticConnFactory(t *testing.T) {
68+
69+
// Initialise the attributes of the CF in whatever way you like
70+
cf := mqjms.ConnectionFactoryImpl{
71+
QMName: "QM_ONE",
72+
Hostname: "random.hostname.com",
73+
PortNumber: 1414,
74+
ChannelName: "SYSTEM.APP.SVRCONN",
75+
UserName: os.Getenv("MQ_SAMP_USERID"),
76+
Password: os.Getenv("MQ_SAMP_PASSWORD"),
77+
}
78+
79+
assert.NotNil(t, cf)
80+
81+
}

deliverymode_test.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright (c) IBM Corporation 2019
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*/
10+
package main
11+
12+
import (
13+
"fmt"
14+
"github.com/ibm-messaging/mq-golang-jms20/jms20subset"
15+
"github.com/ibm-messaging/mq-golang-jms20/mqjms"
16+
"github.com/stretchr/testify/assert"
17+
"testing"
18+
)
19+
20+
/*
21+
* Test the ability to send both Persistent and Non-Persistent messages.
22+
*/
23+
func TestDeliveryMode(t *testing.T) {
24+
25+
// Loads CF parameters from connection_info.json and apiKey.json in the Downloads directory
26+
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
27+
assert.Nil(t, cfErr)
28+
29+
// Creates a connection to the queue manager, using defer to close it automatically
30+
// at the end of the function (if it was created successfully)
31+
context, ctxErr := cf.CreateContext()
32+
assert.Nil(t, ctxErr)
33+
if context != nil {
34+
defer context.Close()
35+
}
36+
37+
// Equivalent to a JNDI lookup or other declarative definition
38+
queue := context.CreateQueue("DEV.QUEUE.1")
39+
40+
// First, check the queue is empty
41+
consumer, conErr := context.CreateConsumer(queue)
42+
assert.Nil(t, conErr)
43+
if consumer != nil {
44+
defer consumer.Close()
45+
}
46+
47+
expectedNilMsg, rcvErr := consumer.ReceiveStringBodyNoWait()
48+
assert.Nil(t, rcvErr)
49+
assert.Nil(t, expectedNilMsg)
50+
if expectedNilMsg != nil {
51+
fmt.Println("Unexpected message with body: " + *expectedNilMsg)
52+
}
53+
54+
// Check the default behaviour if you don't set anything, which is that
55+
// messages are send as Persistent.
56+
tmpProducer := context.CreateProducer()
57+
assert.Equal(t, jms20subset.DeliveryMode_PERSISTENT, tmpProducer.GetDeliveryMode())
58+
msgBody := "DeliveryMode test default"
59+
errDef := tmpProducer.SendString(queue, msgBody)
60+
assert.Nil(t, errDef)
61+
defMsg, errDef2 := consumer.ReceiveNoWait()
62+
assert.Nil(t, errDef2)
63+
assert.NotNil(t, defMsg)
64+
assert.Equal(t, jms20subset.DeliveryMode_PERSISTENT, defMsg.GetJMSDeliveryMode())
65+
66+
// We have a "Message" object and can use a switch to safely test it is our expected TextMessage
67+
switch msg := defMsg.(type) {
68+
case jms20subset.TextMessage:
69+
assert.Equal(t, msgBody, *msg.GetText())
70+
default:
71+
assert.Fail(t, "Got something other than a text message")
72+
}
73+
74+
// Check the getter/setter behaviour on the Producer
75+
tmpProducer = context.CreateProducer().SetDeliveryMode(jms20subset.DeliveryMode_PERSISTENT)
76+
assert.Equal(t, jms20subset.DeliveryMode_PERSISTENT, tmpProducer.GetDeliveryMode())
77+
tmpProducer.SetDeliveryMode(jms20subset.DeliveryMode_NON_PERSISTENT)
78+
assert.Equal(t, jms20subset.DeliveryMode_NON_PERSISTENT, tmpProducer.GetDeliveryMode())
79+
80+
// Create a Producer and set the delivery mode to Persistent
81+
msgBody = "DeliveryMode test P"
82+
err := context.CreateProducer().SetDeliveryMode(jms20subset.DeliveryMode_PERSISTENT).SendString(queue, msgBody)
83+
assert.Nil(t, err)
84+
85+
pMsg, err2 := consumer.ReceiveNoWait()
86+
assert.Nil(t, err2)
87+
assert.NotNil(t, pMsg)
88+
89+
assert.Equal(t, jms20subset.DeliveryMode_PERSISTENT, pMsg.GetJMSDeliveryMode())
90+
91+
// Create a Producer and set the delivery mode to NonPersistent
92+
msgBody = "DeliveryMode test NP"
93+
err3 := context.CreateProducer().SetDeliveryMode(jms20subset.DeliveryMode_NON_PERSISTENT).SendString(queue, msgBody)
94+
assert.Nil(t, err3)
95+
96+
npMsg, err4 := consumer.ReceiveNoWait()
97+
assert.Nil(t, err4)
98+
assert.NotNil(t, npMsg)
99+
100+
assert.Equal(t, jms20subset.DeliveryMode_NON_PERSISTENT, npMsg.GetJMSDeliveryMode())
101+
102+
// We have a "Message" object and can use a switch to safely test it is our expected TextMessage
103+
switch msg := npMsg.(type) {
104+
case jms20subset.TextMessage:
105+
assert.Equal(t, msgBody, *msg.GetText())
106+
default:
107+
assert.Fail(t, "Got something other than a text message")
108+
}
109+
110+
}

getbycorrelid_test.go

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
/*
2+
* Copyright (c) IBM Corporation 2019
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*/
10+
package main
11+
12+
import (
13+
"github.com/ibm-messaging/mq-golang-jms20/jms20subset"
14+
"github.com/ibm-messaging/mq-golang-jms20/mqjms"
15+
"github.com/stretchr/testify/assert"
16+
"testing"
17+
)
18+
19+
/*
20+
* Test receiving a specific message from a queue using its CorrelationID
21+
*/
22+
func TestGetByCorrelID(t *testing.T) {
23+
24+
// Loads CF parameters from connection_info.json and apiKey.json in the Downloads directory
25+
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
26+
assert.Nil(t, cfErr)
27+
28+
// Creates a connection to the queue manager, using defer to close it automatically
29+
// at the end of the function (if it was created successfully)
30+
context, ctxErr := cf.CreateContext()
31+
assert.Nil(t, ctxErr)
32+
if context != nil {
33+
defer context.Close()
34+
}
35+
36+
// First, check the queue is empty
37+
queue := context.CreateQueue("DEV.QUEUE.1")
38+
consumer, conErr := context.CreateConsumer(queue)
39+
assert.Nil(t, conErr)
40+
if consumer != nil {
41+
defer consumer.Close()
42+
}
43+
reqMsgTest, err := consumer.ReceiveNoWait()
44+
assert.Nil(t, err)
45+
assert.Nil(t, reqMsgTest)
46+
47+
// Put a couple of messages before the one we're aiming to get back
48+
context.CreateProducer().SendString(queue, "One")
49+
context.CreateProducer().SendString(queue, "Two")
50+
51+
myCorrelID := "MyCorrelID"
52+
myMsgThreeStr := "Three"
53+
sentMsg := context.CreateTextMessageWithString(myMsgThreeStr)
54+
sentMsg.SetJMSCorrelationID(myCorrelID)
55+
err = context.CreateProducer().Send(queue, sentMsg)
56+
assert.Nil(t, err)
57+
sentMsgID := sentMsg.GetJMSMessageID()
58+
59+
// Put a couple of messages after the one we're aiming to get back
60+
context.CreateProducer().SendString(queue, "Four")
61+
context.CreateProducer().SendString(queue, "Five")
62+
63+
// Create the consumer to read by CorrelID
64+
correlIDConsumer, correlErr := context.CreateConsumerWithSelector(queue, "JMSCorrelationID = '"+myCorrelID+"'")
65+
assert.Nil(t, correlErr)
66+
gotCorrelMsg, correlGetErr := correlIDConsumer.ReceiveNoWait()
67+
68+
// Clean up the remaining messages from the queue before we start checking if
69+
// we got the right one back.
70+
var cleanupMsg jms20subset.Message
71+
for ok := true; ok; ok = (cleanupMsg != nil) {
72+
cleanupMsg, err = consumer.ReceiveNoWait()
73+
}
74+
75+
// Now do the comparisons
76+
assert.Nil(t, correlGetErr)
77+
assert.NotNil(t, gotCorrelMsg)
78+
gotMsgID := gotCorrelMsg.GetJMSMessageID()
79+
assert.Equal(t, sentMsgID, gotMsgID)
80+
81+
switch msg := gotCorrelMsg.(type) {
82+
case jms20subset.TextMessage:
83+
assert.Equal(t, myMsgThreeStr, *msg.GetText())
84+
default:
85+
assert.Fail(t, "Got something other than a text message")
86+
}
87+
88+
}
89+
90+
/*
91+
* Test that errors are returned for invalid selector strings.
92+
*/
93+
func TestSelectorParsing(t *testing.T) {
94+
95+
// Loads CF parameters from connection_info.json and apiKey.json in the Downloads directory
96+
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
97+
assert.Nil(t, cfErr)
98+
99+
// Creates a connection to the queue manager, using defer to close it automatically
100+
// at the end of the function (if it was created successfully)
101+
context, ctxErr := cf.CreateContext()
102+
assert.Nil(t, ctxErr)
103+
if context != nil {
104+
defer context.Close()
105+
}
106+
107+
queue := context.CreateQueue("DEV.QUEUE.1")
108+
109+
// Creating a consumer with an empty string selector is equivalent to creating
110+
// a consumer without a selector - should succeed.
111+
noSelectorConsumer, noSelectorErr := context.CreateConsumerWithSelector(queue, "")
112+
assert.Nil(t, noSelectorErr)
113+
assert.NotNil(t, noSelectorConsumer)
114+
_, noSelectorErr = noSelectorConsumer.ReceiveNoWait()
115+
noSelectorConsumer.Close()
116+
assert.Nil(t, noSelectorErr)
117+
118+
// Check that we can create a consumer with a CorrelID that matches a messageID
119+
// which is used in request/reply scenarios.
120+
correlIDConsumer, correlIDErr := context.CreateConsumerWithSelector(queue, "JMSCorrelationID = '414d5120514d312020202020202020201017155c0255b621'")
121+
assert.Nil(t, correlIDErr)
122+
assert.NotNil(t, correlIDConsumer)
123+
124+
// Check that we get an appropriate error when trying to create a consumer with
125+
// a selector that is not (yet) supported.
126+
msgIDConsumer, msgIDErr := context.CreateConsumerWithSelector(queue, "JMSMessageID = 'ID:1234'")
127+
assert.NotNil(t, msgIDErr)
128+
assert.Nil(t, msgIDConsumer)
129+
130+
// Check that we get an appropriate error when trying to create a consumer with
131+
// a malformed selector.
132+
fail1Consumer, fail1Err := context.CreateConsumerWithSelector(queue, "JMSCorrelationID")
133+
assert.NotNil(t, fail1Err)
134+
assert.Nil(t, fail1Consumer)
135+
136+
// Check that we get an appropriate error when trying to create a consumer with
137+
// a malformed selector.
138+
fail2Consumer, fail2Err := context.CreateConsumerWithSelector(queue, "JMSCorrelationID = ")
139+
assert.NotNil(t, fail2Err)
140+
assert.Nil(t, fail2Consumer)
141+
142+
// Check that we get an appropriate error when trying to create a consumer with
143+
// a malformed selector.
144+
fail3Consumer, fail3Err := context.CreateConsumerWithSelector(queue, "JMSCorrelationID = '")
145+
assert.NotNil(t, fail3Err)
146+
assert.Nil(t, fail3Consumer)
147+
148+
// Check that we get an appropriate error when trying to create a consumer with
149+
// a malformed selector.
150+
fail4Consumer, fail4Err := context.CreateConsumerWithSelector(queue, "JMSCorrelationID = ''")
151+
assert.NotNil(t, fail4Err)
152+
assert.Nil(t, fail4Consumer)
153+
154+
}
155+
156+
/*
157+
* Test that we can round trip various correlation IDs into and out of the
158+
* message object successfully.
159+
*/
160+
func TestCorrelIDParsing(t *testing.T) {
161+
162+
// Loads CF parameters from connection_info.json and apiKey.json in the Downloads directory
163+
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
164+
assert.Nil(t, cfErr)
165+
166+
// Creates a connection to the queue manager, using defer to close it automatically
167+
// at the end of the function (if it was created successfully)
168+
context, ctxErr := cf.CreateContext()
169+
assert.Nil(t, ctxErr)
170+
if context != nil {
171+
defer context.Close()
172+
}
173+
174+
msg := context.CreateTextMessage()
175+
assert.Equal(t, "", msg.GetJMSCorrelationID())
176+
177+
msg.SetJMSCorrelationID("")
178+
assert.Equal(t, "", msg.GetJMSCorrelationID())
179+
180+
testCorrel := "Hello World"
181+
msg.SetJMSCorrelationID(testCorrel)
182+
assert.Equal(t, testCorrel, msg.GetJMSCorrelationID())
183+
184+
testCorrel = " "
185+
msg.SetJMSCorrelationID(testCorrel)
186+
assert.Equal(t, testCorrel, msg.GetJMSCorrelationID())
187+
188+
testCorrel = "010203040506"
189+
msg.SetJMSCorrelationID(testCorrel)
190+
assert.Equal(t, testCorrel, msg.GetJMSCorrelationID())
191+
192+
testCorrel = "ThisIsAVeryLongCorrelationIDWhichIsMoreThanTwentyFourCharacters"
193+
msg.SetJMSCorrelationID(testCorrel)
194+
assert.Equal(t, testCorrel[0:24], msg.GetJMSCorrelationID())
195+
196+
// MessageID format
197+
testCorrel = "414d5120514d312020202020202020201017155c0255b621"
198+
msg.SetJMSCorrelationID(testCorrel)
199+
assert.Equal(t, testCorrel, msg.GetJMSCorrelationID())
200+
201+
}

0 commit comments

Comments
 (0)