| package co.nstant.in.cbor.examples; |
| |
| import java.io.ByteArrayInputStream; |
| import java.io.ByteArrayOutputStream; |
| import java.io.InputStream; |
| |
| import org.junit.Assert; |
| import org.junit.Test; |
| |
| 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; |
| import co.nstant.in.cbor.model.SimpleValue; |
| |
| /** |
| * simple(24) -> 0xf818 |
| */ |
| public class Example46Test { |
| |
| private static final SimpleValue VALUE = new SimpleValue(24); |
| private static final byte[] ENCODED_VALUE = new byte[] { (byte) 0xf8, 0x18 }; |
| |
| @Test |
| public void shouldEncode() throws CborException { |
| ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); |
| CborEncoder encoder = new CborEncoder(byteOutputStream); |
| encoder.encode(VALUE); |
| Assert.assertArrayEquals(ENCODED_VALUE, byteOutputStream.toByteArray()); |
| } |
| |
| @Test |
| public void shouldDecode() throws CborException { |
| InputStream inputStream = new ByteArrayInputStream(ENCODED_VALUE); |
| CborDecoder decoder = new CborDecoder(inputStream); |
| DataItem dataItem = decoder.decodeNext(); |
| Assert.assertTrue(dataItem instanceof SimpleValue); |
| SimpleValue simpleValue = (SimpleValue) dataItem; |
| Assert.assertEquals(VALUE, simpleValue); |
| } |
| |
| } |