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

box / box-java-sdk / #3905

11 Jul 2024 03:34PM CUT coverage: 72.44% (+0.002%) from 72.438%
#3905

Pull #1258

github

web-flow
Merge 5d85071f4 into f08844889
Pull Request #1258: test: support stream file upload

7683 of 10606 relevant lines covered (72.44%)

0.72 hits per line

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

92.59
/src/main/java/com/box/sdk/MetadataQuery.java
1
package com.box.sdk;
2

3
import static java.util.stream.Collectors.toList;
4

5
import com.eclipsesource.json.Json;
6
import com.eclipsesource.json.JsonArray;
7
import com.eclipsesource.json.JsonObject;
8
import com.eclipsesource.json.JsonValue;
9
import java.util.ArrayList;
10
import java.util.Arrays;
11
import java.util.List;
12

13
/**
14
 * Represents Metadata Query.
15
 */
16
public class MetadataQuery {
17
    static final String FROM = "from";
18
    static final String LIMIT = "limit";
19
    static final String QUERY = "query";
20
    static final String ANCESTOR_FOLDER_ID = "ancestor_folder_id";
21
    static final String MARKER = "marker";
22
    static final String ORDER_BY = "order_by";
23
    static final String FIELDS = "fields";
24
    static final String QUERY_PARAMS = "query_params";
25
    private final String from;
26
    private final int limit;
27
    private String query;
28
    private JsonObject queryParameters = new JsonObject();
1✔
29
    private String ancestorFolderId = "0";
1✔
30
    private List<OrderBy> orderBy = new ArrayList<>();
1✔
31
    private String marker;
32
    private List<String> fields = new ArrayList<>();
1✔
33

34
    /**
35
     * Creates Metadata Query
36
     *
37
     * @param from  The template used in the query. Must be in the form scope_enterpriseID.templateKey
38
     * @param limit Max results to return for a single request (0-100 inclusive)
39
     */
40
    public MetadataQuery(String from, int limit) {
1✔
41
        this.from = from;
1✔
42
        this.limit = limit;
1✔
43
    }
1✔
44

45
    /**
46
     * Creates Metadata Query
47
     *
48
     * @param from The template used in the query. Must be in the form scope.templateKey
49
     */
50
    public MetadataQuery(String from) {
51
        this(from, 100);
×
52
    }
×
53

54
    /**
55
     * The logical expression of the query
56
     *
57
     * @param query Query string
58
     * @return Returns current MetadataQuery object
59
     */
60
    public MetadataQuery setQuery(String query) {
61
        this.query = query;
1✔
62
        return this;
1✔
63
    }
64

65
    /**
66
     * Sets the folder_id to which to restrain the query.
67
     * If not set query starts at root level.
68
     *
69
     * @param ancestorFolderId The folder id
70
     * @return Returns current MetadataQuery object
71
     */
72
    public MetadataQuery setAncestorFolderId(String ancestorFolderId) {
73
        this.ancestorFolderId = ancestorFolderId;
1✔
74
        return this;
1✔
75
    }
76

77
    /**
78
     * The marker to use for requesting the next page
79
     *
80
     * @param marker Marker string.
81
     * @return Returns current MetadataQuery object
82
     */
83
    public MetadataQuery setMarker(String marker) {
84
        this.marker = marker;
1✔
85
        return this;
1✔
86
    }
87

88
    /**
89
     * The field_key(s) to order on and the corresponding direction(s)
90
     *
91
     * @param fields Fields with sort order
92
     * @return Returns current MetadataQuery object
93
     */
94
    public MetadataQuery setOrderBy(OrderBy... fields) {
95
        this.orderBy = new ArrayList<>();
1✔
96
        this.orderBy.addAll(Arrays.asList(fields));
1✔
97
        return this;
1✔
98
    }
99

100
    MetadataQuery setOrderBy(JsonArray orderBy) {
101
        if (orderBy != null) {
1✔
102
            this.orderBy = orderBy.values().stream().map(OrderBy::fromJson).collect(toList());
1✔
103
        }
104
        return this;
1✔
105
    }
106

107
    /**
108
     * The fields to retrieve.
109
     *
110
     * @param fields Field names
111
     * @return Returns current MetadataQuery object
112
     */
113
    public MetadataQuery setFields(String... fields) {
114
        this.fields = new ArrayList<>();
1✔
115
        this.fields.addAll(Arrays.asList(fields));
1✔
116
        return this;
1✔
117
    }
118

119
    /**
120
     * Adds parameter to query
121
     *
122
     * @param name  Parameter name
123
     * @param value Parameter value
124
     * @return Returns current MetadataQuery object
125
     */
126
    public MetadataQuery addParameter(String name, String value) {
127
        this.queryParameters.add(name, value);
1✔
128
        return this;
1✔
129
    }
130

131
    /**
132
     * Adds parameter to query
133
     *
134
     * @param name  Parameter name
135
     * @param value Parameter value
136
     * @return Returns current MetadataQuery object
137
     */
138
    public MetadataQuery addParameter(String name, int value) {
139
        this.queryParameters.add(name, value);
1✔
140
        return this;
1✔
141
    }
142

143
    /**
144
     * Adds parameter to query
145
     *
146
     * @param name  Parameter name
147
     * @param value Parameter value
148
     * @return Returns current MetadataQuery object
149
     */
150
    public MetadataQuery addParameter(String name, boolean value) {
151
        this.queryParameters.add(name, value);
1✔
152
        return this;
1✔
153
    }
154

155
    /**
156
     * Adds parameter to query
157
     *
158
     * @param name  Parameter name
159
     * @param value Parameter value
160
     * @return Returns current MetadataQuery object
161
     */
162
    public MetadataQuery addParameter(String name, float value) {
163
        this.queryParameters.add(name, value);
1✔
164
        return this;
1✔
165
    }
166

167
    /**
168
     * Adds parameter to query
169
     *
170
     * @param name  Parameter name
171
     * @param value Parameter value
172
     * @return Returns current MetadataQuery object
173
     */
174
    public MetadataQuery addParameter(String name, long value) {
175
        this.queryParameters.add(name, value);
1✔
176
        return this;
1✔
177
    }
178

179
    /**
180
     * Adds parameter to query
181
     *
182
     * @param name  Parameter name
183
     * @param value Parameter value
184
     * @return Returns current MetadataQuery object
185
     */
186
    public MetadataQuery addParameter(String name, double value) {
187
        this.queryParameters.add(name, value);
1✔
188
        return this;
1✔
189
    }
190

191
    /**
192
     * Adds parameter to query
193
     *
194
     * @param name  Parameter name
195
     * @param value Parameter value
196
     * @return Returns current MetadataQuery object
197
     */
198
    public MetadataQuery addParameter(String name, JsonValue value) {
199
        this.queryParameters.add(name, Json.parse(value.toString()));
1✔
200
        return this;
1✔
201
    }
202

203
    MetadataQuery setQueryParams(JsonObject queryParameters) {
204
        this.queryParameters = new JsonObject(queryParameters);
1✔
205
        return this;
1✔
206
    }
207

208
    JsonObject toJsonObject() {
209
        JsonObject jsonObject = new JsonObject()
1✔
210
            .add(FROM, from)
1✔
211
            .add(LIMIT, limit);
1✔
212
        if (query != null) {
1✔
213
            jsonObject.add(QUERY, query);
1✔
214
        }
215
        if (ancestorFolderId != null) {
1✔
216
            jsonObject.add(ANCESTOR_FOLDER_ID, ancestorFolderId);
1✔
217
        }
218
        if (marker != null) {
1✔
219
            jsonObject.add(MARKER, marker);
1✔
220
        }
221
        if (!orderBy.isEmpty()) {
1✔
222
            JsonArray orderByJson = new JsonArray();
1✔
223
            orderBy.stream().map(OrderBy::toJsonObject).forEach(orderByJson::add);
1✔
224
            jsonObject.add(ORDER_BY, orderByJson);
1✔
225
        }
226
        if (!fields.isEmpty()) {
1✔
227
            JsonArray fieldsJson = new JsonArray();
1✔
228
            fields.forEach(fieldsJson::add);
1✔
229
            jsonObject.add(FIELDS, fieldsJson);
1✔
230
        }
231
        if (queryParameters.iterator().hasNext()) {
1✔
232
            jsonObject.add(QUERY_PARAMS, new JsonObject(queryParameters));
1✔
233
        }
234
        return jsonObject;
1✔
235
    }
236

237
    int getLimit() {
238
        return limit;
1✔
239
    }
240

241
    String getMarker() {
242
        return marker;
1✔
243
    }
244

245
    public static final class OrderBy {
246

247
        static final String FIELD_KEY = "field_key";
248
        static final String DIRECTION = "direction";
249
        static final String DIRECTION_ASCENDING = "asc";
250
        static final String DIRECTION_DESCENDING = "desc";
251
        private final String fieldName;
252
        private final String direction;
253

254
        private OrderBy(String fieldName, String direction) {
1✔
255
            this.fieldName = fieldName;
1✔
256
            this.direction = direction;
1✔
257
        }
1✔
258

259
        JsonObject toJsonObject() {
260
            return new JsonObject().add(FIELD_KEY, fieldName).add(DIRECTION, direction);
1✔
261
        }
262

263
        /**
264
         * Creates OrderBy for ascending sort with a specified field.
265
         * @param fieldName Name of a field
266
         * @return OrderBy instance
267
         */
268
        public static OrderBy ascending(String fieldName) {
269
            return new OrderBy(fieldName, DIRECTION_ASCENDING);
1✔
270
        }
271

272
        /**
273
         * Creates OrderBy for descending sort with a specified field.
274
         * @param fieldName Name of a field
275
         * @return OrderBy instance
276
         */
277
        public static OrderBy descending(String fieldName) {
278
            return new OrderBy(fieldName, DIRECTION_DESCENDING);
1✔
279
        }
280

281
        static OrderBy fromJson(JsonValue jsonValue) {
282
            if (jsonValue.isObject()) {
1✔
283
                JsonObject object = jsonValue.asObject();
1✔
284
                String fieldName = object.get(FIELD_KEY).asString();
1✔
285
                String direction = object.get(DIRECTION).asString().toLowerCase();
1✔
286
                if (!DIRECTION_ASCENDING.equals(direction) && !DIRECTION_DESCENDING.equals(direction)) {
1✔
287
                    throw new RuntimeException(
×
288
                        String.format("Unsupported sort direction [%s] for field [%s]", direction, fieldName)
×
289
                    );
290
                }
291
                return object.getString(DIRECTION, "").equals(DIRECTION_ASCENDING)
1✔
292
                    ? ascending(fieldName)
×
293
                    : descending(fieldName);
1✔
294
            }
295
            throw new RuntimeException("Unsupported json " + jsonValue);
×
296
        }
297
    }
298
}
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