Michael Hoennig
2022-10-29 9053f546b4792e13d4e0eb65a8afc6c6b485f59c
import legacy hsadmin db (WIP)
1 files added
78 ■■■■■ changed files
src/test/java/net/hostsharing/hsadminng/hs/office/migration/ImportBusinessPartners.java 78 ●●●●● patch | view | raw | blame | history
src/test/java/net/hostsharing/hsadminng/hs/office/migration/ImportBusinessPartners.java
New file
@@ -0,0 +1,78 @@
package net.hostsharing.hsadminng.hs.office.migration;
import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import net.hostsharing.hsadminng.hs.office.membership.HsOfficeMembershipEntity;
import net.hostsharing.hsadminng.hs.office.partner.HsOfficePartnerEntity;
import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonEntity;
import org.junit.jupiter.api.Test;
import jakarta.validation.constraints.NotNull;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
public class ImportBusinessPartners {
    private Map<Integer, HsOfficePartnerEntity> partners = new HashMap<>();
    private Map<Integer, HsOfficeMembershipEntity> members = new HashMap<>();
    @Test
    void importsBusinessPartners() throws Exception {
        try (Reader reader = resourceReader("migration/business_partners.csv")) {
            final var records = readAllLines(reader);
            importsBusinessPartners(records);
            assertThat(records).hasSize(4);
        }
    }
    public List<String[]> readAllLines(Reader reader) throws Exception {
        final var parser = new CSVParserBuilder()
                .withSeparator(';')
                .withQuoteChar('"')
                .build();
        try (CSVReader csvReader = new CSVReaderBuilder(reader)
                .withSkipLines(1)
                .withCSVParser(parser)
                .build()) {
            return csvReader.readAll();
        }
    }
    private void importsBusinessPartners(final List<String[]> records) {
        records.forEach( record -> {
            HsOfficePersonEntity.builder()
                    .tradeName(record[])
                    .build();
        });
    }
    private Reader resourceReader(@NotNull final String resourcePath) {
        return new InputStreamReader(getClass().getClassLoader().getResourceAsStream(resourcePath));
    }
    private Reader fileReader(@NotNull final Path filePath) throws IOException {
        //        Path path = Paths.get(
        //                ClassLoader.getSystemResource("csv/twoColumn.csv").toURI())
        //    );
        return Files.newBufferedReader(filePath);
    }
}