@@ -2,17 +2,20 @@ package za.co.absa.atum.agent.dispatcher
22
33import org .scalatest .flatspec .AnyFlatSpec
44import org .scalatest .matchers .should .Matchers
5- import org .scalatest .BeforeAndAfterEach
5+ import org .scalatest .{ BeforeAndAfterEach , Ignore }
66import org .mockito .Mockito ._
77import org .mockito .ArgumentMatchers ._
88import sttp .client3 ._
99import sttp .model .StatusCode
1010import com .typesafe .config .Config
1111import sttp .capabilities
12+ import za .co .absa .atum .agent .exception .AtumAgentException .HttpException
1213import za .co .absa .atum .model .dto ._
1314import za .co .absa .atum .model .envelopes .SuccessResponse .{MultiSuccessResponse , SingleSuccessResponse }
1415import za .co .absa .atum .model .utils .JsonSyntaxExtensions .JsonSerializationSyntax
1516
17+ import java .time .ZonedDateTime
18+
1619class HttpDispatcherUnitTests extends AnyFlatSpec with Matchers with BeforeAndAfterEach {
1720
1821 var mockBackend : SttpBackend [Identity , sttp.capabilities.WebSockets ] = _
@@ -23,9 +26,9 @@ class HttpDispatcherUnitTests extends AnyFlatSpec with Matchers with BeforeAndAf
2326 val testPartitioningSubmitDTO = PartitioningSubmitDTO (testPartitioningDTO, None , " author" )
2427 val createdPartitioningWithId = PartitioningWithIdDTO (123L , testPartitioningDTO, " author" )
2528 val measures : Seq [MeasureDTO ] = Seq (MeasureDTO (" m1" , Seq (" c1" )), MeasureDTO (" m2" , Seq (" c2" )))
26- val additionalData : Map [ String , Option [ AdditionalDataItemDTO ]] = Map (
27- " key1" -> Some (AdditionalDataItemDTO ( " val1" , " author1" ) ),
28- " key2" -> None
29+ val additionalData : Seq [ AdditionalDataItemV2DTO ] = Seq (
30+ AdditionalDataItemV2DTO ( " key1" , Some (" val1" ) , " author1" ),
31+ AdditionalDataItemV2DTO ( " key2" , None , " author2 " )
2932 )
3033
3134 def encodedPartitioning (partitioning : Seq [PartitionDTO ]): String =
@@ -100,7 +103,7 @@ class HttpDispatcherUnitTests extends AnyFlatSpec with Matchers with BeforeAndAf
100103 val measuresResponse =
101104 Response (Right (MultiSuccessResponse (measures).asJsonString): Either [String , String ], StatusCode .Ok )
102105 val additionalDataResponse =
103- Response (Right (SingleSuccessResponse (additionalData).asJsonString): Either [String , String ], StatusCode .Ok )
106+ Response (Right (MultiSuccessResponse [ AdditionalDataItemV2DTO ] (additionalData).asJsonString): Either [String , String ], StatusCode .Ok )
104107
105108 stubGetPartitioning(parentPartitioning, getParentResponse)
106109 stubGetPartitioning(testPartitioningDTO, getPartitioningResponse)
@@ -158,7 +161,7 @@ class HttpDispatcherUnitTests extends AnyFlatSpec with Matchers with BeforeAndAf
158161 val measuresResponse =
159162 Response (Right (MultiSuccessResponse (Seq .empty[MeasureDTO ]).asJsonString): Either [String , String ], StatusCode .Ok )
160163 val additionalDataResponse = Response (
161- Right (SingleSuccessResponse ( Map .empty[String , Option [ AdditionalDataItemDTO ] ]).asJsonString): Either [
164+ Right (MultiSuccessResponse ( Seq .empty[AdditionalDataItemV2DTO ]).asJsonString): Either [
162165 String ,
163166 String
164167 ],
@@ -177,4 +180,83 @@ class HttpDispatcherUnitTests extends AnyFlatSpec with Matchers with BeforeAndAf
177180 result.measures shouldBe empty
178181 result.additionalData shouldBe empty
179182 }
183+
184+ " saveCheckpoint" should " successfully save a checkpoint when partitioning exists" in {
185+ val checkpoint = CheckpointDTO (
186+ id = java.util.UUID .randomUUID(),
187+ name = " cp" ,
188+ author = " author" ,
189+ measuredByAtumAgent = true ,
190+ processStartTime = ZonedDateTime .now(),
191+ processEndTime = Some (ZonedDateTime .now()),
192+ measurements = Set .empty,
193+ partitioning = testPartitioningDTO
194+ )
195+ val partitioningWithId = PartitioningWithIdDTO (123L , testPartitioningDTO, " author" )
196+ val getPartitioningResponse = Response (
197+ Right (SingleSuccessResponse (partitioningWithId).asJsonString): Either [String , String ],
198+ StatusCode .Ok
199+ )
200+ val postCheckpointResponse = Response (Right (" {}" ): Either [String , String ], StatusCode .Created )
201+
202+ stubGetPartitioning(testPartitioningDTO, getPartitioningResponse)
203+ when(
204+ mockBackend.send(
205+ argThat[Request [Either [String , String ], capabilities.WebSockets ]](
206+ req => req.method.method == " POST" && req.uri.path.mkString.contains(" checkpoints" )
207+ )
208+ )
209+ ).thenReturn(postCheckpointResponse)
210+
211+ noException should be thrownBy dispatcher.saveCheckpoint(checkpoint)
212+ }
213+
214+ it should " throw HttpException if partitioning is not found" in {
215+ val checkpoint = CheckpointDTO (
216+ id = java.util.UUID .randomUUID(),
217+ name = " cp" ,
218+ author = " author" ,
219+ measuredByAtumAgent = true ,
220+ processStartTime = ZonedDateTime .now(),
221+ processEndTime = Some (ZonedDateTime .now()),
222+ measurements = Set .empty,
223+ partitioning = testPartitioningDTO
224+ )
225+ val notFoundResponse = Response (Left (" Not found" ): Either [String , String ], StatusCode .NotFound )
226+
227+ stubGetPartitioning(testPartitioningDTO, notFoundResponse)
228+
229+ an[HttpException ] should be thrownBy dispatcher.saveCheckpoint(checkpoint)
230+ }
231+
232+ it should " throw HttpException if server returns error on checkpoint save" in {
233+ val checkpoint = CheckpointDTO (
234+ id = java.util.UUID .randomUUID(),
235+ name = " cp" ,
236+ author = " author" ,
237+ measuredByAtumAgent = true ,
238+ processStartTime = ZonedDateTime .now(),
239+ processEndTime = Some (ZonedDateTime .now()),
240+ measurements = Set .empty,
241+ partitioning = testPartitioningDTO
242+ )
243+ val partitioningWithId = PartitioningWithIdDTO (123L , testPartitioningDTO, " author" )
244+ val getPartitioningResponse = Response (
245+ Right (SingleSuccessResponse (partitioningWithId).asJsonString): Either [String , String ],
246+ StatusCode .Ok
247+ )
248+ val badRequestResponse = Response (Left (" Bad request" ): Either [String , String ], StatusCode .BadRequest )
249+
250+ stubGetPartitioning(testPartitioningDTO, getPartitioningResponse)
251+ when(
252+ mockBackend.send(
253+ argThat[Request [Either [String , String ], capabilities.WebSockets ]](
254+ req => req.method.method == " POST" && req.uri.path.mkString.contains(" checkpoints" )
255+ )
256+ )
257+ ).thenReturn(badRequestResponse)
258+
259+ an[HttpException ] should be thrownBy dispatcher.saveCheckpoint(checkpoint)
260+ }
261+
180262}
0 commit comments