package com.antonio.samir.simulator;

import com.antonio.samir.tool.MessageUtil;
import com.antonio.samir.wonderfulredtooth.proxyrecorder.conservation.Message;
import com.antonio.samir.wonderfulredtooth.proxyrecorder.simulator.ClientDeviceSimulator;
import com.antonio.samir.wonderfulredtooth.proxyrecorder.simulator.ClientDeviceSimulatorSequecial;

import junit.framework.Assert;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.apache.commons.lang3.StringUtils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.List;

/**
 * ClientDeviceSimulatorSequecial Tester.
 */
public class ClientDeviceSimulatorSequecialTest extends TestCase {

    ClientDeviceSimulator simulator;

    public ClientDeviceSimulatorSequecialTest(String name) {
        super(name);
    }

    public static Test suite() {
        return new TestSuite(ClientDeviceSimulatorSequecialTest.class);
    }

    public void setUp() throws Exception {
        super.setUp();
        simulator = new ClientDeviceSimulatorSequecial();
    }

    public void tearDown() throws Exception {
        super.tearDown();
    }

    /**
     *
     * Method: start(InputStream input, OutputStream outputStream, List<Message> messageList)
     *
     */
    public void testStart() throws Exception {
        List<Message> msgs = MessageUtil.getSampleMessages();

        final String response = "first_responsesecond_responsegoodbye_response";

        final PipedOutputStream pipedOutputStream = new PipedOutputStream();

        final InputStream input = new PipedInputStream(pipedOutputStream);

        final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        simulator.start(input, outputStream, msgs);

        final String stringResponse = new String(outputStream.toByteArray());

        Assert.assertTrue("Response messages shoud be the same", StringUtils.equals(stringResponse, response));

    }
} 

@@ -153,16 +153,99 @@
 essage;%0A
+import com.antonio.samir.wonderfulredtooth.proxyrecorder.conservation.MessageType;%0A
 import c
@@ -1503,16 +1503,22 @@
         
+final 
 List%3CMes
@@ -1568,89 +1568,8 @@
 );%0A%0A
-        final String response = %22first_responsesecond_responsegoodbye_response%22;%0A
 %0A   
@@ -1635,32 +1635,33 @@
 utputStream();%0A%0A
+%0A
         final In
@@ -1813,140 +1813,580 @@
     
-simulator.start(input, outputStream, msgs);%0A%0A        final String stringResponse = new String(outputStream.toByteArray()
+new Thread() %7B%0A            @Override%0A            public void run() %7B%0A                simulator.start(input, outputStream, msgs);%0A            %7D%0A        %7D.start();%0A%0A        Thread.sleep(1000);%0A%0A        List%3CMessage%3E msgsRequest = MessageUtil.filterMessage(msgs, MessageType.REQUEST);%0A        List%3CMessage%3E msgsResponse = MessageUtil.filterMessage(msgs, MessageType.RESPONSE);%0A%0A        int index = 0;%0A%0A        for (Message msg : msgsResponse) %7B%0A%0A            final byte%5B%5D data = outputStream.toByteArray();%0A            final String outputRequest = new String(data
 );%0A%0A
+%0A
         
 Asse
@@ -2385,25 +2385,298 @@
     
-Assert.assertTrue
+    if (msgsRequest.size() %3C index) %7B%0A%0A                final String stringRightRequest = new String(msgsRequest.get(index).content);%0A%0A                final boolean contains = StringUtils.contains(outputRequest, stringRightRequest);%0A%0A                final String messageError = String.format
 (%22Re
@@ -2695,16 +2695,17 @@
 ges shou
+l
 d be the
@@ -2713,57 +2713,245 @@
 same
-%22, StringUtils.equals(stringResponse, response));
+ %25s %25s%22, outputRequest, stringRightRequest);%0A%0A                Assert.assertTrue(messageError, contains);%0A%0A            %7D%0A%0A            pipedOutputStream.write(msg.content);%0A%0A            Thread.sleep(1500);%0A%0A            index++;%0A%0A        %7D%0A
 %0A%0A  

Test for ClientDeviceSimulatorSequencial done