// Path: src/main/java/com/jenkov/iap/ion/IonFieldTypes.java
// public class IonFieldTypes {
// 
//     /* Core type ID constants */
//     public static final int BYTES           =  0;  //a series of raw bytes
//     public static final int BOOLEAN         =  1;  // a number between 1 and 15, useful for booleans and small enums
//     public static final int INT_POS         =  2;
//     public static final int INT_NEG         =  3;
//     public static final int FLOAT           =  4;
//     public static final int UTF_8           =  5;
// 
//     public static final int UTF_8_SHORT     =  6;
//     public static final int UTC_DATE_TIME   =  7;
// 
//     public static final int ARRAY           = 10;
//     public static final int TABLE           = 11;
//     public static final int OBJECT          = 12;
//     public static final int KEY             = 13;   //a sequence of bytes identifying a key or a property fieldName - often UTF-8 encoded field names.
//     public static final int KEY_SHORT       = 14;   //a sequence of bytes identifying a key or a property fieldName - often UTF-8 encoded field names - 15 bytes or less.
// 
//     public static final int EXTENDED = 15;   //a sequence of bytes identifying a key or a property fieldName - often UTF-8 encoded field names - 15 bytes or less.
// 
// 
//     /*
//         Temporarily moved types
// 
//      */
//     //public static final int COMPLEX_TYPE_ID_SHORT = 12;   //the type of an object - reserved for special IAP object types - none so far.
//     // public static final int COPY            =  8;   //a relative reference to a field located earlier in same ION structure.
// 
// 
//     /*
//         Extended type ID constants - can be from 0 to 255 - but we use only from 16 to 255 to avoid
//         numeric clashes with the core type IDs. The clashes are only "mental". They could be avoided in code.
//      */
//     public static final int ELEMENT_COUNT        = 16;  //extended short
//     public static final int COMPLEX_TYPE_ID      = 17;  //extended normal
//     public static final int COMPLEX_TYPE_VERSION = 18;  //extended short - 15 chars to indicate version must be enough
// 
//     /*
//         reference
//         UTC time (without date)
//      */
// 
// 
// 
// 
// 
// 
// }
// 
// Path: src/main/java/com/jenkov/iap/ion/types/Utf8.java
// public class Utf8 {
//     public byte[] source = null;
//     public int    offset = 0;
//     public int    length = 0;
// 
// }


import com.jenkov.iap.ion.IonFieldTypes;
import com.jenkov.iap.ion.types.Utf8;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import java.io.UnsupportedEncodingException;
package com.jenkov.iap.message;



/**
 * Created by jjenkov on 21-02-2016.
 */
public class IapMessageWriterTest {

    byte[] dest = new byte[10 * 1024];



    @Test
    public void testWriteSemanticProtocolId() throws UnsupportedEncodingException {

        int bytesWritten = IapMessageWriter.writeSemanticProtocolId(dest, 0, "http".getBytes("UTF-8"));

        assertEquals(7, bytesWritten);

        int index = 0;
        assertEquals((IonFieldTypes.KEY_SHORT << 4) | 1, 255 & dest[index++]);
        assertEquals(IapMessageHeaders.SEMANTIC_PROTOCOL_ID_KEY_FIELD_VALUE, 255 & dest[index++]);

        assertEquals((IonFieldTypes.UTF_8_SHORT << 4) | 4, 255 & dest[index++]);
        assertEquals('h', 255 & dest[index++]);
        assertEquals('t', 255 & dest[index++]);
        assertEquals('t', 255 & dest[index++]);
        assertEquals('p', 255 & dest[index++]);
    }

