-
Notifications
You must be signed in to change notification settings - Fork 920
/
AMQPMessageTest.java
2916 lines (2243 loc) · 116 KB
/
AMQPMessageTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF 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.
*/
package org.apache.activemq.artemis.protocol.amqp.broker;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.OpenDataException;
import org.apache.activemq.artemis.api.core.ActiveMQBuffer;
import org.apache.activemq.artemis.api.core.ActiveMQBuffers;
import org.apache.activemq.artemis.api.core.ActiveMQException;
import org.apache.activemq.artemis.api.core.ICoreMessage;
import org.apache.activemq.artemis.api.core.RoutingType;
import org.apache.activemq.artemis.api.core.SimpleString;
import org.apache.activemq.artemis.core.message.openmbean.CompositeDataConstants;
import org.apache.activemq.artemis.protocol.amqp.converter.AMQPMessageIdHelper;
import org.apache.activemq.artemis.protocol.amqp.converter.AMQPMessageSupport;
import org.apache.activemq.artemis.protocol.amqp.util.NettyReadable;
import org.apache.activemq.artemis.protocol.amqp.util.NettyWritable;
import org.apache.activemq.artemis.protocol.amqp.util.TLSEncode;
import org.apache.activemq.artemis.reader.MessageUtil;
import org.apache.activemq.artemis.spi.core.protocol.EmbedMessageUtil;
import org.apache.activemq.artemis.utils.RandomUtil;
import org.apache.activemq.artemis.utils.collections.TypedProperties;
import org.apache.qpid.proton.Proton;
import org.apache.qpid.proton.amqp.Binary;
import org.apache.qpid.proton.amqp.Symbol;
import org.apache.qpid.proton.amqp.UnsignedByte;
import org.apache.qpid.proton.amqp.UnsignedInteger;
import org.apache.qpid.proton.amqp.UnsignedLong;
import org.apache.qpid.proton.amqp.messaging.AmqpSequence;
import org.apache.qpid.proton.amqp.messaging.AmqpValue;
import org.apache.qpid.proton.amqp.messaging.ApplicationProperties;
import org.apache.qpid.proton.amqp.messaging.Data;
import org.apache.qpid.proton.amqp.messaging.DeliveryAnnotations;
import org.apache.qpid.proton.amqp.messaging.Footer;
import org.apache.qpid.proton.amqp.messaging.Header;
import org.apache.qpid.proton.amqp.messaging.MessageAnnotations;
import org.apache.qpid.proton.amqp.messaging.Properties;
import org.apache.qpid.proton.amqp.messaging.Section;
import org.apache.qpid.proton.codec.EncoderImpl;
import org.apache.qpid.proton.codec.EncodingCodes;
import org.apache.qpid.proton.codec.ReadableBuffer;
import org.apache.qpid.proton.message.Message;
import org.apache.qpid.proton.message.impl.MessageImpl;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
public class AMQPMessageTest {
private static final String TEST_TO_ADDRESS = "someAddress";
private static final String TEST_MESSAGE_ANNOTATION_KEY = "x-opt-test-annotation";
private static final String TEST_MESSAGE_ANNOTATION_VALUE = "test-annotation";
private static final String TEST_MESSAGE_ANNOTATION_KEY2 = "x-opt-test-annotation2";
private static final String TEST_MESSAGE_ANNOTATION_VALUE2 = "test-annotation2";
private static final String TEST_EXTRA_PROPERTY_KEY1 = "extraPropertyKey1";
private static final String TEST_EXTRA_PROPERTY_VALUE1 = "extraPropertyValue1";
private static final String TEST_EXTRA_PROPERTY_KEY2 = "extraPropertyKey2";
private static final String TEST_EXTRA_PROPERTY_VALUE2 = "extraPropertyValue2";
private static final String TEST_APPLICATION_PROPERTY_KEY = "key-1";
private static final String TEST_APPLICATION_PROPERTY_VALUE = "value-1";
private static final String TEST_APPLICATION_PROPERTY_KEY2 = "key-2";
private static final String TEST_APPLICATION_PROPERTY_VALUE2 = "value-2";
private static final String TEST_STRING_BODY = "test-string-body";
private static final String PROPERTY_MAP_APP_PROPERTIES_PREFIX = "applicationProperties.";
private static final String PROPERTY_MAP_PROPERTIES_PREFIX = "properties.";
private static final String PROPERTY_MAP_MESSAGE_ANNOTATIONS_PREFIX = "messageAnnotations.";
private static final String PROPERTY_MAP_EXTRA_PROPERTIES_PREFIX = "extraProperties.";
private byte[] encodedProtonMessage;
@Before
public void setUp() {
encodedProtonMessage = encodeMessage(createProtonMessage());
}
//----- Test Message Creation ---------------------------------------------//
@Test
public void testCreateMessageFromEncodedByteArrayData() {
// Constructor 1
AMQPStandardMessage decoded = new AMQPStandardMessage(0, encodedProtonMessage, null);
assertTrue(decoded.isDurable());
assertEquals(TEST_TO_ADDRESS, decoded.getAddress());
// Constructor 2
decoded = new AMQPStandardMessage(0, encodedProtonMessage, null, null);
assertTrue(decoded.isDurable());
assertEquals(TEST_TO_ADDRESS, decoded.getAddress());
}
@Test
public void testCreateMessageFromEncodedReadableBuffer() {
AMQPStandardMessage decoded = new AMQPStandardMessage(0, ReadableBuffer.ByteBufferReader.wrap(encodedProtonMessage), null, null);
assertEquals(true, decoded.getHeader().getDurable());
assertEquals(TEST_TO_ADDRESS, decoded.getAddress());
}
@Test
public void testCreateMessageFromEncodedByteArrayDataWithExtraProperties() {
AMQPStandardMessage decoded = new AMQPStandardMessage(0, encodedProtonMessage, new TypedProperties(), null);
assertEquals(true, decoded.getHeader().getDurable());
assertEquals(TEST_TO_ADDRESS, decoded.getAddress());
assertNotNull(decoded.getExtraProperties());
}
@Test
public void testCreateMessageForPersistenceDataReload() throws ActiveMQException {
MessageImpl protonMessage = createProtonMessage();
ActiveMQBuffer encoded = encodeMessageAsPersistedBuffer(protonMessage);
AMQPStandardMessage message = new AMQPStandardMessage(0);
try {
message.getProtonMessage();
fail("Should throw NPE due to not being initialized yet");
} catch (NullPointerException npe) {
}
final long persistedSize = (long) encoded.readableBytes();
// Now reload from encoded data
message.reloadPersistence(encoded, null);
assertEquals(persistedSize, message.getPersistSize());
assertEquals(persistedSize - Integer.BYTES, message.getPersistentSize());
assertEquals(persistedSize - Integer.BYTES, message.getEncodeSize());
assertEquals(true, message.getHeader().getDurable());
assertEquals(TEST_TO_ADDRESS, message.getAddress());
}
@Test
public void testHasScheduledDeliveryTimeReloadPersistence() {
final long scheduledTime = System.currentTimeMillis();
MessageImpl protonMessage = createProtonMessage();
MessageAnnotations annotations = protonMessage.getMessageAnnotations();
annotations.getValue().put(AMQPMessageSupport.SCHEDULED_DELIVERY_TIME, scheduledTime);
ActiveMQBuffer encoded = encodeMessageAsPersistedBuffer(protonMessage);
AMQPMessage message = new AMQPStandardMessage(0);
try {
message.getProtonMessage();
fail("Should throw NPE due to not being initialized yet");
} catch (NullPointerException npe) {
}
Assert.assertEquals(AMQPMessage.MessageDataScanningStatus.NOT_SCANNED, message.getDataScanningStatus());
// Now reload from encoded data
message.reloadPersistence(encoded, null);
Assert.assertEquals(AMQPMessage.MessageDataScanningStatus.RELOAD_PERSISTENCE, message.getDataScanningStatus());
assertTrue(message.hasScheduledDeliveryTime());
Assert.assertEquals(AMQPMessage.MessageDataScanningStatus.RELOAD_PERSISTENCE, message.getDataScanningStatus());
message.getHeader();
Assert.assertEquals(AMQPMessage.MessageDataScanningStatus.SCANNED, message.getDataScanningStatus());
assertTrue(message.hasScheduledDeliveryTime());
}
@Test
public void testHasScheduledDeliveryDelayReloadPersistence() {
final long scheduledDelay = 100000;
MessageImpl protonMessage = createProtonMessage();
MessageAnnotations annotations = protonMessage.getMessageAnnotations();
annotations.getValue().put(AMQPMessageSupport.SCHEDULED_DELIVERY_DELAY, scheduledDelay);
ActiveMQBuffer encoded = encodeMessageAsPersistedBuffer(protonMessage);
AMQPMessage message = new AMQPStandardMessage(0);
try {
message.getProtonMessage();
fail("Should throw NPE due to not being initialized yet");
} catch (NullPointerException npe) {
}
Assert.assertEquals(AMQPMessage.MessageDataScanningStatus.NOT_SCANNED, message.getDataScanningStatus());
// Now reload from encoded data
message.reloadPersistence(encoded, null);
Assert.assertEquals(AMQPMessage.MessageDataScanningStatus.RELOAD_PERSISTENCE, message.getDataScanningStatus());
assertTrue(message.hasScheduledDeliveryTime());
Assert.assertEquals(AMQPMessage.MessageDataScanningStatus.RELOAD_PERSISTENCE, message.getDataScanningStatus());
message.getHeader();
Assert.assertEquals(AMQPMessage.MessageDataScanningStatus.SCANNED, message.getDataScanningStatus());
assertTrue(message.hasScheduledDeliveryTime());
}
@Test
public void testNoScheduledDeliveryTimeOrDelayReloadPersistence() {
MessageImpl protonMessage = createProtonMessage();
ActiveMQBuffer encoded = encodeMessageAsPersistedBuffer(protonMessage);
AMQPMessage message = new AMQPStandardMessage(0);
try {
message.getProtonMessage();
fail("Should throw NPE due to not being initialized yet");
} catch (NullPointerException npe) {
}
Assert.assertEquals(AMQPMessage.MessageDataScanningStatus.NOT_SCANNED, message.getDataScanningStatus());
// Now reload from encoded data
message.reloadPersistence(encoded, null);
Assert.assertEquals(AMQPMessage.MessageDataScanningStatus.RELOAD_PERSISTENCE, message.getDataScanningStatus());
assertFalse(message.hasScheduledDeliveryTime());
Assert.assertEquals(AMQPMessage.MessageDataScanningStatus.RELOAD_PERSISTENCE, message.getDataScanningStatus());
message.getHeader();
Assert.assertEquals(AMQPMessage.MessageDataScanningStatus.SCANNED, message.getDataScanningStatus());
assertFalse(message.hasScheduledDeliveryTime());
}
//----- Test Memory Estimate access ---------------------------------------//
@Test
public void testGetMemoryEstimate() {
AMQPStandardMessage decoded = new AMQPStandardMessage(0, encodedProtonMessage, new TypedProperties(), null);
int estimate = decoded.getMemoryEstimate();
assertTrue(encodedProtonMessage.length < decoded.getMemoryEstimate());
assertEquals(estimate, decoded.getMemoryEstimate());
decoded.putStringProperty(new SimpleString("newProperty"), "newValue");
decoded.reencode();
assertNotEquals(estimate, decoded.getMemoryEstimate());
}
@Test
public void testGetMemoryEstimateWithDecodedApplicationProperties() {
AMQPStandardMessage decoded = new AMQPStandardMessage(0, encodedProtonMessage, new TypedProperties(), null);
AMQPStandardMessage decodedWithApplicationPropertiesUnmarshalled =
new AMQPStandardMessage(0, encodeMessage(createProtonMessage()), new TypedProperties(), null);
assertEquals(decodedWithApplicationPropertiesUnmarshalled.getStringProperty(TEST_APPLICATION_PROPERTY_KEY), TEST_APPLICATION_PROPERTY_VALUE);
assertNotEquals(decodedWithApplicationPropertiesUnmarshalled.getMemoryEstimate(), decoded.getMemoryEstimate());
}
//----- Test Connection ID access -----------------------------------------//
@Test
public void testDecodeMultiThreaded() throws Exception {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
protonMessage.setHeader( new Header());
Properties properties = new Properties();
properties.setTo("someNiceLocal");
protonMessage.setProperties(properties);
protonMessage.getHeader().setDeliveryCount(new UnsignedInteger(7));
protonMessage.getHeader().setDurable(Boolean.TRUE);
protonMessage.setApplicationProperties(new ApplicationProperties(new HashMap<>()));
final AtomicInteger failures = new AtomicInteger(0);
for (int testTry = 0; testTry < 100; testTry++) {
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
Thread[] threads = new Thread[100];
CountDownLatch latchAlign = new CountDownLatch(threads.length);
CountDownLatch go = new CountDownLatch(1);
Runnable run = new Runnable() {
@Override
public void run() {
try {
latchAlign.countDown();
go.await();
Assert.assertNotNull(decoded.getHeader());
// this is a method used by Core Converter
decoded.getProtonMessage();
Assert.assertNotNull(decoded.getHeader());
} catch (Throwable e) {
e.printStackTrace();
failures.incrementAndGet();
}
}
};
for (int i = 0; i < threads.length; i++) {
threads[i] = new Thread(run);
threads[i].start();
}
Assert.assertTrue(latchAlign.await(10, TimeUnit.SECONDS));
go.countDown();
for (Thread thread : threads) {
thread.join(5000);
Assert.assertFalse(thread.isAlive());
}
Assert.assertEquals(0, failures.get());
}
}
@Test
public void testGetConnectionID() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertEquals(null, decoded.getConnectionID());
}
@Test
public void testSetConnectionID() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
final String ID = UUID.randomUUID().toString();
assertEquals(null, decoded.getConnectionID());
decoded.setConnectionID(ID);
assertEquals(ID, decoded.getConnectionID());
}
@Test
public void testGetConnectionIDFromProperties() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
final String ID = UUID.randomUUID().toString();
assertEquals(null, decoded.getConnectionID());
decoded.setConnectionID(ID);
assertEquals(ID, decoded.getConnectionID());
assertEquals(ID, decoded.getStringProperty(MessageUtil.CONNECTION_ID_PROPERTY_NAME));
}
//----- Test LastValueProperty access -------------------------------//
@Test
public void testGetLastValueFromMessageWithNone() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertNull(decoded.getLastValueProperty());
}
@Test
public void testSetLastValueFromMessageWithNone() {
SimpleString lastValue = new SimpleString("last-address");
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertNull(decoded.getLastValueProperty());
decoded.setLastValueProperty(lastValue);
assertEquals(lastValue, decoded.getLastValueProperty());
}
//----- Test User ID access -----------------------------------------//
@Test
public void getUserIDWhenNoPropertiesExists() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertNull(decoded.getUserID());
decoded.setUserID(UUID.randomUUID().toString());
assertNull(decoded.getUserID());
}
@Test
public void testSetUserIDHasNoEffectOnMessagePropertiesWhenNotPresent() {
final String ID = UUID.randomUUID().toString();
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertNull(decoded.getUserID());
assertNull(decoded.getProperties());
decoded.setUserID(ID);
decoded.reencode();
assertNull(decoded.getUserID());
assertNull(decoded.getProperties());
}
@Test
public void testSetUserIDHasNoEffectOnMessagePropertiesWhenPresentButNoMessageID() {
final String ID = UUID.randomUUID().toString();
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
protonMessage.setProperties(new Properties());
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertNull(decoded.getUserID());
assertNotNull(decoded.getProperties());
assertNull(decoded.getProperties().getMessageId());
decoded.setUserID(ID);
decoded.reencode();
assertNull(decoded.getUserID());
assertNotNull(decoded.getProperties());
assertNull(decoded.getProperties().getMessageId());
}
@Test
public void testSetUserIDHasNoEffectOnMessagePropertiesWhenPresentWithMessageID() {
final String ID = UUID.randomUUID().toString();
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
protonMessage.setProperties(new Properties());
protonMessage.setMessageId(ID);
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertNotNull(decoded.getUserID());
assertNotNull(decoded.getProperties());
assertNotNull(decoded.getProperties().getMessageId());
assertEquals(ID, decoded.getUserID());
decoded.setUserID(ID);
decoded.reencode();
assertNotNull(decoded.getUserID());
assertNotNull(decoded.getProperties());
assertNotNull(decoded.getProperties().getMessageId());
assertEquals(ID, decoded.getUserID());
}
//----- Test the getDuplicateProperty methods -----------------------------//
@Test
public void testGetDuplicateProperty() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertEquals(null, decoded.getDuplicateProperty());
}
//----- Test the getAddress methods ---------------------------------------//
@Test
public void testGetAddressFromMessage() {
final String ADDRESS = "myQueue";
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
protonMessage.setHeader(new Header());
protonMessage.setAddress(ADDRESS);
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertEquals(ADDRESS, decoded.getAddress());
}
@Test
public void testGetAddressSimpleStringFromMessage() {
final String ADDRESS = "myQueue";
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
protonMessage.setHeader(new Header());
protonMessage.setAddress(ADDRESS);
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertEquals(ADDRESS, decoded.getAddressSimpleString().toString());
}
@Test
public void testGetAddressFromMessageWithNoValueSet() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertNull(decoded.getAddress());
assertNull(decoded.getAddressSimpleString());
}
@Test
public void testSetAddressFromMessage() {
final String ADDRESS = "myQueue";
final SimpleString NEW_ADDRESS = new SimpleString("myQueue-1");
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
protonMessage.setAddress(ADDRESS);
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertEquals(ADDRESS, decoded.getAddress());
decoded.setAddress(NEW_ADDRESS);
assertEquals(NEW_ADDRESS, decoded.getAddressSimpleString());
}
@Test
public void testSetAddressFromMessageUpdatesPropertiesOnReencode() {
final String ADDRESS = "myQueue";
final SimpleString NEW_ADDRESS = new SimpleString("myQueue-1");
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
protonMessage.setAddress(ADDRESS);
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertEquals(ADDRESS, decoded.getAddress());
decoded.setAddress(NEW_ADDRESS);
decoded.reencode();
assertEquals(NEW_ADDRESS.toString(), decoded.getProperties().getTo());
assertEquals(NEW_ADDRESS, decoded.getAddressSimpleString());
}
//----- Test the durability set and get methods ---------------------------//
@Test
public void testIsDurableFromMessageWithHeaderTaggedAsTrue() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
protonMessage.setHeader(new Header());
protonMessage.setDurable(true);
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertTrue(decoded.isDurable());
}
@Test
public void testIsDurableFromMessageWithHeaderTaggedAsFalse() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
protonMessage.setHeader(new Header());
protonMessage.setDurable(false);
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertFalse(decoded.isDurable());
}
@Test
public void testIsDurableFromMessageWithNoValueSet() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertFalse(decoded.isDurable());
}
@Test
public void testIsDuranleReturnsTrueOnceUpdated() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertFalse(decoded.isDurable());
decoded.setDurable(true);
assertTrue(decoded.isDurable());
}
@Test
public void testNonDurableMessageReencodedToDurable() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
protonMessage.setHeader(new Header());
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertFalse(decoded.isDurable());
// Underlying message data not updated yet
assertNull(decoded.getHeader().getDurable());
decoded.setDurable(true);
decoded.reencode();
assertTrue(decoded.isDurable());
// Underlying message data now updated
assertTrue(decoded.getHeader().getDurable());
}
@Test
public void testMessageWithNoHeaderGetsOneWhenDurableSetAndReencoded() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertFalse(decoded.isDurable());
// Underlying message data not updated yet
assertNull(decoded.getHeader());
decoded.setDurable(true);
decoded.reencode();
assertTrue(decoded.isDurable());
// Underlying message data now updated
Header header = decoded.getHeader();
assertNotNull(header);
assertTrue(header.getDurable());
}
//----- Test RoutingType access -------------------------------------------//
@Test
public void testGetRoutingTypeFromMessageWithoutIt() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertNull(decoded.getRoutingType());
}
@Test
public void testSetRoutingType() {
RoutingType type = RoutingType.ANYCAST;
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertNull(decoded.getRoutingType());
decoded.setRoutingType(type);
assertEquals(type, decoded.getRoutingType());
}
@Test
public void testSetRoutingTypeToClear() {
RoutingType type = RoutingType.ANYCAST;
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertNull(decoded.getRoutingType());
decoded.setRoutingType(type);
assertEquals(type, decoded.getRoutingType());
decoded.setRoutingType(null);
assertNull(decoded.getRoutingType());
}
@Test
public void testRemoveRoutingTypeFromMessageEncodedWithOne() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
MessageAnnotations annotations = new MessageAnnotations(new HashMap<>());
annotations.getValue().put(AMQPMessageSupport.ROUTING_TYPE, RoutingType.ANYCAST.getType());
protonMessage.setMessageAnnotations(annotations);
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertEquals(RoutingType.ANYCAST, decoded.getRoutingType());
decoded.setRoutingType(null);
decoded.reencode();
assertNull(decoded.getRoutingType());
assertTrue(decoded.getMessageAnnotations().getValue().isEmpty());
}
@Test
public void testGetRoutingTypeFromMessageWithAnyCastType() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
MessageAnnotations annotations = new MessageAnnotations(new HashMap<>());
annotations.getValue().put(AMQPMessageSupport.ROUTING_TYPE, RoutingType.ANYCAST.getType());
protonMessage.setMessageAnnotations(annotations);
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertEquals(RoutingType.ANYCAST, decoded.getRoutingType());
}
@Test
public void testGetRoutingTypeFromMessageWithMulticastType() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
MessageAnnotations annotations = new MessageAnnotations(new HashMap<>());
annotations.getValue().put(AMQPMessageSupport.ROUTING_TYPE, RoutingType.MULTICAST.getType());
protonMessage.setMessageAnnotations(annotations);
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertEquals(RoutingType.MULTICAST, decoded.getRoutingType());
}
@Test
public void testGetRoutingTypeFromMessageWithQueueType() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
MessageAnnotations annotations = new MessageAnnotations(new HashMap<>());
annotations.getValue().put(AMQPMessageSupport.JMS_DEST_TYPE_MSG_ANNOTATION, AMQPMessageSupport.QUEUE_TYPE);
protonMessage.setMessageAnnotations(annotations);
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertEquals(RoutingType.ANYCAST, decoded.getRoutingType());
}
@Test
public void testGetRoutingTypeFromMessageWithTempQueueType() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
MessageAnnotations annotations = new MessageAnnotations(new HashMap<>());
annotations.getValue().put(AMQPMessageSupport.JMS_DEST_TYPE_MSG_ANNOTATION, AMQPMessageSupport.TEMP_QUEUE_TYPE);
protonMessage.setMessageAnnotations(annotations);
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertEquals(RoutingType.ANYCAST, decoded.getRoutingType());
}
@Test
public void testGetRoutingTypeFromMessageWithTopicType() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
MessageAnnotations annotations = new MessageAnnotations(new HashMap<>());
annotations.getValue().put(AMQPMessageSupport.JMS_DEST_TYPE_MSG_ANNOTATION, AMQPMessageSupport.TOPIC_TYPE);
protonMessage.setMessageAnnotations(annotations);
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertEquals(RoutingType.MULTICAST, decoded.getRoutingType());
}
@Test
public void testGetRoutingTypeFromMessageWithTempTopicType() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
MessageAnnotations annotations = new MessageAnnotations(new HashMap<>());
annotations.getValue().put(AMQPMessageSupport.JMS_DEST_TYPE_MSG_ANNOTATION, AMQPMessageSupport.TEMP_TOPIC_TYPE);
protonMessage.setMessageAnnotations(annotations);
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertEquals(RoutingType.MULTICAST, decoded.getRoutingType());
}
@Test
public void testGetRoutingTypeFromMessageWithUnknownType() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
MessageAnnotations annotations = new MessageAnnotations(new HashMap<>());
annotations.getValue().put(AMQPMessageSupport.JMS_DEST_TYPE_MSG_ANNOTATION, (byte) 32);
protonMessage.setMessageAnnotations(annotations);
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertNull(decoded.getRoutingType());
}
//----- Test access to message Group ID -----------------------------------//
@Test
public void testGetGroupIDFromMessage() {
final String GROUP_ID = "group-1";
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
protonMessage.setHeader(new Header());
protonMessage.setGroupId(GROUP_ID);
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertEquals(GROUP_ID, decoded.getGroupID().toString());
}
@Test
public void testGetGroupIDFromMessageWithNoGroupId() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
protonMessage.setProperties(new Properties());
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertNull(decoded.getGroupID());
}
@Test
public void testGetGroupIDFromMessageWithNoProperties() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertNull(decoded.getGroupID());
}
//----- Test access to message Group ID -----------------------------------//
@Test
public void testGetReplyToFromMessage() {
final String REPLY_TO = "address-1";
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
protonMessage.setHeader(new Header());
protonMessage.setReplyTo(REPLY_TO);
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertEquals(REPLY_TO, decoded.getReplyTo().toString());
}
@Test
public void testGetReplyToFromMessageWithNoReplyTo() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
protonMessage.setProperties(new Properties());
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertNull(decoded.getReplyTo());
}
@Test
public void testGetReplyToFromMessageWithNoProperties() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertNull(decoded.getReplyTo());
}
@Test
public void testSetReplyToFromMessageWithProperties() {
final String REPLY_TO = "address-1";
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
protonMessage.setProperties(new Properties());
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertNull(decoded.getReplyTo());
decoded.setReplyTo(new SimpleString(REPLY_TO));
decoded.reencode();
assertEquals(REPLY_TO, decoded.getReplyTo().toString());
assertEquals(REPLY_TO, decoded.getProperties().getReplyTo());
}
@Test
public void testSetReplyToFromMessageWithNoProperties() {
final String REPLY_TO = "address-1";
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertNull(decoded.getReplyTo());
decoded.setReplyTo(new SimpleString(REPLY_TO));
decoded.reencode();
assertEquals(REPLY_TO, decoded.getReplyTo().toString());
assertEquals(REPLY_TO, decoded.getProperties().getReplyTo());
}
@Test
public void testSetReplyToFromMessageWithPropertiesCanClear() {
final String REPLY_TO = "address-1";
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
protonMessage.setProperties(new Properties());
protonMessage.setReplyTo(REPLY_TO);
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertEquals(REPLY_TO, decoded.getReplyTo().toString());
decoded.setReplyTo(null);
decoded.reencode();
assertEquals(null, decoded.getReplyTo());
assertEquals(null, decoded.getProperties().getReplyTo());
}
//----- Test access to User ID --------------------------------------------//
@Test
public void testGetUserIDFromMessage() {
final String USER_NAME = "foo";
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
protonMessage.setUserId(USER_NAME.getBytes(StandardCharsets.UTF_8));
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertEquals(USER_NAME, decoded.getAMQPUserID());
}
@Test
public void testGetUserIDFromMessageWithNoProperties() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertNull(decoded.getAMQPUserID());
}
@Test
public void testGetUserIDFromMessageWithNoUserID() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
protonMessage.setProperties(new Properties());
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertNull(decoded.getAMQPUserID());
}
//----- Test access message priority --------------------------------------//
@Test
public void testGetPriorityFromMessage() {
final short PRIORITY = 7;
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
protonMessage.setHeader(new Header());
protonMessage.setPriority(PRIORITY);
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertEquals(PRIORITY, decoded.getPriority());
}
@Test
public void testGetPriorityFromMessageWithNoHeader() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertEquals(AMQPStandardMessage.DEFAULT_MESSAGE_PRIORITY, decoded.getPriority());
}
@Test
public void testGetPriorityFromMessageWithNoPrioritySet() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
protonMessage.setHeader(new Header());
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertEquals(AMQPStandardMessage.DEFAULT_MESSAGE_PRIORITY, decoded.getPriority());
}
@Test
public void testSetPriorityOnMessageWithHeader() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
protonMessage.setHeader(new Header());
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertEquals(AMQPStandardMessage.DEFAULT_MESSAGE_PRIORITY, decoded.getPriority());
decoded.setPriority((byte) 9);
decoded.reencode();
assertEquals(9, decoded.getPriority());
assertEquals(9, decoded.getHeader().getPriority().byteValue());
}
@Test
public void testSetPriorityOnMessageWithoutHeader() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertEquals(AMQPStandardMessage.DEFAULT_MESSAGE_PRIORITY, decoded.getPriority());
decoded.setPriority((byte) 9);
decoded.reencode();
assertEquals(9, decoded.getPriority());
assertEquals(9, decoded.getHeader().getPriority().byteValue());
}
//----- Test access message expiration ------------------------------------//
@Test
public void testGetExpirationFromMessageWithNoHeader() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertEquals(0, decoded.getExpiration());
}
@Test
public void testGetExpirationFromMessageWithNoTTLInHeader() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
protonMessage.setHeader(new Header());
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertEquals(0, decoded.getExpiration());
}
@Test
public void testGetExpirationFromMessageWithNoTTLInHeaderOrExpirationInProperties() {
MessageImpl protonMessage = (MessageImpl) Message.Factory.create();
protonMessage.setHeader(new Header());
protonMessage.setProperties(new Properties());
AMQPStandardMessage decoded = encodeAndDecodeMessage(protonMessage);
assertEquals(0, decoded.getExpiration());
}