Skip to content

Commit f4ba9d2

Browse files
committed
test: add new unit tests
1 parent bf28c98 commit f4ba9d2

2 files changed

Lines changed: 106 additions & 0 deletions

File tree

src/test/java/com/itasocialacademy/oitassist/taskassignment/controller/AssignmentControllerTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package com.itasocialacademy.oitassist.taskassignment.controller;
22

33
import com.itasocialacademy.oitassist.ControllerUnitTest;
4+
import com.itasocialacademy.oitassist.competition.dao.enums.ExecutionStatus;
45
import com.itasocialacademy.oitassist.competition.exceptions.CompetitionHierarchyValidationException;
56
import com.itasocialacademy.oitassist.competition.exceptions.TourNotFoundException;
67
import com.itasocialacademy.oitassist.filemanager.api.dto.FileDetailsDTO;
8+
import com.itasocialacademy.oitassist.task.exceptions.TaskNotFoundException;
79
import com.itasocialacademy.oitassist.taskassignment.dao.enums.AssignmentVisibility;
810
import com.itasocialacademy.oitassist.taskassignment.dao.model.TaskRequirements;
911
import com.itasocialacademy.oitassist.taskassignment.dto.request.CreateAndAssignTaskRequestDTO;
1012
import com.itasocialacademy.oitassist.taskassignment.dto.request.CreateTaskAssignmentRequestDTO;
1113
import com.itasocialacademy.oitassist.taskassignment.dto.request.TaskRequirementsRequestDTO;
1214
import com.itasocialacademy.oitassist.taskassignment.dto.request.UpdateTaskAssignmentRequestDTO;
1315
import com.itasocialacademy.oitassist.taskassignment.dto.response.DetailedTaskAssignmentResponseDTO;
16+
import com.itasocialacademy.oitassist.taskassignment.dto.response.LinkedToursResponseDTO;
1417
import com.itasocialacademy.oitassist.taskassignment.dto.response.TaskAssignmentResponseDTO;
1518
import com.itasocialacademy.oitassist.taskassignment.exceptions.TaskAlreadyAssignedException;
1619
import com.itasocialacademy.oitassist.taskassignment.exceptions.TaskAssignmentNotFoundException;
@@ -274,6 +277,50 @@ void getByTour_tourNotFound_shouldReturn404() throws Exception {
274277
.andExpect(status().isNotFound());
275278
}
276279

280+
// GET /api/v1/tasks/{taskId}/linked-tours — getLinkedTours
281+
282+
@Test
283+
void getLinkedTours_shouldReturnListAnd200() throws Exception {
284+
LinkedToursResponseDTO mockTourResponse = LinkedToursResponseDTO.builder()
285+
.tourId(10L)
286+
.title("Tour 1")
287+
.description("Description")
288+
.location("Location")
289+
.executionStatus(ExecutionStatus.SCHEDULED)
290+
.build();
291+
292+
when(assignmentService.getLinkedToursByTaskId(3L))
293+
.thenReturn(List.of(mockTourResponse));
294+
295+
mockMvc.perform(get("/api/v1/tasks/{taskId}/linked-tours", 3L))
296+
.andExpect(status().isOk())
297+
.andExpect(jsonPath("$").isArray())
298+
.andExpect(jsonPath("$[0].tourId").value(10L))
299+
.andExpect(jsonPath("$[0].title").value("Tour 1"))
300+
.andExpect(jsonPath("$[0].description").value("Description"))
301+
.andExpect(jsonPath("$[0].location").value("Location"))
302+
.andExpect(jsonPath("$[0].executionStatus").value("SCHEDULED"));
303+
}
304+
305+
@Test
306+
void getLinkedTours_taskNotFound_shouldReturn404() throws Exception {
307+
when(assignmentService.getLinkedToursByTaskId(99L))
308+
.thenThrow(new TaskNotFoundException(99L));
309+
310+
mockMvc.perform(get("/api/v1/tasks/{taskId}/linked-tours", 99L))
311+
.andExpect(status().isNotFound());
312+
}
313+
314+
@Test
315+
void getLinkedTours_tourNotFound_shouldReturn404() throws Exception {
316+
when(assignmentService.getLinkedToursByTaskId(3L))
317+
.thenThrow(new TourNotFoundException(10L));
318+
319+
mockMvc.perform(get("/api/v1/tasks/{taskId}/linked-tours", 3L))
320+
.andExpect(status().isNotFound());
321+
}
322+
323+
277324
// PATCH /api/v1/task-assignments/{assignmentId} — update
278325

