Spaces:
Running
Running
File size: 5,903 Bytes
dc510b0 | 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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | package com.erp.system.init;
import com.erp.system.repository.mongo.MongoErpDatasourceRepository;
import com.erp.system.repository.mysql.MysqlErpDatasourceRepository;
import com.erp.system.repository.postgres.PostgresErpDatasourceRepository;
import org.hibernate.exception.JDBCConnectionException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.boot.DefaultApplicationArguments;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.transaction.CannotCreateTransactionException;
import java.net.UnknownHostException;
import java.sql.SQLException;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
public class ErpDataSeederTest {
@Mock
private MongoErpDatasourceRepository mongoRepo;
@Mock
private MysqlErpDatasourceRepository mysqlRepo;
@Mock
private PostgresErpDatasourceRepository postgresRepo;
@InjectMocks
private ErpDataSeeder seeder;
@Test
public void testSeederDoesNotCrashWhenPostgresHostFailsDns() {
ReflectionTestUtils.setField(seeder, "recordCount", 10);
ReflectionTestUtils.setField(seeder, "batchSize", 5);
ReflectionTestUtils.setField(seeder, "mongoEnabled", true);
ReflectionTestUtils.setField(seeder, "mysqlEnabled", true);
ReflectionTestUtils.setField(seeder, "postgresEnabled", true);
ReflectionTestUtils.setField(seeder, "wipeBeforeSeed", false);
ReflectionTestUtils.setField(seeder, "skipIfSeeded", false);
when(mongoRepo.count()).thenReturn(0L);
when(mysqlRepo.count()).thenReturn(0L);
when(postgresRepo.count()).thenThrow(new CannotCreateTransactionException(
"Could not open JPA EntityManager",
new JDBCConnectionException("Failed", new SQLException("Conn err", "08001", new UnknownHostException("pg-dc4be3e-binabose955-e13a.d.aivencloud.com")))
));
assertDoesNotThrow(() -> seeder.run(new DefaultApplicationArguments()));
verify(mongoRepo, times(2)).saveAll(anyList());
verify(mysqlRepo, times(2)).saveAll(anyList());
verify(postgresRepo, never()).saveAll(anyList());
}
@Test
public void testSeederSkipsWhenAlreadySeeded() {
ReflectionTestUtils.setField(seeder, "recordCount", 10);
ReflectionTestUtils.setField(seeder, "batchSize", 5);
ReflectionTestUtils.setField(seeder, "mongoEnabled", true);
ReflectionTestUtils.setField(seeder, "mysqlEnabled", true);
ReflectionTestUtils.setField(seeder, "postgresEnabled", true);
ReflectionTestUtils.setField(seeder, "wipeBeforeSeed", false);
ReflectionTestUtils.setField(seeder, "skipIfSeeded", true);
when(mongoRepo.count()).thenReturn(200L);
when(mysqlRepo.count()).thenReturn(200L);
when(postgresRepo.count()).thenReturn(200L);
assertDoesNotThrow(() -> seeder.run(new DefaultApplicationArguments()));
verify(mongoRepo, never()).deleteAll();
verify(mysqlRepo, never()).deleteAll();
verify(postgresRepo, never()).deleteAll();
verify(mongoRepo, never()).saveAll(anyList());
verify(mysqlRepo, never()).saveAll(anyList());
verify(postgresRepo, never()).saveAll(anyList());
}
@Test
public void testSeederSkipsWhenExistingDataCountIsExactSame() {
ReflectionTestUtils.setField(seeder, "recordCount", 10);
ReflectionTestUtils.setField(seeder, "batchSize", 5);
ReflectionTestUtils.setField(seeder, "mongoEnabled", true);
ReflectionTestUtils.setField(seeder, "mysqlEnabled", true);
ReflectionTestUtils.setField(seeder, "postgresEnabled", true);
ReflectionTestUtils.setField(seeder, "wipeBeforeSeed", false);
ReflectionTestUtils.setField(seeder, "skipIfSeeded", true);
when(mongoRepo.count()).thenReturn(10L);
when(mysqlRepo.count()).thenReturn(10L);
when(postgresRepo.count()).thenReturn(10L);
assertDoesNotThrow(() -> seeder.run(new DefaultApplicationArguments()));
verify(mongoRepo, never()).deleteAll();
verify(mysqlRepo, never()).deleteAll();
verify(postgresRepo, never()).deleteAll();
verify(mongoRepo, never()).saveAll(anyList());
verify(mysqlRepo, never()).saveAll(anyList());
verify(postgresRepo, never()).saveAll(anyList());
}
@Test
public void testSeederSeedsOnlyDifferenceWhenExistingCountIsLessThanTarget() {
ReflectionTestUtils.setField(seeder, "recordCount", 10);
ReflectionTestUtils.setField(seeder, "batchSize", 5);
ReflectionTestUtils.setField(seeder, "mongoEnabled", true);
ReflectionTestUtils.setField(seeder, "mysqlEnabled", true);
ReflectionTestUtils.setField(seeder, "postgresEnabled", true);
ReflectionTestUtils.setField(seeder, "wipeBeforeSeed", false);
ReflectionTestUtils.setField(seeder, "skipIfSeeded", true);
when(mongoRepo.count()).thenReturn(6L);
when(mysqlRepo.count()).thenReturn(7L);
when(postgresRepo.count()).thenReturn(8L);
assertDoesNotThrow(() -> seeder.run(new DefaultApplicationArguments()));
verify(mongoRepo, never()).deleteAll();
verify(mysqlRepo, never()).deleteAll();
verify(postgresRepo, never()).deleteAll();
verify(mongoRepo, times(1)).saveAll(anyList());
verify(mysqlRepo, times(1)).saveAll(anyList());
verify(postgresRepo, times(1)).saveAll(anyList());
}
}
|