Search in sources :

Example 6 with HttpsTransportManager

use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsTransportManager in project azure-iot-sdk-java by Azure.

the class HttpsTransportManagerTest method openSucceed.

/* Tests_SRS_HTTPSTRANSPORTMANAGER_21_003: [The open shall create and store a new transport connection `HttpsIotHubConnection`.] */
@Test
public void openSucceed() {
    // arrange
    final HttpsIotHubConnection httpsIotHubConnection = mockConn;
    HttpsTransportManager httpsTransportManager = Deencapsulation.newInstance(HttpsTransportManager.class, new Class[] { DeviceClientConfig.class }, mockConfig);
    new NonStrictExpectations() {

        {
            Deencapsulation.newInstance(HttpsIotHubConnection.class, new Class[] { DeviceClientConfig.class }, mockConfig);
            result = httpsIotHubConnection;
            times = 1;
        }
    };
    // act
    Deencapsulation.invoke(httpsTransportManager, "open");
    // assert
    assertNotNull(Deencapsulation.getField(httpsTransportManager, "httpsIotHubConnection"));
}
Also used : HttpsTransportManager(com.microsoft.azure.sdk.iot.device.transport.https.HttpsTransportManager) HttpsIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsIotHubConnection) Test(org.junit.Test)

Example 7 with HttpsTransportManager

use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsTransportManager in project azure-iot-sdk-java by Azure.

the class HttpsTransportManagerTest method invokeMethodOnModuleSuccess.

// Tests_SRS_HTTPSTRANSPORTMANAGER_34_017: [This function shall call invokeMethod with the provided request and
// a uri in the format twins/<device id>/modules/<module id>/methods?api-version=<api_version>.]
@Test
public void invokeMethodOnModuleSuccess() throws TransportException, IOException, URISyntaxException {
    // arrange
    final HttpsTransportManager transportManager = new HttpsTransportManager(mockConfig);
    final String expectedDeviceId = "myDevice";
    final String expectedModuleId = "myModule";
    final String expectedSenderDeviceId = "mySenderDevice";
    final String expectedSenderModuleId = "mySenderModule";
    final String expectedMethodRequestJson = "someJson";
    final String expectedResponseBody = "some body";
    // assert
    new Expectations(HttpsTransportManager.class) {

        {
            mockedMethodRequest.toJson();
            result = expectedMethodRequestJson;
            new IotHubTransportMessage(expectedMethodRequestJson);
            result = mockedTransportMessage;
            mockedTransportMessage.setIotHubMethod(IotHubMethod.POST);
            mockedTransportMessage.setUriPath("/twins/" + expectedDeviceId + "/modules/" + expectedModuleId + "/methods");
            mockConfig.getDeviceId();
            result = expectedSenderDeviceId;
            mockConfig.getModuleId();
            result = expectedSenderModuleId;
            transportManager.send(mockedTransportMessage, (Map) any);
            result = mockResponseMessage;
            mockResponseMessage.getStatus();
            result = IotHubStatusCode.OK_EMPTY;
            mockResponseMessage.getBytes();
            result = expectedResponseBody.getBytes(StandardCharsets.UTF_8);
            new MethodResult(expectedResponseBody);
        }
    };
    // act
    transportManager.invokeMethod(mockedMethodRequest, expectedDeviceId, expectedModuleId);
}
Also used : HttpsTransportManager(com.microsoft.azure.sdk.iot.device.transport.https.HttpsTransportManager) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) MethodResult(com.microsoft.azure.sdk.iot.device.edge.MethodResult) Test(org.junit.Test)

Example 8 with HttpsTransportManager

use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsTransportManager in project azure-iot-sdk-java by Azure.

the class HttpsTransportManagerTest method invokeMethodOnDeviceThrowsIfIotHubRespondsWithErrorCode.

// Tests_SRS_HTTPSTRANSPORTMANAGER_34_026 [If the http response contains an error code, this function shall throw the associated exception.]
@Test(expected = HubOrDeviceIdNotFoundException.class)
public void invokeMethodOnDeviceThrowsIfIotHubRespondsWithErrorCode() throws TransportException, IOException, URISyntaxException {
    // arrange
    final HttpsTransportManager transportManager = new HttpsTransportManager(mockConfig);
    final String expectedDeviceId = "myDevice";
    final String expectedSenderDeviceId = "mySenderDevice";
    final String expectedSenderModuleId = "mySenderModule";
    final String expectedMethodRequestJson = "someJson";
    final String expectedResponseBody = "some body";
    // assert
    new Expectations(HttpsTransportManager.class) {

        {
            mockedMethodRequest.toJson();
            result = expectedMethodRequestJson;
            new IotHubTransportMessage(expectedMethodRequestJson);
            result = mockedTransportMessage;
            mockedTransportMessage.setIotHubMethod(IotHubMethod.POST);
            mockedTransportMessage.setUriPath("/twins/" + expectedDeviceId + "/methods");
            mockConfig.getDeviceId();
            result = expectedSenderDeviceId;
            mockConfig.getModuleId();
            result = expectedSenderModuleId;
            transportManager.send(mockedTransportMessage, (Map) any);
            result = mockResponseMessage;
            mockResponseMessage.getStatus();
            result = IotHubStatusCode.HUB_OR_DEVICE_ID_NOT_FOUND;
            mockResponseMessage.getBytes();
            result = expectedResponseBody.getBytes(StandardCharsets.UTF_8);
        }
    };
    // act
    transportManager.invokeMethod(mockedMethodRequest, expectedDeviceId, "");
}
Also used : HttpsTransportManager(com.microsoft.azure.sdk.iot.device.transport.https.HttpsTransportManager) IotHubTransportMessage(com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage) Test(org.junit.Test)

