|
| 1 | +/* |
| 2 | + * Copyright (c) IBM Corporation 2022 |
| 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 | + "reflect" |
| 15 | + "testing" |
| 16 | + |
| 17 | + "github.com/ibm-messaging/mq-golang-jms20/jms20subset" |
| 18 | + "github.com/ibm-messaging/mq-golang-jms20/mqjms" |
| 19 | + "github.com/stretchr/testify/assert" |
| 20 | +) |
| 21 | + |
| 22 | +/* |
| 23 | + * Test receiving a specific message from a queue using its JMSMessageID |
| 24 | + */ |
| 25 | +func TestGetByMsgID(t *testing.T) { |
| 26 | + |
| 27 | + // Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory |
| 28 | + cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles() |
| 29 | + assert.Nil(t, cfErr) |
| 30 | + |
| 31 | + // Creates a connection to the queue manager, using defer to close it automatically |
| 32 | + // at the end of the function (if it was created successfully) |
| 33 | + context, ctxErr := cf.CreateContext() |
| 34 | + assert.Nil(t, ctxErr) |
| 35 | + if context != nil { |
| 36 | + defer context.Close() |
| 37 | + } |
| 38 | + |
| 39 | + // First, check the queue is empty |
| 40 | + queue := context.CreateQueue("DEV.QUEUE.1") |
| 41 | + consumer, conErr := context.CreateConsumer(queue) |
| 42 | + assert.Nil(t, conErr) |
| 43 | + if consumer != nil { |
| 44 | + defer consumer.Close() |
| 45 | + } |
| 46 | + reqMsgTest, err := consumer.ReceiveNoWait() |
| 47 | + assert.Nil(t, err) |
| 48 | + assert.Nil(t, reqMsgTest) |
| 49 | + |
| 50 | + // Put a couple of messages before the one we're aiming to get back |
| 51 | + producer := context.CreateProducer().SetTimeToLive(10000) |
| 52 | + producer.SendString(queue, "One") |
| 53 | + producer.SendString(queue, "Two") |
| 54 | + |
| 55 | + myMsgThreeStr := "Three" |
| 56 | + sentMsg := context.CreateTextMessageWithString(myMsgThreeStr) |
| 57 | + err = producer.Send(queue, sentMsg) |
| 58 | + assert.Nil(t, err) |
| 59 | + sentMsgID := sentMsg.GetJMSMessageID() |
| 60 | + |
| 61 | + // Put a couple of messages after the one we're aiming to get back |
| 62 | + producer.SendString(queue, "Four") |
| 63 | + producer.SendString(queue, "Five") |
| 64 | + |
| 65 | + // Create the consumer to read by CorrelID |
| 66 | + correlIDConsumer, correlErr := context.CreateConsumerWithSelector(queue, "JMSMessageID = '"+sentMsgID+"'") |
| 67 | + assert.Nil(t, correlErr) |
| 68 | + gotCorrelMsg, correlGetErr := correlIDConsumer.ReceiveNoWait() |
| 69 | + |
| 70 | + // Clean up the remaining messages from the queue before we start checking if |
| 71 | + // we got the right one back. |
| 72 | + var cleanupMsg jms20subset.Message |
| 73 | + for ok := true; ok; ok = (cleanupMsg != nil) { |
| 74 | + cleanupMsg, err = consumer.ReceiveNoWait() |
| 75 | + } |
| 76 | + |
| 77 | + // Now do the comparisons |
| 78 | + assert.Nil(t, correlGetErr) |
| 79 | + assert.NotNil(t, gotCorrelMsg) |
| 80 | + gotMsgID := gotCorrelMsg.GetJMSMessageID() |
| 81 | + assert.Equal(t, sentMsgID, gotMsgID) |
| 82 | + |
| 83 | + switch msg := gotCorrelMsg.(type) { |
| 84 | + case jms20subset.TextMessage: |
| 85 | + assert.Equal(t, myMsgThreeStr, *msg.GetText()) |
| 86 | + default: |
| 87 | + fmt.Println(reflect.TypeOf(gotCorrelMsg)) |
| 88 | + assert.Fail(t, "Got something other than a text message") |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments