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