| 1 | package com.reallifedeveloper.tools.test.database.csv; | |
| 2 | ||
| 3 | import java.io.BufferedReader; | |
| 4 | import java.io.FileNotFoundException; | |
| 5 | import java.io.IOException; | |
| 6 | import java.io.InputStream; | |
| 7 | import java.io.InputStreamReader; | |
| 8 | import java.io.Reader; | |
| 9 | import java.io.Serializable; | |
| 10 | import java.nio.charset.StandardCharsets; | |
| 11 | import java.util.ArrayList; | |
| 12 | import java.util.Arrays; | |
| 13 | import java.util.List; | |
| 14 | ||
| 15 | import org.checkerframework.checker.nullness.qual.Nullable; | |
| 16 | import org.slf4j.Logger; | |
| 17 | import org.slf4j.LoggerFactory; | |
| 18 | import org.springframework.data.repository.CrudRepository; | |
| 19 | ||
| 20 | import com.opencsv.CSVParser; | |
| 21 | import com.opencsv.CSVParserBuilder; | |
| 22 | import com.opencsv.CSVReader; | |
| 23 | import com.opencsv.CSVReaderBuilder; | |
| 24 | import com.opencsv.exceptions.CsvException; | |
| 25 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | |
| 26 | import lombok.Getter; | |
| 27 | import lombok.ToString; | |
| 28 | ||
| 29 | import com.reallifedeveloper.tools.test.TestUtil; | |
| 30 | import com.reallifedeveloper.tools.test.database.CrudRepositoryWriter; | |
| 31 | import com.reallifedeveloper.tools.test.database.CrudRepositoryWriter.DbTableField; | |
| 32 | import com.reallifedeveloper.tools.test.database.CrudRepositoryWriter.DbTableRow; | |
| 33 | ||
| 34 | /** | |
| 35 | * A class to read a CSV file and populate a Spring Data {@code CrudRepository} using the information in the file. | |
| 36 | * <p> | |
| 37 | * This is useful for testing in-memory repositories using the same test cases as for real repository implementations, and also for | |
| 38 | * populating in-memory repositories for testing services, without having to use a real database. | |
| 39 | * <p> | |
| 40 | * The file is assumed to have a header containing the names of the database columns to populate, followed by the data rows. An example: | |
| 41 | * | |
| 42 | * <pre> | |
| 43 | * id;name | |
| 44 | * 1;foo | |
| 45 | * 2;bar | |
| 46 | * </pre> | |
| 47 | * <p> | |
| 48 | * | |
| 49 | * @author RealLifeDeveloper | |
| 50 | */ | |
| 51 | @Getter | |
| 52 | public class CsvDatabaseReader { | |
| 53 | ||
| 54 | private static final Logger LOG = LoggerFactory.getLogger(CsvDatabaseReader.class); | |
| 55 | ||
| 56 | private final char csvSeparatorCharacter; | |
| 57 | private final int csvSkipLines; | |
| 58 | ||
| 59 | private final CrudRepositoryWriter crudRepositoryWriter = new CrudRepositoryWriter(); | |
| 60 | ||
| 61 | /** | |
| 62 | * Creates a new {@code CsvDatabaseReader} with the given configuration. | |
| 63 | * | |
| 64 | * @param csvSeparatorCharacter the separator character to use when reading the file, normally ',' or ';' | |
| 65 | * @param csvSkipLines the number of lines to skip at the beginning of the file | |
| 66 | */ | |
| 67 | public CsvDatabaseReader(char csvSeparatorCharacter, int csvSkipLines) { | |
| 68 | this.csvSeparatorCharacter = csvSeparatorCharacter; | |
| 69 | this.csvSkipLines = csvSkipLines; | |
| 70 | } | |
| 71 | ||
| 72 | /** | |
| 73 | * Reads a CSV file from the named resource, populating the given repository with entities of the given type. | |
| 74 | * | |
| 75 | * @param resourceName the classpath resource containing a CSV file | |
| 76 | * @param repository the repository to populate with the entities from the CSV file | |
| 77 | * @param repositoryEntityType the class object representing {@code <T>}, i.e., the class of entities in the repository | |
| 78 | * @param entityType the class object representing {@code <E>}, i.e., the class of entity being read | |
| 79 | * @param tableName the name of the database table to use; may be either the table associated with the entity, or a join | |
| 80 | * table | |
| 81 | * @param <T> the type of entities in the repository | |
| 82 | * @param <E> the type of entity being read | |
| 83 | * @param <ID> the type of the primary key of the entities in the repository | |
| 84 | * | |
| 85 | * @throws IOException if reading the file failed | |
| 86 | * @throws CsvException if parsing the file failed | |
| 87 | */ | |
| 88 | public <T, E, ID extends Serializable> void read(String resourceName, CrudRepository<T, ID> repository, Class<T> repositoryEntityType, | |
| 89 | @Nullable Class<E> entityType, String tableName) throws IOException, CsvException { | |
| 90 | try (InputStream in = CsvDatabaseReader.class.getResourceAsStream(resourceName)) { | |
| 91 |
1
1. read : negated conditional → KILLED |
if (in == null) { |
| 92 | throw new FileNotFoundException(resourceName); | |
| 93 | } | |
| 94 | LOG.info("Reading from {}", resourceName.replaceAll("[\r\n]", "")); | |
| 95 | try (Reader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))) { | |
| 96 | CSVParser parser = new CSVParserBuilder().withSeparator(csvSeparatorCharacter).build(); | |
| 97 | try (CSVReader csvReader = new CSVReaderBuilder(reader).withSkipLines(csvSkipLines).withCSVParser(parser).build()) { | |
| 98 | String[] header = csvReader.readNext(); | |
| 99 | String[] row; | |
| 100 |
1
1. read : negated conditional → KILLED |
while ((row = csvReader.readNext()) != null) { |
| 101 | @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") | |
| 102 | DbTableRow tableRow = new CsvTableRow(header, row); | |
| 103 |
1
1. read : negated conditional → KILLED |
if (crudRepositoryWriter.writeEntity(tableRow, repositoryEntityType, entityType, repository, tableName)) { |
| 104 | continue; | |
| 105 | } | |
| 106 |
1
1. read : removed call to com/reallifedeveloper/tools/test/database/CrudRepositoryWriter::addEntitiesFromJoinTable → KILLED |
crudRepositoryWriter.addEntitiesFromJoinTable(tableRow, tableName); |
| 107 | } | |
| 108 | } | |
| 109 |
1
1. read : removed call to com/reallifedeveloper/tools/test/database/CrudRepositoryWriter::fillReferencesBetweenEntities → KILLED |
crudRepositoryWriter.fillReferencesBetweenEntities(); |
| 110 | } | |
| 111 | } catch (ReflectiveOperationException | SecurityException e) { | |
| 112 | throw new IllegalStateException("Unexpected problem reading CSV file from '" + resourceName + "'", e); | |
| 113 | } | |
| 114 | } | |
| 115 | ||
| 116 | @ToString | |
| 117 | private static class CsvTableRow implements DbTableRow { | |
| 118 | ||
| 119 | private final List<String> header; | |
| 120 | private final List<String> row; | |
| 121 | ||
| 122 | @SuppressWarnings("PMD.UseVarargs") | |
| 123 | @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Private class") | |
| 124 | /* package-private */ CsvTableRow(String[] header, String[] row) { | |
| 125 |
4
1. <init> : negated conditional → KILLED 2. <init> : negated conditional → KILLED 3. <init> : negated conditional → KILLED 4. <init> : negated conditional → KILLED |
if (header == null || row == null || header.length == 0 || row.length == 0) { |
| 126 | throw new IllegalArgumentException( | |
| 127 | "Arguments must not be null or empty: header=" + TestUtil.asList(header) + ", row=" + TestUtil.asList(row)); | |
| 128 | } | |
| 129 |
1
1. <init> : negated conditional → KILLED |
if (header.length != row.length) { |
| 130 | throw new IllegalArgumentException( | |
| 131 | "header and row should be of same length: header=" + Arrays.asList(header) + ", row=" + Arrays.asList(row)); | |
| 132 | } | |
| 133 | this.header = Arrays.asList(header); | |
| 134 | this.row = Arrays.asList(row); | |
| 135 | } | |
| 136 | ||
| 137 | @Override | |
| 138 | public List<DbTableField> columns() { | |
| 139 | List<DbTableField> columns = new ArrayList<>(); | |
| 140 |
2
1. columns : changed conditional boundary → KILLED 2. columns : negated conditional → KILLED |
for (int i = 0; i < row.size(); i++) { |
| 141 | columns.add(new DbTableField(header.get(i), row.get(i))); | |
| 142 | } | |
| 143 |
1
1. columns : replaced return value with Collections.emptyList for com/reallifedeveloper/tools/test/database/csv/CsvDatabaseReader$CsvTableRow::columns → KILLED |
return columns; |
| 144 | } | |
| 145 | ||
| 146 | } | |
| 147 | } | |
Mutations | ||
| 91 |
1.1 |
|
| 100 |
1.1 |
|
| 103 |
1.1 |
|
| 106 |
1.1 |
|
| 109 |
1.1 |
|
| 125 |
1.1 2.2 3.3 4.4 |
|
| 129 |
1.1 |
|
| 140 |
1.1 2.2 |
|
| 143 |
1.1 |