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

Nanopublication / nanopub-java / 21634068091

03 Feb 2026 02:24PM UTC coverage: 51.699% (+0.007%) from 51.692%
21634068091

push

github

ashleycaselli
fix(QueryRef): replace "name" with "queryId" to keep it consistent throughout the code and update related methods

1015 of 2944 branches covered (34.48%)

Branch coverage included in aggregate %.

5223 of 9122 relevant lines covered (57.26%)

8.05 hits per line

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

73.24
src/main/java/org/nanopub/extra/services/QueryRef.java
1
package org.nanopub.extra.services;
2

3
import com.google.common.collect.ArrayListMultimap;
4
import com.google.common.collect.Multimap;
5
import org.apache.commons.codec.Charsets;
6
import org.apache.http.NameValuePair;
7
import org.apache.http.client.utils.URLEncodedUtils;
8

9
import java.io.Serializable;
10
import java.net.URLEncoder;
11
import java.util.ArrayList;
12
import java.util.Comparator;
13
import java.util.List;
14
import java.util.Map.Entry;
15

16
/**
17
 * A reference to a query with optional parameters.
18
 * This class is used to encapsulate the id of the query and any parameters
19
 * that need to be passed to it.
20
 */
21
public class QueryRef implements Serializable {
22

23
    private final String queryId;
24
    private final Multimap<String, String> params;
25
    private String urlString;
26

27
    /**
28
     * Constructor for QueryRef.
29
     *
30
     * @param queryId the id of the query (of the form "artifactCode/querySuffix")
31
     * @param params  a map of parameters for the query
32
     */
33
    public QueryRef(String queryId, Multimap<String, String> params) {
6✔
34
        validateQueryId(queryId);
9✔
35
        this.queryId = queryId;
9✔
36
        this.params = params;
9✔
37
    }
3✔
38

39
    private void validateQueryId(String queryId) {
40
        if (queryId == null || queryId.isBlank()) {
15✔
41
            throw new IllegalArgumentException("Query id cannot be null or empty");
15✔
42
        }
43
        if (!queryId.matches("RA[A-Za-z0-9-_]{43}[/#][^/#]+")) {
12✔
44
            throw new IllegalArgumentException("Query id is invalid: " + queryId);
18✔
45
        }
46
    }
3✔
47

48
    /**
49
     * Constructor for QueryRef with no parameters.
50
     *
51
     * @param queryId the id of the query (of the form "artifactCode/querySuffix")
52
     */
53
    public QueryRef(String queryId) {
54
        this(queryId, ArrayListMultimap.create());
12✔
55
    }
3✔
56

57
    /**
58
     * Constructor for QueryRef with a single parameter.
59
     *
60
     * @param queryId    the id of the query (of the form "artifactCode/querySuffix")
61
     * @param paramKey   the key of the parameter
62
     * @param paramValue the value of the parameter
63
     */
64
    public QueryRef(String queryId, String paramKey, String paramValue) {
65
        this(queryId);
9✔
66
        if (paramKey == null || paramKey.isBlank()) {
15✔
67
            throw new IllegalArgumentException("Parameter key cannot be null or empty");
15✔
68
        }
69
        params.put(paramKey, paramValue);
18✔
70
    }
3✔
71

72
    /**
73
     * Get the id of the query.
74
     *
75
     * @return the id of the query
76
     */
77
    public String getQueryId() {
78
        return queryId;
9✔
79
    }
80

81
    /**
82
     * Get the parameters of the query.
83
     *
84
     * @return a map of parameters
85
     */
86
    public Multimap<String, String> getParams() {
87
        return params;
9✔
88
    }
89

90
    /**
91
     * Get the query reference as a URL string.
92
     *
93
     * @return the query reference as a URL string
94
     */
95
    public String getAsUrlString() {
96
        if (urlString == null) {
9✔
97
            String paramString = "";
6✔
98
            if (params != null) {
9!
99
                paramString = "?";
6✔
100
                List<Entry<String, String>> entryList = new ArrayList<>(params.entries());
21✔
101
                entryList.sort(Comparator.comparing(Entry::getValue));
12✔
102
                entryList.sort(Comparator.comparing(Entry::getKey));
12✔
103
                for (Entry<String, String> e : entryList) {
30✔
104
                    if (paramString.length() > 1) paramString += "&";
12!
105
                    paramString += (e.getKey() == null ? "$null" : e.getKey()) + "=";
27!
106
                    paramString += URLEncoder.encode(e.getValue() == null ? "" : e.getValue(), Charsets.UTF_8);
30!
107
                }
3✔
108
            }
109
            urlString = queryId + paramString;
18✔
110
        }
111
        return urlString;
9✔
112
    }
113

114
    @Override
115
    public String toString() {
116
        return getAsUrlString();
9✔
117
    }
118

119
    /**
120
     * Parse a QueryRef from a string.
121
     *
122
     * @param queryRefUrlString the string to parse
123
     * @return the parsed QueryRef
124
     */
125
    public static QueryRef parseString(String queryRefUrlString) {
126
        // TODO add check that the string is a valid one before parsing
127
        if (queryRefUrlString.contains("?")) {
×
128
            String queryName = queryRefUrlString.split("\\?")[0];
×
129
            Multimap<String, String> queryParams = ArrayListMultimap.create();
×
130
            if (!queryRefUrlString.endsWith("?")) {
×
131
                for (NameValuePair nvp : URLEncodedUtils.parse(queryRefUrlString.split("\\?")[1], Charsets.UTF_8)) {
×
132
                    queryParams.put(nvp.getName(), nvp.getValue());
×
133
                }
×
134
            }
135
            return new QueryRef(queryName, queryParams);
×
136
        } else {
137
            return new QueryRef(queryRefUrlString);
×
138
        }
139
    }
140

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