Search in sources :

Example 1 with Timestamp

use of software.amazon.awssdk.iot.Timestamp 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());
}
Also used : JobExecutionData(software.amazon.awssdk.iot.iotjobs.model.JobExecutionData) HashMap(java.util.HashMap) DescribeJobExecutionResponse(software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionResponse) Deployment(com.aws.greengrass.deployment.model.Deployment) Timestamp(software.amazon.awssdk.iot.Timestamp) Date(java.util.Date) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DeploymentTaskMetadata(com.aws.greengrass.deployment.model.DeploymentTaskMetadata) Test(org.junit.jupiter.api.Test)

Example 2 with Timestamp

use of software.amazon.awssdk.iot.Timestamp 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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JobExecutionData(software.amazon.awssdk.iot.iotjobs.model.JobExecutionData) HashMap(java.util.HashMap) DescribeJobExecutionResponse(software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionResponse) Deployment(com.aws.greengrass.deployment.model.Deployment) Timestamp(software.amazon.awssdk.iot.Timestamp) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 3 with Timestamp

use of software.amazon.awssdk.iot.Timestamp 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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DescribeJobExecutionResponse(software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionResponse) Deployment(com.aws.greengrass.deployment.model.Deployment) Timestamp(software.amazon.awssdk.iot.Timestamp) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Aggregations

Deployment (com.aws.greengrass.deployment.model.Deployment)3 Date (java.util.Date)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Test (org.junit.jupiter.api.Test)3 Timestamp (software.amazon.awssdk.iot.Timestamp)3 DescribeJobExecutionResponse (software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionResponse)3 HashMap (java.util.HashMap)2 JobExecutionData (software.amazon.awssdk.iot.iotjobs.model.JobExecutionData)2 DeploymentTaskMetadata (com.aws.greengrass.deployment.model.DeploymentTaskMetadata)1