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

knowledgepixels / nanopub-query / 16989716008

15 Aug 2025 12:12PM UTC coverage: 25.926% (+0.1%) from 25.779%
16989716008

push

github

ashleycaselli
refactor: update code style and files formatting

122 of 494 branches covered (24.7%)

Branch coverage included in aggregate %.

333 of 1261 relevant lines covered (26.41%)

1.29 hits per line

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

81.75
src/main/java/com/knowledgepixels/query/OpenApiSpecPage.java
1
package com.knowledgepixels.query;
2

3
import io.vertx.core.MultiMap;
4
import net.trustyuri.TrustyUriUtils;
5
import org.eclipse.rdf4j.model.IRI;
6
import org.eclipse.rdf4j.model.Statement;
7
import org.eclipse.rdf4j.model.vocabulary.DCTERMS;
8
import org.eclipse.rdf4j.model.vocabulary.RDFS;
9
import org.eclipse.rdf4j.query.algebra.Var;
10
import org.eclipse.rdf4j.query.algebra.helpers.AbstractSimpleQueryModelVisitor;
11
import org.eclipse.rdf4j.query.parser.ParsedQuery;
12
import org.eclipse.rdf4j.query.parser.sparql.SPARQLParser;
13
import org.nanopub.Nanopub;
14
import org.nanopub.extra.server.GetNanopub;
15
import org.nanopub.extra.services.QueryAccess;
16
import org.yaml.snakeyaml.DumperOptions;
17
import org.yaml.snakeyaml.Yaml;
18

19
import java.util.*;
20

21
/**
22
 * Page for Open API compliant specification.
23
 */
