Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrading to Wiremock 3 and rewriting a couple Wiremock kotest tests into JUnit for ease #1282

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ kotlin_version=1.9.22
kotest_version=5.8.1
spockVersion=2.3-groovy-3.0
strikt_version=0.34.1
wiremockVersion=2.35.2
wiremockVersion=3.0.1

github_api_version=1.321
logbackVersion=1.5.5
Expand Down
4 changes: 4 additions & 0 deletions server/notification/github/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ dependencies {

testImplementation "com.github.tomakehurst:wiremock-jre8:$wiremockVersion"

testImplementation(platform('org.junit:junit-bom:5.10.2'))
testImplementation('org.junit.jupiter:junit-jupiter')
testRuntimeOnly('org.junit.platform:junit-platform-launcher')

testFixturesImplementation "com.github.tomakehurst:wiremock-jre8:$wiremockVersion"
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package projektor.notification.github.comment

import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo
import com.github.tomakehurst.wiremock.junit5.WireMockTest
import org.junit.jupiter.api.Test
import projektor.notification.github.GitHubClientConfig
import projektor.notification.github.GitHubWireMockStubber
import projektor.notification.github.auth.MockJwtProvider
import strikt.api.expectThat
import strikt.assertions.*
import kotlin.test.assertNotNull

@WireMockTest
class GitHubCommentClientTest {
private val jwtToken = "my-jwt"
private val jwtProvider = MockJwtProvider(jwtToken)

@Test
fun `should create comment on issue`(wmRuntimeInfo: WireMockRuntimeInfo) {
val gitHubWireMockStubber = GitHubWireMockStubber(wmRuntimeInfo.wireMock)

val gitHubApiUrl = "http://localhost:${wmRuntimeInfo.httpPort}/"
val clientConfig =
GitHubClientConfig(
gitHubApiUrl,
)
val gitHubCommentClient = GitHubCommentClient(clientConfig, jwtProvider)

val orgName = "my-org"
val repoName = "my-repo"
val issueId = 12

val commentText = "Here is my comment"

gitHubWireMockStubber.stubRepositoryRequests(orgName, repoName)

gitHubWireMockStubber.stubGetIssue(orgName, repoName, issueId)
gitHubWireMockStubber.stubAddComment(orgName, repoName, issueId)

val repository = gitHubCommentClient.getRepository(orgName, repoName)
assertNotNull(repository)
gitHubCommentClient.addComment(repository, issueId, commentText)

val addCommentRequestBodies = gitHubWireMockStubber.findAddCommentRequestBodies(orgName, repoName, issueId)
expectThat(addCommentRequestBodies).hasSize(1)

expectThat(addCommentRequestBodies[0]).contains(commentText)
}

@Test
fun `should return null when trying to get repository that does not have app enabled`(wmRuntimeInfo: WireMockRuntimeInfo) {
val gitHubWireMockStubber = GitHubWireMockStubber(wmRuntimeInfo.wireMock)

val gitHubApiUrl = "http://localhost:${wmRuntimeInfo.httpPort}/"
val clientConfig =
GitHubClientConfig(
gitHubApiUrl,
)
val gitHubCommentClient = GitHubCommentClient(clientConfig, jwtProvider)

val orgName = "my-org"
val repoName = "app-not-installed-repo"

gitHubWireMockStubber.stubApp()
gitHubWireMockStubber.stubGetRepoInstallationNotFound(orgName, repoName)

val repository = gitHubCommentClient.getRepository(orgName, repoName)
expectThat(repository).isNull()
}

@Test
fun `when open PR exists for branch should find the PR number`(wmRuntimeInfo: WireMockRuntimeInfo) {
val gitHubWireMockStubber = GitHubWireMockStubber(wmRuntimeInfo.wireMock)

val gitHubApiUrl = "http://localhost:${wmRuntimeInfo.httpPort}/"
val clientConfig =
GitHubClientConfig(
gitHubApiUrl,
)
val gitHubCommentClient = GitHubCommentClient(clientConfig, jwtProvider)

val orgName = "my-org"
val repoName = "my-repo"

gitHubWireMockStubber.stubRepositoryRequests(orgName, repoName)

gitHubWireMockStubber.stubListPullRequests(orgName, repoName, listOf("the-branch", "another-branch"))

val repository = gitHubCommentClient.getRepository(orgName, repoName)
assertNotNull(repository)

val pullRequestNumber = gitHubCommentClient.findOpenPullRequests(repository, "the-branch", null)
expectThat(pullRequestNumber).isNotNull().isEqualTo(1)
}

@Test
fun `when no open PR for branch should return null for PR number`(wmRuntimeInfo: WireMockRuntimeInfo) {
val gitHubWireMockStubber = GitHubWireMockStubber(wmRuntimeInfo.wireMock)

val gitHubApiUrl = "http://localhost:${wmRuntimeInfo.httpPort}/"
val clientConfig =
GitHubClientConfig(
gitHubApiUrl,
)
val gitHubCommentClient = GitHubCommentClient(clientConfig, jwtProvider)

val orgName = "my-org"
val repoName = "my-repo"

gitHubWireMockStubber.stubRepositoryRequests(orgName, repoName)

gitHubWireMockStubber.stubListPullRequests(orgName, repoName, listOf("the-branch", "another-branch"))

val repository = gitHubCommentClient.getRepository(orgName, repoName)
assertNotNull(repository)

val pullRequestNumber = gitHubCommentClient.findOpenPullRequests(repository, "some-branch", null)
expectThat(pullRequestNumber).isNull()
}
}
Loading
Loading