Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mgoworko committed Nov 7, 2024
1 parent 5fdbebe commit 071be85
Show file tree
Hide file tree
Showing 13 changed files with 204 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Comment private (val content: String) extends AnyVal {
object Comment {

def from(content: String): Option[Comment] = {
if (content.trim.isEmpty) None else Some(new Comment(content))
if (content.trim.nonEmpty) Some(new Comment(content)) else None
}

def unsafeFrom(content: String): Comment = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ class ActivityService(
user = loggedUser.scenarioUser,
date = now,
scenarioVersionId = Some(ScenarioVersionId.from(scenarioGraphVersionId)),
comment = commentOpt match {
case Some(comment) => ScenarioComment.WithContent(comment.content, UserName(loggedUser.username), now)
case None => ScenarioComment.WithoutContent(UserName(loggedUser.username), now)
},
comment = ScenarioComment.from(commentOpt.map(_.content), UserName(loggedUser.username), now),
result = DeploymentResult.Success(clock.instant()),
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,19 +211,11 @@ class DBProcessRepository(
date = Instant.now(),
previousScenarioVersionId = oldVersionId.map(ScenarioVersionId.from),
scenarioVersionId = versionId.map(ScenarioVersionId.from),
comment = updateProcessAction.comment.map(_.content) match {
case Some(content) =>
ScenarioComment.WithContent(
comment = content,
lastModifiedByUserName = UserName(loggedUser.username),
lastModifiedAt = clock.instant(),
)
case None =>
ScenarioComment.WithoutContent(
lastModifiedByUserName = UserName(loggedUser.username),
lastModifiedAt = clock.instant(),
)
},
comment = ScenarioComment.from(
content = updateProcessAction.comment.map(_.content),
lastModifiedByUserName = UserName(loggedUser.username),
lastModifiedAt = clock.instant(),
)
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ class DbScenarioActivityRepository private (override protected val dbRef: DbRef,
id,
activity,
ScenarioComment
.WithContent(s"Rename: [${activity.oldName}] -> [${activity.newName}]", UserName(""), activity.date),
.from(Some(s"Rename: [${activity.oldName}] -> [${activity.newName}]"), UserName(""), activity.date),
None
)
case activity: ScenarioActivity.CommentAdded =>
Expand All @@ -306,8 +306,9 @@ class DbScenarioActivityRepository private (override protected val dbRef: DbRef,
toComment(
id,
activity,
ScenarioComment.WithContent(
comment = s"Scenario migrated from ${activity.sourceEnvironment.name} by ${activity.sourceUser.value}",
ScenarioComment.from(
content =
Some(s"Scenario migrated from ${activity.sourceEnvironment.name} by ${activity.sourceUser.value}"),
lastModifiedByUserName = activity.user.name,
lastModifiedAt = activity.date
),
Expand All @@ -323,8 +324,8 @@ class DbScenarioActivityRepository private (override protected val dbRef: DbRef,
toComment(
id,
activity,
ScenarioComment.WithContent(
comment = s"Migrations applied: ${activity.changes}",
ScenarioComment.from(
content = Some(s"Migrations applied: ${activity.changes}"),
lastModifiedByUserName = activity.user.name,
lastModifiedAt = activity.date
),
Expand Down Expand Up @@ -505,8 +506,8 @@ class DbScenarioActivityRepository private (override protected val dbRef: DbRef,

private def comment(scenarioComment: ScenarioComment): Option[String] = {
scenarioComment match {
case ScenarioComment.WithContent(comment, _, _) if comment.nonEmpty => Some(comment.value)
case _ => None
case ScenarioComment.WithContent(comment, _, _) => Some(comment)
case _ => None
}
}

Expand Down Expand Up @@ -676,21 +677,11 @@ class DbScenarioActivityRepository private (override protected val dbRef: DbRef,
for {
lastModifiedByUserName <- entity.lastModifiedByUserName.toRight("Missing lastModifiedByUserName field")
lastModifiedAt <- entity.lastModifiedAt.toRight("Missing lastModifiedAt field")
} yield {
entity.comment match {
case Some(comment) if comment.nonEmpty =>
ScenarioComment.WithContent(
comment = comment,
lastModifiedByUserName = UserName(lastModifiedByUserName),
lastModifiedAt = lastModifiedAt.toInstant
)
case Some(_) | None =>
ScenarioComment.WithoutContent(
lastModifiedByUserName = UserName(lastModifiedByUserName),
lastModifiedAt = lastModifiedAt.toInstant
)
}
}
} yield ScenarioComment.from(
content = entity.comment,
lastModifiedByUserName = UserName(lastModifiedByUserName),
lastModifiedAt = lastModifiedAt.toInstant
)
}

private def attachmentFromEntity(entity: ScenarioActivityEntityData): Either[String, ScenarioAttachment] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ trait ScenarioActivityRepository {
user = user.scenarioUser,
date = now,
scenarioVersionId = Some(ScenarioVersionId.from(processVersionId)),
comment = ScenarioComment.WithContent(
comment = comment,
comment = ScenarioComment.from(
content = Some(comment),
lastModifiedByUserName = UserName(user.username),
lastModifiedAt = now,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class V1_057__MigrateActionsAndCommentsToScenarioActivities
user = user,
date = date,
scenarioVersionId = sv,
comment = WithContent("Deployment with scenario fix", user.name, date),
comment = ScenarioComment.from(Some("Deployment with scenario fix"), user.name, date),
result = DeploymentResult.Success(date),
)
)
Expand All @@ -139,7 +139,7 @@ class V1_057__MigrateActionsAndCommentsToScenarioActivities
user = user,
date = date,
scenarioVersionId = sv,
comment = WithContent("I'm canceling this scenario, it causes problems", user.name, date),
comment = ScenarioComment.from(Some("I'm canceling this scenario, it causes problems"), user.name, date),
result = DeploymentResult.Success(date),
)
)
Expand Down Expand Up @@ -183,7 +183,8 @@ class V1_057__MigrateActionsAndCommentsToScenarioActivities
user = user,
date = date,
scenarioVersionId = sv,
comment = WithContent("Paused because marketing campaign is paused for now", user.name, date),
comment =
ScenarioComment.from(Some("Paused because marketing campaign is paused for now"), user.name, date),
result = DeploymentResult.Success(date),
)
)
Expand Down Expand Up @@ -239,7 +240,7 @@ class V1_057__MigrateActionsAndCommentsToScenarioActivities
user = user,
date = date,
scenarioVersionId = sv,
comment = WithContent("Deployed at the request of business", user.name, date),
comment = ScenarioComment.from(Some("Deployed at the request of business"), user.name, date),
result = DeploymentResult.Success(date),
)
)
Expand All @@ -256,7 +257,7 @@ class V1_057__MigrateActionsAndCommentsToScenarioActivities
date = date,
scenarioVersionId = sv,
actionName = "special action",
comment = WithContent("Special action needed to be executed", user.name, date),
comment = ScenarioComment.from(Some("Special action needed to be executed"), user.name, date),
result = DeploymentResult.Success(date),
)
)
Expand All @@ -281,15 +282,15 @@ class V1_057__MigrateActionsAndCommentsToScenarioActivities
user = ScenarioUser(None, UserName("John Doe"), None, None),
date = now.toInstant,
scenarioVersionId = Some(ScenarioVersionId(processVersionId)),
comment = WithContent("ABC1", UserName(user), now.toInstant)
comment = ScenarioComment.from(Some("ABC1"), UserName(user), now.toInstant)
),
ScenarioActivity.CommentAdded(
scenarioId = ScenarioId(process.id.value),
scenarioActivityId = activities(1).scenarioActivityId,
user = ScenarioUser(None, UserName("John Doe"), None, None),
date = now.toInstant,
scenarioVersionId = Some(ScenarioVersionId(processVersionId)),
comment = WithContent("ABC2", UserName(user), now.toInstant)
comment = ScenarioComment.from(Some("ABC2"), UserName(user), now.toInstant)
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ class V1_058__UpdateAndAddMissingScenarioActivitiesSpec
),
date = Instant.now,
scenarioVersionId = None,
comment = ScenarioComment.WithContent(
comment = "Scenario migrated from TEST_ENV by test env user",
comment = ScenarioComment.from(
content = Some("Scenario migrated from TEST_ENV by test env user"),
lastModifiedByUserName = UserName(loggedUser.username),
lastModifiedAt = Instant.now
)
Expand All @@ -87,7 +87,7 @@ class V1_058__UpdateAndAddMissingScenarioActivitiesSpec
date = activities(0).date,
previousScenarioVersionId = None,
scenarioVersionId = Some(ScenarioVersionId(2)),
comment = ScenarioComment.WithoutContent(UserName("Test User"), activities(0).date)
comment = ScenarioComment.from(None, UserName("Test User"), activities(0).date)
),
ScenarioActivity.ScenarioCreated(
scenarioId = ScenarioId(process.id.value),
Expand Down Expand Up @@ -128,8 +128,8 @@ class V1_058__UpdateAndAddMissingScenarioActivitiesSpec
date = Instant.now,
previousScenarioVersionId = None,
scenarioVersionId = None,
comment = ScenarioComment.WithContent(
comment = "Scenario migrated from TEST_ENV by test env user",
comment = ScenarioComment.from(
content = Some("Scenario migrated from TEST_ENV by test env user"),
lastModifiedByUserName = UserName(loggedUser.username),
lastModifiedAt = Instant.now
)
Expand Down Expand Up @@ -177,8 +177,8 @@ class V1_058__UpdateAndAddMissingScenarioActivitiesSpec
),
date = Instant.now,
scenarioVersionId = None,
comment = ScenarioComment.WithContent(
comment = "Migrations applied: feature A\\nfeature B\\nfeature C",
comment = ScenarioComment.from(
content = Some("Migrations applied: feature A\\nfeature B\\nfeature C"),
lastModifiedByUserName = UserName(loggedUser.username),
lastModifiedAt = Instant.now
)
Expand Down Expand Up @@ -226,8 +226,8 @@ class V1_058__UpdateAndAddMissingScenarioActivitiesSpec
date = Instant.now,
previousScenarioVersionId = None,
scenarioVersionId = None,
comment = ScenarioComment.WithContent(
comment = "Migrations applied: feature A\\nfeature B\\nfeature C",
comment = ScenarioComment.from(
content = Some("Migrations applied: feature A\\nfeature B\\nfeature C"),
lastModifiedByUserName = UserName(loggedUser.username),
lastModifiedAt = Instant.now
)
Expand Down Expand Up @@ -274,8 +274,8 @@ class V1_058__UpdateAndAddMissingScenarioActivitiesSpec
),
date = Instant.now,
scenarioVersionId = None,
comment = ScenarioComment.WithContent(
comment = "Rename: [oldUglyName] -> [newPrettyName]",
comment = ScenarioComment.from(
content = Some("Rename: [oldUglyName] -> [newPrettyName]"),
lastModifiedByUserName = UserName(loggedUser.username),
lastModifiedAt = Instant.now
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ class MockDeploymentManager(
date = Instant.now(),
scenarioVersionId = Some(ScenarioVersionId.from(processVersion.versionId)),
actionName = "Custom action of MockDeploymentManager just before deployment",
comment = ScenarioComment.WithContent(
comment = "With comment from DeploymentManager",
comment = ScenarioComment.from(
content = Some("With comment from DeploymentManager"),
lastModifiedByUserName = ScenarioUser.internalNuUser.name,
lastModifiedAt = Instant.now()
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,8 @@ class ScenarioActivityApiHttpServiceBusinessSpec
date = clock.instant(),
scenarioVersionId = None,
actionName = "Custom action handled by deployment manager",
comment = ScenarioComment.WithContent(
comment = "Executed on custom deployment manager",
comment = ScenarioComment.from(
content = Some("Executed on custom deployment manager"),
lastModifiedByUserName = UserName("custom-user"),
lastModifiedAt = clock.instant()
),
Expand All @@ -417,8 +417,8 @@ class ScenarioActivityApiHttpServiceBusinessSpec
date = clock.instant(),
scenarioVersionId = None,
actionName = "Custom action handled by deployment manager",
comment = ScenarioComment.WithContent(
comment = "Executed on custom deployment manager",
comment = ScenarioComment.from(
content = Some("Executed on custom deployment manager"),
lastModifiedByUserName = UserName("custom-user"),
lastModifiedAt = clock.instant()
),
Expand Down Expand Up @@ -481,8 +481,8 @@ class ScenarioActivityApiHttpServiceBusinessSpec
user = ScenarioUser(None, UserName("custom-user"), None, None),
date = clock.instant(),
scenarioVersionId = None,
comment = ScenarioComment.WithContent(
comment = "Immediate execution",
comment = ScenarioComment.from(
content = Some("Immediate execution"),
lastModifiedByUserName = UserName("custom-user"),
lastModifiedAt = clock.instant()
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -943,23 +943,37 @@ class DeploymentServiceSpec
val processName: ProcessName = generateProcessName
val processIdWithName = prepareProcess(processName).dbioActionValues
val actionName = ScenarioActionName("hello")
val comment = Comment.from("not empty comment")
val nonEmptyCommentContent = "not empty comment"
val comments = List(
Comment.from(nonEmptyCommentContent),
Comment.from(""),
Comment.from(" "),
)

val result =
val results = comments.map { comment =>
deploymentService
.processCommand(
CustomActionCommand(CommonCommandData(processIdWithName, comment, user), actionName, Map.empty)
)
.futureValue
}

eventually {
result shouldBe CustomActionResult("Hi")
val action =
actionRepository.getFinishedProcessActions(processIdWithName.id, Some(Set(actionName))).dbioActionValues

action.loneElement.state shouldBe ProcessActionState.Finished
action.loneElement.comment shouldBe comment.map(_.content)
listener.events.toArray.filter(_.isInstanceOf[OnActionSuccess]) should have length 1
all(results) shouldBe CustomActionResult("Hi")
val actions =
actionRepository
.getFinishedProcessActions(processIdWithName.id, Some(Set(actionName)))
.dbioActionValues
.sortBy(_.createdAt)

actions.size shouldBe 3
all(actions.map(_.state)) shouldBe ProcessActionState.Finished
actions.map(_.comment) shouldBe List(
Some(nonEmptyCommentContent),
None,
None,
)
listener.events.toArray.filter(_.isInstanceOf[OnActionSuccess]) should have length 3
}
}

Expand Down
Loading

0 comments on commit 071be85

Please sign in to comment.