PageRenderTime 22ms CodeModel.GetById 8ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 23package org.mobicents.protocols.smpp.util;
 24
 25import static org.testng.Assert.assertEquals;
 26import static org.testng.Assert.assertTrue;
 27import static org.testng.Assert.fail;
 28
 29import org.testng.annotations.Test;
 30
 31import org.mobicents.protocols.smpp.BadCommandIDException;
 32import org.mobicents.protocols.smpp.SMPPRuntimeException;
 33import org.mobicents.protocols.smpp.message.CommandId;
 34import org.mobicents.protocols.smpp.message.SMPPPacket;
 35import org.mobicents.protocols.smpp.util.PacketFactory;
 36
 37@Test
 38public class PacketFactoryTest {
 39
 40    public static final int VENDOR_ID = 0x10201;
 41    private final int[] allIds = new int[] {
 42            CommandId.ALERT_NOTIFICATION,
 43            CommandId.BIND_RECEIVER,
 44            CommandId.BIND_RECEIVER_RESP,
 45            CommandId.BIND_TRANSCEIVER,
 46            CommandId.BIND_TRANSCEIVER_RESP,
 47            CommandId.BIND_TRANSMITTER,
 48            CommandId.BIND_TRANSMITTER_RESP,
 49            CommandId.BROADCAST_SM,
 50            CommandId.BROADCAST_SM_RESP,
 51            CommandId.CANCEL_BROADCAST_SM,
 52            CommandId.CANCEL_BROADCAST_SM_RESP,
 53            CommandId.CANCEL_SM,
 54            CommandId.CANCEL_SM_RESP,
 55            CommandId.DATA_SM,
 56            CommandId.DATA_SM_RESP,
 57            CommandId.DELIVER_SM,
 58            CommandId.DELIVER_SM_RESP,
 59            CommandId.ENQUIRE_LINK,
 60            CommandId.ENQUIRE_LINK_RESP,
 61            CommandId.GENERIC_NACK,
 62            CommandId.OUTBIND,
 63            CommandId.PARAM_RETRIEVE,
 64            CommandId.PARAM_RETRIEVE_RESP,
 65            CommandId.QUERY_BROADCAST_SM,
 66            CommandId.QUERY_BROADCAST_SM_RESP,
 67            CommandId.QUERY_LAST_MSGS,
 68            CommandId.QUERY_LAST_MSGS_RESP,
 69            CommandId.QUERY_MSG_DETAILS,
 70            CommandId.QUERY_MSG_DETAILS_RESP,
 71            CommandId.QUERY_SM,
 72            CommandId.QUERY_SM_RESP,
 73            CommandId.REPLACE_SM,
 74            CommandId.REPLACE_SM_RESP,
 75            CommandId.SUBMIT_MULTI,
 76            CommandId.SUBMIT_MULTI_RESP,
 77            CommandId.SUBMIT_SM,
 78            CommandId.SUBMIT_SM_RESP,
 79            CommandId.UNBIND,
 80            CommandId.UNBIND_RESP,
 81    };
 82    private PacketFactory packetFactory = new PacketFactory();
 83    
 84    public void testCreatePackets() throws Exception {
 85        for (int id : allIds) {
 86            packetFactory.newInstance(id);
 87        }
 88    }
 89    
 90    public void testCreateResponses() throws Exception {
 91        for (int id : allIds) {
 92            SMPPPacket p = packetFactory.newInstance(id);
 93            if (p.isResponse()) {
 94                continue;
 95            }
 96            // Commands that have no responses
 97            if (id == CommandId.ALERT_NOTIFICATION || id == CommandId.OUTBIND) {
 98                continue;
 99            }
100            p.setSequenceNum(89);
101            SMPPPacket o = packetFactory.newResponse(p);
102            assertEquals(o.getCommandId(), id | 0x80000000);
103            assertEquals(o.getSequenceNum(), p.getSequenceNum());
104        }
105    }
106    
107    public void testCreateResponseFailsWithResponse() throws Exception {
108        for (int id : allIds) {
109            if ((id & 0x80000000) == 0) {
110                continue;
111            }
112            SMPPPacket p = packetFactory.newInstance(id);
113            try {
114                packetFactory.newResponse(p);
115                fail("Should not create a response to a response.");
116            } catch (SMPPRuntimeException x) {
117                // Pass
118            }
119        }
120    }
121    
122    public void testCustomCommand() throws Exception {
123        try {
124            packetFactory.newInstance(VENDOR_ID);
125            fail("Vendor ID should not be recognized yet.");
126        } catch (BadCommandIDException x) {
127            // Pass
128        }
129        packetFactory.registerVendorPacket(
130                VENDOR_ID, VendorRequest.class, VendorResponse.class);
131        SMPPPacket packet = packetFactory.newInstance(VENDOR_ID);
132        assertTrue(packet instanceof VendorRequest);
133        assertEquals(packet.getCommandId(), VENDOR_ID);
134        
135        packet = packetFactory.newInstance(VENDOR_ID | 0x80000000);
136        assertTrue(packet instanceof VendorResponse);
137        assertEquals(packet.getCommandId(), VENDOR_ID | 0x80000000);
138        
139        packet = packetFactory.newInstance(VENDOR_ID);
140        packet.setSequenceNum(101);
141        SMPPPacket response = packetFactory.newResponse(packet);
142        assertTrue(response instanceof VendorResponse);
143        assertEquals(response.getSequenceNum(), 101);
144    }
145}
146
147class VendorRequest extends SMPPPacket {
148    private static final long serialVersionUID = 1L;
149    VendorRequest() {
150        super(PacketFactoryTest.VENDOR_ID);
151    }
152}
153class VendorResponse extends SMPPPacket {
154    private static final long serialVersionUID = 1L;
155    public VendorResponse() {
156        super(PacketFactoryTest.VENDOR_ID | 0x80000000);
157    }
158    public VendorResponse(SMPPPacket request) {
159        super(request);
160    }
161}