-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CID-1813: Send admin and status logs
- Loading branch information
mohamedlajmileanix
committed
Aug 24, 2023
1 parent
54860fc
commit 0c48e58
Showing
14 changed files
with
288 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/main/kotlin/net/leanix/vsm/gitlab/broker/logs/adapter/feign/FeignLogProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/net/leanix/vsm/gitlab/broker/logs/adapter/feign/LoggingClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/net/leanix/vsm/gitlab/broker/logs/adapter/feign/StatusLogClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/kotlin/net/leanix/vsm/gitlab/broker/logs/adapter/feign/data/AdminRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/kotlin/net/leanix/vsm/gitlab/broker/logs/adapter/feign/data/StatusRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/kotlin/net/leanix/vsm/gitlab/broker/logs/application/LoggingService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/kotlin/net/leanix/vsm/gitlab/broker/logs/domain/AdminLog.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? | ||
) |
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/net/leanix/vsm/gitlab/broker/logs/domain/LogLevel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/net/leanix/vsm/gitlab/broker/logs/domain/LogProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/net/leanix/vsm/gitlab/broker/logs/domain/LogStatus.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/net/leanix/vsm/gitlab/broker/logs/domain/StatusLog.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? | ||
) |
44 changes: 44 additions & 0 deletions
44
src/test/kotlin/net/leanix/vsm/gitlab/broker/logs/adapter/feign/FeignLogProviderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) } | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/test/kotlin/net/leanix/vsm/gitlab/broker/logs/application/LoggingServiceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } | ||
} | ||
} |