• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pkiraly / metadata-qa-marc / #1527

22 Aug 2025 02:21PM UTC coverage: 90.345%. Remained the same
#1527

push

pkiraly
Improve timeline handling

5191 of 6416 new or added lines in 219 files covered. (80.91%)

886 existing lines in 78 files now uncovered.

36717 of 40641 relevant lines covered (90.34%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.0
/src/main/java/de/gwdg/metadataqa/marc/analysis/validator/ControlValueValidator.java
1
package de.gwdg.metadataqa.marc.analysis.validator;
2

3
import de.gwdg.metadataqa.marc.definition.ControlValue;
4
import de.gwdg.metadataqa.marc.definition.general.parser.ParserException;
5
import de.gwdg.metadataqa.marc.definition.general.parser.SubfieldContentParser;
6
import de.gwdg.metadataqa.marc.definition.structure.ControlfieldPositionDefinition;
7
import de.gwdg.metadataqa.marc.model.validation.ValidationError;
8
import de.gwdg.metadataqa.marc.model.validation.ValidationErrorType;
9

10
import java.util.ArrayList;
11

12
public class ControlValueValidator extends AbstractValidator {
13

14
  public ControlValueValidator() {
15
    super(new ValidatorConfiguration());
1✔
16
  }
1✔
17

18
  public ControlValueValidator(ValidatorConfiguration configuration) {
19
    super(configuration);
1✔
20
  }
1✔
21

22
  public boolean validate(ControlValue controlValue) {
23
    validationErrors = new ArrayList<>();
1✔
24

25
    var definition = controlValue.getDefinition();
1✔
26
    var value = controlValue.getValue();
1✔
27

28
    boolean isCodeValid = definition.validate(value);
1✔
29

30
    if (!isCodeValid) {
1✔
31
      if (definition.isHistoricalCode(value)) {
1✔
32
        addError(controlValue, ValidationErrorType.CONTROL_POSITION_OBSOLETE_CODE, value);
1✔
33
      } else {
34
        // It's unnecessary to check if the code is repeatable in here, as there's already the validate method in the
35
        // definition that does this - it's a matter of question where the validation should be done but currently,
36
        // it's in the definition.
37

38
        // This checking here is just to conform to the test cases which expect that error is added whenever the code
39
        // isn't valid in case it's repeatable.
40
        fallbackValidateRepeatableCode(definition, controlValue, value);
1✔
41
      }
42
    }
43

44
    if (definition.hasParser()) {
1✔
45
      try {
46
        SubfieldContentParser parser = definition.getParser();
1✔
47
        parser.parse(value);
1✔
UNCOV
48
      } catch (ParserException e) {
×
UNCOV
49
        addError(controlValue, ValidationErrorType.CONTROL_POSITION_INVALID_CODE, e.getMessage());
×
50
      }
1✔
51
    }
52

53
    return validationErrors.isEmpty();
1✔
54
  }
55

56

57
  /**
58
   * In case the control value is repeatable, the value is split into units of the length of the unit and then each unit is validated.
59
   */
60
  private void fallbackValidateRepeatableCode(ControlfieldPositionDefinition definition,
61
                                              ControlValue controlValue,
62
                                              String value) {
63
    if (!definition.isRepeatableContent()) {
1✔
64
      addError(controlValue, ValidationErrorType.CONTROL_POSITION_INVALID_VALUE, value);
1✔
65
      return;
1✔
66
    }
67

NEW
68
    int unitLength = definition.getUnitLength();
×
NEW
69
    for (int i = 0; i < value.length(); i += unitLength) {
×
NEW
70
      String unit = value.substring(i, i + unitLength);
×
NEW
71
      if (!definition.getValidCodes().contains(unit)) {
×
NEW
72
        addError(controlValue, ValidationErrorType.CONTROL_POSITION_INVALID_CODE, String.format("'%s' in '%s'", unit, value));
×
73
      }
74
    }
NEW
75
  }
×
76

77
  private void addError(ControlValue controlValue, ValidationErrorType type, String message) {
78
    if (isIgnorableType(type)) {
1✔
NEW
79
      return;
×
80
    }
81

82
    var definition = controlValue.getDefinition();
1✔
83
    validationErrors.add(
1✔
84
      new ValidationError(
85
        ((controlValue.getMarcRecord() == null) ? null : controlValue.getMarcRecord().getId()),
1✔
86
        definition.getPath(),
1✔
87
        type,
88
        message,
89
        definition.getDescriptionUrl()
1✔
90
      )
91
    );
92
  }
1✔
93
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc