|
| 1 | +package com.xpeho.spring_boot_java_random_user.presentation; |
| 2 | + |
| 3 | +import com.xpeho.spring_boot_java_random_user.data.models.database.User; |
| 4 | +import com.xpeho.spring_boot_java_random_user.data.sources.database.UserRepository; |
| 5 | +import com.xpeho.spring_boot_java_random_user.domain.entities.UserEntity; |
| 6 | +import org.junit.jupiter.api.BeforeEach; |
| 7 | +import org.junit.jupiter.api.DisplayName; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; |
| 10 | +import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate; |
| 11 | +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; |
| 12 | +import org.springframework.boot.test.context.SpringBootTest; |
| 13 | +import org.springframework.boot.resttestclient.TestRestTemplate; |
| 14 | +import org.springframework.http.HttpStatus; |
| 15 | +import org.springframework.http.ResponseEntity; |
| 16 | +import org.springframework.test.context.ActiveProfiles; |
| 17 | +import org.testcontainers.containers.PostgreSQLContainer; |
| 18 | +import org.testcontainers.junit.jupiter.Container; |
| 19 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 20 | + |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | + |
| 23 | +@Testcontainers |
| 24 | +@AutoConfigureTestRestTemplate |
| 25 | +@ActiveProfiles("test") |
| 26 | +@SpringBootTest( |
| 27 | + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, |
| 28 | + properties = { |
| 29 | + "spring.sql.init.mode=never", |
| 30 | + "spring.jpa.hibernate.ddl-auto=create-drop" |
| 31 | + } |
| 32 | +) |
| 33 | +class UserGetByIdContainerTest { |
| 34 | + |
| 35 | + @Container |
| 36 | + @ServiceConnection |
| 37 | + static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:16-alpine"); |
| 38 | + |
| 39 | + @Autowired |
| 40 | + private TestRestTemplate restTemplate; |
| 41 | + |
| 42 | + @Autowired |
| 43 | + private UserRepository userRepository; |
| 44 | + |
| 45 | + @BeforeEach |
| 46 | + void setUp() { |
| 47 | + userRepository.deleteAll(); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + @DisplayName("GET /random-users/{id} should return 200 with persisted user") |
| 52 | + void shouldReturnUserByIdWhenUserExists() { |
| 53 | + User user = new User(); |
| 54 | + user.setGender("female"); |
| 55 | + user.setFirstname("Jane"); |
| 56 | + user.setLastname("Doe"); |
| 57 | + user.setCivility("Ms"); |
| 58 | + user.setEmail("jane.doe@example.com"); |
| 59 | + user.setPhone("0600000000"); |
| 60 | + user.setPicture("https://example.com/jane.jpg"); |
| 61 | + user.setNationality("FR"); |
| 62 | + |
| 63 | + User saved = userRepository.saveAndFlush(user); |
| 64 | + |
| 65 | + ResponseEntity<UserEntity> response = restTemplate.getForEntity( |
| 66 | + "/random-users/{id}", |
| 67 | + UserEntity.class, |
| 68 | + saved.getId() |
| 69 | + ); |
| 70 | + |
| 71 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); |
| 72 | + assertThat(response.getBody()).isNotNull(); |
| 73 | + assertThat(response.getBody().id()).isEqualTo(saved.getId()); |
| 74 | + assertThat(response.getBody().firstname()).isEqualTo("Jane"); |
| 75 | + assertThat(response.getBody().nat()).isEqualTo("FR"); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + @DisplayName("GET /random-users/{id} should return 404 when user does not exist") |
| 80 | + void shouldReturnNotFoundWhenUserDoesNotExist() { |
| 81 | + ResponseEntity<UserEntity> response = restTemplate.getForEntity( |
| 82 | + "/random-users/{id}", |
| 83 | + UserEntity.class, |
| 84 | + -1 |
| 85 | + ); |
| 86 | + |
| 87 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); |
| 88 | + } |
| 89 | +} |
0 commit comments