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

knowledgepixels / nanopub-registry / 20569441615

29 Dec 2025 09:25AM UTC coverage: 8.51% (+0.9%) from 7.6%
20569441615

push

github

ashleycaselli
test(Utils): add unit tests for peer URLs handling

49 of 588 branches covered (8.33%)

Branch coverage included in aggregate %.

156 of 1821 relevant lines covered (8.57%)

1.25 hits per line

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

76.88
src/main/java/com/knowledgepixels/registry/Utils.java
1
package com.knowledgepixels.registry;
2

3
import com.github.jsonldjava.shaded.com.google.common.base.Charsets;
4
import com.github.jsonldjava.shaded.com.google.common.reflect.TypeToken;
5
import com.google.common.hash.Hashing;
6
import com.google.gson.Gson;
7
import com.google.gson.JsonIOException;
8
import com.google.gson.JsonSyntaxException;
9
import com.mongodb.client.ClientSession;
10
import io.vertx.ext.web.RoutingContext;
11
import net.trustyuri.TrustyUriUtils;
12
import org.apache.commons.lang.StringUtils;
13
import org.commonjava.mimeparse.MIMEParse;
14
import org.eclipse.rdf4j.common.exception.RDF4JException;
15
import org.eclipse.rdf4j.model.IRI;
16
import org.eclipse.rdf4j.model.Resource;
17
import org.eclipse.rdf4j.model.Statement;
18
import org.eclipse.rdf4j.model.ValueFactory;
19
import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
20
import org.nanopub.MalformedNanopubException;
21
import org.nanopub.Nanopub;
22
import org.nanopub.NanopubImpl;
23
import org.nanopub.NanopubUtils;
24
import org.nanopub.extra.setting.NanopubSetting;
25
import org.nanopub.vocabulary.NPX;
26
import org.slf4j.Logger;
27
import org.slf4j.LoggerFactory;
28

29
import java.io.File;
30
import java.io.IOException;
31
import java.io.InputStreamReader;
32
import java.lang.reflect.Type;
33
import java.net.URI;
34
import java.net.URISyntaxException;
35
import java.net.URLEncoder;
36
import java.nio.charset.StandardCharsets;
37
import java.util.*;
38

