1 /* 2 * Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"). 5 * You may not use this file except in compliance with the License. 6 * A copy of the License is located at 7 * 8 * http://aws.amazon.com/apache2.0 9 * 10 * or in the "license" file accompanying this file. This file is distributed 11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 12 * express or implied. See the License for the specific language governing 13 * permissions and limitations under the License. 14 */ 15 package software.amazon.eventstream; 16 17 import org.junit.jupiter.api.Test; 18 19 import java.io.ByteArrayOutputStream; 20 import java.io.DataOutputStream; 21 import java.io.IOException; 22 import java.nio.ByteBuffer; 23 24 import static org.junit.jupiter.api.Assertions.assertEquals; 25 import static software.amazon.eventstream.HeaderValue.fromInteger; 26 27 public class HeaderTest { 28 @Test genericHeaders()29 public void genericHeaders() throws Exception { 30 roundTrip(new Header("test-string-header", "test-string-value")); 31 roundTrip(new Header("test-byte-array-header", HeaderValue.fromByteArray(bb(1, 2, 3, 4, 5, 6, 7, 8)))); 32 roundTrip(new Header("test-uint32-header", fromInteger(8918230))); 33 } 34 35 @Test typeId()36 public void typeId() { 37 for (byte i = 0; i <= 9; i++) { 38 assertEquals(i, HeaderType.fromTypeId(i).headerTypeId); 39 } 40 } 41 roundTrip(Header header)42 static void roundTrip(Header header) throws IOException { 43 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 44 try (DataOutputStream dos = new DataOutputStream(baos)) { 45 header.encode(dos); 46 } 47 byte[] bytes = baos.toByteArray(); 48 Header actual = Header.decode(ByteBuffer.wrap(bytes)); 49 50 assertEquals(header, actual); 51 } 52 bb(int... bytes)53 static byte[] bb(int... bytes) { 54 byte[] bs = new byte[bytes.length]; 55 for (int i = 0; i < bytes.length; i++) { 56 bs[i] = (byte) bytes[i]; 57 } 58 return bs; 59 } 60 } 61