Skip to content

Commit c4cb9de

Browse files
committed
chore: Simplify test assertions with shouldBeSingleton
Signed-off-by: Sebastian Schuberth <[email protected]>
1 parent 01d0eb5 commit c4cb9de

File tree

11 files changed

+144
-125
lines changed

11 files changed

+144
-125
lines changed

clients/keycloak/src/test/kotlin/DefaultKeycloakClientTest.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ import dasniko.testcontainers.keycloak.KeycloakContainer
3131
import io.kotest.assertions.throwables.shouldThrow
3232
import io.kotest.core.extensions.install
3333
import io.kotest.matchers.collections.beEmpty
34+
import io.kotest.matchers.collections.shouldBeSingleton
3435
import io.kotest.matchers.collections.shouldContainAll
3536
import io.kotest.matchers.collections.shouldHaveSize
37+
import io.kotest.matchers.shouldBe
3638
import io.kotest.matchers.shouldNot
3739
import io.kotest.matchers.string.shouldStartWith
3840

@@ -162,9 +164,9 @@ class DefaultKeycloakClientTest : AbstractKeycloakClientTest() {
162164

163165
val groups = confidentialClient.getGroups(groupNameFilter = "B")
164166

165-
groups shouldHaveSize 1
166-
groups.map { it.name.value } shouldContainAll
167-
listOf("Organization-B")
167+
groups.shouldBeSingleton {
168+
it.name.value shouldBe "Organization-B"
169+
}
168170
}
169171
}
170172
}

components/authorization/backend/src/test/kotlin/rights/HierarchyPermissionsTest.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package org.eclipse.apoapsis.ortserver.components.authorization.rights
2121

2222
import io.kotest.core.spec.style.WordSpec
2323
import io.kotest.inspectors.forAll
24+
import io.kotest.matchers.collections.shouldBeSingleton
2425
import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder
2526
import io.kotest.matchers.maps.beEmpty
2627
import io.kotest.matchers.maps.shouldHaveSize
@@ -300,10 +301,10 @@ class HierarchyPermissionsTest : WordSpec({
300301
"return a map with includes containing only the wildcard ID" {
301302
val includes = superuserPermissions.includes()
302303

303-
includes shouldHaveSize 1
304-
includes[CompoundHierarchyId.WILDCARD_LEVEL] shouldContainExactlyInAnyOrder listOf(
305-
CompoundHierarchyId.WILDCARD
306-
)
304+
includes.entries.shouldBeSingleton { (key, value) ->
305+
key shouldBe CompoundHierarchyId.WILDCARD_LEVEL
306+
value shouldBe listOf(CompoundHierarchyId.WILDCARD)
307+
}
307308
}
308309

309310
"return an empty map for implicit includes" {

components/authorization/backend/src/test/kotlin/service/DbAuthorizationServiceTest.kt

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ package org.eclipse.apoapsis.ortserver.components.authorization.service
2121

2222
import io.kotest.core.spec.style.WordSpec
2323
import io.kotest.inspectors.forAll
24+
import io.kotest.matchers.collections.shouldBeSingleton
2425
import io.kotest.matchers.collections.shouldContainExactly
2526
import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder
26-
import io.kotest.matchers.collections.shouldHaveSize
2727
import io.kotest.matchers.maps.shouldHaveSize
2828
import io.kotest.matchers.nulls.beNull
2929
import io.kotest.matchers.nulls.shouldNotBeNull
@@ -769,8 +769,10 @@ class DbAuthorizationServiceTest : WordSpec() {
769769

770770
val users = service.listUsers(repositoryCompoundId)
771771

772-
users.keys shouldHaveSize 1
773-
users[USER_ID] shouldContainExactlyInAnyOrder listOf(RepositoryRole.READER)
772+
users.entries.shouldBeSingleton { (key, value) ->
773+
key shouldBe USER_ID
774+
value shouldBe setOf(RepositoryRole.READER)
775+
}
774776
}
775777
}
776778

@@ -818,12 +820,14 @@ class DbAuthorizationServiceTest : WordSpec() {
818820
repositoryPermissions = setOf(RepositoryPermission.READ)
819821
)
820822

821-
filter.transitiveIncludes shouldHaveSize 1
822-
filter.transitiveIncludes[CompoundHierarchyId.REPOSITORY_LEVEL] shouldContainExactlyInAnyOrder setOf(
823-
repositoryCompoundId,
824-
otherRepoId,
825-
repoInOtherStructureId
826-
)
823+
filter.transitiveIncludes.entries.shouldBeSingleton { (key, value) ->
824+
key shouldBe CompoundHierarchyId.REPOSITORY_LEVEL
825+
value shouldBe setOf(
826+
repositoryCompoundId,
827+
otherRepoId,
828+
repoInOtherStructureId
829+
)
830+
}
827831
filter.isWildcard shouldBe false
828832
}
829833

@@ -887,10 +891,10 @@ class DbAuthorizationServiceTest : WordSpec() {
887891
productPermissions = setOf(ProductPermission.WRITE)
888892
)
889893

890-
filter.transitiveIncludes[CompoundHierarchyId.PRODUCT_LEVEL] shouldContainExactlyInAnyOrder setOf(
891-
otherProductId
892-
)
893-
filter.transitiveIncludes shouldHaveSize 1
894+
filter.transitiveIncludes.entries.shouldBeSingleton { (key, value) ->
895+
key shouldBe CompoundHierarchyId.PRODUCT_LEVEL
896+
value shouldBe setOf(otherProductId)
897+
}
894898
}
895899

