|
| 1 | +package ca.bc.gov.farms.persistence.v1.dao; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.assertj.core.api.Assertions.assertThatNoException; |
| 5 | +import static org.junit.jupiter.api.Assertions.fail; |
| 6 | + |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import org.junit.jupiter.api.MethodOrderer; |
| 10 | +import org.junit.jupiter.api.Order; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import org.junit.jupiter.api.TestMethodOrder; |
| 13 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 14 | +import org.springframework.beans.factory.annotation.Autowired; |
| 15 | +import org.springframework.context.annotation.ComponentScan; |
| 16 | +import org.springframework.test.context.ContextConfiguration; |
| 17 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 18 | + |
| 19 | +import ca.bc.gov.brmb.common.persistence.dao.DaoException; |
| 20 | +import ca.bc.gov.farms.api.rest.v1.spring.EndpointsSpringConfigTest; |
| 21 | +import ca.bc.gov.farms.persistence.v1.dto.YearConfigurationParameterDto; |
| 22 | +import ca.bc.gov.farms.persistence.v1.spring.PersistenceSpringConfig; |
| 23 | + |
| 24 | +@ExtendWith(SpringExtension.class) |
| 25 | +@ComponentScan(basePackages = "ca.bc.gov.farms") |
| 26 | +@ContextConfiguration(classes = { EndpointsSpringConfigTest.class, PersistenceSpringConfig.class }) |
| 27 | +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) |
| 28 | +public class YearConfigurationParameterDaoTest { |
| 29 | + |
| 30 | + @Autowired |
| 31 | + private YearConfigurationParameterDao yearConfigurationParameterDao; |
| 32 | + |
| 33 | + private static Long yearConfigurationParameterId; |
| 34 | + |
| 35 | + @Test |
| 36 | + @Order(1) |
| 37 | + public void testInsert() { |
| 38 | + YearConfigurationParameterDto dto = new YearConfigurationParameterDto(); |
| 39 | + dto.setProgramYear(2023); |
| 40 | + dto.setParameterName("Payment Limitation - Percentage of Total Margin Decline"); |
| 41 | + dto.setParameterValue("70"); |
| 42 | + dto.setConfigParamTypeCode("DECIMAL"); |
| 43 | + |
| 44 | + YearConfigurationParameterDto result = null; |
| 45 | + try { |
| 46 | + yearConfigurationParameterDao.insert(dto, "testUser"); |
| 47 | + yearConfigurationParameterId = dto.getYearConfigurationParameterId(); |
| 48 | + result = yearConfigurationParameterDao.fetch(yearConfigurationParameterId); |
| 49 | + } catch (DaoException e) { |
| 50 | + fail(e.getMessage()); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + assertThat(result.getProgramYear()).isEqualTo(2023); |
| 55 | + assertThat(result.getParameterName()).isEqualTo("Payment Limitation - Percentage of Total Margin Decline"); |
| 56 | + assertThat(result.getParameterValue()).isEqualTo("70"); |
| 57 | + assertThat(result.getConfigParamTypeCode()).isEqualTo("DECIMAL"); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + @Order(2) |
| 62 | + public void testFetchAll() { |
| 63 | + List<YearConfigurationParameterDto> dtos = null; |
| 64 | + try { |
| 65 | + dtos = yearConfigurationParameterDao.fetchAll(); |
| 66 | + } catch (DaoException e) { |
| 67 | + fail(e.getMessage()); |
| 68 | + return; |
| 69 | + } |
| 70 | + assertThat(dtos).isNotNull(); |
| 71 | + assertThat(dtos).isNotEmpty(); |
| 72 | + assertThat(dtos.size()).isEqualTo(1); |
| 73 | + |
| 74 | + YearConfigurationParameterDto fetchedDto = dtos.get(0); |
| 75 | + assertThat(fetchedDto.getProgramYear()).isEqualTo(2023); |
| 76 | + assertThat(fetchedDto.getParameterName()).isEqualTo("Payment Limitation - Percentage of Total Margin Decline"); |
| 77 | + assertThat(fetchedDto.getParameterValue()).isEqualTo("70"); |
| 78 | + assertThat(fetchedDto.getConfigParamTypeCode()).isEqualTo("DECIMAL"); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + @Order(3) |
| 83 | + public void testUpdate() { |
| 84 | + YearConfigurationParameterDto dto = null; |
| 85 | + try { |
| 86 | + dto = yearConfigurationParameterDao.fetch(yearConfigurationParameterId); |
| 87 | + } catch (DaoException e) { |
| 88 | + fail(e.getMessage()); |
| 89 | + return; |
| 90 | + } |
| 91 | + assertThat(dto).isNotNull(); |
| 92 | + |
| 93 | + dto.setParameterValue("700"); |
| 94 | + |
| 95 | + YearConfigurationParameterDto result = null; |
| 96 | + try { |
| 97 | + yearConfigurationParameterDao.update(dto, "testUser"); |
| 98 | + result = yearConfigurationParameterDao.fetch(yearConfigurationParameterId); |
| 99 | + } catch (DaoException e) { |
| 100 | + fail(e.getMessage()); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + assertThat(result.getProgramYear()).isEqualTo(2023); |
| 105 | + assertThat(result.getParameterName()).isEqualTo("Payment Limitation - Percentage of Total Margin Decline"); |
| 106 | + assertThat(result.getParameterValue()).isEqualTo("700"); |
| 107 | + assertThat(result.getConfigParamTypeCode()).isEqualTo("DECIMAL"); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + @Order(4) |
| 112 | + public void testDelete() { |
| 113 | + assertThatNoException().isThrownBy(() -> { |
| 114 | + yearConfigurationParameterDao.delete(yearConfigurationParameterId); |
| 115 | + }); |
| 116 | + } |
| 117 | +} |
0 commit comments