Skip to content

Package: Accounts

Accounts

nameinstructionbranchcomplexitylinemethod
Accounts()
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, AccountQuery)
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, AccountCreator)
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%
deleteAccount(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: 17 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, AccountQuery)
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, boolean, String, SortOrder, int, int)
M: 51 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
update(ScopeId, EntityId, Account)
M: 12 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.SortOrder;
25: import org.eclipse.kapua.model.query.predicate.AndPredicate;
26: import org.eclipse.kapua.service.KapuaService;
27: import org.eclipse.kapua.service.account.Account;
28: import org.eclipse.kapua.service.account.AccountCreator;
29: import org.eclipse.kapua.service.account.AccountFactory;
30: import org.eclipse.kapua.service.account.AccountListResult;
31: import org.eclipse.kapua.service.account.AccountQuery;
32: import org.eclipse.kapua.service.account.AccountService;
33:
34: import javax.ws.rs.Consumes;
35: import javax.ws.rs.DELETE;
36: import javax.ws.rs.DefaultValue;
37: import javax.ws.rs.GET;
38: import javax.ws.rs.POST;
39: import javax.ws.rs.PUT;
40: import javax.ws.rs.Path;
41: import javax.ws.rs.PathParam;
42: import javax.ws.rs.Produces;
43: import javax.ws.rs.QueryParam;
44: import javax.ws.rs.core.MediaType;
45: import javax.ws.rs.core.Response;
46:
47: @Path("{scopeId}/accounts")
48: public class Accounts extends AbstractKapuaResource {
49:
50: private final KapuaLocator locator = KapuaLocator.getInstance();
51: private final AccountService accountService = locator.getService(AccountService.class);
52: private final AccountFactory accountFactory = locator.getFactory(AccountFactory.class);
53:
54: /**
55: * Gets the {@link Account} list in the scope.
56: *
57: * @param scopeId The {@link ScopeId} in which to search results.
58: * @param name The {@link Account} name in which to search results.
59: * @param recursive The {@link Account} name in which to search results.
60: * @param sortParam The name of the parameter that will be used as a sorting key
61: * @param sortDir The sort direction. Can be ASCENDING (default), DESCENDING. Case-insensitive.
62: * @param offset The result set offset.
63: * @param limit The result set limit.
64: * @return The {@link AccountListResult} of all the accounts associated to the current selected scope.
65: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
66: * @since 1.0.0
67: */
68: @GET
69: @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
70: public AccountListResult simpleQuery(@PathParam("scopeId") ScopeId scopeId, //
71: @QueryParam("name") String name, //
72: @QueryParam("recursive") boolean recursive, //
73: @QueryParam("sortParam") String sortParam,
74: @QueryParam("sortDir") @DefaultValue("ASCENDING") SortOrder sortDir,
75: @QueryParam("offset") @DefaultValue("0") int offset, //
76: @QueryParam("limit") @DefaultValue("50") int limit) throws KapuaException {
77:
78:• if (recursive) {
79: return accountService.findChildrenRecursively(scopeId);
80: }
81:
82: AccountQuery query = accountFactory.newQuery(scopeId);
83:
84: AndPredicate andPredicate = query.andPredicate();
85:• if (!Strings.isNullOrEmpty(name)) {
86: andPredicate.and(query.attributePredicate(KapuaNamedEntityAttributes.NAME, name));
87: }
88:• if (!Strings.isNullOrEmpty(sortParam)) {
89: query.setSortCriteria(query.fieldSortCriteria(sortParam, sortDir));
90: }
91: query.setPredicate(andPredicate);
92:
93: query.setOffset(offset);
94: query.setLimit(limit);
95:
96: return query(scopeId, query);
97: }
98:
99: /**
100: * Queries the results with the given {@link AccountQuery} parameter.
101: *
102: * @param scopeId The {@link ScopeId} in which to search results.
103: * @param query The {@link AccountQuery} to use to filter results.
104: * @return The {@link AccountListResult} of all the result matching the given {@link AccountQuery} parameter.
105: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
106: * @since 1.0.0
107: */
108: //
109: @POST
110: @Path("_query")
111: @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
112: @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
113: public AccountListResult query(
114: @PathParam("scopeId") ScopeId scopeId, //
115: AccountQuery query) throws KapuaException {
116: query.setScopeId(scopeId);
117:
118: return accountService.query(query);
119: }
120:
121: /**
122: * Counts the results with the given {@link AccountQuery} parameter.
123: *
124: * @param scopeId The {@link ScopeId} in which to count results.
125: * @param query The {@link AccountQuery} to use to filter results.
126: * @return The count of all the result matching the given {@link AccountQuery} parameter.
127: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
128: * @since 1.0.0
129: */
130:
131: @POST
132: @Path("_count")
133: @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
134: @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
135: public CountResult count(
136: @PathParam("scopeId") ScopeId scopeId, //
137: AccountQuery query) throws KapuaException {
138: query.setScopeId(scopeId);
139:
140: return new CountResult(accountService.count(query));
141: }
142:
143: /**
144: * Creates a new Account based on the information provided in AccountCreator
145: * parameter.
146: *
147: * @param scopeId The {@link ScopeId} in which to create the {@link Account}
148: * @param accountCreator Provides the information for the new {@link Account} to be created.
149: * @return The newly created {@link Account} object.
150: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
151: * @since 1.0.0
152: */
153:
154: @POST
155: @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
156: @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
157: public Response create(
158: @PathParam("scopeId") ScopeId scopeId, //
159: AccountCreator accountCreator) throws KapuaException {
160: accountCreator.setScopeId(scopeId);
161:
162: return returnCreated(accountService.create(accountCreator));
163: }
164:
165: /**
166: * Returns the Account specified by the "accountId" path parameter.
167: *
168: * @param scopeId The {@link ScopeId} of the requested {@link Account}.
169: * @param accountId The id of the requested Account.
170: * @return The requested Account object.
171: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
172: * @since 1.0.0
173: */
174:
175: @GET
176: @Path("{accountId}")
177: @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
178: public Account find(
179: @PathParam("scopeId") ScopeId scopeId, //
180: @PathParam("accountId") EntityId accountId) throws KapuaException {
181: Account account = accountService.find(scopeId, accountId);
182:
183:• if (account == null) {
184: throw new KapuaEntityNotFoundException(Account.TYPE, accountId);
185: }
186:
187: return account;
188: }
189:
190: /**
191: * Updates the Account based on the information provided in the Account parameter.
192: *
193: * @param scopeId The ScopeId of the requested {@link Account}.
194: * @param accountId The id of the requested {@link Account}
195: * @param account The modified Account whose attributed need to be updated.
196: * @return The updated account.
197: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
198: * @since 1.0.0
199: */
200:
201: @PUT
202: @Path("{accountId}")
203: @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
204: @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
205: public Account update(
206: @PathParam("scopeId") ScopeId scopeId, //
207: @PathParam("accountId") EntityId accountId, //
208: Account account) throws KapuaException {
209: account.setScopeId(scopeId);
210: account.setId(accountId);
211:
212: return accountService.update(account);
213: }
214:
215: /**
216: * Deletes the Account specified by the "accountId" path parameter.
217: *
218: * @param scopeId The ScopeId of the requested {@link Account}.
219: * @param accountId The id of the Account to be deleted.
220: * @return HTTP 200 if operation has completed successfully.
221: * @throws KapuaException Whenever something bad happens. See specific {@link KapuaService} exceptions.
222: * @since 1.0.0
223: */
224:
225: @DELETE
226: @Path("{accountId}")
227: public Response deleteAccount(
228: @PathParam("scopeId") ScopeId scopeId, //
229: @PathParam("accountId") EntityId accountId) throws KapuaException {
230: accountService.delete(scopeId, accountId);
231:
232: return returnNoContent();
233: }
234:
235: }