Search in sources :

Example 1 with MockTcpConnectionThread

use of com.vodafone360.people.tests.service.transport.tcp.conn_less.hb_tests.MockTcpConnectionThread in project 360-Engine-for-Android by 360.

the class HeartbeatSenderThreadTest method testStopConnection.

@Suppress
@MediumTest
public void testStopConnection() {
    DataOutputStream dos = new DataOutputStream(new ByteArrayOutputStream());
    MockTcpConnectionThread connThread = new MockTcpConnectionThread(new DecoderThread(), null);
    MockHeartbeatSenderThread hbSender = new MockHeartbeatSenderThread(connThread, null, //QUICKFIX: Not sure about this value
    null);
    hbSender.setOutputStream(dos);
    hbSender.startConnection();
    try {
        Thread.sleep(500);
    } catch (Exception e) {
    }
    hbSender.stopConnection();
    try {
        Thread.sleep(1000);
    } catch (Exception e) {
    }
    assertNotNull(hbSender.getConnectionThread());
    if (null != hbSender.getConnectionThread()) {
        assertFalse(hbSender.getConnectionThread().isAlive());
        try {
            dos.write(1);
            fail("Should not be able to write here!");
        } catch (IOException e) {
            assertTrue(true);
        }
    }
}
Also used : DecoderThread(com.vodafone360.people.service.transport.DecoderThread) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) IOException(java.io.IOException) Suppress(android.test.suitebuilder.annotation.Suppress) MediumTest(android.test.suitebuilder.annotation.MediumTest)

Example 2 with MockTcpConnectionThread

use of com.vodafone360.people.tests.service.transport.tcp.conn_less.hb_tests.MockTcpConnectionThread in project 360-Engine-for-Android by 360.

the class HeartbeatSenderThreadTest method testSendHeartbeat_valid.

@MediumTest
public void testSendHeartbeat_valid() {
    byte[] header = { (byte) 0xFF, (byte) 0xFF, (byte) 0x64 };
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    MockTcpConnectionThread connThread = new MockTcpConnectionThread(new DecoderThread(), null);
    MockHeartbeatSenderThread hbSender = new MockHeartbeatSenderThread(connThread, null, //QUICKFIX: Not sure about this value
    null);
    AuthSessionHolder authHolder = new AuthSessionHolder();
    authHolder.userID = 007;
    authHolder.sessionID = "SESS_ID";
    authHolder.sessionSecret = "SESS_SECRET";
    authHolder.userName = "James_Bond";
    LoginEngine.setTestSession(authHolder);
    hbSender.setOutputStream(baos);
    try {
        hbSender.sendHeartbeat();
    } catch (Exception e) {
        fail("sendHeartbeatTest() should not throw an Exception here " + e.toString());
    }
    try {
        baos.flush();
        baos.close();
    } catch (IOException ioe) {
    }
    byte[] payload = baos.toByteArray();
    if (null != payload) {
        boolean isHeaderOk = false, isEndingOk = false, isPayloadLenOk = false;
        if ((header[0] == payload[0]) && (header[1] == payload[1]) && (header[2] == payload[2])) {
            isHeaderOk = true;
        } else {
            fail("RPG Header was malformed!");
        }
        if (payload[payload.length - 1] == ((byte) 0x7A)) {
            isPayloadLenOk = true;
        } else {
            fail("Message End was malformed! Char was: " + payload[payload.length - 1]);
        }
        int payloadSize = byteArrayToInt(new byte[] { payload[11], payload[12], payload[13], payload[14] }, 0);
        if (payloadSize == (payload.length - 16)) {
            isEndingOk = true;
        } else {
            fail("Payload length is not okay: " + payloadSize + " vs. " + (payload.length - 16));
        }
        assertTrue((isHeaderOk && isEndingOk && isPayloadLenOk));
    } else {
        fail("HB-Payload was null!");
    }
}
Also used : AuthSessionHolder(com.vodafone360.people.datatypes.AuthSessionHolder) DecoderThread(com.vodafone360.people.service.transport.DecoderThread) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) IOException(java.io.IOException) MediumTest(android.test.suitebuilder.annotation.MediumTest)

Example 3 with MockTcpConnectionThread

use of com.vodafone360.people.tests.service.transport.tcp.conn_less.hb_tests.MockTcpConnectionThread in project 360-Engine-for-Android by 360.

the class HeartbeatSenderThreadTest method testSendHeartbeat_exception.

@MediumTest
public void testSendHeartbeat_exception() {
    MockByteArrayOutputStream baos = new MockByteArrayOutputStream();
    MockTcpConnectionThread connThread = new MockTcpConnectionThread(new DecoderThread(), null);
    MockHeartbeatSenderThread hbSender = new MockHeartbeatSenderThread(connThread, null, //QUICKFIX: Not sure about this value
    null);
    AuthSessionHolder authHolder = new AuthSessionHolder();
    authHolder.userID = 007;
    authHolder.sessionID = "SESS_ID";
    authHolder.sessionSecret = "SESS_SECRET";
    authHolder.userName = "James_Bond";
    LoginEngine.setTestSession(authHolder);
    hbSender.setOutputStream(baos);
    try {
        baos.close();
    } catch (IOException ioe) {
    }
    // IOException Test
    try {
        hbSender.sendHeartbeat();
    } catch (IOException ioe) {
        // we succeed because we expect an IOE to be thrown here!
        assertTrue(true);
    } catch (Exception e) {
        fail("We should not have received a generic exception, but an IOException!");
    }
    baos = null;
    // NullPointerException Test
    hbSender.setOutputStream(null);
    try {
        hbSender.sendHeartbeat();
    } catch (IOException ioe) {
        fail("We should not have received an IOException, but a generic (NP-)Exception!");
    } catch (Exception e) {
        // we succeed because we expect an IOE to be thrown here!
        assertTrue(true);
    }
}
Also used : AuthSessionHolder(com.vodafone360.people.datatypes.AuthSessionHolder) DecoderThread(com.vodafone360.people.service.transport.DecoderThread) IOException(java.io.IOException) IOException(java.io.IOException) MediumTest(android.test.suitebuilder.annotation.MediumTest)

