|
| 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