2 * ============LICENSE_START=======================================================
3 * dcaegen2-collectors-veshv
4 * ================================================================================
5 * Copyright (C) 2018 NOKIA
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=========================================================
20 package org.onap.dcae.collectors.veshv.domain
22 import io.netty.buffer.Unpooled
23 import io.netty.buffer.UnpooledByteBufAllocator
24 import org.assertj.core.api.Assertions.assertThat
25 import org.assertj.core.api.Assertions.assertThatExceptionOfType
26 import org.jetbrains.spek.api.Spek
27 import org.jetbrains.spek.api.dsl.describe
28 import org.jetbrains.spek.api.dsl.given
29 import org.jetbrains.spek.api.dsl.it
30 import org.onap.dcae.collectors.veshv.domain.exceptions.InvalidWireFrameMarkerException
31 import org.onap.dcae.collectors.veshv.domain.exceptions.MissingWireFrameBytesException
32 import java.nio.charset.Charset
35 * @author Piotr Jaszczyk <piotr.jaszczyk@nokia.com>
38 object WireFrameCodecsTest : Spek({
39 val payloadAsString = "coffeebabe"
40 val encoder = WireFrameEncoder(UnpooledByteBufAllocator.DEFAULT)
41 val decoder = WireFrameDecoder()
43 fun createSampleFrame() =
44 WireFrame(payloadAsString.toByteArray(Charset.defaultCharset()))
46 fun encodeSampleFrame() =
47 createSampleFrame().let {
51 describe("Wire Frame invariants") {
53 given("input with unsupported version") {
54 val input = WireFrame(
55 payload = ByteData.EMPTY,
57 payloadTypeRaw = PayloadContentType.GOOGLE_PROTOCOL_BUFFER.hexValue,
60 it("should fail validation") {
61 assertThat(input.isValid()).isFalse()
65 given("input with unsupported payload type") {
66 val input = WireFrame(
67 payload = ByteData.EMPTY,
69 payloadTypeRaw = 0x69,
72 it("should fail validation") {
73 assertThat(input.isValid()).isFalse()
77 given("input with too small payload size") {
78 val input = WireFrame(
79 payload = ByteData(byteArrayOf(1, 2, 3)),
81 payloadTypeRaw = PayloadContentType.GOOGLE_PROTOCOL_BUFFER.hexValue,
84 it("should fail validation") {
85 assertThat(input.isValid()).isFalse()
89 given("input with too big payload size") {
90 val input = WireFrame(
91 payload = ByteData(byteArrayOf(1, 2, 3)),
93 payloadTypeRaw = PayloadContentType.GOOGLE_PROTOCOL_BUFFER.hexValue,
96 it("should fail validation") {
97 assertThat(input.isValid()).isFalse()
101 given("valid input") {
102 val payload = byteArrayOf(6, 9, 8, 6)
103 val input = WireFrame(
104 payload = ByteData(payload),
106 payloadTypeRaw = PayloadContentType.GOOGLE_PROTOCOL_BUFFER.hexValue,
107 payloadSize = payload.size)
109 it("should pass validation") {
110 assertThat(input.isValid()).isTrue()
117 describe("Wire Frame codec") {
119 describe("encode-decode methods' compatibility") {
120 val frame = createSampleFrame()
121 val encoded = encodeSampleFrame()
122 val decoded = decoder.decodeFirst(encoded)
124 it("should decode version") {
125 assertThat(decoded.version).isEqualTo(frame.version)
128 it("should decode payload type") {
129 assertThat(decoded.payloadTypeRaw).isEqualTo(frame.payloadTypeRaw)
132 it("should decode payload size") {
133 assertThat(decoded.payloadSize).isEqualTo(frame.payloadSize)
136 it("should decode payload") {
137 assertThat(decoded.payload.asString())
138 .isEqualTo(payloadAsString)
142 describe("TCP framing") {
143 // see "Dealing with a Stream-based Transport" on http://netty.io/wiki/user-guide-for-4.x.html#wiki-h3-11
145 it("should decode message leaving rest unread") {
146 val buff = Unpooled.buffer()
147 .writeBytes(encodeSampleFrame())
149 val decoded = decoder.decodeFirst(buff)
151 assertThat(decoded.isValid()).describedAs("should be valid").isTrue()
152 assertThat(buff.readableBytes()).isEqualTo(1)
155 it("should throw exception when not even header fits") {
156 val buff = Unpooled.buffer()
159 assertThatExceptionOfType(MissingWireFrameBytesException::class.java)
160 .isThrownBy { decoder.decodeFirst(buff) }
163 it("should throw exception when first byte is not 0xFF but length looks ok") {
164 val buff = Unpooled.buffer()
166 .writeBytes("some garbage".toByteArray())
168 assertThatExceptionOfType(InvalidWireFrameMarkerException::class.java)
169 .isThrownBy { decoder.decodeFirst(buff) }
172 it("should throw exception when first byte is not 0xFF and length is to short") {
173 val buff = Unpooled.buffer()
176 assertThatExceptionOfType(InvalidWireFrameMarkerException::class.java)
177 .isThrownBy { decoder.decodeFirst(buff) }
180 it("should throw exception when payload doesn't fit") {
181 val buff = Unpooled.buffer()
182 .writeBytes(encodeSampleFrame())
183 buff.writerIndex(buff.writerIndex() - 2)
185 assertThatExceptionOfType(MissingWireFrameBytesException::class.java)
186 .isThrownBy { decoder.decodeFirst(buff) }