Example 9 with HttpsTransportManager

use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsTransportManager in project azure-iot-sdk-java by Azure.

the class HttpsTransportManagerTest method openWithTopicsSucceed.

/* Tests_SRS_HTTPSTRANSPORTMANAGER_21_004: [The open shall create and store a new transport connection `HttpsIotHubConnection`.] */
/* Tests_SRS_HTTPSTRANSPORTMANAGER_21_005: [The open shall ignore the parameter `topics`.] */
@Test
public void openWithTopicsSucceed() {
    // arrange
    final HttpsIotHubConnection httpsIotHubConnection = mockConn;
    final String[] topics = new String[] { "a", "b" };
    HttpsTransportManager httpsTransportManager = Deencapsulation.newInstance(HttpsTransportManager.class, new Class[] { DeviceClientConfig.class }, mockConfig);
    new NonStrictExpectations() {

        {
            Deencapsulation.newInstance(HttpsIotHubConnection.class, new Class[] { DeviceClientConfig.class }, mockConfig);
            result = httpsIotHubConnection;
            times = 1;
        }
    };
    // act
    Deencapsulation.invoke(httpsTransportManager, "open", new Class<?>[] { String[].class }, (Object) topics);
    // assert
    assertNotNull(Deencapsulation.getField(httpsTransportManager, "httpsIotHubConnection"));
}
Also used : HttpsTransportManager(com.microsoft.azure.sdk.iot.device.transport.https.HttpsTransportManager) HttpsIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsIotHubConnection) Test(org.junit.Test)

Example 10 with HttpsTransportManager

use of com.microsoft.azure.sdk.iot.device.transport.https.HttpsTransportManager in project azure-iot-sdk-java by Azure.

the class HttpsTransportManagerTest method sendSendHttpsMessageThrows.

/* Tests_SRS_HTTPSTRANSPORTMANAGER_21_014: [If `sendHttpsMessage` failed, the send shall bypass the exception.] */
@Test(expected = IOException.class)
public void sendSendHttpsMessageThrows() throws IOException, TransportException {
    // arrange
    final HttpsIotHubConnection httpsIotHubConnection = mockConn;
    final String uriPath = "/files/notifications";
    new NonStrictExpectations() {

        {
            Deencapsulation.newInstance(HttpsIotHubConnection.class, new Class[] { DeviceClientConfig.class }, mockConfig);
            result = httpsIotHubConnection;
            Deencapsulation.invoke(HttpsSingleMessage.class, "parseHttpsJsonMessage", new Class[] { Message.class }, mockTransportMsg);
            result = mockHttpsMessage;
            mockTransportMsg.getIotHubMethod();
            result = IotHubMethod.POST;
            mockTransportMsg.getUriPath();
            result = uriPath;
            httpsIotHubConnection.sendHttpsMessage(mockHttpsMessage, (HttpsMethod) any, (String) any, (Map) any);
            result = new IOException();
        }
    };
    HttpsTransportManager httpsTransportManager = Deencapsulation.newInstance(HttpsTransportManager.class, new Class[] { DeviceClientConfig.class }, mockConfig);
    Deencapsulation.invoke(httpsTransportManager, "open");
    // act
    Deencapsulation.invoke(httpsTransportManager, "send", new Class[] { IotHubTransportMessage.class, Map.class }, mockTransportMsg, new HashMap<String, String>());
}
Also used : HttpsTransportManager(com.microsoft.azure.sdk.iot.device.transport.https.HttpsTransportManager) HttpsIotHubConnection(com.microsoft.azure.sdk.iot.device.transport.https.HttpsIotHubConnection) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

HttpsTransportManager (com.microsoft.azure.sdk.iot.device.transport.https.HttpsTransportManager)32 Test (org.junit.Test)31 HttpsIotHubConnection (com.microsoft.azure.sdk.iot.device.transport.https.HttpsIotHubConnection)12 FileUpload (com.microsoft.azure.sdk.iot.device.fileupload.FileUpload)5 IOException (java.io.IOException)5 MethodResult (com.microsoft.azure.sdk.iot.device.edge.MethodResult)4 IotHubTransportMessage (com.microsoft.azure.sdk.iot.device.transport.IotHubTransportMessage)3 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)3 HashMap (java.util.HashMap)2 IotHubEventCallback (com.microsoft.azure.sdk.iot.device.IotHubEventCallback)1 ModuleClientException (com.microsoft.azure.sdk.iot.device.exceptions.ModuleClientException)1 TransportException (com.microsoft.azure.sdk.iot.device.exceptions.TransportException)1 FileUploadInProgress (com.microsoft.azure.sdk.iot.device.fileupload.FileUploadInProgress)1 FileUploadStatusCallBack (com.microsoft.azure.sdk.iot.device.fileupload.FileUploadStatusCallBack)1 URISyntaxException (java.net.URISyntaxException)1