24
public class OpenApiSpecPage {
25

26
    private Map<String, Object> dataMap = new LinkedHashMap<>();
5✔
27
    private Nanopub np;
28

29
    /**
30
     * Creates a new page instance.
31
     *
32
     * @param requestUrl The request URL
33
     * @param parameters The URL request parameters
34
     */
35
    public OpenApiSpecPage(String requestUrl, MultiMap parameters) {
2✔
36
        requestUrl = requestUrl.replaceFirst("\\?.*$", "");
5✔
37
        if (!requestUrl.matches(".*/RA[A-Za-z0-9\\-_]{43}/(.*)?")) return;
5✔
38
        String artifactCode = requestUrl.replaceFirst("^(.*/)(RA[A-Za-z0-9\\-_]{43})/(.*)?$", "$2");
5✔
39
//                String queryPart = requestUrl.replaceFirst("^(.*/)(RA[A-Za-z0-9\\-_]{43}/)(.*)?$", "$3");
40
//                String requestUrlBase = requestUrl.replaceFirst("^/(.*/)(RA[A-Za-z0-9\\-_]{43})/(.*)?$", "$1");
41

42
        // TODO Get the nanopub from the local store:
43
        np = GetNanopub.get(artifactCode);
4✔
44
        if (parameters.get("api-version") != null && parameters.get("api-version").equals("latest")) {
10!
45
            // TODO Get the latest version from the local store:
46
            np = GetNanopub.get(QueryAccess.getLatestVersionId(np.getUri().stringValue()));
8✔
47
            artifactCode = TrustyUriUtils.getArtifactCode(np.getUri().stringValue());
6✔
48
        }
49
        String queryName = null;
2✔
50
        String label = null;
2✔
51
        String desc = null;
2✔
52
        String queryContent = null;
2✔
53
        String endpoint = null;
2✔
54
        for (Statement st : np.getAssertion()) {
12✔
55
            if (!st.getSubject().stringValue().startsWith(np.getUri().stringValue())) continue;
9!
56
            String qn = st.getSubject().stringValue().replaceFirst("^.*[#/](.*)$", "$1");
7✔
57
            if (queryName != null && !qn.equals(queryName)) {
6!
58
                np = null;
×
59
                break;
×
60
            }
61
            queryName = qn;
2✔
62
            if (st.getPredicate().equals(RDFS.LABEL)) {
5✔
63
                label = st.getObject().stringValue();
5✔
64
            } else if (st.getPredicate().equals(DCTERMS.DESCRIPTION)) {
5✔
65
                desc = st.getObject().stringValue();
5✔
66
//                        } else if (st.getPredicate().equals(DCTERMS.LICENSE) && st.getObject() instanceof IRI) {
67
//                                license = st.getObject().stringValue();
68
            } else if (st.getPredicate().equals(GrlcSpecPage.HAS_SPARQL)) {
5✔
69
                queryContent = st.getObject().stringValue().replace("https://w3id.org/np/l/nanopub-query-1.1/", GrlcSpecPage.nanopubQueryUrl);
8✔
70
            } else if (st.getPredicate().equals(GrlcSpecPage.HAS_ENDPOINT) && st.getObject() instanceof IRI) {
9!
71
                endpoint = st.getObject().stringValue();
4✔
72
                if (endpoint.startsWith("https://w3id.org/np/l/nanopub-query-1.1/")) {
4!
73
                    endpoint = endpoint.replace("https://w3id.org/np/l/nanopub-query-1.1/", GrlcSpecPage.nanopubQueryUrl);
5✔
74
                }
75
            }
76
        }
1✔
77

78
        // TODO Code duplicated from com.knowledgepixels.nanodash.GrlcQuery:
79
        final Set<String> placeholders = new HashSet<>();
4✔
80
        ParsedQuery query = new SPARQLParser().parseQuery(queryContent, null);
7✔
81
        query.getTupleExpr().visitChildren(new AbstractSimpleQueryModelVisitor<>() {
14✔
82

83
            @Override
84
            public void meet(Var node) throws RuntimeException {
85
                super.meet(node);
3✔
86
                if (!node.isConstant() && !node.isAnonymous() && node.getName().startsWith("_")) {
11!
87
                    placeholders.add(node.getName());
×
88
                }
89
            }
1✔
90

91
        });
92
        List<String> placeholdersList = new ArrayList<>(placeholders);
5✔
93
        Collections.sort(placeholdersList);
2✔
94

95
        dataMap.put("openapi", "3.0.4");
6✔
96

97
        Map<String, Object> infoMap = new LinkedHashMap<>();
4✔
98
        infoMap.put("title", label);
5✔
99
        infoMap.put("description", "API definition source: <a target=\"_blank\" href=\"" + np.getUri() + "\"><svg height=\"0.8em\" xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 8 8\"><path d=\"M5,8H8L3,0H0M8,4.8V0H5M0,3.2V8H3\"/></svg> " + artifactCode.substring(0, 10) + "</a>");
13✔
100
        infoMap.put("version", artifactCode.substring(0, 10));
8✔
101
        dataMap.put("info", infoMap);
6✔
102

103
        List<Object> serversList = new ArrayList<>();
4✔
104
        Map<String, Object> serverMap = new LinkedHashMap<>();
4✔
105
        serverMap.put("url", Utils.getEnvString("NANOPUB_QUERY_URL", "http://localhost:9393/") + "api/" + artifactCode);
9✔
106
        serverMap.put("description", "This Nanopub Query instance");
5✔
107
        serversList.add(serverMap);
4✔
108
        dataMap.put("servers", serversList);
6✔
109

110
        Map<String, Object> pathsMap = new LinkedHashMap<>();
4✔
111
        Map<String, Object> rootPathMap = new LinkedHashMap<>();
4✔
112
        Map<String, Object> getOpMap = new LinkedHashMap<>();
4✔
113
        //getOpMap.put("summary", label);
114
        getOpMap.put("description", desc);
5✔
115
        Map<String, Object> responsesMap = new LinkedHashMap<>();
4✔
116
        Map<String, Object> successrespMap = new LinkedHashMap<>();
4✔
117
        Map<String, Object> contentMap = new LinkedHashMap<>();
4✔
118
        contentMap.put("text/csv", new HashMap<>());
7✔
119
        contentMap.put("application/json", new HashMap<>());
7✔
120
        successrespMap.put("content", contentMap);
5✔
121
        successrespMap.put("description", "result table");
5✔
122
        responsesMap.put("200", successrespMap);
5✔
123
        List<Object> parametersList = new ArrayList<>();
4✔
124
        for (String p : placeholdersList) {
6!
125
            Map<String, Object> paramMap = new LinkedHashMap<>();
×
126
            paramMap.put("in", "query");
×
127
            String name = p.replaceFirst("^_*", "").replaceFirst("_iri$", "");
×
128
            paramMap.put("name", name);
×
129
            paramMap.put("required", !p.startsWith("__"));
×
130
            Map<String, Object> stringType = new LinkedHashMap<>();
×
131
            stringType.put("type", "string");
×
132
            paramMap.put("schema", stringType);
×
133
            parametersList.add(paramMap);
×
134
        }
×
135
        getOpMap.put("parameters", parametersList);
5✔
136
        getOpMap.put("responses", responsesMap);
5✔
137
        rootPathMap.put("get", getOpMap);
5✔
138
        pathsMap.put("/" + queryName, rootPathMap);
6✔
139
        dataMap.put("paths", pathsMap);
6✔
140
    }
1✔
141

142
    /**
143
     * Returns the Open API spec as a string.
144
     *
145
     * @return Open API specification string
146
     */
147
    public String getSpec() {
148
        if (np == null) return null;
5✔
149
        final DumperOptions options = new DumperOptions();
4✔
150
        options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
3✔
151
        options.setPrettyFlow(true);
3✔
152
        return new Yaml(options).dump(dataMap);
8✔
153
    }
154

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