2 * ============LICENSE_START=======================================================
3 * dcaegen2-collectors-veshv
4 * ================================================================================
5 * Copyright (C) 2019 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.config.impl.gsonadapters
22 import com.google.gson.Gson
23 import com.google.gson.JsonDeserializationContext
24 import com.google.gson.JsonParseException
25 import com.google.gson.reflect.TypeToken
26 import com.nhaarman.mockitokotlin2.mock
27 import org.assertj.core.api.Assertions.assertThat
28 import org.jetbrains.spek.api.Spek
29 import org.jetbrains.spek.api.dsl.describe
30 import org.jetbrains.spek.api.dsl.given
31 import org.jetbrains.spek.api.dsl.it
32 import org.onap.dcae.collectors.veshv.config.impl.gsonadapters.AddressAdapter.InvalidAddressException
33 import java.lang.NumberFormatException
34 import kotlin.test.assertFailsWith
37 internal object AddressAdapterTest : Spek({
39 describe("deserialization") {
41 val context = mock<JsonDeserializationContext>()
42 val addressAdapterType = TypeToken.get(AddressAdapter::class.java).type
44 val cut = AddressAdapter()
46 given("valid string") {
47 val address = "hostname:9000"
48 val json = gson.toJsonTree(address)
50 it("should return address") {
51 val deserialized = cut.deserialize(json, addressAdapterType, context)
53 assertThat(deserialized.hostName).isEqualTo("hostname")
54 assertThat(deserialized.port).isEqualTo(9000)
58 val invalidAddresses = mapOf(
59 Pair("missingPort", InvalidAddressException::class),
60 Pair("NaNPort:Hey", NumberFormatException::class),
61 Pair(":6036", InvalidAddressException::class))
63 invalidAddresses.forEach { address, exception ->
64 given("invalid address string: $address") {
66 val json = gson.toJsonTree(address)
67 it("should throw exception") {
68 assertFailsWith(exception) {
69 cut.deserialize(json, addressAdapterType, context)