|
| 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 | + "testing" |
| 14 | + |
| 15 | + "github.com/ibm-messaging/mq-golang-jms20/mqjms" |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | +) |
| 18 | + |
| 19 | +/* |
| 20 | + * Test the behaviour of message groups. |
| 21 | + * |
| 22 | + * JMSXGroupID |
| 23 | + * JMSXGroupSeq |
| 24 | + * JMS_IBM_Last_Msg_In_Group |
| 25 | + * |
| 26 | + * https://www.ibm.com/docs/en/ibm-mq/9.2?topic=ordering-grouping-logical-messages |
| 27 | + */ |
| 28 | +func TestMessageGroup(t *testing.T) { |
| 29 | + |
| 30 | + // Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory |
| 31 | + cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles() |
| 32 | + assert.Nil(t, cfErr) |
| 33 | + |
| 34 | + // Creates a connection to the queue manager, using defer to close it automatically |
| 35 | + // at the end of the function (if it was created successfully) |
| 36 | + context, ctxErr := cf.CreateContext() |
| 37 | + assert.Nil(t, ctxErr) |
| 38 | + if context != nil { |
| 39 | + defer context.Close() |
| 40 | + } |
| 41 | + |
| 42 | + // Set up objects for send/receive |
| 43 | + queue := context.CreateQueue("DEV.QUEUE.1") |
| 44 | + consumer, errCons := context.CreateConsumer(queue) |
| 45 | + if consumer != nil { |
| 46 | + defer consumer.Close() |
| 47 | + } |
| 48 | + assert.Nil(t, errCons) |
| 49 | + |
| 50 | + // Since we need more work to support the "set" operations (see big comment below) |
| 51 | + // lets just do a short test of the "get" behaviour. |
| 52 | + |
| 53 | + txtMsg1 := context.CreateTextMessage() |
| 54 | + |
| 55 | + // Force the population of the MQMD field. |
| 56 | + myFormat := "MYFMT" |
| 57 | + txtMsg1.SetStringProperty("JMS_IBM_Format", &myFormat) |
| 58 | + |
| 59 | + groupId, err := txtMsg1.GetStringProperty("JMSXGroupID") |
| 60 | + assert.Nil(t, err) |
| 61 | + assert.Nil(t, groupId) |
| 62 | + |
| 63 | + groupSeq, err := txtMsg1.GetIntProperty("JMSXGroupSeq") |
| 64 | + assert.Nil(t, err) |
| 65 | + assert.Equal(t, 1, groupSeq) |
| 66 | + |
| 67 | + gotLastMsg, err := txtMsg1.GetBooleanProperty("JMS_IBM_Last_Msg_In_Group") |
| 68 | + assert.Equal(t, false, gotLastMsg) |
| 69 | + |
| 70 | + myGroup := "hello" |
| 71 | + err = txtMsg1.SetStringProperty("JMSXGroupID", &myGroup) |
| 72 | + assert.NotNil(t, err) |
| 73 | + assert.Equal(t, "Not yet implemented", err.GetLinkedError().Error()) |
| 74 | + |
| 75 | + err = txtMsg1.SetIntProperty("JMSXGroupSeq", 2) |
| 76 | + assert.NotNil(t, err) |
| 77 | + assert.Equal(t, "Not yet implemented", err.GetLinkedError().Error()) |
| 78 | + |
| 79 | + err = txtMsg1.SetBooleanProperty("JMS_IBM_Last_Msg_In_Group", true) |
| 80 | + assert.NotNil(t, err) |
| 81 | + assert.Equal(t, "Not yet implemented", err.GetLinkedError().Error()) |
| 82 | + |
| 83 | + /* |
| 84 | + * Setting these properties requires an MQMD V2 header and is also |
| 85 | + * not supported for PUT1 operations so there is some more extensive |
| 86 | + * implementation work required in order to enable the "set" scenarios |
| 87 | + * for these Group properties. |
| 88 | +
|
| 89 | + // Create a TextMessage and check that we can populate it |
| 90 | + txtMsg1 := context.CreateTextMessage() |
| 91 | + txtMsg1.SetText(msgBody) |
| 92 | + txtMsg1.SetStringProperty("JMSXGroupID", &groupID) |
| 93 | + txtMsg1.SetIntProperty("JMSXGroupSeq", 1) |
| 94 | + errSend := producer.Send(queue, txtMsg1) |
| 95 | + assert.Nil(t, errSend) |
| 96 | +
|
| 97 | + txtMsg2 := context.CreateTextMessage() |
| 98 | + txtMsg2.SetText(msgBody) |
| 99 | + txtMsg2.SetStringProperty("JMSXGroupID", &groupID) |
| 100 | + txtMsg2.SetIntProperty("JMSXGroupSeq", 2) |
| 101 | + errSend = producer.Send(queue, txtMsg2) |
| 102 | + assert.Nil(t, errSend) |
| 103 | +
|
| 104 | + txtMsg3 := context.CreateTextMessage() |
| 105 | + txtMsg3.SetText(msgBody) |
| 106 | + txtMsg3.SetStringProperty("JMSXGroupID", &groupID) |
| 107 | + txtMsg3.SetIntProperty("JMSXGroupSeq", 3) |
| 108 | + txtMsg3.SetBooleanProperty("JMS_IBM_Last_Msg_In_Group", true) |
| 109 | + errSend = producer.Send(queue, txtMsg3) |
| 110 | + assert.Nil(t, errSend) |
| 111 | +
|
| 112 | + // Check the first message. |
| 113 | + rcvMsg, errRvc := consumer.ReceiveNoWait() |
| 114 | + assert.Nil(t, errRvc) |
| 115 | + assert.NotNil(t, rcvMsg) |
| 116 | + assert.Equal(t, txtMsg1.GetJMSMessageID(), rcvMsg.GetJMSMessageID()) |
| 117 | + gotGroupIDValue, gotErr := rcvMsg.GetStringProperty("JMSXGroupID") |
| 118 | + assert.Nil(t, gotErr) |
| 119 | + assert.Equal(t, groupID, *gotGroupIDValue) |
| 120 | + gotSeqValue, gotErr := rcvMsg.GetIntProperty("JMSXGroupSeq") |
| 121 | + assert.Equal(t, 1, gotSeqValue) |
| 122 | + gotLastMsgValue, gotErr := rcvMsg.GetBooleanProperty("JMS_IBM_Last_Msg_In_Group") |
| 123 | + assert.Equal(t, false, gotLastMsgValue) |
| 124 | +
|
| 125 | + // Check the second message. |
| 126 | + rcvMsg, errRvc = consumer.ReceiveNoWait() |
| 127 | + assert.Nil(t, errRvc) |
| 128 | + assert.NotNil(t, rcvMsg) |
| 129 | + assert.Equal(t, txtMsg2.GetJMSMessageID(), rcvMsg.GetJMSMessageID()) |
| 130 | + gotGroupIDValue, gotErr = rcvMsg.GetStringProperty("JMSXGroupID") |
| 131 | + assert.Nil(t, gotErr) |
| 132 | + assert.Equal(t, groupID, *gotGroupIDValue) |
| 133 | + gotSeqValue, gotErr = rcvMsg.GetIntProperty("JMSXGroupSeq") |
| 134 | + assert.Equal(t, 2, gotSeqValue) |
| 135 | + gotLastMsgValue, gotErr = rcvMsg.GetBooleanProperty("JMS_IBM_Last_Msg_In_Group") |
| 136 | + assert.Equal(t, false, gotLastMsgValue) |
| 137 | +
|
| 138 | + // Check the third message. |
| 139 | + rcvMsg, errRvc = consumer.ReceiveNoWait() |
| 140 | + assert.Nil(t, errRvc) |
| 141 | + assert.NotNil(t, rcvMsg) |
| 142 | + assert.Equal(t, txtMsg3.GetJMSMessageID(), rcvMsg.GetJMSMessageID()) |
| 143 | + gotGroupIDValue, gotErr = rcvMsg.GetStringProperty("JMSXGroupID") |
| 144 | + assert.Nil(t, gotErr) |
| 145 | + assert.Equal(t, groupID, *gotGroupIDValue) |
| 146 | + gotSeqValue, gotErr = rcvMsg.GetIntProperty("JMSXGroupSeq") |
| 147 | + assert.Equal(t, 3, gotSeqValue) |
| 148 | + gotLastMsgValue, gotErr = rcvMsg.GetBooleanProperty("JMS_IBM_Last_Msg_In_Group") |
| 149 | + assert.Equal(t, true, gotLastMsgValue) |
| 150 | + */ |
| 151 | + |
| 152 | +} |
0 commit comments