Custom detekt rule for logger usage check
[dcaegen2/collectors/hv-ves.git] / sources / hv-collector-ves-message-generator / src / test / kotlin / org / onap / dcae / collectors / veshv / ves / message / generator / impl / CommonEventHeaderParserTest.kt
1 /*
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
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 package org.onap.dcae.collectors.veshv.ves.message.generator.impl
21
22 import arrow.core.Option
23 import arrow.core.identity
24 import com.google.protobuf.util.JsonFormat
25 import org.assertj.core.api.Assertions.assertThat
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.VesEventDomain.STATE_CHANGE
31 import org.onap.dcae.collectors.veshv.tests.utils.commonHeader
32 import org.onap.ves.VesEventOuterClass.CommonEventHeader
33 import java.io.ByteArrayInputStream
34 import javax.json.Json
35 import kotlin.test.fail
36
37 class CommonEventHeaderParserTest : Spek({
38
39     describe("Common event header parser") {
40         val parser = CommonEventHeaderParser()
41
42         given("valid header in JSON format") {
43             val commonEventHeader = commonHeader(
44                     domain = STATE_CHANGE,
45                     id = "sample-event-id")
46             val json = JsonFormat.printer().print(commonEventHeader).byteInputStream()
47
48             it("should parse common event header") {
49                 val result =
50                         parser.parse(jsonObject(json))
51                                 .fold({ fail() }, ::identity)
52
53                 assertThat(result).describedAs("common event header").isEqualTo(commonEventHeader)
54             }
55         }
56
57         given("invalid header in JSON format") {
58             val json = "{}".byteInputStream()
59
60             it("should throw exception") {
61                 val result = parser.parse(jsonObject(json))
62
63                 assertFailed(result)
64             }
65         }
66
67         given("invalid JSON") {
68             val json = "{}}}}".byteInputStream()
69
70             it("should throw exception") {
71                 val result = parser.parse(jsonObject(json))
72
73                 assertFailed(result)
74             }
75         }
76     }
77 })
78
79 fun assertFailed(result: Option<CommonEventHeader>) =
80         result.fold({}, { fail() })
81
82 fun jsonObject(json: ByteArrayInputStream) = Json.createReader(json).readObject()!!