Skip to content

Commit

Permalink
Merge pull request #63 from leanix/feature/CID-2058/enhance-test-suite
Browse files Browse the repository at this point in the history
CID-2058 Enhance test suite
  • Loading branch information
geoandri authored Oct 26, 2023
2 parents f5ac38e + 4261af6 commit 776fed1
Show file tree
Hide file tree
Showing 14 changed files with 508 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ The second `-v` param is the path to temporary folder that the `vsm-sbom-booster

`DEV_MODE`(optional): This is a flag to enable/disable the dev mode. When enabled, all logs from ORT containers will be preserved in the temp folder location and the ORT project folders will be retained. Additionally, the logging level for the ORT containers will be set to DEBUG. The processing of each repository consists of three separate phases (download, analyze and generate_sbom) that are facilitated by the ORT software. The produced logs are saved in the temp folder using the `<repository_name>_<phase>_log.txt` naming pattern.This is useful for debugging purposes. Default: false

`ALLOW_NO_COMPONENT_SBOMS`(optional): This is a flag to enable/disable the submission of SBOM files without any components. Default: true
`ALLOW_NO_COMPONENT_SBOMS`(optional): This is a flag to enable/disable the submission of SBOM files without any components. Default: false

`HTTP_PROXY`: (optional): The HTTP proxy to use for the ORT container (See https://github.com/oss-review-toolkit/ort#environment-variables). Default: none

Expand Down
4 changes: 3 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ dependencies {
implementation("com.expediagroup:graphql-kotlin-spring-client:6.2.2")
implementation("org.cyclonedx:cyclonedx-core-java:7.2.0")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("io.mockk:mockk:1.13.8")
testImplementation("com.ninja-squad:springmockk:4.0.2"){
exclude(module = "mockito-core")
}
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ data class PropertiesConfiguration(
val devMode: Boolean,
val httpProxy: String?,
val httpsProxy: String?,
val allowNoComponentSboms: Boolean = true,
val allowNoComponentSboms: Boolean = false,

// LeanIX configs
@field:NotBlank
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import org.springframework.web.client.RestTemplate
import java.util.*

@Service
class MtMService {
class MtMService(
private val restTemplate: RestTemplate
) {

fun getAccessToken(host: String, lxToken: String): String {
val restTemplate = RestTemplate()
val headers = HttpHeaders()

val auth = "apitoken:$lxToken"
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ leanix:
dev-mode: ${DEV_MODE:false}
http-proxy: ${HTTP_PROXY:}
https-proxy: ${HTTPS_PROXY:}
allow-no-component-sboms: ${ALLOW_NO_COMPONENT_SBOMS:true}
allow-no-component-sboms: ${ALLOW_NO_COMPONENT_SBOMS:false}

# LeanIX configs
leanIx-host: ${LEANIX_HOST:}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,47 @@
package net.leanix.vsm.sbomBooster

import com.ninjasquad.springmockk.MockkBean
import net.leanix.vsm.sbomBooster.service.BitBucketApiService
import net.leanix.vsm.sbomBooster.service.ExitScheduler
import net.leanix.vsm.sbomBooster.service.GitHubApiService
import net.leanix.vsm.sbomBooster.service.GitLabApiService
import net.leanix.vsm.sbomBooster.service.MtMService
import net.leanix.vsm.sbomBooster.service.OrtService
import net.leanix.vsm.sbomBooster.service.SummaryReportService
import net.leanix.vsm.sbomBooster.service.VsmDiscoveryService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import java.util.concurrent.ThreadPoolExecutor

@SpringBootTest
class VsmSbomBoosterApplicationTests
class VsmSbomBoosterApplicationTests {
@MockkBean
internal lateinit var threadPoolExecutor: ThreadPoolExecutor

@MockkBean(name = "taskExecutor")
internal lateinit var taskExecutor: ThreadPoolExecutor

@MockkBean
internal lateinit var exitScheduler: ExitScheduler

@MockkBean(relaxed = true)
internal lateinit var summaryReportService: SummaryReportService

@MockkBean
internal lateinit var vsmDiscoveryService: VsmDiscoveryService

@MockkBean(relaxed = true)
internal lateinit var mtMService: MtMService

@Autowired
internal lateinit var bitBucketApiService: BitBucketApiService

@Autowired
internal lateinit var gitHubApiService: GitHubApiService

@Autowired
internal lateinit var gitLabApiService: GitLabApiService

@Autowired
internal lateinit var ortService: OrtService
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package net.leanix.vsm.sbomBooster

import io.mockk.verify
import net.leanix.vsm.sbomBooster.configuration.BitBucketTestConfiguration
import org.junit.jupiter.api.Test
import org.springframework.context.annotation.Import

@Import(BitBucketTestConfiguration::class)
class VsmSbomBoosterBitBucketApplicationTests : VsmSbomBoosterApplicationTests() {

@Test
fun `test BitBucket integration`() {
verify(exactly = 1) {
summaryReportService.appendRecord(
"BITBUCKET_WORKSPACE: bitbucketWorkspace\n"
)
}
verify(exactly = 1) { bitBucketApiService.getUsername("") }
verify(exactly = 1) {
bitBucketApiService
.getRepositories("bitbucketKey:bitbucketSecret", "bitbucketWorkspace")
}
verify(exactly = 1) {
ortService.downloadProject(
"cloneUrl1",
"bitbucketUsername",
"bitbucketToken",
)
}
verify(exactly = 1) {
ortService.downloadProject(
"cloneUrl2",
"bitbucketUsername",
"bitbucketToken",
)
}
verify(exactly = 1) {
ortService.analyzeProject(
"cloneUrl1",
"downloadedFolder",
)
}
verify(exactly = 1) {
ortService.analyzeProject(
"cloneUrl2",
"downloadedFolder",
)
}
verify(exactly = 1) { ortService.generateSbom("cloneUrl1") }
verify(exactly = 1) { ortService.generateSbom("cloneUrl2") }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package net.leanix.vsm.sbomBooster

import io.mockk.verify
import net.leanix.vsm.sbomBooster.configuration.GitHubTestConfiguration
import org.junit.jupiter.api.Test
import org.springframework.context.annotation.Import

@Import(GitHubTestConfiguration::class)
class VsmSbomBoosterGitHubApplicationTests : VsmSbomBoosterApplicationTests() {
@Test
fun `test GitHub integration`() {
verify(exactly = 1) {
summaryReportService.appendRecord(
"GITHUB_ORGANIZATION: githubOrganization\n"
)
}
verify(exactly = 1) { gitHubApiService.getUsername("githubToken") }
verify(exactly = 1) { gitHubApiService.getRepositories("githubToken", "githubOrganization") }
verify(exactly = 1) {
ortService.downloadProject(
"cloneUrl1",
"githubUsername",
"githubToken",
)
}
verify(exactly = 1) {
ortService.downloadProject(
"cloneUrl2",
"githubUsername",
"githubToken",
)
}
verify(exactly = 1) {
ortService.analyzeProject(
"cloneUrl1",
"downloadedFolder"
)
}
verify(exactly = 1) {
ortService.analyzeProject(
"cloneUrl2",
"downloadedFolder"
)
}
verify(exactly = 1) { ortService.generateSbom("cloneUrl1") }
verify(exactly = 1) { ortService.generateSbom("cloneUrl2") }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package net.leanix.vsm.sbomBooster

import io.mockk.verify
import net.leanix.vsm.sbomBooster.configuration.GitLabTestConfiguration
import org.junit.jupiter.api.Test
import org.springframework.context.annotation.Import

@Import(GitLabTestConfiguration::class)
class VsmSbomBoosterGitLabApplicationTests : VsmSbomBoosterApplicationTests() {
@Test
fun `test GitLab integration`() {
verify(exactly = 1) {
summaryReportService.appendRecord(
"GITLAB_GROUP: gitlabGroup\n"
)
}
verify(exactly = 1) { gitLabApiService.getUsername("gitlabToken") }
verify(exactly = 1) { gitLabApiService.getRepositories("gitlabToken", "gitlabGroup") }
verify(exactly = 1) {
ortService.downloadProject(
"cloneUrl1",
"gitlabUsername",
"gitlabToken",
)
}
verify(exactly = 1) {
ortService.downloadProject(
"cloneUrl2",
"gitlabUsername",
"gitlabToken",
)
}
verify(exactly = 1) {
ortService.analyzeProject(
"cloneUrl1",
"downloadedFolder"
)
}
verify(exactly = 1) {
ortService.analyzeProject(
"cloneUrl2",
"downloadedFolder"
)
}
verify(exactly = 1) { ortService.generateSbom("cloneUrl1") }
verify(exactly = 1) { ortService.generateSbom("cloneUrl2") }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package net.leanix.vsm.sbomBooster.configuration

import com.ninjasquad.springmockk.MockkBean
import io.mockk.every
import jakarta.annotation.PostConstruct
import net.leanix.vsm.sbomBooster.domain.Repository
import net.leanix.vsm.sbomBooster.service.BitBucketApiService
import org.springframework.boot.test.context.TestConfiguration

@TestConfiguration
class BitBucketTestConfiguration : GenericTestConfiguration() {

@MockkBean
private lateinit var bitBucketApiService: BitBucketApiService

@PostConstruct
fun prepare() {
every { propertiesConfiguration.bitbucketKey } returns "bitbucketKey"
every { propertiesConfiguration.gitProvider } returns "bitbucket"
every { propertiesConfiguration.bitbucketSecret } returns "bitbucketSecret"
every { propertiesConfiguration.bitbucketWorkspace } returns "bitbucketWorkspace"
every { propertiesConfiguration.bitbucketKey } returns "bitbucketKey"

every { bitBucketApiService.getUsername("") } returns "bitbucketUsername"
every { bitBucketApiService.authenticate("bitbucketKey:bitbucketSecret") } returns "bitbucketToken"
every {
bitBucketApiService
.getRepositories("bitbucketKey:bitbucketSecret", "bitbucketWorkspace")
} returns listOf(
Repository(
"cloneUrl1",
"sourceType",
"sourceInstance",
"repositoryName1",
"repositoryId1"
),
Repository(
"cloneUrl2",
"sourceType",
"sourceInstance",
"repositoryName2",
"repositoryId2"
)
)

every {
ortService.downloadProject(
any(),
"bitbucketUsername",
"bitbucketToken"
)
} returns "downloadedFolder"
every { ortService.analyzeProject(any(), "downloadedFolder") } returns "ortFolder"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package net.leanix.vsm.sbomBooster.configuration

import com.ninjasquad.springmockk.MockkBean
import io.mockk.every
import jakarta.annotation.PostConstruct
import net.leanix.vsm.sbomBooster.service.OrtService

open class GenericTestConfiguration {
@MockkBean(relaxed = true)
protected lateinit var ortService: OrtService

@MockkBean
protected lateinit var propertiesConfiguration: PropertiesConfiguration

@PostConstruct
fun setUp() {
every { propertiesConfiguration.concurrencyFactor } returns 2
every { propertiesConfiguration.mountedVolume } returns "mountedVolume"
every { propertiesConfiguration.allowNoComponentSboms } returns false
every { propertiesConfiguration.devMode } returns false
every { propertiesConfiguration.leanIxHost } returns "de"
every { propertiesConfiguration.leanIxToken } returns "dummyLeanIxToken"
every { propertiesConfiguration.sourceInstance } returns "sourceInstance"
every { propertiesConfiguration.sourceType } returns "sourceType"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package net.leanix.vsm.sbomBooster.configuration

import com.ninjasquad.springmockk.MockkBean
import io.mockk.every
import jakarta.annotation.PostConstruct
import net.leanix.vsm.sbomBooster.domain.Repository
import net.leanix.vsm.sbomBooster.service.GitHubApiService
import org.springframework.boot.test.context.TestConfiguration

@TestConfiguration
class GitHubTestConfiguration : GenericTestConfiguration() {
@MockkBean
private lateinit var gitHubApiService: GitHubApiService

@PostConstruct
fun prepare() {
every { propertiesConfiguration.githubApiHost } returns "https://api.github.com"
every { propertiesConfiguration.gitProvider } returns "github"
every { propertiesConfiguration.githubGraphqlApiUrl } returns "https://api.github.com/graphql"
every { propertiesConfiguration.githubOrganization } returns "githubOrganization"
every { propertiesConfiguration.githubToken } returns "githubToken"

every { gitHubApiService.getUsername("githubToken") } returns "githubUsername"
every { gitHubApiService.getRepositories("githubToken", "githubOrganization") } returns listOf(
Repository(
"cloneUrl1",
"sourceType",
"sourceInstance",
"repositoryName1",
"repositoryId1"
),
Repository(
"cloneUrl2",
"sourceType",
"sourceInstance",
"repositoryName2",
"repositoryId2"
)
)

every {
ortService.downloadProject(
any(),
"githubUsername",
"githubToken"
)
} returns "downloadedFolder"
every { ortService.analyzeProject(any(), "downloadedFolder") } returns "ortFolder"
}
}
Loading

0 comments on commit 776fed1

Please sign in to comment.