Search in sources :

Example 1 with ProvisioningDeviceClient

use of com.microsoft.azure.sdk.iot.provisioning.device.ProvisioningDeviceClient in project azure-iot-sdk-java by Azure.

the class ProvisioningDeviceClientTest method registerSucceeds.

// SRS_ProvisioningDeviceClient_25_009: [ This method shall set the config with the callback. ]
// SRS_ProvisioningDeviceClient_25_010: [ This method shall start the executor with the ProvisioningTask. ]
@Test
public void registerSucceeds() throws ProvisioningDeviceClientException {
    // arrange
    ProvisioningDeviceClient testProvisioningDeviceClient = ProvisioningDeviceClient.create(END_POINT, SCOPE_ID, TEST_PROTOCOL, mockedSecurityProvider);
    // act
    testProvisioningDeviceClient.registerDevice(mockedRegistrationCB, null);
    // assert
    new Verifications() {

        {
            mockedProvisioningDeviceClientConfig.setRegistrationCallback((ProvisioningDeviceClientRegistrationCallback) any, any);
            times = 1;
            mockedExecutorService.submit((ProvisioningTask) any);
            times = 1;
        }
    };
}
Also used : ProvisioningDeviceClient(com.microsoft.azure.sdk.iot.provisioning.device.ProvisioningDeviceClient) Verifications(mockit.Verifications) Test(org.junit.Test)

Example 2 with ProvisioningDeviceClient

use of com.microsoft.azure.sdk.iot.provisioning.device.ProvisioningDeviceClient in project azure-iot-sdk-java by Azure.

the class ProvisioningDeviceClientTest method constructorSucceeds.

// SRS_ProvisioningDeviceClient_25_005: [ The constructor shall create provisioningDeviceClientConfig and set all the provided values to it.. ]
// SRS_ProvisioningDeviceClient_25_006: [ The constructor shall create provisioningDeviceClientContract with the given config. ]
// SRS_ProvisioningDeviceClient_25_007: [ The constructor shall create an executor service with fixed thread pool of size 1. ]
@Test
public void constructorSucceeds() throws ProvisioningDeviceClientException {
    // arrange
    // act
    ProvisioningDeviceClient testProvisioningDeviceClient = ProvisioningDeviceClient.create(END_POINT, SCOPE_ID, TEST_PROTOCOL, mockedSecurityProvider);
    // assert
    new Verifications() {

        {
            mockedProvisioningDeviceClientConfig.setProvisioningServiceGlobalEndpoint(END_POINT);
            times = 1;
            mockedProvisioningDeviceClientConfig.setIdScope(SCOPE_ID);
            times = 1;
            mockedProvisioningDeviceClientConfig.setSecurityProvider(mockedSecurityProvider);
            times = 1;
            mockedProvisioningDeviceClientConfig.setProtocol(TEST_PROTOCOL);
            times = 1;
            ProvisioningDeviceClientContract.createProvisioningContract((ProvisioningDeviceClientConfig) any);
            times = 1;
            Executors.newFixedThreadPool(1);
            times = 1;
        }
    };
}
Also used : ProvisioningDeviceClient(com.microsoft.azure.sdk.iot.provisioning.device.ProvisioningDeviceClient) Verifications(mockit.Verifications) Test(org.junit.Test)

Example 3 with ProvisioningDeviceClient

use of com.microsoft.azure.sdk.iot.provisioning.device.ProvisioningDeviceClient in project azure-iot-sdk-java by Azure.

the class ProvisioningDeviceClientTest method registerThrowsOnNullCB.

// SRS_ProvisioningDeviceClient_25_008: [ This method shall throw IllegalArgumentException if provisioningDeviceClientRegistrationCallback is null. ]
@Test(expected = IllegalArgumentException.class)
public void registerThrowsOnNullCB() throws ProvisioningDeviceClientException {
    // arrange
    ProvisioningDeviceClient testProvisioningDeviceClient = ProvisioningDeviceClient.create(END_POINT, SCOPE_ID, TEST_PROTOCOL, mockedSecurityProvider);
    // act
    testProvisioningDeviceClient.registerDevice(null, null);
}
Also used : ProvisioningDeviceClient(com.microsoft.azure.sdk.iot.provisioning.device.ProvisioningDeviceClient) Test(org.junit.Test)

Example 4 with ProvisioningDeviceClient

use of com.microsoft.azure.sdk.iot.provisioning.device.ProvisioningDeviceClient in project azure-iot-sdk-java by Azure.

the class ProvisioningDeviceClientTest method closeNowSucceeds.

// SRS_ProvisioningDeviceClient_25_011: [ This method shall check if executor is terminated and if not shall shutdown the executor. ]
@Test
public void closeNowSucceeds() throws ProvisioningDeviceClientException {
    // arrange
    new NonStrictExpectations() {

        {
            mockedExecutorService.isTerminated();
            result = false;
        }
    };
    ProvisioningDeviceClient testProvisioningDeviceClient = ProvisioningDeviceClient.create(END_POINT, SCOPE_ID, TEST_PROTOCOL, mockedSecurityProvider);
    // act
    testProvisioningDeviceClient.closeNow();
    // assert
    new Verifications() {

        {
            mockedExecutorService.shutdownNow();
            times = 1;
        }
    };
}
Also used : ProvisioningDeviceClient(com.microsoft.azure.sdk.iot.provisioning.device.ProvisioningDeviceClient) Verifications(mockit.Verifications) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 5 with ProvisioningDeviceClient

