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

pkiraly / metadata-qa-api / #644

03 Apr 2025 09:28AM UTC coverage: 87.234% (-0.2%) from 87.454%
#644

push

pkiraly
Add mandatory and scope as generic rule properties #225

70 of 87 new or added lines in 12 files covered. (80.46%)

6 existing lines in 4 files now uncovered.

5453 of 6251 relevant lines covered (87.23%)

0.87 hits per line

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

83.75
/src/main/java/de/gwdg/metadataqa/api/rule/BaseRuleChecker.java
1
package de.gwdg.metadataqa.api.rule;
2

3
import de.gwdg.metadataqa.api.configuration.schema.ApplicationScope;
4
import de.gwdg.metadataqa.api.counter.FieldCounter;
5

6
import java.util.logging.Logger;
7

8
public abstract class BaseRuleChecker implements RuleChecker {
1✔
9

10
  protected static final Logger LOGGER = Logger.getLogger(BaseRuleChecker.class.getCanonicalName());
1✔
11

12
  protected String id;
13
  protected Integer failureScore;
14
  protected Integer successScore;
15
  protected Integer naScore;
16
  protected String header;
17
  protected Boolean hidden = false;
1✔
18
  private Boolean debug = false;
1✔
19
  private Boolean mandatory = false;
1✔
20
  private ApplicationScope scope;
21

22
  /**
23
   * A flag to denote if the RuleChecker should count the number instances and failures
24
   */
25
  private Boolean countInstances = false;
1✔
26

27
  @Override
28
  public String getId() {
29
    return id == null ? String.valueOf(0) : id;
1✔
30
  }
31

32
  @Override
33
  public void setId(String id) {
34
    this.id = id;
1✔
35
  }
1✔
36

37
  @Override
38
  public Integer getFailureScore() {
39
    return failureScore;
1✔
40
  }
41

42
  @Override
43
  public void setFailureScore(Integer failureScore) {
44
    this.failureScore = failureScore;
1✔
45
  }
1✔
46

47
  public RuleChecker withFailureScore(Integer failureScore) {
48
    this.failureScore = failureScore;
1✔
49
    return this;
1✔
50
  }
51

52
  @Override
53
  public Integer getSuccessScore() {
54
    return successScore;
1✔
55
  }
56

57
  @Override
58
  public void setSuccessScore(Integer successScore) {
59
    this.successScore = successScore;
1✔
60
  }
1✔
61

62
  public RuleChecker withSuccessScore(Integer successScore) {
63
    this.successScore = successScore;
1✔
64
    return this;
1✔
65
  }
66

67
  public void setNaScore(Integer naScore) {
68
    this.naScore = naScore;
1✔
69
  }
1✔
70

71
  public Integer getNaScore() {
72
    return naScore;
1✔
73
  }
74

75
  public RuleChecker withNaScore(Integer naScore) {
76
    this.naScore = naScore;
1✔
77
    return this;
1✔
78
  }
79

80
  @Override
81
  public String getHeaderWithoutId() {
82
    return header;
1✔
83
  }
84

85
  public String getHeader() {
86
    return header + ":" + getId();
1✔
87
  }
88

89
  public String getIdOrHeader() {
90
    return id != null ? id : header + ":" + getId();
1✔
91
  }
92

93
  public String getHeader(RuleCheckingOutputType outputType) {
94
    return getHeader() + getKeySuffix(outputType);
1✔
95
  }
96

97
  private static String getKeySuffix(RuleCheckingOutputType outputType) {
98
    String suffix = "";
1✔
99
    if (outputType.equals(RuleCheckingOutputType.STATUS))
1✔
100
      suffix = ":status";
1✔
101
    else if (outputType.equals(RuleCheckingOutputType.SCORE))
1✔
102
      suffix = ":score";
1✔
103
    return suffix;
1✔
104
  }
105

106
  public String getIdOrHeader(RuleCheckingOutputType outputType) {
107
    return getIdOrHeader() + getKeySuffix(outputType);
1✔
108
  }
109

110
  protected void addOutput(FieldCounter<RuleCheckerOutput> results,
111
                           boolean isNA,
112
                           boolean allPassed,
113
                           RuleCheckingOutputType outputType) {
114
    addOutput(results, isNA, allPassed, outputType, null, null);
1✔
115
  }
1✔
116

117
  protected void addOutput(FieldCounter<RuleCheckerOutput> results,
118
                           boolean isNA,
119
                           boolean allPassed,
120
                           RuleCheckingOutputType outputType,
121
                           Integer instanceCount,
122
                           Integer failureCount) {
123

124
    RuleCheckerOutput output = new RuleCheckerOutput(this, isNA, allPassed);
1✔
125
    if (instanceCount != null)
1✔
126
      output.setInstanceCount(instanceCount);
1✔
127
    if (failureCount != null)
1✔
128
      output.setFailureCount(failureCount);
1✔
129

130
    if (outputType.equals(RuleCheckingOutputType.STATUS) || outputType.equals(RuleCheckingOutputType.SCORE)) {
1✔
131
      results.put(getIdOrHeader(), output.setOutputType(outputType));
1✔
132
    } else {
133
      try {
134
        RuleCheckerOutput output2 = (RuleCheckerOutput) output.clone();
1✔
135
        results.put(getIdOrHeader(RuleCheckingOutputType.STATUS), output.setOutputType(RuleCheckingOutputType.STATUS));
1✔
136
        results.put(getIdOrHeader(RuleCheckingOutputType.SCORE), output2.setOutputType(RuleCheckingOutputType.SCORE));
1✔
137
      } catch (CloneNotSupportedException e) {
×
138
        e.printStackTrace(System.err);
×
139
      }
1✔
140
    }
141
  }
1✔
142

143
  public void setHidden() {
144
    this.hidden = true;
1✔
145
  }
1✔
146

147
  public boolean isHidden() {
148
    return hidden;
1✔
149
  }
150

151
  public void setDebug() {
152
    this.debug = true;
1✔
153
  }
1✔
154

155
  public boolean isDebug() {
156
    return debug;
1✔
157
  }
158

159
  @Override
160
  public void setMandatory() {
161
    this.mandatory = true;
×
162
  }
×
163

164
  @Override
165
  public boolean isMandatory() {
166
    return mandatory;
1✔
167
  }
168

169
  @Override
170
  public ApplicationScope getScope() {
171
    return scope;
×
172
  }
173

174
  @Override
175
  public void setScope(ApplicationScope scope) {
176
    this.scope = scope;
1✔
177
  }
1✔
178

179
  @Override
180
  public boolean hasScope() {
181
    return scope != null;
1✔
182
  }
183

184
  public Boolean countInstances() {
185
    return countInstances;
1✔
186
  }
187

188
  public void setCountInstances(Boolean countInstances) {
189
    this.countInstances = countInstances;
1✔
190
  }
1✔
191

192
  /**
193
   * Decide if the criterium has been passed base on the results, and the scope
194
   * @param passCount The number of times the check has been passed
195
   * @param hasFailed Whether there were a failure
196
   * @return
197
   */
198
  public boolean isPassed(int passCount, boolean hasFailed) {
199
    boolean passed = false;
1✔
200
    if (hasScope()) {
1✔
NEW
201
      if (scopeIsAllOf()) {
×
NEW
202
        passed = passCount > 0 && !hasFailed;
×
NEW
203
      } else if (scopeIsAnyOf()) {
×
NEW
204
        passed = passCount > 0;
×
NEW
205
      } else if (scopeIsOneOf()) {
×
NEW
206
        passed = passCount == 1;
×
207
      }
208
    } else {
209
      passed = passCount > 0;
1✔
210
    }
211
    return passed;
1✔
212
  }
213

214
  protected boolean scopeIsAllOf() {
215
    return hasScope() && scope.equals(ApplicationScope.allOf);
1✔
216
  }
217

218
  protected boolean scopeIsAnyOf() {
NEW
219
    return hasScope() && scope.equals(ApplicationScope.anyOf);
×
220
  }
221

222
  protected boolean scopeIsOneOf() {
NEW
223
    return hasScope() && scope.equals(ApplicationScope.oneOf);
×
224
  }
225
}
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