Skip to content

Commit 41fd8f0

Browse files
authored
Fix/758 fix flaky notification rule validation service test (#765)
1 parent af63bc1 commit 41fd8f0

File tree

1 file changed

+63
-59
lines changed

1 file changed

+63
-59
lines changed

src/test/scala/za/co/absa/hyperdrive/trigger/api/rest/services/NotificationRuleValidationServiceTest.scala

Lines changed: 63 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import org.mockito.ArgumentMatchers.{eq => eqTo, _}
1919
import org.mockito.Mockito._
2020
import org.scalatest.mockito.MockitoSugar
2121
import org.scalatest.{AsyncFlatSpec, BeforeAndAfter, Matchers}
22-
import za.co.absa.hyperdrive.trigger.TestUtils.await
2322
import za.co.absa.hyperdrive.trigger.models.NotificationRule
2423
import za.co.absa.hyperdrive.trigger.models.enums.DagInstanceStatuses
2524
import za.co.absa.hyperdrive.trigger.models.errors.{ApiException, ValidationError}
@@ -46,40 +45,40 @@ class NotificationRuleValidationServiceTest extends AsyncFlatSpec with Matchers
4645
.thenReturn(Future(true))
4746

4847
// when
49-
await(underTest.validate(notificationRule))
50-
51-
// then
52-
verify(workflowRepository).existsProject(eqTo(notificationRule.project.get))(any[ExecutionContext])
53-
verify(workflowRepository).existsWorkflowWithPrefix(eqTo(notificationRule.workflowPrefix.get))(
54-
any[ExecutionContext]
55-
)
56-
succeed
48+
underTest.validate(notificationRule).map { _ =>
49+
// then
50+
verify(workflowRepository).existsProject(eqTo(notificationRule.project.get))(any[ExecutionContext])
51+
verify(workflowRepository).existsWorkflowWithPrefix(eqTo(notificationRule.workflowPrefix.get))(
52+
any[ExecutionContext]
53+
)
54+
succeed
55+
}
5756
}
5857

5958
it should "succeed if neither project nor workflowPrefix are specified" in {
6059
// given
6160
val notificationRule = createNotificationRule().copy(project = None, workflowPrefix = None)
6261

6362
// when
64-
await(underTest.validate(notificationRule))
65-
66-
// then
67-
verify(workflowRepository, never()).existsProject(any())(any())
68-
verify(workflowRepository, never()).existsWorkflowWithPrefix(any())(any())
69-
succeed
63+
underTest.validate(notificationRule).map { _ =>
64+
// then
65+
verify(workflowRepository, never()).existsProject(any())(any())
66+
verify(workflowRepository, never()).existsWorkflowWithPrefix(any())(any())
67+
succeed
68+
}
7069
}
7170

7271
it should "succeed if both project and workflowPrefix are empty" in {
7372
// given
7473
val notificationRule = createNotificationRule().copy(project = Some(""), workflowPrefix = Some(""))
7574

7675
// when
77-
await(underTest.validate(notificationRule))
78-
79-
// then
80-
verify(workflowRepository, never()).existsProject(any())(any())
81-
verify(workflowRepository, never()).existsWorkflowWithPrefix(any())(any())
82-
succeed
76+
underTest.validate(notificationRule).map { _ =>
77+
// then
78+
verify(workflowRepository, never()).existsProject(any())(any())
79+
verify(workflowRepository, never()).existsWorkflowWithPrefix(any())(any())
80+
succeed
81+
}
8382
}
8483

8584
it should "succeed if minElapsedSeconds is > 0" in {
@@ -91,10 +90,10 @@ class NotificationRuleValidationServiceTest extends AsyncFlatSpec with Matchers
9190
.thenReturn(Future(true))
9291

9392
// when
94-
await(underTest.validate(notificationRule))
95-
96-
// then
97-
succeed
93+
underTest.validate(notificationRule).map { _ =>
94+
// then
95+
succeed
96+
}
9897
}
9998

10099
it should "fail if the project doesn't exist" in {
@@ -106,11 +105,12 @@ class NotificationRuleValidationServiceTest extends AsyncFlatSpec with Matchers
106105
.thenReturn(Future(true))
107106

108107
// when
109-
val result = the[ApiException] thrownBy await(underTest.validate(notificationRule))
110-
111-
// then
112-
result.apiErrors should have size 1
113-
result.apiErrors.head shouldBe ValidationError(s"No project with name ${notificationRule.project.get} exists")
108+
underTest.validate(notificationRule).failed.map { error =>
109+
// then
110+
val result = error.asInstanceOf[ApiException]
111+
result.apiErrors should have size 1
112+
result.apiErrors.head shouldBe ValidationError(s"No project with name ${notificationRule.project.get} exists")
113+
}
114114
}
115115

