/
AccountRepositoryIT.java
90 lines (80 loc) · 4.05 KB
/
AccountRepositoryIT.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package io.hexlet.typoreporter.repository;
import com.github.database.rider.core.api.configuration.DBUnit;
import com.github.database.rider.core.api.dataset.DataSet;
import com.github.database.rider.spring.api.DBRider;
import io.hexlet.typoreporter.config.audit.AuditConfiguration;
import io.hexlet.typoreporter.domain.account.Account;
import io.hexlet.typoreporter.domain.typo.Typo;
import io.hexlet.typoreporter.domain.workspace.Workspace;
import io.hexlet.typoreporter.test.DBUnitEnumPostgres;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.Import;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.transaction.annotation.Transactional;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import static com.github.database.rider.core.api.configuration.Orthography.LOWERCASE;
import static io.hexlet.typoreporter.test.Constraints.POSTGRES_IMAGE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
@DataJpaTest
@Testcontainers
@Import(AuditConfiguration.class)
@Transactional
@DBRider
@DBUnit(caseInsensitiveStrategy = LOWERCASE, dataTypeFactoryClass = DBUnitEnumPostgres.class, cacheConnection = false)
@DataSet(value = {"accounts.yml", "workspaces.yml"})
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class AccountRepositoryIT {
@Container
public static PostgreSQLContainer<?> postgreSQLContainer = new PostgreSQLContainer<>(POSTGRES_IMAGE)
.withPassword("inmemory")
.withUsername("inmemory");
@Autowired
private AccountRepository accountRepository;
@Autowired
private WorkspaceRepository workspaceRepository;
@DynamicPropertySource
static void datasourceProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgreSQLContainer::getJdbcUrl);
registry.add("spring.datasource.password", postgreSQLContainer::getPassword);
registry.add("spring.datasource.username", postgreSQLContainer::getUsername);
}
@ParameterizedTest
@MethodSource("io.hexlet.typoreporter.test.factory.EntitiesFactory#getAccountEmailExist")
void getAccountByEmail(final String email) {
final var account = accountRepository.findAccountByEmail(email);
assertThat(account).isNotEmpty();
assertThat(account.map(Account::getEmail).orElseThrow()).isEqualTo(email);
}
@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = "invalid-email")
void getAccountByEmailNotExist(final String email) {
assertThat(accountRepository.findAccountByEmail(email)).isEmpty();
}
@Test
void getPageAccountByWorkspaceName() {
Workspace wks = workspaceRepository.findAll().stream().findFirst().get();
accountRepository.findAll().forEach(account -> account.setWorkspace(wks));
Pageable pageable = PageRequest.of(0, 10, Sort.by("id"));
Page<Account> page = accountRepository.findPageAccountByWorkspaceName(pageable, wks.getName());
assertThat(page).isNotEmpty();
assertThat(page.getTotalElements()).isEqualTo(accountRepository.count());
assertThat(page.getTotalPages()).isEqualTo(1);
}
}