Skip to content

Package: AclSteps

AclSteps

nameinstructionbranchcomplexitylinemethod
AclSteps(StepData)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
aclStepsBefore(Scenario)
M: 57 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 14 C: 0
0%
M: 1 C: 0
0%
brokerDoesntReceiveStringOnTopic(String, String)
M: 15 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
brokerReceivesStringOnTopic(String, String)
M: 18 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
clientDoesntReceiveStringOnTopic(String, String, String)
M: 20 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
clientPublishString(String, String, String)
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%
connectClientToBroker(String, String, String, String)
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
createBrokerAccountAndUser()
M: 20 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
createDataManageAccountAndUser()
M: 20 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
createDataViewAccountAndUser()
M: 20 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
createDeviceAccountAndUser()
M: 20 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
createOtherBrokerAccountAndUser()
M: 20 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
disconnectClientsFromBroker()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
exceptionIsNotThrown()
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
exceptionIsThrown()
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
iReceiveStringOnTopic(String, String, String)
M: 41 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
startMqttDevice()
M: 17 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
stopMqttDevice()
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
waitInMillis(long)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2017, 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
12: * Red Hat Inc
13: *******************************************************************************/
14: package org.eclipse.kapua.service.device.registry.steps;
15:
16: import cucumber.api.Scenario;
17: import cucumber.api.java.Before;
18: import cucumber.api.java.en.And;
19: import cucumber.api.java.en.Given;
20: import cucumber.api.java.en.Then;
21: import cucumber.api.java.en.When;
22: import cucumber.runtime.java.guice.ScenarioScoped;
23: import org.eclipse.kapua.KapuaException;
24: import org.eclipse.kapua.locator.KapuaLocator;
25: import org.eclipse.kapua.qa.common.TestBase;
26: import org.eclipse.kapua.qa.common.StepData;
27: import org.eclipse.kapua.service.account.Account;
28: import org.eclipse.kapua.service.account.AccountFactory;
29: import org.eclipse.kapua.service.account.AccountService;
30: import org.eclipse.kapua.service.authentication.AuthenticationService;
31: import org.eclipse.kapua.service.authentication.CredentialsFactory;
32: import org.eclipse.kapua.service.authentication.LoginCredentials;
33: import org.eclipse.kapua.service.authentication.credential.CredentialService;
34: import org.eclipse.kapua.service.authorization.access.AccessInfoService;
35: import org.eclipse.kapua.service.user.User;
36: import org.eclipse.kapua.service.user.UserService;
37: import org.eclipse.paho.client.mqttv3.MqttException;
38: import org.junit.Assert;
39:
40: import javax.inject.Inject;
41: import java.util.HashMap;
42: import java.util.Map;
43:
44: /**
45: * Steps for testing Access Control List functionality on Broker service.
46: */
47: @ScenarioScoped
48: public class AclSteps extends TestBase {
49:
50: public static final int BROKER_START_WAIT_MILLIS = 5000;
51:
52: private static final String SYS_USERNAME = "kapua-sys";
53:
54: private static final String SYS_PASSWORD = "kapua-password";
55:
56: private static final String ACCOUNT = "acme";
57: private static final String MAIL = "john@acme.org";
58: private static final String ORG = "ACME Corp.";
59: private static final String NAME = "luise";
60:
61: /**
62: * Mqtt device for listening and sending data from/to broker
63: */
64: private static MqttDevice mqttDevice;
65:
66: /**
67: * Topic / value pair containing message that was received from broker by client.
68: */
69: private static Map<String, Map<String, String>> clientMqttMessage;
70:
71: /**
72: * Topic / value pair containing message that was received from broker by listener.
73: */
74: private static Map<String, String> listenerMqttMessage;
75:
76: /**
77: * Authentication service.
78: */
79: private static AuthenticationService authenticationService;
80:
81: /**
82: * Credentials factory.
83: */
84: private static CredentialsFactory credentialsFactory;
85:
86: /**
87: * Account service.
88: */
89: private static AccountService accountService;
90:
91: /**
92: * Account factory.
93: */
94: private static AccountFactory accountFactory;
95:
96: /**
97: * User service.
98: */
99: private static UserService userService;
100:
101: /**
102: * Credential service.
103: */
104: private static CredentialService credentialService;
105:
106: /**
107: * Accessinfo service.
108: */
109: private static AccessInfoService accessInfoService;
110:
111: /**
112: * Helper for creating Accoutn, User and other artifacts needed in tests.
113: */
114: private static AclCreator aclCreator;
115:
116: @Inject
117: public AclSteps(StepData stepData) {
118:
119: this.stepData = stepData;
120: }
121:
122: @Before
123: public void aclStepsBefore(Scenario scenario) {
124:
125: KapuaLocator locator = KapuaLocator.getInstance();
126: authenticationService = locator.getService(AuthenticationService.class);
127: credentialsFactory = locator.getFactory(CredentialsFactory.class);
128: accountService = locator.getService(AccountService.class);
129: accountFactory = locator.getFactory(AccountFactory.class);
130: userService = locator.getService(UserService.class);
131: credentialService = locator.getService(CredentialService.class);
132: accessInfoService = locator.getService(AccessInfoService.class);
133:
134: mqttDevice = new MqttDevice();
135: clientMqttMessage = new HashMap<>();
136: listenerMqttMessage = new HashMap<>();
137:
138: this.scenario = scenario;
139:
140: aclCreator = new AclCreator();
141: }
142:
143: @Given("string \"(.*)\" is published to topic \"(.*)\" with client \"(.*)\"$")
144: public void clientPublishString(String payload, String topic, String clientId) {
145:
146: mqttDevice.mqttClientPublishString(clientId, payload, topic, clientMqttMessage, listenerMqttMessage);
147: }
148:
149: @Given("^Mqtt Device is started$")
150: public void startMqttDevice() throws KapuaException {
151:
152: mqttDevice.mqttSubscriberConnect();
153: // Wait for broker to start
154: waitInMillis(BROKER_START_WAIT_MILLIS);
155: // Login with system user
156: String passwd = SYS_PASSWORD;
157: LoginCredentials credentials = credentialsFactory.newUsernamePasswordCredentials(SYS_USERNAME, passwd);
158: authenticationService.login(credentials);
159: }
160:
161: @When("^Mqtt Device is stoped$")
162: public void stopMqttDevice() throws KapuaException {
163:
164: mqttDevice.mqttClientsDisconnect();
165: mqttDevice.mqttSubscriberDisconnect();
166: // Logout system user
167: authenticationService.logout();
168: }
169:
170: @Given("^broker with clientId \"(.*)\" and user \"(.*)\" and password \"(.*)\" is listening on topic \"(.*)\"$")
171: public void connectClientToBroker(String clientId, String userName, String password, String topicFilter)
172: throws Exception {
173:
174: try {
175: primeException();
176: mqttDevice.mqttClientConnect(clientId, userName, password, topicFilter);
177: } catch (MqttException mqtte) {
178: verifyException(mqtte);
179: }
180: }
181:
182: @Given("^clients are disconnected$")
183: public void disconnectClientsFromBroker() {
184:
185: mqttDevice.mqttClientsDisconnect();
186: }
187:
188: @Then("^Broker receives string \"([^\"]*)\" on topic \"([^\"]*)\"$")
189: public void brokerReceivesStringOnTopic(String payload, String topic) throws Throwable {
190:
191:• if ((listenerMqttMessage != null) && (listenerMqttMessage.size() == 1)) {
192: String message = listenerMqttMessage.get(topic);
193: Assert.assertEquals(payload, message);
194: } else {
195: Assert.fail("Message not received by broker.");
196: }
197: }
198:
199: @Then("^Broker doesn't receive string \"([^\"]*)\" on topic \"([^\"]*)\"$")
200: public void brokerDoesntReceiveStringOnTopic(String payload, String topic) throws Throwable {
201:
202:• if ((listenerMqttMessage != null) && (listenerMqttMessage.size() >= 1)) {
203: String message = listenerMqttMessage.get(topic);
204: Assert.assertNotEquals(payload, message);
205: }
206: }
207:
208: @Then("^client \"([^\"]*)\" receives string \"([^\"]*)\" on topic \"([^\"]*)\"$")
209: public void iReceiveStringOnTopic(String clientId, String payload, String topic) throws Throwable {
210:
211: Map<String, String> messages = clientMqttMessage.get(clientId);
212:• if ((messages != null) && (messages.size() >= 1)) {
213: String message = messages.get(topic);
214: Assert.assertEquals(payload, message);
215: } else {
216: // TODO log (or append in the failure message) this error in a better way
217:• Assert.fail("Message not received by broker." + (listenerMqttMessage != null && listenerMqttMessage.size() > 0 ? listenerMqttMessage.get(0) : " NULL"));
218: }
219: }
220:
221: @Then("^client \"([^\"]*)\" doesn't receive string \"([^\"]*)\" on topic \"([^\"]*)\"$")
222: public void clientDoesntReceiveStringOnTopic(String clientId, String payload, String topic) throws Throwable {
223:
224: Map<String, String> messages = clientMqttMessage.get(clientId);
225:• if ((messages != null) && (messages.size() >= 1)) {
226: String message = messages.get(topic);
227: Assert.assertNotEquals(payload, message);
228: }
229: }
230:
231: @And("^broker account and user are created$")
232: public void createBrokerAccountAndUser() throws Throwable {
233:
234: Account account = aclCreator.createAccount(ACCOUNT, ORG, MAIL);
235: User user = aclCreator.createUser(account, NAME);
236: aclCreator.attachUserCredentials(account, user);
237: aclCreator.attachBrokerPermissions(account, user);
238: }
239:
240: @And("^device account and user are created$")
241: public void createDeviceAccountAndUser() throws Throwable {
242:
243: Account account = aclCreator.createAccount(ACCOUNT, ORG, MAIL);
244: User user = aclCreator.createUser(account, NAME);
245: aclCreator.attachUserCredentials(account, user);
246: aclCreator.attachDevicePermissions(account, user);
247: }
248:
249: @And("^data view account and user are created$")
250: public void createDataViewAccountAndUser() throws Throwable {
251:
252: Account account = aclCreator.createAccount(ACCOUNT, ORG, MAIL);
253: User user = aclCreator.createUser(account, NAME);
254: aclCreator.attachUserCredentials(account, user);
255: aclCreator.attachDataViewPermissions(account, user);
256: }
257:
258: @And("^data manage account and user are created$")
259: public void createDataManageAccountAndUser() throws Throwable {
260:
261: Account account = aclCreator.createAccount(ACCOUNT, ORG, MAIL);
262: User user = aclCreator.createUser(account, NAME);
263: aclCreator.attachUserCredentials(account, user);
264: aclCreator.attachDataManagePermissions(account, user);
265: }
266:
267: @And("^other broker account and user are created$")
268: public void createOtherBrokerAccountAndUser() throws Throwable {
269:
270: Account account = aclCreator.createAccount("domino","Domino Corp.", "lisa@domino.org");
271: User user = aclCreator.createUser(account, "domina");
272: aclCreator.attachUserCredentials(account, user);
273: aclCreator.attachBrokerPermissions(account, user);
274: }
275:
276: @Then("^exception is thrown$")
277: public void exceptionIsThrown() throws Throwable {
278:
279: Exception e = (Exception) stepData.get("exception");
280: Assert.assertNotNull("Exception expected!", e);
281: }
282:
283: @Then("^exception is not thrown$")
284: public void exceptionIsNotThrown() throws Throwable {
285:
286: Exception e = (Exception) stepData.get("exception");
287: Assert.assertNull("Exception not expected!", e);
288: }
289:
290: /**
291: * Simple wait implementation.
292: *
293: * @param millis milli seconds
294: */
295: private void waitInMillis(long millis) {
296: try {
297: Thread.sleep(millis);
298: } catch (InterruptedException e) {
299: // ignore
300: }
301: }
302:
303: }