44import static org .junit .jupiter .api .Assertions .assertNotNull ;
55import static org .junit .jupiter .api .Assertions .assertNull ;
66import static org .junit .jupiter .api .Assertions .assertTrue ;
7+ import static org .mockito .Mockito .mock ;
78import static org .mockito .Mockito .verify ;
89import static org .mockito .Mockito .verifyNoInteractions ;
10+ import static org .mockito .ArgumentMatchers .any ;
11+ import static org .mockito .ArgumentMatchers .eq ;
912import static org .mockito .Mockito .when ;
1013
1114import com .epam .reportportal .base .core .tms .dto .TmsTestCaseExecutionCommentRQ ;
1215import com .epam .reportportal .base .core .tms .dto .TmsTestCaseExecutionCommentRS ;
16+ import com .epam .reportportal .base .core .tms .dto .TmsTestCaseExecutionRQ ;
17+ import com .epam .reportportal .base .core .tms .dto .TmsTestCaseExecutionRS ;
18+ import com .epam .reportportal .base .core .tms .mapper .TmsTestCaseExecutionMapper ;
19+ import com .epam .reportportal .base .core .item .UpdateTestItemHandler ;
20+ import com .epam .reportportal .base .infrastructure .persistence .commons .ReportPortalUser ;
21+ import com .epam .reportportal .base .infrastructure .persistence .dao .TestItemRepository ;
22+ import com .epam .reportportal .base .infrastructure .persistence .entity .enums .StatusEnum ;
23+ import com .epam .reportportal .base .infrastructure .persistence .entity .item .TestItemResults ;
24+ import com .epam .reportportal .base .infrastructure .persistence .entity .organization .MembershipDetails ;
25+ import com .epam .reportportal .base .model .item .UpdateTestItemRQ ;
1326import com .epam .reportportal .base .infrastructure .rules .exception .ReportPortalException ;
1427import com .epam .reportportal .base .infrastructure .persistence .dao .tms .TmsTestCaseExecutionRepository ;
1528import com .epam .reportportal .base .infrastructure .persistence .entity .item .TestItem ;
@@ -34,6 +47,21 @@ class TmsTestCaseExecutionServiceImplTest {
3447 @ Mock
3548 private TmsTestCaseExecutionCommentService tmsTestCaseExecutionCommentService ;
3649
50+ @ Mock
51+ private UpdateTestItemHandler updateTestItemHandler ;
52+
53+ @ Mock
54+ private TmsTestCaseExecutionMapper tmsTestCaseExecutionMapper ;
55+
56+ @ Mock
57+ private TestItemRepository testItemRepository ;
58+
59+ @ Mock
60+ private TmsManualLaunchService tmsManualLaunchService ;
61+
62+ @ Mock
63+ private TmsTestCaseService tmsTestCaseService ;
64+
3765 @ InjectMocks
3866 private TmsTestCaseExecutionServiceImpl sut ;
3967
@@ -59,6 +87,9 @@ void setUp() {
5987 testItem1 = new TestItem ();
6088 testItem1 .setItemId (10L );
6189 testItem1 .setName ("Test Item 1" );
90+ TestItemResults results1 = new TestItemResults ();
91+ results1 .setStatus (StatusEnum .TO_RUN );
92+ testItem1 .setItemResults (results1 );
6293
6394 testItem2 = new TestItem ();
6495 testItem2 .setItemId (20L );
@@ -89,9 +120,63 @@ void setUp() {
89120 .testCaseId (testCaseId3 )
90121 .testCaseVersionId (3L )
91122 .testItem (testItem3 )
92- .testCaseSnapshot ("{\" name\" : \" Test Case 3\" }" )
93- .build ();
123+ .testCaseSnapshot ("{\" name\" : \" Test Case 3\" }" )
124+ .build ();
125+
126+ sut .setTmsManualLaunchService (tmsManualLaunchService );
127+ sut .setTmsTestCaseService (tmsTestCaseService );
94128 }
129+
130+ @ Test
131+ void patch_WhenStatusIsInProgress_ShouldBypassUpdateTestItemHandler () {
132+ // Given
133+ Long executionId = 100L ;
134+ Long launchId = 10L ;
135+ TmsTestCaseExecutionRQ request = TmsTestCaseExecutionRQ .builder ().status ("IN_PROGRESS" ).build ();
136+ MembershipDetails membershipDetails = new MembershipDetails ();
137+ ReportPortalUser user = mock (ReportPortalUser .class );
138+
139+ when (tmsTestCaseExecutionRepository .findByTestCaseExecutionIdAndLaunchId (executionId , launchId ))
140+ .thenReturn (Optional .of (execution1 ));
141+ when (testItemRepository .save (any (TestItem .class ))).thenReturn (testItem1 );
142+ when (tmsTestCaseExecutionRepository .save (execution1 )).thenReturn (execution1 );
143+ when (tmsTestCaseExecutionMapper .convert (execution1 )).thenReturn (new TmsTestCaseExecutionRS ());
144+
145+ // When
146+ var result = sut .patch (membershipDetails , user , executionId , launchId , request );
147+
148+ // Then
149+ assertNotNull (result );
150+ assertEquals (StatusEnum .IN_PROGRESS , testItem1 .getItemResults ().getStatus ());
151+ verify (testItemRepository ).save (testItem1 );
152+ verifyNoInteractions (updateTestItemHandler );
153+ }
154+
155+ @ Test
156+ void patch_WhenStatusIsFailed_ShouldCallUpdateTestItemHandler () {
157+ // Given
158+ Long executionId = 100L ;
159+ Long launchId = 10L ;
160+ TmsTestCaseExecutionRQ request = TmsTestCaseExecutionRQ .builder ().status ("FAILED" ).build ();
161+ MembershipDetails membershipDetails = new MembershipDetails ();
162+ ReportPortalUser user = mock (ReportPortalUser .class );
163+
164+ when (tmsTestCaseExecutionRepository .findByTestCaseExecutionIdAndLaunchId (executionId , launchId ))
165+ .thenReturn (Optional .of (execution1 ));
166+ when (updateTestItemHandler .updateTestItem (eq (membershipDetails ), eq (testItem1 ), any (UpdateTestItemRQ .class ), eq (user )))
167+ .thenReturn (testItem1 );
168+ when (tmsTestCaseExecutionRepository .save (execution1 )).thenReturn (execution1 );
169+ when (tmsTestCaseExecutionMapper .convert (execution1 )).thenReturn (new TmsTestCaseExecutionRS ());
170+
171+ // When
172+ var result = sut .patch (membershipDetails , user , executionId , launchId , request );
173+
174+ // Then
175+ assertNotNull (result );
176+ verify (updateTestItemHandler ).updateTestItem (eq (membershipDetails ), eq (testItem1 ), any (UpdateTestItemRQ .class ), eq (user ));
177+ }
178+
179+
95180
96181 @ Test
97182 void getLastTestCasesExecutionsByTestCaseIds_WithMultipleExecutions_ShouldReturnMapWithAllExecutions () {
0 commit comments