use of org.wso2.carbon.device.mgt.common.Device 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);
}
use of org.wso2.carbon.device.mgt.common.Device in project product-iots by wso2.
the class AndroidSenseEnrollment method testEnrollment.
@Test(description = "Test an Android sense device enrollment.")
public void testEnrollment() throws Exception {
// Time for deploying the carbon apps
Thread.sleep(30000);
HttpResponse response = client.post(Constants.AndroidSenseEnrollment.ENROLLMENT_ENDPOINT + DEVICE_ID + "/register?deviceName=android_sense_test", "");
Assert.assertEquals(HttpStatus.SC_OK, response.getResponseCode());
JsonElement jsonElement = new JsonParser().parse(response.getData());
JsonObject expectedPayloadObject = jsonElement.getAsJsonObject();
Assert.assertNotNull("Mqtt end-point is returned with the android sense enrollment " + "payload", expectedPayloadObject.get("mqttEndpoint"));
Assert.assertNotNull("Tenant domain is returned with the android sense enrollment " + "payload", expectedPayloadObject.get("tenantDomain"));
}
use of org.wso2.carbon.device.mgt.common.Device in project product-iots by wso2.
the class AndroidEnrollment method testIsEnrolled.
@Test(description = "Test an Android device is enrolled.", dependsOnMethods = { "testEnrollment" })
public void testIsEnrolled() throws Exception {
HttpResponse response = client.get(Constants.AndroidEnrollment.ENROLLMENT_ENDPOINT + "/" + deviceId + "/status");
Assert.assertEquals(HttpStatus.SC_OK, response.getResponseCode());
AssertUtil.jsonPayloadCompare(PayloadGenerator.getJsonPayload(Constants.AndroidEnrollment.ENROLLMENT_RESPONSE_PAYLOAD_FILE_NAME, Constants.HTTP_METHOD_GET).toString(), response.getData(), true);
}
use of org.wso2.carbon.device.mgt.common.Device in project product-iots by wso2.
the class AndroidEnrollment method testUpdateApplications.
@Test(description = "Test update applications", dependsOnMethods = { "testModifyEnrollment" })
public void testUpdateApplications() throws Exception {
JsonArray updateApplicationData = PayloadGenerator.getJsonArray(Constants.AndroidEnrollment.ENROLLMENT_PAYLOAD_FILE_NAME, Constants.AndroidEnrollment.UPDATE_APPLICATION_METHOD);
HttpResponse response = client.put(Constants.AndroidEnrollment.ENROLLMENT_ENDPOINT + "/" + deviceId + "/applications", updateApplicationData.toString());
Assert.assertEquals("Update of applications for the device id " + deviceId + " failed", HttpStatus.SC_ACCEPTED, response.getResponseCode());
response = client.get(Constants.MobileDeviceManagement.CHANGE_DEVICE_STATUS_ENDPOINT + Constants.AndroidEnrollment.ANDROID_DEVICE_TYPE + "/" + deviceId + "/applications");
Assert.assertEquals("Error while getting application list for the device with the id " + deviceId + " failed", HttpStatus.SC_OK, response.getResponseCode());
JsonArray jsonArray = new JsonParser().parse(response.getData()).getAsJsonArray();
Assert.assertEquals("Installed applications for the device with the device id " + deviceId + " has not been " + "updated yet", 3, jsonArray.size());
}
use of org.wso2.carbon.device.mgt.common.Device in project product-iots by wso2.
the class AndroidEnrollment method testGetPendingOperations.
@Test(description = "Test get pending operations", dependsOnMethods = { "testModifyEnrollment" })
public void testGetPendingOperations() throws Exception {
JsonArray pendingOperationsData = PayloadGenerator.getJsonArray(Constants.AndroidEnrollment.ENROLLMENT_PAYLOAD_FILE_NAME, Constants.AndroidEnrollment.GET_PENDING_OPERATIONS_METHOD);
HttpResponse response = client.put(Constants.AndroidEnrollment.ENROLLMENT_ENDPOINT + "/" + deviceId + "/pending-operations", pendingOperationsData.toString());
JsonArray pendingOperations = new JsonParser().parse(response.getData()).getAsJsonArray();
Assert.assertEquals("Error while getting pending operations for android device with the id " + deviceId, HttpStatus.SC_CREATED, response.getResponseCode());
Assert.assertTrue("Pending operation count is 0. Periodic monitoring tasks are not running.", 0 < pendingOperations.size());
}
Aggregations