2 * ============LICENSE_START=======================================================
3 * DCAEGEN2-SERVICES-SDK
4 * ================================================================================
5 * Copyright (C) 2019 Nokia. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.dcaegen2.services.sdk.services.hvves.client.producer.impl.encoders;
23 import static org.assertj.core.api.Assertions.assertThat;
25 import io.vavr.control.Try;
26 import org.junit.jupiter.api.Test;
27 import io.netty.buffer.ByteBuf;
28 import io.netty.buffer.ByteBufAllocator;
30 import java.nio.ByteBuffer;
32 public class WireFrameEncoderTest {
33 private static final byte MARKER_BYTE = (byte) 0xAA;
34 private static final byte SUPPORTED_VERSION_MAJOR = (byte) 0x01;
35 private static final byte SUPPORTED_VERSION_MINOR = (byte) 0x00;
36 private static final int RESERVED_BYTES_COUNT = 3;
37 private static final int HEADER_SIZE = 1 * Byte.BYTES + // marker
38 2 * Byte.BYTES + // single byte fields (versions)
39 RESERVED_BYTES_COUNT * java.lang.Byte.BYTES + // reserved bytes
40 1 * Short.BYTES + // paylaod type
41 1 * Integer.BYTES; // payload length
43 private final WireFrameEncoder wireFrameEncoder = new WireFrameEncoder(ByteBufAllocator.DEFAULT);
46 void encode_givenNullPayload_shouldThrowEncodingException() {
47 final ByteBuffer buffer = null;
49 Try<ByteBuf> encodedBuffer = wireFrameEncoder.encode(buffer);
51 assertThat(encodedBuffer.isFailure()).isTrue();
52 assertThat(encodedBuffer.getCause()).isInstanceOf(WTPEncodingException.class);
56 void encode_givenEmptyPayload_shouldThrowEncodingException() {
57 final ByteBuffer buffer = ByteBuffer.allocateDirect(0);
59 Try<ByteBuf> encodedBuffer = wireFrameEncoder.encode(buffer);
61 assertThat(encodedBuffer.isFailure()).isTrue();
62 assertThat(encodedBuffer.getCause()).isInstanceOf(WTPEncodingException.class);
66 void encode_givenSomePayloadBytes_shouldCreateValidGPBFrameWithPayloadAtTheEnd() {
67 final byte[] payloadBytes = new byte[]{0x1A, 0x2B, 0x3C};
68 final int bufferSize = payloadBytes.length;
69 final ByteBuffer buffer = ByteBuffer.wrap(payloadBytes);
71 final Try<ByteBuf> encodedBuffer = wireFrameEncoder.encode(buffer);
73 assertThat(encodedBuffer.isSuccess()).isTrue();
74 final ByteBuf actualEncodedBuffer = encodedBuffer.get();
75 assertBufferSizeIs(actualEncodedBuffer, HEADER_SIZE + bufferSize);
76 assertValidHeaderBeggining(actualEncodedBuffer);
77 skipReservedBytes(actualEncodedBuffer);
78 assertNextBytesAreInOrder(actualEncodedBuffer, (byte) 0x00, (byte) 0x01);
79 assertNextBytesAreInOrder(actualEncodedBuffer, intToBytes(bufferSize));
80 assertNextBytesAreInOrder(actualEncodedBuffer, payloadBytes);
81 assertAllBytesVerified(actualEncodedBuffer);
84 private void assertNextBytesAreInOrder(ByteBuf encodedBuffer, byte... bytes) {
85 for (int i = 0; i < bytes.length; i++) {
86 assertThat(encodedBuffer.readByte())
87 .describedAs("byte in " + (i + 1) + " assertion")
92 private void assertValidHeaderBeggining(ByteBuf encodedBuffer) {
93 assertNextBytesAreInOrder(encodedBuffer,
95 SUPPORTED_VERSION_MAJOR,
96 SUPPORTED_VERSION_MINOR);
99 private void assertBufferSizeIs(ByteBuf encodedBuffer, int headerSize) {
100 assertThat(encodedBuffer.readableBytes()).describedAs("buffer's readable bytes").isEqualTo(headerSize);
103 private void skipReservedBytes(ByteBuf encodedBuffer) {
104 encodedBuffer.readBytes(RESERVED_BYTES_COUNT);
107 private void assertAllBytesVerified(ByteBuf encodedBuffer) {
108 assertThat(encodedBuffer.readableBytes())
109 .describedAs("all bytes should've been asserted")
113 private byte[] intToBytes(int integer) {
114 return ByteBuffer.allocate(4).putInt(integer).array();