116116
it should "fail if the workflow prefix doesn't match any workflows" in {
@@ -122,13 +122,14 @@ class NotificationRuleValidationServiceTest extends AsyncFlatSpec with Matchers
122122
.thenReturn(Future(false))
123123

124124
// when
125-
val result = the[ApiException] thrownBy await(underTest.validate(notificationRule))
126-
127-
// then
128-
result.apiErrors should have size 1
129-
result.apiErrors.head shouldBe ValidationError(
130-
s"No workflow with prefix ${notificationRule.workflowPrefix.get} exists"
131-
)
125+
underTest.validate(notificationRule).failed.map { error =>
126+
val result = error.asInstanceOf[ApiException]
127+
// then
128+
result.apiErrors should have size 1
129+
result.apiErrors.head shouldBe ValidationError(
130+
s"No workflow with prefix ${notificationRule.workflowPrefix.get} exists"
131+
)
132+
}
132133
}
133134

134135
it should "fail if any email address is invalid" in {
@@ -140,14 +141,15 @@ class NotificationRuleValidationServiceTest extends AsyncFlatSpec with Matchers
140141
.thenReturn(Future(true))
141142

142143
// when
143-
val result = the[ApiException] thrownBy await(underTest.validate(notificationRule))
144-
145-
// then
146-
result.apiErrors should have size 2
147-
result.apiErrors should contain theSameElementsAs Seq(
148-
ValidationError(s"Recipient abc@com is not a valid e-mail address"),
149-
ValidationError(s"Recipient abc.def.ghi is not a valid e-mail address")
150-
)
144+
underTest.validate(notificationRule).failed.map { error =>
145+
val result = error.asInstanceOf[ApiException]
146+
// then
147+
result.apiErrors should have size 2
148+
result.apiErrors should contain theSameElementsAs Seq(
149+
ValidationError(s"Recipient abc@com is not a valid e-mail address"),
150+
ValidationError(s"Recipient abc.def.ghi is not a valid e-mail address")
151+
)
152+
}
151153
}
152154

153155
it should "fail if minElapsedSeconds is < 0" in {
@@ -159,11 +161,12 @@ class NotificationRuleValidationServiceTest extends AsyncFlatSpec with Matchers
159161
.thenReturn(Future(true))
160162

161163
// when
162-
val result = the[ApiException] thrownBy await(underTest.validate(notificationRule))
163-
164-
// then
165-
result.apiErrors should have size 1
166-
result.apiErrors.head.message shouldBe "Min elapsed seconds since last success cannot be negative, is -1"
164+
underTest.validate(notificationRule).failed.map { error =>
165+
val result = error.asInstanceOf[ApiException]
166+
// then
167+
result.apiErrors should have size 1
168+
result.apiErrors.head.message shouldBe "Min elapsed seconds since last success cannot be negative, is -1"
169+
}
167170
}
168171

169172
it should "return all validation errors" in {
@@ -175,15 +178,16 @@ class NotificationRuleValidationServiceTest extends AsyncFlatSpec with Matchers
175178
.thenReturn(Future(false))
176179

177180
// when
178-
val result = the[ApiException] thrownBy await(underTest.validate(notificationRule))
179-
180-
// then
181-
result.apiErrors should have size 3
182-
result.apiErrors should contain theSameElementsAs Seq(
183-
ValidationError(s"No workflow with prefix ${notificationRule.workflowPrefix.get} exists"),
184-
ValidationError(s"No project with name ${notificationRule.project.get} exists"),
185-
ValidationError(s"Recipient abc@com is not a valid e-mail address")
186-
)
181+
underTest.validate(notificationRule).failed.map { error =>
182+
val result = error.asInstanceOf[ApiException]
183+
// then
184+
result.apiErrors should have size 3
185+
result.apiErrors should contain theSameElementsAs Seq(
186+
ValidationError(s"No workflow with prefix ${notificationRule.workflowPrefix.get} exists"),
187+
ValidationError(s"No project with name ${notificationRule.project.get} exists"),
188+
ValidationError(s"Recipient abc@com is not a valid e-mail address")
189+
)
190+
}
187191
}
188192

189193
private def createNotificationRule() =

0 commit comments

Comments
 (0)