|
| 1 | +package io.mosip.admin.bulkdataupload.batch; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | +import org.junit.runner.RunWith; |
| 5 | +import org.mockito.junit.MockitoJUnitRunner; |
| 6 | +import org.springframework.batch.extensions.excel.support.rowset.RowSet; |
| 7 | +import org.springframework.beans.NotWritablePropertyException; |
| 8 | +import org.springframework.boot.convert.ApplicationConversionService; |
| 9 | +import org.springframework.boot.test.context.SpringBootTest; |
| 10 | +import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext; |
| 11 | +import org.springframework.format.support.DefaultFormattingConversionService; |
| 12 | +import org.springframework.validation.BindException; |
| 13 | +import org.springframework.validation.DataBinder; |
| 14 | +import org.springframework.validation.DefaultBindingErrorProcessor; |
| 15 | +import org.springframework.validation.beanvalidation.CustomValidatorBean; |
| 16 | + |
| 17 | +import javax.validation.ConstraintViolationException; |
| 18 | +import java.util.HashSet; |
| 19 | +import java.util.Properties; |
| 20 | + |
| 21 | +import static org.junit.Assert.*; |
| 22 | +import static org.mockito.Mockito.mock; |
| 23 | +import static org.mockito.Mockito.when; |
| 24 | + |
| 25 | +@SpringBootTest |
| 26 | +@RunWith(MockitoJUnitRunner.class) |
| 27 | +public class CustomExcelRowMapperTest { |
| 28 | + |
| 29 | + @Test |
| 30 | + public void createBinder_withApplicationConversionService_returnSuccessResponse() { |
| 31 | + ApplicationConversionService conversionService = new ApplicationConversionService(); |
| 32 | + DataBinder actualCreateBinderResult = (new CustomExcelRowMapper<>(conversionService, new CustomValidatorBean())) |
| 33 | + .createBinder("Target"); |
| 34 | + |
| 35 | + assertNotNull(actualCreateBinderResult); |
| 36 | + assertFalse(actualCreateBinderResult.isIgnoreUnknownFields()); |
| 37 | + assertFalse(actualCreateBinderResult.isIgnoreInvalidFields()); |
| 38 | + assertTrue(actualCreateBinderResult.isAutoGrowNestedPaths()); |
| 39 | + assertNull(actualCreateBinderResult.getValidator()); |
| 40 | + assertEquals("Target", actualCreateBinderResult.getTarget()); |
| 41 | + assertEquals("target", actualCreateBinderResult.getObjectName()); |
| 42 | + assertTrue(actualCreateBinderResult.getBindingErrorProcessor() instanceof DefaultBindingErrorProcessor); |
| 43 | + assertEquals(256, actualCreateBinderResult.getAutoGrowCollectionLimit()); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void createBinder_withDefaultFormattingConversionService_returnSuccessResponse() { |
| 48 | + DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService(); |
| 49 | + |
| 50 | + DataBinder actualCreateBinderResult = (new CustomExcelRowMapper<>(conversionService, new CustomValidatorBean())) |
| 51 | + .createBinder("Target"); |
| 52 | + |
| 53 | + assertNotNull(actualCreateBinderResult); |
| 54 | + assertFalse(actualCreateBinderResult.isIgnoreUnknownFields()); |
| 55 | + assertFalse(actualCreateBinderResult.isIgnoreInvalidFields()); |
| 56 | + assertTrue(actualCreateBinderResult.isAutoGrowNestedPaths()); |
| 57 | + assertNull(actualCreateBinderResult.getValidator()); |
| 58 | + assertEquals("Target", actualCreateBinderResult.getTarget()); |
| 59 | + assertEquals("target", actualCreateBinderResult.getObjectName()); |
| 60 | + assertTrue(actualCreateBinderResult.getBindingErrorProcessor() instanceof DefaultBindingErrorProcessor); |
| 61 | + assertEquals(256, actualCreateBinderResult.getAutoGrowCollectionLimit()); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void customExcelRowMapper_withSetValues_returnSuccessResponse() { |
| 66 | + ApplicationConversionService conversionService = new ApplicationConversionService(); |
| 67 | + CustomExcelRowMapper<Object> actualCustomExcelRowMapper = new CustomExcelRowMapper<>(conversionService, |
| 68 | + new CustomValidatorBean()); |
| 69 | + actualCustomExcelRowMapper.setBeanFactory(new AnnotationConfigReactiveWebApplicationContext()); |
| 70 | + actualCustomExcelRowMapper.setDistanceLimit(1); |
| 71 | + actualCustomExcelRowMapper.setPrototypeBeanName("Name"); |
| 72 | + actualCustomExcelRowMapper.setStrict(true); |
| 73 | + actualCustomExcelRowMapper.setTargetType(Object.class); |
| 74 | + DataBinder dataBinder = new DataBinder("Target", "Object Name"); |
| 75 | + actualCustomExcelRowMapper.initBinder(dataBinder); |
| 76 | + assertNotNull(dataBinder); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void customExcelRowMapper_withInitBinder_returnSuccessResponse() { |
| 81 | + ApplicationConversionService conversionService = new ApplicationConversionService(); |
| 82 | + CustomExcelRowMapper<Object> actualCustomExcelRowMapper = new CustomExcelRowMapper<>(conversionService, |
| 83 | + new CustomValidatorBean()); |
| 84 | + DataBinder dataBinder = new DataBinder("Target", "Object Name"); |
| 85 | + actualCustomExcelRowMapper.initBinder(dataBinder); |
| 86 | + assertNotNull(dataBinder); |
| 87 | + } |
| 88 | + |
| 89 | + @Test(expected = Exception.class) |
| 90 | + public void mapRow_withEmptyRowSet_throwException() throws BindException { |
| 91 | + ApplicationConversionService conversionService = new ApplicationConversionService(); |
| 92 | + CustomExcelRowMapper<Object> customExcelRowMapper = new CustomExcelRowMapper<>(conversionService, |
| 93 | + new CustomValidatorBean()); |
| 94 | + customExcelRowMapper.setTargetType(Object.class); |
| 95 | + RowSet rowSet = mock(RowSet.class); |
| 96 | + when(rowSet.getProperties()).thenReturn(new Properties()); |
| 97 | + customExcelRowMapper.mapRow(rowSet); |
| 98 | + } |
| 99 | + |
| 100 | + @Test(expected = NotWritablePropertyException.class) |
| 101 | + public void mapRow_withProperties_throwNotWritablePropertyException() throws BindException { |
| 102 | + ApplicationConversionService conversionService = new ApplicationConversionService(); |
| 103 | + CustomExcelRowMapper<Object> customExcelRowMapper = new CustomExcelRowMapper<>(conversionService, |
| 104 | + new CustomValidatorBean()); |
| 105 | + customExcelRowMapper.setTargetType(Object.class); |
| 106 | + |
| 107 | + Properties properties = new Properties(); |
| 108 | + properties.setProperty("key", "value"); |
| 109 | + RowSet rowSet = mock(RowSet.class); |
| 110 | + when(rowSet.getProperties()).thenReturn(properties); |
| 111 | + customExcelRowMapper.mapRow(rowSet); |
| 112 | + } |
| 113 | + |
| 114 | + @Test(expected = ConstraintViolationException.class) |
| 115 | + public void mapRow_withConstraintViolation_throwConstraintViolationException() throws BindException { |
| 116 | + ApplicationConversionService conversionService = new ApplicationConversionService(); |
| 117 | + CustomExcelRowMapper<Object> customExcelRowMapper = new CustomExcelRowMapper<>(conversionService, |
| 118 | + new CustomValidatorBean()); |
| 119 | + customExcelRowMapper.setTargetType(Object.class); |
| 120 | + RowSet rowSet = mock(RowSet.class); |
| 121 | + when(rowSet.getProperties()).thenThrow(new ConstraintViolationException(new HashSet<>())); |
| 122 | + customExcelRowMapper.mapRow(rowSet); |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments