Skip to content

Package: Groups

Groups

nameinstructionbranchcomplexitylinemethod
Groups()
M: 20 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
count(ScopeId, GroupQuery)
M: 11 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
create(ScopeId, GroupCreator)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
deleteGroup(ScopeId, EntityId)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
find(ScopeId, EntityId)
M: 16 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
query(ScopeId, GroupQuery)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
simpleQuery(ScopeId, String, int, int)
M: 35 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
update(ScopeId, EntityId, Group)
M: 11 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2016, 2022 Eurotech and/or its affiliates and others
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Eurotech - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.app.api.resources.v1.resources;
14:
15: import com.google.common.base.Strings;
16: import org.eclipse.kapua.KapuaEntityNotFoundException;
17: import org.eclipse.kapua.KapuaException;
18: import org.eclipse.kapua.app.api.core.resources.AbstractKapuaResource;
19: import org.eclipse.kapua.app.api.core.model.CountResult;
20: import org.eclipse.kapua.app.api.core.model.EntityId;
21: import org.eclipse.kapua.app.api.core.model.ScopeId;
22: import org.eclipse.kapua.locator.KapuaLocator;
23: import org.eclipse.kapua.model.KapuaNamedEntityAttributes;
24: import org.eclipse.kapua.model.query.predicate.AndPredicate;
25: import org.eclipse.kapua.service.KapuaService;
26: import org.eclipse.kapua.service.authorization.group.Group;
27: import org.eclipse.kapua.service.authorization.group.GroupCreator;
28: import org.eclipse.kapua.service.authorization.group.GroupFactory;
29: import org.eclipse.kapua.service.authorization.group.GroupListResult;
30: import org.eclipse.kapua.service.authorization.group.GroupQuery;
31: import org.eclipse.kapua.service.authorization.group.GroupService;
32:
33: import javax.ws.rs.Consumes;
34: import javax.ws.rs.DELETE;
35: import javax.ws.rs.DefaultValue;
36: import javax.ws.rs.GET;
37: import javax.ws.rs.POST;
38: import javax.ws.rs.PUT;
39: import javax.ws.rs.Path;
40: import javax.ws.rs.PathParam;
41: import javax.ws.rs.Produces;
42: import javax.ws.rs.QueryParam;
43: import javax.ws.rs.core.MediaType;
44: import javax.ws.rs.core.Response;
45:
46: @Path("{scopeId}/groups")
47: public class Groups extends AbstractKapuaResource {
48:
49: private final KapuaLocator locator = KapuaLocator.getInstance();
50: private final GroupService groupService = locator.getService(GroupService.class);
51: private final GroupFactory groupFactory = locator.getFactory(GroupFactory.class);
52:
53: /**
54: * Gets the {@link Group} list in the scope.
55: *
56: * @param scopeId The {@link ScopeId} in which to search results.
57: * @param name The {@link Group} name to filter results
58: * @param offset The result set offset.
59: * @param limit The result set limit.
60: * @return The {@link GroupListResult} of all the groups associated to the current selected scope.
61: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
62: * @since 1.0.0
63: */
64: @GET
65: @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
66: public GroupListResult simpleQuery(
67: @PathParam("scopeId") ScopeId scopeId,
68: @QueryParam("name") String name,
69: @QueryParam("offset") @DefaultValue("0") int offset,
70: @QueryParam("limit") @DefaultValue("50") int limit) throws KapuaException {
71: GroupQuery query = groupFactory.newQuery(scopeId);
72:
73: AndPredicate andPredicate = query.andPredicate();
74:• if (!Strings.isNullOrEmpty(name)) {
75: andPredicate.and(query.attributePredicate(KapuaNamedEntityAttributes.NAME, name));
76: }
77: query.setPredicate(andPredicate);
78:
79: query.setOffset(offset);
80: query.setLimit(limit);
81:
82: return query(scopeId, query);
83: }
84:
85: /**
86: * Queries the results with the given {@link GroupQuery} parameter.
87: *
88: * @param scopeId The {@link ScopeId} in which to search results.
89: * @param query The {@link GroupQuery} to use to filter results.
90: * @return The {@link GroupListResult} of all the result matching the given {@link GroupQuery} parameter.
91: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
92: * @since 1.0.0
93: */
94: @POST
95: @Path("_query")
96: @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
97: @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
98: public GroupListResult query(
99: @PathParam("scopeId") ScopeId scopeId,
100: GroupQuery query) throws KapuaException {
101: query.setScopeId(scopeId);
102:
103: return groupService.query(query);
104: }
105:
106: /**
107: * Counts the results with the given {@link GroupQuery} parameter.
108: *
109: * @param scopeId The {@link ScopeId} in which to search results.
110: * @param query The {@link GroupQuery} to use to filter results.
111: * @return The count of all the result matching the given {@link GroupQuery} parameter.
112: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
113: * @since 1.0.0
114: */
115: @POST
116: @Path("_count")
117: @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
118: @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
119: public CountResult count(
120: @PathParam("scopeId") ScopeId scopeId,
121: GroupQuery query) throws KapuaException {
122: query.setScopeId(scopeId);
123:
124: return new CountResult(groupService.count(query));
125: }
126:
127: /**
128: * Creates a new Group based on the information provided in GroupCreator
129: * parameter.
130: *
131: * @param scopeId The {@link ScopeId} in which to create the {@link Group}
132: * @param groupCreator Provides the information for the new {@link Group} to be created.
133: * @return The newly created {@link Group} object.
134: */
135: @POST
136: @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
137: @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
138: public Response create(
139: @PathParam("scopeId") ScopeId scopeId,
140: GroupCreator groupCreator) throws KapuaException {
141: groupCreator.setScopeId(scopeId);
142:
143: return returnCreated(groupService.create(groupCreator));
144: }
145:
146: /**
147: * Returns the Group specified by the "groupId" path parameter.
148: *
149: * @param scopeId The {@link ScopeId} of the requested {@link Group}.
150: * @param groupId The id of the requested Group.
151: * @return The requested Group object.
152: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
153: * @since 1.0.0
154: */
155: @GET
156: @Path("{groupId}")
157: @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
158: public Group find(
159: @PathParam("scopeId") ScopeId scopeId,
160: @PathParam("groupId") EntityId groupId) throws KapuaException {
161: Group group = groupService.find(scopeId, groupId);
162:
163:• if (group == null) {
164: throw new KapuaEntityNotFoundException(Group.TYPE, groupId);
165: }
166:
167: return group;
168: }
169:
170: /**
171: * Updates the Group based on the information provided in the Group parameter.
172: *
173: * @param scopeId The ScopeId of the requested {@link Group}.
174: * @param groupId The id of the requested {@link Group}
175: * @param group The modified Group whose attributed need to be updated.
176: * @return The updated group.
177: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
178: * @since 1.0.0
179: */
180: @PUT
181: @Path("{groupId}")
182: @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
183: @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
184: public Group update(
185: @PathParam("scopeId") ScopeId scopeId,
186: @PathParam("groupId") EntityId groupId,
187: Group group) throws KapuaException {
188: group.setScopeId(scopeId);
189: group.setId(groupId);
190:
191: return groupService.update(group);
192: }
193:
194: /**
195: * Deletes the Group specified by the "groupId" path parameter.
196: *
197: * @param scopeId The ScopeId of the requested {@link Group}.
198: * @param groupId The id of the Group to be deleted.
199: * @return HTTP 200 if operation has completed successfully.
200: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
201: * @since 1.0.0
202: */
203: @DELETE
204: @Path("{groupId}")
205: public Response deleteGroup(
206: @PathParam("scopeId") ScopeId scopeId,
207: @PathParam("groupId") EntityId groupId) throws KapuaException {
208: groupService.delete(scopeId, groupId);
209:
210: return returnNoContent();
211: }
212: }