Example 4 with MockTcpConnectionThread

use of com.vodafone360.people.tests.service.transport.tcp.conn_less.hb_tests.MockTcpConnectionThread in project 360-Engine-for-Android by 360.

the class ResponseReaderThreadTest method testRun_exception.

@Suppress
@MediumTest
public void testRun_exception() {
    DecoderThread decoder = new DecoderThread();
    MockTcpConnectionThread mockThread = new MockTcpConnectionThread(decoder, null);
    MockResponseReaderThread respReader = new MockResponseReaderThread(mockThread, decoder, // QUICKFIX: Not sure about this value
    null);
    MockOTAInputStream mIs = new MockOTAInputStream(new ByteArrayInputStream(new byte[] { 1, 2, 3, 4, 5 }));
    respReader.setInputStream(new BufferedInputStream(mIs));
    // IO Exception test
    try {
        mIs.close();
    } catch (IOException e) {
    }
    respReader.startConnection();
    try {
        Thread.sleep(1000);
    } catch (Exception e) {
    }
    assertTrue(mockThread.getDidErrorOccur());
    respReader.stopConnection();
    mockThread = null;
    respReader = null;
    // NP Exception test
    mockThread = new MockTcpConnectionThread(new DecoderThread(), null);
    respReader = new MockResponseReaderThread(mockThread, decoder, // QUICKFIX: Not sure about this value
    null);
    respReader.setInputStream(null);
    respReader.startConnection();
    try {
        Thread.sleep(1000);
    } catch (Exception e) {
    }
    assertTrue(mockThread.getDidErrorOccur());
    respReader.stopConnection();
    mockThread = null;
    respReader = null;
    // EOF Exception
    mockThread = new MockTcpConnectionThread(new DecoderThread(), null);
    respReader = new MockResponseReaderThread(mockThread, decoder, // QUICKFIX: Not sure about this value
    null);
    ByteArrayInputStream bais = new ByteArrayInputStream(new byte[] { 1 });
    respReader.setInputStream(new BufferedInputStream(bais));
    respReader.startConnection();
    try {
        Thread.sleep(1000);
    } catch (Exception e) {
    }
    assertTrue(mockThread.getDidErrorOccur());
    respReader.stopConnection();
    mockThread = null;
    respReader = null;
}
Also used : DecoderThread(com.vodafone360.people.service.transport.DecoderThread) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedInputStream(java.io.BufferedInputStream) MockTcpConnectionThread(com.vodafone360.people.tests.service.transport.tcp.conn_less.hb_tests.MockTcpConnectionThread) IOException(java.io.IOException) IOException(java.io.IOException) Suppress(android.test.suitebuilder.annotation.Suppress) MediumTest(android.test.suitebuilder.annotation.MediumTest)

Example 5 with MockTcpConnectionThread

use of com.vodafone360.people.tests.service.transport.tcp.conn_less.hb_tests.MockTcpConnectionThread in project 360-Engine-for-Android by 360.

the class ResponseReaderThreadTest method testStartConnection.

@Suppress
@MediumTest
public void testStartConnection() {
    DecoderThread decoder = new DecoderThread();
    MockTcpConnectionThread mockThread = new MockTcpConnectionThread(decoder, null);
    MockResponseReaderThread respReader = new MockResponseReaderThread(mockThread, decoder, // QUICKFIX: Not sure about this value 
    null);
    MockOTAInputStream mIs = new MockOTAInputStream(new ByteArrayInputStream(new byte[] { 1, 2, 3, 4, 5 }));
    respReader.setInputStream(new BufferedInputStream(mIs));
    assertNull(respReader.getConnectionThread());
    respReader.startConnection();
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    }
    assertNotNull(respReader.getConnectionThread());
    if (null != respReader.getConnectionThread()) {
        assertTrue(respReader.getConnectionThread().isAlive());
    }
    assertTrue(respReader.getIsConnectionRunning());
}
Also used : DecoderThread(com.vodafone360.people.service.transport.DecoderThread) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedInputStream(java.io.BufferedInputStream) MockTcpConnectionThread(com.vodafone360.people.tests.service.transport.tcp.conn_less.hb_tests.MockTcpConnectionThread) Suppress(android.test.suitebuilder.annotation.Suppress) MediumTest(android.test.suitebuilder.annotation.MediumTest)

Aggregations

MediumTest (android.test.suitebuilder.annotation.MediumTest)11 DecoderThread (com.vodafone360.people.service.transport.DecoderThread)10 Suppress (android.test.suitebuilder.annotation.Suppress)7 IOException (java.io.IOException)6 MockTcpConnectionThread (com.vodafone360.people.tests.service.transport.tcp.conn_less.hb_tests.MockTcpConnectionThread)5 BufferedInputStream (java.io.BufferedInputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 AuthSessionHolder (com.vodafone360.people.datatypes.AuthSessionHolder)2 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1