Skip to content

Commit

Permalink
CID-1813: Send admin and status logs
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedlajmileanix committed Aug 24, 2023
1 parent 54860fc commit 0c48e58
Show file tree
Hide file tree
Showing 14 changed files with 288 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ dependencies {
testImplementation("org.springframework.cloud:spring-cloud-starter-contract-stub-runner")
testImplementation("org.springframework.security:spring-security-test")
testImplementation("org.awaitility:awaitility-kotlin:4.2.0")
testImplementation("com.ninja-squad:springmockk:4.0.2")
}

dependencyManagement {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package net.leanix.vsm.gitlab.broker.logs.adapter.feign

import feign.FeignException
import net.leanix.vsm.githubbroker.logs.adapter.feign.data.AdminRequest
import net.leanix.vsm.githubbroker.logs.adapter.feign.data.StatusRequest
import net.leanix.vsm.gitlab.broker.logs.domain.AdminLog
import net.leanix.vsm.gitlab.broker.logs.domain.LogProvider
import net.leanix.vsm.gitlab.broker.logs.domain.StatusLog
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component

private const val FAILED_TO_SEND_STATUS_LOG = "Failed to send Status Log"
private const val FAILED_TO_SEND_ADMIN_LOG = "Failed to send Admin Log"

@Component
class FeignLogProvider(
private val loggingClient: LoggingClient,
private val statusLogClient: StatusLogClient
) : LogProvider {

private val logger: Logger = LoggerFactory.getLogger(FeignLogProvider::class.java)

override fun sendAdminLog(adminLog: AdminLog) {
try {
loggingClient.sendAdminLog(AdminRequest.fromDomain(adminLog))
} catch (e: FeignException) {
val message = "$FAILED_TO_SEND_ADMIN_LOG, ${e.message}"
logger.error(message)
}
}

override fun sendStatusLog(statusLog: StatusLog) {
try {
statusLogClient.sendStatusLog(StatusRequest.fromDomain(statusLog))
} catch (e: FeignException) {
val message = "$FAILED_TO_SEND_STATUS_LOG, ${e.message}"
logger.error(message)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.leanix.vsm.gitlab.broker.logs.adapter.feign

import net.leanix.vsm.githubbroker.logs.adapter.feign.data.AdminRequest
import net.leanix.vsm.gitlab.broker.shared.auth.adapter.feign.config.MtmFeignClientConfiguration
import org.springframework.cloud.openfeign.FeignClient
import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody

@FeignClient(
name = "loggingClient",
url = "\${leanix.vsm.event-broker.base-url}",
configuration = [MtmFeignClientConfiguration::class]
)
interface LoggingClient {

@PostMapping(value = ["/logs/admin"], consumes = [MediaType.APPLICATION_JSON_VALUE])
fun sendAdminLog(@RequestBody request: AdminRequest)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.leanix.vsm.gitlab.broker.logs.adapter.feign

import net.leanix.vsm.githubbroker.logs.adapter.feign.data.StatusRequest
import net.leanix.vsm.gitlab.broker.shared.auth.adapter.feign.config.MtmFeignClientConfiguration
import org.springframework.cloud.openfeign.FeignClient
import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody

@FeignClient(
name = "statusLoggingClient",
url = "\${leanix.vsm.base-url}",
configuration = [MtmFeignClientConfiguration::class]
)
interface StatusLogClient {

@PostMapping(value = ["/log/connector-status"], consumes = [MediaType.APPLICATION_JSON_VALUE])
fun sendStatusLog(@RequestBody request: StatusRequest)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.leanix.vsm.githubbroker.logs.adapter.feign.data

import jakarta.validation.constraints.NotNull
import net.leanix.vsm.gitlab.broker.logs.domain.AdminLog
import net.leanix.vsm.gitlab.broker.logs.domain.LogLevel
import java.util.UUID

data class AdminRequest(
@field:NotNull(message = "Field \"runId\" cannot be empty")
val runId: UUID?,
@field:NotNull(message = "Field \"configurationId\" cannot be empty")
val configurationId: UUID?,
@field:NotNull(message = "Field \"subject\" cannot be empty")
val subject: String?,
@field:NotNull(message = "Field \"level\" cannot be empty")
val level: LogLevel?,
@field:NotNull(message = "Field \"message\" cannot be empty")
val message: String?
) {
companion object {
fun fromDomain(admin: AdminLog): AdminRequest {
return AdminRequest(
runId = admin.runId,
configurationId = admin.configurationId,
subject = admin.subject,
level = admin.level,
message = admin.message,
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package net.leanix.vsm.githubbroker.logs.adapter.feign.data

import jakarta.validation.constraints.NotNull
import net.leanix.vsm.gitlab.broker.logs.domain.LogStatus
import net.leanix.vsm.gitlab.broker.logs.domain.StatusLog
import java.util.UUID

data class StatusRequest(
@field:NotNull(message = "Field \"runId\" cannot be empty")
val runId: UUID?,
@field:NotNull(message = "Field \"status\" cannot be empty")
val status: LogStatus?,
val message: String? = null
) {
companion object {
fun fromDomain(status: StatusLog): StatusRequest {
return StatusRequest(
runId = status.runId,
status = status.status,
message = status.message
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package net.leanix.vsm.gitlab.broker.logs.application

import net.leanix.vsm.gitlab.broker.logs.domain.AdminLog
import net.leanix.vsm.gitlab.broker.logs.domain.LogProvider
import net.leanix.vsm.gitlab.broker.logs.domain.StatusLog
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service

@Service
class LoggingService(
private val logProvider: LogProvider
) {
private val logger: Logger = LoggerFactory.getLogger(LoggingService::class.java)

fun sendStatusLog(statusLog: StatusLog) {
logger.info("Sending status log with runId: ${statusLog.runId}")
logProvider.sendStatusLog(statusLog)
}

fun sendAdminLog(adminLog: AdminLog) {
logger.info("Sending admin log with runId: ${adminLog.runId}")
logProvider.sendAdminLog(adminLog)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package net.leanix.vsm.gitlab.broker.logs.domain

import java.util.UUID

data class AdminLog(
val runId: UUID,
val configurationId: UUID,
val subject: String,
val level: LogLevel,
val message: String?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.leanix.vsm.gitlab.broker.logs.domain

enum class LogLevel {
INFO,
WARNING,
ERROR
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.leanix.vsm.gitlab.broker.logs.domain


interface LogProvider {
fun sendAdminLog(adminLog: AdminLog)
fun sendStatusLog(statusLog: StatusLog)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package net.leanix.vsm.gitlab.broker.logs.domain

enum class LogStatus {
STARTED,
IN_PROGRESS,
FAILED,
SUCCESSFUL
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.leanix.vsm.gitlab.broker.logs.domain

import java.util.UUID

data class StatusLog(
val runId: UUID,
val status: LogStatus,
val message: String?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package net.leanix.vsm.gitlab.broker.logs.adapter.feign

import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import net.leanix.vsm.gitlab.broker.logs.domain.AdminLog
import net.leanix.vsm.gitlab.broker.logs.domain.LogLevel
import net.leanix.vsm.gitlab.broker.logs.domain.LogStatus
import net.leanix.vsm.gitlab.broker.logs.domain.StatusLog
import org.junit.jupiter.api.Test
import java.util.UUID

internal class FeignLogProviderTest {

private val loggingClient = mockk<LoggingClient>()
private val statusLogClient = mockk<StatusLogClient>()
private val feignLogProvider = FeignLogProvider(loggingClient, statusLogClient)

@Test
fun `sending admin log should call correct client`() {
val adminLog = AdminLog(
runId = UUID.randomUUID(),
configurationId = UUID.randomUUID(),
subject = "dummy",
level = LogLevel.INFO,
message = "dummy"
)
every { loggingClient.sendAdminLog(any()) } returns Unit
feignLogProvider.sendAdminLog(adminLog)
verify(exactly = 1) { loggingClient.sendAdminLog(any()) }
}

@Test
fun `sending status log should call correct client`() {
val statusLog = StatusLog(
runId = UUID.randomUUID(),
status = LogStatus.IN_PROGRESS,
message = "Success"
)
every { statusLogClient.sendStatusLog(any()) } returns Unit
feignLogProvider.sendStatusLog(statusLog)
verify(exactly = 1) { statusLogClient.sendStatusLog(any()) }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package net.leanix.vsm.gitlab.broker.logs.application

import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import net.leanix.vsm.gitlab.broker.logs.domain.AdminLog
import net.leanix.vsm.gitlab.broker.logs.domain.LogLevel
import net.leanix.vsm.gitlab.broker.logs.domain.LogProvider
import net.leanix.vsm.gitlab.broker.logs.domain.LogStatus
import net.leanix.vsm.gitlab.broker.logs.domain.StatusLog
import org.junit.jupiter.api.Test
import java.util.UUID

class LoggingServiceTest {

private val logProvider = mockk<LogProvider>()
private val loggingService = LoggingService(logProvider)
@Test
fun `sending status log should call correct client`() {
val statusLog = StatusLog(
runId = UUID.randomUUID(),
status = LogStatus.SUCCESSFUL,
message = "Success"
)
every { logProvider.sendStatusLog(any()) } returns Unit
loggingService.sendStatusLog(statusLog)
verify(exactly = 1) { logProvider.sendStatusLog(statusLog) }
}
@Test
fun `sending admin log should call correct client`() {
val adminLog = AdminLog(
runId = UUID.randomUUID(),
configurationId = UUID.randomUUID(),
subject = "dummy",
level = LogLevel.INFO,
message = "dummy"
)
every { logProvider.sendAdminLog(any()) } returns Unit
loggingService.sendAdminLog(adminLog)
verify(exactly = 1) { logProvider.sendAdminLog(adminLog) }
}
}

0 comments on commit 0c48e58

Please sign in to comment.