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.ves.message.generator.impl
 
  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
 
  37 class CommonEventHeaderParserTest : Spek({
 
  39     describe("Common event header parser") {
 
  40         val parser = CommonEventHeaderParser()
 
  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()
 
  48             it("should parse common event header") {
 
  50                         parser.parse(jsonObject(json))
 
  51                                 .fold({ fail() }, ::identity)
 
  53                 assertThat(result).describedAs("common event header").isEqualTo(commonEventHeader)
 
  57         given("invalid header in JSON format") {
 
  58             val json = "{}".byteInputStream()
 
  60             it("should throw exception") {
 
  61                 val result = parser.parse(jsonObject(json))
 
  67         given("invalid JSON") {
 
  68             val json = "{}}}}".byteInputStream()
 
  70             it("should throw exception") {
 
  71                 val result = parser.parse(jsonObject(json))
 
  79 fun assertFailed(result: Option<CommonEventHeader>) =
 
  80         result.fold({}, { fail() })
 
  82 fun jsonObject(json: ByteArrayInputStream) = Json.createReader(json).readObject()!!