-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: rebuild project due to codegen change (#51)
- Loading branch information
1 parent
1dc76ce
commit 4c5be68
Showing
2 changed files
with
298 additions
and
0 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
onebusaway-sdk-java-core/src/main/kotlin/org/onebusaway/core/http/QueryParams.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package org.onebusaway.core.http | ||
|
||
import org.onebusaway.core.toImmutable | ||
|
||
class QueryParams | ||
private constructor( | ||
private val map: Map<String, List<String>>, | ||
@get:JvmName("size") val size: Int | ||
) { | ||
|
||
fun isEmpty(): Boolean = map.isEmpty() | ||
|
||
fun keys(): Set<String> = map.keys | ||
|
||
fun values(key: String): List<String> = map[key].orEmpty() | ||
|
||
fun toBuilder(): Builder = Builder().putAll(map) | ||
|
||
companion object { | ||
|
||
@JvmStatic fun builder() = Builder() | ||
} | ||
|
||
class Builder { | ||
|
||
private val map: MutableMap<String, MutableList<String>> = mutableMapOf() | ||
private var size: Int = 0 | ||
|
||
fun put(key: String, value: String) = apply { | ||
map.getOrPut(key) { mutableListOf() }.add(value) | ||
size++ | ||
} | ||
|
||
fun put(key: String, values: Iterable<String>) = apply { values.forEach { put(key, it) } } | ||
|
||
fun putAll(queryParams: Map<String, Iterable<String>>) = apply { | ||
queryParams.forEach(::put) | ||
} | ||
|
||
fun putAll(queryParams: QueryParams) = apply { | ||
queryParams.keys().forEach { put(it, queryParams.values(it)) } | ||
} | ||
|
||
fun replace(key: String, value: String) = apply { | ||
remove(key) | ||
put(key, value) | ||
} | ||
|
||
fun replace(key: String, values: Iterable<String>) = apply { | ||
remove(key) | ||
put(key, values) | ||
} | ||
|
||
fun replaceAll(queryParams: Map<String, Iterable<String>>) = apply { | ||
queryParams.forEach(::replace) | ||
} | ||
|
||
fun replaceAll(queryParams: QueryParams) = apply { | ||
queryParams.keys().forEach { replace(it, queryParams.values(it)) } | ||
} | ||
|
||
fun remove(key: String) = apply { size -= map.remove(key).orEmpty().size } | ||
|
||
fun removeAll(keys: Set<String>) = apply { keys.forEach(::remove) } | ||
|
||
fun clear() = apply { | ||
map.clear() | ||
size = 0 | ||
} | ||
|
||
fun build() = | ||
QueryParams(map.mapValues { (_, values) -> values.toImmutable() }.toImmutable(), size) | ||
} | ||
|
||
override fun hashCode(): Int = map.hashCode() | ||
|
||
override fun equals(other: Any?): Boolean { | ||
if (this === other) { | ||
return true | ||
} | ||
|
||
return other is QueryParams && map == other.map | ||
} | ||
|
||
override fun toString(): String = "QueryParams{map=$map}" | ||
} |
212 changes: 212 additions & 0 deletions
212
onebusaway-sdk-java-core/src/test/kotlin/org/onebusaway/core/http/QueryParamsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
package org.onebusaway.core.http | ||
|
||
import org.assertj.core.api.Assertions.assertThat | ||
import org.assertj.core.api.Assertions.catchThrowable | ||
import org.assertj.core.api.Assumptions.assumeThat | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.EnumSource | ||
|
||
internal class QueryParamsTest { | ||
|
||
enum class TestCase( | ||
val queryParams: QueryParams, | ||
val expectedMap: Map<String, List<String>>, | ||
val expectedSize: Int | ||
) { | ||
EMPTY(QueryParams.builder().build(), expectedMap = mapOf(), expectedSize = 0), | ||
PUT_ONE( | ||
QueryParams.builder().put("key", "value").build(), | ||
expectedMap = mapOf("key" to listOf("value")), | ||
expectedSize = 1 | ||
), | ||
PUT_MULTIPLE( | ||
QueryParams.builder().put("key", listOf("value1", "value2")).build(), | ||
expectedMap = mapOf("key" to listOf("value1", "value2")), | ||
expectedSize = 2 | ||
), | ||
MULTIPLE_PUT( | ||
QueryParams.builder().put("key1", "value").put("key2", "value").build(), | ||
expectedMap = mapOf("key1" to listOf("value"), "key2" to listOf("value")), | ||
expectedSize = 2 | ||
), | ||
MULTIPLE_PUT_SAME_NAME( | ||
QueryParams.builder().put("key", "value1").put("key", "value2").build(), | ||
expectedMap = mapOf("key" to listOf("value1", "value2")), | ||
expectedSize = 2 | ||
), | ||
MULTIPLE_PUT_MULTIPLE( | ||
QueryParams.builder() | ||
.put("key", listOf("value1", "value2")) | ||
.put("key", listOf("value1", "value2")) | ||
.build(), | ||
expectedMap = mapOf("key" to listOf("value1", "value2", "value1", "value2")), | ||
expectedSize = 4 | ||
), | ||
PUT_ALL_MAP( | ||
QueryParams.builder() | ||
.putAll( | ||
mapOf( | ||
"key1" to listOf("value1", "value2"), | ||
"key2" to listOf("value1", "value2") | ||
) | ||
) | ||
.build(), | ||
expectedMap = | ||
mapOf("key1" to listOf("value1", "value2"), "key2" to listOf("value1", "value2")), | ||
expectedSize = 4 | ||
), | ||
PUT_ALL_HEADERS( | ||
QueryParams.builder().putAll(QueryParams.builder().put("key", "value").build()).build(), | ||
expectedMap = mapOf("key" to listOf("value")), | ||
expectedSize = 1 | ||
), | ||
REMOVE_ABSENT( | ||
QueryParams.builder().remove("key").build(), | ||
expectedMap = mapOf(), | ||
expectedSize = 0 | ||
), | ||
REMOVE_PRESENT_ONE( | ||
QueryParams.builder().put("key", "value").remove("key").build(), | ||
expectedMap = mapOf(), | ||
expectedSize = 0 | ||
), | ||
REMOVE_PRESENT_MULTIPLE( | ||
QueryParams.builder().put("key", listOf("value1", "value2")).remove("key").build(), | ||
expectedMap = mapOf(), | ||
expectedSize = 0 | ||
), | ||
REMOVE_ALL( | ||
QueryParams.builder() | ||
.put("key1", "value") | ||
.put("key3", "value") | ||
.removeAll(setOf("key1", "key2", "key3")) | ||
.build(), | ||
expectedMap = mapOf(), | ||
expectedSize = 0 | ||
), | ||
CLEAR( | ||
QueryParams.builder().put("key1", "value").put("key2", "value").clear().build(), | ||
expectedMap = mapOf(), | ||
expectedSize = 0 | ||
), | ||
REPLACE_ONE_ABSENT( | ||
QueryParams.builder().replace("key", "value").build(), | ||
expectedMap = mapOf("key" to listOf("value")), | ||
expectedSize = 1 | ||
), | ||
REPLACE_ONE_PRESENT_ONE( | ||
QueryParams.builder().put("key", "value1").replace("key", "value2").build(), | ||
expectedMap = mapOf("key" to listOf("value2")), | ||
expectedSize = 1 | ||
), | ||
REPLACE_ONE_PRESENT_MULTIPLE( | ||
QueryParams.builder() | ||
.put("key", listOf("value1", "value2")) | ||
.replace("key", "value3") | ||
.build(), | ||
expectedMap = mapOf("key" to listOf("value3")), | ||
expectedSize = 1 | ||
), | ||
REPLACE_MULTIPLE_ABSENT( | ||
QueryParams.builder().replace("key", listOf("value1", "value2")).build(), | ||
expectedMap = mapOf("key" to listOf("value1", "value2")), | ||
expectedSize = 2 | ||
), | ||
REPLACE_MULTIPLE_PRESENT_ONE( | ||
QueryParams.builder() | ||
.put("key", "value1") | ||
.replace("key", listOf("value2", "value3")) | ||
.build(), | ||
expectedMap = mapOf("key" to listOf("value2", "value3")), | ||
expectedSize = 2 | ||
), | ||
REPLACE_MULTIPLE_PRESENT_MULTIPLE( | ||
QueryParams.builder() | ||
.put("key", listOf("value1", "value2")) | ||
.replace("key", listOf("value3", "value4")) | ||
.build(), | ||
expectedMap = mapOf("key" to listOf("value3", "value4")), | ||
expectedSize = 2 | ||
), | ||
REPLACE_ALL_MAP( | ||
QueryParams.builder() | ||
.put("key1", "value1") | ||
.put("key2", "value1") | ||
.put("key3", "value1") | ||
.replaceAll(mapOf("key1" to listOf("value2"), "key3" to listOf("value2"))) | ||
.build(), | ||
expectedMap = | ||
mapOf( | ||
"key1" to listOf("value2"), | ||
"key2" to listOf("value1"), | ||
"key3" to listOf("value2") | ||
), | ||
expectedSize = 3 | ||
), | ||
REPLACE_ALL_HEADERS( | ||
QueryParams.builder() | ||
.put("key1", "value1") | ||
.put("key2", "value1") | ||
.put("key3", "value1") | ||
.replaceAll( | ||
QueryParams.builder().put("key1", "value2").put("key3", "value2").build() | ||
) | ||
.build(), | ||
expectedMap = | ||
mapOf( | ||
"key1" to listOf("value2"), | ||
"key2" to listOf("value1"), | ||
"key3" to listOf("value2") | ||
), | ||
expectedSize = 3 | ||
) | ||
} | ||
|
||
@ParameterizedTest | ||
@EnumSource | ||
fun keysAndValues(testCase: TestCase) { | ||
val map = mutableMapOf<String, List<String>>() | ||
val queryParams = testCase.queryParams | ||
queryParams.keys().forEach { key -> map[key] = queryParams.values(key) } | ||
|
||
assertThat(map).isEqualTo(testCase.expectedMap) | ||
} | ||
|
||
@ParameterizedTest | ||
@EnumSource | ||
fun size(testCase: TestCase) { | ||
val size = testCase.queryParams.size | ||
|
||
assertThat(size).isEqualTo(testCase.expectedSize) | ||
} | ||
|
||
@ParameterizedTest | ||
@EnumSource | ||
fun keysAreImmutable(testCase: TestCase) { | ||
val queryParams = testCase.queryParams | ||
val queryParamKeysCopy = queryParams.keys().toSet() | ||
|
||
val throwable = catchThrowable { | ||
(queryParams.keys() as MutableSet<String>).add("another key") | ||
} | ||
|
||
assertThat(throwable).isInstanceOf(UnsupportedOperationException::class.java) | ||
assertThat(queryParams.keys()).isEqualTo(queryParamKeysCopy) | ||
} | ||
|
||
@ParameterizedTest | ||
@EnumSource | ||
fun valuesAreImmutable(testCase: TestCase) { | ||
val queryParams = testCase.queryParams | ||
assumeThat(queryParams.size).isNotEqualTo(0) | ||
val key = queryParams.keys().first() | ||
val queryParamValuesCopy = queryParams.values(key).toList() | ||
|
||
val throwable = catchThrowable { | ||
(queryParams.values(key) as MutableList<String>).add("another value") | ||
} | ||
|
||
assertThat(throwable).isInstanceOf(UnsupportedOperationException::class.java) | ||
assertThat(queryParams.values(key)).isEqualTo(queryParamValuesCopy) | ||
} | ||
} |