Search in sources :

Example 16 with PublishRequest

use of com.aws.greengrass.mqttclient.PublishRequest in project aws-greengrass-nucleus by aws-greengrass.

the class FleetStatusServiceTest method GIVEN_component_removed_WHEN_deployment_finishes_THEN_MQTT_Sent_with_fss_data_with_overall_healthy_state.

@Test
void GIVEN_component_removed_WHEN_deployment_finishes_THEN_MQTT_Sent_with_fss_data_with_overall_healthy_state() throws ServiceLoadException, IOException, InterruptedException {
    // Set up all the topics
    Topics statusConfigTopics = Topics.of(context, FLEET_STATUS_CONFIG_TOPICS, null);
    statusConfigTopics.createLeafChild(FLEET_STATUS_PERIODIC_PUBLISH_INTERVAL_SEC).withValue("10000");
    Topics allComponentToGroupsTopics = Topics.of(context, GROUP_TO_ROOT_COMPONENTS_TOPICS, null);
    lenient().when(config.lookupTopics(COMPONENTS_TO_GROUPS_TOPICS)).thenReturn(allComponentToGroupsTopics);
    // Set up all the mocks
    when(mockDeploymentStatusKeeper.registerDeploymentStatusConsumer(any(), consumerArgumentCaptor.capture(), anyString())).thenReturn(true);
    when(mockGreengrassService1.getName()).thenReturn("MockService");
    when(mockGreengrassService1.getServiceConfig()).thenReturn(config);
    when(mockGreengrassService1.getState()).thenReturn(State.RUNNING);
    when(mockKernel.locate(DeploymentService.DEPLOYMENT_SERVICE_TOPICS)).thenReturn(mockDeploymentService);
    when(mockKernel.locate("MockService")).thenReturn(mockGreengrassService1);
    when(mockKernel.orderedDependencies()).thenReturn(Collections.singletonList(mockDeploymentService));
    when(mockDeploymentService.getConfig()).thenReturn(config);
    doNothing().when(context).addGlobalStateChangeListener(addGlobalStateChangeListenerArgumentCaptor.capture());
    when(mockDeviceConfiguration.getStatusConfigurationTopics()).thenReturn(statusConfigTopics);
    when(context.get(ScheduledExecutorService.class)).thenReturn(ses);
    // Create the fleet status service instance
    fleetStatusService = createFSS();
    fleetStatusService.startup();
    // Update the job status for an ongoing deployment to IN_PROGRESS.
    Map<String, Object> map = new HashMap<>();
    map.put(DEPLOYMENT_STATUS_KEY_NAME, JobStatus.IN_PROGRESS.toString());
    map.put(DEPLOYMENT_ID_KEY_NAME, "testJob");
    map.put(DEPLOYMENT_TYPE_KEY_NAME, IOT_JOBS);
    consumerArgumentCaptor.getValue().apply(map);
    // Update the state of an EG service.
    addGlobalStateChangeListenerArgumentCaptor.getValue().globalServiceStateChanged(mockGreengrassService1, State.INSTALLED, State.RUNNING);
    fleetStatusService.addServicesToPreviouslyKnownServicesList(Collections.singletonList(mockGreengrassService1), Instant.MIN);
    map.put(DEPLOYMENT_STATUS_KEY_NAME, JobStatus.SUCCEEDED.toString());
    Map<String, String> statusDetails = new HashMap<>();
    statusDetails.put(DEPLOYMENT_DETAILED_STATUS_KEY, DeploymentResult.DeploymentStatus.SUCCESSFUL.toString());
    map.put(DEPLOYMENT_STATUS_DETAILS_KEY_NAME, statusDetails);
    consumerArgumentCaptor.getValue().apply(map);
    // Verify that an MQTT message with the components' status is uploaded.
    verify(mockMqttClient, times(1)).publish(publishRequestArgumentCaptor.capture());
    PublishRequest publishRequest = publishRequestArgumentCaptor.getValue();
    assertEquals(QualityOfService.AT_LEAST_ONCE, publishRequest.getQos());
    assertEquals("$aws/things/testThing/greengrassv2/health/json", publishRequest.getTopic());
    ObjectMapper mapper = new ObjectMapper();
    FleetStatusDetails fleetStatusDetails = mapper.readValue(publishRequest.getPayload(), FleetStatusDetails.class);
    assertEquals(VERSION, fleetStatusDetails.getGgcVersion());
    assertEquals("testThing", fleetStatusDetails.getThing());
    assertEquals(OverallStatus.HEALTHY, fleetStatusDetails.getOverallStatus());
    assertEquals(JobStatus.SUCCEEDED.toString(), fleetStatusDetails.getDeploymentInformation().getStatus());
    assertEquals(DeploymentResult.DeploymentStatus.SUCCESSFUL.toString(), fleetStatusDetails.getDeploymentInformation().getStatusDetails().getDetailedStatus());
    assertNull(fleetStatusDetails.getDeploymentInformation().getStatusDetails().getFailureCause());
    fleetStatusDetails.getComponentStatusDetails().forEach(System.out::println);
    assertEquals(1, fleetStatusDetails.getComponentStatusDetails().size());
    assertEquals("MockService", fleetStatusDetails.getComponentStatusDetails().get(0).getComponentName());
    assertNull(fleetStatusDetails.getComponentStatusDetails().get(0).getStatusDetails());
    assertEquals(State.RUNNING, fleetStatusDetails.getComponentStatusDetails().get(0).getState());
    assertThat(fleetStatusDetails.getComponentStatusDetails().get(0).getFleetConfigArns(), is(IsEmptyCollection.empty()));
}
Also used : Topics(com.aws.greengrass.config.Topics) HashMap(java.util.HashMap) CaseInsensitiveString(com.aws.greengrass.config.CaseInsensitiveString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) PublishRequest(com.aws.greengrass.mqttclient.PublishRequest) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 17 with PublishRequest