896900
"handle role assignments on higher levels correctly" {
@@ -962,10 +966,10 @@ class DbAuthorizationServiceTest : WordSpec() {
962966
repositoryPermissions = setOf(RepositoryPermission.READ)
963967
)
964968

965-
filter.transitiveIncludes[CompoundHierarchyId.ORGANIZATION_LEVEL] shouldContainExactly setOf(
966-
organizationId
967-
)
968-
filter.transitiveIncludes shouldHaveSize 1
969+
filter.transitiveIncludes.entries.shouldBeSingleton { (key, value) ->
970+
key shouldBe CompoundHierarchyId.ORGANIZATION_LEVEL
971+
value shouldBe setOf(organizationId)
972+
}
969973
}
970974

971975
"handle superuser assignments correctly" {
@@ -1117,10 +1121,10 @@ class DbAuthorizationServiceTest : WordSpec() {
11171121
containedIn = repositoryCompoundId.productId
11181122
)
11191123

1120-
filter.transitiveIncludes shouldHaveSize 1
1121-
filter.transitiveIncludes[CompoundHierarchyId.PRODUCT_LEVEL] shouldContainExactly setOf(
1122-
repositoryCompoundId.parent
1123-
)
1124+
filter.transitiveIncludes.entries.shouldBeSingleton { (key, value) ->
1125+
key shouldBe CompoundHierarchyId.PRODUCT_LEVEL
1126+
value shouldBe setOf(repositoryCompoundId.parent)
1127+
}
11241128
}
11251129

11261130
"handle a containedIn filter together with permissive rights on a higher level" {
@@ -1141,10 +1145,10 @@ class DbAuthorizationServiceTest : WordSpec() {
11411145
containedIn = repositoryCompoundId.productId
11421146
)
11431147

1144-
filter.transitiveIncludes shouldHaveSize 1
1145-
filter.transitiveIncludes[CompoundHierarchyId.PRODUCT_LEVEL] shouldContainExactly setOf(
1146-
repositoryCompoundId.parent
1147-
)
1148+
filter.transitiveIncludes.entries.shouldBeSingleton { (key, value) ->
1149+
key shouldBe CompoundHierarchyId.PRODUCT_LEVEL
1150+
value shouldBe setOf(repositoryCompoundId.parent)
1151+
}
11481152
}
11491153

