Skip to content

Commit

Permalink
CID-2776: rename classes
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedlajmileanix committed Aug 9, 2024
1 parent bd2df7c commit d488c3c
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.leanix.githubagent.controllers

import net.leanix.githubagent.services.GitHubWebhookService
import net.leanix.githubagent.services.GitHubWebhookHandler
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
Expand All @@ -11,7 +11,7 @@ import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("github")
class GitHubWebhookController(private val gitHubWebhookService: GitHubWebhookService) {
class GitHubWebhookController(private val gitHubWebhookHandler: GitHubWebhookHandler) {

@PostMapping("/webhook")
@ResponseStatus(HttpStatus.ACCEPTED)
Expand All @@ -21,6 +21,6 @@ class GitHubWebhookController(private val gitHubWebhookService: GitHubWebhookSer
@RequestHeader("X-Hub-Signature-256", required = false) signature256: String?,
@RequestBody payload: String
) {
gitHubWebhookService.processWebhookEvent(eventType, host, signature256, payload)
gitHubWebhookHandler.handleWebhookEvent(eventType, host, signature256, payload)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service

@Service
class GitHubWebhookService(
private val webhookService: WebhookService,
class GitHubWebhookHandler(
private val webhookEventService: WebhookEventService,
private val gitHubEnterpriseProperties: GitHubEnterpriseProperties
) {

private val logger = LoggerFactory.getLogger(GitHubWebhookService::class.java)
private val logger = LoggerFactory.getLogger(GitHubWebhookHandler::class.java)
private var isWebhookProcessingEnabled = true

fun processWebhookEvent(eventType: String, host: String, signature256: String?, payload: String) {
fun handleWebhookEvent(eventType: String, host: String, signature256: String?, payload: String) {
if (!isWebhookProcessingEnabled) {
throw WebhookSecretNotSetException()
}
Expand All @@ -42,7 +42,7 @@ class GitHubWebhookService(
logger.warn("Webhook secret is not set, Skipping signature verification")
}
if (SUPPORTED_EVENT_TYPES.contains(eventType.uppercase())) {
webhookService.consumeWebhookEvent(eventType, payload)
webhookEventService.consumeWebhookEvent(eventType, payload)
} else {
logger.warn("Received an unsupported event of type: $eventType")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service

@Service
class WebhookService(
class WebhookEventService(
private val webSocketService: WebSocketService,
private val gitHubGraphQLService: GitHubGraphQLService,
private val gitHubEnterpriseProperties: GitHubEnterpriseProperties,
private val cachingService: CachingService,
private val gitHubAuthenticationService: GitHubAuthenticationService
) {

private val logger = LoggerFactory.getLogger(WebhookService::class.java)
private val logger = LoggerFactory.getLogger(WebhookEventService::class.java)
private val objectMapper = jacksonObjectMapper()

fun consumeWebhookEvent(eventType: String, payload: String) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package net.leanix.githubagent.controllers
import com.ninjasquad.springmockk.MockkBean
import io.mockk.every
import net.leanix.githubagent.exceptions.WebhookSecretNotSetException
import net.leanix.githubagent.services.GitHubWebhookService
import net.leanix.githubagent.services.GitHubWebhookHandler
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
Expand All @@ -18,15 +18,15 @@ class GitHubWebhookControllerTest {
private lateinit var mockMvc: MockMvc

@MockkBean
private lateinit var gitHubWebhookService: GitHubWebhookService
private lateinit var gitHubWebhookHandler: GitHubWebhookHandler

@Test
fun `should return 202 if webhook event is processed successfully`() {
val eventType = "PUSH"
val payload = "{}"
val host = "valid.host"

every { gitHubWebhookService.processWebhookEvent(any(), any(), any(), any()) } returns Unit
every { gitHubWebhookHandler.handleWebhookEvent(any(), any(), any(), any()) } returns Unit

mockMvc.perform(
MockMvcRequestBuilders.post("/github/webhook")
Expand All @@ -45,7 +45,7 @@ class GitHubWebhookControllerTest {
val signature256 = "sha256=invalidsignature"

every {
gitHubWebhookService.processWebhookEvent(
gitHubWebhookHandler.handleWebhookEvent(
eventType, host, signature256, payload
)
} throws WebhookSecretNotSetException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,32 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import java.lang.reflect.Field

class GitHubWebhookServiceTest {
class GitHubWebhookHandlerTest {

private val webhookService = mockk<WebhookService>()
private val webhookEventService = mockk<WebhookEventService>()
private val gitHubEnterpriseProperties = mockk<GitHubEnterpriseProperties>()
private val gitHubWebhookService = GitHubWebhookService(webhookService, gitHubEnterpriseProperties)
private val gitHubWebhookHandler = GitHubWebhookHandler(webhookEventService, gitHubEnterpriseProperties)

@BeforeEach
fun setUp() {
}

@Test
fun `should throw WebhookSecretNotSetException when webhook processing is disabled`() {
setPrivateField(gitHubWebhookService, "isWebhookProcessingEnabled", false)
setPrivateField(gitHubWebhookHandler, "isWebhookProcessingEnabled", false)

assertThrows<WebhookSecretNotSetException> {
gitHubWebhookService.processWebhookEvent("PUSH", "host", null, "{}")
gitHubWebhookHandler.handleWebhookEvent("PUSH", "host", null, "{}")
}
}

@Test
fun `should not process event if unknown host`() {
every { gitHubEnterpriseProperties.baseUrl } returns "known.host"

gitHubWebhookService.processWebhookEvent("PUSH", "unknown.host", null, "{}")
gitHubWebhookHandler.handleWebhookEvent("PUSH", "unknown.host", null, "{}")

verify(exactly = 0) { webhookService.consumeWebhookEvent(any(), any()) }
verify(exactly = 0) { webhookEventService.consumeWebhookEvent(any(), any()) }
}

@Test
Expand All @@ -45,7 +45,7 @@ class GitHubWebhookServiceTest {
every { gitHubEnterpriseProperties.webhookSecret } returns ""

assertThrows<WebhookSecretNotSetException> {
gitHubWebhookService.processWebhookEvent("PUSH", "known.host", "sha256=signature", "{}")
gitHubWebhookHandler.handleWebhookEvent("PUSH", "known.host", "sha256=signature", "{}")
}
}

Expand All @@ -55,7 +55,7 @@ class GitHubWebhookServiceTest {
every { gitHubEnterpriseProperties.webhookSecret } returns "secret"

assertThrows<InvalidEventSignatureException> {
gitHubWebhookService.processWebhookEvent("PUSH", "known.host", "sha256=signature", "{}")
gitHubWebhookHandler.handleWebhookEvent("PUSH", "known.host", "sha256=signature", "{}")
}
}

Expand All @@ -64,20 +64,20 @@ class GitHubWebhookServiceTest {
every { gitHubEnterpriseProperties.baseUrl } returns "known.host"
every { gitHubEnterpriseProperties.webhookSecret } returns ""

gitHubWebhookService.processWebhookEvent("UNSUPPORTED_EVENT", "known.host", null, "{}")
gitHubWebhookHandler.handleWebhookEvent("UNSUPPORTED_EVENT", "known.host", null, "{}")

verify(exactly = 0) { webhookService.consumeWebhookEvent(any(), any()) }
verify(exactly = 0) { webhookEventService.consumeWebhookEvent(any(), any()) }
}

@Test
fun `should process supported event type successfully`() {
every { gitHubEnterpriseProperties.baseUrl } returns "host"
every { gitHubEnterpriseProperties.webhookSecret } returns ""
every { webhookService.consumeWebhookEvent(any(), any()) } returns Unit
every { webhookEventService.consumeWebhookEvent(any(), any()) } returns Unit

gitHubWebhookService.processWebhookEvent("PUSH", "host", null, "{}")
gitHubWebhookHandler.handleWebhookEvent("PUSH", "host", null, "{}")

verify { webhookService.consumeWebhookEvent("PUSH", "{}") }
verify { webhookEventService.consumeWebhookEvent("PUSH", "{}") }
}

private fun setPrivateField(target: Any, fieldName: String, value: Any) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import org.springframework.test.context.ActiveProfiles

@SpringBootTest
@ActiveProfiles("test")
class WebhookServiceTest {
class WebhookEventServiceTest {

@MockkBean
private lateinit var webSocketService: WebSocketService
Expand All @@ -28,7 +28,7 @@ class WebhookServiceTest {
private lateinit var gitHubAuthenticationService: GitHubAuthenticationService

@Autowired
private lateinit var webhookService: WebhookService
private lateinit var webhookEventService: WebhookEventService

@BeforeEach
fun setUp() {
Expand Down Expand Up @@ -56,7 +56,7 @@ class WebhookServiceTest {

every { cachingService.get("installationToken:1") } returns null andThen "token"

webhookService.consumeWebhookEvent("PUSH", payload)
webhookEventService.consumeWebhookEvent("PUSH", payload)

verify(exactly = 1) { gitHubAuthenticationService.refreshTokens() }
}
Expand All @@ -82,7 +82,7 @@ class WebhookServiceTest {
"ref": "refs/heads/main"
}"""

webhookService.consumeWebhookEvent("PUSH", payload)
webhookEventService.consumeWebhookEvent("PUSH", payload)

verify(exactly = 1) {
webSocketService.sendMessage(
Expand Down Expand Up @@ -114,7 +114,7 @@ class WebhookServiceTest {
"ref": "refs/heads/main"
}"""

webhookService.consumeWebhookEvent("OTHER", payload)
webhookEventService.consumeWebhookEvent("OTHER", payload)

verify(exactly = 1) { webSocketService.sendMessage("/events/other", payload) }
}
Expand Down

0 comments on commit d488c3c

Please sign in to comment.