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

box / box-java-sdk / #4583

29 Apr 2025 07:23AM UTC coverage: 72.058% (-0.002%) from 72.06%
#4583

push

github

web-flow
fix: use `Locale.ROOT` to prevent issues with non-US locales (#1306)

14 of 23 new or added lines in 16 files covered. (60.87%)

1 existing line in 1 file now uncovered.

8216 of 11402 relevant lines covered (72.06%)

0.72 hits per line

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

74.59
/src/main/java/com/box/sdk/BoxGroupMembership.java
1
package com.box.sdk;
2

3
import com.eclipsesource.json.Json;
4
import com.eclipsesource.json.JsonObject;
5
import com.eclipsesource.json.JsonValue;
6
import java.net.URL;
7
import java.util.Date;
8
import java.util.HashMap;
9
import java.util.Map;
10

11
/**
12
 * Represents a relationship between a user and a group.
13
 *
14
 * <p>Unless otherwise noted, the methods in this class can throw an unchecked {@link BoxAPIException} (unchecked
15
 * meaning that the compiler won't force you to handle it) if an error occurs. If you wish to implement custom error
16
 * handling for errors related to the Box REST API, you should capture this exception explicitly.</p>
17
 */
18
@BoxResourceType("group_membership")
19
public class BoxGroupMembership extends BoxResource {
20

21
    /**
22
     * The URL template for all group membership requests.
23
     *
24
     * @see #getInfo()
25
     */
26
    public static final URLTemplate MEMBERSHIP_URL_TEMPLATE = new URLTemplate("group_memberships/%s");
1✔
27

28
    /**
29
     * Constructs a BoxGroupMembership for a group membership with a given ID.
30
     *
31
     * @param api the API connection to be used by the group membership.
32
     * @param id  the ID of the group membership.
33
     */
34
    public BoxGroupMembership(BoxAPIConnection api, String id) {
35
        super(api, id);
1✔
36
    }
1✔
37

38
    /**
39
     * Gets information about this group membership.
40
     *
41
     * @return info about this group membership.
42
     */
43
    public Info getInfo() {
44
        BoxAPIConnection api = this.getAPI();
×
45
        URL url = MEMBERSHIP_URL_TEMPLATE.build(api.getBaseURL(), this.getID());
×
46

47
        BoxJSONRequest request = new BoxJSONRequest(api, url, "GET");
×
48
        try (BoxJSONResponse response = request.send()) {
×
49
            JsonObject jsonObject = Json.parse(response.getJSON()).asObject();
×
50
            return new Info(jsonObject);
×
51
        }
52
    }
53

54
    /**
55
     * Updates the information about this group membership with any info fields that have been modified locally.
56
     *
57
     * @param info the updated info.
58
     */
59
    public void updateInfo(Info info) {
60
        BoxAPIConnection api = this.getAPI();
1✔
61
        URL url = MEMBERSHIP_URL_TEMPLATE.build(api.getBaseURL(), this.getID());
1✔
62

63
        BoxJSONRequest request = new BoxJSONRequest(api, url, "PUT");
1✔
64
        request.setBody(info.getPendingChanges());
1✔
65
        try (BoxJSONResponse response = request.send()) {
1✔
66
            JsonObject jsonObject = Json.parse(response.getJSON()).asObject();
1✔
67
            info.update(jsonObject);
1✔
68
        }
69
    }
1✔
70

71
    /**
72
     * Deletes this group membership.
73
     */
74
    public void delete() {
75
        BoxAPIConnection api = this.getAPI();
1✔
76
        URL url = MEMBERSHIP_URL_TEMPLATE.build(api.getBaseURL(), this.getID());
1✔
77

78
        BoxAPIRequest request = new BoxAPIRequest(api, url, "DELETE");
1✔
79
        request.send().close();
1✔
80
    }
1✔
81

82
    /**
83
     * Enumerates the possible roles that a user can have within a group.
84
     */
85
    public enum GroupRole {
1✔
86
        /**
87
         * The user is an administrator in the group.
88
         */
89
        ADMIN("admin"),
1✔
90

91
        /**
92
         * The user is a regular member in the group.
93
         */
94
        MEMBER("member");
1✔
95

96
        /**
97
         * String representation of the groupRole.
98
         */
99
        private final String jsonValue;
100

101
        /**
102
         * Constructor.
103
         *
104
         * @param jsonValue string representation of the role.
105
         */
106
        GroupRole(String jsonValue) {
1✔
107
            this.jsonValue = jsonValue;
1✔
108
        }
1✔
109

110
        /**
111
         * Creates the groupRole from given string.
112
         *
113
         * @param jsonValue string to be converted to role.
114
         * @return the role, created from string value.
115
         */
116
        static GroupRole fromJSONString(String jsonValue) {
117
            for (GroupRole role : GroupRole.values()) {
1✔
118
                if (role.jsonValue.equalsIgnoreCase(jsonValue)) {
1✔
119
                    return role;
1✔
120
                }
121
            }
122
            throw new IllegalArgumentException("Invalid value for enum GroupRole: " + jsonValue);
×
123
        }
124

125
        /**
126
         * @return string representation of the groupRole.
127
         */
128
        String toJSONString() {
129
            return this.jsonValue;
1✔
130
        }
131
    }
132

133
    /**
134
     * Enumerates the possible permissions that a user can have as a group admin.
135
     */
136
    public enum Permission {
1✔
137
        /**
138
         * The user can create accounts.
139
         */
140
        CAN_CREATE_ACCOUNTS("can_create_accounts"),
1✔
141

142
        /**
143
         * The user can edit accounts.
144
         */
145
        CAN_EDIT_ACCOUNTS("can_edit_accounts"),
1✔
146

147
        /**
148
         * The user can instant login as another user.
149
         */
150
        CAN_INSTANT_LOGIN("can_instant_login"),
1✔
151

152
        /**
153
         * The user can run reports.
154
         */
155
        CAN_RUN_REPORTS("can_run_reports");
1✔
156

157
        private final String jsonValue;
158

159
        Permission(String jsonValue) {
1✔
160
            this.jsonValue = jsonValue;
1✔
161
        }
1✔
162

163
        static Permission fromJSONValue(String jsonValue) {
NEW
164
            return Permission.valueOf(jsonValue.toUpperCase(java.util.Locale.ROOT));
×
165
        }
166

167
        String toJSONValue() {
168
            return this.jsonValue;
×
169
        }
170
    }
171

172
    /**
173
     * Contains information about a BoxGroupMembership.
174
     */
175
    public class Info extends BoxResource.Info {
176

177
        /**
178
         * @see #getUser()
179
         */
180
        private BoxUser.Info user;
181

182
        /**
183
         * @see #getGroup()
184
         */
185
        private BoxGroup.Info group;
186

187
        /**
188
         * @see #getGroupRole()
189
         */
190
        private GroupRole groupRole;
191

192
        /**
193
         * @see #getCreatedAt()
194
         */
195
        private Date createdAt;
196

197
        /**
198
         * @see #getModifiedAt()
199
         */
200
        private Date modifiedAt;
201

202
        /**
203
         * @see #getConfigurablePermissions()
204
         */
205
        private Map<Permission, Boolean> configurablePermissions;
206

207
        /**
208
         * Constructs an empty Info object.
209
         */
210
        public Info() {
1✔
211
            super();
1✔
212
        }
1✔
213

214
        /**
215
         * Constructs an Info object by parsing information from a JSON string.
216
         *
217
         * @param json the JSON string to parse.
218
         */
219
        public Info(String json) {
×
220
            super(json);
×
221
        }
×
222

223
        /**
224
         * Constructs an Info object using an already parsed JSON object.
225
         *
226
         * @param jsonObject the parsed JSON object.
227
         */
228
        Info(JsonObject jsonObject) {
1✔
229
            super(jsonObject);
1✔
230
        }
1✔
231

232
        /**
233
         * Gets the user belonging to the group.
234
         *
235
         * <p>Note: the BoxUser.Info returned by this method will only have the ID, name, and login fields
236
         * populated.</p>
237
         *
238
         * @return the user belonging to the group.
239
         */
240
        public BoxUser.Info getUser() {
241
            return this.user;
1✔
242
        }
243

244
        /**
245
         * Gets the group the user belongs to.
246
         *
247
         * <p>Note: the BoxGroup.Info returned by this method will only have the ID and name fields populated.</p>
248
         *
249
         * @return the group the user belongs to.
250
         */
251
        public BoxGroup.Info getGroup() {
252
            return this.group;
1✔
253
        }
254

255
        /**
256
         * Gets the level of access the user has.
257
         *
258
         * @return the level of access the user has.
259
         */
260
        public GroupRole getGroupRole() {
261
            return this.groupRole;
1✔
262
        }
263

264
        /**
265
         * Sets the level of access the user has.
266
         *
267
         * @param role the new level of access to give the user.
268
         */
269
        public void setGroupRole(GroupRole role) {
270
            this.groupRole = role;
1✔
271
            this.addPendingChange("role", role.toJSONString());
1✔
272
        }
1✔
273

274
        /**
275
         * Gets the time the group membership was created.
276
         *
277
         * @return the time the group membership was created.
278
         */
279
        public Date getCreatedAt() {
280
            return this.createdAt;
1✔
281
        }
282

283
        /**
284
         * Gets the time the group membership was last modified.
285
         *
286
         * @return the time the group membership was last modified.
287
         */
288
        public Date getModifiedAt() {
289
            return this.modifiedAt;
1✔
290
        }
291

292
        /**
293
         * Gets the configurablePermissions that the current user has on the group as group admin.
294
         *
295
         * @return the configurablePermissions that the current user has on the group as group admin.
296
         */
297
        public Map<Permission, Boolean> getConfigurablePermissions() {
298
            return this.configurablePermissions;
×
299
        }
300

301
        /**
302
         * Sets the configurablePermissions that the current user has on the group as group admin.
303
         *
304
         * @param configurablePermissions a Map representing the group admin configurable permissions
305
         */
306
        public void setConfigurablePermissions(Map<Permission, Boolean> configurablePermissions) {
307
            this.configurablePermissions = configurablePermissions;
×
308
            this.addPendingChange("configurable_permissions", this.configurablePermissionJson());
×
309
        }
×
310

311
        /**
312
         * append new configurable permissions to the previous existing list.
313
         *
314
         * @param permission the group admin permission one wants to enable or disable of the user on the group.
315
         * @param value      the true/false value of the attribute to set.
316
         */
317
        public void appendConfigurablePermissions(Permission permission, Boolean value) {
318
            this.configurablePermissions.put(permission, value);
×
319
            this.addPendingChange("configurable_permissions", this.configurablePermissionJson());
×
320
        }
×
321

322
        private JsonObject configurablePermissionJson() {
323
            JsonObject configurablePermissionJson = new JsonObject();
×
324
            for (Permission attrKey : this.configurablePermissions.keySet()) {
×
325
                configurablePermissionJson.set(attrKey.toJSONValue(), this.configurablePermissions.get(attrKey));
×
326
            }
×
327
            return configurablePermissionJson;
×
328
        }
329

330
        /**
331
         * {@inheritDoc}
332
         */
333
        @Override
334
        public BoxGroupMembership getResource() {
335
            return BoxGroupMembership.this;
1✔
336
        }
337

338
        private Map<Permission, Boolean> parseConfigurablePermissions(JsonObject jsonObject) {
339
            if (jsonObject == null) {
1✔
340
                return null;
×
341
            }
342
            Map<Permission, Boolean> permissions = new HashMap<>();
1✔
343
            for (JsonObject.Member member : jsonObject) {
1✔
344
                String memberName = member.getName();
1✔
345
                boolean memberValue = member.getValue().asBoolean();
1✔
346
                switch (memberName) {
1✔
347
                    case "can_create_accounts":
348
                        permissions.put(Permission.CAN_CREATE_ACCOUNTS, memberValue);
1✔
349
                        break;
1✔
350
                    case "can_edit_accounts":
351
                        permissions.put(Permission.CAN_EDIT_ACCOUNTS, memberValue);
1✔
352
                        break;
1✔
353
                    case "can_instant_login":
354
                        permissions.put(Permission.CAN_INSTANT_LOGIN, memberValue);
1✔
355
                        break;
1✔
356
                    case "can_run_reports":
357
                        permissions.put(Permission.CAN_RUN_REPORTS, memberValue);
1✔
358
                        break;
1✔
359
                    default:
360
                        break;
361
                }
362
            }
1✔
363
            return permissions;
1✔
364
        }
365

366
        /**
367
         * {@inheritDoc}
368
         */
369
        @Override
370
        protected void parseJSONMember(JsonObject.Member member) {
371
            super.parseJSONMember(member);
1✔
372

373
            String memberName = member.getName();
1✔
374
            JsonValue value = member.getValue();
1✔
375

376
            try {
377
                switch (memberName) {
1✔
378
                    case "user":
379
                        JsonObject userJSON = value.asObject();
1✔
380
                        if (this.user == null) {
1✔
381
                            String userID = userJSON.get("id").asString();
1✔
382
                            BoxUser user = new BoxUser(getAPI(), userID);
1✔
383
                            this.user = user.new Info(userJSON);
1✔
384
                        } else {
1✔
385
                            this.user.update(userJSON);
×
386
                        }
387

388
                        break;
×
389
                    case "group":
390
                        JsonObject groupJSON = value.asObject();
1✔
391
                        if (this.group == null) {
1✔
392
                            String userID = groupJSON.get("id").asString();
1✔
393
                            BoxGroup group = new BoxGroup(getAPI(), userID);
1✔
394
                            this.group = group.new Info(groupJSON);
1✔
395
                        } else {
1✔
396
                            this.group.update(groupJSON);
×
397
                        }
398

399
                        break;
×
400
                    case "role":
401
                        this.groupRole = GroupRole.fromJSONString(value.asString());
1✔
402

403
                        break;
1✔
404
                    case "created_at":
405
                        this.createdAt = BoxDateFormat.parse(value.asString());
1✔
406

407
                        break;
1✔
408
                    case "modified_at":
409
                        this.modifiedAt = BoxDateFormat.parse(value.asString());
1✔
410

411
                        break;
1✔
412
                    case "configurable_permissions":
413
                        this.configurablePermissions = this.parseConfigurablePermissions(value.asObject());
1✔
414

415
                        break;
1✔
416
                    default:
417
                        break;
418
                }
419
            } catch (Exception e) {
×
420
                throw new BoxDeserializationException(memberName, value.toString(), e);
×
421
            }
1✔
422
        }
1✔
423
    }
424
}
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