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

pkiraly / metadata-qa-api / #716

27 Aug 2025 07:52AM UTC coverage: 86.526% (-0.03%) from 86.556%
#716

push

pkiraly
Fix test for content type

5651 of 6531 relevant lines covered (86.53%)

0.87 hits per line

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

75.0
/src/main/java/de/gwdg/metadataqa/api/rule/singlefieldchecker/ContentTypeChecker.java
1
package de.gwdg.metadataqa.api.rule.singlefieldchecker;
2

3
import de.gwdg.metadataqa.api.counter.FieldCounter;
4
import de.gwdg.metadataqa.api.json.DataElement;
5
import de.gwdg.metadataqa.api.model.XmlFieldInstance;
6
import de.gwdg.metadataqa.api.model.selector.Selector;
7
import de.gwdg.metadataqa.api.rule.RuleCheckerOutput;
8
import de.gwdg.metadataqa.api.rule.RuleCheckingOutputStatus;
9
import de.gwdg.metadataqa.api.rule.RuleCheckingOutputType;
10
import de.gwdg.metadataqa.api.util.ContentTypeExtractor;
11

12
import java.io.IOException;
13
import java.util.List;
14
import java.util.logging.Logger;
15
import java.util.regex.Pattern;
16

17
public class ContentTypeChecker extends SingleFieldChecker {
18

19
  private static final long serialVersionUID = 6376677768166208863L;
20
  public static final String PREFIX = "contentType";
21

22
  private static final Logger LOGGER = Logger.getLogger(ContentTypeChecker.class.getCanonicalName());
1✔
23

24
  protected List<String> expectedContentTypes;
25
  private ContentTypeExtractor contentTypeExtractor;
26
  private Pattern skippableUrl;
27

28
  public ContentTypeChecker(DataElement field, List<String> expectedContentTypes) {
29
    this(field, field.getLabel(), expectedContentTypes, ContentTypeExtractor.DEFAULT_TIMEOUT);
1✔
30
  }
1✔
31

32
  public ContentTypeChecker(DataElement field, String header, List<String> expectedContentTypes, int timeout) {
33
    super(field, header + ":" + PREFIX);
1✔
34
    this.expectedContentTypes = expectedContentTypes;
1✔
35
    contentTypeExtractor = new ContentTypeExtractor(timeout);
1✔
36
  }
1✔
37

38
  @Override
39
  public void update(Selector selector, FieldCounter<RuleCheckerOutput> results, RuleCheckingOutputType outputType) {
40
    if (isDebug())
1✔
41
      LOGGER.info(this.getClass().getSimpleName() + " " + this.id);
×
42

43
    var allPassed = true;
1✔
44
    var isNA = true;
1✔
45
    int instanceCount = 0;
1✔
46
    int failureCount = 0;
1✔
47
    List<XmlFieldInstance> instances = selector.get(field);
1✔
48
    if (instances != null && !instances.isEmpty()) {
1✔
49
      for (XmlFieldInstance instance : instances) {
1✔
50
        if (instance.hasValue()) {
1✔
51
          if (countInstances())
1✔
52
            instanceCount++;
1✔
53
          isNA = false;
1✔
54
          try {
55
            String url = instance.getValue();
1✔
56
            if (skippableUrl == null || !skippableUrl.matcher(url).find()) {
1✔
57
              String contentType = contentTypeExtractor.getContentType(url);
1✔
58
              if (isDebug())
1✔
59
                LOGGER.info(String.format("value: '%s' -> '%s'", url, contentType));
×
60
              if (contentType == null || !expectedContentTypes.contains(contentType)) {
1✔
61
                allPassed = false;
1✔
62
                if (countInstances())
1✔
63
                  failureCount++;
×
64
              }
65
            }
66
          } catch (IOException e) {
×
67
            LOGGER.warning(String.format("%s: %s", e.getClass().getSimpleName(), e.getMessage()));
×
68
            allPassed = false;
×
69
            if (countInstances())
×
70
              failureCount++;
×
71
          }
1✔
72
          if (!countInstances() && !allPassed)
1✔
73
            break;
1✔
74
        }
75
      }
1✔
76
    }
77

78
    addOutput(results, isNA, allPassed, outputType, instanceCount, failureCount);
1✔
79
    if (isDebug())
1✔
80
      LOGGER.info(this.getClass().getSimpleName() + " " + this.id + ") result: " + RuleCheckingOutputStatus.create(isNA, allPassed, isMandatory()));
×
81
  }
1✔
82

83
  public void setSkippableUrl(String skippableUrl) {
84
    this.skippableUrl = Pattern.compile(skippableUrl);
×
85
  }
×
86
}
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