Skip to content

Commit

Permalink
Refactor tests (#42)
Browse files Browse the repository at this point in the history
Also: add issue key validation.
  • Loading branch information
CamaradeRoman committed Jun 3, 2024
1 parent 065ab1f commit a1aa45a
Show file tree
Hide file tree
Showing 158 changed files with 19,058 additions and 1,637 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package org.octopusden.octopus.vcsfacade.client.common.dto

data class FileChange(val type: FileChangeType, val path: String, val link: String)
data class FileChange(val type: FileChangeType, val path: String, val link: String) : Comparable<FileChange> {
override fun compareTo(other: FileChange) = path compareTo other.path
}
2 changes: 1 addition & 1 deletion ft/docker/application-ft.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
vcs-facade:
job:
fast-work-timout-secs: 0
fast-work-timout-secs: 0 #check retry mechanism
retry-interval-secs: 1
4 changes: 4 additions & 0 deletions ft/docker/gitea/application-gitea.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ vcs-facade:
root-commit: 9320183f5d5f5868fdb82b36e3abd6f9d1424114
last-release: 321d4908aef10bafa1488f9b053270acc29f3d78
expected-commits: 9320183f5d5f5868fdb82b36e3abd6f9d1424114,00cc61dd4c3eca64d12e6beceff1a40a436962f5
index:
webhook-secret: b59dd966-2445-4c84-b631-49502427477e
scan:
delay: 5000
gitlab:
enabled: false
opensearch:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,114 +2,64 @@ package org.octopusden.octopus.vcsfacade

import java.util.Date
import org.octopusden.octopus.infrastructure.common.test.TestClient
import org.octopusden.octopus.vcsfacade.client.common.dto.Commit
import org.octopusden.octopus.vcsfacade.TestService.Companion.VCS_FACADE_API_URL
import org.octopusden.octopus.vcsfacade.client.common.dto.CreatePullRequest
import org.octopusden.octopus.vcsfacade.client.common.dto.PullRequest
import org.octopusden.octopus.vcsfacade.client.common.dto.SearchIssueInRangesResponse
import org.octopusden.octopus.vcsfacade.client.common.dto.SearchIssuesInRangesRequest
import org.octopusden.octopus.vcsfacade.client.common.dto.Tag
import org.octopusden.octopus.vcsfacade.client.common.exception.ArgumentsNotCompatibleException
import org.octopusden.octopus.vcsfacade.client.common.exception.NotFoundException
import org.octopusden.octopus.vcsfacade.client.impl.ClassicVcsFacadeClient
import org.octopusden.octopus.vcsfacade.client.impl.VcsFacadeClientParametersProvider

abstract class BaseVcsFacadeFunctionalTest(testClient: TestClient, sshUrlFormat: String) :
BaseVcsFacadeTest(testClient, sshUrlFormat) {
abstract class BaseVcsFacadeFunctionalTest(
testService: TestService, testClient: TestClient
) : BaseVcsFacadeTest(testService, testClient) {
override fun createPullRequest(sshUrl: String, createPullRequest: CreatePullRequest) =
client.createPullRequest(sshUrl, createPullRequest)

override fun requestTags(
override fun getCommits(
sshUrl: String,
status: Int,
checkSuccess: (List<Tag>) -> Unit,
checkError: CheckError
) {
try {
val tags = client.getTags(sshUrl)
checkSuccess(tags)
} catch (e: NotFoundException) {
checkError(Pair(400, e.message!!))
}
}
fromHashOrRef: String?,
fromDate: Date?,
toHashOrRef: String
) = client.getCommits(sshUrl, fromHashOrRef, fromDate, toHashOrRef)

override fun requestCommitsInterval(
override fun getCommitsWithFiles(
sshUrl: String,
fromId: String?,
fromHashOrRef: String?,
fromDate: Date?,
toId: String,
status: Int,
checkSuccess: (List<Commit>) -> Unit,
checkError: CheckError
) {
try {
val commits = client.getCommits(sshUrl, fromId, fromDate, toId)
checkSuccess(commits)
} catch (e: NotFoundException) {
checkError(Pair(400, e.message!!))
} catch (e: ArgumentsNotCompatibleException) {
checkError(Pair(400, e.message!!))
}
}
toHashOrRef: String,
commitFilesLimit: Int?
) = client.getCommitsWithFiles(sshUrl, fromHashOrRef, fromDate, toHashOrRef, commitFilesLimit)

override fun requestCommitsByIssueKey(
issueKey: String,
status: Int,
checkSuccess: (List<Commit>) -> Unit,
checkError: CheckError
) {
try {
val commits = client.findCommitsByIssueKey(issueKey)
checkSuccess(commits)
} catch (e: NotFoundException) {
checkError(Pair(400, e.message!!))
}
}
override fun getCommit(sshUrl: String, hashOrRef: String) = client.getCommit(sshUrl, hashOrRef)

override fun requestCommitById(
override fun getCommitWithFiles(sshUrl: String, hashOrRef: String, commitFilesLimit: Int?) =
client.getCommitWithFiles(sshUrl, hashOrRef, commitFilesLimit)

override fun getIssuesFromCommits(
sshUrl: String,
commitId: String,
status: Int,
checkSuccess: (Commit) -> Unit,
checkError: CheckError
) {
try {
val commit = client.getCommit(sshUrl, commitId)
checkSuccess(commit)
} catch (e: NotFoundException) {
checkError(Pair(400, e.message!!))
}
}
fromHashOrRef: String?,
fromDate: Date?,
toHashOrRef: String
) = client.getIssuesFromCommits(sshUrl, fromHashOrRef, fromDate, toHashOrRef)

override fun searchIssuesInRanges(
searchRequest: SearchIssuesInRangesRequest,
status: Int,
checkSuccess: (SearchIssueInRangesResponse) -> Unit,
checkError: CheckError
) {
try {
val response = client.searchIssuesInRanges(searchRequest)
checkSuccess(response)
} catch (e: NotFoundException) {
checkError(Pair(400, e.message!!))
}
}
override fun getTags(sshUrl: String) = client.getTags(sshUrl)

override fun createPullRequest(
sshUrl: String,
createPullRequest: CreatePullRequest,
status: Int,
checkSuccess: (PullRequest) -> Unit,
checkError: CheckError
) {
try {
val pullRequest = client.createPullRequest(sshUrl, createPullRequest)
checkSuccess(pullRequest)
} catch (e: NotFoundException) {
checkError(Pair(400, e.message!!))
}
}
override fun searchIssuesInRanges(searchRequest: SearchIssuesInRangesRequest) =
client.searchIssuesInRanges(searchRequest)

override fun findByIssueKey(issueKey: String) = client.findByIssueKey(issueKey)

override fun findBranchesByIssueKey(issueKey: String) = client.findBranchesByIssueKey(issueKey)

override fun findCommitsByIssueKey(issueKey: String) = client.findCommitsByIssueKey(issueKey)

override fun findCommitsWithFilesByIssueKey(issueKey: String, commitFilesLimit: Int?) =
client.findCommitsWithFilesByIssueKey(issueKey, commitFilesLimit)

override fun findPullRequestsByIssueKey(issueKey: String) = client.findPullRequestsByIssueKey(issueKey)

companion object {
private val client = ClassicVcsFacadeClient(object : VcsFacadeClientParametersProvider {
override fun getApiUrl() = "http://localhost:8080"
override fun getApiUrl() = VCS_FACADE_API_URL
override fun getTimeRetryInMillis() = 180000
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,8 @@ package org.octopusden.octopus.vcsfacade
import org.junit.jupiter.api.condition.EnabledIfSystemProperty
import org.octopusden.octopus.infastructure.bitbucket.test.BitbucketTestClient

private const val VCS_HOST = "bitbucket:7990"

@EnabledIfSystemProperty(named = "test.profile", matches = "bitbucket")
class VcsFacadeFunctionalTestBitbucket : BaseVcsFacadeFunctionalTest(
BitbucketTestClient("http://localhost:7990", BITBUCKET_USER, BITBUCKET_PASSWORD, VCS_HOST),
"ssh://git@$VCS_HOST/%s/%s.git"
) {
override val exceptionsMessageInfo: Map<String, String> by lazy {
mapOf(
"absent-repo" to "Repository $PROJECT/absent does not exist.",
"commitById" to "Commit '$DEFAULT_ID' does not exist in repository '$REPOSITORY'.",
"commitsException_1" to "Commit '$DEFAULT_ID' does not exist in repository '$REPOSITORY'.",
"commitsException_2" to "'hashOrRef' and 'date' can not be used together",
"commitsException_3" to "Cannot find commit '${MESSAGE_3.commitId(REPOSITORY)}' in commit graph for commit '${MESSAGE_1.commitId(REPOSITORY)}' in '$PROJECT:$REPOSITORY'",
"pr_1" to "Project absent does not exist.",
"pr_2" to "Source branch 'absent' not found in '$PROJECT:$REPOSITORY'",
"pr_3" to "Target branch 'absent' not found in '$PROJECT:$REPOSITORY'"
)
}
}
TestService.Bitbucket(BITBUCKET_HOST, BITBUCKET_EXTERNAL_HOST),
BitbucketTestClient("http://$BITBUCKET_HOST", BITBUCKET_USER, BITBUCKET_PASSWORD, BITBUCKET_EXTERNAL_HOST)
)
Original file line number Diff line number Diff line change
@@ -1,36 +1,90 @@
package org.octopusden.octopus.vcsfacade

import java.util.stream.Stream
import java.net.HttpURLConnection
import java.net.URI
import java.util.Base64
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.condition.EnabledIfSystemProperty
import org.junit.jupiter.params.provider.Arguments
import org.octopusden.octopus.infrastructure.common.test.dto.NewChangeSet
import org.octopusden.octopus.infrastructure.gitea.test.GiteaTestClient

private const val VCS_HOST = "gitea:3000"
import org.octopusden.octopus.vcsfacade.client.common.dto.CreatePullRequest

@EnabledIfSystemProperty(named = "test.profile", matches = "gitea")
class VcsFacadeFunctionalTestGitea : BaseVcsFacadeFunctionalTest(
GiteaTestClient("http://localhost:3000", GITEA_USER, GITEA_PASSWORD, VCS_HOST),
"ssh://git@$VCS_HOST:%s/%s.git"
TestService.Gitea(GITEA_HOST, GITEA_EXTERNAL_HOST, true),
GiteaTestClient("http://$GITEA_HOST", GITEA_USER, GITEA_PASSWORD, GITEA_EXTERNAL_HOST)
) {
//TODO: test using opensearch
override fun issueCommits(): Stream<Arguments> = Stream.of(
Arguments.of("ABSENT-1", emptyList<String>()),
)
@BeforeAll
fun beforeAllVcsFacadeFunctionalTestGitea() {
val url = URI("http://$GITEA_HOST/api/v1/repos/$GROUP/$REPOSITORY_2/hooks").toURL()
with(url.openConnection() as HttpURLConnection) {
setRequestMethod("POST")
setRequestProperty(
"Authorization",
"Basic " + Base64.getEncoder().encodeToString("$GITEA_USER:$GITEA_PASSWORD".toByteArray())
)
setRequestProperty("Content-Type", "application/json")
setRequestProperty("Accept", "application/json")
setDoOutput(true)
outputStream.use {
it.write(WEBHOOK_CREATION_REQUEST.toByteArray())
}
if (getResponseCode() / 100 != 2) {
throw RuntimeException("Unable to create webhook for '$GROUP:$REPOSITORY_2'")
}
}
(testService as TestService.Gitea).scan(GROUP, REPOSITORY_2)
}

override val exceptionsMessageInfo: Map<String, String> by lazy {
mapOf(
"absent-repo" to "The target couldn't be found.",
"commitById" to DEFAULT_ID,
"commitsException_1" to "object does not exist [id: $DEFAULT_ID, rel_path: ]",
"commitsException_2" to "'hashOrRef' and 'date' can not be used together",
"commitsException_3" to "Cannot find commit '${MESSAGE_3.commitId(REPOSITORY)}' in commit graph for commit '${
MESSAGE_1.commitId(
REPOSITORY
)
}' in '$PROJECT:$REPOSITORY'",
"pr_1" to "GetUserByName",
"pr_2" to "Source branch 'absent' not found in '$PROJECT:$REPOSITORY'",
"pr_3" to "Target branch 'absent' not found in '$PROJECT:$REPOSITORY'"
@Test
fun webhooksTest() {
var success = false
testClient.commit(
NewChangeSet("Commit (ISSUE-10)", testService.sshUrl(GROUP, REPOSITORY_2), "ISSUE-10"),
"master"
)
for (i in 1..5) {
Thread.sleep(1000L * i)
success = with(findByIssueKey("ISSUE-10")) {
branches.size == 1 && commits.size == 1 && pullRequests.size == 0
}
if (success) break
}
if (!success) throw RuntimeException("Commit and branch for ISSUE-10 have not been registered")
createPullRequest(
testService.sshUrl(GROUP, REPOSITORY_2),
CreatePullRequest("ISSUE-10", "master", "Webhook test PR", "Description ISSUE-10")
)
for (i in 1..5) {
Thread.sleep(1000L * i)
success = with(findByIssueKey("ISSUE-10")) {
branches.size == 1 && commits.size == 1 && pullRequests.size == 1
}
if (success) break
}
if (!success) throw RuntimeException("Pull request for ISSUE-10 has not been registered")
}

companion object {
//<editor-fold defaultstate="collapsed" desc="webhook creation request">
const val WEBHOOK_CREATION_REQUEST = """{
"type": "gitea",
"branch_filter": "*",
"config": {
"content_type": "json",
"url": "http://vcs-facade:8080/rest/api/1/indexer/gitea/webhook",
"secret": "b59dd966-2445-4c84-b631-49502427477e"
},
"events": [
"create",
"delete",
"push",
"pull_request"
],
"authorization_header": "",
"active": true
}"""
//</editor-fold>
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,10 @@
package org.octopusden.octopus.vcsfacade

import java.util.stream.Stream
import org.junit.jupiter.api.condition.EnabledIfSystemProperty
import org.junit.jupiter.params.provider.Arguments
import org.octopusden.octopus.infrastructure.gitlab.test.GitlabTestClient

private const val VCS_HOST = "gitlab:8990"

@EnabledIfSystemProperty(named = "test.profile", matches = "gitlab")
class VcsFacadeFunctionalTestGitlab : BaseVcsFacadeFunctionalTest(
GitlabTestClient("http://localhost:8990", GITLAB_USER, GITLAB_PASSWORD, VCS_HOST),
"ssh://git@$VCS_HOST:%s/%s.git"
) {
override fun issueCommits(): Stream<Arguments> = Stream.of(
Arguments.of("ABSENT-1", emptyList<String>()),
)

override val exceptionsMessageInfo: Map<String, String> by lazy {
mapOf(
"absent-repo" to "Repository '$PROJECT:absent' does not exist.",
"commitById" to "Commit '$DEFAULT_ID' does not exist in repository '$PROJECT:$REPOSITORY'.",
"commitsException_1" to "Commit '$DEFAULT_ID' does not exist in repository '$PROJECT:$REPOSITORY'.",
"commitsException_2" to "'hashOrRef' and 'date' can not be used together",
"commitsException_3" to "Can't find commit '${MESSAGE_3.commitId(REPOSITORY)}' in graph but it exists in the '$PROJECT:$REPOSITORY'",
"pr_1" to "Group 'absent' does not exist.",
"pr_2" to "Source branch 'absent' not found in '$PROJECT:$REPOSITORY'",
"pr_3" to "Target branch 'absent' not found in '$PROJECT:$REPOSITORY'"
)
}
}
TestService.Gitlab(GITLAB_HOST, GITLAB_EXTERNAL_HOST),
GitlabTestClient("http://$GITLAB_HOST", GITLAB_USER, GITLAB_PASSWORD, GITLAB_EXTERNAL_HOST)
)
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ bmuschko-docker-plugin.version=9.4.0
docker-compose-plugin.version=0.16.9
spring-data-opensearch.version=1.3.0
springdoc-openapi.version=2.3.0
external-systems-client.version=2.0.39
external-systems-client.version=2.0.40
gitlab4j-api.version=6.0.0-rc.4
bitbucket.version=8.14.0-jdk11
gitea.version=1.21.10
Expand Down
Loading

0 comments on commit a1aa45a

Please sign in to comment.