11501154
"find non-transitive includes" {
@@ -1201,10 +1205,10 @@ class DbAuthorizationServiceTest : WordSpec() {
12011205
containedIn = repositoryCompoundId.productId
12021206
)
12031207

1204-
filter.nonTransitiveIncludes[CompoundHierarchyId.PRODUCT_LEVEL] shouldContainExactly setOf(
1205-
repositoryCompoundId.parent
1206-
)
1207-
filter.nonTransitiveIncludes shouldHaveSize 1
1208+
filter.nonTransitiveIncludes.entries.shouldBeSingleton { (key, value) ->
1209+
key shouldBe CompoundHierarchyId.PRODUCT_LEVEL
1210+
value shouldBe setOf(repositoryCompoundId.parent)
1211+
}
12081212
}
12091213
}
12101214
}

core/src/test/kotlin/api/OrganizationsRouteIntegrationTest.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ import io.kotest.inspectors.forAll
2626
import io.kotest.matchers.collections.containAll
2727
import io.kotest.matchers.collections.containAnyOf
2828
import io.kotest.matchers.collections.shouldBeEmpty
29-
import io.kotest.matchers.collections.shouldContain
29+
import io.kotest.matchers.collections.shouldBeSingleton
3030
import io.kotest.matchers.collections.shouldContainExactly
31-
import io.kotest.matchers.collections.shouldHaveSize
3231
import io.kotest.matchers.maps.shouldContainExactly
3332
import io.kotest.matchers.nulls.beNull
3433
import io.kotest.matchers.nulls.shouldBeNull
@@ -903,8 +902,9 @@ class OrganizationsRouteIntegrationTest : AbstractIntegrationTest({
903902
group.shouldNotBeNull()
904903

905904
val members = keycloakClient.getGroupMembers(group.name)
906-
members shouldHaveSize 1
907-
members.map { it.username } shouldContain TEST_USER.username
905+
members.shouldBeSingleton {
906+
it.username shouldBe TEST_USER.username
907+
}
908908
}
909909
}
910910
}
@@ -923,8 +923,9 @@ class OrganizationsRouteIntegrationTest : AbstractIntegrationTest({
923923
val groupName = role.mapToModel().groupName(createdOrg.id)
924924
val groupBefore = keycloakClient.getGroup(GroupName(groupName))
925925
val membersBefore = keycloakClient.getGroupMembers(groupBefore.name)
926-
membersBefore shouldHaveSize 1
927-
membersBefore.map { it.username } shouldContain TEST_USER.username
926+
membersBefore.shouldBeSingleton {
927+
it.username shouldBe TEST_USER.username
928+
}
928929

929930
val response = superuserClient.delete(
930931
"/api/v1/organizations/${createdOrg.id}/roles/${role.name}?username=${user.username}"

core/src/test/kotlin/api/ProductsRouteIntegrationTest.kt

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import io.kotest.inspectors.forAll
2626
import io.kotest.matchers.collections.containAll
2727
import io.kotest.matchers.collections.containAnyOf
2828
import io.kotest.matchers.collections.shouldBeEmpty
29-
import io.kotest.matchers.collections.shouldContain
29+
import io.kotest.matchers.collections.shouldBeSingleton
3030
import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder
3131
import io.kotest.matchers.collections.shouldHaveSize
3232
import io.kotest.matchers.maps.shouldContainExactly
@@ -613,8 +613,9 @@ class ProductsRouteIntegrationTest : AbstractIntegrationTest({
613613
group.shouldNotBeNull()
614614

615615
val members = keycloakClient.getGroupMembers(group.name)
616-
members shouldHaveSize 1
617-
members.map { it.username } shouldContain TEST_USER.username
616+
members.shouldBeSingleton {
617+
it.username shouldBe TEST_USER.username
618+
}
618619
}
619620
}
620621
}
@@ -633,8 +634,9 @@ class ProductsRouteIntegrationTest : AbstractIntegrationTest({
633634
val groupName = role.mapToModel().groupName(createdProd.id)
634635
val groupBefore = keycloakClient.getGroup(GroupName(groupName))
635636
val membersBefore = keycloakClient.getGroupMembers(groupBefore.name)
636-
membersBefore shouldHaveSize 1
637-
membersBefore.map { it.username } shouldContain TEST_USER.username
637+
membersBefore.shouldBeSingleton {
638+
it.username shouldBe TEST_USER.username
639+
}
638640

639641
val response = superuserClient.delete(
640642
"/api/v1/products/${createdProd.id}/roles/${role.name}?username=${user.username}"
@@ -1572,12 +1574,9 @@ class ProductsRouteIntegrationTest : AbstractIntegrationTest({
15721574
responseSpecific shouldHaveStatus HttpStatusCode.Created
15731575

15741576
val createdRunsSpecific = responseSpecific.body<List<OrtRun>>()
1575-
createdRunsSpecific shouldHaveSize 1
1576-
1577-
val repositoryIdsSpecific = createdRunsSpecific.map { run ->
1578-
dbExtension.fixtures.ortRunRepository.get(run.id)?.repositoryId
1577+
createdRunsSpecific.shouldBeSingleton {
1578+
dbExtension.fixtures.ortRunRepository.get(it.id)?.repositoryId shouldBe repository1Id
15791579
}
1580-
repositoryIdsSpecific shouldContainExactlyInAnyOrder listOf(repository1Id)
15811580
}
15821581
}
15831582

core/src/test/kotlin/api/RepositoriesRouteIntegrationTest.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ import io.kotest.data.row
2525
import io.kotest.inspectors.forAll
2626
import io.kotest.matchers.collections.containAnyOf
2727
import io.kotest.matchers.collections.shouldBeEmpty
28-
import io.kotest.matchers.collections.shouldContain
28+
import io.kotest.matchers.collections.shouldBeSingleton
2929
import io.kotest.matchers.collections.shouldContainExactly
30-
import io.kotest.matchers.collections.shouldHaveSize
3130
import io.kotest.matchers.nulls.beNull
3231
import io.kotest.matchers.nulls.shouldNotBeNull
3332
import io.kotest.matchers.shouldBe
@@ -1103,8 +1102,9 @@ class RepositoriesRouteIntegrationTest : AbstractIntegrationTest({
11031102
group.shouldNotBeNull()
11041103

11051104
val members = keycloakClient.getGroupMembers(group.name)
1106-
members shouldHaveSize 1
1107-
members.map { it.username } shouldContain TEST_USER.username
1105+
members.shouldBeSingleton {
1106+
it.username shouldBe TEST_USER.username
1107+
}
11081108
}
11091109
}
11101110
}
@@ -1123,8 +1123,9 @@ class RepositoriesRouteIntegrationTest : AbstractIntegrationTest({
11231123
val groupName = role.mapToModel().groupName(createdRepo.id)
11241124
val groupBefore = keycloakClient.getGroup(GroupName(groupName))
11251125
val membersBefore = keycloakClient.getGroupMembers(groupBefore.name)
1126-
membersBefore shouldHaveSize 1
1127-
membersBefore.map { it.username } shouldContain TEST_USER.username
1126+
membersBefore.shouldBeSingleton {
1127+
it.username shouldBe TEST_USER.username
1128+
}
11281129

11291130
val response = superuserClient.delete(
11301131
"/api/v1/repositories/${createdRepo.id}/roles/${role.name}?username=${user.username}"

services/ort-run/src/test/kotlin/PackageServiceTest.kt

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import io.kotest.core.spec.style.WordSpec
2323
import io.kotest.matchers.collections.beEmpty
2424
import io.kotest.matchers.collections.containExactly
2525
import io.kotest.matchers.collections.containExactlyInAnyOrder
26+
import io.kotest.matchers.collections.shouldBeSingleton
2627
import io.kotest.matchers.collections.shouldContainInOrder
2728
import io.kotest.matchers.collections.shouldHaveSize
2829
import io.kotest.matchers.maps.containExactly as containExactlyEntries
@@ -380,8 +381,9 @@ class PackageServiceTest : WordSpec() {
380381
)
381382
)
382383

383-
results.data shouldHaveSize 1
384-
results.data.first().pkg.identifier shouldBe Identifier("NPM", "", "example", "1.0")
384+
results.data.shouldBeSingleton {
385+
it.pkg.identifier shouldBe Identifier("NPM", "", "example", "1.0")
386+
}
385387
}
386388

387389
"use case insensitive filtering for purl and identifier filtering" {
@@ -413,10 +415,10 @@ class PackageServiceTest : WordSpec() {
413415
)
414416
)
415417

416-
results1.data shouldHaveSize 1
417418
results1.totalCount shouldBe 1
418-
419-
results1.data.first().pkg.identifier shouldBe Identifier("Maven", "com.example", "example2", "1.0")
419+
results1.data.shouldBeSingleton {
420+
it.pkg.identifier shouldBe Identifier("Maven", "com.example", "example2", "1.0")
421+
}
420422

421423
results2 shouldBe results1
422424
}
@@ -441,10 +443,10 @@ class PackageServiceTest : WordSpec() {
441443
)
442444
)
443445

444-
results.data shouldHaveSize 1
445446
results.totalCount shouldBe 1
446-
447-
results.data.first().pkg.purl shouldBe "pkg:NPM/com.example/[email protected]"
447+
results.data.shouldBeSingleton {
448+
it.pkg.purl shouldBe "pkg:NPM/com.example/[email protected]"
449+
}
448450
}
449451

450452
"allow filtering by processed declared license" {
@@ -545,17 +547,19 @@ class PackageServiceTest : WordSpec() {
545547
with(packages.data.single { it.pkg.identifier == pkg1.identifier }) {
546548
pkg.authors should containExactly(*curation1.data.authors.orEmpty().toTypedArray())
547549
concludedLicense shouldBe curation1.data.concludedLicense
548-
curations shouldHaveSize 1
549-
curations.first() shouldBe curation1.data
550+
curations.shouldBeSingleton {
551+
it shouldBe curation1.data
552+
}
550553
}
551554

552555
with(packages.data.single { it.pkg.identifier == pkg2.identifier }) {
553556
pkg.processedDeclaredLicense.spdxExpression shouldBe "LicenseRef-declared1 AND LicenseRef-mapped"
554557
pkg.processedDeclaredLicense.mappedLicenses should
555558
containExactlyEntries("invalid-license" to "LicenseRef-mapped")
556559
concludedLicense shouldBe curation2.data.concludedLicense
557-
curations shouldHaveSize 1
558-
curations.first() shouldBe curation2.data
560+
curations.shouldBeSingleton {
561+
it shouldBe curation2.data
562+
}
559563
}
560564
}
561565
}

services/ort-run/src/test/kotlin/RuleViolationServiceTest.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.eclipse.apoapsis.ortserver.services.ortrun
2121

2222
import io.kotest.core.spec.style.WordSpec
23+
import io.kotest.matchers.collections.shouldBeSingleton
2324
import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder
2425
import io.kotest.matchers.collections.shouldHaveSize
2526
import io.kotest.matchers.shouldBe
@@ -162,10 +163,12 @@ class RuleViolationServiceTest : WordSpec() {
162163
ruleViolationFilter = RuleViolationFilters(resolved = false)
163164
).data
164165

165-
resultsResolved shouldHaveSize 1
166-
resultsResolved[0].rule shouldBe "Rule-1"
167-
resultsResolved[0].resolutions shouldHaveSize 1
168-
resultsResolved[0].resolutions[0].message shouldBe "Message-1"
166+
resultsResolved.shouldBeSingleton {
167+
it.rule shouldBe "Rule-1"
168+
it.resolutions.shouldBeSingleton { resolution ->
169+
resolution.message shouldBe "Message-1"
170+
}
171+
}
169172

170173
resultsUnresolved shouldHaveSize 2
171174
resultsUnresolved[0].rule shouldBe "Rule-2"

0 commit comments

Comments
 (0)