/protocols/smpp/src/test/java/org/mobicents/protocols/smpp/util/PacketFactoryTest.java

http://mobicents.googlecode.com/ · Java · 161 lines · 124 code · 13 blank · 24 comment · 10 complexity · 326a5b4d5d4d153b402457b08b022d48 MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2011, Red Hat, Inc. and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.mobicents.protocols.smpp.util;
  23. import static org.testng.Assert.assertEquals;
  24. import static org.testng.Assert.assertTrue;
  25. import static org.testng.Assert.fail;
  26. import org.testng.annotations.Test;
  27. import org.mobicents.protocols.smpp.BadCommandIDException;
  28. import org.mobicents.protocols.smpp.SMPPRuntimeException;
  29. import org.mobicents.protocols.smpp.message.CommandId;
  30. import org.mobicents.protocols.smpp.message.SMPPPacket;
  31. import org.mobicents.protocols.smpp.util.PacketFactory;
  32. @Test
  33. public class PacketFactoryTest {
  34. public static final int VENDOR_ID = 0x10201;
  35. private final int[] allIds = new int[] {
  36. CommandId.ALERT_NOTIFICATION,
  37. CommandId.BIND_RECEIVER,
  38. CommandId.BIND_RECEIVER_RESP,
  39. CommandId.BIND_TRANSCEIVER,
  40. CommandId.BIND_TRANSCEIVER_RESP,
  41. CommandId.BIND_TRANSMITTER,
  42. CommandId.BIND_TRANSMITTER_RESP,
  43. CommandId.BROADCAST_SM,
  44. CommandId.BROADCAST_SM_RESP,
  45. CommandId.CANCEL_BROADCAST_SM,
  46. CommandId.CANCEL_BROADCAST_SM_RESP,
  47. CommandId.CANCEL_SM,
  48. CommandId.CANCEL_SM_RESP,
  49. CommandId.DATA_SM,
  50. CommandId.DATA_SM_RESP,
  51. CommandId.DELIVER_SM,
  52. CommandId.DELIVER_SM_RESP,
  53. CommandId.ENQUIRE_LINK,
  54. CommandId.ENQUIRE_LINK_RESP,
  55. CommandId.GENERIC_NACK,
  56. CommandId.OUTBIND,
  57. CommandId.PARAM_RETRIEVE,
  58. CommandId.PARAM_RETRIEVE_RESP,
  59. CommandId.QUERY_BROADCAST_SM,
  60. CommandId.QUERY_BROADCAST_SM_RESP,
  61. CommandId.QUERY_LAST_MSGS,
  62. CommandId.QUERY_LAST_MSGS_RESP,
  63. CommandId.QUERY_MSG_DETAILS,
  64. CommandId.QUERY_MSG_DETAILS_RESP,
  65. CommandId.QUERY_SM,
  66. CommandId.QUERY_SM_RESP,
  67. CommandId.REPLACE_SM,
  68. CommandId.REPLACE_SM_RESP,
  69. CommandId.SUBMIT_MULTI,
  70. CommandId.SUBMIT_MULTI_RESP,
  71. CommandId.SUBMIT_SM,
  72. CommandId.SUBMIT_SM_RESP,
  73. CommandId.UNBIND,
  74. CommandId.UNBIND_RESP,
  75. };
  76. private PacketFactory packetFactory = new PacketFactory();
  77. public void testCreatePackets() throws Exception {
  78. for (int id : allIds) {
  79. packetFactory.newInstance(id);
  80. }
  81. }
  82. public void testCreateResponses() throws Exception {
  83. for (int id : allIds) {
  84. SMPPPacket p = packetFactory.newInstance(id);
  85. if (p.isResponse()) {
  86. continue;
  87. }
  88. // Commands that have no responses
  89. if (id == CommandId.ALERT_NOTIFICATION || id == CommandId.OUTBIND) {
  90. continue;
  91. }
  92. p.setSequenceNum(89);
  93. SMPPPacket o = packetFactory.newResponse(p);
  94. assertEquals(o.getCommandId(), id | 0x80000000);
  95. assertEquals(o.getSequenceNum(), p.getSequenceNum());
  96. }
  97. }
  98. public void testCreateResponseFailsWithResponse() throws Exception {
  99. for (int id : allIds) {
  100. if ((id & 0x80000000) == 0) {
  101. continue;
  102. }
  103. SMPPPacket p = packetFactory.newInstance(id);
  104. try {
  105. packetFactory.newResponse(p);
  106. fail("Should not create a response to a response.");
  107. } catch (SMPPRuntimeException x) {
  108. // Pass
  109. }
  110. }
  111. }
  112. public void testCustomCommand() throws Exception {
  113. try {
  114. packetFactory.newInstance(VENDOR_ID);
  115. fail("Vendor ID should not be recognized yet.");
  116. } catch (BadCommandIDException x) {
  117. // Pass
  118. }
  119. packetFactory.registerVendorPacket(
  120. VENDOR_ID, VendorRequest.class, VendorResponse.class);
  121. SMPPPacket packet = packetFactory.newInstance(VENDOR_ID);
  122. assertTrue(packet instanceof VendorRequest);
  123. assertEquals(packet.getCommandId(), VENDOR_ID);
  124. packet = packetFactory.newInstance(VENDOR_ID | 0x80000000);
  125. assertTrue(packet instanceof VendorResponse);
  126. assertEquals(packet.getCommandId(), VENDOR_ID | 0x80000000);
  127. packet = packetFactory.newInstance(VENDOR_ID);
  128. packet.setSequenceNum(101);
  129. SMPPPacket response = packetFactory.newResponse(packet);
  130. assertTrue(response instanceof VendorResponse);
  131. assertEquals(response.getSequenceNum(), 101);
  132. }
  133. }
  134. class VendorRequest extends SMPPPacket {
  135. private static final long serialVersionUID = 1L;
  136. VendorRequest() {
  137. super(PacketFactoryTest.VENDOR_ID);
  138. }
  139. }
  140. class VendorResponse extends SMPPPacket {
  141. private static final long serialVersionUID = 1L;
  142. public VendorResponse() {
  143. super(PacketFactoryTest.VENDOR_ID | 0x80000000);
  144. }
  145. public VendorResponse(SMPPPacket request) {
  146. super(request);
  147. }
  148. }