|
| 1 | +package za.co.absa.atum.agent.dispatcher |
| 2 | + |
| 3 | +import org.scalatest.flatspec.AnyFlatSpec |
| 4 | +import org.scalatest.matchers.should.Matchers |
| 5 | +import org.scalatest.BeforeAndAfterEach |
| 6 | +import org.mockito.Mockito._ |
| 7 | +import org.mockito.ArgumentMatchers._ |
| 8 | +import sttp.client3._ |
| 9 | +import sttp.model.StatusCode |
| 10 | +import com.typesafe.config.Config |
| 11 | +import sttp.capabilities |
| 12 | +import za.co.absa.atum.model.dto._ |
| 13 | +import za.co.absa.atum.model.envelopes.SuccessResponse.{MultiSuccessResponse, SingleSuccessResponse} |
| 14 | +import za.co.absa.atum.model.utils.JsonSyntaxExtensions.JsonSerializationSyntax |
| 15 | + |
| 16 | +class HttpDispatcherUnitTests extends AnyFlatSpec with Matchers with BeforeAndAfterEach { |
| 17 | + |
| 18 | + var mockBackend: SttpBackend[Identity, sttp.capabilities.WebSockets] = _ |
| 19 | + var mockConfig: Config = _ |
| 20 | + val serverUrl = "http://test-server" |
| 21 | + |
| 22 | + val testPartitioningDTO: Seq[PartitionDTO] = Seq(PartitionDTO("k", "v")) |
| 23 | + val testPartitioningSubmitDTO = PartitioningSubmitDTO(testPartitioningDTO, None, "author") |
| 24 | + val createdPartitioningWithId = PartitioningWithIdDTO(123L, testPartitioningDTO, "author") |
| 25 | + 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 | + ) |
| 30 | + |
| 31 | + def encodedPartitioning(partitioning: Seq[PartitionDTO]): String = |
| 32 | + partitioning.asBase64EncodedJsonString |
| 33 | + |
| 34 | + override def beforeEach(): Unit = { |
| 35 | + mockBackend = mock(classOf[SttpBackend[Identity, sttp.capabilities.WebSockets]]) |
| 36 | + mockConfig = mock(classOf[Config]) |
| 37 | + when(mockConfig.getString(any[String])).thenReturn(serverUrl) |
| 38 | + } |
| 39 | + |
| 40 | + def dispatcher: HttpDispatcher = new HttpDispatcher(mockConfig) { |
| 41 | + override private[dispatcher] val backend = mockBackend |
| 42 | + } |
| 43 | + |
| 44 | + def isGetPartitioningRequest( |
| 45 | + partitioning: Seq[PartitionDTO] |
| 46 | + ): Request[Either[String, String], capabilities.WebSockets] = |
| 47 | + argThat(new org.mockito.ArgumentMatcher[Request[Either[String, String], sttp.capabilities.WebSockets]] { |
| 48 | + override def matches(req: Request[Either[String, String], sttp.capabilities.WebSockets]): Boolean = |
| 49 | + req != null && |
| 50 | + req.method.method == "GET" && |
| 51 | + req.uri.path.mkString.contains("partitionings") && |
| 52 | + req.uri.params.toSeq.exists { case (k, v) => |
| 53 | + k == "partitioning" && v == encodedPartitioning(partitioning) |
| 54 | + } |
| 55 | + }) |
| 56 | + |
| 57 | + def isPostPartitioningRequest: Request[Either[String, String], capabilities.WebSockets] = |
| 58 | + argThat(new org.mockito.ArgumentMatcher[Request[Either[String, String], sttp.capabilities.WebSockets]] { |
| 59 | + override def matches(req: Request[Either[String, String], sttp.capabilities.WebSockets]): Boolean = |
| 60 | + req != null && req.method.method == "POST" && req.uri.path.mkString.contains("partitionings") |
| 61 | + }) |
| 62 | + |
| 63 | + def isGetMeasuresRequest: Request[Either[String, String], capabilities.WebSockets] = |
| 64 | + argThat(new org.mockito.ArgumentMatcher[Request[Either[String, String], sttp.capabilities.WebSockets]] { |
| 65 | + override def matches(req: Request[Either[String, String], sttp.capabilities.WebSockets]): Boolean = |
| 66 | + req != null && req.method.method == "GET" && req.uri.path.mkString.contains("measures") |
| 67 | + }) |
| 68 | + |
| 69 | + def isGetAdditionalDataRequest: Request[Either[String, String], capabilities.WebSockets] = |
| 70 | + argThat(new org.mockito.ArgumentMatcher[Request[Either[String, String], sttp.capabilities.WebSockets]] { |
| 71 | + override def matches(req: Request[Either[String, String], sttp.capabilities.WebSockets]): Boolean = |
| 72 | + req != null && req.method.method == "GET" && req.uri.path.mkString.contains("additional-data") |
| 73 | + }) |
| 74 | + |
| 75 | + def stubGetPartitioning(partitioning: Seq[PartitionDTO], response: Response[Either[String, String]]): Unit = |
| 76 | + when(mockBackend.send(isGetPartitioningRequest(partitioning))).thenReturn(response) |
| 77 | + |
| 78 | + def stubPostPartitioning(response: Response[Either[String, String]]): Unit = |
| 79 | + when(mockBackend.send(isPostPartitioningRequest)).thenReturn(response) |
| 80 | + |
| 81 | + def stubGetMeasures(response: Response[Either[String, String]]): Unit = |
| 82 | + when(mockBackend.send(isGetMeasuresRequest)).thenReturn(response) |
| 83 | + |
| 84 | + def stubGetAdditionalData(response: Response[Either[String, String]]): Unit = |
| 85 | + when(mockBackend.send(isGetAdditionalDataRequest)).thenReturn(response) |
| 86 | + |
| 87 | + "createPartitioning" should "create and return AtumContextDTO when partitioning does not exist" in { |
| 88 | + val parentPartitioning = Seq(PartitionDTO("parentK", "parentV")) |
| 89 | + val parentPartitioningId = 999L |
| 90 | + val parentPartitioningWithId = PartitioningWithIdDTO(parentPartitioningId, parentPartitioning, "parentAuthor") |
| 91 | + val getParentResponse = Response( |
| 92 | + Right(SingleSuccessResponse(parentPartitioningWithId).asJsonString): Either[String, String], |
| 93 | + StatusCode.Ok |
| 94 | + ) |
| 95 | + val getPartitioningResponse = Response(Left("Not found"): Either[String, String], StatusCode.NotFound) |
| 96 | + val postPartitioningResponse = Response( |
| 97 | + Right(SingleSuccessResponse(createdPartitioningWithId).asJsonString): Either[String, String], |
| 98 | + StatusCode.Created |
| 99 | + ) |
| 100 | + val measuresResponse = |
| 101 | + Response(Right(MultiSuccessResponse(measures).asJsonString): Either[String, String], StatusCode.Ok) |
| 102 | + val additionalDataResponse = |
| 103 | + Response(Right(SingleSuccessResponse(additionalData).asJsonString): Either[String, String], StatusCode.Ok) |
| 104 | + |
| 105 | + stubGetPartitioning(parentPartitioning, getParentResponse) |
| 106 | + stubGetPartitioning(testPartitioningDTO, getPartitioningResponse) |
| 107 | + stubPostPartitioning(postPartitioningResponse) |
| 108 | + stubGetMeasures(measuresResponse) |
| 109 | + stubGetAdditionalData(additionalDataResponse) |
| 110 | + |
| 111 | + val dispatcherWithMocks = dispatcher |
| 112 | + val result = dispatcherWithMocks.createPartitioning( |
| 113 | + PartitioningSubmitDTO(testPartitioningDTO, Some(parentPartitioning), "author") |
| 114 | + ) |
| 115 | + |
| 116 | + result.partitioning shouldBe createdPartitioningWithId.partitioning |
| 117 | + result.measures shouldBe measures.toSet |
| 118 | + result.additionalData should contain key "key1" |
| 119 | + result.additionalData should contain key "key2" |
| 120 | + result.additionalData("key1") shouldBe Some("val1") |
| 121 | + result.additionalData("key2") shouldBe None |
| 122 | + } |
| 123 | + |
| 124 | + it should "return AtumContextDTO for existing partitioning without creating a new one" in { |
| 125 | + val existingPartitioningWithId = PartitioningWithIdDTO(123L, testPartitioningDTO, "author") |
| 126 | + val getPartitioningResponse = Response( |
| 127 | + Right(SingleSuccessResponse(existingPartitioningWithId).asJsonString): Either[String, String], |
| 128 | + StatusCode.Ok |
| 129 | + ) |
| 130 | + val measuresResponse = |
| 131 | + Response(Right(MultiSuccessResponse(measures).asJsonString): Either[String, String], StatusCode.Ok) |
| 132 | + val additionalDataResponse = |
| 133 | + Response(Right(SingleSuccessResponse(additionalData).asJsonString): Either[String, String], StatusCode.Ok) |
| 134 | + |
| 135 | + stubGetPartitioning(testPartitioningDTO, getPartitioningResponse) |
| 136 | + stubGetMeasures(measuresResponse) |
| 137 | + stubGetAdditionalData(additionalDataResponse) |
| 138 | + |
| 139 | + val dispatcherWithMocks = dispatcher |
| 140 | + val result = dispatcherWithMocks.createPartitioning(testPartitioningSubmitDTO) |
| 141 | + |
| 142 | + result.partitioning shouldBe existingPartitioningWithId.partitioning |
| 143 | + result.measures shouldBe measures.toSet |
| 144 | + result.additionalData should contain key "key1" |
| 145 | + result.additionalData should contain key "key2" |
| 146 | + result.additionalData("key1") shouldBe Some("val1") |
| 147 | + result.additionalData("key2") shouldBe None |
| 148 | + |
| 149 | + verify(mockBackend, never()).send(isPostPartitioningRequest) |
| 150 | + } |
| 151 | + |
| 152 | + it should "handle empty measures and additional data for a new partitioning" in { |
| 153 | + val getPartitioningResponse = Response(Left("Not found"): Either[String, String], StatusCode.NotFound) |
| 154 | + val postPartitioningResponse = Response( |
| 155 | + Right(SingleSuccessResponse(createdPartitioningWithId).asJsonString): Either[String, String], |
| 156 | + StatusCode.Created |
| 157 | + ) |
| 158 | + val measuresResponse = |
| 159 | + Response(Right(MultiSuccessResponse(Seq.empty[MeasureDTO]).asJsonString): Either[String, String], StatusCode.Ok) |
| 160 | + val additionalDataResponse = Response( |
| 161 | + Right(SingleSuccessResponse(Map.empty[String, Option[AdditionalDataItemDTO]]).asJsonString): Either[ |
| 162 | + String, |
| 163 | + String |
| 164 | + ], |
| 165 | + StatusCode.Ok |
| 166 | + ) |
| 167 | + |
| 168 | + stubGetPartitioning(testPartitioningDTO, getPartitioningResponse) |
| 169 | + stubPostPartitioning(postPartitioningResponse) |
| 170 | + stubGetMeasures(measuresResponse) |
| 171 | + stubGetAdditionalData(additionalDataResponse) |
| 172 | + |
| 173 | + val dispatcherWithMocks = dispatcher |
| 174 | + val result = dispatcherWithMocks.createPartitioning(testPartitioningSubmitDTO) |
| 175 | + |
| 176 | + result.partitioning shouldBe createdPartitioningWithId.partitioning |
| 177 | + result.measures shouldBe empty |
| 178 | + result.additionalData shouldBe empty |
| 179 | + } |
| 180 | +} |
0 commit comments