22
33import ca .bc .gov .nrs .wfprev .controllers .ActivityBoundaryController ;
44import ca .bc .gov .nrs .wfprev .data .models .ActivityBoundaryModel ;
5+ import ca .bc .gov .nrs .wfprev .data .models .ActivityModel ;
56import ca .bc .gov .nrs .wfprev .services .ActivityBoundaryService ;
7+ import ca .bc .gov .nrs .wfprev .services .ActivityService ;
68import ca .bc .gov .nrs .wfprev .services .CoordinatesService ;
79import com .nimbusds .jose .shaded .gson .Gson ;
810import com .nimbusds .jose .shaded .gson .GsonBuilder ;
1719import org .locationtech .jts .geom .LinearRing ;
1820import org .locationtech .jts .geom .MultiPolygon ;
1921import org .locationtech .jts .geom .Polygon ;
22+ import org .mockito .ArgumentCaptor ;
2023import org .springframework .beans .factory .annotation .Autowired ;
2124import org .springframework .boot .test .autoconfigure .web .servlet .WebMvcTest ;
2225import org .springframework .boot .test .mock .mockito .MockBean ;
3538import java .util .List ;
3639import java .util .UUID ;
3740
41+ import static org .junit .jupiter .api .Assertions .assertFalse ;
42+ import static org .junit .jupiter .api .Assertions .assertTrue ;
3843import static org .mockito .ArgumentMatchers .any ;
3944import static org .mockito .ArgumentMatchers .anyString ;
45+ import static org .mockito .ArgumentMatchers .argThat ;
4046import static org .mockito .ArgumentMatchers .eq ;
4147import static org .mockito .Mockito .doNothing ;
4248import static org .mockito .Mockito .verify ;
@@ -59,6 +65,9 @@ class ActivityBoundaryControllerTest {
5965 @ MockBean
6066 private CoordinatesService coordinatesService ;
6167
68+ @ MockBean
69+ private ActivityService activityService ;
70+
6271 @ Autowired
6372 private MockMvc mockMvc ;
6473
@@ -200,10 +209,15 @@ void testGetActivityBoundary_EntityNotFoundException() throws Exception {
200209 @ WithMockUser
201210 void testCreateActivityBoundary_Success () throws Exception {
202211 ActivityBoundaryModel requestModel = buildActivityBoundaryRequestModel ();
212+ ActivityModel activity = new ActivityModel ();
213+ activity .setActivityGuid (requestModel .getActivityGuid ());
203214
204215 when (activityBoundaryService .createActivityBoundary (anyString (), anyString (), anyString (), any (ActivityBoundaryModel .class )))
205216 .thenReturn (requestModel );
206217
218+ when (activityService .getActivity (anyString (), anyString (), anyString ()))
219+ .thenReturn (activity );
220+
207221 String requestJson = activityBoundaryJson .formatted (
208222 requestModel .getActivityBoundaryGuid (),
209223 requestModel .getActivityGuid (),
@@ -218,8 +232,17 @@ void testCreateActivityBoundary_Success() throws Exception {
218232 .content (requestJson ))
219233 .andExpect (status ().isCreated ())
220234 .andExpect (jsonPath ("$.activityBoundaryGuid" ).value (requestModel .getActivityBoundaryGuid ()));
221- }
222235
236+ // Verify calls to activityService
237+ verify (activityService ).getActivity (anyString (), anyString (), anyString ());
238+
239+ verify (activityService ).updateActivity (
240+ anyString (),
241+ anyString (),
242+ argThat (updatedActivity ->
243+ updatedActivity .getIsSpatialAddedInd () != null && updatedActivity .getIsSpatialAddedInd ())
244+ );
245+ }
223246 @ Test
224247 @ WithMockUser
225248 void testCreateActivityBoundary_DataIntegrityViolationException () throws Exception {
@@ -293,9 +316,14 @@ void testCreateActivityBoundary_IllegalArgumentException() throws Exception {
293316 @ WithMockUser
294317 void testUpdateActivityBoundary_Success () throws Exception {
295318 ActivityBoundaryModel requestModel = buildActivityBoundaryRequestModel ();
319+ ActivityModel activity = new ActivityModel ();
320+ activity .setActivityGuid (requestModel .getActivityGuid ());
321+ activity .setIsSpatialAddedInd (false );
296322
297323 when (activityBoundaryService .updateActivityBoundary (anyString (), anyString (), anyString (), any (ActivityBoundaryModel .class )))
298324 .thenReturn (requestModel );
325+ when (activityService .getActivity (anyString (), anyString (), anyString ()))
326+ .thenReturn (activity );
299327
300328 String requestJson = activityBoundaryJson .formatted (
301329 requestModel .getActivityBoundaryGuid (),
@@ -311,6 +339,13 @@ void testUpdateActivityBoundary_Success() throws Exception {
311339 .content (requestJson ))
312340 .andExpect (status ().isOk ())
313341 .andExpect (jsonPath ("$.activityBoundaryGuid" ).value (requestModel .getActivityBoundaryGuid ()));
342+
343+ // Verify spatial indicator update
344+ verify (activityService ).getActivity (anyString (), anyString (), anyString ());
345+ ArgumentCaptor <ActivityModel > activityCaptor = ArgumentCaptor .forClass (ActivityModel .class );
346+ verify (activityService ).updateActivity (anyString (), anyString (), activityCaptor .capture ());
347+
348+ assertTrue (activityCaptor .getValue ().getIsSpatialAddedInd (), "isSpatialAddedInd should be true after update" );
314349 }
315350
316351 @ Test
@@ -410,14 +445,44 @@ void testUpdateActivityBoundary_DataIntegrityViolationException() throws Excepti
410445 @ WithMockUser
411446 void testDeleteActivityBoundary_Success () throws Exception {
412447 UUID boundaryId = UUID .randomUUID ();
413- doNothing ().when (activityBoundaryService ).deleteActivityBoundary (anyString (), anyString (), anyString (), anyString ());
448+ UUID projectId = UUID .randomUUID ();
449+ UUID fiscalId = UUID .randomUUID ();
450+ UUID activityId = UUID .randomUUID ();
451+
452+ ActivityModel updatedActivity = new ActivityModel ();
453+ updatedActivity .setIsSpatialAddedInd (false );
454+
455+ when (activityService .updateActivity (eq (projectId .toString ()), eq (fiscalId .toString ()), any (ActivityModel .class )))
456+ .thenReturn (updatedActivity );
457+
458+ doNothing ().when (activityBoundaryService ).deleteActivityBoundary (
459+ eq (projectId .toString ()), eq (fiscalId .toString ()), eq (activityId .toString ()), eq (boundaryId .toString ()));
460+
461+ when (activityService .getActivity (eq (projectId .toString ()), eq (fiscalId .toString ()), eq (activityId .toString ())))
462+ .thenReturn (new ActivityModel ());
463+
464+ when (activityBoundaryService .getAllActivityBoundaries (eq (projectId .toString ()), eq (fiscalId .toString ()), eq (activityId .toString ())))
465+ .thenReturn (CollectionModel .empty ());
466+
467+ doNothing ().when (coordinatesService ).updateProjectCoordinates (eq (projectId .toString ()));
468+
469+ when (activityService .updateActivity (eq (projectId .toString ()), eq (fiscalId .toString ()), any (ActivityModel .class )))
470+ .thenReturn (updatedActivity );
414471
415472 mockMvc .perform (delete ("/projects/{projectId}/projectFiscals/{projectFiscalId}/activities/{activityId}/activityBoundary/{id}" ,
416- UUID . randomUUID (), UUID . randomUUID (), UUID . randomUUID () , boundaryId )
473+ projectId , fiscalId , activityId , boundaryId )
417474 .header (HttpHeaders .AUTHORIZATION , "Bearer test-token" ))
418475 .andExpect (status ().isNoContent ());
419476
420- verify (activityBoundaryService ).deleteActivityBoundary (anyString (), anyString (), anyString (), eq (boundaryId .toString ()));
477+ verify (activityBoundaryService ).deleteActivityBoundary (eq (projectId .toString ()), eq (fiscalId .toString ()), eq (activityId .toString ()), eq (boundaryId .toString ()));
478+ verify (coordinatesService ).updateProjectCoordinates (eq (projectId .toString ()));
479+ verify (activityService ).getActivity (eq (projectId .toString ()), eq (fiscalId .toString ()), eq (activityId .toString ()));
480+ verify (activityBoundaryService ).getAllActivityBoundaries (eq (projectId .toString ()), eq (fiscalId .toString ()), eq (activityId .toString ()));
481+
482+ ArgumentCaptor <ActivityModel > activityCaptor = ArgumentCaptor .forClass (ActivityModel .class );
483+ verify (activityService ).updateActivity (eq (projectId .toString ()), eq (fiscalId .toString ()), activityCaptor .capture ());
484+
485+ assertFalse (activityCaptor .getValue ().getIsSpatialAddedInd (), "isSpatialAddedInd should be false after deleting last boundary" );
421486 }
422487
423488 ActivityBoundaryModel buildActivityBoundaryRequestModel () {
0 commit comments