xenoterracide
xenoterracide

Reputation: 16865

OpenCSV Header is missing required fields found []

opencsv 5.1

Caused by: com.opencsv.exceptions.CsvRequiredFieldEmptyException: Header is missing required fields [ALGVERIFICATION, DISTAL MV, LOCATION, PREDICTED STATE, PROXIMAL MV, RUN, SAMPLE TIME]. The list of headers encountered is [].
    at com.opencsv.bean.HeaderNameBaseMappingStrategy.captureHeader(HeaderNameBaseMappingStrategy.java:69)
@ParameterizedTest
@ArgumentsSource(MyArgumentsProvider.class)
void test( AlgorithmVerification verifications )
{
    Log.d("test", verifications.location);
    assertThat(verifications).isNotNull();
}


public enum State {
    NA,
    ADVANCE,
    RETRACT,
}

public static class StateConverter extends AbstractBeanField {

    @Override
    protected Object convert(String value) {
        return State.valueOf(value);
    }
}

public static class AlgorithmVerification {
    @CsvBindByName(column = "Sample Time", required = true)
    protected float sampleTime;

    @CsvBindByName(column = "Distal mV", required = true)
    protected int distalMV;

    @CsvBindByName(column = "Proximal mV", required = true)
    protected int proximalMV;

    @CsvCustomBindByName(column = "Predicted State", converter = StateConverter.class, required = true)
    protected State predictedState;

    @CsvBindByName(column = "run", required = true)
    protected String run;

    @CsvCustomBindByName(column = "Location", converter = StateConverter.class, required = true)
    protected String location;

    @CsvCustomBindByName(column = "AlgVerification", converter = StateConverter.class, required = true)
    protected State algVerification;
}


static class MyArgumentsProvider implements ArgumentsProvider {

    @Override
    public Stream<? extends Arguments> provideArguments(ExtensionContext context) throws IOException, URISyntaxException {
        return Files.list(Paths.get(ClassLoader.getSystemResource("avd").toURI()))
            .map(Path::toFile)
            .map( f -> Try.withResources( () -> new FileReader(f) )
                .of(CsvToBeanBuilder::new)
                .map(b -> b.withType(AlgorithmVerification.class) )
                .map(CsvToBeanBuilder::build)
                .map(CsvToBean::parse)
                .getOrElseThrow((throwable) -> new RuntimeException(f.getName(), throwable))
            )
            .flatMap(List::stream)
            .map(Arguments::of);
    }
}

and this is the beginning of the file

Sample Time,Distal mV,Proximal mV,Predicted State,run,Location,AlgVerification
0.016,2509,2502,NA,DV-MyString,-1,-1

Did I miss a step? are the headers wrong somehow? I notice that it's looking for uppercase headers... but even then it's finding none

Upvotes: 3

Views: 4083

Answers (1)

xenoterracide
xenoterracide

Reputation: 16865

looks like my usage of vavr Try.withResources is wrong, it needed to be this

return Files.list(Paths.get(ClassLoader.getSystemResource("avd").toURI()))
    .map(Path::toFile)
    .map( f -> Try.withResources( () -> new FileReader(f) )
            .of((fr ) -> new CsvToBeanBuilder<AlgorithmVerification>(fr)
                .withType(AlgorithmVerification.class)
                .build()
                .parse())
        .getOrElseThrow((throwable) -> new RuntimeException(f.getName(), throwable))
    )
    .flatMap(List::stream)
    .map(Arguments::of);

Upvotes: 2

Related Questions