Testy
This commit is contained in:
10
pom.xml
10
pom.xml
@@ -46,6 +46,16 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
||||
@@ -1,25 +1,36 @@
|
||||
package _11.asktpk.artisanconnectbackend;
|
||||
|
||||
import _11.asktpk.artisanconnectbackend.dto.ClientDTO;
|
||||
import _11.asktpk.artisanconnectbackend.entities.Client;
|
||||
import _11.asktpk.artisanconnectbackend.repository.ClientRepository;
|
||||
import _11.asktpk.artisanconnectbackend.service.ClientService;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static _11.asktpk.artisanconnectbackend.utils.Enums.Role.USER;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
|
||||
|
||||
@SpringBootTest
|
||||
class ArtisanConnectBackendApplicationTests {
|
||||
private static final Logger logger = LogManager.getLogger(ArtisanConnectBackendApplicationTests.class);
|
||||
|
||||
// @Test
|
||||
// void testPostgresDatabase() {
|
||||
// postgresDatabase.add(new Notice("Test Notice", "Username", "Test Description"));
|
||||
// Boolean isRecordAvailable = postgresDatabase.get().size() > 0;
|
||||
// if(isRecordAvailable) {
|
||||
// logger.info("The record is available in the database");
|
||||
// } else {
|
||||
// logger.error("The record is not available in the database");
|
||||
// }
|
||||
// assert isRecordAvailable;
|
||||
// }
|
||||
//
|
||||
|
||||
// @Test
|
||||
// void getAllNotices() throws IOException {
|
||||
// OkHttpClient client = new OkHttpClient().newBuilder()
|
||||
@@ -30,4 +41,182 @@ class ArtisanConnectBackendApplicationTests {
|
||||
// .build();
|
||||
// Response response = client.newCall(request).execute();
|
||||
// }
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Testy jednostkowe serwisu klienta")
|
||||
class ClientServiceUnitTest {
|
||||
|
||||
private ClientRepository clientRepository;
|
||||
private ClientService clientService;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
logger.info("Inicjalizacja testu jednostkowego ClientService");
|
||||
clientRepository = mock(ClientRepository.class);
|
||||
clientService = new ClientService(clientRepository);
|
||||
logger.info("Utworzono mock ClientRepository i zainicjalizowano ClientService");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Powinien zwrócić listę zmapowanych klientów na DTO")
|
||||
void getAllClientsShouldReturnMappedDTOs() {
|
||||
logger.info("Tworzenie testowych danych");
|
||||
List<Client> mockClients = List.of(
|
||||
createTestClient("Jan", "Kowalski"),
|
||||
createTestClient("Anna", "Nowak")
|
||||
);
|
||||
logger.info("Utworzono mockową listę z {} klientami", mockClients.size());
|
||||
when(clientRepository.findAll()).thenReturn(mockClients);
|
||||
|
||||
|
||||
logger.info("Wywołanie metody getAllClients");
|
||||
List<ClientDTO> result = clientService.getAllClients();
|
||||
|
||||
assertThat(result)
|
||||
.hasSize(2)
|
||||
.extracting(ClientDTO::getFirstName)
|
||||
.containsExactly("Jan", "Anna");
|
||||
|
||||
verify(clientRepository).findAll();
|
||||
logger.info("Test wykonany poprawnie");
|
||||
|
||||
}
|
||||
|
||||
private Client createTestClient(String firstName, String lastName) {
|
||||
logger.debug("Tworzenie klienta testowego: {} {}", firstName, lastName);
|
||||
Client client = new Client();
|
||||
client.setFirstName(firstName);
|
||||
client.setLastName(lastName);
|
||||
client.setEmail(firstName.toLowerCase() + "." + lastName.toLowerCase() + "@example.com");
|
||||
client.setRole(USER);
|
||||
return client;
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@AutoConfigureTestDatabase
|
||||
@DisplayName("Testy kontrolera klienta")
|
||||
class ClientControllerTest {
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Autowired //Pawelkiewicz final
|
||||
private ClientRepository clientRepository;
|
||||
|
||||
private TestRestTemplate restTemplate;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
restTemplate = new TestRestTemplate();
|
||||
logger.info("Inicjalizacja testu - port: {}", port);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Powinien zaktualizować dane klienta")
|
||||
void shouldUpdateClientData() {
|
||||
logger.info("Start testu aktualizacji klienta");
|
||||
|
||||
logger.info("Przygotowanie danych testowych...");
|
||||
Client existingClient = new Client();
|
||||
existingClient.setFirstName("John");
|
||||
existingClient.setLastName("Doe");
|
||||
existingClient.setEmail("john.doe@example.com");
|
||||
existingClient.setRole(USER);
|
||||
existingClient.setImage("null");
|
||||
|
||||
Client savedClient = clientRepository.save(existingClient);
|
||||
logger.info("Utworzono testowego klienta z ID: {}", savedClient.getId());
|
||||
|
||||
ClientDTO updateData = new ClientDTO();
|
||||
updateData.setEmail("dignissim.tempor.arcu@aol.ca");
|
||||
updateData.setFirstName("Peter");
|
||||
updateData.setLastName("Harrison");
|
||||
updateData.setImage("null");
|
||||
updateData.setRole(USER);
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
|
||||
HttpEntity<ClientDTO> requestEntity = new HttpEntity<>(updateData, headers);
|
||||
logger.info("Przygotowano dane do aktualizacji: {}", updateData);
|
||||
|
||||
logger.info("Wysyłanie żądania PUT...");
|
||||
ResponseEntity<ClientDTO> response = restTemplate.exchange(
|
||||
createURLWithPort("/api/v1/clients/edit/" + savedClient.getId()),
|
||||
HttpMethod.PUT,
|
||||
requestEntity,
|
||||
ClientDTO.class
|
||||
);
|
||||
|
||||
logger.info("Weryfikacja odpowiedzi...");
|
||||
logger.info("Status odpowiedzi: {}", response.getStatusCode());
|
||||
assertThat(response.getStatusCode()).isEqualTo(org.springframework.http.HttpStatus.OK);
|
||||
assertThat(response.getBody()).isNotNull();
|
||||
assertThat(response.getBody().getFirstName()).isEqualTo("Peter");
|
||||
assertThat(response.getBody().getLastName()).isEqualTo("Harrison");
|
||||
assertThat(response.getBody().getEmail()).isEqualTo("dignissim.tempor.arcu@aol.ca");
|
||||
|
||||
// sprawdzenie w bazie
|
||||
logger.info("Weryfikacja danych w bazie...");
|
||||
|
||||
Client updatedClient = clientRepository.findById(savedClient.getId()).orElseThrow();
|
||||
assertThat(updatedClient.getFirstName()).isEqualTo("Peter");
|
||||
assertThat(updatedClient.getLastName()).isEqualTo("Harrison");
|
||||
assertThat(updatedClient.getEmail()).isEqualTo("dignissim.tempor.arcu@aol.ca");
|
||||
|
||||
logger.info("Test zakończony pomyślnie");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Powinien zwrócić 404 przy próbie aktualizacji nieistniejącego klienta")
|
||||
void shouldReturn404WhenUpdatingNonExistingClient() {
|
||||
logger.info("Start testu dla nieistniejącego klienta");
|
||||
|
||||
// given
|
||||
ClientDTO updateData = new ClientDTO();
|
||||
updateData.setEmail("test@example.com");
|
||||
updateData.setFirstName("Test");
|
||||
updateData.setLastName("Test");
|
||||
updateData.setRole(USER);
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
|
||||
HttpEntity<ClientDTO> requestEntity = new HttpEntity<>(updateData, headers);
|
||||
logger.info("Przygotowano dane do aktualizacji dla nieistniejącego ID: 99999");
|
||||
|
||||
|
||||
// when
|
||||
logger.info("Wysyłanie żądania PUT...");
|
||||
|
||||
ResponseEntity<ClientDTO> response = restTemplate.exchange(
|
||||
createURLWithPort("/api/v1/clients/edit/99999"),
|
||||
HttpMethod.PUT,
|
||||
requestEntity,
|
||||
ClientDTO.class
|
||||
);
|
||||
|
||||
// then
|
||||
logger.info("Otrzymany status odpowiedzi: {}", response.getStatusCode());
|
||||
assertThat(response.getStatusCode()).isEqualTo(org.springframework.http.HttpStatus.NOT_FOUND);
|
||||
logger.info("Test zakończony pomyślnie");
|
||||
|
||||
}
|
||||
|
||||
private String createURLWithPort(String uri) {
|
||||
return "http://localhost:" + port + uri;
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void cleanup() {
|
||||
clientRepository.deleteAll();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user