Compare commits
3 Commits
7c7e82b0e6
...
tests
| Author | SHA1 | Date | |
|---|---|---|---|
| c59998c113 | |||
| ff5dc5c090 | |||
| 4d7a191e8a |
@@ -29,6 +29,8 @@ public class ClientServiceTest {
|
||||
@Test
|
||||
@DisplayName("Test pobierania wszystkich klientów")
|
||||
public void testGetAllClients() {
|
||||
System.out.println("Rozpoczęcie testu: testGetAllClients - Test pobierania wszystkich klientów");
|
||||
|
||||
Client client1 = new Client();
|
||||
client1.setId(1L);
|
||||
client1.setEmail("client1@example.com");
|
||||
@@ -43,17 +45,20 @@ public class ClientServiceTest {
|
||||
|
||||
List<ClientDTO> clients = clientService.getAllClients();
|
||||
|
||||
System.out.println("Pobrano listę klientów, liczba elementów: " + clients.size());
|
||||
assertEquals(2, clients.size(), "Lista klientów powinna zawierać 2 elementy");
|
||||
System.out.println("Pierwszy klient na liście: " + clients.getFirst().getEmail());
|
||||
assertEquals("client1@example.com", clients.getFirst().getEmail(), "Email pierwszego klienta powinien być poprawny");
|
||||
|
||||
System.out.println("Test pobierania wszystkich klientów przeszedł pomyślnie.");
|
||||
System.out.println("Test pobierania wszystkich klientów zakończony sukcesem. Zwrócono " + clients.size() + " klientów.");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test pobierania klienta po ID - klient istnieje")
|
||||
public void testGetClientByIdExists() {
|
||||
// Przygotowanie danych
|
||||
Long clientId = 1L;
|
||||
System.out.println("Rozpoczęcie testu: testGetClientByIdExists - Test pobierania klienta po ID (ID: " + clientId + ")");
|
||||
|
||||
Client client = new Client();
|
||||
client.setId(clientId);
|
||||
client.setEmail("client@example.com");
|
||||
@@ -63,29 +68,35 @@ public class ClientServiceTest {
|
||||
|
||||
Client retrievedClient = clientService.getClientById(clientId);
|
||||
|
||||
System.out.println("Pobrano klienta o ID: " + (retrievedClient != null ? retrievedClient.getId() : "null"));
|
||||
assertNotNull(retrievedClient, "Pobrany klient nie powinien być null");
|
||||
assertEquals(clientId, retrievedClient.getId(), "ID klienta powinno być zgodne");
|
||||
|
||||
System.out.println("Test pobierania klienta po ID (klient istnieje) przeszedł pomyślnie.");
|
||||
System.out.println("Test pobierania klienta po ID (ID: " + clientId + ") zakończony sukcesem. Znaleziono klienta: " + retrievedClient.getEmail());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test pobierania klienta po ID - klient nie istnieje")
|
||||
public void testGetClientByIdNotExists() {
|
||||
Long clientId = 1L;
|
||||
System.out.println("Rozpoczęcie testu: testGetClientByIdNotExists - Test pobierania nieistniejącego klienta (ID: " + clientId + ")");
|
||||
|
||||
when(clientRepository.findById(clientId)).thenReturn(Optional.empty());
|
||||
|
||||
Client retrievedClient = clientService.getClientById(clientId);
|
||||
|
||||
System.out.println("Próba pobrania nieistniejącego klienta zwróciła: " + retrievedClient);
|
||||
assertNull(retrievedClient, "Pobrany klient powinien być null, gdy nie istnieje");
|
||||
|
||||
System.out.println("Test pobierania klienta po ID (klient nie istnieje) przeszedł pomyślnie.");
|
||||
System.out.println("Test pobierania nieistniejącego klienta (ID: " + clientId + ") zakończony sukcesem. Zwrócono null zgodnie z oczekiwaniami.");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test pobierania klienta po emailu")
|
||||
public void testGetClientByEmail() {
|
||||
String email = "client@example.com";
|
||||
System.out.println("Rozpoczęcie testu: testGetClientByEmail - Test pobierania klienta po emailu (" + email + ")");
|
||||
|
||||
Client client = new Client();
|
||||
client.setEmail(email);
|
||||
|
||||
@@ -93,16 +104,18 @@ public class ClientServiceTest {
|
||||
|
||||
Client retrievedClient = clientService.getClientByEmail(email);
|
||||
|
||||
System.out.println("Pobrano klienta o emailu: " + (retrievedClient != null ? retrievedClient.getEmail() : "null"));
|
||||
assertNotNull(retrievedClient, "Pobrany klient nie powinien być null");
|
||||
assertEquals(email, retrievedClient.getEmail(), "Email klienta powinien być zgodny");
|
||||
|
||||
System.out.println("Test pobierania klienta po emailu przeszedł pomyślnie.");
|
||||
System.out.println("Test pobierania klienta po emailu (" + email + ") zakończony sukcesem.");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test dodawania klienta")
|
||||
public void testAddClient() {
|
||||
// Przygotowanie danych
|
||||
System.out.println("Rozpoczęcie testu: testAddClient - Test dodawania nowego klienta");
|
||||
|
||||
ClientDTO clientDTO = new ClientDTO();
|
||||
clientDTO.setEmail("newclient@example.com");
|
||||
clientDTO.setRole("USER");
|
||||
@@ -115,16 +128,19 @@ public class ClientServiceTest {
|
||||
|
||||
ClientDTO addedClient = clientService.addClient(clientDTO);
|
||||
|
||||
System.out.println("Dodano nowego klienta: " + (addedClient != null ? addedClient.getEmail() : "null"));
|
||||
assertNotNull(addedClient, "Dodany klient nie powinien być null");
|
||||
assertEquals("newclient@example.com", addedClient.getEmail(), "Email dodanego klienta powinien być poprawny");
|
||||
|
||||
System.out.println("Test dodawania klienta przeszedł pomyślnie.");
|
||||
System.out.println("Test dodawania klienta zakończony sukcesem. Dodano klienta: " + addedClient.getEmail());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test aktualizacji klienta")
|
||||
public void testUpdateClient() {
|
||||
Long clientId = 1L;
|
||||
System.out.println("Rozpoczęcie testu: testUpdateClient - Test aktualizacji klienta (ID: " + clientId + ")");
|
||||
|
||||
Client existingClient = new Client();
|
||||
existingClient.setId(clientId);
|
||||
existingClient.setEmail("old@example.com");
|
||||
@@ -142,41 +158,49 @@ public class ClientServiceTest {
|
||||
|
||||
ClientDTO updatedClient = clientService.updateClient(clientId, updatedDTO);
|
||||
|
||||
System.out.println("Zaktualizowano klienta. Nowy email: " + updatedClient.getEmail());
|
||||
assertEquals("updated@example.com", updatedClient.getEmail(), "Email klienta powinien być zaktualizowany");
|
||||
|
||||
System.out.println("Test aktualizacji klienta przeszedł pomyślnie.");
|
||||
System.out.println("Test aktualizacji klienta (ID: " + clientId + ") zakończony sukcesem. Nowy email: " + updatedClient.getEmail());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test aktualizacji klienta - klient nie istnieje")
|
||||
public void testUpdateClientNotExists() {
|
||||
long clientId = 1L;
|
||||
System.out.println("Rozpoczęcie testu: testUpdateClientNotExists - Test aktualizacji nieistniejącego klienta (ID: " + clientId + ")");
|
||||
|
||||
ClientDTO updatedDTO = new ClientDTO();
|
||||
updatedDTO.setEmail("updated@example.com");
|
||||
|
||||
when(clientRepository.findById(clientId)).thenReturn(Optional.empty());
|
||||
|
||||
System.out.println("Oczekiwanie na EntityNotFoundException...");
|
||||
assertThrows(EntityNotFoundException.class, () -> clientService.updateClient(clientId, updatedDTO),
|
||||
"Powinien zostać rzucony EntityNotFoundException");
|
||||
|
||||
System.out.println("Test aktualizacji klienta (klient nie istnieje) przeszedł pomyślnie.");
|
||||
System.out.println("Test aktualizacji nieistniejącego klienta (ID: " + clientId + ") zakończony sukcesem. Rzucono wyjątek EntityNotFoundException zgodnie z oczekiwaniami.");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test usuwania klienta")
|
||||
public void testDeleteClient() {
|
||||
Long clientId = 1L;
|
||||
System.out.println("Rozpoczęcie testu: testDeleteClient - Test usuwania klienta (ID: " + clientId + ")");
|
||||
|
||||
clientService.deleteClient(clientId);
|
||||
|
||||
verify(clientRepository, times(1)).deleteById(clientId);
|
||||
System.out.println("Weryfikacja: metoda deleteById została wywołana 1 raz z ID: " + clientId);
|
||||
|
||||
System.out.println("Test usuwania klienta przeszedł pomyślnie.");
|
||||
System.out.println("Test usuwania klienta (ID: " + clientId + ") zakończony sukcesem.");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Test konwersji encji do DTO")
|
||||
public void testToDto() {
|
||||
System.out.println("Rozpoczęcie testu: testToDto - Test konwersji encji Client do ClientDTO");
|
||||
|
||||
Client client = new Client();
|
||||
client.setId(1L);
|
||||
client.setEmail("client@example.com");
|
||||
@@ -187,8 +211,24 @@ public class ClientServiceTest {
|
||||
role.setRole("USER");
|
||||
client.setRole(role);
|
||||
|
||||
System.out.println("Przygotowano encję Client do konwersji:");
|
||||
System.out.println("ID: " + client.getId());
|
||||
System.out.println("Email: " + client.getEmail());
|
||||
System.out.println("Imię: " + client.getFirstName());
|
||||
System.out.println("Nazwisko: " + client.getLastName());
|
||||
System.out.println("Obraz: " + client.getImage());
|
||||
System.out.println("Rola: " + client.getRole().getRole());
|
||||
|
||||
ClientDTO dto = clientService.toDto(client);
|
||||
|
||||
System.out.println("Wynik konwersji do DTO:");
|
||||
System.out.println("ID: " + dto.getId());
|
||||
System.out.println("Email: " + dto.getEmail());
|
||||
System.out.println("Imię: " + dto.getFirstName());
|
||||
System.out.println("Nazwisko: " + dto.getLastName());
|
||||
System.out.println("Obraz: " + dto.getImage());
|
||||
System.out.println("Rola: " + dto.getRole());
|
||||
|
||||
assertEquals(1L, dto.getId(), "ID w DTO powinno być zgodne");
|
||||
assertEquals("client@example.com", dto.getEmail(), "Email w DTO powinien być zgodny");
|
||||
assertEquals("Jan", dto.getFirstName(), "Imię w DTO powinno być zgodne");
|
||||
@@ -196,7 +236,6 @@ public class ClientServiceTest {
|
||||
assertEquals("image.jpg", dto.getImage(), "Obraz w DTO powinien być zgodny");
|
||||
assertEquals("USER", dto.getRole(), "Rola w DTO powinna być zgodna");
|
||||
|
||||
System.out.println("Test konwersji encji do DTO przeszedł pomyślnie.");
|
||||
System.out.println("Test konwersji encji do DTO zakończony sukcesem. Wszystkie pola zostały poprawnie zmapowane.");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,6 +8,7 @@ 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.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
@@ -28,13 +29,13 @@ import static org.mockito.Mockito.*;
|
||||
class NoticeControllerTest {
|
||||
|
||||
@Mock
|
||||
private final NoticeService noticeService = mock(NoticeService.class);
|
||||
private NoticeService noticeService;
|
||||
|
||||
@Mock
|
||||
private final ClientService clientService = mock(ClientService.class);
|
||||
private ClientService clientService;
|
||||
|
||||
@Mock
|
||||
private final Tools tools = mock(Tools.class);
|
||||
private Tools tools;
|
||||
|
||||
@Mock
|
||||
private HttpServletRequest request;
|
||||
@@ -47,7 +48,7 @@ class NoticeControllerTest {
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
System.out.println("Inicjalizacja danych testowych przed każdym testem");
|
||||
System.out.println("Inicjalizacja danych testowych...");
|
||||
|
||||
sampleNotice = new NoticeResponseDTO();
|
||||
sampleNotice.setNoticeId(1L);
|
||||
@@ -69,53 +70,43 @@ class NoticeControllerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Pobranie wszystkich ogłoszeń")
|
||||
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ń");
|
||||
System.out.println("Test GET /notices zakończony sukcesem");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Pobranie istniejącego ogłoszenia")
|
||||
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");
|
||||
System.out.println("Test GET /notices/{id} (istniejące) zakończony sukcesem");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Pobranie nieistniejącego ogłoszenia")
|
||||
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");
|
||||
System.out.println("Test GET /notices/{id} (nieistniejące) zakończony sukcesem");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Dodanie poprawnego ogłoszenia")
|
||||
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);
|
||||
@@ -123,17 +114,12 @@ class NoticeControllerTest {
|
||||
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");
|
||||
System.out.println("Test POST /notices (poprawne dane) zakończony sukcesem");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Dodanie ogłoszenia z błędną kategorią")
|
||||
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);
|
||||
@@ -142,32 +128,24 @@ class NoticeControllerTest {
|
||||
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");
|
||||
System.out.println("Test POST /notices (błędna kategoria) zakończony sukcesem");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Dodanie ogłoszenia przez nieistniejącego klienta")
|
||||
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");
|
||||
System.out.println("Test POST /notices (nieistniejący klient) zakończony sukcesem");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Aktualizacja własnego ogłoszenia")
|
||||
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);
|
||||
@@ -176,16 +154,12 @@ class NoticeControllerTest {
|
||||
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");
|
||||
System.out.println("Test PUT /notices/{id} (własne ogłoszenie) zakończony sukcesem");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Próba aktualizacji cudzego ogłoszenia")
|
||||
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);
|
||||
@@ -193,16 +167,12 @@ class NoticeControllerTest {
|
||||
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");
|
||||
System.out.println("Test PUT /notices/{id} (cudze ogłoszenie) zakończony sukcesem");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Usunięcie własnego ogłoszenia")
|
||||
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);
|
||||
@@ -210,12 +180,7 @@ class NoticeControllerTest {
|
||||
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");
|
||||
System.out.println("Test DELETE /notices/{id} (własne ogłoszenie) zakończony sukcesem");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,11 +5,11 @@ 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.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
@@ -28,20 +28,16 @@ import static org.mockito.Mockito.*;
|
||||
class NoticeServiceTest {
|
||||
|
||||
@Mock
|
||||
private final NoticeRepository noticeRepository = mock(NoticeRepository.class);
|
||||
private NoticeRepository noticeRepository;
|
||||
|
||||
@Mock
|
||||
private final ClientRepository clientRepository = mock(ClientRepository.class);
|
||||
|
||||
private ClientRepository clientRepository;
|
||||
|
||||
@Mock
|
||||
private final AttributesRepository attributesRepository = mock(AttributesRepository.class);
|
||||
private AttributesRepository attributesRepository;
|
||||
|
||||
@Mock
|
||||
private final AttributeValuesRepository attributeValuesRepository = mock(AttributeValuesRepository.class);
|
||||
|
||||
@Mock
|
||||
private final AttributesNoticeRepository attributesNoticeRepository = mock(AttributesNoticeRepository.class);
|
||||
private AttributeValuesRepository attributeValuesRepository;
|
||||
|
||||
@InjectMocks
|
||||
private NoticeService noticeService;
|
||||
@@ -52,7 +48,7 @@ class NoticeServiceTest {
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
System.out.println("Inicjalizacja danych testowych przed każdym testem");
|
||||
System.out.println("Przygotowanie danych testowych...");
|
||||
|
||||
sampleClient = new Client();
|
||||
sampleClient.setId(1L);
|
||||
@@ -78,66 +74,52 @@ class NoticeServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Pobranie wszystkich ogłoszeń - powinno zwrócić listę ogłoszeń")
|
||||
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ń");
|
||||
System.out.println("Test pobrania wszystkich ogłoszeń zakończony");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Pobranie ogłoszenia po ID - gdy istnieje")
|
||||
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");
|
||||
System.out.println("Test pobrania istniejącego ogłoszenia zakończony");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Pobranie ogłoszenia po ID - gdy nie istnieje")
|
||||
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");
|
||||
System.out.println("Test pobrania nieistniejącego ogłoszenia zakończony");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Dodanie nowego ogłoszenia - poprawne dane")
|
||||
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");
|
||||
System.out.println("Test dodania ogłoszenia zakończony");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Dodanie ogłoszenia z atrybutami")
|
||||
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");
|
||||
@@ -151,50 +133,39 @@ class NoticeServiceTest {
|
||||
Long result = noticeService.addNotice(sampleNoticeRequest);
|
||||
|
||||
assertNotNull(result);
|
||||
verify(attributesNoticeRepository, times(1)).save(any(AttributesNotice.class));
|
||||
|
||||
System.out.println("Pomyślnie zapisano atrybuty ogłoszenia");
|
||||
System.out.println("Test dodania ogłoszenia z atrybutami zakończony");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@DisplayName("Usunięcie istniejącego ogłoszenia")
|
||||
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");
|
||||
System.out.println("Test usunięcia ogłoszenia zakończony");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Sprawdzenie właściciela ogłoszenia - gdy należy do klienta")
|
||||
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");
|
||||
System.out.println("Test sprawdzenia właściciela (true) zakończony");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Boostowanie ogłoszenia - aktualizacja daty publikacji")
|
||||
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");
|
||||
System.out.println("Test boostowania ogłoszenia zakończony");
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import _11.asktpk.artisanconnectbackend.security.JwtUtil;
|
||||
import _11.asktpk.artisanconnectbackend.utils.Tools;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
@@ -25,7 +26,10 @@ class ToolsTest {
|
||||
private Tools tools;
|
||||
|
||||
@Test
|
||||
@DisplayName("Pobieranie ID klienta z requestu - powinno zwrócić ID gdy token jest poprawny")
|
||||
void getClientIdFromRequest_shouldReturnClientIdWhenTokenValid() {
|
||||
System.out.println("Rozpoczęcie testu getClientIdFromRequest_shouldReturnClientIdWhenTokenValid");
|
||||
|
||||
String token = "valid.token.here";
|
||||
Long expectedClientId = 1L;
|
||||
|
||||
@@ -35,6 +39,7 @@ class ToolsTest {
|
||||
Long result = tools.getClientIdFromRequest(request);
|
||||
|
||||
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 jakarta.servlet.http.HttpServletRequest;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
@@ -50,11 +51,15 @@ class WishlistControllerTest {
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
System.out.println("[Konfiguracja] Przygotowanie środowiska testowego...");
|
||||
when(tools.getClientIdFromRequest(request)).thenReturn(testClientId);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Dodanie/Usunięcie z wishlisty - powinno zwrócić sukces gdy ogłoszenie istnieje")
|
||||
void toggleWishlist_shouldReturnSuccessWhenNoticeExists() {
|
||||
System.out.println("Rozpoczęcie testu toggleWishlist_shouldReturnSuccessWhenNoticeExists");
|
||||
|
||||
NoticeResponseDTO noticeResponse = new NoticeResponseDTO();
|
||||
noticeResponse.setNoticeId(testNoticeId);
|
||||
|
||||
@@ -65,24 +70,34 @@ class WishlistControllerTest {
|
||||
|
||||
ResponseEntity<RequestResponseDTO> response = wishlistController.toggleWishlist(testNoticeId, request);
|
||||
|
||||
assertEquals(200, response.getStatusCodeValue());
|
||||
assertEquals(200, response.getStatusCode().value());
|
||||
assertNotNull(response.getBody());
|
||||
assertEquals("Wishlist entry added", response.getBody().getMessage());
|
||||
|
||||
System.out.println("Test zakończony powodzeniem: Poprawnie obsłużono dodanie/usunięcie z wishlisty");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Dodanie/Usunięcie z wishlisty - powinno zwrócić błąd gdy ogłoszenie nie istnieje")
|
||||
void toggleWishlist_shouldReturnBadRequestWhenNoticeNotFound() {
|
||||
System.out.println("Rozpoczęcie testu toggleWishlist_shouldReturnBadRequestWhenNoticeNotFound");
|
||||
|
||||
when(noticeService.getNoticeById(testNoticeId)).thenReturn(null);
|
||||
|
||||
ResponseEntity<RequestResponseDTO> response = wishlistController.toggleWishlist(testNoticeId, request);
|
||||
|
||||
assertEquals(400, response.getStatusCodeValue());
|
||||
assertEquals(400, response.getStatusCode().value());
|
||||
assertNotNull(response.getBody());
|
||||
assertEquals("Notice not found", response.getBody().getMessage());
|
||||
|
||||
System.out.println("Test zakończony powodzeniem: Poprawnie obsłużono brak ogłoszenia");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Pobieranie wishlisty - powinno zwrócić listę ogłoszeń")
|
||||
void getWishlistForClient_shouldReturnNoticeList() {
|
||||
System.out.println("Rozpoczęcie testu getWishlistForClient_shouldReturnNoticeList");
|
||||
|
||||
NoticeResponseDTO noticeResponse = new NoticeResponseDTO();
|
||||
noticeResponse.setNoticeId(testNoticeId);
|
||||
|
||||
@@ -93,15 +108,22 @@ class WishlistControllerTest {
|
||||
assertNotNull(result);
|
||||
assertEquals(1, result.size());
|
||||
assertEquals(testNoticeId, result.getFirst().getNoticeId());
|
||||
|
||||
System.out.println("Test zakończony powodzeniem: Poprawnie pobrano listę ogłoszeń");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Pobieranie wishlisty - powinno zwrócić pustą listę gdy brak wpisów")
|
||||
void getWishlistForClient_shouldReturnEmptyListWhenNoEntries() {
|
||||
System.out.println("Rozpoczęcie testu getWishlistForClient_shouldReturnEmptyListWhenNoEntries");
|
||||
|
||||
when(wishlistService.getNoticesInWishlist(testClientId)).thenReturn(Collections.emptyList());
|
||||
|
||||
List<NoticeResponseDTO> result = wishlistController.getWishlistForClient(request);
|
||||
|
||||
assertNotNull(result);
|
||||
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.WishlistService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
@@ -40,6 +41,8 @@ class WishlistServiceTest {
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
System.out.println("Przygotowanie danych testowych...");
|
||||
|
||||
testClient = new Client();
|
||||
testClient.setId(1L);
|
||||
testClient.setEmail("test@example.com");
|
||||
@@ -52,10 +55,15 @@ class WishlistServiceTest {
|
||||
testWishlist.setId(1L);
|
||||
testWishlist.setClient(testClient);
|
||||
testWishlist.setNotice(testNotice);
|
||||
|
||||
System.out.println("[Konfiguracja] Dane testowe gotowe");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Przełączanie wishlisty - powinno dodać gdy wpis nie istnieje")
|
||||
void toggleWishlist_shouldAddWhenNotExists() {
|
||||
System.out.println("Rozpoczęcie testu toggleWishlist_shouldAddWhenNotExists");
|
||||
|
||||
when(wishlistRepository.findByClientAndNotice(testClient, testNotice)).thenReturn(Optional.empty());
|
||||
when(wishlistRepository.save(any(Wishlist.class))).thenReturn(testWishlist);
|
||||
|
||||
@@ -63,20 +71,30 @@ class WishlistServiceTest {
|
||||
|
||||
assertTrue(result);
|
||||
verify(wishlistRepository, times(1)).save(any(Wishlist.class));
|
||||
|
||||
System.out.println("Test zakończony powodzeniem: Poprawnie dodano do wishlisty");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Przełączanie wishlisty - powinno usunąć gdy wpis istnieje")
|
||||
void toggleWishlist_shouldRemoveWhenExists() {
|
||||
System.out.println("Rozpoczęcie testu 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);
|
||||
|
||||
System.out.println("Test zakończony powodzeniem: Poprawnie usunięto z wishlisty");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Pobieranie ogłoszeń z wishlisty - powinno zwrócić listę ogłoszeń")
|
||||
void getNoticesInWishlist_shouldReturnNoticeList() {
|
||||
System.out.println("Rozpoczęcie testu getNoticesInWishlist_shouldReturnNoticeList");
|
||||
|
||||
List<Wishlist> wishlistEntries = new ArrayList<>();
|
||||
wishlistEntries.add(testWishlist);
|
||||
|
||||
@@ -87,15 +105,22 @@ class WishlistServiceTest {
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(1, result.size());
|
||||
|
||||
System.out.println(" Test zakończony powodzeniem: Poprawnie zwrócono listę ogłoszeń");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Pobieranie ogłoszeń z wishlisty - powinno zwrócić pustą listę gdy brak wpisów")
|
||||
void getNoticesInWishlist_shouldReturnEmptyListWhenNoEntries() {
|
||||
System.out.println("Rozpoczęcie testu getNoticesInWishlist_shouldReturnEmptyListWhenNoEntries");
|
||||
|
||||
when(wishlistRepository.findAllByClientId(1L)).thenReturn(new ArrayList<>());
|
||||
|
||||
List<NoticeResponseDTO> result = wishlistService.getNoticesInWishlist(1L);
|
||||
|
||||
assertNotNull(result);
|
||||
assertTrue(result.isEmpty());
|
||||
|
||||
System.out.println("Test zakończony powodzeniem: Poprawnie zwrócono pustą listę");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user