Warnings fixed, logs added to tests
This commit is contained in:
@@ -8,6 +8,7 @@ import _11.asktpk.artisanconnectbackend.utils.Enums;
|
|||||||
import _11.asktpk.artisanconnectbackend.utils.Tools;
|
import _11.asktpk.artisanconnectbackend.utils.Tools;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
import org.mockito.InjectMocks;
|
import org.mockito.InjectMocks;
|
||||||
@@ -28,13 +29,13 @@ import static org.mockito.Mockito.*;
|
|||||||
class NoticeControllerTest {
|
class NoticeControllerTest {
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private final NoticeService noticeService = mock(NoticeService.class);
|
private NoticeService noticeService;
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private final ClientService clientService = mock(ClientService.class);
|
private ClientService clientService;
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private final Tools tools = mock(Tools.class);
|
private Tools tools;
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private HttpServletRequest request;
|
private HttpServletRequest request;
|
||||||
@@ -47,7 +48,7 @@ class NoticeControllerTest {
|
|||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() {
|
void setUp() {
|
||||||
System.out.println("Inicjalizacja danych testowych przed każdym testem");
|
System.out.println("Inicjalizacja danych testowych...");
|
||||||
|
|
||||||
sampleNotice = new NoticeResponseDTO();
|
sampleNotice = new NoticeResponseDTO();
|
||||||
sampleNotice.setNoticeId(1L);
|
sampleNotice.setNoticeId(1L);
|
||||||
@@ -69,53 +70,43 @@ class NoticeControllerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Pobranie wszystkich ogłoszeń")
|
||||||
void getAllNotices_ShouldReturnListOfNotices() {
|
void getAllNotices_ShouldReturnListOfNotices() {
|
||||||
System.out.println("Test: getAllNotices_ShouldReturnListOfNotices - powinien zwrócić listę ogłoszeń");
|
|
||||||
|
|
||||||
when(noticeService.getAllNotices()).thenReturn(List.of(sampleNotice));
|
when(noticeService.getAllNotices()).thenReturn(List.of(sampleNotice));
|
||||||
|
|
||||||
List<NoticeResponseDTO> result = noticeController.getAllNotices();
|
List<NoticeResponseDTO> result = noticeController.getAllNotices();
|
||||||
|
|
||||||
assertNotNull(result);
|
assertNotNull(result);
|
||||||
assertEquals(1, result.size());
|
assertEquals(1, result.size());
|
||||||
assertEquals(sampleNotice.getNoticeId(), result.getFirst().getNoticeId());
|
System.out.println("Test GET /notices zakończony sukcesem");
|
||||||
|
|
||||||
System.out.println("Pomyślnie zwrócono listę ogłoszeń");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Pobranie istniejącego ogłoszenia")
|
||||||
void getNoticeById_WhenNoticeExists_ShouldReturnNotice() {
|
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.noticeExists(1L)).thenReturn(true);
|
||||||
when(noticeService.getNoticeById(1L)).thenReturn(sampleNotice);
|
when(noticeService.getNoticeById(1L)).thenReturn(sampleNotice);
|
||||||
|
|
||||||
ResponseEntity<?> response = noticeController.getNoticeById(1L);
|
ResponseEntity<?> response = noticeController.getNoticeById(1L);
|
||||||
|
|
||||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||||
assertNotNull(response.getBody());
|
System.out.println("Test GET /notices/{id} (istniejące) zakończony sukcesem");
|
||||||
assertEquals(sampleNotice, response.getBody());
|
|
||||||
|
|
||||||
System.out.println("Pomyślnie zwrócono istniejące ogłoszenie");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Pobranie nieistniejącego ogłoszenia")
|
||||||
void getNoticeById_WhenNoticeNotExists_ShouldReturnNotFound() {
|
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);
|
when(noticeService.noticeExists(1L)).thenReturn(false);
|
||||||
|
|
||||||
ResponseEntity<?> response = noticeController.getNoticeById(1L);
|
ResponseEntity<?> response = noticeController.getNoticeById(1L);
|
||||||
|
|
||||||
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||||
|
System.out.println("Test GET /notices/{id} (nieistniejące) zakończony sukcesem");
|
||||||
System.out.println("Pomyślnie zwrócono status 404 dla nieistniejącego ogłoszenia");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Dodanie poprawnego ogłoszenia")
|
||||||
void addNotice_WithValidData_ShouldCreateNotice() {
|
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(tools.getClientIdFromRequest(request)).thenReturn(1L);
|
||||||
when(clientService.clientExists(1L)).thenReturn(true);
|
when(clientService.clientExists(1L)).thenReturn(true);
|
||||||
when(noticeService.addNotice(any(NoticeRequestDTO.class))).thenReturn(1L);
|
when(noticeService.addNotice(any(NoticeRequestDTO.class))).thenReturn(1L);
|
||||||
@@ -123,17 +114,12 @@ class NoticeControllerTest {
|
|||||||
ResponseEntity<NoticeAdditionDTO> response = noticeController.addNotice(sampleNoticeRequest, request);
|
ResponseEntity<NoticeAdditionDTO> response = noticeController.addNotice(sampleNoticeRequest, request);
|
||||||
|
|
||||||
assertEquals(HttpStatus.CREATED, response.getStatusCode());
|
assertEquals(HttpStatus.CREATED, response.getStatusCode());
|
||||||
assertNotNull(response.getBody());
|
System.out.println("Test POST /notices (poprawne dane) zakończony sukcesem");
|
||||||
assertEquals(1L, response.getBody().getNoticeId());
|
|
||||||
assertEquals("Dodano ogłoszenie.", response.getBody().getMessage());
|
|
||||||
|
|
||||||
System.out.println("Pomyślnie utworzono nowe ogłoszenie");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Dodanie ogłoszenia z błędną kategorią")
|
||||||
void addNotice_WithInvalidCategory_ShouldReturnBadRequest() {
|
void addNotice_WithInvalidCategory_ShouldReturnBadRequest() {
|
||||||
System.out.println("Test: addNotice_WithInvalidCategory_ShouldReturnBadRequest - powinien zwrócić błąd dla nieprawidłowej kategorii");
|
|
||||||
|
|
||||||
sampleNoticeRequest.setCategory(null);
|
sampleNoticeRequest.setCategory(null);
|
||||||
|
|
||||||
when(tools.getClientIdFromRequest(request)).thenReturn(1L);
|
when(tools.getClientIdFromRequest(request)).thenReturn(1L);
|
||||||
@@ -142,32 +128,24 @@ class NoticeControllerTest {
|
|||||||
ResponseEntity<NoticeAdditionDTO> response = noticeController.addNotice(sampleNoticeRequest, request);
|
ResponseEntity<NoticeAdditionDTO> response = noticeController.addNotice(sampleNoticeRequest, request);
|
||||||
|
|
||||||
assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
|
assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
|
||||||
assertNotNull(response.getBody());
|
System.out.println("Test POST /notices (błędna kategoria) zakończony sukcesem");
|
||||||
assertEquals("Nie ma takiej kategorii", response.getBody().getMessage());
|
|
||||||
|
|
||||||
System.out.println("Pomyślnie zwrócono błąd dla nieprawidłowej kategorii");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Dodanie ogłoszenia przez nieistniejącego klienta")
|
||||||
void addNotice_WhenClientNotExists_ShouldReturnBadRequest() {
|
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(tools.getClientIdFromRequest(request)).thenReturn(1L);
|
||||||
when(clientService.clientExists(1L)).thenReturn(false);
|
when(clientService.clientExists(1L)).thenReturn(false);
|
||||||
|
|
||||||
ResponseEntity<NoticeAdditionDTO> response = noticeController.addNotice(sampleNoticeRequest, request);
|
ResponseEntity<NoticeAdditionDTO> response = noticeController.addNotice(sampleNoticeRequest, request);
|
||||||
|
|
||||||
assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
|
assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
|
||||||
assertNotNull(response.getBody());
|
System.out.println("Test POST /notices (nieistniejący klient) zakończony sukcesem");
|
||||||
assertTrue(response.getBody().getMessage().contains("Nie znaleziono klienta o ID:"));
|
|
||||||
|
|
||||||
System.out.println("Pomyślnie zwrócono błąd dla nieistniejącego klienta");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Aktualizacja własnego ogłoszenia")
|
||||||
void editNotice_WhenNoticeExistsAndOwnedByClient_ShouldUpdateNotice() {
|
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(tools.getClientIdFromRequest(request)).thenReturn(1L);
|
||||||
when(noticeService.noticeExists(1L)).thenReturn(true);
|
when(noticeService.noticeExists(1L)).thenReturn(true);
|
||||||
when(noticeService.isNoticeOwnedByClient(1L, 1L)).thenReturn(true);
|
when(noticeService.isNoticeOwnedByClient(1L, 1L)).thenReturn(true);
|
||||||
@@ -176,16 +154,12 @@ class NoticeControllerTest {
|
|||||||
ResponseEntity<Object> response = noticeController.editNotice(1L, sampleNoticeRequest, request);
|
ResponseEntity<Object> response = noticeController.editNotice(1L, sampleNoticeRequest, request);
|
||||||
|
|
||||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||||
assertNotNull(response.getBody());
|
System.out.println("Test PUT /notices/{id} (własne ogłoszenie) zakończony sukcesem");
|
||||||
assertEquals(sampleNotice, response.getBody());
|
|
||||||
|
|
||||||
System.out.println("Pomyślnie zaktualizowano ogłoszenie należące do klienta");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Próba aktualizacji cudzego ogłoszenia")
|
||||||
void editNotice_WhenNoticeNotOwnedByClient_ShouldReturnForbidden() {
|
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(tools.getClientIdFromRequest(request)).thenReturn(2L);
|
||||||
when(noticeService.noticeExists(1L)).thenReturn(true);
|
when(noticeService.noticeExists(1L)).thenReturn(true);
|
||||||
when(noticeService.isNoticeOwnedByClient(1L, 2L)).thenReturn(false);
|
when(noticeService.isNoticeOwnedByClient(1L, 2L)).thenReturn(false);
|
||||||
@@ -193,16 +167,12 @@ class NoticeControllerTest {
|
|||||||
ResponseEntity<Object> response = noticeController.editNotice(1L, sampleNoticeRequest, request);
|
ResponseEntity<Object> response = noticeController.editNotice(1L, sampleNoticeRequest, request);
|
||||||
|
|
||||||
assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
|
assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
|
||||||
assertNotNull(response.getBody());
|
System.out.println("Test PUT /notices/{id} (cudze ogłoszenie) zakończony sukcesem");
|
||||||
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
|
@Test
|
||||||
|
@DisplayName("Usunięcie własnego ogłoszenia")
|
||||||
void deleteNotice_WhenNoticeExistsAndOwnedByClient_ShouldDeleteNotice() {
|
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(tools.getClientIdFromRequest(request)).thenReturn(1L);
|
||||||
when(noticeService.noticeExists(1L)).thenReturn(true);
|
when(noticeService.noticeExists(1L)).thenReturn(true);
|
||||||
when(noticeService.isNoticeOwnedByClient(1L, 1L)).thenReturn(true);
|
when(noticeService.isNoticeOwnedByClient(1L, 1L)).thenReturn(true);
|
||||||
@@ -210,12 +180,7 @@ class NoticeControllerTest {
|
|||||||
ResponseEntity<RequestResponseDTO> response = noticeController.deleteNotice(1L, request);
|
ResponseEntity<RequestResponseDTO> response = noticeController.deleteNotice(1L, request);
|
||||||
|
|
||||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||||
assertNotNull(response.getBody());
|
|
||||||
assertTrue(response.getBody().getMessage().contains("Pomyślnie usunięto"));
|
|
||||||
|
|
||||||
verify(noticeService, times(1)).deleteNotice(1L);
|
verify(noticeService, times(1)).deleteNotice(1L);
|
||||||
|
System.out.println("Test DELETE /notices/{id} (własne ogłoszenie) zakończony sukcesem");
|
||||||
System.out.println("Pomyślnie usunięto ogłoszenie należące do klienta");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -5,11 +5,11 @@ import _11.asktpk.artisanconnectbackend.dto.NoticeRequestDTO;
|
|||||||
import _11.asktpk.artisanconnectbackend.dto.NoticeResponseDTO;
|
import _11.asktpk.artisanconnectbackend.dto.NoticeResponseDTO;
|
||||||
import _11.asktpk.artisanconnectbackend.entities.*;
|
import _11.asktpk.artisanconnectbackend.entities.*;
|
||||||
import _11.asktpk.artisanconnectbackend.repository.*;
|
import _11.asktpk.artisanconnectbackend.repository.*;
|
||||||
import _11.asktpk.artisanconnectbackend.service.ImageService;
|
|
||||||
import _11.asktpk.artisanconnectbackend.service.NoticeService;
|
import _11.asktpk.artisanconnectbackend.service.NoticeService;
|
||||||
import _11.asktpk.artisanconnectbackend.utils.Enums;
|
import _11.asktpk.artisanconnectbackend.utils.Enums;
|
||||||
import jakarta.persistence.EntityNotFoundException;
|
import jakarta.persistence.EntityNotFoundException;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
import org.mockito.InjectMocks;
|
import org.mockito.InjectMocks;
|
||||||
@@ -28,20 +28,16 @@ import static org.mockito.Mockito.*;
|
|||||||
class NoticeServiceTest {
|
class NoticeServiceTest {
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private final NoticeRepository noticeRepository = mock(NoticeRepository.class);
|
private NoticeRepository noticeRepository;
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private final ClientRepository clientRepository = mock(ClientRepository.class);
|
private ClientRepository clientRepository;
|
||||||
|
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private final AttributesRepository attributesRepository = mock(AttributesRepository.class);
|
private AttributesRepository attributesRepository;
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private final AttributeValuesRepository attributeValuesRepository = mock(AttributeValuesRepository.class);
|
private AttributeValuesRepository attributeValuesRepository;
|
||||||
|
|
||||||
@Mock
|
|
||||||
private final AttributesNoticeRepository attributesNoticeRepository = mock(AttributesNoticeRepository.class);
|
|
||||||
|
|
||||||
@InjectMocks
|
@InjectMocks
|
||||||
private NoticeService noticeService;
|
private NoticeService noticeService;
|
||||||
@@ -52,7 +48,7 @@ class NoticeServiceTest {
|
|||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() {
|
void setUp() {
|
||||||
System.out.println("Inicjalizacja danych testowych przed każdym testem");
|
System.out.println("Przygotowanie danych testowych...");
|
||||||
|
|
||||||
sampleClient = new Client();
|
sampleClient = new Client();
|
||||||
sampleClient.setId(1L);
|
sampleClient.setId(1L);
|
||||||
@@ -78,66 +74,52 @@ class NoticeServiceTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Pobranie wszystkich ogłoszeń - powinno zwrócić listę ogłoszeń")
|
||||||
void getAllNotices_ShouldReturnListOfNotices() {
|
void getAllNotices_ShouldReturnListOfNotices() {
|
||||||
System.out.println("Test: getAllNotices_ShouldReturnListOfNotices - powinien zwrócić listę ogłoszeń");
|
|
||||||
|
|
||||||
when(noticeRepository.findAll()).thenReturn(List.of(sampleNotice));
|
when(noticeRepository.findAll()).thenReturn(List.of(sampleNotice));
|
||||||
|
|
||||||
List<NoticeResponseDTO> result = noticeService.getAllNotices();
|
List<NoticeResponseDTO> result = noticeService.getAllNotices();
|
||||||
|
|
||||||
assertNotNull(result);
|
assertNotNull(result);
|
||||||
assertEquals(1, result.size());
|
assertEquals(1, result.size());
|
||||||
assertEquals(sampleNotice.getIdNotice(), result.getFirst().getNoticeId());
|
System.out.println("Test pobrania wszystkich ogłoszeń zakończony");
|
||||||
|
|
||||||
System.out.println("Pomyślnie zwrócono listę ogłoszeń");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Pobranie ogłoszenia po ID - gdy istnieje")
|
||||||
void getNoticeById_WhenNoticeExists_ShouldReturnNotice() {
|
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));
|
when(noticeRepository.findById(1L)).thenReturn(Optional.of(sampleNotice));
|
||||||
|
|
||||||
NoticeResponseDTO result = noticeService.getNoticeById(1L);
|
NoticeResponseDTO result = noticeService.getNoticeById(1L);
|
||||||
|
|
||||||
assertNotNull(result);
|
assertNotNull(result);
|
||||||
assertEquals(sampleNotice.getIdNotice(), result.getNoticeId());
|
System.out.println("Test pobrania istniejącego ogłoszenia zakończony");
|
||||||
|
|
||||||
System.out.println("Pomyślnie zwrócono istniejące ogłoszenie");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Pobranie ogłoszenia po ID - gdy nie istnieje")
|
||||||
void getNoticeById_WhenNoticeNotExists_ShouldThrowException() {
|
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());
|
when(noticeRepository.findById(1L)).thenReturn(Optional.empty());
|
||||||
|
|
||||||
assertThrows(EntityNotFoundException.class, () -> noticeService.getNoticeById(1L));
|
assertThrows(EntityNotFoundException.class, () -> noticeService.getNoticeById(1L));
|
||||||
|
System.out.println("Test pobrania nieistniejącego ogłoszenia zakończony");
|
||||||
System.out.println("Pomyślnie rzucono wyjątek dla nieistniejącego ogłoszenia");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Dodanie nowego ogłoszenia - poprawne dane")
|
||||||
void addNotice_WithValidData_ShouldCreateNotice() {
|
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(clientRepository.findById(1L)).thenReturn(Optional.of(sampleClient));
|
||||||
when(noticeRepository.save(any(Notice.class))).thenReturn(sampleNotice);
|
when(noticeRepository.save(any(Notice.class))).thenReturn(sampleNotice);
|
||||||
|
|
||||||
Long result = noticeService.addNotice(sampleNoticeRequest);
|
Long result = noticeService.addNotice(sampleNoticeRequest);
|
||||||
|
|
||||||
assertNotNull(result);
|
assertNotNull(result);
|
||||||
assertEquals(1L, result);
|
System.out.println("Test dodania ogłoszenia zakończony");
|
||||||
|
|
||||||
verify(noticeRepository, times(1)).save(any(Notice.class));
|
|
||||||
|
|
||||||
System.out.println("Pomyślnie utworzono nowe ogłoszenie");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Dodanie ogłoszenia z atrybutami")
|
||||||
void addNotice_WithAttributes_ShouldSaveAttributes() {
|
void addNotice_WithAttributes_ShouldSaveAttributes() {
|
||||||
System.out.println("Test: addNotice_WithAttributes_ShouldSaveAttributes - powinien zapisać atrybuty ogłoszenia");
|
|
||||||
|
|
||||||
AttributeDto attributeDto = new AttributeDto();
|
AttributeDto attributeDto = new AttributeDto();
|
||||||
attributeDto.setName("Materiał");
|
attributeDto.setName("Materiał");
|
||||||
attributeDto.setValue("Drewno");
|
attributeDto.setValue("Drewno");
|
||||||
@@ -151,50 +133,39 @@ class NoticeServiceTest {
|
|||||||
Long result = noticeService.addNotice(sampleNoticeRequest);
|
Long result = noticeService.addNotice(sampleNoticeRequest);
|
||||||
|
|
||||||
assertNotNull(result);
|
assertNotNull(result);
|
||||||
verify(attributesNoticeRepository, times(1)).save(any(AttributesNotice.class));
|
System.out.println("Test dodania ogłoszenia z atrybutami zakończony");
|
||||||
|
|
||||||
System.out.println("Pomyślnie zapisano atrybuty ogłoszenia");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Usunięcie istniejącego ogłoszenia")
|
||||||
void deleteNotice_WhenNoticeExists_ShouldDeleteNotice() {
|
void deleteNotice_WhenNoticeExists_ShouldDeleteNotice() {
|
||||||
System.out.println("Test: deleteNotice_WhenNoticeExists_ShouldDeleteNotice - powinien usunąć istniejące ogłoszenie");
|
|
||||||
|
|
||||||
when(noticeRepository.existsById(1L)).thenReturn(true);
|
when(noticeRepository.existsById(1L)).thenReturn(true);
|
||||||
|
|
||||||
noticeService.deleteNotice(1L);
|
noticeService.deleteNotice(1L);
|
||||||
|
|
||||||
verify(noticeRepository, times(1)).deleteById(1L);
|
System.out.println("Test usunięcia ogłoszenia zakończony");
|
||||||
|
|
||||||
System.out.println("Pomyślnie usunięto istniejące ogłoszenie");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Sprawdzenie właściciela ogłoszenia - gdy należy do klienta")
|
||||||
void isNoticeOwnedByClient_WhenOwned_ShouldReturnTrue() {
|
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);
|
when(noticeRepository.existsByIdNoticeAndClientId(1L, 1L)).thenReturn(true);
|
||||||
|
|
||||||
boolean result = noticeService.isNoticeOwnedByClient(1L, 1L);
|
boolean result = noticeService.isNoticeOwnedByClient(1L, 1L);
|
||||||
|
|
||||||
assertTrue(result);
|
assertTrue(result);
|
||||||
|
System.out.println("Test sprawdzenia właściciela (true) zakończony");
|
||||||
System.out.println("Pomyślnie zwrócono true dla ogłoszenia należącego do klienta");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Boostowanie ogłoszenia - aktualizacja daty publikacji")
|
||||||
void boostNotice_ShouldUpdatePublishDate() {
|
void boostNotice_ShouldUpdatePublishDate() {
|
||||||
System.out.println("Test: boostNotice_ShouldUpdatePublishDate - powinien zaktualizować datę publikacji");
|
|
||||||
|
|
||||||
when(noticeRepository.findById(1L)).thenReturn(Optional.of(sampleNotice));
|
when(noticeRepository.findById(1L)).thenReturn(Optional.of(sampleNotice));
|
||||||
when(noticeRepository.save(any(Notice.class))).thenReturn(sampleNotice);
|
when(noticeRepository.save(any(Notice.class))).thenReturn(sampleNotice);
|
||||||
|
|
||||||
noticeService.boostNotice(1L);
|
noticeService.boostNotice(1L);
|
||||||
|
|
||||||
assertNotNull(sampleNotice.getPublishDate());
|
assertNotNull(sampleNotice.getPublishDate());
|
||||||
verify(noticeRepository, times(1)).save(sampleNotice);
|
System.out.println("Test boostowania ogłoszenia zakończony");
|
||||||
|
|
||||||
System.out.println("Pomyślnie zaktualizowano datę publikacji ogłoszenia");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,6 +4,7 @@ import _11.asktpk.artisanconnectbackend.security.JwtUtil;
|
|||||||
import _11.asktpk.artisanconnectbackend.utils.Tools;
|
import _11.asktpk.artisanconnectbackend.utils.Tools;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
import org.mockito.InjectMocks;
|
import org.mockito.InjectMocks;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
@@ -25,7 +26,10 @@ class ToolsTest {
|
|||||||
private Tools tools;
|
private Tools tools;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Pobieranie ID klienta z requestu - powinno zwrócić ID gdy token jest poprawny")
|
||||||
void getClientIdFromRequest_shouldReturnClientIdWhenTokenValid() {
|
void getClientIdFromRequest_shouldReturnClientIdWhenTokenValid() {
|
||||||
|
System.out.println("Rozpoczęcie testu getClientIdFromRequest_shouldReturnClientIdWhenTokenValid");
|
||||||
|
|
||||||
String token = "valid.token.here";
|
String token = "valid.token.here";
|
||||||
Long expectedClientId = 1L;
|
Long expectedClientId = 1L;
|
||||||
|
|
||||||
@@ -35,6 +39,7 @@ class ToolsTest {
|
|||||||
Long result = tools.getClientIdFromRequest(request);
|
Long result = tools.getClientIdFromRequest(request);
|
||||||
|
|
||||||
assertEquals(expectedClientId, result);
|
assertEquals(expectedClientId, result);
|
||||||
}
|
|
||||||
|
|
||||||
|
System.out.println("Test zakończony powodzeniem: Poprawnie pobrano ID klienta z tokenu");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -11,6 +11,7 @@ import _11.asktpk.artisanconnectbackend.service.WishlistService;
|
|||||||
import _11.asktpk.artisanconnectbackend.utils.Tools;
|
import _11.asktpk.artisanconnectbackend.utils.Tools;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
import org.mockito.InjectMocks;
|
import org.mockito.InjectMocks;
|
||||||
@@ -50,11 +51,15 @@ class WishlistControllerTest {
|
|||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() {
|
void setUp() {
|
||||||
|
System.out.println("[Konfiguracja] Przygotowanie środowiska testowego...");
|
||||||
when(tools.getClientIdFromRequest(request)).thenReturn(testClientId);
|
when(tools.getClientIdFromRequest(request)).thenReturn(testClientId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Dodanie/Usunięcie z wishlisty - powinno zwrócić sukces gdy ogłoszenie istnieje")
|
||||||
void toggleWishlist_shouldReturnSuccessWhenNoticeExists() {
|
void toggleWishlist_shouldReturnSuccessWhenNoticeExists() {
|
||||||
|
System.out.println("Rozpoczęcie testu toggleWishlist_shouldReturnSuccessWhenNoticeExists");
|
||||||
|
|
||||||
NoticeResponseDTO noticeResponse = new NoticeResponseDTO();
|
NoticeResponseDTO noticeResponse = new NoticeResponseDTO();
|
||||||
noticeResponse.setNoticeId(testNoticeId);
|
noticeResponse.setNoticeId(testNoticeId);
|
||||||
|
|
||||||
@@ -65,24 +70,34 @@ class WishlistControllerTest {
|
|||||||
|
|
||||||
ResponseEntity<RequestResponseDTO> response = wishlistController.toggleWishlist(testNoticeId, request);
|
ResponseEntity<RequestResponseDTO> response = wishlistController.toggleWishlist(testNoticeId, request);
|
||||||
|
|
||||||
assertEquals(200, response.getStatusCodeValue());
|
assertEquals(200, response.getStatusCode().value());
|
||||||
assertNotNull(response.getBody());
|
assertNotNull(response.getBody());
|
||||||
assertEquals("Wishlist entry added", response.getBody().getMessage());
|
assertEquals("Wishlist entry added", response.getBody().getMessage());
|
||||||
|
|
||||||
|
System.out.println("Test zakończony powodzeniem: Poprawnie obsłużono dodanie/usunięcie z wishlisty");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Dodanie/Usunięcie z wishlisty - powinno zwrócić błąd gdy ogłoszenie nie istnieje")
|
||||||
void toggleWishlist_shouldReturnBadRequestWhenNoticeNotFound() {
|
void toggleWishlist_shouldReturnBadRequestWhenNoticeNotFound() {
|
||||||
|
System.out.println("Rozpoczęcie testu toggleWishlist_shouldReturnBadRequestWhenNoticeNotFound");
|
||||||
|
|
||||||
when(noticeService.getNoticeById(testNoticeId)).thenReturn(null);
|
when(noticeService.getNoticeById(testNoticeId)).thenReturn(null);
|
||||||
|
|
||||||
ResponseEntity<RequestResponseDTO> response = wishlistController.toggleWishlist(testNoticeId, request);
|
ResponseEntity<RequestResponseDTO> response = wishlistController.toggleWishlist(testNoticeId, request);
|
||||||
|
|
||||||
assertEquals(400, response.getStatusCodeValue());
|
assertEquals(400, response.getStatusCode().value());
|
||||||
assertNotNull(response.getBody());
|
assertNotNull(response.getBody());
|
||||||
assertEquals("Notice not found", response.getBody().getMessage());
|
assertEquals("Notice not found", response.getBody().getMessage());
|
||||||
|
|
||||||
|
System.out.println("Test zakończony powodzeniem: Poprawnie obsłużono brak ogłoszenia");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Pobieranie wishlisty - powinno zwrócić listę ogłoszeń")
|
||||||
void getWishlistForClient_shouldReturnNoticeList() {
|
void getWishlistForClient_shouldReturnNoticeList() {
|
||||||
|
System.out.println("Rozpoczęcie testu getWishlistForClient_shouldReturnNoticeList");
|
||||||
|
|
||||||
NoticeResponseDTO noticeResponse = new NoticeResponseDTO();
|
NoticeResponseDTO noticeResponse = new NoticeResponseDTO();
|
||||||
noticeResponse.setNoticeId(testNoticeId);
|
noticeResponse.setNoticeId(testNoticeId);
|
||||||
|
|
||||||
@@ -93,15 +108,22 @@ class WishlistControllerTest {
|
|||||||
assertNotNull(result);
|
assertNotNull(result);
|
||||||
assertEquals(1, result.size());
|
assertEquals(1, result.size());
|
||||||
assertEquals(testNoticeId, result.getFirst().getNoticeId());
|
assertEquals(testNoticeId, result.getFirst().getNoticeId());
|
||||||
|
|
||||||
|
System.out.println("Test zakończony powodzeniem: Poprawnie pobrano listę ogłoszeń");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Pobieranie wishlisty - powinno zwrócić pustą listę gdy brak wpisów")
|
||||||
void getWishlistForClient_shouldReturnEmptyListWhenNoEntries() {
|
void getWishlistForClient_shouldReturnEmptyListWhenNoEntries() {
|
||||||
|
System.out.println("Rozpoczęcie testu getWishlistForClient_shouldReturnEmptyListWhenNoEntries");
|
||||||
|
|
||||||
when(wishlistService.getNoticesInWishlist(testClientId)).thenReturn(Collections.emptyList());
|
when(wishlistService.getNoticesInWishlist(testClientId)).thenReturn(Collections.emptyList());
|
||||||
|
|
||||||
List<NoticeResponseDTO> result = wishlistController.getWishlistForClient(request);
|
List<NoticeResponseDTO> result = wishlistController.getWishlistForClient(request);
|
||||||
|
|
||||||
assertNotNull(result);
|
assertNotNull(result);
|
||||||
assertTrue(result.isEmpty());
|
assertTrue(result.isEmpty());
|
||||||
|
|
||||||
|
System.out.println("Test zakończony powodzeniem: Poprawnie zwrócono pustą wishlistę");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -8,6 +8,7 @@ import _11.asktpk.artisanconnectbackend.repository.WishlistRepository;
|
|||||||
import _11.asktpk.artisanconnectbackend.service.NoticeService;
|
import _11.asktpk.artisanconnectbackend.service.NoticeService;
|
||||||
import _11.asktpk.artisanconnectbackend.service.WishlistService;
|
import _11.asktpk.artisanconnectbackend.service.WishlistService;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
import org.mockito.InjectMocks;
|
import org.mockito.InjectMocks;
|
||||||
@@ -40,6 +41,8 @@ class WishlistServiceTest {
|
|||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() {
|
void setUp() {
|
||||||
|
System.out.println("Przygotowanie danych testowych...");
|
||||||
|
|
||||||
testClient = new Client();
|
testClient = new Client();
|
||||||
testClient.setId(1L);
|
testClient.setId(1L);
|
||||||
testClient.setEmail("test@example.com");
|
testClient.setEmail("test@example.com");
|
||||||
@@ -52,10 +55,15 @@ class WishlistServiceTest {
|
|||||||
testWishlist.setId(1L);
|
testWishlist.setId(1L);
|
||||||
testWishlist.setClient(testClient);
|
testWishlist.setClient(testClient);
|
||||||
testWishlist.setNotice(testNotice);
|
testWishlist.setNotice(testNotice);
|
||||||
|
|
||||||
|
System.out.println("[Konfiguracja] Dane testowe gotowe");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Przełączanie wishlisty - powinno dodać gdy wpis nie istnieje")
|
||||||
void toggleWishlist_shouldAddWhenNotExists() {
|
void toggleWishlist_shouldAddWhenNotExists() {
|
||||||
|
System.out.println("Rozpoczęcie testu toggleWishlist_shouldAddWhenNotExists");
|
||||||
|
|
||||||
when(wishlistRepository.findByClientAndNotice(testClient, testNotice)).thenReturn(Optional.empty());
|
when(wishlistRepository.findByClientAndNotice(testClient, testNotice)).thenReturn(Optional.empty());
|
||||||
when(wishlistRepository.save(any(Wishlist.class))).thenReturn(testWishlist);
|
when(wishlistRepository.save(any(Wishlist.class))).thenReturn(testWishlist);
|
||||||
|
|
||||||
@@ -63,20 +71,30 @@ class WishlistServiceTest {
|
|||||||
|
|
||||||
assertTrue(result);
|
assertTrue(result);
|
||||||
verify(wishlistRepository, times(1)).save(any(Wishlist.class));
|
verify(wishlistRepository, times(1)).save(any(Wishlist.class));
|
||||||
|
|
||||||
|
System.out.println("Test zakończony powodzeniem: Poprawnie dodano do wishlisty");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Przełączanie wishlisty - powinno usunąć gdy wpis istnieje")
|
||||||
void toggleWishlist_shouldRemoveWhenExists() {
|
void toggleWishlist_shouldRemoveWhenExists() {
|
||||||
|
System.out.println("Rozpoczęcie testu toggleWishlist_shouldRemoveWhenExists");
|
||||||
|
|
||||||
when(wishlistRepository.findByClientAndNotice(testClient, testNotice)).thenReturn(Optional.of(testWishlist));
|
when(wishlistRepository.findByClientAndNotice(testClient, testNotice)).thenReturn(Optional.of(testWishlist));
|
||||||
|
|
||||||
boolean result = wishlistService.toggleWishlist(testClient, testNotice);
|
boolean result = wishlistService.toggleWishlist(testClient, testNotice);
|
||||||
|
|
||||||
assertFalse(result);
|
assertFalse(result);
|
||||||
verify(wishlistRepository, times(1)).delete(testWishlist);
|
verify(wishlistRepository, times(1)).delete(testWishlist);
|
||||||
|
|
||||||
|
System.out.println("Test zakończony powodzeniem: Poprawnie usunięto z wishlisty");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Pobieranie ogłoszeń z wishlisty - powinno zwrócić listę ogłoszeń")
|
||||||
void getNoticesInWishlist_shouldReturnNoticeList() {
|
void getNoticesInWishlist_shouldReturnNoticeList() {
|
||||||
|
System.out.println("Rozpoczęcie testu getNoticesInWishlist_shouldReturnNoticeList");
|
||||||
|
|
||||||
List<Wishlist> wishlistEntries = new ArrayList<>();
|
List<Wishlist> wishlistEntries = new ArrayList<>();
|
||||||
wishlistEntries.add(testWishlist);
|
wishlistEntries.add(testWishlist);
|
||||||
|
|
||||||
@@ -87,15 +105,22 @@ class WishlistServiceTest {
|
|||||||
|
|
||||||
assertNotNull(result);
|
assertNotNull(result);
|
||||||
assertEquals(1, result.size());
|
assertEquals(1, result.size());
|
||||||
|
|
||||||
|
System.out.println(" Test zakończony powodzeniem: Poprawnie zwrócono listę ogłoszeń");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Pobieranie ogłoszeń z wishlisty - powinno zwrócić pustą listę gdy brak wpisów")
|
||||||
void getNoticesInWishlist_shouldReturnEmptyListWhenNoEntries() {
|
void getNoticesInWishlist_shouldReturnEmptyListWhenNoEntries() {
|
||||||
|
System.out.println("Rozpoczęcie testu getNoticesInWishlist_shouldReturnEmptyListWhenNoEntries");
|
||||||
|
|
||||||
when(wishlistRepository.findAllByClientId(1L)).thenReturn(new ArrayList<>());
|
when(wishlistRepository.findAllByClientId(1L)).thenReturn(new ArrayList<>());
|
||||||
|
|
||||||
List<NoticeResponseDTO> result = wishlistService.getNoticesInWishlist(1L);
|
List<NoticeResponseDTO> result = wishlistService.getNoticesInWishlist(1L);
|
||||||
|
|
||||||
assertNotNull(result);
|
assertNotNull(result);
|
||||||
assertTrue(result.isEmpty());
|
assertTrue(result.isEmpty());
|
||||||
|
|
||||||
|
System.out.println("Test zakończony powodzeniem: Poprawnie zwrócono pustą listę");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user