use of com.aws.greengrass.mqttclient.PublishRequest in project aws-greengrass-nucleus by aws-greengrass.

the class FleetStatusServiceTest method GIVEN_component_status_changes_to_broken_WHEN_deployment_finishes_THEN_MQTT_Sent_with_fss_data_with_overall_unhealthy_state.

@Test
void GIVEN_component_status_changes_to_broken_WHEN_deployment_finishes_THEN_MQTT_Sent_with_fss_data_with_overall_unhealthy_state() throws ServiceLoadException, IOException, InterruptedException {
    // Set up all the topics
    Topics statusConfigTopics = Topics.of(context, FLEET_STATUS_CONFIG_TOPICS, null);
    statusConfigTopics.createLeafChild(FLEET_STATUS_PERIODIC_PUBLISH_INTERVAL_SEC).withValue("10000");
    Topics allComponentToGroupsTopics = Topics.of(context, GROUP_TO_ROOT_COMPONENTS_TOPICS, null);
    Topics groupsTopics = Topics.of(context, "MockService", allComponentToGroupsTopics);
    Topics groupsTopics2 = Topics.of(context, "MockService2", allComponentToGroupsTopics);
    Topic groupTopic1 = Topic.of(context, "arn:aws:greengrass:testRegion:12345:configuration:testGroup:12", true);
    groupsTopics.children.put(new CaseInsensitiveString("MockService"), groupTopic1);
    groupsTopics2.children.put(new CaseInsensitiveString("MockService2"), groupTopic1);
    allComponentToGroupsTopics.children.put(new CaseInsensitiveString("MockService"), groupsTopics);
    allComponentToGroupsTopics.children.put(new CaseInsensitiveString("MockService2"), groupsTopics2);
    lenient().when(config.lookupTopics(COMPONENTS_TO_GROUPS_TOPICS)).thenReturn(allComponentToGroupsTopics);
    // Set up all the mocks
    when(mockDeploymentStatusKeeper.registerDeploymentStatusConsumer(any(), consumerArgumentCaptor.capture(), anyString())).thenReturn(true);
    when(mockGreengrassService1.getName()).thenReturn("MockService");
    when(mockGreengrassService1.getServiceConfig()).thenReturn(config);
    when(mockGreengrassService1.getState()).thenReturn(State.BROKEN);
    when(mockKernel.locate(DeploymentService.DEPLOYMENT_SERVICE_TOPICS)).thenReturn(mockDeploymentService);
    when(mockKernel.locate("MockService")).thenReturn(mockGreengrassService1);
    when(mockKernel.orderedDependencies()).thenReturn(Collections.singleton(mockGreengrassService1));
    when(mockDeploymentService.getConfig()).thenReturn(config);
    doNothing().when(context).addGlobalStateChangeListener(addGlobalStateChangeListenerArgumentCaptor.capture());
    when(mockDeviceConfiguration.getStatusConfigurationTopics()).thenReturn(statusConfigTopics);
    when(context.get(ScheduledExecutorService.class)).thenReturn(ses);
    // Create the fleet status service instance
    fleetStatusService = createFSS();
    fleetStatusService.startup();
    // Update the job status for an ongoing deployment to IN_PROGRESS.
    Map<String, Object> map = new HashMap<>();
    map.put(DEPLOYMENT_STATUS_KEY_NAME, JobStatus.IN_PROGRESS.toString());
    map.put(DEPLOYMENT_ID_KEY_NAME, "testJob");
    map.put(DEPLOYMENT_TYPE_KEY_NAME, IOT_JOBS);
    consumerArgumentCaptor.getValue().apply(map);
    // Update the state of an EG service.
    addGlobalStateChangeListenerArgumentCaptor.getValue().globalServiceStateChanged(mockGreengrassService1, State.INSTALLED, State.BROKEN);
    // Update the job status for service broken after deployment
    String failureCauseMessage = "Service in broken state after deployment";
    map.put(DEPLOYMENT_STATUS_KEY_NAME, JobStatus.FAILED.toString());
    Map<String, String> statusDetails = new HashMap<>();
    statusDetails.put(DEPLOYMENT_DETAILED_STATUS_KEY, DeploymentResult.DeploymentStatus.FAILED_ROLLBACK_NOT_REQUESTED.toString());
    statusDetails.put(DEPLOYMENT_FAILURE_CAUSE_KEY, failureCauseMessage);
    map.put(DEPLOYMENT_STATUS_DETAILS_KEY_NAME, statusDetails);
    consumerArgumentCaptor.getValue().apply(map);
    // Verify that an MQTT message with the components' status is uploaded.
    verify(mockMqttClient, times(1)).publish(publishRequestArgumentCaptor.capture());
    PublishRequest publishRequest = publishRequestArgumentCaptor.getValue();
    assertEquals(QualityOfService.AT_LEAST_ONCE, publishRequest.getQos());
    assertEquals("$aws/things/testThing/greengrassv2/health/json", publishRequest.getTopic());
    ObjectMapper mapper = new ObjectMapper();
    FleetStatusDetails fleetStatusDetails = mapper.readValue(publishRequest.getPayload(), FleetStatusDetails.class);
    assertEquals(VERSION, fleetStatusDetails.getGgcVersion());
    assertEquals("testThing", fleetStatusDetails.getThing());
    assertEquals(OverallStatus.UNHEALTHY, fleetStatusDetails.getOverallStatus());
    assertEquals(JobStatus.FAILED.toString(), fleetStatusDetails.getDeploymentInformation().getStatus());
    assertEquals(DeploymentResult.DeploymentStatus.FAILED_ROLLBACK_NOT_REQUESTED.toString(), fleetStatusDetails.getDeploymentInformation().getStatusDetails().getDetailedStatus());
    assertEquals(failureCauseMessage, fleetStatusDetails.getDeploymentInformation().getStatusDetails().getFailureCause());
    assertEquals(1, fleetStatusDetails.getComponentStatusDetails().size());
    assertEquals("MockService", fleetStatusDetails.getComponentStatusDetails().get(0).getComponentName());
    assertNull(fleetStatusDetails.getComponentStatusDetails().get(0).getStatusDetails());
    assertEquals(State.BROKEN, fleetStatusDetails.getComponentStatusDetails().get(0).getState());
    assertEquals(Collections.singletonList("arn:aws:greengrass:testRegion:12345:configuration:testGroup:12"), fleetStatusDetails.getComponentStatusDetails().get(0).getFleetConfigArns());
}
Also used : Topics(com.aws.greengrass.config.Topics) HashMap(java.util.HashMap) CaseInsensitiveString(com.aws.greengrass.config.CaseInsensitiveString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Topic(com.aws.greengrass.config.Topic) PublishRequest(com.aws.greengrass.mqttclient.PublishRequest) CaseInsensitiveString(com.aws.greengrass.config.CaseInsensitiveString) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 18 with PublishRequest

use of com.aws.greengrass.mqttclient.PublishRequest in project aws-greengrass-nucleus by aws-greengrass.

the class FleetStatusServiceTest method GIVEN_MQTT_connection_interrupted_WHEN_connection_resumes_THEN_MQTT_Sent_with_periodic_triggered_fss_data.

@Test
void GIVEN_MQTT_connection_interrupted_WHEN_connection_resumes_THEN_MQTT_Sent_with_periodic_triggered_fss_data() throws InterruptedException, ServiceLoadException, IOException {
    // Set up all the topics
    Topics statusConfigTopics = Topics.of(context, FLEET_STATUS_CONFIG_TOPICS, null);
    statusConfigTopics.createLeafChild(FLEET_STATUS_PERIODIC_PUBLISH_INTERVAL_SEC).withValue("3");
    Topics allComponentToGroupsTopics = Topics.of(context, GROUP_TO_ROOT_COMPONENTS_TOPICS, null);
    Topics groupsTopics = Topics.of(context, "MockService", allComponentToGroupsTopics);
    Topics groupsTopics2 = Topics.of(context, "MockService2", allComponentToGroupsTopics);
    Topic groupTopic1 = Topic.of(context, "arn:aws:greengrass:testRegion:12345:configuration:testGroup:12", true);
    groupsTopics.children.put(new CaseInsensitiveString("MockService"), groupTopic1);
    groupsTopics2.children.put(new CaseInsensitiveString("MockService2"), groupTopic1);
    allComponentToGroupsTopics.children.put(new CaseInsensitiveString("MockService"), groupsTopics);
    allComponentToGroupsTopics.children.put(new CaseInsensitiveString("MockService2"), groupsTopics2);
    lenient().when(config.lookupTopics(COMPONENTS_TO_GROUPS_TOPICS)).thenReturn(allComponentToGroupsTopics);
    // Set up all the mocks
    when(mockDeploymentStatusKeeper.registerDeploymentStatusConsumer(any(), consumerArgumentCaptor.capture(), anyString())).thenReturn(true);
    when(mockGreengrassService1.getName()).thenReturn("MockService");
    when(mockGreengrassService1.getServiceConfig()).thenReturn(config);
    when(mockGreengrassService1.getState()).thenReturn(State.RUNNING);
    when(mockKernel.locate(DeploymentService.DEPLOYMENT_SERVICE_TOPICS)).thenReturn(mockDeploymentService);
    when(mockKernel.locate("MockService")).thenReturn(mockGreengrassService1);
    when(mockKernel.orderedDependencies()).thenReturn(Collections.singleton(mockGreengrassService1));
    when(mockDeploymentService.getConfig()).thenReturn(config);
    doNothing().when(context).addGlobalStateChangeListener(addGlobalStateChangeListenerArgumentCaptor.capture());
    when(mockDeviceConfiguration.getStatusConfigurationTopics()).thenReturn(statusConfigTopics);
    when(context.get(ScheduledExecutorService.class)).thenReturn(ses);
    doNothing().when(mockMqttClient).addToCallbackEvents(mqttClientConnectionEventsArgumentCaptor.capture());
    // Create the fleet status service instance
    fleetStatusService = createFSS(3);
    fleetStatusService.startup();
    mqttClientConnectionEventsArgumentCaptor.getValue().onConnectionInterrupted(500);
    TimeUnit.SECONDS.sleep(4);
    mqttClientConnectionEventsArgumentCaptor.getValue().onConnectionResumed(false);
    TimeUnit.SECONDS.sleep(1);
    // Verify that an MQTT message with the components' status is uploaded.
    verify(mockMqttClient, atLeast(1)).publish(publishRequestArgumentCaptor.capture());
    PublishRequest publishRequest = publishRequestArgumentCaptor.getValue();
    assertEquals(QualityOfService.AT_LEAST_ONCE, publishRequest.getQos());
    assertEquals("$aws/things/testThing/greengrassv2/health/json", publishRequest.getTopic());
    ObjectMapper mapper = new ObjectMapper();
    FleetStatusDetails fleetStatusDetails = mapper.readValue(publishRequest.getPayload(), FleetStatusDetails.class);
    assertEquals(VERSION, fleetStatusDetails.getGgcVersion());
    assertEquals("testThing", fleetStatusDetails.getThing());
    assertEquals(OverallStatus.HEALTHY, fleetStatusDetails.getOverallStatus());
    assertEquals(1, fleetStatusDetails.getComponentStatusDetails().size());
    assertEquals("MockService", fleetStatusDetails.getComponentStatusDetails().get(0).getComponentName());
    assertNull(fleetStatusDetails.getComponentStatusDetails().get(0).getStatusDetails());
    assertEquals(State.RUNNING, fleetStatusDetails.getComponentStatusDetails().get(0).getState());
    assertEquals(Collections.singletonList("arn:aws:greengrass:testRegion:12345:configuration:testGroup:12"), fleetStatusDetails.getComponentStatusDetails().get(0).getFleetConfigArns());
}
Also used : Topics(com.aws.greengrass.config.Topics) Topic(com.aws.greengrass.config.Topic) PublishRequest(com.aws.greengrass.mqttclient.PublishRequest) CaseInsensitiveString(com.aws.greengrass.config.CaseInsensitiveString) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Aggregations

PublishRequest (com.aws.greengrass.mqttclient.PublishRequest)18 Test (org.junit.jupiter.api.Test)15 Topics (com.aws.greengrass.config.Topics)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)9 CaseInsensitiveString (com.aws.greengrass.config.CaseInsensitiveString)8 HashMap (java.util.HashMap)8 Topic (com.aws.greengrass.config.Topic)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 FleetStatusDetails (com.aws.greengrass.status.FleetStatusDetails)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 GreengrassService (com.aws.greengrass.lifecyclemanager.GreengrassService)3 HashSet (java.util.HashSet)3 PublishToIoTCoreRequest (software.amazon.awssdk.aws.greengrass.model.PublishToIoTCoreRequest)3 DeviceConfiguration (com.aws.greengrass.deployment.DeviceConfiguration)2 Kernel (com.aws.greengrass.lifecyclemanager.Kernel)2 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)2 UnrecognizedPropertyException (com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 CompletableFuture (java.util.concurrent.CompletableFuture)2