CsvDatabaseReader.java

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
 *
48
 * @author RealLifeDeveloper
49
 */
50
@Getter
51
public class CsvDatabaseReader {
52
53
    private static final Logger LOG = LoggerFactory.getLogger(CsvDatabaseReader.class);
54
55
    private final char csvSeparatorCharacter;
56
    private final int csvSkipLines;
57
58
    private final CrudRepositoryWriter crudRepositoryWriter = new CrudRepositoryWriter();
59
60
    /**
61
     * Creates a new {@code CsvDatabaseReader} with the given configuration.
62
     *
63
     * @param csvSeparatorCharacter the separator character to use when reading the file, normally ',' or ';'
64
     * @param csvSkipLines          the number of lines to skip at the beginning of the file
65
     */
66
    public CsvDatabaseReader(char csvSeparatorCharacter, int csvSkipLines) {
67
        this.csvSeparatorCharacter = csvSeparatorCharacter;
68
        this.csvSkipLines = csvSkipLines;
69
    }
70
71
    /**
72
     * Reads a CSV file from the named resource, populating the given repository with entities of the given type.
73
     *
74
     * @param resourceName         the classpath resource containing a CSV file
75
     * @param repository           the repository to populate with the entities from the CSV file
76
     * @param repositoryEntityType the class object representing {@code <T>}, i.e., the class of entities in the repository
77
     * @param entityType           the class object representing {@code <E>}, i.e., the class of entity being read
78
     * @param tableName            the name of the database table to use; may be either the table associated with the entity, or a join
79
     *                             table
80
     * @param <T>                  the type of entities in the repository
81
     * @param <E>                  the type of entity being read
82
     * @param <ID>                 the type of the primary key of the entities in the repository
83
     *
84
     * @throws IOException  if reading the file failed
85
     * @throws CsvException if parsing the file failed
86
     */
87
    public <T, E, ID extends Serializable> void read(String resourceName, CrudRepository<T, ID> repository, Class<T> repositoryEntityType,
88
            @Nullable Class<E> entityType, String tableName) throws IOException, CsvException {
89
        try (InputStream in = CsvDatabaseReader.class.getResourceAsStream(resourceName)) {
90 1 1. read : negated conditional → KILLED
            if (in == null) {
91
                throw new FileNotFoundException(resourceName);
92
            }
93
            LOG.info("Reading from {}", resourceName.replaceAll("[\r\n]", ""));
94
            try (Reader reader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))) {
95
                CSVParser parser = new CSVParserBuilder().withSeparator(csvSeparatorCharacter).build();
96
                try (CSVReader csvReader = new CSVReaderBuilder(reader).withSkipLines(csvSkipLines).withCSVParser(parser).build()) {
97
                    String[] header = csvReader.readNext();
98
                    String[] row;
99 1 1. read : negated conditional → KILLED
                    while ((row = csvReader.readNext()) != null) {
100
                        @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
101
                        DbTableRow tableRow = new CsvTableRow(header, row);
102 1 1. read : negated conditional → KILLED
                        if (crudRepositoryWriter.writeEntity(tableRow, repositoryEntityType, entityType, repository, tableName)) {
103
                            continue;
104
                        }
105 1 1. read : removed call to com/reallifedeveloper/tools/test/database/CrudRepositoryWriter::addEntitiesFromJoinTable → KILLED
                        crudRepositoryWriter.addEntitiesFromJoinTable(tableRow, tableName);
106
                    }
107
                }
108 1 1. read : removed call to com/reallifedeveloper/tools/test/database/CrudRepositoryWriter::fillReferencesBetweenEntities → KILLED
                crudRepositoryWriter.fillReferencesBetweenEntities();
109
            }
110
        } catch (ReflectiveOperationException | SecurityException e) {
111
            throw new IllegalStateException("Unexpected problem reading CSV file from '" + resourceName + "'", e);
112
        }
113
    }
114
115
    @ToString
116
    private static class CsvTableRow implements DbTableRow {
117
118
        private final List<String> header;
119
        private final List<String> row;
120
121
        @SuppressWarnings("PMD.UseVarargs")
122
        @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Private class")
123
        /* package-private */ CsvTableRow(String[] header, String[] row) {
124 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) {
125
                throw new IllegalArgumentException(
126
                        "Arguments must not be null or empty: header=" + TestUtil.asList(header) + ", row=" + TestUtil.asList(row));
127
            }
128 1 1. <init> : negated conditional → KILLED
            if (header.length != row.length) {
129
                throw new IllegalArgumentException(
130
                        "header and row should be of same length: header=" + Arrays.asList(header) + ", row=" + Arrays.asList(row));
131
            }
132
            this.header = Arrays.asList(header);
133
            this.row = Arrays.asList(row);
134
        }
