package com.heartbeat.network.udp;

import com.heartbeat.network.BeatListener;
import com.heartbeat.network.HeartBeatConfiguration;
import org.junit.Test;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
 * Created by serayuzgur on 24/02/16.
 */
public class UDPHeartBeatTest {
    HeartBeatConfiguration conf = new HeartBeatConfiguration() {
        @Override
        public long getInterval() {
            return 2000;
        }

        @Override
        public long getFailedRetryInterval() {
            return 4000;
        }

        @Override
        public int getSocketTimeout() {
            return 1000;
        }

        @Override
        public int getSocketPort() {
            return 9999;
        }

        @Override
        public int getServerPort() {
            return 9998;
        }
    };

    HeartBeatInfo me = new HeartBeatInfo() {

        @Override
        public byte[] serialize() {
            return "TestNode".getBytes();
        }

        @Override
        public byte[] deserialize(byte[] data) {
            return new byte[0];
        }
    };

    @Test
    public void testStart() throws Exception {
        UDPHeartBeat heartBeat = new UDPHeartBeat(conf, me);
        heartBeat.start();
        try {
            DatagramSocket receiver = new DatagramSocket(conf.getServerPort());
            assert new String(UDPOperations.receiveData(receiver)).equals("TestNode");
            heartBeat.stop();
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testStartWithListener() throws Exception {
        UDPHeartBeat heartBeat = new UDPHeartBeat(conf, me, new BeatListener() {
            @Override
            public void onBeat(byte[] response) {
                assert new String(response).equals("TestServer");
            }

            @Override
            public void onError(Exception e) {

            }
        });
        heartBeat.start();
        try {
            DatagramSocket receiver = new DatagramSocket(conf.getServerPort());
            DatagramPacket receivedPacket = UDPOperations.receive(receiver);
            assert new String(receivedPacket.getData()).equals("TestNode");
            UDPOperations.send(receiver, receivedPacket.getAddress(), receivedPacket.getPort(), "TestServer".getBytes());
            heartBeat.stop();
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
@@ -121,24 +121,57 @@
 figuration;%0A
+import org.junit.FixMethodOrder;%0A
 import org.j
@@ -185,36 +185,48 @@
 st;%0A
-%0A
 import 
-java.io.IOException;
+org.junit.runners.MethodSorters;%0A
 %0Aimp
@@ -289,41 +289,8 @@
 ket;
-%0Aimport java.net.SocketException;
 %0A%0A/*
@@ -333,16 +333,62 @@
 16.%0A */%0A
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)%0A
 public c
@@ -1365,34 +1365,16 @@
 tart();%0A
-        try %7B%0A    
         
@@ -1441,36 +1441,32 @@
 ort());%0A        
-  
-  
 assert new Strin
@@ -1524,36 +1524,32 @@
 Node%22);%0A        
-    
 heartBeat.stop()
@@ -1562,147 +1562,26 @@
     
-%7D catch (SocketException e) %7B%0A            e.printStackTrace();%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A        %7D
+receiver.close();%0A
 %0A   
@@ -2014,26 +2014,8 @@
 ();%0A
-        try %7B%0A    
     
@@ -2090,28 +2090,24 @@
 ));%0A        
-   
- 
 DatagramPack
@@ -2159,36 +2159,32 @@
 eiver);%0A        
-    
 assert new Strin
@@ -2239,20 +2239,16 @@
         
- 
-   
 UDPOpera
@@ -2353,28 +2353,24 @@
 ));%0A        
-  
-  
 heartBeat.st
@@ -2387,147 +2387,26 @@
     
-%7D catch (SocketException e) %7B%0A            e.printStackTrace();%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A        %7D
+receiver.close();%0A
 %0A   

Test cases are fixed with sequential order and socket close.