Search in sources :

Example 36 with Device

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);
}
Also used : MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) MemoryPersistence(org.eclipse.paho.client.mqttv3.persist.MemoryPersistence) MqttConnectOptions(org.eclipse.paho.client.mqttv3.MqttConnectOptions) JsonElement(com.google.gson.JsonElement) HttpResponse(org.wso2.carbon.automation.test.utils.http.client.HttpResponse) MqttSubscriberClient(org.wso2.iot.integration.common.MqttSubscriberClient) JsonParser(com.google.gson.JsonParser) Test(org.testng.annotations.Test) JMeterTest(org.wso2.carbon.automation.extensions.jmeter.JMeterTest)

Example 37 with Device

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"));
}
Also used : JsonElement(com.google.gson.JsonElement) HttpResponse(org.wso2.carbon.automation.test.utils.http.client.HttpResponse) JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser) Test(org.testng.annotations.Test)

Example 38 with Device

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);
}
Also used : HttpResponse(org.wso2.carbon.automation.test.utils.http.client.HttpResponse) Test(org.testng.annotations.Test)

Example 39 with Device

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());
}
Also used : JsonArray(com.google.gson.JsonArray) HttpResponse(org.wso2.carbon.automation.test.utils.http.client.HttpResponse) JsonParser(com.google.gson.JsonParser) Test(org.testng.annotations.Test)

Example 40 with Device

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());
}
Also used : JsonArray(com.google.gson.JsonArray) HttpResponse(org.wso2.carbon.automation.test.utils.http.client.HttpResponse) JsonParser(com.google.gson.JsonParser) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)20 HttpResponse (org.wso2.carbon.automation.test.utils.http.client.HttpResponse)17 DeviceManagementException (org.wso2.carbon.device.mgt.common.DeviceManagementException)13 DeviceMgtPluginException (org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException)11 JsonParser (com.google.gson.JsonParser)10 SQLException (java.sql.SQLException)8 JsonObject (com.google.gson.JsonObject)7 Connection (java.sql.Connection)7 PreparedStatement (java.sql.PreparedStatement)7 HashMap (java.util.HashMap)6 ConnectedCupDeviceMgtPluginException (org.coffeeking.connectedcup.plugin.exception.ConnectedCupDeviceMgtPluginException)6 Device (org.wso2.carbon.device.mgt.common.Device)6 JsonArray (com.google.gson.JsonArray)5 JsonElement (com.google.gson.JsonElement)4 ResultSet (java.sql.ResultSet)4 ArrayList (java.util.ArrayList)4 Path (javax.ws.rs.Path)4 JSONObject (org.json.simple.JSONObject)4 PrivilegedCarbonContext (org.wso2.carbon.context.PrivilegedCarbonContext)4 DeviceIdentifier (org.wso2.carbon.device.mgt.common.DeviceIdentifier)4