Skip to content

Package: AccessRoles

AccessRoles

nameinstructionbranchcomplexitylinemethod
AccessRoles()
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, EntityId, AccessRoleQuery)
M: 17 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
create(ScopeId, EntityId, AccessRoleCreator)
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
deleteAccessRole(ScopeId, EntityId, 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, EntityId)
M: 54 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
query(ScopeId, EntityId, AccessRoleQuery)
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
simpleQuery(ScopeId, EntityId, int, int)
M: 26 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 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 org.eclipse.kapua.KapuaEntityNotFoundException;
16: import org.eclipse.kapua.KapuaException;
17: import org.eclipse.kapua.app.api.core.resources.AbstractKapuaResource;
18: import org.eclipse.kapua.app.api.core.model.CountResult;
19: import org.eclipse.kapua.app.api.core.model.EntityId;
20: import org.eclipse.kapua.app.api.core.model.ScopeId;
21: import org.eclipse.kapua.locator.KapuaLocator;
22: import org.eclipse.kapua.model.KapuaEntityAttributes;
23: import org.eclipse.kapua.model.query.predicate.AndPredicate;
24: import org.eclipse.kapua.service.KapuaService;
25: import org.eclipse.kapua.service.authorization.access.AccessRole;
26: import org.eclipse.kapua.service.authorization.access.AccessRoleAttributes;
27: import org.eclipse.kapua.service.authorization.access.AccessRoleCreator;
28: import org.eclipse.kapua.service.authorization.access.AccessRoleFactory;
29: import org.eclipse.kapua.service.authorization.access.AccessRoleListResult;
30: import org.eclipse.kapua.service.authorization.access.AccessRoleQuery;
31: import org.eclipse.kapua.service.authorization.access.AccessRoleService;
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.Path;
39: import javax.ws.rs.PathParam;
40: import javax.ws.rs.Produces;
41: import javax.ws.rs.QueryParam;
42: import javax.ws.rs.core.MediaType;
43: import javax.ws.rs.core.Response;
44:
45: @Path("{scopeId}/accessinfos/{accessInfoId}/roles")
46: public class AccessRoles extends AbstractKapuaResource {
47:
48: private final KapuaLocator locator = KapuaLocator.getInstance();
49: private final AccessRoleService accessRoleService = locator.getService(AccessRoleService.class);
50: private final AccessRoleFactory accessRoleFactory = locator.getFactory(AccessRoleFactory.class);
51:
52: /**
53: * Gets the {@link AccessRole} list in the scope.
54: *
55: * @param scopeId The {@link ScopeId} in which to search results.
56: * @param offset The result set offset.
57: * @param limit The result set limit.
58: * @return The {@link AccessRoleListResult} of all the accessRoles associated to the current selected scope.
59: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
60: * @since 1.0.0
61: */
62: @GET
63: @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
64: public AccessRoleListResult simpleQuery(
65: @PathParam("scopeId") ScopeId scopeId,
66: @PathParam("accessInfoId") EntityId accessInfoId,
67: @QueryParam("offset") @DefaultValue("0") int offset,
68: @QueryParam("limit") @DefaultValue("50") int limit) throws KapuaException {
69: AccessRoleQuery query = accessRoleFactory.newQuery(scopeId);
70:
71: query.setPredicate(query.attributePredicate(AccessRoleAttributes.ACCESS_INFO_ID, accessInfoId));
72:
73: query.setOffset(offset);
74: query.setLimit(limit);
75:
76: return query(scopeId, accessInfoId, query);
77: }
78:
79: /**
80: * Queries the results with the given {@link AccessRoleQuery} parameter.
81: *
82: * @param scopeId The {@link ScopeId} in which to search results.
83: * @param query The {@link AccessRoleQuery} to use to filter results.
84: * @return The {@link AccessRoleListResult} of all the result matching the given {@link AccessRoleQuery} parameter.
85: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
86: * @since 1.0.0
87: */
88: @POST
89: @Path("_query")
90: @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
91: @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
92: public AccessRoleListResult query(
93: @PathParam("scopeId") ScopeId scopeId,
94: @PathParam("accessInfoId") EntityId accessInfoId,
95: AccessRoleQuery query) throws KapuaException {
96: query.setScopeId(scopeId);
97:
98: query.setPredicate(query.attributePredicate(AccessRoleAttributes.ACCESS_INFO_ID, accessInfoId));
99:
100: return accessRoleService.query(query);
101: }
102:
103: /**
104: * Counts the results with the given {@link AccessRoleQuery} parameter.
105: *
106: * @param scopeId The {@link ScopeId} in which to search results.
107: * @param query The {@link AccessRoleQuery} to use to filter results.
108: * @return The count of all the result matching the given {@link AccessRoleQuery} parameter.
109: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
110: * @since 1.0.0
111: */
112: @POST
113: @Path("_count")
114: @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
115: @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
116: public CountResult count(
117: @PathParam("scopeId") ScopeId scopeId,
118: @PathParam("accessInfoId") EntityId accessInfoId,
119: AccessRoleQuery query) throws KapuaException {
120: query.setScopeId(scopeId);
121:
122: query.setPredicate(query.attributePredicate(AccessRoleAttributes.ACCESS_INFO_ID, accessInfoId));
123:
124: return new CountResult(accessRoleService.count(query));
125: }
126:
127: /**
128: * Creates a new AccessRole based on the information provided in AccessRoleCreator
129: * parameter.
130: *
131: * @param scopeId The {@link ScopeId} in which to create the {@link AccessRole}.
132: * @param accessRoleCreator Provides the information for the new AccessRole to be created.
133: * @return The newly created {@link AccessRole} object.
134: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
135: * @since 1.0.0
136: */
137: @POST
138: @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
139: @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
140: public Response create(
141: @PathParam("scopeId") ScopeId scopeId,
142: @PathParam("accessInfoId") EntityId accessInfoId,
143: AccessRoleCreator accessRoleCreator) throws KapuaException {
144: accessRoleCreator.setScopeId(scopeId);
145: accessRoleCreator.setAccessInfoId(accessInfoId);
146:
147: return returnCreated(accessRoleService.create(accessRoleCreator));
148: }
149:
150: /**
151: * Returns the AccessRole specified by the "accessRoleId" path parameter.
152: *
153: * @param scopeId The {@link ScopeId} of the requested {@link AccessRole}.
154: * @param accessRoleId The id of the requested {@link AccessRole}.
155: * @return The requested {@link AccessRole} object.
156: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
157: * @since 1.0.0
158: */
159: @GET
160: @Path("{accessRoleId}")
161: @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
162: public AccessRole find(
163: @PathParam("scopeId") ScopeId scopeId,
164: @PathParam("accessInfoId") EntityId accessInfoId,
165: @PathParam("accessRoleId") EntityId accessRoleId) throws KapuaException {
166: AccessRoleQuery query = accessRoleFactory.newQuery(scopeId);
167:
168: AndPredicate andPredicate = query.andPredicate(
169: query.attributePredicate(AccessRoleAttributes.ACCESS_INFO_ID, accessInfoId),
170: query.attributePredicate(KapuaEntityAttributes.ENTITY_ID, accessRoleId)
171: );
172:
173: query.setPredicate(andPredicate);
174: query.setOffset(0);
175: query.setLimit(1);
176:
177: AccessRoleListResult results = accessRoleService.query(query);
178:
179:• if (results.isEmpty()) {
180: throw new KapuaEntityNotFoundException(AccessRole.TYPE, accessRoleId);
181: }
182:
183: return results.getFirstItem();
184: }
185:
186: /**
187: * Deletes the AccessRole specified by the "accessRoleId" path parameter.
188: *
189: * @param accessRoleId The id of the AccessRole to be deleted.
190: * @return HTTP 200 if operation has completed successfully.
191: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
192: * @since 1.0.0
193: */
194: @DELETE
195: @Path("{accessRoleId}")
196: public Response deleteAccessRole(
197: @PathParam("scopeId") ScopeId scopeId,
198: @PathParam("accessInfoId") EntityId accessInfoId,
199: @PathParam("accessRoleId") EntityId accessRoleId) throws KapuaException {
200: accessRoleService.delete(scopeId, accessRoleId);
201:
202: return returnNoContent();
203: }
204: }