279326
@Test

src/test/java/com/itasocialacademy/oitassist/taskassignment/service/AssignmentServiceTest.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.Set;
44
import com.itasocialacademy.oitassist.competition.dao.enums.ExecutionStatus;
55
import com.itasocialacademy.oitassist.filemanager.dao.enums.FileRole;
6+
import com.itasocialacademy.oitassist.taskassignment.dto.response.LinkedToursResponseDTO;
67
import org.junit.jupiter.api.BeforeEach;
78
import org.junit.jupiter.api.Test;
89
import org.junit.jupiter.api.extension.ExtendWith;
@@ -564,4 +565,62 @@ void existsByTaskBodyId_whenNotExists_shouldReturnFalse() {
564565

565566
assertFalse(assignmentService.existsByTaskBodyId(99L));
566567
}
568+
569+
// ---- getLinkedToursByTaskId ----
570+
571+
@Test
572+
void getLinkedToursByTaskId_whenTaskExistsAndUserAuthorized_shouldReturnLinkedTours() {
573+
LinkedToursResponseDTO mockTourResponse = LinkedToursResponseDTO.builder()
574+
.tourId(10L)
575+
.title("Tour 1")
576+
.description("Description")
577+
.location("Location")
578+
.executionStatus(ExecutionStatus.SCHEDULED)
579+
.build();
580+
581+
when(taskBodyFacade.findTaskBodyById(3L)).thenReturn(Optional.of(taskBodyDetail));
582+
when(securityFacade.hasRole("ADMIN")).thenReturn(true);
583+
when(taskAssignmentRepository.findTourIdsByTaskBodyId(3L)).thenReturn(List.of(10L));
584+
when(competitionFacade.findTourById(10L)).thenReturn(Optional.of(tourDetail));
585+
when(taskAssignmentMapper.toLinkedToursResponse(tourDetail)).thenReturn(mockTourResponse);
586+
587+
List<LinkedToursResponseDTO> result = assignmentService.getLinkedToursByTaskId(3L);
588+
589+
assertNotNull(result);
590+
assertEquals(1, result.size());
591+
assertEquals(10L, result.getFirst().tourId());
592+
assertEquals("Tour 1", result.getFirst().title());
593+
assertEquals("Description", result.getFirst().description());
594+
assertEquals("Location", result.getFirst().location());
595+
assertEquals(ExecutionStatus.SCHEDULED, result.getFirst().executionStatus());
596+
}
597+
598+
@Test
599+
void getLinkedToursByTaskId_whenTaskDoesNotExist_shouldThrowTaskNotFoundException() {
600+
when(taskBodyFacade.findTaskBodyById(99L)).thenReturn(Optional.empty());
601+
602+
assertThrows(TaskNotFoundException.class, () -> assignmentService.getLinkedToursByTaskId(99L));
603+
verify(taskAssignmentRepository, never()).findTourIdsByTaskBodyId(any());
604+
}
605+
606+
@Test
607+
void getLinkedToursByTaskId_whenUserNotAuthorized_shouldThrowAuthorizationException() {
608+
when(taskBodyFacade.findTaskBodyById(3L)).thenReturn(Optional.of(taskBodyDetail));
609+
when(securityFacade.hasRole("ADMIN")).thenReturn(false);
610+
when(securityFacade.hasRole("ORG")).thenReturn(false);
611+
612+
assertThrows(com.itasocialacademy.oitassist.core.exceptions.AuthorizationException.class,
613+
() -> assignmentService.getLinkedToursByTaskId(3L));
614+
verify(taskAssignmentRepository, never()).findTourIdsByTaskBodyId(any());
615+
}
616+
617+
@Test
618+
void getLinkedToursByTaskId_whenLinkedTourNotFound_shouldThrowTourNotFoundException() {
619+
when(taskBodyFacade.findTaskBodyById(3L)).thenReturn(Optional.of(taskBodyDetail));
620+
when(securityFacade.hasRole("ADMIN")).thenReturn(true);
621+
when(taskAssignmentRepository.findTourIdsByTaskBodyId(3L)).thenReturn(List.of(10L));
622+
when(competitionFacade.findTourById(10L)).thenReturn(Optional.empty());
623+
624+
assertThrows(TourNotFoundException.class, () -> assignmentService.getLinkedToursByTaskId(3L));
625+
}
567626
}

0 commit comments

Comments
 (0)