From 81aaf2971df82d69686b6afccb2cfe35f952af30 Mon Sep 17 00:00:00 2001 From: Oleg Smirnov Date: Mon, 13 May 2024 11:20:10 +0400 Subject: [PATCH] Add `remove` and `clear` oparations to MapBuilder (#302) * Add remove operation for body builder * Update version and readme --- README.md | 6 +- gradle.properties | 2 +- .../rabbitmq/transport/builders/MapBuilder.kt | 15 ++ .../transport/builders/MapBuilderTest.kt | 150 ++++++++++++++++++ 4 files changed, 171 insertions(+), 2 deletions(-) create mode 100644 src/test/kotlin/com/exactpro/th2/common/schema/message/impl/rabbitmq/transport/builders/MapBuilderTest.kt diff --git a/README.md b/README.md index 9615cb31..2109d847 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# th2 common library (Java) (5.10.1) +# th2 common library (Java) (5.11.1) ## Usage @@ -511,6 +511,10 @@ dependencies { ## Release notes +### 5.11.1-dev + ++ Add `remove` and `clear` operations to `MapBuilder` + ### 5.11.0-dev + Migrated to the th2 gradle plugin: `0.0.6` (bom: `4.6.1`) diff --git a/gradle.properties b/gradle.properties index 16a7caeb..8d8989e3 100644 --- a/gradle.properties +++ b/gradle.properties @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # -release_version=5.11.0 +release_version=5.11.1 kotlin_version=1.8.22 description='th2 common library (Java)' vcs_url=https://github.com/th2-net/th2-common-j diff --git a/src/main/kotlin/com/exactpro/th2/common/schema/message/impl/rabbitmq/transport/builders/MapBuilder.kt b/src/main/kotlin/com/exactpro/th2/common/schema/message/impl/rabbitmq/transport/builders/MapBuilder.kt index e30e5c32..8c335ea2 100644 --- a/src/main/kotlin/com/exactpro/th2/common/schema/message/impl/rabbitmq/transport/builders/MapBuilder.kt +++ b/src/main/kotlin/com/exactpro/th2/common/schema/message/impl/rabbitmq/transport/builders/MapBuilder.kt @@ -32,6 +32,21 @@ class MapBuilder( innerMap.putAll(from) } + fun remove(key: K): MapBuilder = apply { + innerMap.remove(key) + } + + fun removeAll(key: K, vararg keys: K): MapBuilder = apply { + innerMap.remove(key) + for (otherKeys in keys) { + innerMap.remove(otherKeys) + } + } + + fun clear(): MapBuilder = apply { + innerMap.clear() + } + fun build(): Map { return innerMap } diff --git a/src/test/kotlin/com/exactpro/th2/common/schema/message/impl/rabbitmq/transport/builders/MapBuilderTest.kt b/src/test/kotlin/com/exactpro/th2/common/schema/message/impl/rabbitmq/transport/builders/MapBuilderTest.kt new file mode 100644 index 00000000..b3a0af93 --- /dev/null +++ b/src/test/kotlin/com/exactpro/th2/common/schema/message/impl/rabbitmq/transport/builders/MapBuilderTest.kt @@ -0,0 +1,150 @@ +/* + * Copyright 2024 Exactpro (Exactpro Systems Limited) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.exactpro.th2.common.schema.message.impl.rabbitmq.transport.builders + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertFalse +import org.junit.jupiter.api.Assertions.assertNull +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertAll +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource + +class MapBuilderTest { + private val builder = MapBuilder() + + @ParameterizedTest + @ValueSource(ints = [0, 1, 10]) + fun getSize(elements: Int) { + repeat(elements) { + builder.put("key$it", it) + } + assertEquals(elements, builder.size, "unexpected size") + } + + @Test + fun contains() { + builder.put("A", 42) + + assertAll( + { + assertTrue(builder.contains("A"), "unexpected contains result for existing key") + }, + { + assertFalse(builder.contains("B"), "unexpected contains result for not existing key") + }, + ) + } + + @Test + fun get() { + builder.put("A", 42) + + assertAll( + { + assertEquals(42, builder["A"], "unexpected value for existing key") + }, + { + assertNull(builder["B"], "unexpected value for not existing key") + } + ) + } + + @Test + fun putAll() { + builder.putAll(mapOf("A" to 42, "B" to 54)) + + assertAll( + { + assertEquals(42, builder["A"], "unexpected value for existing key A") + }, + { + assertEquals(54, builder["B"], "unexpected value for existing key B") + }, + ) + } + + @Test + fun remove() { + builder.put("A", 42) + builder.put("B", 52) + builder.remove("A") + + assertAll( + { + assertNull(builder["A"], "unexpected value for removed key A") + }, + { + assertEquals(52, builder["B"], "unexpected value for remaining key B") + }, + ) + } + + @Test + fun removeAll() { + builder.put("A", 42) + builder.put("B", 52) + builder.put("C", 62) + + builder.removeAll("A", "C") + + assertAll( + { + assertNull(builder["A"], "unexpected value for removed key A") + }, + { + assertNull(builder["C"], "unexpected value for removed key C") + }, + { + assertEquals(52, builder["B"], "unexpected value for remaining key B") + }, + ) + } + + @Test + fun clear() { + builder.put("A", 42) + builder.putAll(mapOf("B" to 52, "C" to 62)) + + builder.clear() + + assertAll( + { + assertEquals(0, builder.size, "unexpected size after clear") + }, + { + assertEquals(emptyMap(), builder.build(), "unexpected map after builder clear") + }, + ) + } + + @Test + fun build() { + builder.put("A", 42) + builder.putAll(mapOf("B" to 52, "C" to 62)) + builder.remove("B") + assertEquals( + mapOf( + "A" to 42, + "C" to 62, + ), + builder.build(), + "unexpected build result", + ) + } +} \ No newline at end of file