Skip to content

Commit

Permalink
refactor(solrteur): refactor state factory with state keywords IQSS#7662
Browse files Browse the repository at this point in the history
  • Loading branch information
poikilotherm committed Apr 29, 2022
1 parent 3733117 commit 3c6a5fa
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 43 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
package cli.util.model;

import cli.util.TsvBlockReader;

public enum ParsingState {
Vocabularies(ControlledVocabulary.TRIGGER),
Fields(Field.TRIGGER, Vocabularies),
MetadataBlock(Block.TRIGGER, Fields),
Vocabularies(ControlledVocabulary.KEYWORD),
Fields(Field.KEYWORD, Vocabularies),
MetadataBlock(Block.KEYWORD, Fields),
// This state is only used exactly once and should never be reached from input.
// For safety, make the validation fail.
Init(Constants.COMMENT_INDICATOR, MetadataBlock);
Init(null, MetadataBlock);

private final String stateTrigger;
private final String stateKeyword;
private final ParsingState nextState;

ParsingState(String trigger, ParsingState next) {
this.stateTrigger = trigger;
ParsingState(String keyword, ParsingState next) {
this.stateKeyword = keyword;
this.nextState = next;
}

/**
* Create final state (no next step)
* @param trigger
*/
ParsingState(String trigger) {
this.stateTrigger = trigger;
ParsingState(String keyword) {
this.stateKeyword = keyword;
this.nextState = this;
}

public boolean isAllowedFinalState() {
return this == Fields || this == Vocabularies;
}

public ParsingState transitionState(String headerLine) throws ParserException {
public ParsingState transitionState(String headerLine, Configuration config) throws ParserException {
// if not null, not starting the same state again (no loops allowed) and starting the correct next state, return the next state
if(headerLine != null && ! headerLine.startsWith(this.stateTrigger) &&
headerLine.startsWith(this.nextState.stateTrigger)) {
if(headerLine != null &&
! headerLine.startsWith(config.trigger(this.stateKeyword)) &&
headerLine.startsWith(config.trigger(this.nextState.stateKeyword))) {
return this.nextState;
}
// otherwise throw a parsing exception
throw new ParserException("Invalid header '" +
// otherwise, throw a parsing exception
throw new ParserException("Found invalid header '" +
(headerLine == null ? "null" : headerLine.substring(0, Math.min(25, headerLine.length()))) +
"...' while in section '" + this.stateTrigger + "'");
"...' while " +
(this.stateKeyword == null ? "initializing." : "in section '" + this.stateKeyword + "'."));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

class ParsingStateTest {

static Configuration config = Configuration.defaultConfig();

static Stream<Arguments> failingStateTransitionExamples() {
return Stream.of(
Expand All @@ -28,43 +30,43 @@ static Stream<Arguments> failingStateTransitionExamples() {
Arguments.of(ParsingState.Fields, "foobar"),
Arguments.of(ParsingState.Vocabularies, "foobar"),

Arguments.of(ParsingState.Init, Constants.TRIGGER_INDICATOR),
Arguments.of(ParsingState.Init, Constants.COMMENT_INDICATOR),
Arguments.of(ParsingState.Init, Constants.COLUMN_SEPARATOR),
Arguments.of(ParsingState.Init, config.triggerIndicator()),
Arguments.of(ParsingState.Init, config.commentIndicator()),
Arguments.of(ParsingState.Init, config.columnSeparator()),

Arguments.of(ParsingState.Init, Field.TRIGGER),
Arguments.of(ParsingState.Init, ControlledVocabulary.TRIGGER),
Arguments.of(ParsingState.Init, config.trigger(Field.KEYWORD)),
Arguments.of(ParsingState.Init, config.trigger(ControlledVocabulary.KEYWORD)),

Arguments.of(ParsingState.MetadataBlock, Constants.COMMENT_INDICATOR),
Arguments.of(ParsingState.MetadataBlock, ControlledVocabulary.TRIGGER),
Arguments.of(ParsingState.MetadataBlock, config.commentIndicator()),
Arguments.of(ParsingState.MetadataBlock, config.trigger(ControlledVocabulary.KEYWORD)),

Arguments.of(ParsingState.Fields, Constants.COMMENT_INDICATOR),
Arguments.of(ParsingState.Fields, Block.TRIGGER),
Arguments.of(ParsingState.Fields, config.commentIndicator()),
Arguments.of(ParsingState.Fields, config.trigger(Block.KEYWORD)),

Arguments.of(ParsingState.Vocabularies, Constants.COMMENT_INDICATOR),
Arguments.of(ParsingState.Vocabularies, Block.TRIGGER),
Arguments.of(ParsingState.Vocabularies, Field.TRIGGER)
Arguments.of(ParsingState.Vocabularies, config.commentIndicator()),
Arguments.of(ParsingState.Vocabularies, config.trigger(Block.KEYWORD)),
Arguments.of(ParsingState.Vocabularies, config.trigger(Field.KEYWORD))
);
}

@ParameterizedTest
@MethodSource("failingStateTransitionExamples")
void failingTransitions(ParsingState source, String triggerLine) throws ParserException {
ParserException ex = assertThrows(ParserException.class, () -> source.transitionState(triggerLine));
void failingTransitions(final ParsingState source, final String triggerLine) throws ParserException {
ParserException ex = assertThrows(ParserException.class, () -> source.transitionState(triggerLine, config));
}

static Stream<Arguments> successfulStateTransitionExamples() {
return Stream.of(
Arguments.of(ParsingState.Init, Block.TRIGGER, ParsingState.MetadataBlock),
Arguments.of(ParsingState.MetadataBlock, Field.TRIGGER, ParsingState.Fields),
Arguments.of(ParsingState.Fields, ControlledVocabulary.TRIGGER, ParsingState.Vocabularies)
Arguments.of(ParsingState.Init, config.trigger(Block.KEYWORD), ParsingState.MetadataBlock),
Arguments.of(ParsingState.MetadataBlock, config.trigger(Field.KEYWORD), ParsingState.Fields),
Arguments.of(ParsingState.Fields, config.trigger(ControlledVocabulary.KEYWORD), ParsingState.Vocabularies)
);
}

@ParameterizedTest
@MethodSource("successfulStateTransitionExamples")
void successfulTransitions(ParsingState source, String triggerLine, ParsingState expected) throws ParserException {
assertEquals(expected, source.transitionState(triggerLine));
void successfulTransitions(final ParsingState source, final String triggerLine, final ParsingState expected) throws ParserException {
assertEquals(expected, source.transitionState(triggerLine, config));
}

}

0 comments on commit 3c6a5fa

Please sign in to comment.