1 | package com.example.rezevirajmasa.demo;
|
---|
2 |
|
---|
3 | import com.example.rezevirajmasa.demo.model.Customer;
|
---|
4 | import com.example.rezevirajmasa.demo.model.MembershipLevel;
|
---|
5 | import com.example.rezevirajmasa.demo.model.exceptions.InvalidCustomerIdException;
|
---|
6 | import com.example.rezevirajmasa.demo.repository.CustomerRepository;
|
---|
7 |
|
---|
8 | import org.assertj.core.api.Assertions;
|
---|
9 |
|
---|
10 | import org.junit.jupiter.api.Test;
|
---|
11 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
12 | import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
|
---|
13 | import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
---|
14 | import org.springframework.test.annotation.Rollback;
|
---|
15 |
|
---|
16 | import java.util.List;
|
---|
17 |
|
---|
18 | @DataJpaTest
|
---|
19 | @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
|
---|
20 | @Rollback(false)
|
---|
21 | public class CustomerRepositoryTests {
|
---|
22 | @Autowired private CustomerRepository customerRepository;
|
---|
23 |
|
---|
24 | @Test
|
---|
25 | public void testAddNew(){
|
---|
26 | Customer customer = new Customer();
|
---|
27 | customer.setFirstName("Aleksandar");
|
---|
28 | customer.setLastName("Panovski");
|
---|
29 | customer.setEmail("lfc@liverpoo.com");
|
---|
30 | customer.setAddress("5th Avenue, NY");
|
---|
31 | customer.setPhone("+1 7743831817");
|
---|
32 | customer.setMembershipLevel(MembershipLevel.GOLD);
|
---|
33 |
|
---|
34 | Customer savedCustomer = customerRepository.save(customer);
|
---|
35 |
|
---|
36 | Assertions.assertThat(savedCustomer).isNotNull();
|
---|
37 | Assertions.assertThat(savedCustomer.getCustomerId()).isGreaterThan(0);
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Test
|
---|
41 | public void findById() {
|
---|
42 | Customer customer = customerRepository.findById(Long.valueOf(1)).orElseThrow(InvalidCustomerIdException::new);
|
---|
43 | System.out.println(customer);
|
---|
44 | }
|
---|
45 |
|
---|
46 | @Test
|
---|
47 | public void testListAll() {
|
---|
48 | List<Customer> customerList = customerRepository.findAll();
|
---|
49 | Assertions.assertThat(customerList).hasSizeGreaterThan(0);
|
---|
50 |
|
---|
51 | for (Customer customer : customerList) {
|
---|
52 | System.out.println(customer);
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | @Test
|
---|
57 | public void testUpdate() {
|
---|
58 | Customer customer = customerRepository.findById(2L).orElseThrow(InvalidCustomerIdException::new);
|
---|
59 | customer.setPhone("+38978450442");
|
---|
60 | customerRepository.save(customer);
|
---|
61 |
|
---|
62 | Customer updatedCustomer = customerRepository.findById(2L).get();
|
---|
63 | Assertions.assertThat(updatedCustomer.getEmail().equals("apano74@gmail.com"));
|
---|
64 | }
|
---|
65 | }
|
---|