135
136
        @Override
137
        public List<DbTableField> columns() {
138
            List<DbTableField> columns = new ArrayList<>();
139 2 1. columns : changed conditional boundary → KILLED
2. columns : negated conditional → KILLED
            for (int i = 0; i < row.size(); i++) {
140
                columns.add(new DbTableField(header.get(i), row.get(i)));
141
            }
142 1 1. columns : replaced return value with Collections.emptyList for com/reallifedeveloper/tools/test/database/csv/CsvDatabaseReader$CsvTableRow::columns → KILLED
            return columns;
143
        }
144
145
    }
146
}

Mutations

90

1.1
Location : read
Killed by : com.reallifedeveloper.tools.test.database.csv.CsvDatabaseReaderTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.csv.CsvDatabaseReaderTest]/[method:readNonExistingFile()]
negated conditional → KILLED

99

1.1
Location : read
Killed by : com.reallifedeveloper.tools.test.database.csv.CsvDatabaseReaderTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.csv.CsvDatabaseReaderTest]/[method:readWrongTypeOfFile()]
negated conditional → KILLED

102

1.1
Location : read
Killed by : com.reallifedeveloper.tools.test.database.csv.CsvDatabaseReaderTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.csv.CsvDatabaseReaderTest]/[method:readFileForEntityWithAssociations()]
negated conditional → KILLED

105

1.1
Location : read
Killed by : com.reallifedeveloper.tools.test.database.csv.CsvDatabaseReaderTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.csv.CsvDatabaseReaderTest]/[method:readFileForEntityWithAssociations()]
removed call to com/reallifedeveloper/tools/test/database/CrudRepositoryWriter::addEntitiesFromJoinTable → KILLED

108

1.1
Location : read
Killed by : com.reallifedeveloper.tools.test.database.csv.CsvDatabaseReaderTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.csv.CsvDatabaseReaderTest]/[method:readFileForEntityWithAssociations()]
removed call to com/reallifedeveloper/tools/test/database/CrudRepositoryWriter::fillReferencesBetweenEntities → KILLED

124

1.1
Location : <init>
Killed by : com.reallifedeveloper.tools.test.database.csv.CsvDatabaseReaderTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.csv.CsvDatabaseReaderTest]/[method:readWrongTypeOfFile()]
negated conditional → KILLED

2.2
Location : <init>
Killed by : com.reallifedeveloper.tools.test.database.csv.CsvDatabaseReaderTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.csv.CsvDatabaseReaderTest]/[method:readWrongTypeOfFile()]
negated conditional → KILLED

3.3
Location : <init>
Killed by : com.reallifedeveloper.tools.test.database.csv.CsvDatabaseReaderTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.csv.CsvDatabaseReaderTest]/[method:readWrongTypeOfFile()]
negated conditional → KILLED

4.4
Location : <init>
Killed by : com.reallifedeveloper.tools.test.database.csv.CsvDatabaseReaderTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.csv.CsvDatabaseReaderTest]/[method:readWrongTypeOfFile()]
negated conditional → KILLED

128

1.1
Location : <init>
Killed by : com.reallifedeveloper.tools.test.database.csv.CsvDatabaseReaderTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.csv.CsvDatabaseReaderTest]/[method:readWrongTypeOfFile()]
negated conditional → KILLED

139

1.1
Location : columns
Killed by : com.reallifedeveloper.tools.test.database.csv.CsvDatabaseReaderTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.csv.CsvDatabaseReaderTest]/[method:readWrongTypeOfFile()]
changed conditional boundary → KILLED

2.2
Location : columns
Killed by : com.reallifedeveloper.tools.test.database.csv.CsvDatabaseReaderTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.csv.CsvDatabaseReaderTest]/[method:readWrongTypeOfFile()]
negated conditional → KILLED

142

1.1
Location : columns
Killed by : com.reallifedeveloper.tools.test.database.csv.CsvDatabaseReaderTest.[engine:junit-jupiter]/[class:com.reallifedeveloper.tools.test.database.csv.CsvDatabaseReaderTest]/[method:readWrongTypeOfFile()]
replaced return value with Collections.emptyList for com/reallifedeveloper/tools/test/database/csv/CsvDatabaseReader$CsvTableRow::columns → KILLED

Active mutators

Tests examined


Report generated by PIT 1.23.0