39
public class Utils {
40

41
    private static final Logger logger = LoggerFactory.getLogger(Utils.class);
9✔
42

43
    private Utils() {
44
    }  // no instances allowed
45

46
    public static String getMimeType(RoutingContext context, String supported) {
47
        List<String> supportedList = Arrays.asList(StringUtils.split(supported, ','));
×
48
        String mimeType = supportedList.getFirst();
×
49
        try {
50
            mimeType = MIMEParse.bestMatch(supportedList, context.request().getHeader("Accept"));
×
51
        } catch (Exception ex) {
×
52
            logger.error("Error parsing Accept header.", ex);
×
53
        }
×
54
        return mimeType;
×
55
    }
56

57
    public static String urlEncode(Object o) {
58
        return URLEncoder.encode((o == null ? "" : o.toString()), StandardCharsets.UTF_8);
27✔
59
    }
60

61
    public static String getHash(String s) {
62
        return Hashing.sha256().hashString(s, Charsets.UTF_8).toString();
18✔
63
    }
64

65
    public static Set<IRI> getInvalidatedNanopubIds(Nanopub np) {
66
        Set<IRI> l = new HashSet<IRI>();
×
67
        for (Statement st : NanopubUtils.getStatements(np)) {
×
68
            if (!(st.getObject() instanceof IRI)) continue;
×
69
            Resource s = st.getSubject();
×
70
            IRI p = st.getPredicate();
×
71
            if ((p.equals(NPX.RETRACTS) || p.equals(NPX.INVALIDATES)) || (p.equals(NPX.SUPERSEDES) && s.equals(np.getUri()))) {
×
72
                if (TrustyUriUtils.isPotentialTrustyUri(st.getObject().stringValue())) {
×
73
                    l.add((IRI) st.getObject());
×
74
                }
75
            }
76
        }
×
77
        return l;
×
78
    }
79

80
    private static ReadsEnvironment ENV_READER = new ReadsEnvironment(System::getenv);
15✔
81

82
    static void setEnvReader(ReadsEnvironment reader) {
83
        ENV_READER = reader;
6✔
84
    }
3✔
85

86
    public static String getEnv(String name, String defaultValue) {
87
        String value = ENV_READER.getEnv(name);
12✔
88
        if (value == null) value = defaultValue;
12✔
89
        return value;
6✔
90
    }
91

92
    public static String getTypeHash(ClientSession mongoSession, Object type) {
93
        String typeHash = Utils.getHash(type.toString());
12✔
94
        if (type.toString().equals("$")) {
15✔
95
            typeHash = "$";
9✔
96
        } else {
97
            RegistryDB.recordHash(mongoSession, type.toString());
12✔
98
        }
99
        return typeHash;
6✔
100
    }
101

102
    public static String getAgentLabel(String agentId) {
103
        if (agentId == null || agentId.isBlank()) {
15✔
104
            throw new IllegalArgumentException("Agent ID cannot be null or blank");
15✔
105
        }
106
        agentId = agentId.replaceFirst("^https://orcid\\.org/", "orcid:");
15✔
107
        if (agentId.length() > 55) {
12✔
108
            return agentId.substring(0, 50) + "...";
18✔
109
        }
110
        return agentId;
6✔
111
    }
112

113
    public static boolean isUnloadedStatus(String status) {
114
        if (status.equals(EntryStatus.seen.getValue())) return true;  // only exists in "accounts_loading"?
21✔
115
        return status.equals(EntryStatus.skipped.getValue());
15✔
116
    }
117

118
    public static boolean isCoreLoadedStatus(String status) {
119
        if (status.equals(EntryStatus.visited.getValue())) return true;  // only exists in "accounts_loading"?
21✔
120
        if (status.equals(EntryStatus.expanded.getValue())) return true;  // only exists in "accounts_loading"?
21✔
121
        if (status.equals(EntryStatus.processed.getValue())) return true;  // only exists in "accounts_loading"?
21✔
122
        if (status.equals(EntryStatus.aggregated.getValue())) return true;  // only exists in "accounts_loading"?
21✔
123
        if (status.equals(EntryStatus.approved.getValue())) return true;  // only exists in "accounts_loading"?
21✔
124
        if (status.equals(EntryStatus.contested.getValue())) return true;
21✔
125
        if (status.equals(EntryStatus.toLoad.getValue())) return true;  // only exists in "accounts_loading"?
21✔
126
        return status.equals(EntryStatus.loaded.getValue());
15✔
127
    }
128

129
    public static boolean isFullyLoadedStatus(String status) {
130
        return status.equals(EntryStatus.loaded.getValue());
15✔
131
    }
132

133
    private static ValueFactory vf = SimpleValueFactory.getInstance();
6✔
134
    public static final IRI APPROVES_OF = vf.createIRI("http://purl.org/nanopub/x/approvesOf");
12✔
135

136
    public static final String TYPE_JSON = "application/json";
137
    public static final String TYPE_TRIG = "application/trig";
138
    public static final String TYPE_JELLY = "application/x-jelly-rdf";
139
    public static final String TYPE_JSONLD = "application/ld+json";
140
    public static final String TYPE_NQUADS = "application/n-quads";
141
    public static final String TYPE_TRIX = "application/trix";
142
    public static final String TYPE_HTML = "text/html";
143

144
    // Content types supported on a ListPage
145
    public static final String SUPPORTED_TYPES_LIST = TYPE_JSON + "," + TYPE_JELLY + "," + TYPE_HTML;
146
    // Content types supported on a NanopubPage
147
    public static final String SUPPORTED_TYPES_NANOPUB = TYPE_TRIG + "," + TYPE_JELLY + "," + TYPE_JSONLD + "," + TYPE_NQUADS + "," + TYPE_TRIX + "," + TYPE_HTML;
148

149
    private static Map<String, String> extensionTypeMap;
150

151
    public static String getType(String extension) {
152
        if (extension == null) {
6✔
153
            return null;
6✔
154
        }
155
        if (extensionTypeMap == null) {
6✔
156
            extensionTypeMap = new HashMap<>();
12✔
157
            extensionTypeMap.put("trig", TYPE_TRIG);
15✔
158
            extensionTypeMap.put("jelly", TYPE_JELLY);
15✔
159
            extensionTypeMap.put("jsonld", TYPE_JSONLD);
15✔
160
            extensionTypeMap.put("nq", TYPE_NQUADS);
15✔
161
            extensionTypeMap.put("xml", TYPE_TRIX);
15✔
162
            extensionTypeMap.put("html", TYPE_HTML);
15✔
163
            extensionTypeMap.put("json", TYPE_JSON);
15✔
164
        }
165
        return extensionTypeMap.get(extension);
15✔
166
    }
167

168
    private static List<String> peerUrls;
169

170
    public static List<String> getPeerUrls() {
171
        if (peerUrls == null) {
6✔
172
            peerUrls = new ArrayList<>();
12✔
173
            String envPeerUrls = getEnv("REGISTRY_PEER_URLS", "");
12✔
174
            String thisRegistryUrl = getEnv("REGISTRY_SERVICE_URL", "");
12✔
175
            if (!envPeerUrls.isEmpty()) {
9✔
176
                for (String peerUrl : envPeerUrls.split(";")) {
57✔
177
                    if (thisRegistryUrl.equals(peerUrl)) {
12!
178
                        continue;
×
179
                    }
180
                    peerUrls.add(peerUrl);
12✔
181
                }
182
            } else {
183
                NanopubSetting setting;
184
                try {
185
                    setting = getSetting();
6✔
186
                } catch (MalformedNanopubException | IOException ex) {
3✔
187
                    logger.error("Error loading registry setting: {}", ex.getMessage());
15✔
188
                    throw new RuntimeException(ex);
15✔
189
                }
3✔
190
                for (IRI iri : setting.getBootstrapServices()) {
33✔
191
                    String peerUrl = iri.stringValue();
9✔
192
                    if (thisRegistryUrl.equals(peerUrl)) {
12!
193
                        continue;
×
194
                    }
195
                    peerUrls.add(peerUrl);
12✔
196
                }
3✔
197
            }
198
        }
199
        return peerUrls;
6✔
200
    }
201

202
    //private static final String SETTING_FILE_PATH = Utils.getEnv("REGISTRY_SETTING_FILE", "/data/setting.trig");
203
    private static volatile NanopubSetting settingNp;
204

205
    public static NanopubSetting getSetting() throws RDF4JException, MalformedNanopubException, IOException {
206
        if (settingNp == null) {
6✔
207
            synchronized (Utils.class) {
12✔
208
                if (settingNp == null) {
6!
209
                    String settingPath = getEnv("REGISTRY_SETTING_FILE", "/data/setting.trig");
12✔
210
                    settingNp = new NanopubSetting(new NanopubImpl(new File(settingPath)));
33✔
211
                }
212
            }
9✔
213
        }
214
        return settingNp;
6✔
215
    }
216

217
    public static String getRandomPeer() throws RDF4JException {
218
        List<String> peerUrls = getPeerUrls();
6✔
219
        return peerUrls.get(random.nextInt(peerUrls.size()));
24✔
220
    }
221

222
    private static final Random random = new Random();
12✔
223

224
    public static Random getRandom() {
225
        return random;
6✔
226
    }
227

228
    private static final Gson g = new Gson();
12✔
229
    private static Type listType = new TypeToken<List<String>>() {
21✔
230
    }.getType();
6✔
231

232
    public static List<String> retrieveListFromJsonUrl(String url) throws JsonIOException, JsonSyntaxException, IOException, URISyntaxException {
233
        return g.fromJson(new InputStreamReader(new URI(url).toURL().openStream()), listType);
×
234
    }
235

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