Testy
This commit is contained in:
@@ -23,6 +23,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||||
|
import org.springframework.dao.DataIntegrityViolationException;
|
||||||
import org.springframework.http.*;
|
import org.springframework.http.*;
|
||||||
import _11.asktpk.artisanconnectbackend.entities.Image;
|
import _11.asktpk.artisanconnectbackend.entities.Image;
|
||||||
import _11.asktpk.artisanconnectbackend.repository.ImageRepository;
|
import _11.asktpk.artisanconnectbackend.repository.ImageRepository;
|
||||||
@@ -36,6 +37,7 @@ import org.springframework.web.multipart.MultipartFile;
|
|||||||
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -57,34 +59,30 @@ class ArtisanConnectBackendApplicationTests {
|
|||||||
private static final Logger logger = LogManager.getLogger(ArtisanConnectBackendApplicationTests.class);
|
private static final Logger logger = LogManager.getLogger(ArtisanConnectBackendApplicationTests.class);
|
||||||
|
|
||||||
@LocalServerPort
|
@LocalServerPort
|
||||||
private int port;
|
private final int port;
|
||||||
|
|
||||||
|
private final ClientService clientService;
|
||||||
|
private final TestRestTemplate restTemplate;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ClientRepository clientRepository;
|
public ArtisanConnectBackendApplicationTests(ClientService clientService, @LocalServerPort int port) {
|
||||||
|
this.clientService = clientService;
|
||||||
@Autowired
|
this.port = port;
|
||||||
private ClientService clientService;
|
this.restTemplate = new TestRestTemplate();
|
||||||
|
|
||||||
private TestRestTemplate restTemplate;
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
void setUp() {
|
|
||||||
restTemplate = new TestRestTemplate();
|
|
||||||
logger.info("Inicjalizacja komponentów testowych na porcie: {}", port);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Nested
|
@Nested
|
||||||
@DisplayName("Testy jednostkowe ClientService")
|
@DisplayName("Testy jednostkowe ClientService")
|
||||||
class ClientServiceTest {
|
class ClientServiceTest {
|
||||||
|
|
||||||
private ClientRepository clientRepository;
|
private final ClientRepository clientRepository;
|
||||||
private ClientService clientService;
|
private final ClientService clientService;
|
||||||
|
|
||||||
@BeforeEach
|
ClientServiceTest() {
|
||||||
void setUp() {
|
|
||||||
logger.info("Inicjalizacja mocków dla ClientService");
|
logger.info("Inicjalizacja mocków dla ClientService");
|
||||||
clientRepository = mock(ClientRepository.class);
|
this.clientRepository = mock(ClientRepository.class);
|
||||||
clientService = new ClientService(clientRepository);
|
this.clientService = new ClientService(clientRepository);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -117,36 +115,84 @@ class ArtisanConnectBackendApplicationTests {
|
|||||||
@DisplayName("Testy integracyjne ClientController")
|
@DisplayName("Testy integracyjne ClientController")
|
||||||
class ClientControllerTest {
|
class ClientControllerTest {
|
||||||
|
|
||||||
|
private final int port;
|
||||||
|
private final TestRestTemplate restTemplate;
|
||||||
|
private final ClientService clientService;
|
||||||
|
private final NoticeService noticeService;
|
||||||
|
private final NoticeRepository noticeRepository;
|
||||||
|
private final Logger logger = LogManager.getLogger(ClientControllerTest.class);
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public ClientControllerTest(
|
||||||
|
@LocalServerPort int port,
|
||||||
|
TestRestTemplate restTemplate,
|
||||||
|
ClientService clientService,
|
||||||
|
NoticeService noticeService,
|
||||||
|
NoticeRepository noticeRepository) {
|
||||||
|
this.port = port;
|
||||||
|
this.restTemplate = restTemplate;
|
||||||
|
this.clientService = clientService;
|
||||||
|
this.noticeService = noticeService;
|
||||||
|
this.noticeRepository = noticeRepository;
|
||||||
|
}
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void cleanDatabase() {
|
void cleanDatabase() {
|
||||||
clientRepository.deleteAll();
|
|
||||||
|
noticeRepository.deleteAll();
|
||||||
|
|
||||||
|
clientService.getAllClients().forEach(client -> {
|
||||||
|
try {
|
||||||
|
clientService.deleteClient(client.getId());
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("Błąd podczas usuwania klienta: {}", e.getMessage());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean hasNotices(Long clientId) {
|
||||||
|
return noticeService.getAllNotices().stream()
|
||||||
|
.anyMatch(notice -> notice.getClientId().equals(clientId));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Powinien poprawnie dodawać klienta")
|
@DisplayName("Powinien poprawnie usunąć klienta z powiązanymi ogłoszeniami")
|
||||||
void shouldAddClientSuccessfully() {
|
void shouldDeleteClientWithNotices() {
|
||||||
ClientDTO newClient = createTestDTO("new.email@example.com", "John", "Smith");
|
ClientDTO client = clientService.addClient(createTestDTO("client@example.com", "Jan", "Kowalski"));
|
||||||
|
|
||||||
ResponseEntity<ClientDTO> response = restTemplate.postForEntity(
|
NoticeDTO notice = new NoticeDTO();
|
||||||
createURLWithPort("/api/v1/clients/add"),
|
notice.setClientId(client.getId());
|
||||||
newClient,
|
notice.setTitle("Test Notice");
|
||||||
ClientDTO.class
|
Long noticeId = noticeService.addNotice(notice);
|
||||||
|
|
||||||
|
ResponseEntity<Void> deleteNoticeResponse = restTemplate.exchange(
|
||||||
|
createURLWithPort("/api/v1/notices/delete/" + noticeId),
|
||||||
|
HttpMethod.DELETE,
|
||||||
|
null,
|
||||||
|
Void.class
|
||||||
|
);
|
||||||
|
assertThat(deleteNoticeResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||||
|
|
||||||
|
ResponseEntity<Void> deleteClientResponse = restTemplate.exchange(
|
||||||
|
createURLWithPort("/api/v1/clients/delete/" + client.getId()),
|
||||||
|
HttpMethod.DELETE,
|
||||||
|
null,
|
||||||
|
Void.class
|
||||||
);
|
);
|
||||||
|
|
||||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.CREATED);
|
assertThat(deleteClientResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||||
assertThat(response.getBody()).isNotNull();
|
assertThat(clientService.clientExists(client.getId())).isFalse();
|
||||||
assertThat(response.getBody().getEmail()).isEqualTo("new.email@example.com");
|
assertThat(noticeService.noticeExists(noticeId)).isFalse();
|
||||||
|
|
||||||
assertThat(clientRepository.existsById(response.getBody().getId()));
|
|
||||||
logger.info("Pomyślnie dodano klienta");
|
|
||||||
}
|
}
|
||||||
|
@Autowired
|
||||||
|
private ClientRepository clientRepository;
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Powinien zwracać wszystkich klientów")
|
@DisplayName("Powinien zwracać wszystkich klientów")
|
||||||
void shouldReturnAllClients() {
|
void shouldReturnAllClients() {
|
||||||
logger.info("Dodawanie przykładowych klientów do testu...");
|
ClientDTO client1 = clientService.addClient(createTestDTO("client1@example.com", "Anna", "Nowak"));
|
||||||
clientRepository.save(createTestClient("client1@example.com", "Anna", "Nowak"));
|
ClientDTO client2 = clientService.addClient(createTestDTO("client2@example.com", "Adam", "Kowalski"));
|
||||||
clientRepository.save(createTestClient("client2@example.com", "Adam", "Kowalski"));
|
|
||||||
|
|
||||||
ResponseEntity<ClientDTO[]> response = restTemplate.getForEntity(
|
ResponseEntity<ClientDTO[]> response = restTemplate.getForEntity(
|
||||||
createURLWithPort("/api/v1/clients/get/all"),
|
createURLWithPort("/api/v1/clients/get/all"),
|
||||||
@@ -156,16 +202,46 @@ class ArtisanConnectBackendApplicationTests {
|
|||||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||||
assertThat(response.getBody()).isNotNull();
|
assertThat(response.getBody()).isNotNull();
|
||||||
assertThat(response.getBody()).hasSize(2);
|
assertThat(response.getBody()).hasSize(2);
|
||||||
logger.info("Wszyscy klienci poprawnie pobrani");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Client createTestClient(String email, String firstName, String lastName) {
|
|
||||||
Client client = new Client();
|
|
||||||
client.setEmail(email);
|
@Test
|
||||||
client.setFirstName(firstName);
|
@DisplayName("Powinien zwrócić błąd przy próbie usunięcia klienta z powiązanymi ogłoszeniami bez kaskady")
|
||||||
client.setLastName(lastName);
|
void shouldFailWhenDeletingClientWithNoticesWithoutCascade() {
|
||||||
client.setRole(USER);
|
noticeService.getAllNotices().forEach(n -> noticeService.deleteNotice(n.getNoticeId()));
|
||||||
return client;
|
clientService.getAllClients().forEach(c -> clientService.deleteClient(c.getId()));
|
||||||
|
|
||||||
|
ClientDTO client = clientService.addClient(createTestDTO("client@example.com", "Jan", "Kowalski"));
|
||||||
|
|
||||||
|
NoticeDTO notice = new NoticeDTO();
|
||||||
|
notice.setClientId(client.getId());
|
||||||
|
notice.setTitle("Test Notice");
|
||||||
|
noticeService.addNotice(notice);
|
||||||
|
|
||||||
|
try {
|
||||||
|
clientService.deleteClient(client.getId());
|
||||||
|
fail("Powinien zostać rzucony wyjątek DataIntegrityViolationException");
|
||||||
|
} catch (DataIntegrityViolationException e) {
|
||||||
|
// Oczekiwany wyjątek
|
||||||
|
assertThat(e.getMessage()).contains("could not execute statement");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Powinien poprawnie usunąć klienta bez powiązanych ogłoszeń")
|
||||||
|
void shouldDeleteClientWithoutNotices() {
|
||||||
|
ClientDTO client = clientService.addClient(createTestDTO("client@example.com", "Jan", "Kowalski"));
|
||||||
|
|
||||||
|
ResponseEntity<Void> deleteResponse = restTemplate.exchange(
|
||||||
|
createURLWithPort("/api/v1/clients/delete/" + client.getId()),
|
||||||
|
HttpMethod.DELETE,
|
||||||
|
null,
|
||||||
|
Void.class
|
||||||
|
);
|
||||||
|
|
||||||
|
assertThat(deleteResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||||
|
assertThat(clientService.clientExists(client.getId())).isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
private ClientDTO createTestDTO(String email, String firstName, String lastName) {
|
private ClientDTO createTestDTO(String email, String firstName, String lastName) {
|
||||||
@@ -186,22 +262,26 @@ class ArtisanConnectBackendApplicationTests {
|
|||||||
@DisplayName("Testy jednostkowe NoticeService")
|
@DisplayName("Testy jednostkowe NoticeService")
|
||||||
class NoticeServiceUnitTest {
|
class NoticeServiceUnitTest {
|
||||||
|
|
||||||
private NoticeRepository noticeRepository;
|
private final NoticeRepository noticeRepository;
|
||||||
private ClientRepository clientRepository;
|
private final ClientRepository clientRepository;
|
||||||
private NoticeService noticeService;
|
private final NoticeService noticeService;
|
||||||
|
|
||||||
@BeforeEach
|
NoticeServiceUnitTest() {
|
||||||
void setUp() {
|
this.noticeRepository = mock(NoticeRepository.class);
|
||||||
noticeRepository = mock(NoticeRepository.class);
|
this.clientRepository = mock(ClientRepository.class);
|
||||||
clientRepository = mock(ClientRepository.class);
|
this.noticeService = new NoticeService(
|
||||||
noticeService = new NoticeService(noticeRepository, clientRepository, null, null);
|
noticeRepository,
|
||||||
|
clientRepository,
|
||||||
|
null,
|
||||||
|
null
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Powinien poprawnie dodać ogłoszenie")
|
@DisplayName("Powinien poprawnie dodać ogłoszenie")
|
||||||
void shouldAddNoticeSuccessfully() {
|
void shouldAddNoticeSuccessfully() {
|
||||||
Client client = createTestClient("test@example.com", "Anna", "Kowalska");
|
Client client = createTestClient("test@example.com", "Anna", "Kowalska");
|
||||||
when(clientRepository.findById(1L)).thenReturn(java.util.Optional.of(client));
|
when(clientRepository.findById(1L)).thenReturn(Optional.of(client));
|
||||||
|
|
||||||
NoticeDTO noticeDTO = new NoticeDTO();
|
NoticeDTO noticeDTO = new NoticeDTO();
|
||||||
noticeDTO.setClientId(1L);
|
noticeDTO.setClientId(1L);
|
||||||
@@ -226,7 +306,7 @@ class ArtisanConnectBackendApplicationTests {
|
|||||||
NoticeDTO noticeDTO = new NoticeDTO();
|
NoticeDTO noticeDTO = new NoticeDTO();
|
||||||
noticeDTO.setClientId(1L);
|
noticeDTO.setClientId(1L);
|
||||||
|
|
||||||
when(clientRepository.findById(1L)).thenReturn(java.util.Optional.empty());
|
when(clientRepository.findById(1L)).thenReturn(Optional.empty());
|
||||||
|
|
||||||
assertThrows(EntityNotFoundException.class, () -> noticeService.addNotice(noticeDTO));
|
assertThrows(EntityNotFoundException.class, () -> noticeService.addNotice(noticeDTO));
|
||||||
}
|
}
|
||||||
@@ -241,17 +321,19 @@ class ArtisanConnectBackendApplicationTests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Nested
|
@Nested
|
||||||
@DisplayName("Testy integracyjne ImageService")
|
@DisplayName("Testy integracyjne ImageService")
|
||||||
class ImageServiceTest {
|
class ImageServiceTest {
|
||||||
|
|
||||||
private ImageRepository imageRepository;
|
private final ImageRepository imageRepository;
|
||||||
private ImageService imageService;
|
private final ImageService imageService;
|
||||||
|
|
||||||
@BeforeEach
|
ImageServiceTest() throws Exception {
|
||||||
void setUp() {
|
this.imageRepository = mock(ImageRepository.class);
|
||||||
imageRepository = mock(ImageRepository.class);
|
Constructor<ImageService> constructor = ImageService.class.getDeclaredConstructor(ImageRepository.class);
|
||||||
imageService = new ImageService(imageRepository);
|
constructor.setAccessible(true);
|
||||||
|
this.imageService = constructor.newInstance(imageRepository);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -351,15 +433,14 @@ class ArtisanConnectBackendApplicationTests {
|
|||||||
@DisplayName("Testy integracyjne WishlistService")
|
@DisplayName("Testy integracyjne WishlistService")
|
||||||
class WishlistServiceTest {
|
class WishlistServiceTest {
|
||||||
|
|
||||||
private WishlistRepository wishlistRepository;
|
private final WishlistRepository wishlistRepository;
|
||||||
private WishlistService wishlistService;
|
private final NoticeService noticeService;
|
||||||
private NoticeService noticeService;
|
private final WishlistService wishlistService;
|
||||||
|
|
||||||
@BeforeEach
|
WishlistServiceTest() {
|
||||||
void setUp() {
|
this.wishlistRepository = mock(WishlistRepository.class);
|
||||||
wishlistRepository = mock(WishlistRepository.class);
|
this.noticeService = mock(NoticeService.class);
|
||||||
noticeService = mock(NoticeService.class);
|
this.wishlistService = new WishlistService(wishlistRepository, noticeService);
|
||||||
wishlistService = new WishlistService(wishlistRepository, noticeService);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -459,11 +540,14 @@ class ArtisanConnectBackendApplicationTests {
|
|||||||
@DisplayName("Testy dla VariablesController")
|
@DisplayName("Testy dla VariablesController")
|
||||||
class VariablesControllerTest {
|
class VariablesControllerTest {
|
||||||
|
|
||||||
@LocalServerPort
|
private final int port;
|
||||||
private int port;
|
private final TestRestTemplate restTemplate;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private TestRestTemplate restTemplate;
|
public VariablesControllerTest(@LocalServerPort int port, TestRestTemplate restTemplate) {
|
||||||
|
this.port = port;
|
||||||
|
this.restTemplate = restTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Powinien zwrócić kategorie")
|
@DisplayName("Powinien zwrócić kategorie")
|
||||||
|
|||||||
Reference in New Issue
Block a user