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

Nanopublication / nanopub-java / 19896267626

03 Dec 2025 01:54PM UTC coverage: 51.73% (-0.07%) from 51.795%
19896267626

push

github

ashleycaselli
feat(QueryRef): add parseString method to create an object from a valid string

1013 of 2934 branches covered (34.53%)

Branch coverage included in aggregate %.

5220 of 9115 relevant lines covered (57.27%)

2.68 hits per line

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

70.77
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 name 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 name;
24
    private final Multimap<String, String> params;
25
    private String urlString;
26

27
    /**
28
     * Constructor for QueryRef.
29
     *
30
     * @param name   the name of the query
31
     * @param params a map of parameters for the query
32
     */
33
    public QueryRef(String name, Multimap<String, String> params) {
2✔
34
        if (name == null || name.isBlank()) {
5✔
35
            throw new IllegalArgumentException("Query name cannot be null or empty");
5✔
36
        }
37
        this.name = name;
3✔
38
        this.params = params;
3✔
39
    }
1✔
40

41
    /**
42
     * Constructor for QueryRef with no parameters.
43
     *
44
     * @param name the name of the query
45
     */
46
    public QueryRef(String name) {
47
        this(name, ArrayListMultimap.create());
4✔
48
    }
1✔
49

50
    /**
51
     * Constructor for QueryRef with a single parameter.
52
     *
53
     * @param name       the name of the query
54
     * @param paramKey   the key of the parameter
55
     * @param paramValue the value of the parameter
56
     */
57
    public QueryRef(String name, String paramKey, String paramValue) {
58
        this(name);
3✔
59
        if (paramKey == null || paramKey.isBlank()) {
5✔
60
            throw new IllegalArgumentException("Parameter key cannot be null or empty");
5✔
61
        }
62
        params.put(paramKey, paramValue);
6✔
63
    }
1✔
64

65
    /**
66
     * Get the name of the query.
67
     *
68
     * @return the name of the query
69
     */
70
    public String getName() {
71
        return name;
3✔
72
    }
73

74
    /**
75
     * Get the parameters of the query.
76
     *
77
     * @return a map of parameters
78
     */
79
    public Multimap<String, String> getParams() {
80
        return params;
3✔
81
    }
82

83
    /**
84
     * Get the query reference as a URL string.
85
     *
86
     * @return the query reference as a URL string
87
     */
88
    public String getAsUrlString() {
89
        if (urlString == null) {
3✔
90
            String paramString = "";
2✔
91
            if (params != null) {
3!
92
                paramString = "?";
2✔
93
                List<Entry<String, String>> entryList = new ArrayList<>(params.entries());
7✔
94
                entryList.sort(Comparator.comparing(Entry::getValue));
4✔
95
                entryList.sort(Comparator.comparing(Entry::getKey));
4✔
96
                for (Entry<String, String> e : entryList) {
10✔
97
                    if (paramString.length() > 1) paramString += "&";
4!
98
                    paramString += (e.getKey() == null ? "$null" : e.getKey()) + "=";
9!
99
                    paramString += URLEncoder.encode(e.getValue() == null ? "" : e.getValue(), Charsets.UTF_8);
10!
100
                }
1✔
101
            }
102
            urlString = name + paramString;
6✔
103
        }
104
        return urlString;
3✔
105
    }
106

107
    @Override
108
    public String toString() {
109
        return getAsUrlString();
3✔
110
    }
111

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

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

© 2025 Coveralls, Inc