1+ @file:Suppress(" UNCHECKED_CAST" )
2+
13package com.trendyol.kediatr.framewokUseCases
24
35import com.trendyol.kediatr.*
46import io.kotest.matchers.shouldNotBe
57
68class TestNonExistCommand : Command
79
8- class TestCommand : Command
10+ class TestCommand : Command , EnrichedWithMetadata ()
911
1012class TestCommandHandler (
1113 private val mediator : Mediator
1214) : CommandHandler<TestCommand> {
1315 override suspend fun handle (command : TestCommand ) {
1416 mediator shouldNotBe null
17+ command.incrementInvocationCount()
1518 }
1619}
1720
@@ -21,33 +24,69 @@ class Result(
2124
2225class NonExistCommandWithResult : CommandWithResult <Result >
2326
24- data class TestCommandWithResult (val invoked : Int = 0 ) : CommandWithResult<Result>
27+ data class TestCommandWithResult (
28+ val invoked : Int = 0
29+ ) : CommandWithResult<Result>, EnrichedWithMetadata()
2530
2631class TestCommandWithResultCommandHandler (
2732 val mediator : Mediator
2833) : CommandWithResultHandler<TestCommandWithResult, Result> {
29- override suspend fun handle (command : TestCommandWithResult ): Result = Result (command.invoked + 1 )
34+ override suspend fun handle (
35+ command : TestCommandWithResult
36+ ): Result = Result (command.invoked + 1 ).also { command.incrementInvocationCount() }
3037}
3138
32- class TestNotification : Notification
39+ class TestNotification : Notification , EnrichedWithMetadata ()
3340
3441class TestNotificationHandler (
3542 private val mediator : Mediator
3643) : NotificationHandler<TestNotification> {
3744 override suspend fun handle (notification : TestNotification ) {
3845 mediator shouldNotBe null
46+ notification.incrementInvocationCount()
3947 }
4048}
4149
42- class TestBrokenCommand : Command
50+ class TestBrokenCommand : Command , EnrichedWithMetadata ()
51+
52+ class TestPipelineCommand : Command , EnrichedWithMetadata ()
53+
54+ abstract class EnrichedWithMetadata {
55+ private val metadata = mutableMapOf<String , Any >()
56+
57+ internal fun incrementInvocationCount () {
58+ val invocationCount = invocationCount()
59+ addMetadata(INVOCATION_COUNT , invocationCount + 1 )
60+ }
61+
62+ fun invocationCount (): Int = getMetadata(INVOCATION_COUNT ) as ? Int ? : 0
63+
64+ internal fun visitedPipeline (pipeline : String ) {
65+ val visitedPipelines = visitedPipelines().toMutableSet()
66+ visitedPipelines.add(pipeline)
67+ addMetadata(VISITED_PIPELINES , visitedPipelines)
68+ }
69+
70+ fun visitedPipelines (): Set <String > = getMetadata(VISITED_PIPELINES ) as ? Set <String > ? : emptySet()
71+
72+ private fun addMetadata (key : String , value : Any ) {
73+ metadata[key] = value
74+ }
75+
76+ private fun getMetadata (key : String ): Any? = metadata[key]
4377
44- class TestPipelineCommand : Command
78+ companion object {
79+ private const val INVOCATION_COUNT = " invocationCount"
80+ private const val VISITED_PIPELINES = " visitedPipelines"
81+ }
82+ }
4583
4684class TestPipelineCommandHandler (
4785 val mediator : Mediator
4886) : CommandHandler<TestPipelineCommand> {
4987 override suspend fun handle (command : TestPipelineCommand ) {
5088 mediator shouldNotBe null
89+ command.incrementInvocationCount()
5190 }
5291}
5392
@@ -56,6 +95,7 @@ class TestBrokenCommandHandler(
5695) : CommandHandler<TestBrokenCommand> {
5796 override suspend fun handle (command : TestBrokenCommand ) {
5897 mediator shouldNotBe null
98+ command.incrementInvocationCount()
5999 throw Exception ()
60100 }
61101}
@@ -64,14 +104,13 @@ class ExceptionPipelineBehavior : PipelineBehavior {
64104 override suspend fun <TRequest , TResponse > handle (
65105 request : TRequest ,
66106 next : RequestHandlerDelegate <TRequest , TResponse >
67- ): TResponse {
68- try {
69- // exceptionPipelineBehaviorHandleCounter++
70- return next(request)
71- } catch (ex: Exception ) {
72- // exceptionPipelineBehaviorHandleCatchCounter++
73- throw ex
107+ ): TResponse = try {
108+ when (request) {
109+ is EnrichedWithMetadata -> request.visitedPipeline(this ::class .java.simpleName)
74110 }
111+ next(request)
112+ } catch (ex: Exception ) {
113+ throw ex
75114 }
76115}
77116
@@ -80,22 +119,23 @@ class LoggingPipelineBehavior : PipelineBehavior {
80119 request : TRequest ,
81120 next : RequestHandlerDelegate <TRequest , TResponse >
82121 ): TResponse {
83- // loggingPipelineBehaviorHandleBeforeNextCounter++
84- val result = next( request)
85- // loggingPipelineBehaviorHandleAfterNextCounter++
86- return result
122+ when (request) {
123+ is EnrichedWithMetadata -> request.visitedPipeline( this :: class .java.simpleName )
124+ }
125+ return next(request)
87126 }
88127}
89128
90129class NonExistQuery : Query <String >
91130
92- class TestQuery (val id : Int ) : Query<String>
131+ class TestQuery (val id : Int ) : Query<String>, EnrichedWithMetadata()
93132
94133class TestQueryHandler (
95134 private val mediator : Mediator
96135) : QueryHandler<TestQuery, String> {
97136 override suspend fun handle (query : TestQuery ): String {
98137 mediator shouldNotBe null
138+ query.incrementInvocationCount()
99139 return " hello " + query.id
100140 }
101141}
0 commit comments