use of com.microsoft.azure.sdk.iot.provisioning.device.ProvisioningDeviceClient in project azure-iot-sdk-java by Azure.

the class ProvisioningTPMTests method provisioningTpmFlow.

@Test
public void provisioningTpmFlow() throws SecurityProviderException, ProvisioningServiceClientException, ProvisioningDeviceClientException, InterruptedException {
    ProvisioningServiceClient provisioningServiceClient = ProvisioningServiceClient.createFromConnectionString(provisioningServiceConnectionString);
    String registrationId = UUID.randomUUID().toString();
    String provisionedDeviceId = "Some-Provisioned-Device-" + TPM + "-" + UUID.randomUUID().toString();
    SecurityProvider securityProvider = new SecurityProviderTPMEmulator(registrationId);
    Attestation attestation = new TpmAttestation(new String(encodeBase64(((SecurityProviderTpm) securityProvider).getEndorsementKey())));
    IndividualEnrollment individualEnrollment = new IndividualEnrollment(registrationId, attestation);
    individualEnrollment.setDeviceIdFinal(provisionedDeviceId);
    provisioningServiceClient.createOrUpdateIndividualEnrollment(individualEnrollment);
    ProvisioningDeviceClient provisioningDeviceClient = ProvisioningDeviceClient.create(provisioningServiceGlobalEndpoint, provisioningServiceIdScope, ProvisioningDeviceClientTransportProtocol.AMQPS, securityProvider);
    AtomicBoolean registrationCompleted = new AtomicBoolean(false);
    AtomicBoolean registrationCompletedSuccessfully = new AtomicBoolean(false);
    provisioningDeviceClient.registerDevice((provisioningDeviceClientRegistrationResult, e, context) -> {
        log.debug("Provisioning registration callback fired with result {}", provisioningDeviceClientRegistrationResult.getProvisioningDeviceClientStatus());
        if (e != null) {
            log.error("Provisioning registration callback fired with exception {}", e);
        }
        ProvisioningDeviceClientStatus status = provisioningDeviceClientRegistrationResult.getProvisioningDeviceClientStatus();
        if (status == ProvisioningDeviceClientStatus.PROVISIONING_DEVICE_STATUS_ASSIGNED) {
            registrationCompletedSuccessfully.set(true);
        }
        registrationCompleted.set(true);
    }, null);
    long startTime = System.currentTimeMillis();
    while (!registrationCompleted.get()) {
        Thread.sleep(200);
        if (System.currentTimeMillis() - startTime > REGISTRATION_TIMEOUT_MILLISECONDS) {
            fail("Timed out waiting for device registration to complete.");
        }
    }
    assertTrue("Registration completed, but not successfully", registrationCompletedSuccessfully.get());
    provisioningDeviceClient.closeNow();
}
Also used : IndividualEnrollment(com.microsoft.azure.sdk.iot.provisioning.service.configs.IndividualEnrollment) ProvisioningDeviceClient(com.microsoft.azure.sdk.iot.provisioning.device.ProvisioningDeviceClient) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SecurityProviderTPMEmulator(com.microsoft.azure.sdk.iot.provisioning.security.hsm.SecurityProviderTPMEmulator) ProvisioningDeviceClientStatus(com.microsoft.azure.sdk.iot.provisioning.device.ProvisioningDeviceClientStatus) TpmAttestation(com.microsoft.azure.sdk.iot.provisioning.service.configs.TpmAttestation) SecurityProvider(com.microsoft.azure.sdk.iot.provisioning.security.SecurityProvider) TpmAttestation(com.microsoft.azure.sdk.iot.provisioning.service.configs.TpmAttestation) Attestation(com.microsoft.azure.sdk.iot.provisioning.service.configs.Attestation) ProvisioningServiceClient(com.microsoft.azure.sdk.iot.provisioning.service.ProvisioningServiceClient) Test(org.junit.Test)

Aggregations

ProvisioningDeviceClient (com.microsoft.azure.sdk.iot.provisioning.device.ProvisioningDeviceClient)6 Test (org.junit.Test)6 Verifications (mockit.Verifications)4 NonStrictExpectations (mockit.NonStrictExpectations)2 ProvisioningDeviceClientStatus (com.microsoft.azure.sdk.iot.provisioning.device.ProvisioningDeviceClientStatus)1 SecurityProvider (com.microsoft.azure.sdk.iot.provisioning.security.SecurityProvider)1 SecurityProviderTPMEmulator (com.microsoft.azure.sdk.iot.provisioning.security.hsm.SecurityProviderTPMEmulator)1 ProvisioningServiceClient (com.microsoft.azure.sdk.iot.provisioning.service.ProvisioningServiceClient)1 Attestation (com.microsoft.azure.sdk.iot.provisioning.service.configs.Attestation)1 IndividualEnrollment (com.microsoft.azure.sdk.iot.provisioning.service.configs.IndividualEnrollment)1 TpmAttestation (com.microsoft.azure.sdk.iot.provisioning.service.configs.TpmAttestation)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1