diff --git a/pr-review.patch b/pr-review.patch new file mode 100644 index 00000000..08502669 Binary files /dev/null and b/pr-review.patch differ diff --git a/src/main/java/org/unilab/uniplan/roomcategory/RoomCategoryController.java b/src/main/java/org/unilab/uniplan/roomcategory/RoomCategoryController.java index 26661af8..2ccee76a 100644 --- a/src/main/java/org/unilab/uniplan/roomcategory/RoomCategoryController.java +++ b/src/main/java/org/unilab/uniplan/roomcategory/RoomCategoryController.java @@ -15,7 +15,6 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; -import org.unilab.uniplan.roomcategory.dto.RoomCategoryDto; import org.unilab.uniplan.roomcategory.dto.RoomCategoryRequestDto; import org.unilab.uniplan.roomcategory.dto.RoomCategoryResponseDto; @@ -25,39 +24,32 @@ @Tag(name = "Room-Category Assignments", description = "Manage the association of rooms with categories") public class RoomCategoryController { - private final RoomCategoryService roomCategoryService; - private final RoomCategoryMapper roomCategoryMapper; + private final RoomCategoryWebFacade roomCategoryWebFacade; @PostMapping("/add") - public ResponseEntity createRoomCategory( + public ResponseEntity createRoomCategory( @Valid @NotNull @RequestBody final RoomCategoryRequestDto roomCategoryRequestDto) { - RoomCategoryDto roomCategoryDto = roomCategoryService.createRoomCategory( - roomCategoryMapper.toInternalDto(roomCategoryRequestDto)); + roomCategoryWebFacade.createRoomCategory(roomCategoryRequestDto); - return new ResponseEntity<>(roomCategoryMapper.toResponseDto(roomCategoryDto), - HttpStatus.CREATED); + return ResponseEntity.status(HttpStatus.CREATED).build(); } @GetMapping("/getAll") - public List getAllRoomCategories() { - return roomCategoryMapper.toResponseDtoList(roomCategoryService.getAllRoomCategories()); + public ResponseEntity> getAllRoomCategories() { + return ResponseEntity.ok(roomCategoryWebFacade.getAllRoomCategories()); } @GetMapping("/getById") public ResponseEntity getRoomCategoryById(@RequestParam final UUID roomId, @RequestParam final UUID categoryId) { - RoomCategoryDto roomCategoryDto = roomCategoryService.getRoomCategoryById(roomId, - categoryId); - - return ResponseEntity.ok(roomCategoryMapper.toResponseDto(roomCategoryDto)); + return ResponseEntity.ok(roomCategoryWebFacade.getRoomCategoryById(roomId, categoryId)); } - @DeleteMapping("/delete") public ResponseEntity deleteRoomCategory(@RequestParam final UUID roomId, @RequestParam final UUID categoryId) { - roomCategoryService.deleteRoomCategory(roomId, categoryId); + roomCategoryWebFacade.deleteRoomCategory(roomId, categoryId); return ResponseEntity.noContent().build(); } } \ No newline at end of file diff --git a/src/main/java/org/unilab/uniplan/roomcategory/RoomCategoryMapper.java b/src/main/java/org/unilab/uniplan/roomcategory/RoomCategoryMapper.java index 875a7d4b..cd3908a0 100644 --- a/src/main/java/org/unilab/uniplan/roomcategory/RoomCategoryMapper.java +++ b/src/main/java/org/unilab/uniplan/roomcategory/RoomCategoryMapper.java @@ -4,7 +4,7 @@ import java.util.UUID; import org.mapstruct.Mapper; import org.mapstruct.Mapping; -import org.unilab.uniplan.roomcategory.dto.RoomCategoryDto; +import org.mapstruct.MappingTarget; import org.unilab.uniplan.roomcategory.dto.RoomCategoryRequestDto; import org.unilab.uniplan.roomcategory.dto.RoomCategoryResponseDto; @@ -14,19 +14,19 @@ public interface RoomCategoryMapper { @Mapping(target = "id", expression = "java(new RoomCategoryId(dto.roomId(), dto.categoryId()))") @Mapping(target = "room", ignore = true) @Mapping(target = "category", ignore = true) - RoomCategory toEntity(RoomCategoryDto dto); + RoomCategory toEntity(RoomCategoryRequestDto dto); @Mapping(source = "id.roomId", target = "roomId") @Mapping(source = "id.categoryId", target = "categoryId") - RoomCategoryDto toDto(RoomCategory entity); - - RoomCategoryDto toInternalDto(RoomCategoryRequestDto requestDto); + RoomCategoryResponseDto toResponseDto(final RoomCategory roomCategory); - RoomCategoryResponseDto toResponseDto(RoomCategoryDto roomCategoryDto); + List toResponseDtoList(List roomCategories); - List toDtoList(List entities); - - List toResponseDtoList(List roomCategories); + @Mapping(target = "id", expression = "java(new RoomCategoryId(requestDto.roomId(), requestDto.categoryId()))") + @Mapping(target = "room", ignore = true) + @Mapping(target = "category", ignore = true) + void updateEntity(final RoomCategoryRequestDto requestDto, + @MappingTarget final RoomCategory entity); RoomCategoryId toRoomCategoryId(UUID roomId, UUID categoryId); } diff --git a/src/main/java/org/unilab/uniplan/roomcategory/RoomCategoryService.java b/src/main/java/org/unilab/uniplan/roomcategory/RoomCategoryService.java index 3133878c..0fe8f2f1 100644 --- a/src/main/java/org/unilab/uniplan/roomcategory/RoomCategoryService.java +++ b/src/main/java/org/unilab/uniplan/roomcategory/RoomCategoryService.java @@ -1,52 +1,29 @@ package org.unilab.uniplan.roomcategory; -import static org.unilab.uniplan.utils.ErrorConstants.ROOM_CATEGORY_NOT_FOUND; - import java.util.List; -import java.util.UUID; +import java.util.Optional; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; -import org.unilab.uniplan.exception.ResourceNotFoundException; -import org.unilab.uniplan.roomcategory.dto.RoomCategoryDto; @Service @RequiredArgsConstructor public class RoomCategoryService { private final RoomCategoryRepository roomCategoryRepository; - private final RoomCategoryMapper roomCategoryMapper; - - @Transactional - public RoomCategoryDto createRoomCategory(final RoomCategoryDto roomCategoryDto) { - final RoomCategory roomCategory = roomCategoryMapper.toEntity(roomCategoryDto); - return roomCategoryMapper.toDto(roomCategoryRepository.save(roomCategory)); + public void save(final RoomCategory roomCategory) { + roomCategoryRepository.save(roomCategory); } - public List getAllRoomCategories() { - final List roomCategories = roomCategoryRepository.findAll(); - return roomCategoryMapper.toDtoList(roomCategories); + public List getAll() { + return roomCategoryRepository.findAll(); } - public RoomCategoryDto getRoomCategoryById(final UUID roomId, final UUID categoryId) { - final RoomCategoryId id = roomCategoryMapper.toRoomCategoryId(roomId, categoryId); - - return roomCategoryRepository.findById(id) - .map(roomCategoryMapper::toDto) - .orElseThrow(() -> new ResourceNotFoundException( - ROOM_CATEGORY_NOT_FOUND.getMessage(String.valueOf(id)))); + public Optional getById(final RoomCategoryId id) { + return roomCategoryRepository.findById(id); } - @Transactional - public void deleteRoomCategory(final UUID roomId, final UUID categoryId) { - final RoomCategoryId id = roomCategoryMapper.toRoomCategoryId(roomId, categoryId); - - final RoomCategory roomCategory = roomCategoryRepository.findById(id) - .orElseThrow(() -> new ResourceNotFoundException( - ROOM_CATEGORY_NOT_FOUND.getMessage( - String.valueOf(id)))); - + public void delete(RoomCategory roomCategory) { roomCategoryRepository.delete(roomCategory); } } diff --git a/src/main/java/org/unilab/uniplan/roomcategory/RoomCategoryValidator.java b/src/main/java/org/unilab/uniplan/roomcategory/RoomCategoryValidator.java new file mode 100644 index 00000000..dedf9905 --- /dev/null +++ b/src/main/java/org/unilab/uniplan/roomcategory/RoomCategoryValidator.java @@ -0,0 +1,47 @@ +package org.unilab.uniplan.roomcategory; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; +import org.unilab.uniplan.category.CategoryRepository; +import org.unilab.uniplan.exception.ResourceNotFoundException; +import org.unilab.uniplan.room.RoomRepository; +import org.unilab.uniplan.roomcategory.dto.RoomCategoryRequestDto; + +import java.util.UUID; + +import static org.unilab.uniplan.utils.ErrorConstants.CATEGORY_NOT_FOUND; +import static org.unilab.uniplan.utils.ErrorConstants.ROOM_NOT_FOUND; + +@Component +@RequiredArgsConstructor +public class RoomCategoryValidator { + private final CategoryRepository categoryRepository; + private final RoomRepository roomRepository; + + public void validateForCreate(final RoomCategoryRequestDto requestDto) { + validateCategoryExists(requestDto.categoryId()); + validateRoomExists(requestDto.roomId()); + } + + public void validateForUpdate(final RoomCategoryId id, final RoomCategoryRequestDto requestDto) { + validateCategoryExists(requestDto.categoryId()); + validateRoomExists(requestDto.roomId()); + } + + public void validateCategoryExists(final UUID categoryId) { + if (!categoryRepository.existsById(categoryId)) { + throw new ResourceNotFoundException( + CATEGORY_NOT_FOUND.getMessage(String.valueOf(categoryId)) + ); + } + } + + public void validateRoomExists(final UUID roomId) { + if (!roomRepository.existsById(roomId)) { + throw new ResourceNotFoundException( + ROOM_NOT_FOUND.getMessage(String.valueOf(roomId)) + ); + } + } +} + diff --git a/src/main/java/org/unilab/uniplan/roomcategory/RoomCategoryWebFacade.java b/src/main/java/org/unilab/uniplan/roomcategory/RoomCategoryWebFacade.java new file mode 100644 index 00000000..aea2fdd0 --- /dev/null +++ b/src/main/java/org/unilab/uniplan/roomcategory/RoomCategoryWebFacade.java @@ -0,0 +1,77 @@ +package org.unilab.uniplan.roomcategory; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; +import org.unilab.uniplan.exception.ResourceNotFoundException; +import org.unilab.uniplan.roomcategory.dto.RoomCategoryRequestDto; +import org.unilab.uniplan.roomcategory.dto.RoomCategoryResponseDto; +import java.util.List; +import java.util.UUID; + +import static org.unilab.uniplan.utils.ErrorConstants.ROOM_CATEGORY_NOT_FOUND; + +@Component +@Slf4j +@RequiredArgsConstructor +public class RoomCategoryWebFacade { + + private final RoomCategoryService roomCategoryService; + private final RoomCategoryMapper roomCategoryMapper; + private final RoomCategoryValidator roomCategoryValidator; + + private RoomCategory getRoomCategoryOrThrow(final RoomCategoryId id) { + return roomCategoryService.getById(id) + .orElseThrow(() -> new ResourceNotFoundException(ROOM_CATEGORY_NOT_FOUND.getMessage( + String.valueOf(id)))); + } + + @Transactional + public void createRoomCategory(final RoomCategoryRequestDto requestDto) { + roomCategoryValidator.validateForCreate(requestDto); + + final RoomCategory roomCategory = roomCategoryMapper.toEntity(requestDto); + roomCategoryService.save(roomCategory); + + log.info("created room category with roomId: {} and categoryId: {}", + requestDto.roomId(), + requestDto.categoryId()); + } + + @Transactional(readOnly = true) + public List getAllRoomCategories() { + return roomCategoryMapper.toResponseDtoList(roomCategoryService.getAll()); + } + + @Transactional + public void deleteRoomCategory(final UUID roomId, final UUID categoryId) { + final RoomCategoryId id = roomCategoryMapper.toRoomCategoryId(roomId, categoryId); + final RoomCategory roomCategory = getRoomCategoryOrThrow(id); + + roomCategoryService.delete(roomCategory); + + log.info("deleted room category with ID: {}", id); + } + + @Transactional(readOnly = true) + public RoomCategoryResponseDto getRoomCategoryById(final UUID roomId, final UUID categoryId) { + final RoomCategoryId id = roomCategoryMapper.toRoomCategoryId(roomId, categoryId); + final RoomCategory roomCategory = getRoomCategoryOrThrow(id); + + return roomCategoryMapper.toResponseDto(roomCategory); + } + + @Transactional + public void updateRoomCategory(final UUID roomId, final UUID categoryId, final RoomCategoryRequestDto requestDto) { + final RoomCategoryId id = roomCategoryMapper.toRoomCategoryId(roomId, categoryId); + + roomCategoryValidator.validateForUpdate(id, requestDto); + + final RoomCategory roomCategory = getRoomCategoryOrThrow(id); + roomCategoryMapper.updateEntity(requestDto, roomCategory); + roomCategoryService.save(roomCategory); + + log.info("updated room category with ID: {}", roomCategory.getId()); + } +} diff --git a/src/main/java/org/unilab/uniplan/roomcategory/dto/RoomCategoryRequestDto.java b/src/main/java/org/unilab/uniplan/roomcategory/dto/RoomCategoryRequestDto.java index 026c5e8f..0a4b7971 100644 --- a/src/main/java/org/unilab/uniplan/roomcategory/dto/RoomCategoryRequestDto.java +++ b/src/main/java/org/unilab/uniplan/roomcategory/dto/RoomCategoryRequestDto.java @@ -5,10 +5,10 @@ public record RoomCategoryRequestDto( - @NotNull + @NotNull(message = "Room ID cannot be null") UUID roomId, - @NotNull + @NotNull(message = "Category ID cannot be null") UUID categoryId ) { diff --git a/src/main/java/org/unilab/uniplan/roomcategory/dto/RoomCategoryResponseDto.java b/src/main/java/org/unilab/uniplan/roomcategory/dto/RoomCategoryResponseDto.java index 6c20290e..50a8bcc9 100644 --- a/src/main/java/org/unilab/uniplan/roomcategory/dto/RoomCategoryResponseDto.java +++ b/src/main/java/org/unilab/uniplan/roomcategory/dto/RoomCategoryResponseDto.java @@ -5,10 +5,8 @@ public record RoomCategoryResponseDto( - @NotNull UUID roomId, - @NotNull UUID categoryId ) { diff --git a/src/test/java/org/unilab/uniplan/roomcategory/RoomCategoryServiceTest.java b/src/test/java/org/unilab/uniplan/roomcategory/RoomCategoryServiceTest.java index 4e75c35b..655f6e10 100644 --- a/src/test/java/org/unilab/uniplan/roomcategory/RoomCategoryServiceTest.java +++ b/src/test/java/org/unilab/uniplan/roomcategory/RoomCategoryServiceTest.java @@ -26,98 +26,59 @@ class RoomCategoryServiceTest { @Mock private RoomCategoryRepository roomCategoryRepository; - @Mock - private RoomCategoryMapper roomCategoryMapper; - @InjectMocks private RoomCategoryService roomCategoryService; - private UUID roomId; - private UUID categoryId; - private RoomCategoryDto dto; + private RoomCategoryId id; private RoomCategory entity; @BeforeEach void setUp() { - roomId = UUID.randomUUID(); - categoryId = UUID.randomUUID(); - dto = new RoomCategoryDto(roomId, categoryId); + id = new RoomCategoryId(UUID.randomUUID(), UUID.randomUUID()); entity = new RoomCategory(); } @Test - void testCreateRoomCategoryShouldSaveAndReturnDto() { - when(roomCategoryMapper.toEntity(dto)).thenReturn(entity); - when(roomCategoryRepository.save(entity)).thenReturn(entity); - when(roomCategoryMapper.toDto(entity)).thenReturn(dto); + void save_shouldSaveRoomCategory() { + roomCategoryService.save(entity); - RoomCategoryDto result = roomCategoryService.createRoomCategory(dto); - - assertEquals(dto, result); + verify(roomCategoryRepository).save(entity); } @Test - void testGetAllRoomCategoriesShouldReturnListOfRoomCategoryDtos() { - List entities = List.of(entity); - List dtos = List.of(dto); + void getAll_shouldReturnListOfRoomCategories() { + final List entities = List.of(entity); when(roomCategoryRepository.findAll()).thenReturn(entities); - when(roomCategoryMapper.toDtoList(entities)).thenReturn(dtos); - List result = roomCategoryService.getAllRoomCategories(); + final List result = roomCategoryService.getAll(); - assertEquals(dtos, result); + assertEquals(entities, result); + verify(roomCategoryRepository).findAll(); } @Test - void testGetRoomCategoryByIdShouldReturnRoomCategoryDtoIfFound() { - RoomCategoryId id = new RoomCategoryId(roomId, categoryId); - - when(roomCategoryMapper.toRoomCategoryId(roomId, categoryId)).thenReturn(id); + void getById_shouldReturnOptional_whenRoomCategoryExists() { when(roomCategoryRepository.findById(id)).thenReturn(Optional.of(entity)); - when(roomCategoryMapper.toDto(entity)).thenReturn(dto); - RoomCategoryDto result = roomCategoryService.getRoomCategoryById(roomId, - categoryId); - assertEquals(dto, result); + final Optional result = roomCategoryService.getById(id); + assertEquals((Optional.of(entity)), result); } @Test - void testGetRoomCategoryByIdShouldReturnEmptyOptionalIfNotFound() { - RoomCategoryId id = new RoomCategoryId(roomId, categoryId); - - when(roomCategoryMapper.toRoomCategoryId(roomId, categoryId)).thenReturn(id); + void getById_shouldReturnEmptyOptional_whenRoomCategoryDoesNotExist() { when(roomCategoryRepository.findById(id)).thenReturn(Optional.empty()); - ResourceNotFoundException exception = assertThrows(ResourceNotFoundException.class, () -> roomCategoryService.getRoomCategoryById(roomId, - categoryId)); + final Optional result = roomCategoryService.getById(id); - assertTrue(exception.getMessage().contains(String.valueOf(id))); + assertEquals(Optional.empty(), result); + verify(roomCategoryRepository).findById(id); } @Test - void testDeleteRoomCategoryShouldDeleteIfFound() { - RoomCategoryId id = new RoomCategoryId(roomId, categoryId); - - when(roomCategoryMapper.toRoomCategoryId(roomId, categoryId)).thenReturn(id); - when(roomCategoryRepository.findById(id)).thenReturn(Optional.of(entity)); - doAnswer(invocation -> null).when(roomCategoryRepository).delete(entity); + void delete_shouldDeleteRoomCategory() { + roomCategoryService.delete(entity); - assertDoesNotThrow(() -> roomCategoryService.deleteRoomCategory(roomId, categoryId)); verify(roomCategoryRepository).delete(entity); } - - @Test - void testDeleteRoomCategoryShouldThrowIfNotFound() { - RoomCategoryId id = new RoomCategoryId(roomId, categoryId); - - when(roomCategoryMapper.toRoomCategoryId(roomId, categoryId)).thenReturn(id); - when(roomCategoryRepository.findById(id)).thenReturn(Optional.empty()); - - ResourceNotFoundException exception = assertThrows(ResourceNotFoundException.class, () -> - roomCategoryService.deleteRoomCategory(roomId, categoryId)); - - assertTrue(exception.getMessage().contains(String.valueOf(roomId))); - assertTrue(exception.getMessage().contains(String.valueOf(categoryId))); - } } diff --git a/src/test/java/org/unilab/uniplan/roomcategory/RoomCategoryValidatorTest.java b/src/test/java/org/unilab/uniplan/roomcategory/RoomCategoryValidatorTest.java new file mode 100644 index 00000000..fe8c238f --- /dev/null +++ b/src/test/java/org/unilab/uniplan/roomcategory/RoomCategoryValidatorTest.java @@ -0,0 +1,148 @@ +package org.unilab.uniplan.roomcategory; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.UUID; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.unilab.uniplan.category.CategoryRepository; +import org.unilab.uniplan.exception.ResourceNotFoundException; +import org.unilab.uniplan.room.RoomRepository; +import org.unilab.uniplan.roomcategory.dto.RoomCategoryRequestDto; + +@ExtendWith(MockitoExtension.class) +class RoomCategoryValidatorTest { + + @Mock + private CategoryRepository categoryRepository; + + @Mock + private RoomRepository roomRepository; + + @InjectMocks + private RoomCategoryValidator roomCategoryValidator; + + private UUID roomId; + private UUID categoryId; + private RoomCategoryId id; + private RoomCategoryRequestDto requestDto; + + @BeforeEach + void setUp() { + roomId = UUID.randomUUID(); + categoryId = UUID.randomUUID(); + id = new RoomCategoryId(roomId, categoryId); + requestDto = new RoomCategoryRequestDto(roomId, categoryId); + } + + @Test + void validateForCreate_shouldPass_whenCategoryAndRoomExist() { + when(categoryRepository.existsById(categoryId)).thenReturn(true); + when(roomRepository.existsById(roomId)).thenReturn(true); + + assertDoesNotThrow(() -> roomCategoryValidator.validateForCreate(requestDto)); + + verify(categoryRepository).existsById(categoryId); + verify(roomRepository).existsById(roomId); + } + + @Test + void ValidateForCreateShouldThrowWhenCategoryDoesNotExist() { + when(categoryRepository.existsById(categoryId)).thenReturn(false); + + assertThrows(ResourceNotFoundException.class, + () -> roomCategoryValidator.validateForCreate(requestDto)); + + verify(categoryRepository).existsById(categoryId); + } + + @Test + void validateForCreate_shouldThrow_whenCategoryDoesNotExist() { + when(categoryRepository.existsById(categoryId)).thenReturn(true); + when(roomRepository.existsById(roomId)).thenReturn(false); + + assertThrows(ResourceNotFoundException.class, + () -> roomCategoryValidator.validateForCreate(requestDto)); + + verify(categoryRepository).existsById(categoryId); + verify(roomRepository).existsById(roomId); + } + + @Test + void validateForUpdate_shouldPass_whenCategoryAndRoomExist() { + when(categoryRepository.existsById(categoryId)).thenReturn(true); + when(roomRepository.existsById(roomId)).thenReturn(true); + + assertDoesNotThrow(() -> roomCategoryValidator.validateForUpdate(id, requestDto)); + + verify(categoryRepository).existsById(categoryId); + verify(roomRepository).existsById(roomId); + } + + @Test + void validateForUpdate_shouldThrow_whenCategoryDoesNotExist() { + when(categoryRepository.existsById(categoryId)).thenReturn(false); + + assertThrows(ResourceNotFoundException.class, + () -> roomCategoryValidator.validateForUpdate(id, requestDto)); + + verify(categoryRepository).existsById(categoryId); + } + + @Test + void validateForUpdate_shouldThrow_whenRoomDoesNotExist() { + when(categoryRepository.existsById(categoryId)).thenReturn(true); + when(roomRepository.existsById(roomId)).thenReturn(false); + + assertThrows(ResourceNotFoundException.class, + () -> roomCategoryValidator.validateForUpdate(id, requestDto)); + + verify(categoryRepository).existsById(categoryId); + verify(roomRepository).existsById(roomId); + } + + @Test + void validateCategoryExists_shouldPass_whenCategoryExists() { + when(categoryRepository.existsById(categoryId)).thenReturn(true); + + assertDoesNotThrow(() -> roomCategoryValidator.validateCategoryExists(categoryId)); + + verify(categoryRepository).existsById(categoryId); + } + + @Test + void validateCategoryExists_shouldThrow_whenCategoryDoesNotExist() { + when(categoryRepository.existsById(categoryId)).thenReturn(false); + + assertThrows(ResourceNotFoundException.class, + () -> roomCategoryValidator.validateCategoryExists(categoryId)); + + verify(categoryRepository).existsById(categoryId); + } + + @Test + void validateRoomExists_shouldPass_whenRoomExists() { + when(roomRepository.existsById(roomId)).thenReturn(true); + + assertDoesNotThrow(() -> roomCategoryValidator.validateRoomExists(roomId)); + + verify(roomRepository).existsById(roomId); + } + + @Test + void validateRoomExists_shouldThrow_whenRoomDoesNotExist() { + when(roomRepository.existsById(roomId)).thenReturn(false); + + assertThrows(ResourceNotFoundException.class, + () -> roomCategoryValidator.validateRoomExists(roomId)); + + verify(roomRepository).existsById(roomId); + } +} \ No newline at end of file diff --git a/src/test/java/org/unilab/uniplan/roomcategory/RoomCategoryWebFacadeTest.java b/src/test/java/org/unilab/uniplan/roomcategory/RoomCategoryWebFacadeTest.java new file mode 100644 index 00000000..382b16ce --- /dev/null +++ b/src/test/java/org/unilab/uniplan/roomcategory/RoomCategoryWebFacadeTest.java @@ -0,0 +1,168 @@ +package org.unilab.uniplan.roomcategory; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.inOrder; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.List; +import java.util.Optional; +import java.util.UUID; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InOrder; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.unilab.uniplan.exception.ResourceNotFoundException; +import org.unilab.uniplan.roomcategory.dto.RoomCategoryRequestDto; +import org.unilab.uniplan.roomcategory.dto.RoomCategoryResponseDto; + +@ExtendWith(MockitoExtension.class) +class RoomCategoryWebFacadeTest { + + @Mock + private RoomCategoryService roomCategoryService; + + @Mock + private RoomCategoryMapper roomCategoryMapper; + + @Mock + private RoomCategoryValidator roomCategoryValidator; + + @InjectMocks + private RoomCategoryWebFacade roomCategoryWebFacade; + + private UUID roomId; + private UUID categoryId; + private RoomCategoryId id; + private RoomCategory roomCategory; + private RoomCategoryRequestDto requestDto; + private RoomCategoryResponseDto responseDto; + + @BeforeEach + void setUp() { + roomId = UUID.randomUUID(); + categoryId = UUID.randomUUID(); + id = new RoomCategoryId(roomId, categoryId); + roomCategory = new RoomCategory(); + requestDto = mock(RoomCategoryRequestDto.class); + responseDto = mock(RoomCategoryResponseDto.class); + } + + @Test + void createRoomCategory_shouldValidateMapAndSaveRoomCategory() { + when(roomCategoryMapper.toEntity(requestDto)).thenReturn(roomCategory); + + roomCategoryWebFacade.createRoomCategory(requestDto); + + final InOrder inOrder = inOrder(roomCategoryValidator, roomCategoryService); + inOrder.verify(roomCategoryValidator).validateForCreate(requestDto); + inOrder.verify(roomCategoryService).save(roomCategory); + + verify(roomCategoryMapper).toEntity(requestDto); + } + + @Test + void getAllRoomCategories_shouldReturnResponseDtoList() { + final List roomCategories = List.of(roomCategory); + final List responseDtos = List.of(responseDto); + + when(roomCategoryService.getAll()).thenReturn(roomCategories); + when(roomCategoryMapper.toResponseDtoList(roomCategories)).thenReturn(responseDtos); + + final List result = roomCategoryWebFacade.getAllRoomCategories(); + + assertEquals(responseDtos, result); + verify(roomCategoryService).getAll(); + verify(roomCategoryMapper).toResponseDtoList(roomCategories); + } + + @Test + void getRoomCategoryById_shouldReturnResponseDto_whenRoomCategoryExists() { + when(roomCategoryMapper.toRoomCategoryId(roomId, categoryId)).thenReturn(id); + when(roomCategoryService.getById(id)).thenReturn(Optional.of(roomCategory)); + when(roomCategoryMapper.toResponseDto(roomCategory)).thenReturn(responseDto); + + final RoomCategoryResponseDto result = roomCategoryWebFacade.getRoomCategoryById(roomId, categoryId); + + assertEquals(responseDto, result); + verify(roomCategoryMapper).toRoomCategoryId(roomId, categoryId); + verify(roomCategoryService).getById(id); + verify(roomCategoryMapper).toResponseDto(roomCategory); + } + + @Test + void getRoomCategoryById_shouldThrow_whenRoomCategoryNotExist() { + when(roomCategoryMapper.toRoomCategoryId(roomId, categoryId)).thenReturn(id); + when(roomCategoryService.getById(id)).thenReturn(Optional.empty()); + + assertThrows(ResourceNotFoundException.class, + () -> roomCategoryWebFacade.getRoomCategoryById(roomId, categoryId)); + + verify(roomCategoryMapper).toRoomCategoryId(roomId, categoryId); + verify(roomCategoryService).getById(id); + verify(roomCategoryMapper, never()).toResponseDto(any(RoomCategory.class)); + } + + @Test + void updateRoomCategory_shouldValidateUpdateAndSaveRoomCategory_whenRoomCategoryExists() { + when(roomCategoryMapper.toRoomCategoryId(roomId, categoryId)).thenReturn(id); + when(roomCategoryService.getById(id)).thenReturn(Optional.of(roomCategory)); + + roomCategoryWebFacade.updateRoomCategory(roomId, categoryId, requestDto); + + final InOrder inOrder = inOrder(roomCategoryValidator, roomCategoryService); + inOrder.verify(roomCategoryValidator).validateForUpdate(id, requestDto); + inOrder.verify(roomCategoryService).getById(id); + inOrder.verify(roomCategoryService).save(roomCategory); + + verify(roomCategoryMapper).toRoomCategoryId(roomId, categoryId); + verify(roomCategoryMapper).updateEntity(requestDto, roomCategory); + } + + @Test + void updateRoomCategory_shouldThrow_whenRoomCategoryDoesNotExist() { + when(roomCategoryMapper.toRoomCategoryId(roomId, categoryId)).thenReturn(id); + when(roomCategoryService.getById(id)).thenReturn(Optional.empty()); + + assertThrows(ResourceNotFoundException.class, + () -> roomCategoryWebFacade.updateRoomCategory(roomId, categoryId, requestDto)); + + verify(roomCategoryMapper).toRoomCategoryId(roomId, categoryId); + verify(roomCategoryValidator).validateForUpdate(id, requestDto); + verify(roomCategoryService).getById(id); + verify(roomCategoryMapper, never()).updateEntity(any(RoomCategoryRequestDto.class), any(RoomCategory.class)); + verify(roomCategoryService, never()).save(any(RoomCategory.class)); + } + + @Test + void deleteRoomCategory_shouldDeleteRoomCategory_whenRoomCategoryExists() { + when(roomCategoryMapper.toRoomCategoryId(roomId, categoryId)).thenReturn(id); + when(roomCategoryService.getById(id)).thenReturn(Optional.of(roomCategory)); + + roomCategoryWebFacade.deleteRoomCategory(roomId, categoryId); + + verify(roomCategoryMapper).toRoomCategoryId(roomId, categoryId); + verify(roomCategoryService).getById(id); + verify(roomCategoryService).delete(roomCategory); + } + + @Test + void deleteRoomCategory_shouldThrow_whenRoomCategoryDoesNotExist() { + when(roomCategoryMapper.toRoomCategoryId(roomId, categoryId)).thenReturn(id); + when(roomCategoryService.getById(id)).thenReturn(Optional.empty()); + + assertThrows(ResourceNotFoundException.class, + () -> roomCategoryWebFacade.deleteRoomCategory(roomId, categoryId)); + + verify(roomCategoryMapper).toRoomCategoryId(roomId, categoryId); + verify(roomCategoryService).getById(id); + verify(roomCategoryService, never()).delete(any(RoomCategory.class)); + } +} \ No newline at end of file