Wishlist and Notice Tests added
This commit is contained in:
@@ -0,0 +1,221 @@
|
|||||||
|
package _11.asktpk.artisanconnectbackend;
|
||||||
|
|
||||||
|
import _11.asktpk.artisanconnectbackend.controller.NoticeController;
|
||||||
|
import _11.asktpk.artisanconnectbackend.dto.*;
|
||||||
|
import _11.asktpk.artisanconnectbackend.service.ClientService;
|
||||||
|
import _11.asktpk.artisanconnectbackend.service.NoticeService;
|
||||||
|
import _11.asktpk.artisanconnectbackend.utils.Enums;
|
||||||
|
import _11.asktpk.artisanconnectbackend.utils.Tools;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
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.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyLong;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
class NoticeControllerTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private final NoticeService noticeService = mock(NoticeService.class);
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private final ClientService clientService = mock(ClientService.class);
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private final Tools tools = mock(Tools.class);
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private HttpServletRequest request;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private NoticeController noticeController;
|
||||||
|
|
||||||
|
private NoticeResponseDTO sampleNotice;
|
||||||
|
private NoticeRequestDTO sampleNoticeRequest;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
System.out.println("Inicjalizacja danych testowych przed każdym testem");
|
||||||
|
|
||||||
|
sampleNotice = new NoticeResponseDTO();
|
||||||
|
sampleNotice.setNoticeId(1L);
|
||||||
|
sampleNotice.setTitle("Testowe ogłoszenie");
|
||||||
|
sampleNotice.setClientId(1L);
|
||||||
|
sampleNotice.setDescription("Opis testowego ogłoszenia");
|
||||||
|
sampleNotice.setPrice(100.0);
|
||||||
|
sampleNotice.setCategory(Enums.Category.Woodworking);
|
||||||
|
sampleNotice.setStatus(Enums.Status.ACTIVE);
|
||||||
|
sampleNotice.setPublishDate(LocalDateTime.now());
|
||||||
|
|
||||||
|
sampleNoticeRequest = new NoticeRequestDTO();
|
||||||
|
sampleNoticeRequest.setTitle("Testowe ogłoszenie");
|
||||||
|
sampleNoticeRequest.setClientId(1L);
|
||||||
|
sampleNoticeRequest.setDescription("Opis testowego ogłoszenia");
|
||||||
|
sampleNoticeRequest.setPrice(100.0);
|
||||||
|
sampleNoticeRequest.setCategory(Enums.Category.Woodworking);
|
||||||
|
sampleNoticeRequest.setStatus(Enums.Status.ACTIVE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getAllNotices_ShouldReturnListOfNotices() {
|
||||||
|
System.out.println("Test: getAllNotices_ShouldReturnListOfNotices - powinien zwrócić listę ogłoszeń");
|
||||||
|
|
||||||
|
when(noticeService.getAllNotices()).thenReturn(List.of(sampleNotice));
|
||||||
|
|
||||||
|
List<NoticeResponseDTO> result = noticeController.getAllNotices();
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(1, result.size());
|
||||||
|
assertEquals(sampleNotice.getNoticeId(), result.getFirst().getNoticeId());
|
||||||
|
|
||||||
|
System.out.println("Pomyślnie zwrócono listę ogłoszeń");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getNoticeById_WhenNoticeExists_ShouldReturnNotice() {
|
||||||
|
System.out.println("Test: getNoticeById_WhenNoticeExists_ShouldReturnNotice - powinien zwrócić ogłoszenie gdy istnieje");
|
||||||
|
|
||||||
|
when(noticeService.noticeExists(1L)).thenReturn(true);
|
||||||
|
when(noticeService.getNoticeById(1L)).thenReturn(sampleNotice);
|
||||||
|
|
||||||
|
ResponseEntity<?> response = noticeController.getNoticeById(1L);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||||
|
assertNotNull(response.getBody());
|
||||||
|
assertEquals(sampleNotice, response.getBody());
|
||||||
|
|
||||||
|
System.out.println("Pomyślnie zwrócono istniejące ogłoszenie");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getNoticeById_WhenNoticeNotExists_ShouldReturnNotFound() {
|
||||||
|
System.out.println("Test: getNoticeById_WhenNoticeNotExists_ShouldReturnNotFound - powinien zwrócić 404 gdy ogłoszenie nie istnieje");
|
||||||
|
|
||||||
|
when(noticeService.noticeExists(1L)).thenReturn(false);
|
||||||
|
|
||||||
|
ResponseEntity<?> response = noticeController.getNoticeById(1L);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||||
|
|
||||||
|
System.out.println("Pomyślnie zwrócono status 404 dla nieistniejącego ogłoszenia");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void addNotice_WithValidData_ShouldCreateNotice() {
|
||||||
|
System.out.println("Test: addNotice_WithValidData_ShouldCreateNotice - powinien utworzyć nowe ogłoszenie przy poprawnych danych");
|
||||||
|
|
||||||
|
when(tools.getClientIdFromRequest(request)).thenReturn(1L);
|
||||||
|
when(clientService.clientExists(1L)).thenReturn(true);
|
||||||
|
when(noticeService.addNotice(any(NoticeRequestDTO.class))).thenReturn(1L);
|
||||||
|
|
||||||
|
ResponseEntity<NoticeAdditionDTO> response = noticeController.addNotice(sampleNoticeRequest, request);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.CREATED, response.getStatusCode());
|
||||||
|
assertNotNull(response.getBody());
|
||||||
|
assertEquals(1L, response.getBody().getNoticeId());
|
||||||
|
assertEquals("Dodano ogłoszenie.", response.getBody().getMessage());
|
||||||
|
|
||||||
|
System.out.println("Pomyślnie utworzono nowe ogłoszenie");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void addNotice_WithInvalidCategory_ShouldReturnBadRequest() {
|
||||||
|
System.out.println("Test: addNotice_WithInvalidCategory_ShouldReturnBadRequest - powinien zwrócić błąd dla nieprawidłowej kategorii");
|
||||||
|
|
||||||
|
sampleNoticeRequest.setCategory(null);
|
||||||
|
|
||||||
|
when(tools.getClientIdFromRequest(request)).thenReturn(1L);
|
||||||
|
when(clientService.clientExists(1L)).thenReturn(true);
|
||||||
|
|
||||||
|
ResponseEntity<NoticeAdditionDTO> response = noticeController.addNotice(sampleNoticeRequest, request);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
|
||||||
|
assertNotNull(response.getBody());
|
||||||
|
assertEquals("Nie ma takiej kategorii", response.getBody().getMessage());
|
||||||
|
|
||||||
|
System.out.println("Pomyślnie zwrócono błąd dla nieprawidłowej kategorii");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void addNotice_WhenClientNotExists_ShouldReturnBadRequest() {
|
||||||
|
System.out.println("Test: addNotice_WhenClientNotExists_ShouldReturnBadRequest - powinien zwrócić błąd gdy klient nie istnieje");
|
||||||
|
|
||||||
|
when(tools.getClientIdFromRequest(request)).thenReturn(1L);
|
||||||
|
when(clientService.clientExists(1L)).thenReturn(false);
|
||||||
|
|
||||||
|
ResponseEntity<NoticeAdditionDTO> response = noticeController.addNotice(sampleNoticeRequest, request);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
|
||||||
|
assertNotNull(response.getBody());
|
||||||
|
assertTrue(response.getBody().getMessage().contains("Nie znaleziono klienta o ID:"));
|
||||||
|
|
||||||
|
System.out.println("Pomyślnie zwrócono błąd dla nieistniejącego klienta");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void editNotice_WhenNoticeExistsAndOwnedByClient_ShouldUpdateNotice() {
|
||||||
|
System.out.println("Test: editNotice_WhenNoticeExistsAndOwnedByClient_ShouldUpdateNotice - powinien zaktualizować ogłoszenie gdy istnieje i należy do klienta");
|
||||||
|
|
||||||
|
when(tools.getClientIdFromRequest(request)).thenReturn(1L);
|
||||||
|
when(noticeService.noticeExists(1L)).thenReturn(true);
|
||||||
|
when(noticeService.isNoticeOwnedByClient(1L, 1L)).thenReturn(true);
|
||||||
|
when(noticeService.updateNotice(anyLong(), any(NoticeRequestDTO.class))).thenReturn(sampleNotice);
|
||||||
|
|
||||||
|
ResponseEntity<Object> response = noticeController.editNotice(1L, sampleNoticeRequest, request);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||||
|
assertNotNull(response.getBody());
|
||||||
|
assertEquals(sampleNotice, response.getBody());
|
||||||
|
|
||||||
|
System.out.println("Pomyślnie zaktualizowano ogłoszenie należące do klienta");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void editNotice_WhenNoticeNotOwnedByClient_ShouldReturnForbidden() {
|
||||||
|
System.out.println("Test: editNotice_WhenNoticeNotOwnedByClient_ShouldReturnForbidden - powinien zwrócić błąd 403 gdy ogłoszenie nie należy do klienta");
|
||||||
|
|
||||||
|
when(tools.getClientIdFromRequest(request)).thenReturn(2L);
|
||||||
|
when(noticeService.noticeExists(1L)).thenReturn(true);
|
||||||
|
when(noticeService.isNoticeOwnedByClient(1L, 2L)).thenReturn(false);
|
||||||
|
|
||||||
|
ResponseEntity<Object> response = noticeController.editNotice(1L, sampleNoticeRequest, request);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
|
||||||
|
assertNotNull(response.getBody());
|
||||||
|
assertTrue(((RequestResponseDTO) response.getBody()).getMessage().contains("Nie masz uprawnień"));
|
||||||
|
|
||||||
|
System.out.println("Pomyślnie zwrócono błąd 403 dla próby edycji nie swojego ogłoszenia");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void deleteNotice_WhenNoticeExistsAndOwnedByClient_ShouldDeleteNotice() {
|
||||||
|
System.out.println("Test: deleteNotice_WhenNoticeExistsAndOwnedByClient_ShouldDeleteNotice - powinien usunąć ogłoszenie gdy istnieje i należy do klienta");
|
||||||
|
|
||||||
|
when(tools.getClientIdFromRequest(request)).thenReturn(1L);
|
||||||
|
when(noticeService.noticeExists(1L)).thenReturn(true);
|
||||||
|
when(noticeService.isNoticeOwnedByClient(1L, 1L)).thenReturn(true);
|
||||||
|
|
||||||
|
ResponseEntity<RequestResponseDTO> response = noticeController.deleteNotice(1L, request);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||||
|
assertNotNull(response.getBody());
|
||||||
|
assertTrue(response.getBody().getMessage().contains("Pomyślnie usunięto"));
|
||||||
|
|
||||||
|
verify(noticeService, times(1)).deleteNotice(1L);
|
||||||
|
|
||||||
|
System.out.println("Pomyślnie usunięto ogłoszenie należące do klienta");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,200 @@
|
|||||||
|
package _11.asktpk.artisanconnectbackend;
|
||||||
|
|
||||||
|
import _11.asktpk.artisanconnectbackend.dto.AttributeDto;
|
||||||
|
import _11.asktpk.artisanconnectbackend.dto.NoticeRequestDTO;
|
||||||
|
import _11.asktpk.artisanconnectbackend.dto.NoticeResponseDTO;
|
||||||
|
import _11.asktpk.artisanconnectbackend.entities.*;
|
||||||
|
import _11.asktpk.artisanconnectbackend.repository.*;
|
||||||
|
import _11.asktpk.artisanconnectbackend.service.ImageService;
|
||||||
|
import _11.asktpk.artisanconnectbackend.service.NoticeService;
|
||||||
|
import _11.asktpk.artisanconnectbackend.utils.Enums;
|
||||||
|
import jakarta.persistence.EntityNotFoundException;
|
||||||
|
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 java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
class NoticeServiceTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private final NoticeRepository noticeRepository = mock(NoticeRepository.class);
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private final ClientRepository clientRepository = mock(ClientRepository.class);
|
||||||
|
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private final AttributesRepository attributesRepository = mock(AttributesRepository.class);
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private final AttributeValuesRepository attributeValuesRepository = mock(AttributeValuesRepository.class);
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private final AttributesNoticeRepository attributesNoticeRepository = mock(AttributesNoticeRepository.class);
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private NoticeService noticeService;
|
||||||
|
|
||||||
|
private Notice sampleNotice;
|
||||||
|
private NoticeRequestDTO sampleNoticeRequest;
|
||||||
|
private Client sampleClient;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
System.out.println("Inicjalizacja danych testowych przed każdym testem");
|
||||||
|
|
||||||
|
sampleClient = new Client();
|
||||||
|
sampleClient.setId(1L);
|
||||||
|
sampleClient.setEmail("test@example.com");
|
||||||
|
|
||||||
|
sampleNotice = new Notice();
|
||||||
|
sampleNotice.setIdNotice(1L);
|
||||||
|
sampleNotice.setTitle("Testowe ogłoszenie");
|
||||||
|
sampleNotice.setClient(sampleClient);
|
||||||
|
sampleNotice.setDescription("Opis testowego ogłoszenia");
|
||||||
|
sampleNotice.setPrice(100.0);
|
||||||
|
sampleNotice.setCategory(Enums.Category.Woodworking);
|
||||||
|
sampleNotice.setStatus(Enums.Status.ACTIVE);
|
||||||
|
sampleNotice.setPublishDate(LocalDateTime.now());
|
||||||
|
|
||||||
|
sampleNoticeRequest = new NoticeRequestDTO();
|
||||||
|
sampleNoticeRequest.setTitle("Testowe ogłoszenie");
|
||||||
|
sampleNoticeRequest.setClientId(1L);
|
||||||
|
sampleNoticeRequest.setDescription("Opis testowego ogłoszenia");
|
||||||
|
sampleNoticeRequest.setPrice(100.0);
|
||||||
|
sampleNoticeRequest.setCategory(Enums.Category.Woodworking);
|
||||||
|
sampleNoticeRequest.setStatus(Enums.Status.ACTIVE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getAllNotices_ShouldReturnListOfNotices() {
|
||||||
|
System.out.println("Test: getAllNotices_ShouldReturnListOfNotices - powinien zwrócić listę ogłoszeń");
|
||||||
|
|
||||||
|
when(noticeRepository.findAll()).thenReturn(List.of(sampleNotice));
|
||||||
|
|
||||||
|
List<NoticeResponseDTO> result = noticeService.getAllNotices();
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(1, result.size());
|
||||||
|
assertEquals(sampleNotice.getIdNotice(), result.getFirst().getNoticeId());
|
||||||
|
|
||||||
|
System.out.println("Pomyślnie zwrócono listę ogłoszeń");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getNoticeById_WhenNoticeExists_ShouldReturnNotice() {
|
||||||
|
System.out.println("Test: getNoticeById_WhenNoticeExists_ShouldReturnNotice - powinien zwrócić ogłoszenie gdy istnieje");
|
||||||
|
|
||||||
|
when(noticeRepository.findById(1L)).thenReturn(Optional.of(sampleNotice));
|
||||||
|
|
||||||
|
NoticeResponseDTO result = noticeService.getNoticeById(1L);
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(sampleNotice.getIdNotice(), result.getNoticeId());
|
||||||
|
|
||||||
|
System.out.println("Pomyślnie zwrócono istniejące ogłoszenie");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getNoticeById_WhenNoticeNotExists_ShouldThrowException() {
|
||||||
|
System.out.println("Test: getNoticeById_WhenNoticeNotExists_ShouldThrowException - powinien rzucić wyjątek gdy ogłoszenie nie istnieje");
|
||||||
|
|
||||||
|
when(noticeRepository.findById(1L)).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
assertThrows(EntityNotFoundException.class, () -> noticeService.getNoticeById(1L));
|
||||||
|
|
||||||
|
System.out.println("Pomyślnie rzucono wyjątek dla nieistniejącego ogłoszenia");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void addNotice_WithValidData_ShouldCreateNotice() {
|
||||||
|
System.out.println("Test: addNotice_WithValidData_ShouldCreateNotice - powinien utworzyć nowe ogłoszenie przy poprawnych danych");
|
||||||
|
|
||||||
|
when(clientRepository.findById(1L)).thenReturn(Optional.of(sampleClient));
|
||||||
|
when(noticeRepository.save(any(Notice.class))).thenReturn(sampleNotice);
|
||||||
|
|
||||||
|
Long result = noticeService.addNotice(sampleNoticeRequest);
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(1L, result);
|
||||||
|
|
||||||
|
verify(noticeRepository, times(1)).save(any(Notice.class));
|
||||||
|
|
||||||
|
System.out.println("Pomyślnie utworzono nowe ogłoszenie");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void addNotice_WithAttributes_ShouldSaveAttributes() {
|
||||||
|
System.out.println("Test: addNotice_WithAttributes_ShouldSaveAttributes - powinien zapisać atrybuty ogłoszenia");
|
||||||
|
|
||||||
|
AttributeDto attributeDto = new AttributeDto();
|
||||||
|
attributeDto.setName("Materiał");
|
||||||
|
attributeDto.setValue("Drewno");
|
||||||
|
sampleNoticeRequest.setAttributes(List.of(attributeDto));
|
||||||
|
|
||||||
|
when(clientRepository.findById(1L)).thenReturn(Optional.of(sampleClient));
|
||||||
|
when(noticeRepository.save(any(Notice.class))).thenReturn(sampleNotice);
|
||||||
|
when(attributesRepository.findByName(anyString())).thenReturn(Optional.empty());
|
||||||
|
when(attributeValuesRepository.findByAttributeAndValue(any(), anyString())).thenReturn(Optional.empty());
|
||||||
|
|
||||||
|
Long result = noticeService.addNotice(sampleNoticeRequest);
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
verify(attributesNoticeRepository, times(1)).save(any(AttributesNotice.class));
|
||||||
|
|
||||||
|
System.out.println("Pomyślnie zapisano atrybuty ogłoszenia");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void deleteNotice_WhenNoticeExists_ShouldDeleteNotice() {
|
||||||
|
System.out.println("Test: deleteNotice_WhenNoticeExists_ShouldDeleteNotice - powinien usunąć istniejące ogłoszenie");
|
||||||
|
|
||||||
|
when(noticeRepository.existsById(1L)).thenReturn(true);
|
||||||
|
|
||||||
|
noticeService.deleteNotice(1L);
|
||||||
|
|
||||||
|
verify(noticeRepository, times(1)).deleteById(1L);
|
||||||
|
|
||||||
|
System.out.println("Pomyślnie usunięto istniejące ogłoszenie");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void isNoticeOwnedByClient_WhenOwned_ShouldReturnTrue() {
|
||||||
|
System.out.println("Test: isNoticeOwnedByClient_WhenOwned_ShouldReturnTrue - powinien zwrócić true gdy ogłoszenie należy do klienta");
|
||||||
|
|
||||||
|
when(noticeRepository.existsByIdNoticeAndClientId(1L, 1L)).thenReturn(true);
|
||||||
|
|
||||||
|
boolean result = noticeService.isNoticeOwnedByClient(1L, 1L);
|
||||||
|
|
||||||
|
assertTrue(result);
|
||||||
|
|
||||||
|
System.out.println("Pomyślnie zwrócono true dla ogłoszenia należącego do klienta");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void boostNotice_ShouldUpdatePublishDate() {
|
||||||
|
System.out.println("Test: boostNotice_ShouldUpdatePublishDate - powinien zaktualizować datę publikacji");
|
||||||
|
|
||||||
|
when(noticeRepository.findById(1L)).thenReturn(Optional.of(sampleNotice));
|
||||||
|
when(noticeRepository.save(any(Notice.class))).thenReturn(sampleNotice);
|
||||||
|
|
||||||
|
noticeService.boostNotice(1L);
|
||||||
|
|
||||||
|
assertNotNull(sampleNotice.getPublishDate());
|
||||||
|
verify(noticeRepository, times(1)).save(sampleNotice);
|
||||||
|
|
||||||
|
System.out.println("Pomyślnie zaktualizowano datę publikacji ogłoszenia");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,107 @@
|
|||||||
|
package _11.asktpk.artisanconnectbackend;
|
||||||
|
|
||||||
|
import _11.asktpk.artisanconnectbackend.controller.WishlistController;
|
||||||
|
import _11.asktpk.artisanconnectbackend.dto.NoticeResponseDTO;
|
||||||
|
import _11.asktpk.artisanconnectbackend.dto.RequestResponseDTO;
|
||||||
|
import _11.asktpk.artisanconnectbackend.entities.Client;
|
||||||
|
import _11.asktpk.artisanconnectbackend.entities.Notice;
|
||||||
|
import _11.asktpk.artisanconnectbackend.service.ClientService;
|
||||||
|
import _11.asktpk.artisanconnectbackend.service.NoticeService;
|
||||||
|
import _11.asktpk.artisanconnectbackend.service.WishlistService;
|
||||||
|
import _11.asktpk.artisanconnectbackend.utils.Tools;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
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.springframework.http.ResponseEntity;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
class WishlistControllerTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private WishlistService wishlistService;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private ClientService clientService;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private NoticeService noticeService;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private Tools tools;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private HttpServletRequest request;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private WishlistController wishlistController;
|
||||||
|
|
||||||
|
private final Long testClientId = 1L;
|
||||||
|
private final Long testNoticeId = 1L;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
when(tools.getClientIdFromRequest(request)).thenReturn(testClientId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void toggleWishlist_shouldReturnSuccessWhenNoticeExists() {
|
||||||
|
NoticeResponseDTO noticeResponse = new NoticeResponseDTO();
|
||||||
|
noticeResponse.setNoticeId(testNoticeId);
|
||||||
|
|
||||||
|
when(noticeService.getNoticeById(testNoticeId)).thenReturn(noticeResponse);
|
||||||
|
when(clientService.getClientById(testClientId)).thenReturn(new Client());
|
||||||
|
when(noticeService.getNoticeByIdEntity(testNoticeId)).thenReturn(new Notice());
|
||||||
|
when(wishlistService.toggleWishlist(any(), any())).thenReturn(true);
|
||||||
|
|
||||||
|
ResponseEntity<RequestResponseDTO> response = wishlistController.toggleWishlist(testNoticeId, request);
|
||||||
|
|
||||||
|
assertEquals(200, response.getStatusCodeValue());
|
||||||
|
assertNotNull(response.getBody());
|
||||||
|
assertEquals("Wishlist entry added", response.getBody().getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void toggleWishlist_shouldReturnBadRequestWhenNoticeNotFound() {
|
||||||
|
when(noticeService.getNoticeById(testNoticeId)).thenReturn(null);
|
||||||
|
|
||||||
|
ResponseEntity<RequestResponseDTO> response = wishlistController.toggleWishlist(testNoticeId, request);
|
||||||
|
|
||||||
|
assertEquals(400, response.getStatusCodeValue());
|
||||||
|
assertNotNull(response.getBody());
|
||||||
|
assertEquals("Notice not found", response.getBody().getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getWishlistForClient_shouldReturnNoticeList() {
|
||||||
|
NoticeResponseDTO noticeResponse = new NoticeResponseDTO();
|
||||||
|
noticeResponse.setNoticeId(testNoticeId);
|
||||||
|
|
||||||
|
when(wishlistService.getNoticesInWishlist(testClientId)).thenReturn(Collections.singletonList(noticeResponse));
|
||||||
|
|
||||||
|
List<NoticeResponseDTO> result = wishlistController.getWishlistForClient(request);
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(1, result.size());
|
||||||
|
assertEquals(testNoticeId, result.getFirst().getNoticeId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getWishlistForClient_shouldReturnEmptyListWhenNoEntries() {
|
||||||
|
when(wishlistService.getNoticesInWishlist(testClientId)).thenReturn(Collections.emptyList());
|
||||||
|
|
||||||
|
List<NoticeResponseDTO> result = wishlistController.getWishlistForClient(request);
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
assertTrue(result.isEmpty());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
package _11.asktpk.artisanconnectbackend;
|
||||||
|
|
||||||
|
import _11.asktpk.artisanconnectbackend.dto.NoticeResponseDTO;
|
||||||
|
import _11.asktpk.artisanconnectbackend.entities.Client;
|
||||||
|
import _11.asktpk.artisanconnectbackend.entities.Notice;
|
||||||
|
import _11.asktpk.artisanconnectbackend.entities.Wishlist;
|
||||||
|
import _11.asktpk.artisanconnectbackend.repository.WishlistRepository;
|
||||||
|
import _11.asktpk.artisanconnectbackend.service.NoticeService;
|
||||||
|
import _11.asktpk.artisanconnectbackend.service.WishlistService;
|
||||||
|
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 java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
class WishlistServiceTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private WishlistRepository wishlistRepository;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private NoticeService noticeService;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private WishlistService wishlistService;
|
||||||
|
|
||||||
|
private Client testClient;
|
||||||
|
private Notice testNotice;
|
||||||
|
private Wishlist testWishlist;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
testClient = new Client();
|
||||||
|
testClient.setId(1L);
|
||||||
|
testClient.setEmail("test@example.com");
|
||||||
|
|
||||||
|
testNotice = new Notice();
|
||||||
|
testNotice.setIdNotice(1L);
|
||||||
|
testNotice.setTitle("Test Notice");
|
||||||
|
|
||||||
|
testWishlist = new Wishlist();
|
||||||
|
testWishlist.setId(1L);
|
||||||
|
testWishlist.setClient(testClient);
|
||||||
|
testWishlist.setNotice(testNotice);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void toggleWishlist_shouldAddWhenNotExists() {
|
||||||
|
when(wishlistRepository.findByClientAndNotice(testClient, testNotice)).thenReturn(Optional.empty());
|
||||||
|
when(wishlistRepository.save(any(Wishlist.class))).thenReturn(testWishlist);
|
||||||
|
|
||||||
|
boolean result = wishlistService.toggleWishlist(testClient, testNotice);
|
||||||
|
|
||||||
|
assertTrue(result);
|
||||||
|
verify(wishlistRepository, times(1)).save(any(Wishlist.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void toggleWishlist_shouldRemoveWhenExists() {
|
||||||
|
when(wishlistRepository.findByClientAndNotice(testClient, testNotice)).thenReturn(Optional.of(testWishlist));
|
||||||
|
|
||||||
|
boolean result = wishlistService.toggleWishlist(testClient, testNotice);
|
||||||
|
|
||||||
|
assertFalse(result);
|
||||||
|
verify(wishlistRepository, times(1)).delete(testWishlist);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getNoticesInWishlist_shouldReturnNoticeList() {
|
||||||
|
List<Wishlist> wishlistEntries = new ArrayList<>();
|
||||||
|
wishlistEntries.add(testWishlist);
|
||||||
|
|
||||||
|
when(wishlistRepository.findAllByClientId(1L)).thenReturn(wishlistEntries);
|
||||||
|
when(noticeService.getNoticeById(1L)).thenReturn(new NoticeResponseDTO());
|
||||||
|
|
||||||
|
List<NoticeResponseDTO> result = wishlistService.getNoticesInWishlist(1L);
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(1, result.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getNoticesInWishlist_shouldReturnEmptyListWhenNoEntries() {
|
||||||
|
when(wishlistRepository.findAllByClientId(1L)).thenReturn(new ArrayList<>());
|
||||||
|
|
||||||
|
List<NoticeResponseDTO> result = wishlistService.getNoticesInWishlist(1L);
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
assertTrue(result.isEmpty());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user