MetadataFieldUtils.java

1
package org.cardanofoundation.explorer.api.util;
2
3
import java.util.ArrayList;
4
import java.util.Arrays;
5
import java.util.HashMap;
6
7
import lombok.extern.log4j.Log4j2;
8
9
import com.bloxbean.cardano.client.util.HexUtil;
10
11
import org.cardanofoundation.explorer.api.common.constant.CommonConstant;
12
import org.cardanofoundation.explorer.api.common.enumeration.FormatFieldType;
13
14
@Log4j2
15
public class MetadataFieldUtils {
16
17
  public static final String BASE64_PREFIX = "data:";
18
  public static final String RAW_BYTE_PREFIX = "0x";
19
  public static final String BASE64 = "base64";
20
  public static final String[] MEDIA_TYPE_PREFIX = {
21
    "image/", "application/", "audio/", "example/", "font/", "model/", "text/", "video/"
22
  };
23
  public static final String[] IMAGE_PREFIX = {"http://", "https://", "ipfs://", "ar://"};
24
  private static final String SONG_DURATION_REGEX = "^P(?!$)(T(?=\\d)(\\d+H)?(\\d+M)?(\\d+S)?)$";
25
26
  public static FormatFieldType getFormatTypeByObject(Object object) {
27 1 1. getFormatTypeByObject : negated conditional → SURVIVED
    if (object instanceof String str) {
28 1 1. getFormatTypeByObject : negated conditional → SURVIVED
      if (Arrays.stream(IMAGE_PREFIX).anyMatch(str::startsWith)
29 2 1. getFormatTypeByObject : negated conditional → SURVIVED
2. getFormatTypeByObject : negated conditional → NO_COVERAGE
          || str.startsWith(BASE64_PREFIX) && str.contains(BASE64)) {
30 1 1. getFormatTypeByObject : replaced return value with null for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::getFormatTypeByObject → SURVIVED
        return FormatFieldType.URI;
31
      }
32 1 1. getFormatTypeByObject : negated conditional → SURVIVED
      if (str.startsWith(MEDIA_TYPE_PREFIX[0])) {
33 1 1. getFormatTypeByObject : replaced return value with null for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::getFormatTypeByObject → SURVIVED
        return FormatFieldType.IMAGE_SLASH_MIME_SUB_TYPE;
34
      }
35 1 1. getFormatTypeByObject : negated conditional → SURVIVED
      if (Arrays.stream(MEDIA_TYPE_PREFIX).anyMatch(str::startsWith)) {
36 1 1. getFormatTypeByObject : replaced return value with null for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::getFormatTypeByObject → SURVIVED
        return FormatFieldType.MIME_TYPE;
37
      }
38 1 1. getFormatTypeByObject : negated conditional → SURVIVED
      if (str.matches(SONG_DURATION_REGEX)) {
39 1 1. getFormatTypeByObject : replaced return value with null for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::getFormatTypeByObject → NO_COVERAGE
        return FormatFieldType.STRING_ISO8601_DURATION_FORMAT;
40
      }
41 1 1. getFormatTypeByObject : negated conditional → SURVIVED
      if (isHexString(str)) {
42 1 1. getFormatTypeByObject : replaced return value with null for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::getFormatTypeByObject → NO_COVERAGE
        return FormatFieldType.RAW_BYTES;
43
      }
44 1 1. getFormatTypeByObject : replaced return value with null for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::getFormatTypeByObject → SURVIVED
      return FormatFieldType.STRING;
45 1 1. getFormatTypeByObject : negated conditional → SURVIVED
    } else if (object instanceof Integer) {
46 1 1. getFormatTypeByObject : replaced return value with null for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::getFormatTypeByObject → NO_COVERAGE
      return FormatFieldType.INTEGER;
47 1 1. getFormatTypeByObject : negated conditional → SURVIVED
    } else if (object instanceof Long) {
48 1 1. getFormatTypeByObject : replaced return value with null for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::getFormatTypeByObject → NO_COVERAGE
      return FormatFieldType.LONG;
49 1 1. getFormatTypeByObject : negated conditional → SURVIVED
    } else if (object instanceof Double) {
50 1 1. getFormatTypeByObject : replaced return value with null for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::getFormatTypeByObject → NO_COVERAGE
      return FormatFieldType.DOUBLE;
51 1 1. getFormatTypeByObject : negated conditional → SURVIVED
    } else if (object instanceof Boolean) {
52 1 1. getFormatTypeByObject : replaced return value with null for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::getFormatTypeByObject → NO_COVERAGE
      return FormatFieldType.BOOLEAN;
53 1 1. getFormatTypeByObject : negated conditional → SURVIVED
    } else if (object instanceof ArrayList<?> arr) {
54 2 1. getFormatTypeByObject : negated conditional → NO_COVERAGE
2. getFormatTypeByObject : changed conditional boundary → NO_COVERAGE
      if (arr.size() >= 2
55
          && Arrays.stream(CommonConstant.IMAGE_PREFIX)
56 1 1. getFormatTypeByObject : negated conditional → NO_COVERAGE
              .anyMatch(arr.get(0).toString()::startsWith)) {
57 1 1. getFormatTypeByObject : replaced return value with null for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::getFormatTypeByObject → NO_COVERAGE
        return FormatFieldType.URI_ARRAY_PARTS;
58
      }
59 1 1. getFormatTypeByObject : negated conditional → NO_COVERAGE
      if (arr.stream().allMatch(String.class::isInstance)) {
60 1 1. getFormatTypeByObject : replaced return value with null for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::getFormatTypeByObject → NO_COVERAGE
        return FormatFieldType.ARRAY_STRING;
61
      }
62 1 1. getFormatTypeByObject : replaced return value with null for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::getFormatTypeByObject → NO_COVERAGE
      return FormatFieldType.ARRAY;
63 1 1. getFormatTypeByObject : negated conditional → SURVIVED
    } else if (object == null) {
64 1 1. getFormatTypeByObject : replaced return value with null for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::getFormatTypeByObject → SURVIVED
      return FormatFieldType.NULL_OR_EMPTY;
65 1 1. getFormatTypeByObject : negated conditional → NO_COVERAGE
    } else if (object instanceof HashMap<?, ?> linkMap) {
66
      if (linkMap.entrySet().stream()
67 1 1. getFormatTypeByObject : negated conditional → NO_COVERAGE
          .allMatch(
68 3 1. lambda$getFormatTypeByObject$0 : negated conditional → NO_COVERAGE
2. lambda$getFormatTypeByObject$0 : negated conditional → NO_COVERAGE
3. lambda$getFormatTypeByObject$0 : replaced boolean return with true for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::lambda$getFormatTypeByObject$0 → NO_COVERAGE
              entry -> entry.getKey() instanceof String && entry.getValue() instanceof String)) {
69 1 1. getFormatTypeByObject : replaced return value with null for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::getFormatTypeByObject → NO_COVERAGE
        return FormatFieldType.MAP_STRING_STRING;
70
      }
71
    }
72
73 1 1. getFormatTypeByObject : replaced return value with null for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::getFormatTypeByObject → NO_COVERAGE
    return FormatFieldType.UNKNOWN;
74
  }
75
76
  public static boolean isHexString(String str) {
77
    try {
78
      HexUtil.decodeHexString(str);
79 2 1. isHexString : replaced boolean return with true for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::isHexString → SURVIVED
2. isHexString : replaced boolean return with false for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::isHexString → SURVIVED
      return str.startsWith(RAW_BYTE_PREFIX);
80
    } catch (Exception ex) {
81
      log.error("String is not raw bytes");
82
      log.error("Error: " + ex.getMessage());
83
    }
84 1 1. isHexString : replaced boolean return with true for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::isHexString → SURVIVED
    return false;
85
  }
86
}

