package ru.r2cloud.jradio.jy1sat;

import static org.junit.Assert.assertEquals;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import org.junit.Test;

import ru.r2cloud.jradio.AssertJson;
import ru.r2cloud.jradio.BeaconInputStream;
import ru.r2cloud.ssdv.SsdvDecoder;
import ru.r2cloud.ssdv.SsdvImage;

public class Jy1satSsdvPacketSourceTest {

	@Test
	public void testSuccess() throws Exception {
		int total = 0;
		try (BeaconInputStream<Jy1satBeacon> bis = new BeaconInputStream<>(Jy1satSsdvPacketSourceTest.class.getClassLoader().getResourceAsStream("jy1sat.bin"), Jy1satBeacon.class)) {
			SsdvDecoder decoder = new SsdvDecoder(new Jy1satSsdvPacketSource(bis));
			while (decoder.hasNext()) {
				SsdvImage image = decoder.next();
				total++;
				assertSsdvImage("jy1sat", image);
			}
		}
		assertEquals(1, total);
	}

	private static void assertSsdvImage(String expectedName, SsdvImage cur) throws IOException {
		try (InputStream is1 = Jy1satSsdvPacketSourceTest.class.getClassLoader().getResourceAsStream("expected/ssdv/" + expectedName + ".png")) {
			BufferedImage expected = ImageIO.read(is1);
			for (int i = 0; i < expected.getWidth(); i++) {
				for (int j = 0; j < expected.getHeight(); j++) {
					assertEquals("failure in image: " + expectedName, expected.getRGB(i, j), cur.getImage().getRGB(i, j));
				}
			}
		}
		AssertJson.assertObjectsEqual("ssdv/" + expectedName + ".json", cur);
	}

}

@@ -72,16 +72,60 @@
 tEquals;
+%0Aimport static org.junit.Assert.assertFalse;
 %0A%0Aimport
@@ -210,16 +210,108 @@
 tStream;
+%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.NoSuchElementException;
 %0A%0Aimport
@@ -1005,16 +1005,16 @@
 %09%09%7D%0A%09%09%7D%0A
-
 %09%09assert
@@ -1035,16 +1035,692 @@
 l);%0A%09%7D%0A%0A
+%09@Test(expected = NoSuchElementException.class)%0A%09public void testIllegalToCallNextFirst() throws Exception %7B%0A%09%09try (BeaconInputStream%3CJy1satBeacon%3E bis = new BeaconInputStream%3C%3E(Jy1satSsdvPacketSourceTest.class.getClassLoader().getResourceAsStream(%22jy1sat.bin%22), Jy1satBeacon.class)) %7B%0A%09%09%09new Jy1satSsdvPacketSource(bis).next();%0A%09%09%7D%0A%09%7D%0A%0A%09@Test%0A%09public void testInvalidPayload() throws Exception %7B%0A%09%09Jy1satBeacon beacon = new Jy1satBeacon();%0A%09%09beacon.setPayload(new byte%5B2%5D);%0A%0A%09%09List%3CJy1satBeacon%3E beacons = new ArrayList%3C%3E();%0A%09%09beacons.add(beacon);%0A%09%09SsdvDecoder decoder = new SsdvDecoder(new Jy1satSsdvPacketSource(beacons.iterator()));%0A%09%09assertFalse(decoder.hasNext());%0A%09%7D%0A%0A
 %09private

add some tests