|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>) |
| 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 | + * https://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 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + * License-Filename: LICENSE |
| 18 | + */ |
| 19 | + |
| 20 | +package org.ossreviewtoolkit.plugins.scanners.fossid |
| 21 | + |
| 22 | +import io.kotest.assertions.throwables.shouldThrow |
| 23 | +import io.kotest.core.spec.style.WordSpec |
| 24 | +import io.kotest.core.test.TestCase |
| 25 | +import io.kotest.core.test.TestResult |
| 26 | +import io.kotest.matchers.equals.shouldBeEqual |
| 27 | +import io.kotest.matchers.ints.shouldBeLessThanOrEqual |
| 28 | + |
| 29 | +import io.mockk.every |
| 30 | +import io.mockk.mockkStatic |
| 31 | +import io.mockk.unmockkAll |
| 32 | + |
| 33 | +import java.time.LocalDateTime |
| 34 | + |
| 35 | +class FossIdNamingProviderTest : WordSpec() { |
| 36 | + |
| 37 | + override suspend fun afterEach(testCase: TestCase, result: TestResult) { |
| 38 | + unmockkAll() |
| 39 | + } |
| 40 | + |
| 41 | + companion object { |
| 42 | + const val MAX_SCAN_CODE_LEN = 255 |
| 43 | + } |
| 44 | + |
| 45 | + init { |
| 46 | + "createScanCode" should { |
| 47 | + val namingProvider = FossIdNamingProvider(null, null, emptyMap()) |
| 48 | + |
| 49 | + val mockedDateTime = LocalDateTime.of(2024, 4, 1, 10, 0) |
| 50 | + val expectedTimestamp = "20240401_100000" |
| 51 | + |
| 52 | + val longBranchName = "origin/feature/LOREM-123321_lorem-ipsum-dolor-sit-amet-consectetur-adipiscing-elit-" + |
| 53 | + "aliquam-laoreet-ac-nulla-in-bibendum-phasellus-sodales-vel-lorem-consequat-efficitur-morbi-viverra-a" + |
| 54 | + "ccumsan-libero-a-tincidunt-libero-venenatis-nec-nulla-facilisi-vestibulum-pharetra-finibus-mi-vitae-" + |
| 55 | + "luctus" |
| 56 | + |
| 57 | + val longScanPattern = "#projectName_#currentTimestamp_lorem-ipsum-dolor-sit-amet-consectetur-adipiscing-e" + |
| 58 | + "lit-aliquam-laoreet-ac-nulla-in-bibendum-phasellus-sodales-vel-lorem-consequat-efficitur-morbi-viver" + |
| 59 | + "ra-accumsan-libero-a-tincidunt-libero-venenatis-nec-nulla-facilisi-vestibulum-pharetra-finibus-mi-vi" + |
| 60 | + "tae-luctus" |
| 61 | + |
| 62 | + "create code without branch name, when it's empty" { |
| 63 | + mockkStatic(LocalDateTime::class) { |
| 64 | + every { LocalDateTime.now() } returns mockedDateTime |
| 65 | + |
| 66 | + namingProvider.createScanCode( |
| 67 | + "example-project-name", null, "" |
| 68 | + ) shouldBeEqual "example-project-name_$expectedTimestamp" |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + "create code with branch name" { |
| 73 | + mockkStatic(LocalDateTime::class) { |
| 74 | + every { LocalDateTime.now() } returns mockedDateTime |
| 75 | + |
| 76 | + namingProvider.createScanCode( |
| 77 | + "example-project-name", null, "CODE-2233_Red-dots-added-to-layout" |
| 78 | + ) shouldBeEqual "example-project-name_" + expectedTimestamp + "_CODE-2233_Red-dots-added-to-layout" |
| 79 | + } |
| 80 | + unmockkAll() |
| 81 | + } |
| 82 | + |
| 83 | + "create code with branch name and delta tag" { |
| 84 | + mockkStatic(LocalDateTime::class) { |
| 85 | + every { LocalDateTime.now() } returns mockedDateTime |
| 86 | + |
| 87 | + namingProvider.createScanCode( |
| 88 | + "example-project-name", FossId.DeltaTag.DELTA, "CODE-2233_Red-dots-added-to-layout" |
| 89 | + ) shouldBeEqual "example-project-name_" + expectedTimestamp + |
| 90 | + "_delta_CODE-2233_Red-dots-added-to-layout" |
| 91 | + } |
| 92 | + unmockkAll() |
| 93 | + } |
| 94 | + |
| 95 | + "remove all non-standard signs from branch name when creating code" { |
| 96 | + mockkStatic(LocalDateTime::class) { |
| 97 | + every { LocalDateTime.now() } returns mockedDateTime |
| 98 | + |
| 99 | + namingProvider.createScanCode( |
| 100 | + "example-project-name", null, "feature/CODE-12%%$@@&^_SOME_*&^#!*text!!" |
| 101 | + ) shouldBeEqual "example-project-name_" + |
| 102 | + expectedTimestamp + "_feature_CODE-12________SOME_______text__" |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + "truncate very long scan id to fit maximum length accepted by FossID (255 chars)" { |
| 107 | + mockkStatic(LocalDateTime::class) { |
| 108 | + every { LocalDateTime.now() } returns mockedDateTime |
| 109 | + |
| 110 | + namingProvider.createScanCode( |
| 111 | + "example-project-name", FossId.DeltaTag.DELTA, longBranchName |
| 112 | + ).length shouldBeLessThanOrEqual MAX_SCAN_CODE_LEN |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + "create code without branch name form custom naming pattern" { |
| 117 | + val customScanPattern = "#projectName_#currentTimestamp" |
| 118 | + |
| 119 | + val namingProviderWithLongScanPattern = FossIdNamingProvider(null, customScanPattern, emptyMap()) |
| 120 | + mockkStatic(LocalDateTime::class) { |
| 121 | + every { LocalDateTime.now() } returns mockedDateTime |
| 122 | + |
| 123 | + namingProviderWithLongScanPattern.createScanCode( |
| 124 | + "example-project-name", null, "" |
| 125 | + ) shouldBeEqual "example-project-name_20240401_100000" |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + "create code without branch name form custom naming pattern when branch name is provided" { |
| 130 | + val customScanPattern = "#projectName_#currentTimestamp" |
| 131 | + |
| 132 | + val namingProviderWithLongScanPattern = FossIdNamingProvider(null, customScanPattern, emptyMap()) |
| 133 | + mockkStatic(LocalDateTime::class) { |
| 134 | + every { LocalDateTime.now() } returns mockedDateTime |
| 135 | + |
| 136 | + namingProviderWithLongScanPattern.createScanCode( |
| 137 | + "example-project-name", null, "feature/LOREM-3212" |
| 138 | + ) shouldBeEqual "example-project-name_20240401_100000" |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + "create code without branch name form custom naming pattern when too long branch name is provided" { |
| 143 | + val customScanPattern = "#projectName_#currentTimestamp_#branch" |
| 144 | + val namingProviderWithLongScanPattern = FossIdNamingProvider(null, customScanPattern, emptyMap()) |
| 145 | + mockkStatic(LocalDateTime::class) { |
| 146 | + every { LocalDateTime.now() } returns mockedDateTime |
| 147 | + |
| 148 | + namingProviderWithLongScanPattern.createScanCode( |
| 149 | + "example-project-name", null, longBranchName |
| 150 | + ).length shouldBeLessThanOrEqual MAX_SCAN_CODE_LEN |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + "throw an exception if scan code pattern is too long" { |
| 155 | + val namingProviderWithLongScanPattern = FossIdNamingProvider(null, longScanPattern, emptyMap()) |
| 156 | + mockkStatic(LocalDateTime::class) { |
| 157 | + every { LocalDateTime.now() } returns mockedDateTime |
| 158 | + |
| 159 | + shouldThrow<IllegalArgumentException> { |
| 160 | + namingProviderWithLongScanPattern.createScanCode("example-project-name", null, "") |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments