package com.rendits.router;

/*
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
*/

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertArrayEquals;
import org.junit.Test;

import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.InetAddress;
import java.net.DatagramSocket;
import java.net.DatagramPacket;
import java.net.SocketTimeoutException;
import java.util.Properties;
import java.util.Arrays;
import java.io.IOException;
import net.gcdc.UdpDuplicator;

public class RouterTest {
        private final int MAX_UDP_LENGTH = 256;

        /*
        public RouterTest(String testName) {
                super(testName);
        }


        private void assertArrayEquals(byte[] esperado, byte[] real) {
                assertEquals(Arrays.asList(esperado), Arrays.asList(real));
        }
        */

        private void sendMessages(DatagramSocket socket, InetAddress routerAddress, int portRcvFromVehicle) throws IOException {
                byte[] buffer;
                DatagramPacket packet;
                DatagramPacket rcvPacket;
                int messagesToSend = 1000;
                SimpleCam simpleCam = SampleMessages.getSampleCam();
                SimpleDenm simpleDenm = SampleMessages.getSampleDenm();
                SimpleIclcm simpleIclcm = SampleMessages.getSampleIclcm();
                for (int i = 0; i < messagesToSend; i++) {
                        buffer = simpleCam.asByteArray();
                        packet = new DatagramPacket(buffer, buffer.length);
                        rcvPacket = new DatagramPacket(new byte[buffer.length], buffer.length);
                        packet.setPort(portRcvFromVehicle);
                        packet.setAddress(routerAddress);
                        socket.send(packet);
                        socket.receive(rcvPacket);
                        assertEquals(simpleCam, new SimpleCam(rcvPacket.getData()));

                        buffer = simpleDenm.asByteArray();
                        packet = new DatagramPacket(buffer, buffer.length);
                        rcvPacket = new DatagramPacket(new byte[buffer.length], buffer.length);
                        packet.setPort(portRcvFromVehicle);
                        packet.setAddress(routerAddress);
                        socket.send(packet);
                        socket.receive(rcvPacket);
                        assertEquals(simpleDenm, new SimpleDenm(rcvPacket.getData()));

                        buffer = simpleIclcm.asByteArray();
                        packet = new DatagramPacket(buffer, buffer.length);
                        rcvPacket = new DatagramPacket(new byte[buffer.length], buffer.length);
                        packet.setPort(portRcvFromVehicle);
                        packet.setAddress(routerAddress);
                        socket.send(packet);
                        socket.receive(rcvPacket);
                        assertArrayEquals(buffer, rcvPacket.getData());
                        // assertEquals(simpleIclcm, new SimpleIclcm(rcvPacket.getData()));
                }
                return;
        }

        @Test
        public void testIntegrity() throws IOException {
                int portRcvFromVehicle = 5000;
                int portSendIts = 5001;
                int localPortForUdpLinkLayer = 4000;
                int remotePortForUdpLinkLayer = 4001;
                String remoteAddressForUdpLinkLayer = "127.0.0.1";
                String vehicleAddress = "127.0.0.1";

                /* Setup properties */
                Properties props = new Properties();
                props.setProperty("portRcvFromVehicle", "" + portRcvFromVehicle);
                props.setProperty("portSendCam", "" + portSendIts);
                props.setProperty("portSendDenm", "" + portSendIts);
                props.setProperty("portSendIclcm", "" + portSendIts);
                props.setProperty("receiveThreads", "1");
                props.setProperty("sendThreads", "1");
                props.setProperty("vehicleAddress", vehicleAddress);
                props.setProperty("localPortForUdpLinkLayer", "" + localPortForUdpLinkLayer);
                props.setProperty("remoteAddressForUdpLinkLayer",
                                  remoteAddressForUdpLinkLayer + ":" + remotePortForUdpLinkLayer);
                props.setProperty("macAddress", "00:00:00:00:00:00");
                props.setProperty("countryCode", "46");

                /* Setup the UDP duplicator */
                UdpDuplicator udpDuplicator = new UdpDuplicator();
                SocketAddress remoteAddress =
                        new InetSocketAddress(vehicleAddress, localPortForUdpLinkLayer);
                udpDuplicator.add(remotePortForUdpLinkLayer, remoteAddress);

                /* Start the router */
                Router router = new Router(props);

                /* Setup sockets */
                DatagramSocket socket = new DatagramSocket(portSendIts);
                socket.setSoTimeout(1000);
                InetAddress routerAddress = InetAddress.getByName("127.0.0.1");

                /* Send some messages and make sure we get the same
                 * thing back. */
                sendMessages(socket, routerAddress, portRcvFromVehicle);

                /* Stop the router */
                router.close();
        }
}

@@ -26,111 +26,8 @@
 r;%0A%0A
-/*%0Aimport junit.framework.Test;%0Aimport junit.framework.TestCase;%0Aimport junit.framework.TestSuite;%0A*/%0A%0A
 impo
@@ -3951,33 +3951,33 @@
 %22sendThreads%22, %22
-1
+3
 %22);%0A            

Test now uses several threads for forwarding to vehicle