package com.smartdevicelink.protocol;

import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.livio.BSON.BsonEncoder;
import com.smartdevicelink.protocol.enums.ControlFrameTags;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.HashMap;

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNull;

@RunWith(AndroidJUnit4.class)
public class SdlPacketTests {
	//TODO: Add tests to cover other parts of SdlPacket class

	// Test variables
	private final int TEST_HASH_ID = 65537;
	private final String TEST_PROTOCOL_VERSION = "5.0.0";
	private final Long TEST_MTU = (long) 131072;

	// Test payload from core representing hashId and mtu size
	private byte[] corePayload = hexStringToByteArray("39000000" +
			"10" +
			"68617368496400" +
			"01000100" +
			"126d747500" +
			"000002" + "0000000000" + "02" +
			"70726f746f636f6c56657273" +
			"696f6e0006000000352e302e300000");

	@Test
	public void testNull(){
		byte[] testPayload = hexStringToByteArray(
				"16000000" +
						"02" +
						"68656c6c6f" +"00" + // hello
						"06000000" + "776f726c64" + "00" + //world
						"00");
		String tag = "hello";

		SdlPacket sdlPacket = new SdlPacket();
		assertNull(sdlPacket.getTag(tag));
		sdlPacket.setPayload(testPayload);
		assertEquals(sdlPacket.getTag("hello"),"world");
	}

	@Test
	public void testBsonDecoding(){
		SdlPacket sdlPacket = new SdlPacket();
		sdlPacket.setPayload(corePayload);
		assertEquals(sdlPacket.getTag(ControlFrameTags.RPC.StartServiceACK.HASH_ID), TEST_HASH_ID);
		assertEquals(sdlPacket.getTag(ControlFrameTags.RPC.StartServiceACK.PROTOCOL_VERSION), TEST_PROTOCOL_VERSION);
		assertEquals(sdlPacket.getTag(ControlFrameTags.RPC.StartServiceACK.MTU), TEST_MTU);
	}

	@Test
	public void testBsonEncoding(){
		HashMap<String, Object> testMap = new HashMap<>();
		testMap.put(ControlFrameTags.RPC.StartServiceACK.HASH_ID, TEST_HASH_ID);
		testMap.put(ControlFrameTags.RPC.StartServiceACK.MTU, TEST_MTU);
		testMap.put(ControlFrameTags.RPC.StartServiceACK.PROTOCOL_VERSION, TEST_PROTOCOL_VERSION);

		byte[] observed = BsonEncoder.encodeToBytes(testMap);
		assertEquals(observed.length, corePayload.length);
		for(int i = 0; i < observed.length; i++){
			assertEquals(observed[i], corePayload[i]);
		}
	}

	// Helper method for converting String to Byte Array
	private static byte[] hexStringToByteArray(String s) {
		int len = s.length();
		byte[] data = new byte[len / 2];
		for (int i = 0; i < len; i += 2) {
			data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
					+ Character.digit(s.charAt(i+1), 16));
		}
		return data;
	}
}

@@ -181,16 +181,176 @@
 ameTags;
+%0Aimport com.smartdevicelink.test.TestValues;%0Aimport com.smartdevicelink.transport.enums.TransportType;%0Aimport com.smartdevicelink.transport.utl.TransportRecord;
 %0A%0Aimport
@@ -595,67 +595,8 @@
 ts %7B
-%0A%09//TODO: Add tests to cover other parts of SdlPacket class
 %0A%0A%09/
@@ -1323,16 +1323,17 @@
 cket();%0A
+%0A
 %09%09assert
@@ -1390,32 +1390,32 @@
 d(testPayload);%0A
-
 %09%09assertEquals(s
@@ -1448,16 +1448,462 @@
 world%22);
+%0A%0A%09%09assertEquals(1, sdlPacket.getVersion());%0A%0A%09%09sdlPacket.setPriorityCoefficient(TestValues.GENERAL_INT);%0A%09%09assertEquals(TestValues.GENERAL_INT, sdlPacket.getPrioirtyCoefficient());%0A%0A%09%09sdlPacket.setTransportRecord(new TransportRecord(TransportType.TCP, TestValues.GENERAL_STRING));%0A%09%09assertEquals(TransportType.TCP, sdlPacket.getTransportRecord().getType());%0A%09%09assertEquals(TestValues.GENERAL_STRING, sdlPacket.getTransportRecord().getAddress());
 %0A%09%7D%0A%0A%09@T

Fix todo in SdlPacketTests