package com.frankwu.nmea;

import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;

/**
 * Created by wuf2 on 2/21/2015.
 */
public class GgaNmeaCodecTest {
    @Test
    public void decodeValidMessage() {
        GgaNmeaCodec codec = new GgaNmeaCodec();
        codec.addObserver(new Observer() {
                              @Override
                              public void update(Observable o, Object arg) {
                                  System.out.println(arg);

                                  try {
                                      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                                      ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
                                      objectOutputStream.writeObject(arg);
                                      byte[] bytes = byteArrayOutputStream.toByteArray();
                                      ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
                                      ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
                                      Object object = objectInputStream.readObject();
                                      System.out.println("serialized: " + object);

                                  } catch (Exception e) {
                                      e.printStackTrace();
                                  }
                              }
                          }

        );

        Observer mockObserver = mock(Observer.class);
        codec.addObserver(mockObserver);

        String content = "$GPGGA,092750.000,5321.6802,N,00630.3372,W,1,8,1.03,61.7,M,55.2,M,,*76\r\n";
        codec.decode(content);

        content = "$GPGGA,092751.000,5321.6802,N,00630.3371,W,1,8,1.03,61.7,M,55.3,M,,*75\r\n";
        codec.decode(content);

        content = "$GPGGA,092204.999,4250.5589,S,14718.5084,E,1,04,24.4,19.7,M,,,,0000*1F\r\n";
        codec.decode(content);

        verify(mockObserver, times(3)

        ).

                update(eq(codec), any

                        ());
    }

    @Test
    public void encode() {
        final GgaNmeaCodec codec = new GgaNmeaCodec();
        final String content = "$GPGGA,092750.000,5321.6802,N,00630.3372,W,1,8,1.03,61.7,M,55.2,M,,*76\r\n";
        codec.addObserver(new Observer() {
            @Override
            public void update(Observable o, Object arg) {
                AbstractNmeaObject obj = (AbstractNmeaObject) arg;
                List<String> contents = codec.encode(obj);
                assertEquals(Arrays.asList(content), contents);
            }
        });

        codec.decode(content);
    }
}


@@ -48,152 +48,8 @@
 t;%0A%0A
-import java.io.ByteArrayInputStream;%0Aimport java.io.ByteArrayOutputStream;%0Aimport java.io.ObjectInputStream;%0Aimport java.io.ObjectOutputStream;%0A
 impo
@@ -69,16 +69,16 @@
 Arrays;%0A
+
 import j
@@ -625,1011 +625,8 @@
 rg);
-%0A%0A                                  try %7B%0A                                      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();%0A                                      ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);%0A                                      objectOutputStream.writeObject(arg);%0A                                      byte%5B%5D bytes = byteArrayOutputStream.toByteArray();%0A                                      ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);%0A                                      ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);%0A                                      Object object = objectInputStream.readObject();%0A                                      System.out.println(%22serialized: %22 + object);%0A%0A                                  %7D catch (Exception e) %7B%0A                                      e.printStackTrace();%0A                                  %7D
 %0A   
@@ -678,25 +678,24 @@
           %7D%0A
-%0A
         );%0A%0A
@@ -1222,85 +1222,31 @@
 s(3)
-%0A%0A        ).%0A%0A                update(eq(codec), any%0A%0A                        
+).update(eq(codec), any
 ());

remove temp code