Skip to content

Commit 9afb0a7

Browse files
Merge branch 'main' into release
2 parents 309ff1d + 4160ba8 commit 9afb0a7

File tree

19 files changed

+577
-71
lines changed

19 files changed

+577
-71
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ buildscript {
4545
}
4646

4747
group 'io.onixlabs'
48-
version '2.0.0-rc1'
48+
version '2.0.0-rc2'
4949

5050
subprojects {
5151
repositories {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright 2020 Matthew Layton
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.onixlabs.corda.identityframework.contract
18+
19+
fun Iterable<AbstractClaim<*>>.containsDuplicateProperties(
20+
property: String? = null,
21+
ignoreCase: Boolean = false
22+
): Boolean {
23+
val properties = map { if (ignoreCase) it.property.toLowerCase() else it.property }
24+
val filteredProperties = properties.filter { property?.equals(it, ignoreCase) ?: true }
25+
return filteredProperties.size != filteredProperties.distinct().size
26+
}
27+
28+
/**
29+
* Checks a collection of [AbstractClaim] instances for duplicate keys.
30+
*
31+
* @param isCaseSensitive Determines whether to perform case sensitive key checking.
32+
* @param message The exception message to throw if the collection contains duplicate keys.
33+
* @throws IllegalStateException if the collection contains duplicate keys.
34+
*/
35+
fun Iterable<AbstractClaim<*>>.checkForDuplicateProperties(
36+
property: String? = null,
37+
ignoreCase: Boolean = false,
38+
message: String = "The claim collection contains duplicate keys."
39+
) = check(!containsDuplicateProperties(property, ignoreCase)) { message }
Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,8 @@
1717
package io.onixlabs.corda.identityframework.contract
1818

1919
import kotlin.reflect.KVisibility
20-
import kotlin.reflect.full.declaredMemberProperties
2120
import kotlin.reflect.full.memberProperties
2221

23-
/**
24-
* Checks a collection of [AbstractClaim] instances for duplicate keys.
25-
*
26-
* @param isCaseSensitive Determines whether to perform case sensitive key checking.
27-
* @param message The exception message to throw if the collection contains duplicate keys.
28-
* @throws IllegalStateException if the collection contains duplicate keys.
29-
*/
30-
fun Iterable<AbstractClaim<*>>.checkForDuplicateKeys(
31-
isCaseSensitive: Boolean = false,
32-
message: String = "The claim collection contains duplicate keys."
33-
) = check(count() == distinctBy { if (isCaseSensitive) it.property else it.property.toLowerCase() }.size) { message }
34-
3522
/**
3623
* Formats any object like a Kotlin data class.
3724
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package io.onixlabs.corda.identityframework.contract.general
2+
3+
import io.onixlabs.corda.identityframework.contract.Claim
4+
import io.onixlabs.corda.identityframework.contract.checkForDuplicateProperties
5+
import io.onixlabs.corda.identityframework.contract.containsDuplicateProperties
6+
import org.junit.jupiter.api.Test
7+
import org.junit.jupiter.api.assertThrows
8+
import kotlin.test.assertEquals
9+
import kotlin.test.assertFalse
10+
import kotlin.test.assertTrue
11+
12+
class DuplicatePropertyTests {
13+
14+
@Test
15+
fun `containsDuplicateProperties should return false when a collection does not contain duplicates`() {
16+
17+
// Arrange
18+
val claims = listOf(
19+
Claim("Property1", Unit),
20+
Claim("PROPERTY1", Unit), // Not considered duplicate because case is preserved.
21+
Claim("Property2", Unit),
22+
Claim("Property3", Unit)
23+
)
24+
25+
// Act
26+
val result = claims.containsDuplicateProperties()
27+
28+
// Assert
29+
assertFalse(result)
30+
}
31+
32+
@Test
33+
fun `containsDuplicateProperties should return true when a collection does contain duplicates (ignore case)`() {
34+
35+
// Arrange
36+
val claims = listOf(
37+
Claim("Property1", Unit),
38+
Claim("PROPERTY1", Unit), // Considered duplicate because case is ignored.
39+
Claim("Property2", Unit),
40+
Claim("Property3", Unit)
41+
)
42+
43+
// Act
44+
val result = claims.containsDuplicateProperties(ignoreCase = true)
45+
46+
// Assert
47+
assertTrue(result)
48+
}
49+
50+
@Test
51+
fun `containsDuplicateProperties should return false when a collection does not contain duplicates (property specified)`() {
52+
53+
// Arrange
54+
val claims = listOf(
55+
Claim("Property1", Unit),
56+
Claim("PROPERTY1", Unit), // Not considered duplicate because case is preserved.
57+
Claim("Property2", Unit),
58+
Claim("Property3", Unit)
59+
)
60+
61+
// Act
62+
val result = claims.containsDuplicateProperties("Property1")
63+
64+
// Assert
65+
assertFalse(result)
66+
}
67+
68+
@Test
69+
fun `containsDuplicateProperties should return true when a collection does not contain duplicates (property specified, ignore case)`() {
70+
71+
// Arrange
72+
val claims = listOf(
73+
Claim("Property1", Unit),
74+
Claim("PROPERTY1", Unit), // Considered duplicate because case is ignored.
75+
Claim("Property2", Unit),
76+
Claim("Property3", Unit)
77+
)
78+
79+
// Act
80+
val result = claims.containsDuplicateProperties("Property1", ignoreCase = true)
81+
82+
// Assert
83+
assertTrue(result)
84+
}
85+
86+
@Test
87+
fun `containsDuplicateProperties should return false when a collection does not contain duplicates (distinct property specified)`() {
88+
89+
// Arrange
90+
val claims = listOf(
91+
Claim("Property1", Unit),
92+
Claim("PROPERTY1", Unit), // Considered duplicate because case is ignored.
93+
Claim("Property2", Unit),
94+
Claim("Property3", Unit)
95+
)
96+
97+
// Act
98+
val result = claims.containsDuplicateProperties("Property3")
99+
100+
// Assert
101+
assertFalse(result)
102+
}
103+
104+
@Test
105+
fun `checkForDuplicateProperties should throw an exception when duplicate properties are detected`() {
106+
107+
// Arrange
108+
val claims = listOf(
109+
Claim("Property1", Unit),
110+
Claim("Property1", Unit),
111+
Claim("Property2", Unit),
112+
Claim("Property3", Unit)
113+
)
114+
115+
// Act
116+
val exception = assertThrows<IllegalStateException> {
117+
claims.checkForDuplicateProperties()
118+
}
119+
120+
// Assert
121+
assertEquals("The claim collection contains duplicate keys.", exception.message)
122+
}
123+
}

0 commit comments

Comments
 (0)