Skip to content

Commit

Permalink
[NU-1806] Test with BoundedSourceWithOffset
Browse files Browse the repository at this point in the history
  • Loading branch information
gskrobisz committed Sep 13, 2024
1 parent 5f04e58 commit 72808f2
Show file tree
Hide file tree
Showing 13 changed files with 308 additions and 174 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package pl.touk.nussknacker.engine.api.component

import io.circe.generic.JsonCodec
import io.circe.generic.extras.semiauto.{deriveUnwrappedDecoder, deriveUnwrappedEncoder}
import cats.syntax.functor._
import io.circe.{Decoder, Encoder}
import io.circe.generic.auto._
import io.circe.syntax._
import pl.touk.nussknacker.engine.api.NodeId

final case class NodesDeploymentData(dataByNodeId: Map[NodeId, NodeDeploymentData])
Expand All @@ -20,20 +21,24 @@ object NodesDeploymentData {

}

@JsonCodec sealed trait NodeDeploymentData
sealed trait NodeDeploymentData

final case class SqlFilteringExpression(sqlExpression: String) extends NodeDeploymentData

final case class KafkaSourceDeploymentData(offset: String) extends NodeDeploymentData

//object NodeDeploymentData {
//
// implicit val nodeDeploymentDataEncoder: Encoder[NodeDeploymentData] =
// deriveUnwrappedEncoder[SqlFilteringExpression].contramap { case sqlExpression: SqlFilteringExpression =>
// sqlExpression
// }
//
// implicit val nodeDeploymentDataDecoder: Decoder[NodeDeploymentData] =
// deriveUnwrappedDecoder[SqlFilteringExpression].map(identity)
//
//}
final case class KafkaSourceOffset(offset: Long) extends NodeDeploymentData

object NodeDeploymentData {

implicit val nodeDeploymentDataEncoder: Encoder[NodeDeploymentData] =
Encoder.instance {
case s: SqlFilteringExpression => s.asJson
case o: KafkaSourceOffset => o.asJson
}

implicit val nodeDeploymentDataDecoder: Decoder[NodeDeploymentData] =
List[Decoder[NodeDeploymentData]](
Decoder[SqlFilteringExpression].widen,
Decoder[KafkaSourceOffset].widen
).reduceLeft(_ or _)

}
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ object DeploymentApiEndpoints {
modifiedAt: Instant
)

implicit val nodeDeploymentDataCodec: Schema[NodeDeploymentData] = Schema.string[SqlFilteringExpression].as
implicit val nodeDeploymentDataCodec: Schema[NodeDeploymentData] = Schema.derived

implicit val nodesDeploymentDataCodec: Schema[NodesDeploymentData] = Schema
.schemaForMap[NodeId, NodeDeploymentData](_.id)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,52 +1,89 @@
package pl.touk.nussknacker.ui.api

import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.testkit.ScalatestRouteTest
import de.heikoseeberger.akkahttpcirce.FailFastCirceSupport
import io.circe.Json
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers
import pl.touk.nussknacker.engine.api.graph.ScenarioGraph
import pl.touk.nussknacker.security.Permission
import pl.touk.nussknacker.test.base.it.NuResourcesTest
import pl.touk.nussknacker.test.config.WithSimplifiedDesignerConfig.TestProcessingType.Streaming
import pl.touk.nussknacker.test.utils.domain.ProcessTestData
import pl.touk.nussknacker.test.utils.domain.TestFactory.{mapProcessingTypeDataProvider, withPermissions}
import pl.touk.nussknacker.test.{EitherValuesDetailedMessage, PatientScalaFutures}
import pl.touk.nussknacker.test.utils.scalas.AkkaHttpExtensions.toRequestEntity
import io.restassured.RestAssured.`given`
import io.restassured.module.scala.RestAssuredSupport.AddThenToResponse
import org.hamcrest.Matchers.equalTo
import org.scalatest.freespec.AnyFreeSpecLike
import pl.touk.nussknacker.engine.build.ScenarioBuilder
import pl.touk.nussknacker.test.base.it.{NuItTest, WithSimplifiedConfigScenarioHelper}
import pl.touk.nussknacker.test.config.{WithBusinessCaseRestAssuredUsersExtensions, WithSimplifiedDesignerConfig}
import pl.touk.nussknacker.test.{NuRestAssureMatchers, RestAssuredVerboseLoggingIfValidationFails}
import pl.touk.nussknacker.engine.spel.SpelExtension._
import pl.touk.nussknacker.test.utils.domain.TestProcessUtil

