package co.nstant.in.cbor.decoder;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.List;

import org.junit.Test;

import co.nstant.in.cbor.CborBuilder;
import co.nstant.in.cbor.CborDecoder;
import co.nstant.in.cbor.CborEncoder;
import co.nstant.in.cbor.CborException;
import co.nstant.in.cbor.model.DataItem;

public class ByteStringDecoderTest {

    @Test
    public void shouldDecodeChunkedByteString() throws CborException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        CborEncoder encoder = new CborEncoder(baos);
        encoder.encode(new CborBuilder()
            .startByteString()
            .add(new byte[] { '\0' })
            .add(new byte[] { 0x10 })
            .add(new byte[] { 0x13 })
            .end()
            .build());
        byte[] encodedBytes = baos.toByteArray();
        ByteArrayInputStream bais = new ByteArrayInputStream(encodedBytes);
        CborDecoder decoder = new CborDecoder(bais);
        List<DataItem> dataItems = decoder.decode();
        assertNotNull(dataItems);
        assertEquals(1, dataItems.size());
    }

    @Test
    public void shouldDecodeByteString1K() throws CborException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        CborEncoder encoder = new CborEncoder(baos);
        encoder.encode(new CborBuilder().add(new byte[1024]).build());
        byte[] encodedBytes = baos.toByteArray();
        ByteArrayInputStream bais = new ByteArrayInputStream(encodedBytes);
        CborDecoder decoder = new CborDecoder(bais);
        List<DataItem> dataItems = decoder.decode();
        assertNotNull(dataItems);
        assertEquals(1, dataItems.size());
    }

    @Test
    public void shouldDecodeByteString1M() throws CborException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        CborEncoder encoder = new CborEncoder(baos);
        encoder.encode(new CborBuilder().add(new byte[1024 * 1024]).build());
        byte[] encodedBytes = baos.toByteArray();
        ByteArrayInputStream bais = new ByteArrayInputStream(encodedBytes);
        CborDecoder decoder = new CborDecoder(bais);
        List<DataItem> dataItems = decoder.decode();
        assertNotNull(dataItems);
        assertEquals(1, dataItems.size());
    }

    @Test(expected = CborException.class)
    public void shouldThrowOnIncompleteByteString() throws CborException {
        byte[] bytes = new byte[] {0x42, 0x20};
        CborDecoder.decode(bytes);
    }

    @Test(expected = CborException.class)
    public void shouldTrowOnMissingBreak() throws CborException {
        byte[] bytes = new byte[] {0x5f, 0x41, 0x20};
        CborDecoder.decode(bytes);
    }
}

@@ -429,24 +429,109 @@
 r.model.
-DataItem
+ByteString;%0Aimport co.nstant.in.cbor.model.DataItem;%0Aimport co.nstant.in.cbor.model.MajorType
 ;%0A%0Apubli
@@ -816,16 +816,20 @@
         
+    
 .startBy
@@ -839,16 +839,20 @@
 tring()%0A
+    
         
@@ -877,24 +877,28 @@
 %5D %7B '%5C0' %7D)%0A
+    
             
@@ -927,32 +927,36 @@
  %7D)%0A            
+    
 .add(new byte%5B%5D 
@@ -981,15 +981,23 @@
     
+    
 .end()%0A
+    
     
@@ -2654,16 +2654,17 @@
 byte%5B%5D %7B
+ 
 0x42, 0x
@@ -2661,24 +2661,25 @@
 %7B 0x42, 0x20
+ 
 %7D;%0A        C
@@ -2854,16 +2854,17 @@
 byte%5B%5D %7B
+ 
 0x5f, 0x
@@ -2871,16 +2871,17 @@
 41, 0x20
+ 
 %7D;%0A     
@@ -2916,10 +2916,996 @@
 ;%0A    %7D%0A
+%0A    public void decodingExample() throws CborException %7B%0A        byte bytes%5B%5D = %7B 0, 1, 2, 3 %7D;%0A        // Encode%0A        ByteArrayOutputStream encodedStream = new ByteArrayOutputStream();%0A        new CborEncoder(encodedStream).encode(new ByteString(bytes));%0A        byte encodedBytes%5B%5D = encodedStream.toByteArray();%0A        // Decode%0A        ByteArrayInputStream inputStream = new ByteArrayInputStream(encodedBytes);%0A        CborDecoder decoder = new CborDecoder(inputStream);%0A        DataItem dataItem = decoder.decodeNext();%0A        assertEquals(MajorType.BYTE_STRING, dataItem.getMajorType());%0A        ByteString byteString = (ByteString) dataItem;%0A        byte%5B%5D decodedBytes = byteString.getBytes();%0A        // Verify%0A        assertEquals(bytes.length, decodedBytes.length);%0A        assertEquals(bytes%5B0%5D, decodedBytes%5B0%5D);%0A        assertEquals(bytes%5B1%5D, decodedBytes%5B1%5D);%0A        assertEquals(bytes%5B2%5D, decodedBytes%5B2%5D);%0A        assertEquals(bytes%5B3%5D, decodedBytes%5B3%5D);%0A    %7D%0A%0A
 %7D%0A

we have no example how to decode a byte string