Mutations

27

1.1
Location : getFormatTypeByObject
Killed by : none
negated conditional → SURVIVED

28

1.1
Location : getFormatTypeByObject
Killed by : none
negated conditional → SURVIVED

29

1.1
Location : getFormatTypeByObject
Killed by : none
negated conditional → SURVIVED

2.2
Location : getFormatTypeByObject
Killed by : none
negated conditional → NO_COVERAGE

30

1.1
Location : getFormatTypeByObject
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::getFormatTypeByObject → SURVIVED

32

1.1
Location : getFormatTypeByObject
Killed by : none
negated conditional → SURVIVED

33

1.1
Location : getFormatTypeByObject
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::getFormatTypeByObject → SURVIVED

35

1.1
Location : getFormatTypeByObject
Killed by : none
negated conditional → SURVIVED

36

1.1
Location : getFormatTypeByObject
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::getFormatTypeByObject → SURVIVED

38

1.1
Location : getFormatTypeByObject
Killed by : none
negated conditional → SURVIVED

39

1.1
Location : getFormatTypeByObject
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::getFormatTypeByObject → NO_COVERAGE

41

1.1
Location : getFormatTypeByObject
Killed by : none
negated conditional → SURVIVED

42

1.1
Location : getFormatTypeByObject
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::getFormatTypeByObject → NO_COVERAGE

