Skip to content

Commit

Permalink
Add remove and clear oparations to MapBuilder (#302)
Browse files Browse the repository at this point in the history
* Add remove operation for body builder

* Update version and readme
  • Loading branch information
OptimumCode authored May 13, 2024
1 parent ecc1eb2 commit 81aaf29
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 2 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# th2 common library (Java) (5.10.1)
# th2 common library (Java) (5.11.1)

## Usage

Expand Down Expand Up @@ -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`)
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ class MapBuilder<K, V>(
innerMap.putAll(from)
}

fun remove(key: K): MapBuilder<K, V> = apply {
innerMap.remove(key)
}

fun removeAll(key: K, vararg keys: K): MapBuilder<K, V> = apply {
innerMap.remove(key)
for (otherKeys in keys) {
innerMap.remove(otherKeys)
}
}

fun clear(): MapBuilder<K, V> = apply {
innerMap.clear()
}

fun build(): Map<K, V> {
return innerMap
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Int>()

@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<String, Int>(), 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",
)
}
}

0 comments on commit 81aaf29

Please sign in to comment.