    @Test
    public void testWriteSemanticProtocolVersion() throws UnsupportedEncodingException {

        int bytesWritten = IapMessageWriter.writeSemanticProtocolVersion(dest, 0, "1.1".getBytes("UTF-8"));

        assertEquals(6, bytesWritten);

        int index = 0;
        assertEquals((IonFieldTypes.KEY_SHORT << 4) | 1, 255 & dest[index++]);
        assertEquals(IapMessageHeaders.SEMANTIC_PROTOCOL_VERSION_KEY_FIELD_VALUE, 255 & dest[index++]);

        assertEquals((IonFieldTypes.UTF_8_SHORT << 4) | 3, 255 & dest[index++]);
// Path: src/main/java/com/jenkov/iap/ion/IonFieldTypes.java
// public class IonFieldTypes {
// 
//     /* Core type ID constants */
//     public static final int BYTES           =  0;  //a series of raw bytes
//     public static final int BOOLEAN         =  1;  // a number between 1 and 15, useful for booleans and small enums
//     public static final int INT_POS         =  2;
//     public static final int INT_NEG         =  3;
//     public static final int FLOAT           =  4;
//     public static final int UTF_8           =  5;
// 
//     public static final int UTF_8_SHORT     =  6;
//     public static final int UTC_DATE_TIME   =  7;
// 
//     public static final int ARRAY           = 10;
//     public static final int TABLE           = 11;
//     public static final int OBJECT          = 12;
//     public static final int KEY             = 13;   //a sequence of bytes identifying a key or a property fieldName - often UTF-8 encoded field names.
//     public static final int KEY_SHORT       = 14;   //a sequence of bytes identifying a key or a property fieldName - often UTF-8 encoded field names - 15 bytes or less.
// 
//     public static final int EXTENDED = 15;   //a sequence of bytes identifying a key or a property fieldName - often UTF-8 encoded field names - 15 bytes or less.
// 
// 
//     /*
//         Temporarily moved types
// 
//      */
//     //public static final int COMPLEX_TYPE_ID_SHORT = 12;   //the type of an object - reserved for special IAP object types - none so far.
//     // public static final int COPY            =  8;   //a relative reference to a field located earlier in same ION structure.
// 
// 
//     /*
//         Extended type ID constants - can be from 0 to 255 - but we use only from 16 to 255 to avoid
//         numeric clashes with the core type IDs. The clashes are only "mental". They could be avoided in code.
//      */
//     public static final int ELEMENT_COUNT        = 16;  //extended short
//     public static final int COMPLEX_TYPE_ID      = 17;  //extended normal
//     public static final int COMPLEX_TYPE_VERSION = 18;  //extended short - 15 chars to indicate version must be enough
// 
//     /*
//         reference
//         UTC time (without date)
//      */
// 
// 
// 
// 
// 
// 
// }
// 
// Path: src/main/java/com/jenkov/iap/ion/types/Utf8.java
// public class Utf8 {
//     public byte[] source = null;
//     public int    offset = 0;
//     public int    length = 0;
// 
// }


// Path: src/test/java/com/jenkov/iap/message/IapMessageWriterTest.java
import com.jenkov.iap.ion.IonFieldTypes;
import com.jenkov.iap.ion.types.Utf8;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import java.io.UnsupportedEncodingException;

package com.jenkov.iap.message;



/**
 * Created by jjenkov on 21-02-2016.
 */
public class IapMessageWriterTest {

    byte[] dest = new byte[10 * 1024];



    @Test
    public void testWriteSemanticProtocolId() throws UnsupportedEncodingException {

        int bytesWritten = IapMessageWriter.writeSemanticProtocolId(dest, 0, "http".getBytes("UTF-8"));

        assertEquals(7, bytesWritten);

        int index = 0;
        assertEquals((IonFieldTypes.KEY_SHORT << 4) | 1, 255 & dest[index++]);
        assertEquals(IapMessageHeaders.SEMANTIC_PROTOCOL_ID_KEY_FIELD_VALUE, 255 & dest[index++]);

        assertEquals((IonFieldTypes.UTF_8_SHORT << 4) | 4, 255 & dest[index++]);
        assertEquals('h', 255 & dest[index++]);
        assertEquals('t', 255 & dest[index++]);
        assertEquals('t', 255 & dest[index++]);
        assertEquals('p', 255 & dest[index++]);
    }

    @Test
    public void testWriteSemanticProtocolVersion() throws UnsupportedEncodingException {

        int bytesWritten = IapMessageWriter.writeSemanticProtocolVersion(dest, 0, "1.1".getBytes("UTF-8"));

        assertEquals(6, bytesWritten);

        int index = 0;
        assertEquals((IonFieldTypes.KEY_SHORT << 4) | 1, 255 & dest[index++]);
        assertEquals(IapMessageHeaders.SEMANTIC_PROTOCOL_VERSION_KEY_FIELD_VALUE, 255 & dest[index++]);

        assertEquals((IonFieldTypes.UTF_8_SHORT << 4) | 3, 255 & dest[index++]);
        assertEquals('1', 255 & dest[index++]);