44

1.1
Location : getFormatTypeByObject
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::getFormatTypeByObject → SURVIVED

45

1.1
Location : getFormatTypeByObject
Killed by : none
negated conditional → SURVIVED

46

1.1
Location : getFormatTypeByObject
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::getFormatTypeByObject → NO_COVERAGE

47

1.1
Location : getFormatTypeByObject
Killed by : none
negated conditional → SURVIVED

48

1.1
Location : getFormatTypeByObject
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::getFormatTypeByObject → NO_COVERAGE

49

1.1
Location : getFormatTypeByObject
Killed by : none
negated conditional → SURVIVED

50

1.1
Location : getFormatTypeByObject
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::getFormatTypeByObject → NO_COVERAGE

51

1.1
Location : getFormatTypeByObject
Killed by : none
negated conditional → SURVIVED

52

1.1
Location : getFormatTypeByObject
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::getFormatTypeByObject → NO_COVERAGE

53

1.1
Location : getFormatTypeByObject
Killed by : none
negated conditional → SURVIVED

54

1.1
Location : getFormatTypeByObject
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : getFormatTypeByObject
Killed by : none
changed conditional boundary → NO_COVERAGE

56

1.1
Location : getFormatTypeByObject
Killed by : none
negated conditional → NO_COVERAGE

57

1.1
Location : getFormatTypeByObject
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::getFormatTypeByObject → NO_COVERAGE

59

1.1
Location : getFormatTypeByObject
Killed by : none
negated conditional → NO_COVERAGE

60

1.1
Location : getFormatTypeByObject
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::getFormatTypeByObject → NO_COVERAGE

62

1.1
Location : getFormatTypeByObject
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::getFormatTypeByObject → NO_COVERAGE

63

1.1
Location : getFormatTypeByObject
Killed by : none
negated conditional → SURVIVED

64

1.1
Location : getFormatTypeByObject
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::getFormatTypeByObject → SURVIVED

65

1.1
Location : getFormatTypeByObject
Killed by : none
negated conditional → NO_COVERAGE

67

1.1
Location : getFormatTypeByObject
Killed by : none
negated conditional → NO_COVERAGE

68

1.1
Location : lambda$getFormatTypeByObject$0
Killed by : none
negated conditional → NO_COVERAGE

2.2
Location : lambda$getFormatTypeByObject$0
Killed by : none
negated conditional → NO_COVERAGE

3.3
Location : lambda$getFormatTypeByObject$0
Killed by : none
replaced boolean return with true for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::lambda$getFormatTypeByObject$0 → NO_COVERAGE

69

1.1
Location : getFormatTypeByObject
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::getFormatTypeByObject → NO_COVERAGE

73

1.1
Location : getFormatTypeByObject
Killed by : none
replaced return value with null for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::getFormatTypeByObject → NO_COVERAGE

79

1.1
Location : isHexString
Killed by : none
replaced boolean return with true for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::isHexString → SURVIVED

2.2
Location : isHexString
Killed by : none
replaced boolean return with false for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::isHexString → SURVIVED

84

1.1
Location : isHexString
Killed by : none
replaced boolean return with true for org/cardanofoundation/explorer/api/util/MetadataFieldUtils::isHexString → SURVIVED

Active mutators

Tests examined


Report generated by PIT 1.14.2