content: package org.adelbs.iso8583.payload;
  
  import static org.junit.Assert.assertEquals;
  import static org.junit.Assert.assertTrue;
  
  import java.util.Arrays;
  
  import org.adelbs.iso8583.constants.EncodingEnum;
  import org.adelbs.iso8583.exception.OutOfBoundsException;
  import org.adelbs.iso8583.vo.FieldVO;
  import org.junit.Before;
  import org.junit.BeforeClass;
  import org.junit.Test;
  
  public class TLVTransformatorTest {
  	
  	private FieldVO fieldVO;
  	private static TLVTransformator TLV_TRANSFORMATOR;
  	
  	/**
  	 * [48, 49] = zero padded TLV Type 1 (01)
  	 * [48, 48, 49] = zero padded TVL length 1 (001)
  	 * [48, 48, 50] = zero padded TVL length 2 (001)
  	 * [65] = A letter
  	 */
  	
  	@BeforeClass
  	public static void setUpClass(){
  		TLV_TRANSFORMATOR = new TLVTransformator(EncodingEnum.UTF8);
  	}
  	
  	@Before
  	public void setUp(){
  		this.fieldVO = new FieldVO();
  		this.fieldVO.setTlvLength("1");
  		this.fieldVO.setTlvType("1");
  		this.fieldVO.setValue("A");
  	}
  
  	@Test
  	public void tesTransform_ExactSizeValue() {
  		final byte[] transform = TLV_TRANSFORMATOR.transform(this.fieldVO);
  		
  		assertTrue("Last Byte should be decimal code of the Letter A", Arrays.equals(new byte[]{48, 49, 48, 48, 49, 65}, transform));
  	}
  	
  	@Test
  	public void tesTransform_ValueLesserThanSize() {
  		this.fieldVO.setTlvLength("2");
  		
  		final byte[] transform = TLV_TRANSFORMATOR.transform(this.fieldVO);
  		assertTrue("Last two Bytes should be decimal code of the 0 padding followed by the A", Arrays.equals(new byte[]{48, 49, 48, 48, 50, 48, 65}, transform));
  	}
  	
  	@Test
  	public void tesTransform_ValueBiggerThanSize() {
  		this.fieldVO.setValue("AA");
  		
  		final byte[] transform = TLV_TRANSFORMATOR.transform(this.fieldVO);
  		assertTrue("Last two Bytes should be decimal code of 9, generated by cropping the value", Arrays.equals(new byte[]{48, 49, 48, 48, 49, 57}, transform));
  	}
  	
  	@Test(expected = IllegalArgumentException.class)
  	public void tesTransform_NonFieldVO() {
  		TLV_TRANSFORMATOR.transform("MockObject");
  	}
  	
  	@Test
  	public void testRevert() throws OutOfBoundsException{
  		final String tlvField = "01001A";
  		final String padding = "000000000000000000000000";
  		byte[] payload = (tlvField+padding).getBytes();
  		
  		final RevertResult revert = TLV_TRANSFORMATOR.revert(payload);
  		assertEquals("The resultant revert position should match", revert.getRevertEndPosition(), tlvField.getBytes().length);
  		
  		final FieldVO resultantObject = (FieldVO)revert.getResultantObject();
  		assertEquals("1", resultantObject.getTlvType());
  		assertEquals("1", resultantObject.getTlvLength());
  		assertEquals("A", resultantObject.getValue());
  	}
  
  }
  