4
4
import ch .puzzle .okr .dto .ErrorDto ;
5
5
import ch .puzzle .okr .exception .OkrResponseStatusException ;
6
6
import ch .puzzle .okr .models .Objective ;
7
+ import ch .puzzle .okr .models .Team ;
7
8
import ch .puzzle .okr .models .alignment .KeyResultAlignment ;
8
9
import ch .puzzle .okr .models .alignment .ObjectiveAlignment ;
9
10
import ch .puzzle .okr .models .keyresult .KeyResult ;
10
11
import ch .puzzle .okr .models .keyresult .KeyResultMetric ;
11
12
import ch .puzzle .okr .service .persistence .AlignmentPersistenceService ;
13
+ import ch .puzzle .okr .service .persistence .TeamPersistenceService ;
12
14
import org .junit .jupiter .api .BeforeEach ;
13
15
import org .junit .jupiter .api .Test ;
14
16
import org .junit .jupiter .api .extension .ExtendWith ;
@@ -31,12 +33,18 @@ class AlignmentValidationServiceTest {
31
33
32
34
@ Mock
33
35
AlignmentPersistenceService alignmentPersistenceService ;
36
+ @ Mock
37
+ TeamPersistenceService teamPersistenceService ;
34
38
@ Spy
35
39
@ InjectMocks
36
40
private AlignmentValidationService validator ;
37
41
38
- Objective objective1 = Objective .Builder .builder ().withId (5L ).withTitle ("Objective 1" ).withState (DRAFT ).build ();
39
- Objective objective2 = Objective .Builder .builder ().withId (8L ).withTitle ("Objective 2" ).withState (DRAFT ).build ();
42
+ Team team1 = Team .Builder .builder ().withId (1L ).withName ("Puzzle ITC" ).build ();
43
+ Team team2 = Team .Builder .builder ().withId (2L ).withName ("BBT" ).build ();
44
+ Objective objective1 = Objective .Builder .builder ().withId (5L ).withTitle ("Objective 1" ).withTeam (team1 )
45
+ .withState (DRAFT ).build ();
46
+ Objective objective2 = Objective .Builder .builder ().withId (8L ).withTitle ("Objective 2" ).withTeam (team2 )
47
+ .withState (DRAFT ).build ();
40
48
Objective objective3 = Objective .Builder .builder ().withId (10L ).withTitle ("Objective 3" ).withState (DRAFT ).build ();
41
49
KeyResult metricKeyResult = KeyResultMetric .Builder .builder ().withId (5L ).withTitle ("KR Title 1" ).build ();
42
50
ObjectiveAlignment objectiveALignment = ObjectiveAlignment .Builder .builder ().withId (1L )
@@ -74,6 +82,8 @@ void validateOnGetShouldThrowExceptionIfIdIsNull() {
74
82
@ Test
75
83
void validateOnCreateShouldBeSuccessfulWhenAlignmentIsValid () {
76
84
when (alignmentPersistenceService .findByAlignedObjectiveId (anyLong ())).thenReturn (null );
85
+ when (teamPersistenceService .findById (1L )).thenReturn (team1 );
86
+ when (teamPersistenceService .findById (2L )).thenReturn (team2 );
77
87
78
88
validator .validateOnCreate (createAlignment );
79
89
@@ -165,9 +175,45 @@ void validateOnCreateShouldThrowExceptionWhenAlignedIdIsSameAsTargetId() {
165
175
assertTrue (TestHelper .getAllErrorKeys (expectedErrors ).contains (exception .getReason ()));
166
176
}
167
177
178
+ @ Test
179
+ void validateOnCreateShouldThrowExceptionWhenAlignmentIsInSameTeamObjective () {
180
+ when (teamPersistenceService .findById (2L )).thenReturn (team2 );
181
+ Objective objective = objective1 ;
182
+ objective .setTeam (team2 );
183
+ ObjectiveAlignment objectiveAlignment = ObjectiveAlignment .Builder .builder ().withAlignedObjective (objective )
184
+ .withTargetObjective (objective2 ).build ();
185
+
186
+ OkrResponseStatusException exception = assertThrows (OkrResponseStatusException .class ,
187
+ () -> validator .validateOnCreate (objectiveAlignment ));
188
+
189
+ List <ErrorDto > expectedErrors = List .of (new ErrorDto ("NOT_LINK_IN_SAME_TEAM" , List .of ("teamId" , "2" )));
190
+
191
+ assertEquals (BAD_REQUEST , exception .getStatusCode ());
192
+ assertThat (expectedErrors ).hasSameElementsAs (exception .getErrors ());
193
+ assertTrue (TestHelper .getAllErrorKeys (expectedErrors ).contains (exception .getReason ()));
194
+ }
195
+
196
+ @ Test
197
+ void validateOnCreateShouldThrowExceptionWhenAlignmentIsInSameTeamKeyResult () {
198
+ when (teamPersistenceService .findById (1L )).thenReturn (team1 );
199
+ KeyResult keyResult = KeyResultMetric .Builder .builder ().withId (3L ).withTitle ("KeyResult 1" ).withObjective (objective1 ).build ();
200
+ KeyResultAlignment keyResultAlignment1 = KeyResultAlignment .Builder .builder ().withAlignedObjective (objective1 ).withTargetKeyResult (keyResult ).build ();
201
+
202
+ OkrResponseStatusException exception = assertThrows (OkrResponseStatusException .class ,
203
+ () -> validator .validateOnCreate (keyResultAlignment1 ));
204
+
205
+ List <ErrorDto > expectedErrors = List .of (new ErrorDto ("NOT_LINK_IN_SAME_TEAM" , List .of ("teamId" , "1" )));
206
+
207
+ assertEquals (BAD_REQUEST , exception .getStatusCode ());
208
+ assertThat (expectedErrors ).hasSameElementsAs (exception .getErrors ());
209
+ assertTrue (TestHelper .getAllErrorKeys (expectedErrors ).contains (exception .getReason ()));
210
+ }
211
+
168
212
@ Test
169
213
void validateOnCreateShouldThrowExceptionWhenAlignedObjectiveAlreadyExists () {
170
214
when (alignmentPersistenceService .findByAlignedObjectiveId (anyLong ())).thenReturn (objectiveALignment );
215
+ when (teamPersistenceService .findById (1L )).thenReturn (team1 );
216
+ when (teamPersistenceService .findById (2L )).thenReturn (team2 );
171
217
172
218
ObjectiveAlignment createAlignment = ObjectiveAlignment .Builder .builder ().withAlignedObjective (objective1 ).withTargetObjective (objective2 ).build ();
173
219
@@ -183,6 +229,9 @@ void validateOnCreateShouldThrowExceptionWhenAlignedObjectiveAlreadyExists() {
183
229
184
230
@ Test
185
231
void validateOnUpdateShouldBeSuccessfulWhenAlignmentIsValid () {
232
+ when (teamPersistenceService .findById (1L )).thenReturn (team1 );
233
+ when (teamPersistenceService .findById (2L )).thenReturn (team2 );
234
+
186
235
validator .validateOnUpdate (objectiveALignment .getId (), objectiveALignment );
187
236
188
237
verify (validator , times (1 )).throwExceptionWhenModelIsNull (objectiveALignment );
@@ -274,6 +323,40 @@ void validateOnUpdateShouldThrowExceptionWhenAlignedIdIsSameAsTargetId() {
274
323
assertTrue (TestHelper .getAllErrorKeys (expectedErrors ).contains (exception .getReason ()));
275
324
}
276
325
326
+ @ Test
327
+ void validateOnUpdateShouldThrowExceptionWhenAlignmentIsInSameTeamObjective () {
328
+ when (teamPersistenceService .findById (2L )).thenReturn (team2 );
329
+ Objective objective = objective1 ;
330
+ objective .setTeam (team2 );
331
+ ObjectiveAlignment objectiveAlignment = ObjectiveAlignment .Builder .builder ().withId (3L )
332
+ .withAlignedObjective (objective ).withTargetObjective (objective2 ).build ();
333
+
334
+ OkrResponseStatusException exception = assertThrows (OkrResponseStatusException .class ,
335
+ () -> validator .validateOnUpdate (2L , objectiveAlignment ));
336
+
337
+ List <ErrorDto > expectedErrors = List .of (new ErrorDto ("NOT_LINK_IN_SAME_TEAM" , List .of ("teamId" , "2" )));
338
+
339
+ assertEquals (BAD_REQUEST , exception .getStatusCode ());
340
+ assertThat (expectedErrors ).hasSameElementsAs (exception .getErrors ());
341
+ assertTrue (TestHelper .getAllErrorKeys (expectedErrors ).contains (exception .getReason ()));
342
+ }
343
+
344
+ @ Test
345
+ void validateOnUpdateShouldThrowExceptionWhenAlignmentIsInSameTeamKeyResult () {
346
+ when (teamPersistenceService .findById (1L )).thenReturn (team1 );
347
+ KeyResult keyResult = KeyResultMetric .Builder .builder ().withId (3L ).withTitle ("KeyResult 1" ).withObjective (objective1 ).build ();
348
+ KeyResultAlignment keyResultAlignment1 = KeyResultAlignment .Builder .builder ().withId (2L ).withAlignedObjective (objective1 ).withTargetKeyResult (keyResult ).build ();
349
+
350
+ OkrResponseStatusException exception = assertThrows (OkrResponseStatusException .class ,
351
+ () -> validator .validateOnUpdate (2L , keyResultAlignment1 ));
352
+
353
+ List <ErrorDto > expectedErrors = List .of (new ErrorDto ("NOT_LINK_IN_SAME_TEAM" , List .of ("teamId" , "1" )));
354
+
355
+ assertEquals (BAD_REQUEST , exception .getStatusCode ());
356
+ assertThat (expectedErrors ).hasSameElementsAs (exception .getErrors ());
357
+ assertTrue (TestHelper .getAllErrorKeys (expectedErrors ).contains (exception .getReason ()));
358
+ }
359
+
277
360
@ Test
278
361
void validateOnDeleteShouldBeSuccessfulWhenValidAlignmentId () {
279
362
validator .validateOnDelete (3L );
0 commit comments