use of software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionResponse in project aws-greengrass-nucleus by aws-greengrass.
the class IotJobsClientWrapper method SubscribeToDescribeJobExecutionAccepted.
@Override
public CompletableFuture<Integer> SubscribeToDescribeJobExecutionAccepted(DescribeJobExecutionSubscriptionRequest request, QualityOfService qos, Consumer<DescribeJobExecutionResponse> handler, Consumer<Exception> exceptionHandler) {
if (request.jobId == null || request.thingName == null) {
CompletableFuture result = new CompletableFuture();
result.completeExceptionally(new MqttException("DescribeJobExecutionSubscriptionRequest must have a non-null jobId and a non-null thingName"));
return result;
}
String topic = String.format(JOB_DESCRIBE_ACCEPTED_TOPIC, request.thingName, request.jobId);
Consumer<MqttMessage> messageHandler = describeJobCbs.computeIfAbsent(new Pair<>(handler, exceptionHandler), (k) -> (message) -> {
try {
String payload = new String(message.getPayload(), StandardCharsets.UTF_8);
DescribeJobExecutionResponse response = this.gson.fromJson(payload, DescribeJobExecutionResponse.class);
handler.accept(response);
} catch (Exception e) {
if (exceptionHandler != null) {
exceptionHandler.accept(e);
}
}
});
return this.connection.subscribe(topic, qos, messageHandler);
}
use of software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionResponse in project aws-greengrass-nucleus by aws-greengrass.
the class IotJobsHelperTest method GIVEN_ongoing_job_deployment_with_queued_job_in_cloud_WHEN_cancel_notification_THEN_cancel_current_deployment.
@Test
void GIVEN_ongoing_job_deployment_with_queued_job_in_cloud_WHEN_cancel_notification_THEN_cancel_current_deployment() throws Exception {
iotJobsHelper.postInject();
String TEST_JOB_ID = "jobToBeCancelled";
iotJobsHelper.setDeploymentQueue(mockDeploymentQueue);
when(mockDeploymentQueue.isEmpty()).thenReturn(true);
CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.completedFuture(1);
when(mockIotJobsClientWrapper.SubscribeToJobExecutionsChangedEvents(any(), eq(QualityOfService.AT_LEAST_ONCE), any())).thenReturn(integerCompletableFuture);
when(mockIotJobsClientWrapper.SubscribeToDescribeJobExecutionAccepted(any(), eq(QualityOfService.AT_LEAST_ONCE), any())).thenReturn(integerCompletableFuture);
when(mockIotJobsClientWrapper.SubscribeToDescribeJobExecutionRejected(any(), eq(QualityOfService.AT_LEAST_ONCE), any())).thenReturn(integerCompletableFuture);
DeploymentTaskMetadata mockCurrentDeploymentTaskMetadata = mock(DeploymentTaskMetadata.class);
when(mockCurrentDeploymentTaskMetadata.getDeploymentType()).thenReturn(IOT_JOBS);
when(mockCurrentDeploymentTaskMetadata.isCancellable()).thenReturn(true);
when(mockDeploymentService.getCurrentDeploymentTaskMetadata()).thenReturn(mockCurrentDeploymentTaskMetadata);
iotJobsHelper.subscribeToJobsTopics();
verify(mockIotJobsClientWrapper, times(2)).SubscribeToDescribeJobExecutionAccepted(any(), eq(QualityOfService.AT_LEAST_ONCE), describeJobResponseCaptor.capture());
JobExecutionData jobExecutionData = new JobExecutionData();
jobExecutionData.jobId = TEST_JOB_ID;
jobExecutionData.status = JobStatus.QUEUED;
jobExecutionData.queuedAt = new Timestamp(new Date());
HashMap<String, Object> sampleJobDocument = new HashMap<>();
sampleJobDocument.put("DeploymentId", TEST_JOB_ID);
jobExecutionData.jobDocument = sampleJobDocument;
DescribeJobExecutionResponse describeJobExecutionResponse = new DescribeJobExecutionResponse();
describeJobExecutionResponse.execution = jobExecutionData;
describeJobResponseCaptor.getValue().accept(describeJobExecutionResponse);
ArgumentCaptor<Deployment> deploymentArgumentCaptor = ArgumentCaptor.forClass(Deployment.class);
verify(mockDeploymentQueue, times(2)).offer(deploymentArgumentCaptor.capture());
// First queued deployment should be for cancellation and then next one for next queued job
List<Deployment> actualDeployments = deploymentArgumentCaptor.getAllValues();
assertTrue(actualDeployments.get(0).isCancelled());
assertEquals(IOT_JOBS, actualDeployments.get(0).getDeploymentType());
assertFalse(actualDeployments.get(1).isCancelled());
assertEquals(IOT_JOBS, actualDeployments.get(1).getDeploymentType());
assertEquals(TEST_JOB_ID, actualDeployments.get(1).getId());
}
use of software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionResponse in project aws-greengrass-nucleus by aws-greengrass.
the class IotJobsHelperTest method GIVEN_connected_to_iot_WHEN_subscribe_to_jobs_topics_THEN_get_job_description.
@Test
void GIVEN_connected_to_iot_WHEN_subscribe_to_jobs_topics_THEN_get_job_description() throws Exception {
iotJobsHelper.postInject();
String TEST_JOB_ID = "jobToReceive";
iotJobsHelper.setDeploymentQueue(mockDeploymentQueue);
CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.completedFuture(1);
when(mockIotJobsClientWrapper.SubscribeToJobExecutionsChangedEvents(any(), eq(QualityOfService.AT_LEAST_ONCE), any())).thenReturn(integerCompletableFuture);
when(mockIotJobsClientWrapper.SubscribeToDescribeJobExecutionAccepted(any(), eq(QualityOfService.AT_LEAST_ONCE), any())).thenReturn(integerCompletableFuture);
when(mockIotJobsClientWrapper.SubscribeToDescribeJobExecutionRejected(any(), eq(QualityOfService.AT_LEAST_ONCE), any())).thenReturn(integerCompletableFuture);
when(mockDeploymentService.getCurrentDeploymentTaskMetadata()).thenReturn(null);
iotJobsHelper.subscribeToJobsTopics();
verify(mockIotJobsClientWrapper, times(2)).SubscribeToDescribeJobExecutionAccepted(any(), eq(QualityOfService.AT_LEAST_ONCE), describeJobResponseCaptor.capture());
verify(mockIotJobsClientWrapper, times(2)).SubscribeToDescribeJobExecutionRejected(any(), eq(QualityOfService.AT_LEAST_ONCE), rejectedErrorCaptor.capture());
JobExecutionData jobExecutionData = new JobExecutionData();
jobExecutionData.jobId = TEST_JOB_ID;
jobExecutionData.status = JobStatus.QUEUED;
jobExecutionData.queuedAt = new Timestamp(new Date());
HashMap<String, Object> sampleJobDocument = new HashMap<>();
sampleJobDocument.put("DeploymentId", TEST_JOB_ID);
jobExecutionData.jobDocument = sampleJobDocument;
DescribeJobExecutionResponse describeJobExecutionResponse = new DescribeJobExecutionResponse();
describeJobExecutionResponse.execution = jobExecutionData;
describeJobResponseCaptor.getValue().accept(describeJobExecutionResponse);
ArgumentCaptor<Deployment> deploymentArgumentCaptor = ArgumentCaptor.forClass(Deployment.class);
verify(mockDeploymentQueue).offer(deploymentArgumentCaptor.capture());
Deployment actualDeployment = deploymentArgumentCaptor.getValue();
assertEquals(TEST_JOB_ID, actualDeployment.getId());
assertEquals(IOT_JOBS, actualDeployment.getDeploymentType());
assertEquals("{\"DeploymentId\":\"jobToReceive\"}", actualDeployment.getDeploymentDocument());
}
use of software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionResponse in project aws-iot-device-sdk-java-v2 by aws.
the class IotJobsClient method SubscribeToDescribeJobExecutionAccepted.
/**
* Subscribes to the accepted topic for the DescribeJobExecution operation
*
* Once subscribed, `handler` is invoked each time a message matching
* the `topic` is received. It is possible for such messages to arrive before
* the SUBACK is received.
*
* AWS documentation: https://docs.aws.amazon.com/iot/latest/developerguide/jobs-api.html#mqtt-describejobexecution
*
* @param request Subscription request configuration
* @param qos Maximum requested QoS that server may use when sending messages to the client.
* The server may grant a lower QoS in the SUBACK
* @param handler callback function to invoke with messages received on the subscription topic
* @param exceptionHandler callback function to invoke if an exception occurred deserializing a message
*
* @return a future containing the MQTT packet id used to perform the subscribe operation
*/
public CompletableFuture<Integer> SubscribeToDescribeJobExecutionAccepted(DescribeJobExecutionSubscriptionRequest request, QualityOfService qos, Consumer<DescribeJobExecutionResponse> handler, Consumer<Exception> exceptionHandler) {
String topic = "$aws/things/{thingName}/jobs/{jobId}/get/accepted";
if (request.thingName == null) {
CompletableFuture<Integer> result = new CompletableFuture<Integer>();
result.completeExceptionally(new MqttException("DescribeJobExecutionSubscriptionRequest must have a non-null thingName"));
return result;
}
topic = topic.replace("{thingName}", request.thingName);
if (request.jobId == null) {
CompletableFuture<Integer> result = new CompletableFuture<Integer>();
result.completeExceptionally(new MqttException("DescribeJobExecutionSubscriptionRequest must have a non-null jobId"));
return result;
}
topic = topic.replace("{jobId}", request.jobId);
Consumer<MqttMessage> messageHandler = (message) -> {
try {
String payload = new String(message.getPayload(), StandardCharsets.UTF_8);
DescribeJobExecutionResponse response = gson.fromJson(payload, DescribeJobExecutionResponse.class);
handler.accept(response);
} catch (Exception e) {
if (exceptionHandler != null) {
exceptionHandler.accept(e);
}
}
};
return connection.subscribe(topic, qos, messageHandler);
}
use of software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionResponse in project aws-greengrass-nucleus by aws-greengrass.
the class IotJobsHelperTest method GIVEN_iot_job_notifications_WHEN_duplicate_or_outdated_THEN_ignore_jobs.
@Test
void GIVEN_iot_job_notifications_WHEN_duplicate_or_outdated_THEN_ignore_jobs() throws InterruptedException {
iotJobsHelper.postInject();
String TEST_JOB_ID = "duplicateJob";
iotJobsHelper.setDeploymentQueue(mockDeploymentQueue);
CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.completedFuture(1);
when(mockIotJobsClientWrapper.SubscribeToJobExecutionsChangedEvents(any(), eq(QualityOfService.AT_LEAST_ONCE), any())).thenReturn(integerCompletableFuture);
when(mockIotJobsClientWrapper.SubscribeToDescribeJobExecutionAccepted(any(), eq(QualityOfService.AT_LEAST_ONCE), any())).thenReturn(integerCompletableFuture);
when(mockIotJobsClientWrapper.SubscribeToDescribeJobExecutionRejected(any(), eq(QualityOfService.AT_LEAST_ONCE), any())).thenReturn(integerCompletableFuture);
when(mockDeploymentService.getCurrentDeploymentTaskMetadata()).thenReturn(null);
iotJobsHelper.subscribeToJobsTopics();
verify(mockIotJobsClientWrapper, times(2)).SubscribeToDescribeJobExecutionAccepted(any(), eq(QualityOfService.AT_LEAST_ONCE), describeJobResponseCaptor.capture());
verify(mockIotJobsClientWrapper, times(2)).SubscribeToDescribeJobExecutionRejected(any(), eq(QualityOfService.AT_LEAST_ONCE), rejectedErrorCaptor.capture());
// Create four mock jobs
Timestamp current = new Timestamp(new Date());
DescribeJobExecutionResponse describeJobExecutionResponse1 = new DescribeJobExecutionResponse();
describeJobExecutionResponse1.execution = getMockJobExecutionData(TEST_JOB_ID, current);
describeJobResponseCaptor.getValue().accept(describeJobExecutionResponse1);
DescribeJobExecutionResponse describeJobExecutionResponse2 = new DescribeJobExecutionResponse();
describeJobExecutionResponse2.execution = getMockJobExecutionData(TEST_JOB_ID, current);
describeJobResponseCaptor.getValue().accept(describeJobExecutionResponse2);
DescribeJobExecutionResponse describeJobExecutionResponse3 = new DescribeJobExecutionResponse();
describeJobExecutionResponse3.execution = getMockJobExecutionData("anyId1", new Timestamp(new Date(0)));
describeJobResponseCaptor.getValue().accept(describeJobExecutionResponse3);
DescribeJobExecutionResponse describeJobExecutionResponse4 = new DescribeJobExecutionResponse();
describeJobExecutionResponse4.execution = getMockJobExecutionData("anyId2", current);
describeJobResponseCaptor.getValue().accept(describeJobExecutionResponse4);
// Only two jobs should be queued
ArgumentCaptor<Deployment> deploymentArgumentCaptor = ArgumentCaptor.forClass(Deployment.class);
verify(mockDeploymentQueue, times(2)).offer(deploymentArgumentCaptor.capture());
List<Deployment> actualDeployments = deploymentArgumentCaptor.getAllValues();
assertEquals(2, actualDeployments.size());
assertEquals(TEST_JOB_ID, actualDeployments.get(0).getId());
assertEquals(IOT_JOBS, actualDeployments.get(0).getDeploymentType());
assertEquals("{\"DeploymentId\":\"duplicateJob\"}", actualDeployments.get(0).getDeploymentDocument());
assertEquals("anyId2", actualDeployments.get(1).getId());
assertEquals(IOT_JOBS, actualDeployments.get(1).getDeploymentType());
assertEquals("{\"DeploymentId\":\"anyId2\"}", actualDeployments.get(1).getDeploymentDocument());
}
Aggregations