class ActivityInfoResourcesSpec
extends AnyFunSuite
with ScalatestRouteTest
with Matchers
with FailFastCirceSupport
with NuResourcesTest
with PatientScalaFutures
with EitherValuesDetailedMessage {

private val scenarioGraph: ScenarioGraph = ProcessTestData.sampleScenarioGraph
private val testPermissionAll = List(Permission.Deploy, Permission.Read, Permission.Write)

private def route() = new ActivityInfoResources(
processService,
mapProcessingTypeDataProvider(
Streaming.stringify -> createScenarioActivityService
)
)

test("get activity parameters") {
saveProcess(scenarioGraph) {
Post(
s"/activityInfo/${ProcessTestData.sampleProcessName}/activityParameters",
scenarioGraph.toJsonRequestEntity()
) ~> withPermissions(
route(),
testPermissionAll: _*
) ~> check {
status shouldEqual StatusCodes.OK
val content = entityAs[Json].noSpaces
content shouldBe """{}"""
}
extends AnyFreeSpecLike
with NuItTest
with WithSimplifiedDesignerConfig
with WithSimplifiedConfigScenarioHelper
with WithBusinessCaseRestAssuredUsersExtensions
with NuRestAssureMatchers
with RestAssuredVerboseLoggingIfValidationFails {

"The scenario activity info endpoint when" - {
"return activity parameters when defined" in {
val scenario = ScenarioBuilder
.streaming("scenarioWithSourceWithDeployParameters")
.source("sourceWithParametersId", "boundedSourceWithOffset", "elements" -> "{'one', 'two', 'three'}".spel)
.emptySink("exampleSinkId", "emptySink")

given()
.applicationState {
createSavedScenario(scenario)
}
.when()
.basicAuthAllPermUser()
.jsonBody(TestProcessUtil.toJson(scenario).noSpaces)
.post(s"$nuDesignerHttpAddress/api/activityInfo/${scenario.name.value}/activityParameters")
.Then()
.statusCode(200)
.body(
"DEPLOY[0].sourceId",
equalTo("sourceWithParametersId"),
"DEPLOY[0].parameters[0].name",
equalTo("offset"),
"DEPLOY[0].parameters[0].typ.display",
equalTo("Long")
)
}

"return empty map when no activity parameters" in {
val scenario = ScenarioBuilder
.streaming("scenarioWithoutParameters")
.source("sourceNoParamsId", "boundedSource", "elements" -> "{'one', 'two', 'three'}".spel)
.emptySink("exampleSinkId", "emptySink")

given()
.applicationState {
createSavedScenario(scenario)
}
.when()
.basicAuthAllPermUser()
.jsonBody(TestProcessUtil.toJson(scenario).noSpaces)
.post(s"$nuDesignerHttpAddress/api/activityInfo/${scenario.name.value}/activityParameters")
.Then()
.statusCode(200)
.equalsJsonBody(
"{}"
)
}

"return no data found when there is no scenario" in {
val scenario = ScenarioBuilder
.streaming("invalidScenario")
.source("exampleSource", "boundedSource", "elements" -> "{'one', 'two', 'three'}".spel)
.emptySink("exampleSinkId", "emptySink")

given()
.when()
.basicAuthAllPermUser()
.jsonBody(TestProcessUtil.toJson(scenario).noSpaces)
.post(s"$nuDesignerHttpAddress/api/activityInfo/${scenario.name.value}/activityParameters")
.Then()
.statusCode(404)
.equalsPlainBody(
s"No scenario ${scenario.name.value} found"
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ class ComponentApiHttpServiceBusinessSpec
"streaming-sink-monitor",
"streaming-sink-sendsms",
"streaming-source-boundedsource",
"streaming-source-boundedsourcewithoffset",
"streaming-source-classinstancesource",
"streaming-source-communicationsource",
"streaming-source-csv-source",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ class ComponentApiHttpServiceSecuritySpec
"streaming1-sink-monitor",
"streaming1-sink-sendsms",
"streaming1-source-boundedsource",
"streaming1-source-boundedsourcewithoffset",
"streaming1-source-classinstancesource",
"streaming1-source-communicationsource",
"streaming1-source-csv-source",
Expand Down Expand Up @@ -327,6 +328,7 @@ class ComponentApiHttpServiceSecuritySpec
"streaming2-sink-monitor",
"streaming2-sink-sendsms",
"streaming2-source-boundedsource",
"streaming2-source-boundedsourcewithoffset",
"streaming2-source-classinstancesource",
"streaming2-source-communicationsource",
"streaming2-source-csv-source",
Expand Down Expand Up @@ -391,6 +393,7 @@ class ComponentApiHttpServiceSecuritySpec
"streaming1-sink-monitor",
"streaming1-sink-sendsms",
"streaming1-source-boundedsource",
"streaming1-source-boundedsourcewithoffset",
"streaming1-source-classinstancesource",
"streaming1-source-communicationsource",
"streaming1-source-csv-source",
Expand Down Expand Up @@ -448,6 +451,7 @@ class ComponentApiHttpServiceSecuritySpec
"streaming2-sink-monitor",
"streaming2-sink-sendsms",
"streaming2-source-boundedsource",
"streaming2-source-boundedsourcewithoffset",
"streaming2-source-classinstancesource",
"streaming2-source-communicationsource",
"streaming2-source-csv-source",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class DeploymentApiHttpServiceBusinessSpec
private val correctDeploymentRequest = s"""{
| "scenarioName": "$scenarioName",
| "nodesDeploymentData": {
| "$sourceNodeId": "`date` = '2024-01-01'"
| "$sourceNodeId": {"sqlExpression":"`date` = '2024-01-01'"}
| }
|}""".stripMargin

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class DeploymentApiHttpServiceDeploymentCommentSpec
.jsonBody(s"""{
| "scenarioName": "$scenarioName",
| "nodesDeploymentData": {
| "$sourceNodeId": "`date` = '2024-01-01'"
| "$sourceNodeId": {"sqlExpression":"`date` = '2024-01-01'"}
| }
|}""".stripMargin)
.put(s"$nuDesignerHttpAddress/api/deployments/${DeploymentId.generate}")
Expand All @@ -99,7 +99,7 @@ class DeploymentApiHttpServiceDeploymentCommentSpec
.jsonBody(s"""{
| "scenarioName": "$scenarioName",
| "nodesDeploymentData": {
| "$sourceNodeId": "`date` = '2024-01-01'"
| "$sourceNodeId": {"sqlExpression":"`date` = '2024-01-01'"}
| },
| "comment": "deployment comment not matching configured pattern"
|}""".stripMargin)
Expand All @@ -121,7 +121,7 @@ class DeploymentApiHttpServiceDeploymentCommentSpec
.jsonBody(s"""{
| "scenarioName": "$scenarioName",
| "nodesDeploymentData": {
| "$sourceNodeId": "`date` = '2024-01-01'"
| "$sourceNodeId": {"sqlExpression":"`date` = '2024-01-01'"}
| },
| "comment": "comment with $configuredPhrase"
|}""".stripMargin)
Expand Down
Loading

0 comments on commit 72808f2

Please sign in to comment.