a0a67d9b59c4c45b8a00772bcc9276a863c75611
[dcaegen2/services/sdk.git] /
1 /*
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.dcaegen2.services.sdk.services.hvves.client.producer.impl.encoders;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24
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;
29
30 import java.nio.ByteBuffer;
31
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
42
43     private final WireFrameEncoder wireFrameEncoder = new WireFrameEncoder(ByteBufAllocator.DEFAULT);
44
45     @Test
46     void encode_givenNullPayload_shouldThrowEncodingException() {
47         final ByteBuffer buffer = null;
48
49         Try<ByteBuf> encodedBuffer = wireFrameEncoder.encode(buffer);
50
51         assertThat(encodedBuffer.isFailure()).isTrue();
52         assertThat(encodedBuffer.getCause()).isInstanceOf(WTPEncodingException.class);
53     }
54
55     @Test
56     void encode_givenEmptyPayload_shouldThrowEncodingException() {
57         final ByteBuffer buffer = ByteBuffer.allocateDirect(0);
58
59         Try<ByteBuf> encodedBuffer = wireFrameEncoder.encode(buffer);
60
61         assertThat(encodedBuffer.isFailure()).isTrue();
62         assertThat(encodedBuffer.getCause()).isInstanceOf(WTPEncodingException.class);
63     }
64
65     @Test
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);
70
71         final Try<ByteBuf> encodedBuffer = wireFrameEncoder.encode(buffer);
72
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);
82     }
83
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")
88                     .isEqualTo(bytes[i]);
89         }
90     }
91
92     private void assertValidHeaderBeggining(ByteBuf encodedBuffer) {
93         assertNextBytesAreInOrder(encodedBuffer,
94                 MARKER_BYTE,
95                 SUPPORTED_VERSION_MAJOR,
96                 SUPPORTED_VERSION_MINOR);
97     }
98
99     private void assertBufferSizeIs(ByteBuf encodedBuffer, int headerSize) {
100         assertThat(encodedBuffer.readableBytes()).describedAs("buffer's readable bytes").isEqualTo(headerSize);
101     }
102
103     private void skipReservedBytes(ByteBuf encodedBuffer) {
104         encodedBuffer.readBytes(RESERVED_BYTES_COUNT);
105     }
106
107     private void assertAllBytesVerified(ByteBuf encodedBuffer) {
108         assertThat(encodedBuffer.readableBytes())
109                 .describedAs("all bytes should've been asserted")
110                 .isEqualTo(0);
111     }
112
113     private byte[] intToBytes(int integer) {
114         return ByteBuffer.allocate(4).putInt(integer).array();
115     }
116 }