Skip to content

Package: BrokerSteps

BrokerSteps

nameinstructionbranchcomplexitylinemethod
BrokerSteps(EmbeddedBroker, DBHelper, StepData)
M: 17 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
afterScenario()
M: 11 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
assertPackage(String, String)
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
assetWithNameAndChannelWithNameAndValueAreReceived(String, String, int)
M: 30 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
beforeScenario(Scenario)
M: 65 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 15 C: 0
0%
M: 1 C: 0
0%
bundleIsPresent(String, long, String, String)
M: 14 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
bundlesReceived()
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%
checkConfiguration(String, String, String)
M: 25 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
checkNumberOfReceivedDevicePackages(long)
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%
clientConnect(String, String, String, String)
M: 46 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 14 C: 0
0%
M: 1 C: 0
0%
clientConnected(String)
M: 20 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
clientNotConnected(String)
M: 20 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
configurationReceived()
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%
configurationReceived(int)
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%
deviceAssetsAreRequested()
M: 48 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
deviceBirthMessage()
M: 38 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
deviceConnected()
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
deviceDeathMessage()
M: 38 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
deviceDisconnected()
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%
deviceStatusIs(String)
M: 34 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
deviceWithServerIp(String)
M: 40 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
disconnectClient(String)
M: 20 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
executeCommand(String)
M: 52 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
findAssetByName(String)
M: 45 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
findBundleByNameAndVersion(String, String)
M: 56 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
findConfigurationByNameAndValue(String, String, String)
M: 58 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
findPackageByNameAndVersion(String, String)
M: 56 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
getKuraMockDevice()
M: 41 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
iAddDeviceToKuraMock(int)
M: 48 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
kuraMockDisconnected()
M: 22 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
lambda$findAssetByName$4(String, DeviceAsset)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$findBundleByNameAndVersion$2(String, DeviceBundle)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$findBundleByNameAndVersion$3(String, DeviceBundle)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$findConfigurationByNameAndValue$5(String, DeviceComponentConfiguration)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$findConfigurationByNameAndValue$6(String, String, DeviceComponentConfiguration)
M: 17 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
lambda$findPackageByNameAndVersion$0(String, DevicePackage)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$findPackageByNameAndVersion$1(String, DevicePackage)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
packagesAreRequestedAndPackageIsReceived(int)
M: 45 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
packagesReceived()
M: 14 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
publishMessageByClient(String, String, String)
M: 28 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
requestBundles()
M: 44 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
requestConfiguration()
M: 43 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
requestPackages()
M: 39 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
restartKuraMock()
M: 38 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
startKuraMock()
M: 28 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 7 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.After;
18: import cucumber.api.java.Before;
19: import cucumber.api.java.en.And;
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.broker.core.setting.BrokerSetting;
25: import org.eclipse.kapua.commons.security.KapuaSecurityUtils;
26: import org.eclipse.kapua.commons.util.xml.JAXBContextProvider;
27: import org.eclipse.kapua.commons.util.xml.XmlUtil;
28: import org.eclipse.kapua.locator.KapuaLocator;
29: import org.eclipse.kapua.qa.common.DBHelper;
30: import org.eclipse.kapua.qa.common.StepData;
31: import org.eclipse.kapua.qa.common.TestBase;
32: import org.eclipse.kapua.qa.common.TestJAXBContextProvider;
33: import org.eclipse.kapua.qa.common.utils.EmbeddedBroker;
34: import org.eclipse.kapua.service.device.management.asset.DeviceAsset;
35: import org.eclipse.kapua.service.device.management.asset.DeviceAssetChannel;
36: import org.eclipse.kapua.service.device.management.asset.DeviceAssetManagementService;
37: import org.eclipse.kapua.service.device.management.asset.DeviceAssets;
38: import org.eclipse.kapua.service.device.management.asset.internal.DeviceAssetsImpl;
39: import org.eclipse.kapua.service.device.management.bundle.DeviceBundle;
40: import org.eclipse.kapua.service.device.management.bundle.DeviceBundleManagementService;
41: import org.eclipse.kapua.service.device.management.bundle.DeviceBundles;
42: import org.eclipse.kapua.service.device.management.command.DeviceCommandFactory;
43: import org.eclipse.kapua.service.device.management.command.DeviceCommandInput;
44: import org.eclipse.kapua.service.device.management.command.DeviceCommandManagementService;
45: import org.eclipse.kapua.service.device.management.command.DeviceCommandOutput;
46: import org.eclipse.kapua.service.device.management.configuration.DeviceComponentConfiguration;
47: import org.eclipse.kapua.service.device.management.configuration.DeviceConfiguration;
48: import org.eclipse.kapua.service.device.management.configuration.DeviceConfigurationManagementService;
49: import org.eclipse.kapua.service.device.management.packages.DevicePackageManagementService;
50: import org.eclipse.kapua.service.device.management.packages.model.DevicePackage;
51: import org.eclipse.kapua.service.device.management.packages.model.DevicePackages;
52: import org.eclipse.kapua.service.device.management.snapshot.DeviceSnapshotManagementService;
53: import org.eclipse.kapua.service.device.registry.Device;
54: import org.eclipse.kapua.service.device.registry.DeviceRegistryService;
55: import org.eclipse.kapua.service.device.registry.connection.DeviceConnection;
56: import org.eclipse.kapua.service.device.registry.connection.DeviceConnectionService;
57: import org.eclipse.paho.client.mqttv3.MqttClient;
58: import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
59: import org.eclipse.paho.client.mqttv3.MqttException;
60: import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
61: import org.junit.Assert;
62:
63: import javax.inject.Inject;
64: import java.nio.file.Files;
65: import java.nio.file.Paths;
66: import java.util.ArrayList;
67: import java.util.List;
68: import java.util.stream.Collectors;
69:
70: /**
71: * Steps used in integration scenarios with running MQTT broker and process of
72: * registering mocked Kura device registering with Kapua and issuing basic administrative
73: * commands on Mocked Kura.
74: */
75: @ScenarioScoped
76: public class BrokerSteps extends TestBase {
77:
78: /**
79: * Embedded broker configuration file from classpath resources.
80: */
81: public static final String ACTIVEMQ_XML = "xbean:activemq.xml";
82:
83: /**
84: * Device birth topic.
85: */
86: private String mqttBirth;
87:
88: /**
89: * Device death topic.
90: */
91: private String mqttDc;
92:
93: /**
94: * URI of mqtt broker.
95: */
96: private static final String BROKER_URI = "tcp://localhost:1883";
97:
98: private static final String KURA_DEVICES = "KuraDevices";
99: private static final String PACKAGES = "packages";
100: private static final String BUNDLES = "bundles";
101: private static final String CONFIGURATIONS = "configurations";
102: private static final String MQTT_TEST_CLIENT_NOT_FOUND = "Mqtt test client not found";
103:
104: /**
105: * Access to device management service.
106: */
107: private DevicePackageManagementService devicePackageManagementService;
108:
109: /**
110: * Device registry service for managing devices in DB.
111: */
112: private DeviceRegistryService deviceRegistryService;
113:
114: /**
115: * Configuration service for kura devices.
116: */
117: private DeviceConfigurationManagementService deviceConfiguratiomManagementService;
118:
119: /**
120: * Kura snapshot management service.
121: */
122: private DeviceSnapshotManagementService deviceSnapshotManagementService;
123:
124: /**
125: * Kura bundle management service.
126: */
127: private DeviceBundleManagementService deviceBundleManagementService;
128:
129: /**
130: * Service for issuing commnads on Kura device.
131: */
132: private DeviceCommandManagementService deviceCommandManagementService;
133:
134: /**
135: * Factory for creating commands sent to Kura.
136: */
137: private DeviceCommandFactory deviceCommandFactory;
138:
139: /**
140: * Service for connecting devices.
141: */
142: private static DeviceConnectionService deviceConnectionService;
143: private static DeviceAssetManagementService deviceAssetManagementService;
144:
145: /**
146: * Client simulating Kura device
147: */
148: private KuraDevice kuraDevice;
149: private ArrayList<KuraDevice> kuraDevices = kuraDevices = new ArrayList<>();
150:
151: /**
152: * Scenario scoped step data.
153: */
154: // private StepData stepData;
155: @Inject
156: public BrokerSteps(/* dependency */ EmbeddedBroker broker, DBHelper database, StepData stepData) {
157:
158: this.stepData = stepData;
159: this.database = database;
160: }
161:
162: @Before
163: public void beforeScenario(Scenario scenario) {
164:
165: this.scenario = scenario;
166:
167: BrokerSetting.resetInstance();
168:
169: KapuaLocator locator = KapuaLocator.getInstance();
170: devicePackageManagementService = locator.getService(DevicePackageManagementService.class);
171: deviceRegistryService = locator.getService(DeviceRegistryService.class);
172: deviceConfiguratiomManagementService = locator.getService(DeviceConfigurationManagementService.class);
173: deviceSnapshotManagementService = locator.getService(DeviceSnapshotManagementService.class);
174: deviceBundleManagementService = locator.getService(DeviceBundleManagementService.class);
175: deviceCommandManagementService = locator.getService(DeviceCommandManagementService.class);
176: deviceCommandFactory = locator.getFactory(DeviceCommandFactory.class);
177: deviceConnectionService = locator.getService(DeviceConnectionService.class);
178: deviceAssetManagementService = locator.getService(DeviceAssetManagementService.class);
179:
180: JAXBContextProvider consoleProvider = new TestJAXBContextProvider();
181: XmlUtil.setContextProvider(consoleProvider);
182: }
183:
184: @After
185: public void afterScenario() throws Exception {
186:
187:• if (kuraDevice != null) {
188: this.kuraDevice.mqttClientDisconnect();
189: }
190:
191: KapuaSecurityUtils.clearSession();
192:
193: this.stepData = null;
194: }
195:
196: @When("^I start the Kura Mock$")
197: public void startKuraMock() {
198:
199:• if (!kuraDevices.isEmpty()) {
200: kuraDevices.clear();
201: }
202: kuraDevice = new KuraDevice();
203: kuraDevice.mqttClientConnect();
204: kuraDevices.add(kuraDevice);
205:
206: stepData.put(KURA_DEVICES, kuraDevices);
207: }
208:
209: @When("^I restart the Kura Mock$")
210: public void restartKuraMock() throws Exception {
211: ArrayList<KuraDevice> kuraDevices = (ArrayList<KuraDevice>) stepData.get(KURA_DEVICES);
212: List<KuraDevice> restartedKuraDevices = new ArrayList<>();
213:• if (!kuraDevices.isEmpty()) {
214:• for (KuraDevice kuraDevice : kuraDevices) {
215: kuraDevice.mqttClientSetup();
216: kuraDevice.mqttClientConnect();
217: restartedKuraDevices.add(kuraDevice);
218: }
219: }
220: stepData.put(KURA_DEVICES, restartedKuraDevices);
221: }
222:
223:
224: @When("I get the KuraMock device(?:|s)$")
225: public void getKuraMockDevice() throws Exception {
226: ArrayList<Device> deviceList = new ArrayList<>();
227:• for (KuraDevice kuraDevice : kuraDevices) {
228: Device device = deviceRegistryService.findByClientId(getCurrentScopeId(), kuraDevice.getClientId());
229:• if (device != null) {
230: deviceList.add(device);
231: stepData.put("LastDevice", device);
232: }
233: }
234: stepData.put("DeviceList", deviceList);
235: }
236:
237: @When("^KuraMock is disconnected$")
238: public void kuraMockDisconnected() throws Exception {
239: ArrayList<KuraDevice> kuraDevices = (ArrayList<KuraDevice>) stepData.get(KURA_DEVICES);
240: deviceDeathMessage();
241:
242:• for (KuraDevice kuraDevice : kuraDevices) {
243: kuraDevice.mqttClientDisconnect();
244: }
245: }
246:
247: @When("^Device birth message is sent$")
248: public void deviceBirthMessage() throws Exception {
249: ArrayList<KuraDevice> kuraDevices = (ArrayList<KuraDevice>) stepData.get(KURA_DEVICES);
250:
251:• for (KuraDevice kuraDevice : kuraDevices) {
252: mqttBirth = "$EDC/kapua-sys/" + kuraDevice.getClientId() + "/MQTT/BIRTH";
253: kuraDevice.sendMessageFromFile(mqttBirth, 0, false, "/mqtt/rpione3_MQTT_BIRTH.mqtt");
254:
255: }
256:
257: }
258:
259: @When("^^Device(?:|s) (?:is|are) connected$$")
260: public void deviceConnected() throws Exception {
261: try {
262: deviceBirthMessage();
263: } catch (KapuaException ex) {
264: verifyException(ex);
265: }
266: }
267:
268: @When("^Device death message is sent$")
269: public void deviceDeathMessage() throws Exception {
270: ArrayList<KuraDevice> kuraDevices = (ArrayList<KuraDevice>) stepData.get(KURA_DEVICES);
271:
272:• for (KuraDevice kuraDevice : kuraDevices) {
273: mqttDc = "$EDC/kapua-sys/" + kuraDevice.getClientId() + "/MQTT/DC";
274: kuraDevice.sendMessageFromFile(mqttDc, 0, false, "/mqtt/rpione3_MQTT_DC.mqtt");
275: }
276: }
277:
278: @When("^Device is disconnected$")
279: public void deviceDisconnected() throws Exception {
280:
281: deviceDeathMessage();
282: }
283:
284: @When("^Packages are requested$")
285: public void requestPackages() throws Exception {
286:
287:• for (KuraDevice kuraDevice : kuraDevices) {
288: Device device = deviceRegistryService.findByClientId(SYS_SCOPE_ID, kuraDevice.getClientId());
289:• if (device != null) {
290: DevicePackages deploymentPackages = devicePackageManagementService.getInstalled(device.getScopeId(),
291: device.getId(), null);
292: List<DevicePackage> packages = deploymentPackages.getPackages();
293: stepData.put(PACKAGES, packages);
294: }
295: }
296: }
297:
298: @Then("^Packages are received$")
299: public void packagesReceived() {
300:
301: List<DevicePackage> packages = (List<DevicePackage>) stepData.get(PACKAGES);
302:• if (packages != null) {
303: assertEquals(1, packages.size());
304: }
305: }
306:
307: @Then("^Number of received packages is (\\d+)$")
308: public void checkNumberOfReceivedDevicePackages(long number) {
309: List<DevicePackage> receivedPackages = (List<DevicePackage>) stepData.get(PACKAGES);
310: assertEquals(number, receivedPackages.size());
311: }
312:
313: @Then("Package named (.*) with version (.*) is received$")
314: public void assertPackage(final String packageName, final String version) {
315: final DevicePackage pkg = findPackageByNameAndVersion(packageName, version);
316: Assert.assertEquals(packageName, pkg.getName());
317: Assert.assertEquals(version, pkg.getVersion());
318: }
319:
320: private DevicePackage findPackageByNameAndVersion(final String packageSymbolicName, final String version) {
321: List<DevicePackage> savedPackages = (List<DevicePackage>) stepData.get(PACKAGES);
322: List<DevicePackage> packages = savedPackages.stream()
323: .filter(bundle -> bundle.getName().equals(packageSymbolicName))
324: .filter(bundle -> bundle.getVersion().equals(version))
325: .collect(Collectors.toList());
326:
327:• if (packages.isEmpty()) {
328: Assert.fail(String.format("Package %s/%s is not present", packageSymbolicName, version));
329: }
330:• if (packages.size() > 1) {
331: Assert.fail(String.format("There is more than one entry for bundle %s/%s", packageSymbolicName, version));
332: }
333:
334: return packages.get(0);
335: }
336:
337: @When("^Bundles are requested$")
338: public void requestBundles() throws Exception {
339: ArrayList<KuraDevice> kuraDevices = (ArrayList<KuraDevice>) stepData.get(KURA_DEVICES);
340:
341:• for (KuraDevice kuraDevice : kuraDevices) {
342: Device device = deviceRegistryService.findByClientId(SYS_SCOPE_ID, kuraDevice.getClientId());
343: Assert.assertNotNull(device);
344: DeviceBundles deviceBundles = deviceBundleManagementService.get(device.getScopeId(), device.getId(), null);
345: List<DeviceBundle> bundles = deviceBundles.getBundles();
346: stepData.put(BUNDLES, bundles);
347: }
348: }
349:
350: @Then("^Bundles are received$")
351: public void bundlesReceived() {
352: List<DeviceBundle> bundles = (List<DeviceBundle>) stepData.get(BUNDLES);
353: assertEquals(137, bundles.size());
354: }
355:
356: @When("A bundle named (.*) with id (.*) and version (.*) is present and (.*)$")
357: public void bundleIsPresent(String bundleSymbolicName, long id, String version, String state) {
358: DeviceBundle bundle = findBundleByNameAndVersion(bundleSymbolicName, version);
359: Assert.assertEquals(id, bundle.getId());
360: Assert.assertEquals(state, bundle.getState());
361: }
362:
363: private DeviceBundle findBundleByNameAndVersion(final String bundleSymbolicName, final String version) {
364: List<DeviceBundle> savedBundles = (List<DeviceBundle>) stepData.get(BUNDLES);
365: List<DeviceBundle> bundles = savedBundles.stream()
366: .filter(bundle -> bundle.getName().equals(bundleSymbolicName))
367: .filter(bundle -> bundle.getVersion().equals(version))
368: .collect(Collectors.toList());
369:
370:• if (bundles.isEmpty()) {
371: Assert.fail(String.format("Bundle %s/%s is not present", bundleSymbolicName, version));
372: }
373:• if (bundles.size() > 1) {
374: Assert.fail(String.format("There is more than one entry for bundle %s/%s", bundleSymbolicName, version));
375: }
376:
377: return bundles.get(0);
378: }
379:
380: private DeviceAsset findAssetByName(final String assetSymbolicName) {
381: List<DeviceAsset> savedAssets = (List<DeviceAsset>) stepData.get("assets");
382:
383: List<DeviceAsset> assets = savedAssets.stream()
384: .filter(asset -> asset.getName().equals(assetSymbolicName))
385: .collect(Collectors.toList());
386:
387:• if (assets.isEmpty()) {
388: Assert.fail(String.format("Asset %s is not present", assetSymbolicName));
389: }
390:• if (assets.size() > 1) {
391: Assert.fail(String.format("There is more than one entry for asset %s", assetSymbolicName));
392: }
393:
394: return assets.get(0);
395: }
396:
397: @When("^Configuration is requested$")
398: public void requestConfiguration() throws Exception {
399: ArrayList<KuraDevice> kuraDevices = (ArrayList<KuraDevice>) stepData.get(KURA_DEVICES);
400:
401:• for (KuraDevice kuraDevice : kuraDevices) {
402: Device device = deviceRegistryService.findByClientId(SYS_SCOPE_ID, kuraDevice.getClientId());
403: Assert.assertNotNull(device);
404: DeviceConfiguration deviceConfiguration = deviceConfiguratiomManagementService.get(device.getScopeId(), device.getId(), null, null, null);
405: stepData.put(CONFIGURATIONS, deviceConfiguration);
406: }
407: }
408:
409: @When("A Configuration named (.*) has property (.*) with value (.*)$")
410: public void checkConfiguration(String configurationName, String configurationKey, String configurationValue) {
411: DeviceComponentConfiguration configuration = findConfigurationByNameAndValue(configurationName, configurationKey, configurationValue);
412: Assert.assertEquals(configurationName, configuration.getDefinition().getId());
413: Assert.assertTrue(configuration.getProperties().containsKey(configurationKey));
414: Assert.assertTrue(configuration.getProperties().get(configurationKey).toString().equals(configurationValue));
415: }
416:
417: private DeviceComponentConfiguration findConfigurationByNameAndValue(final String configurationName, final String configurationKey, final String configurationValue) {
418: DeviceConfiguration deviceConfiguration = (DeviceConfiguration) stepData.get(CONFIGURATIONS);
419:
420: List<DeviceComponentConfiguration> configurations = deviceConfiguration.getComponentConfigurations().stream()
421: .filter(configuration -> configuration.getDefinition().getId().equals(configurationName))
422:• .filter(configuration -> configuration.getProperties().containsKey(configurationKey)
423:• && configuration.getProperties().get(configurationKey).toString().equals(configurationValue))
424: .collect(Collectors.toList());
425:
426:• if (configurations.isEmpty()) {
427: Assert.fail(String.format("Configuration %s with value %s for property %s is not present", configurationName, configurationValue, configurationKey));
428: }
429:• if (configurations.size() > 1) {
430: Assert.fail(String.format("There is more than one entry for configuration %s", configurationName));
431: }
432:
433: return configurations.get(0);
434: }
435:
436: @Then("^Configuration is received$")
437: public void configurationReceived() {
438: DeviceConfiguration configurations = (DeviceConfiguration) stepData.get(CONFIGURATIONS);
439: assertEquals(17, configurations.getComponentConfigurations().size());
440: }
441:
442: @When("^Command (.*) is executed$")
443: public void executeCommand(String command) throws Exception {
444:
445:• for (KuraDevice kuraDevice : kuraDevices) {
446: Device device = deviceRegistryService.findByClientId(SYS_SCOPE_ID, kuraDevice.getClientId());
447: DeviceCommandInput commandInput = deviceCommandFactory.newCommandInput();
448: commandInput.setCommand(command);
449: commandInput.setRunAsynch(false);
450: commandInput.setTimeout(0);
451: DeviceCommandOutput deviceCommandOutput = deviceCommandManagementService.exec(device.getScopeId(),
452: device.getId(), commandInput, null);
453: Integer commandExitCode = deviceCommandOutput.getExitCode();
454: stepData.put("commandExitCode", commandExitCode);
455: }
456: }
457:
458: @Then("^Exit code (\\d+) is received$")
459: public void configurationReceived(int expectedExitCode) {
460:
461: Integer commandExitCode = (Integer) stepData.get("commandExitCode");
462: assertEquals(expectedExitCode, commandExitCode.intValue());
463: }
464:
465: @Then("^Device is connected with \"(.*)\" server ip$")
466: public void deviceWithServerIp(String serverIp) {
467:
468: DeviceConnection deviceConn = null;
469: stepData.put("ExceptionCaught", false);
470: try {
471:• for (KuraDevice kuraDevice : kuraDevices) {
472: deviceConn = deviceConnectionService.findByClientId(SYS_SCOPE_ID, kuraDevice.getClientId());
473: }
474: } catch (KapuaException ex) {
475: stepData.put("ExceptionCaught", true);
476: return;
477: }
478: assertEquals(serverIp, deviceConn.getServerIp());
479: }
480:
481:
482: @When("^Client with name \"(.*)\" with client id \"(.*)\" user \"(.*)\" password \"(.*)\" is connected$")
483: public void clientConnect(String clientName, String clientId, String user, String password) throws Exception {
484: MqttClient mqttClient = null;
485: MqttConnectOptions clientOpts = new MqttConnectOptions();
486: primeException();
487: try {
488: mqttClient = new MqttClient(BROKER_URI, clientId, new MemoryPersistence());
489: clientOpts.setUserName(user);
490: clientOpts.setPassword(password.toCharArray());
491: mqttClient.connect(clientOpts);
492: } catch (MqttException e) {
493: verifyException(e);
494: }
495:• if (mqttClient != null) {
496: stepData.put(clientName, mqttClient);
497: } else {
498: throw new Exception("Mqtt test client not connected.");
499: }
500: }
501:
502: @When("^topic \"(.*)\" content \"(.*)\" is published by client named \"(.*)\"$")
503: public void publishMessageByClient(String topic, String content, String clientName) throws Exception {
504: MqttClient mqttClient = (MqttClient) stepData.get(clientName);
505: byte[] payload = Files.readAllBytes(Paths.get(getClass().getResource(content).toURI()));
506:
507:• if (mqttClient == null) {
508: throw new Exception(MQTT_TEST_CLIENT_NOT_FOUND);
509: }
510: mqttClient.publish(topic, payload, 0, false);
511: }
512:
513: @Then("^Client named \"(.*)\" is connected$")
514: public void clientConnected(String clientName) throws Exception {
515: MqttClient mqttClient = (MqttClient) stepData.get(clientName);
516:• if (mqttClient == null) {
517: throw new Exception(MQTT_TEST_CLIENT_NOT_FOUND);
518: }
519: assertEquals(true, mqttClient.isConnected());
520: }
521:
522: @Then("^Client named \"(.*)\" is not connected$")
523: public void clientNotConnected(String clientName) throws Exception {
524: MqttClient mqttClient = (MqttClient) stepData.get(clientName);
525:• if (mqttClient == null) {
526: throw new Exception(MQTT_TEST_CLIENT_NOT_FOUND);
527: }
528: assertEquals(false, mqttClient.isConnected());
529: }
530:
531: @Then("^Disconnect client with name \"(.*)\"$")
532: public void disconnectClient(String clientName) throws Exception {
533: MqttClient mqttClient = (MqttClient) stepData.get(clientName);
534:• if (mqttClient == null) {
535: throw new Exception(MQTT_TEST_CLIENT_NOT_FOUND);
536: }
537: try {
538: mqttClient.disconnect();
539: mqttClient.close();
540: } catch (Exception e) {
541: // Exception eaten on purpose.
542: // Disconnect is for sanity only.
543: }
544: }
545:
546: @Then("^Device(?:|s) status is \"([^\"]*)\"$")
547: public void deviceStatusIs(String deviceStatus) throws Exception {
548: DeviceConnection deviceConn = null;
549: ArrayList<KuraDevice> kuraDevices = (ArrayList<KuraDevice>) stepData.get(KURA_DEVICES);
550: try {
551:• for (KuraDevice kuraDevice : kuraDevices) {
552: deviceConn = deviceConnectionService.findByClientId(SYS_SCOPE_ID, kuraDevice.getClientId());
553: }
554: } catch (KapuaException ex) {
555: return;
556: }
557: assertEquals(deviceStatus, deviceConn.getStatus().toString());
558: }
559:
560: @And("^I add (\\d+) devices to Kura Mock$")
561: public void iAddDeviceToKuraMock(int numberOfDevices) {
562:
563:• if (!kuraDevices.isEmpty()) {
564: kuraDevices.clear();
565: }
566:• for (int i = 0; i < numberOfDevices; i++) {
567: String clientId = "device" + i;
568: kuraDevice = new KuraDevice();
569: kuraDevice.addMoreThanOneDeviceToKuraMock(clientId);
570: kuraDevice.mqttClientConnect();
571: kuraDevices.add(kuraDevice);
572: }
573: stepData.put(KURA_DEVICES, kuraDevices);
574: }
575:
576: @And("^Device assets are requested$")
577: public void deviceAssetsAreRequested() throws Exception {
578: ArrayList<KuraDevice> kuraDevices = (ArrayList<KuraDevice>) stepData.get(KURA_DEVICES);
579: DeviceAssets deviceAssets = new DeviceAssetsImpl();
580:
581:• for (KuraDevice kuraDevice : kuraDevices) {
582: Device device = deviceRegistryService.findByClientId(SYS_SCOPE_ID, kuraDevice.getClientId());
583: Assert.assertNotNull(device);
584: DeviceAssets deviceAsset = deviceAssetManagementService.read(device.getScopeId(), device.getId(), deviceAssets, null);
585: List<DeviceAsset> assets = deviceAsset.getAssets();
586: stepData.put("assets", assets);
587: }
588: }
589:
590: @And("^Asset with name \"([^\"]*)\" and channel with name \"([^\"]*)\" and value (\\d+) are received$")
591: public void assetWithNameAndChannelWithNameAndValueAreReceived(String assetName, String channelName, int channelValue) throws Throwable {
592: DeviceAsset asset = findAssetByName(assetName);
593: Assert.assertEquals(assetName, asset.getName());
594:• for (DeviceAssetChannel deviceAssetChannel : asset.getChannels()) {
595: assertEquals(channelName, deviceAssetChannel.getName());
596: assertEquals(channelValue, deviceAssetChannel.getValue());
597: }
598: }
599:
600: @And("^Packages are requested and (\\d+) package(?:|s) (?:is|are) received$")
601: public void packagesAreRequestedAndPackageIsReceived(int numberOfPackages) throws Exception {
602:• for (KuraDevice kuraDevice : kuraDevices) {
603: Device device = deviceRegistryService.findByClientId(SYS_SCOPE_ID, kuraDevice.getClientId());
604:• if (device != null) {
605: DevicePackages deploymentPackages = devicePackageManagementService.getInstalled(device.getScopeId(),
606: device.getId(), null);
607: List<DevicePackage> packages = deploymentPackages.getPackages();
608: stepData.put(PACKAGES, packages);
609:
610: assertEquals(numberOfPackages, packages.size());
611: }
612: }
613: }
614: }