package org.jsmpp.util;

import static org.testng.Assert.*;

import org.jsmpp.PDUStringException;
import org.jsmpp.SMPPConstant;
import org.jsmpp.bean.BindResp;
import org.jsmpp.bean.BindType;
import org.testng.annotations.Test;

/**
 * Test composing PDU, decompose and read.
 * @author uudashr
 *
 */
public class ComposerDecomposerReaderTest {
    private static final boolean DEBUG = false;
    
    @Test(groups="checkintest")
    public void lowLevel() {
        PDUComposer composer = new DefaultComposer();
        PDUDecomposer decomposer = new DefaultDecomposer();
        
        byte[] b = null;
        String systemId = "smsc";
        /*
        try {
            b = composer.bindResp(BindType.BIND_TRX.responseCommandId(), 1, systemId);
            assertEquals(b.length, 16 + systemId.length() + 1);
            printLog("Length of bytes : " + b.length);
        } catch (PDUStringException e) {
            fail("Failed composing bind response", e);
        }
        */
        
        PDUByteBuffer buf = new PDUByteBuffer(BindType.BIND_TRX.responseCommandId(), 0, 1);
        buf.append(systemId);
        b = buf.toBytes();
        assertEquals(b.length, 16 + systemId.length() + 1);
        printLog("Length of bytes : " + b.length);
        
        
        SequentialBytesReader reader = new SequentialBytesReader(b);
        assertEquals(b.length, reader.remainBytesLength());
        
        int commandLength =  reader.readInt();
        assertEquals(commandLength, b.length);
        assertEquals(4, reader.cursor);
        assertEquals(b.length - 4, reader.remainBytesLength());
        
        int commandId =  reader.readInt();
        assertEquals(commandId, BindType.BIND_TRX.responseCommandId());
        assertEquals(8, reader.cursor);
        assertEquals(b.length - 8, reader.remainBytesLength());
        
        int commandStatus = reader.readInt();
        assertEquals(commandStatus, SMPPConstant.STAT_ESME_ROK);
        assertEquals(12, reader.cursor);
        assertEquals(b.length - 12, reader.remainBytesLength());
        
        int sequenceNumber = reader.readInt();
        assertEquals(sequenceNumber, 1);
        assertEquals(16, reader.cursor);
        assertEquals(b.length - 16, reader.remainBytesLength());
        
        String readedSystemId = reader.readCString();
        assertEquals(readedSystemId, systemId);
        
        /*
        try {
            BindResp resp = decomposer.bindResp(b);
        } catch (PDUStringException e) {
            fail("Failed decomposing bind response", e);
        }
        */
    }
    
    @Test(groups="checkintest")
    public void highLevel() {
        PDUComposer composer = new DefaultComposer();
        PDUDecomposer decomposer = new DefaultDecomposer();
        
        byte[] b = null;
        String systemId = "smsc";
        BindType bindType = BindType.BIND_TRX;
        try {
            b = composer.bindResp(bindType.responseCommandId(), 1, systemId);
            assertEquals(b.length, 16 + systemId.length() + 1);
            printLog("Length of bytes : " + b.length);
        } catch (PDUStringException e) {
            fail("Failed composing bind response", e);
        }
        
        try {
            BindResp resp = decomposer.bindResp(b);
            assertEquals(resp.getCommandLength(), b.length);
            assertEquals(resp.getCommandId(), bindType.responseCommandId());
            assertEquals(resp.getCommandStatus(), SMPPConstant.STAT_ESME_ROK);
            assertEquals(resp.getSequenceNumber(), 1);
            assertEquals(resp.getSystemId(), systemId);
        } catch (PDUStringException e) {
            fail("Failed decomposing bind response", e);
        }
    }
    
    private static void printLog(String message) {
        if (DEBUG) {
            System.out.println(message);
        }
    }
}

@@ -466,529 +466,58 @@
     
-PDUComposer composer = new DefaultComposer();%0A        PDUDecomposer decomposer = new DefaultDecomposer();%0A        %0A        byte%5B%5D b = null;%0A        String systemId = %22smsc%22;%0A        /*%0A        try %7B%0A            b = composer.bindResp(BindType.BIND_TRX.responseCommandId(), 1, systemId);%0A            assertEquals(b.length, 16 + systemId.length() + 1);%0A            printLog(%22Length of bytes : %22 + b.length);%0A        %7D catch (PDUStringException e) %7B%0A            fail(%22Failed composing bind response%22, e);%0A        %7D%0A        */
+byte%5B%5D b = null;%0A        String systemId = %22smsc%22;
 %0A   
@@ -1908,213 +1908,8 @@
 d);%0A
-        %0A        /*%0A        try %7B%0A            BindResp resp = decomposer.bindResp(b);%0A        %7D catch (PDUStringException e) %7B%0A            fail(%22Failed decomposing bind response%22, e);%0A        %7D%0A        */%0A
     

Clean up the code