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

pkiraly / metadata-qa-api / #692

04 Jun 2025 11:56AM UTC coverage: 86.93% (-0.3%) from 87.231%
#692

push

pkiraly
Merge branch 'main' of github.com:pkiraly/metadata-qa-api

5547 of 6381 relevant lines covered (86.93%)

0.87 hits per line

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

86.21
/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
import de.gwdg.metadataqa.api.json.DataElement;
6
import de.gwdg.metadataqa.api.model.selector.Selector;
7
import org.apache.commons.lang3.StringUtils;
8

9
import java.util.logging.Logger;
10

11
public abstract class BaseRuleChecker implements RuleChecker {
1✔
12

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

15
  protected String id;
16
  protected Integer failureScore;
17
  protected Integer successScore;
18
  protected Integer naScore;
19
  protected String header;
20
  protected Boolean hidden = false;
1✔
21
  private Boolean debug = false;
1✔
22
  private Boolean mandatory = false;
1✔
23
  private ApplicationScope scope;
24
  private String valuePath;
25

26
  /**
27
   * A flag to denote if the RuleChecker should count the number instances and failures
28
   */
29
  private Boolean countInstances = false;
1✔
30

31
  @Override
32
  public String getId() {
33
    return id == null ? String.valueOf(0) : id;
1✔
34
  }
35

36
  @Override
37
  public void setId(String id) {
38
    this.id = id;
1✔
39
  }
1✔
40

41
  @Override
42
  public Integer getFailureScore() {
43
    return failureScore;
1✔
44
  }
45

46
  @Override
47
  public void setFailureScore(Integer failureScore) {
48
    this.failureScore = failureScore;
1✔
49
  }
1✔
50

51
  public RuleChecker withFailureScore(Integer failureScore) {
52
    this.failureScore = failureScore;
1✔
53
    return this;
1✔
54
  }
55

56
  @Override
57
  public Integer getSuccessScore() {
58
    return successScore;
1✔
59
  }
60

61
  @Override
62
  public void setSuccessScore(Integer successScore) {
63
    this.successScore = successScore;
1✔
64
  }
1✔
65

66
  public RuleChecker withSuccessScore(Integer successScore) {
67
    this.successScore = successScore;
1✔
68
    return this;
1✔
69
  }
70

71
  public void setNaScore(Integer naScore) {
72
    this.naScore = naScore;
1✔
73
  }
1✔
74

75
  public Integer getNaScore() {
76
    return naScore;
1✔
77
  }
78

79
  public RuleChecker withNaScore(Integer naScore) {
80
    this.naScore = naScore;
1✔
81
    return this;
1✔
82
  }
83

84
  @Override
85
  public String getHeaderWithoutId() {
86
    return header;
1✔
87
  }
88

89
  public String getHeader() {
90
    return header + ":" + getId();
1✔
91
  }
92

93
  public String getIdOrHeader() {
94
    return id != null ? id : header + ":" + getId();
1✔
95
  }
96

97
  public String getHeader(RuleCheckingOutputType outputType) {
98
    return getHeader() + getKeySuffix(outputType);
1✔
99
  }
100

101
  private static String getKeySuffix(RuleCheckingOutputType outputType) {
102
    String suffix = "";
1✔
103
    if (outputType.equals(RuleCheckingOutputType.STATUS))
1✔
104
      suffix = ":status";
1✔
105
    else if (outputType.equals(RuleCheckingOutputType.SCORE))
1✔
106
      suffix = ":score";
1✔
107
    return suffix;
1✔
108
  }
109

110
  public String getIdOrHeader(RuleCheckingOutputType outputType) {
111
    return getIdOrHeader() + getKeySuffix(outputType);
1✔
112
  }
113

114
  protected void addOutput(FieldCounter<RuleCheckerOutput> results,
115
                           boolean isNA,
116
                           boolean allPassed,
117
                           RuleCheckingOutputType outputType) {
118
    addOutput(results, isNA, allPassed, outputType, null, null);
1✔
119
  }
1✔
120

121
  protected void addOutput(FieldCounter<RuleCheckerOutput> results,
122
                           boolean isNA,
123
                           boolean allPassed,
124
                           RuleCheckingOutputType outputType,
125
                           Integer instanceCount,
126
                           Integer failureCount) {
127

128
    RuleCheckerOutput output = new RuleCheckerOutput(this, isNA, allPassed);
1✔
129
    if (instanceCount != null)
1✔
130
      output.setInstanceCount(instanceCount);
1✔
131
    if (failureCount != null)
1✔
132
      output.setFailureCount(failureCount);
1✔
133

134
    addOutput(results, output, outputType);
1✔
135
  }
1✔
136

137
  protected void addOutput(FieldCounter<RuleCheckerOutput> results,
138
                           RuleCheckerOutput output,
139
                           RuleCheckingOutputType outputType) {
140
    if (outputType.equals(RuleCheckingOutputType.STATUS) || outputType.equals(RuleCheckingOutputType.SCORE)) {
1✔
141
      results.put(getIdOrHeader(), output.setOutputType(outputType));
1✔
142
    } else {
143
      try {
144
        RuleCheckerOutput output2 = (RuleCheckerOutput) output.copy();
1✔
145
        results.put(getIdOrHeader(RuleCheckingOutputType.STATUS), output.setOutputType(RuleCheckingOutputType.STATUS));
1✔
146
        results.put(getIdOrHeader(RuleCheckingOutputType.SCORE), output2.setOutputType(RuleCheckingOutputType.SCORE));
1✔
147
      } catch (CloneNotSupportedException e) {
×
148
        e.printStackTrace(System.err);
×
149
      }
1✔
150
    }
151
  }
1✔
152

153
  public void setHidden() {
154
    this.hidden = true;
1✔
155
  }
1✔
156

157
  public boolean isHidden() {
158
    return hidden;
1✔
159
  }
160

161
  public void setDebug() {
162
    this.debug = true;
1✔
163
  }
1✔
164

165
  public boolean isDebug() {
166
    return debug;
1✔
167
  }
168

169
  @Override
170
  public void setMandatory() {
171
    this.mandatory = true;
×
172
  }
×
173

174
  @Override
175
  public boolean isMandatory() {
176
    return mandatory;
1✔
177
  }
178

179
  @Override
180
  public ApplicationScope getScope() {
181
    return scope;
×
182
  }
183

184
  @Override
185
  public void setScope(ApplicationScope scope) {
186
    this.scope = scope;
1✔
187
  }
1✔
188

189
  @Override
190
  public boolean hasScope() {
191
    return scope != null;
1✔
192
  }
193

194
  public String getValuePath() {
195
    return valuePath;
1✔
196
  }
197

198
  @Override
199
  public void setValuePath(String valuePath) {
200
    System.err.println("setValuePath: " + valuePath);
1✔
201
    this.valuePath = valuePath;
1✔
202
  }
1✔
203

204
  @Override
205
  public boolean hasValuePath() {
206
    return StringUtils.isNotBlank(valuePath);
1✔
207
  }
208

209
  public boolean countInstances() {
210
    return countInstances;
1✔
211
  }
212

213
  public void setCountInstances(Boolean countInstances) {
214
    this.countInstances = countInstances;
1✔
215
  }
1✔
216

217
  /**
218
   * Decide if the criterium has been passed base on the results, and the scope
219
   * @param passCount The number of times the check has been passed
220
   * @param hasFailed Whether there were a failure
221
   * @return
222
   */
223
  public boolean isPassed(int passCount, boolean hasFailed) {
224
    boolean passed = false;
1✔
225
    if (hasScope()) {
1✔
226
      if (scopeIsAllOf()) {
×
227
        passed = passCount > 0 && !hasFailed;
×
228
      } else if (scopeIsAnyOf()) {
×
229
        passed = passCount > 0;
×
230
      } else if (scopeIsOneOf()) {
×
231
        passed = passCount == 1;
×
232
      }
233
    } else {
234
      passed = passCount > 0;
1✔
235
    }
236
    return passed;
1✔
237
  }
238

239
  protected boolean scopeIsAllOf() {
240
    return hasScope() && scope.equals(ApplicationScope.allOf);
1✔
241
  }
242

243
  protected boolean scopeIsAnyOf() {
244
    return hasScope() && scope.equals(ApplicationScope.anyOf);
1✔
245
  }
246

247
  protected boolean scopeIsOneOf() {
248
    return hasScope() && scope.equals(ApplicationScope.oneOf);
×
249
  }
250

251
}
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