use of org.wso2.iot.integration.common.MqttSubscriberClient in project product-iots by wso2.
the class VirtualFireAlarmTestCase method testPolicyPublishing.
@Test(description = "Test whether the policy publishing from the server to device works", dependsOnMethods = { "testEventPublishing" })
public void testPolicyPublishing() throws Exception {
String deviceId2 = userMode == TestUserMode.TENANT_ADMIN ? tenantDeviceId2 : VirtualFireAlarmTestCase.deviceId2;
String topic = automationContext.getContextTenant().getDomain() + "/" + DEVICE_TYPE + "/" + deviceId2 + "/operation/#";
String clientId = deviceId2 + ":" + DEVICE_TYPE;
HttpResponse response = restClient.post("/api/device-mgt/v1.0/policies", PayloadGenerator.getJsonPayload(Constants.VirtualFireAlarmConstants.PAYLOAD_FILE, Constants.VirtualFireAlarmConstants.POLICY_DATA).toString());
Assert.assertEquals("Policy creation for virtual fire alarm failed", HttpStatus.SC_CREATED, response.getResponseCode());
JsonObject jsonObject = new JsonParser().parse(response.getData()).getAsJsonObject();
String policyId = jsonObject.getAsJsonPrimitive("id").getAsString();
JsonArray jsonArray = new JsonArray();
jsonArray.add(new JsonPrimitive(policyId));
response = restClient.post(Constants.VirtualFireAlarmConstants.ACTIVATE_POLICY_ENDPOINT, jsonArray.toString());
Assert.assertEquals("Policy activation for virtual fire alarm failed", HttpStatus.SC_OK, response.getResponseCode());
MqttSubscriberClient mqttSubscriberClient = new MqttSubscriberClient(broker, clientId, topic, accessToken);
response = restClient.put(Constants.VirtualFireAlarmConstants.APPLY_CHANGES_ENDPOINT, "");
Assert.assertEquals("Applying changes to policy for virtual fire alarm failed", HttpStatus.SC_OK, response.getResponseCode());
// Allow some time for message delivery
Thread.sleep(20000);
ArrayList<MqttMessage> mqttMessages = mqttSubscriberClient.getMqttMessages();
Assert.assertEquals("Policy published message is not received by the mqtt listener. ", 2, mqttMessages.size());
}
use of org.wso2.iot.integration.common.MqttSubscriberClient in project product-iots by wso2.
the class DeviceTypeManagementJMeterTestCase method testMqttFlow.
@Test(description = "Test whether the policy publishing from the server to device works", dependsOnMethods = { "DeviceTypeManagementTest" })
public void testMqttFlow() throws Exception {
String deviceId = "123422578912";
String deviceType = "firealarmmqtt";
String payload = "{\"deviceIdentifiers\":[123422578912],\"operation\":{\"code\":\"ring\",\"type\":\"CONFIG\"," + "\"payLoad\":\"volume:30%\"}}";
String topic = automationContext.getContextTenant().getDomain() + "/" + deviceType + "/" + deviceId + "/operation/#";
String clientId = deviceId + ":firealarmmqtt";
MqttSubscriberClient mqttDeviceSubscriberClient = new MqttSubscriberClient(broker, clientId, topic, accessToken);
restClient.post("/api/device-mgt/v1.0/devices/" + deviceType + "/operations", payload);
// Allow some time for message delivery
Thread.sleep(10000);
ArrayList<MqttMessage> mqttMessages = mqttDeviceSubscriberClient.getMqttMessages();
Assert.assertEquals("listener did not recieve mqtt messages ", 1, mqttMessages.size());
String topicPub = automationContext.getContextTenant().getDomain() + "/" + deviceType + "/" + deviceId + "/events";
int qos = 2;
String clientIdPub = deviceId + ":firealarmmqttpub";
MemoryPersistence persistence = new MemoryPersistence();
MqttClient sampleClient = new MqttClient(broker, clientIdPub, persistence);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setUserName(accessToken);
connOpts.setPassword("".toCharArray());
connOpts.setKeepAliveInterval(120);
connOpts.setCleanSession(false);
log.info("Connecting to broker: " + broker);
sampleClient.connect(connOpts);
log.info("Connected");
for (int i = 0; i < 100; i++) {
payload = "{\"temperature\":%d,\"status\":\"workingh\",\"humidity\":20}";
MqttMessage message = new MqttMessage(String.format(payload, i).getBytes());
message.setQos(qos);
sampleClient.publish(topicPub, message);
log.info("Message is published to Mqtt Client");
Thread.sleep(1000);
}
sampleClient.disconnect();
log.info("Mqtt Client is Disconnected");
// Allow some time for message delivery
HttpResponse response = restClient.get("/api/device-mgt/v1.0/events/last-known/" + deviceType + "/" + deviceId);
Assert.assertEquals("No published event found (mqtt)", HttpStatus.SC_OK, response.getResponseCode());
log.error(response.getData());
JsonElement jsonElement = new JsonParser().parse(response.getData()).getAsJsonObject().get("count");
int count = jsonElement.getAsInt();
Assert.assertTrue("Event count does not match published event count, " + response.getData(), count > 0);
}
Aggregations