package nl.sikken.bertrik.hab;

import java.io.UnsupportedEncodingException;

import org.junit.Assert;
import org.junit.Test;

/**
 * @author bertrik
 *
 */
public final class SentenceTest {

	@Test
	public void testSentence() throws UnsupportedEncodingException {
		final Sentence sentence = new Sentence("CALL", 1, 2, 3.45, 6.78, 9.0);
		final String s = sentence.format();
		
		Assert.assertEquals("$$CALL,1,00:00:02,3.450000,6.780000,9.0*EFFF\n", s);
	}
	
	@Test
	public void testSentenceExtras() throws UnsupportedEncodingException {
		final Sentence sentence = new Sentence("CALL", 1, 2, 3.45, 6.78, 9.0);
		sentence.addField("hello");
		final String s = sentence.format();
		
		Assert.assertTrue(s.contains("hello"));
	}
	
	/**
	 * Verifies calculation of checksum
	 * 
	 * Known good string with good CRC:
	 * $$hadie,181,10:42:10,54.422829,-6.741293,27799.3,1:10*002A
	 * 
	 * @throws UnsupportedEncodingException 
	 */
	@Test
	public void testCrc() throws UnsupportedEncodingException {
		final String s = "hadie,181,10:42:10,54.422829,-6.741293,27799.3,1:10";
		final byte[] data = s.getBytes("US-ASCII");

		final CcittCrc16 crc = new CcittCrc16();
		int value = crc.calculate(data, 0xFFFF);
		
		Assert.assertEquals(0x002A, value);
	}

}

@@ -72,16 +72,83 @@
 eption;%0D
+%0Aimport java.nio.charset.StandardCharsets;%0D%0Aimport java.util.Date;%0D
 %0A%0D%0Aimpor
@@ -384,33 +384,43 @@
 ence(%22CALL%22, 1, 
-2
+new Date(0)
 , 3.45, 6.78, 9.
@@ -506,17 +506,17 @@
 ,00:00:0
-2
+0
 ,3.45000
@@ -534,12 +534,12 @@
 9.0*
-EFFF
+25E9
 %5Cn%22,
@@ -684,17 +684,26 @@
 LL%22, 1, 
-2
+new Date()
 , 3.45, 
@@ -1228,18 +1228,33 @@
 tes(
-%22US-
+StandardCharsets.US_
 ASCII
-%22
 );%0D%0A

Update SentenceTest to match new Sentence prototype.