Skip to content

Commit 56f3034

Browse files
committedFeb 11, 2025
test(model): Turn createResolvedLicenseInfo() into a constant
Move the logic out of the class for readability and use a constant to execute the construction only once. This requires to also move out the constants for the license expressions. Signed-off-by: Frank Viernau <x9fviern@zeiss.com>
1 parent c28f005 commit 56f3034

File tree

1 file changed

+73
-75
lines changed

1 file changed

+73
-75
lines changed
 

‎model/src/test/kotlin/licenses/ResolvedLicenseInfoTest.kt

+73-75
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,19 @@ import org.ossreviewtoolkit.utils.spdx.SpdxSingleLicenseExpression
3232
import org.ossreviewtoolkit.utils.spdx.toSpdx
3333

3434
class ResolvedLicenseInfoTest : WordSpec() {
35-
private val mit = "MIT"
36-
private val apache = "Apache-2.0 WITH LLVM-exception"
37-
private val gpl = "GPL-2.0-only"
38-
private val bsd = "0BSD"
39-
4035
init {
4136
"effectiveLicense()" should {
4237
"apply choices for LicenseView.ALL on all resolved licenses" {
4338
// All: (Apache-2.0 WITH LLVM-exception OR MIT) AND (MIT OR GPL-2.0-only) AND (0BSD OR GPL-2.0-only)
4439
val choices = listOf(
45-
SpdxLicenseChoice("$apache OR $mit".toSpdx(), mit.toSpdx()),
46-
SpdxLicenseChoice("$mit OR $gpl".toSpdx(), mit.toSpdx()),
47-
SpdxLicenseChoice("$bsd OR $gpl".toSpdx(), bsd.toSpdx())
40+
SpdxLicenseChoice("$APACHE OR $MIT".toSpdx(), MIT.toSpdx()),
41+
SpdxLicenseChoice("$MIT OR $GPL".toSpdx(), MIT.toSpdx()),
42+
SpdxLicenseChoice("$BSD OR $GPL".toSpdx(), BSD.toSpdx())
4843
)
4944

50-
val effectiveLicense = createResolvedLicenseInfo().effectiveLicense(LicenseView.ALL, choices)
45+
val effectiveLicense = RESOLVED_LICENSE_INFO.effectiveLicense(LicenseView.ALL, choices)
5146

52-
effectiveLicense shouldBe "$mit AND $bsd".toSpdx()
47+
effectiveLicense shouldBe "$MIT AND $BSD".toSpdx()
5348
}
5449

5550
"apply a choice for a sub-expression only" {
@@ -59,10 +54,10 @@ class ResolvedLicenseInfoTest : WordSpec() {
5954
licenseInfo = mockk(),
6055
licenses = listOf(
6156
ResolvedLicense(
62-
license = apache.toSpdx() as SpdxSingleLicenseExpression,
63-
originalDeclaredLicenses = setOf("$apache OR $mit"),
57+
license = APACHE.toSpdx() as SpdxSingleLicenseExpression,
58+
originalDeclaredLicenses = setOf("$APACHE OR $MIT"),
6459
originalExpressions = setOf(
65-
ResolvedOriginalExpression("$apache OR $mit OR $gpl".toSpdx(), LicenseSource.DECLARED)
60+
ResolvedOriginalExpression("$APACHE OR $MIT OR $GPL".toSpdx(), LicenseSource.DECLARED)
6661
),
6762
locations = emptySet()
6863
)
@@ -72,122 +67,125 @@ class ResolvedLicenseInfoTest : WordSpec() {
7267
)
7368

7469
val choices = listOf(
75-
SpdxLicenseChoice("$apache OR $mit".toSpdx(), mit.toSpdx())
70+
SpdxLicenseChoice("$APACHE OR $MIT".toSpdx(), MIT.toSpdx())
7671
)
7772

7873
val effectiveLicense = resolvedLicenseInfo.effectiveLicense(LicenseView.ONLY_DECLARED, choices)
7974

80-
effectiveLicense shouldBe "$mit OR $gpl".toSpdx()
75+
effectiveLicense shouldBe "$MIT OR $GPL".toSpdx()
8176
}
8277

8378
"apply choices for LicenseView.CONCLUDED_OR_DECLARED_AND_DETECTED" {
8479
// Concluded: 0BSD OR GPL-2.0-only
8580
val choices = listOf(
86-
SpdxLicenseChoice("$bsd OR $gpl".toSpdx(), bsd.toSpdx())
81+
SpdxLicenseChoice("$BSD OR $GPL".toSpdx(), BSD.toSpdx())
8782
)
8883

89-
val effectiveLicense = createResolvedLicenseInfo().effectiveLicense(
84+
val effectiveLicense = RESOLVED_LICENSE_INFO.effectiveLicense(
9085
LicenseView.CONCLUDED_OR_DECLARED_AND_DETECTED,
9186
choices
9287
)
9388

94-
effectiveLicense shouldBe bsd.toSpdx()
89+
effectiveLicense shouldBe BSD.toSpdx()
9590
}
9691

9792
"apply choices for LicenseView.ONLY_DECLARED" {
9893
// Declared: Apache-2.0 WITH LLVM-exception OR MIT
9994
val choices = listOf(
100-
SpdxLicenseChoice("$apache OR $mit".toSpdx(), mit.toSpdx())
95+
SpdxLicenseChoice("$APACHE OR $MIT".toSpdx(), MIT.toSpdx())
10196
)
10297

103-
val effectiveLicense = createResolvedLicenseInfo().effectiveLicense(
98+
val effectiveLicense = RESOLVED_LICENSE_INFO.effectiveLicense(
10499
LicenseView.ONLY_DECLARED,
105100
choices
106101
)
107102

108-
effectiveLicense shouldBe mit.toSpdx()
103+
effectiveLicense shouldBe MIT.toSpdx()
109104
}
110105

111106
"apply package and repository license choice for LicenseView.ONLY_CONCLUDED in the correct order" {
112107
val repositoryChoices = listOf(
113-
SpdxLicenseChoice("$apache OR $mit".toSpdx(), mit.toSpdx()),
114-
SpdxLicenseChoice("$bsd OR $gpl".toSpdx(), bsd.toSpdx())
108+
SpdxLicenseChoice("$APACHE OR $MIT".toSpdx(), MIT.toSpdx()),
109+
SpdxLicenseChoice("$BSD OR $GPL".toSpdx(), BSD.toSpdx())
115110
)
116111
val packageChoices = listOf(
117-
SpdxLicenseChoice("$apache OR $mit".toSpdx(), apache.toSpdx())
112+
SpdxLicenseChoice("$APACHE OR $MIT".toSpdx(), APACHE.toSpdx())
118113
)
119114

120-
val effectiveLicense = createResolvedLicenseInfo().effectiveLicense(
115+
val effectiveLicense = RESOLVED_LICENSE_INFO.effectiveLicense(
121116
LicenseView.ALL,
122117
packageChoices,
123118
repositoryChoices
124119
)
125120

126-
effectiveLicense shouldBe "$apache and ($mit or $gpl) and $bsd".toSpdx()
121+
effectiveLicense shouldBe "$APACHE and ($MIT or $GPL) and $BSD".toSpdx()
127122
}
128123
}
129124

130125
"applyChoices(licenseChoices)" should {
131126
"apply license choices on all licenses" {
132-
val resolvedLicenseInfo = createResolvedLicenseInfo()
133-
134127
val choices = listOf(
135-
SpdxLicenseChoice("$apache OR $mit".toSpdx(), mit.toSpdx()),
136-
SpdxLicenseChoice("$mit OR $gpl".toSpdx(), mit.toSpdx()),
137-
SpdxLicenseChoice("$bsd OR $gpl".toSpdx(), bsd.toSpdx())
128+
SpdxLicenseChoice("$APACHE OR $MIT".toSpdx(), MIT.toSpdx()),
129+
SpdxLicenseChoice("$MIT OR $GPL".toSpdx(), MIT.toSpdx()),
130+
SpdxLicenseChoice("$BSD OR $GPL".toSpdx(), BSD.toSpdx())
138131
)
139132

140-
val filteredResolvedLicenseInfo = resolvedLicenseInfo.applyChoices(choices)
133+
val filteredResolvedLicenseInfo = RESOLVED_LICENSE_INFO.applyChoices(choices)
141134

142-
filteredResolvedLicenseInfo.licenses should containLicensesExactly(mit, bsd)
135+
filteredResolvedLicenseInfo.licenses should containLicensesExactly(MIT, BSD)
143136
}
144137
}
145138
}
139+
}
146140

147-
private fun createResolvedLicenseInfo(): ResolvedLicenseInfo {
148-
val resolvedLicenses = listOf(
149-
ResolvedLicense(
150-
license = apache.toSpdx() as SpdxSingleLicenseExpression,
151-
originalDeclaredLicenses = setOf("$apache OR $mit"),
152-
originalExpressions = setOf(
153-
ResolvedOriginalExpression("$apache OR $mit".toSpdx(), LicenseSource.DECLARED)
154-
),
155-
locations = emptySet()
141+
private const val MIT = "MIT"
142+
private const val APACHE = "Apache-2.0 WITH LLVM-exception"
143+
private const val GPL = "GPL-2.0-only"
144+
private const val BSD = "0BSD"
145+
146+
private val RESOLVED_LICENSE_INFO: ResolvedLicenseInfo by lazy {
147+
val resolvedLicenses = listOf(
148+
ResolvedLicense(
149+
license = APACHE.toSpdx() as SpdxSingleLicenseExpression,
150+
originalDeclaredLicenses = setOf("$APACHE OR $MIT"),
151+
originalExpressions = setOf(
152+
ResolvedOriginalExpression("$APACHE OR $MIT".toSpdx(), LicenseSource.DECLARED)
156153
),
157-
ResolvedLicense(
158-
license = mit.toSpdx() as SpdxSingleLicenseExpression,
159-
originalDeclaredLicenses = setOf("$apache OR $mit"),
160-
originalExpressions = setOf(
161-
ResolvedOriginalExpression("$apache OR $mit".toSpdx(), LicenseSource.DECLARED),
162-
ResolvedOriginalExpression("$mit OR $gpl".toSpdx(), LicenseSource.DETECTED)
163-
),
164-
locations = emptySet()
154+
locations = emptySet()
155+
),
156+
ResolvedLicense(
157+
license = MIT.toSpdx() as SpdxSingleLicenseExpression,
158+
originalDeclaredLicenses = setOf("$APACHE OR $MIT"),
159+
originalExpressions = setOf(
160+
ResolvedOriginalExpression("$APACHE OR $MIT".toSpdx(), LicenseSource.DECLARED),
161+
ResolvedOriginalExpression("$MIT OR $GPL".toSpdx(), LicenseSource.DETECTED)
165162
),
166-
ResolvedLicense(
167-
license = gpl.toSpdx() as SpdxSingleLicenseExpression,
168-
originalDeclaredLicenses = emptySet(),
169-
originalExpressions = setOf(
170-
ResolvedOriginalExpression("$mit OR $gpl".toSpdx(), LicenseSource.DETECTED),
171-
ResolvedOriginalExpression("$bsd OR $gpl".toSpdx(), LicenseSource.CONCLUDED)
172-
),
173-
locations = emptySet()
163+
locations = emptySet()
164+
),
165+
ResolvedLicense(
166+
license = GPL.toSpdx() as SpdxSingleLicenseExpression,
167+
originalDeclaredLicenses = emptySet(),
168+
originalExpressions = setOf(
169+
ResolvedOriginalExpression("$MIT OR $GPL".toSpdx(), LicenseSource.DETECTED),
170+
ResolvedOriginalExpression("$BSD OR $GPL".toSpdx(), LicenseSource.CONCLUDED)
174171
),
175-
ResolvedLicense(
176-
license = bsd.toSpdx() as SpdxSingleLicenseExpression,
177-
originalDeclaredLicenses = emptySet(),
178-
originalExpressions = setOf(
179-
ResolvedOriginalExpression("$bsd OR $gpl".toSpdx(), LicenseSource.CONCLUDED)
180-
),
181-
locations = emptySet()
182-
)
183-
)
184-
185-
return ResolvedLicenseInfo(
186-
id = Identifier.EMPTY,
187-
licenseInfo = mockk(),
188-
licenses = resolvedLicenses,
189-
copyrightGarbage = emptyMap(),
190-
unmatchedCopyrights = emptyMap()
172+
locations = emptySet()
173+
),
174+
ResolvedLicense(
175+
license = BSD.toSpdx() as SpdxSingleLicenseExpression,
176+
originalDeclaredLicenses = emptySet(),
177+
originalExpressions = setOf(
178+
ResolvedOriginalExpression("$BSD OR $GPL".toSpdx(), LicenseSource.CONCLUDED)
179+
),
180+
locations = emptySet()
191181
)
192-
}
182+
)
183+
184+
ResolvedLicenseInfo(
185+
id = Identifier.EMPTY,
186+
licenseInfo = mockk(),
187+
licenses = resolvedLicenses,
188+
copyrightGarbage = emptyMap(),
189+
unmatchedCopyrights = emptyMap()
190+
)
193191
}

0 commit comments

Comments
 (0)