use of com.aws.greengrass.config.Topics in project aws-greengrass-nucleus by aws-greengrass.
the class DeploymentServiceTest method GIVEN_deployment_job_WHEN_deployment_process_succeeds_THEN_correctly_map_components_to_groups.
@ParameterizedTest
@EnumSource(Deployment.DeploymentType.class)
void GIVEN_deployment_job_WHEN_deployment_process_succeeds_THEN_correctly_map_components_to_groups(Deployment.DeploymentType type) throws Exception {
String expectedGroupName = EXPECTED_GROUP_NAME;
if (type.equals(Deployment.DeploymentType.LOCAL)) {
expectedGroupName = LOCAL_DEPLOYMENT_GROUP_NAME;
}
String deploymentDocument = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("TestDeploymentDocument.json"), StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n"));
deploymentQueue.offer(new Deployment(deploymentDocument, type, TEST_JOB_ID_1));
Topics allGroupTopics = Topics.of(context, GROUP_TO_ROOT_COMPONENTS_TOPICS, null);
Topics groupMembershipTopics = Topics.of(context, GROUP_MEMBERSHIP_TOPICS, null);
groupMembershipTopics.lookup(expectedGroupName);
Topics deploymentGroupTopics = Topics.of(context, expectedGroupName, allGroupTopics);
Topic pkgTopic1 = Topic.of(context, DeploymentService.GROUP_TO_ROOT_COMPONENTS_VERSION_KEY, "1.0.0");
Topic groupTopic1 = Topic.of(context, DeploymentService.GROUP_TO_ROOT_COMPONENTS_GROUP_CONFIG_ARN, "arn:aws:greengrass:testRegion:12345:configuration:testGroup:12");
Topic groupNameTopic1 = Topic.of(context, DeploymentService.GROUP_TO_ROOT_COMPONENTS_GROUP_NAME, expectedGroupName);
Map<CaseInsensitiveString, Node> pkgDetails = new HashMap<>();
pkgDetails.put(new CaseInsensitiveString(DeploymentService.GROUP_TO_ROOT_COMPONENTS_VERSION_KEY), pkgTopic1);
pkgDetails.put(new CaseInsensitiveString(DeploymentService.GROUP_TO_ROOT_COMPONENTS_GROUP_CONFIG_ARN), groupTopic1);
pkgDetails.put(new CaseInsensitiveString(DeploymentService.GROUP_TO_ROOT_COMPONENTS_GROUP_NAME), groupNameTopic1);
Topics pkgTopics = Topics.of(context, EXPECTED_ROOT_PACKAGE_NAME, deploymentGroupTopics);
pkgTopics.children.putAll(pkgDetails);
deploymentGroupTopics.children.put(new CaseInsensitiveString(EXPECTED_ROOT_PACKAGE_NAME), pkgTopics);
allGroupTopics.children.put(new CaseInsensitiveString(expectedGroupName), deploymentGroupTopics);
when(config.lookupTopics(GROUP_MEMBERSHIP_TOPICS)).thenReturn(groupMembershipTopics);
when(config.lookupTopics(GROUP_TO_ROOT_COMPONENTS_TOPICS)).thenReturn(allGroupTopics);
lenient().when(config.lookupTopics(GROUP_TO_ROOT_COMPONENTS_TOPICS, expectedGroupName)).thenReturn(deploymentGroupTopics);
when(config.lookupTopics(COMPONENTS_TO_GROUPS_TOPICS)).thenReturn(mockComponentsToGroupPackages);
when(mockKernel.locate(any())).thenReturn(mockGreengrassService);
when(mockGreengrassService.getName()).thenReturn(EXPECTED_ROOT_PACKAGE_NAME);
mockFuture.complete(new DeploymentResult(DeploymentStatus.SUCCESSFUL, null));
when(mockExecutorService.submit(any(DefaultDeploymentTask.class))).thenReturn(mockFuture);
doNothing().when(deploymentStatusKeeper).persistAndPublishDeploymentStatus(eq(TEST_JOB_ID_1), eq(type), eq(JobStatus.IN_PROGRESS.toString()), any());
startDeploymentServiceInAnotherThread();
verify(deploymentStatusKeeper, timeout(1000)).persistAndPublishDeploymentStatus(eq(TEST_JOB_ID_1), eq(type), eq(JobStatus.IN_PROGRESS.toString()), any());
verify(deploymentStatusKeeper, timeout(10000)).persistAndPublishDeploymentStatus(eq(TEST_JOB_ID_1), eq(type), eq(JobStatus.SUCCEEDED.toString()), any());
verify(mockExecutorService, timeout(1000)).submit(any(DefaultDeploymentTask.class));
verify(deploymentStatusKeeper, timeout(2000)).persistAndPublishDeploymentStatus(eq(TEST_JOB_ID_1), eq(type), eq(JobStatus.SUCCEEDED.toString()), any());
ArgumentCaptor<Map<String, Object>> mapCaptor = ArgumentCaptor.forClass(Map.class);
verify(mockComponentsToGroupPackages).replaceAndWait(mapCaptor.capture());
Map<String, Object> groupToRootPackages = mapCaptor.getValue();
assertThat(groupToRootPackages, is(IsNull.notNullValue()));
assertThat(groupToRootPackages.entrySet(), IsNot.not(IsEmptyCollection.empty()));
assertThat(groupToRootPackages, hasKey(EXPECTED_ROOT_PACKAGE_NAME));
assertThat((Map<String, Boolean>) groupToRootPackages.get(EXPECTED_ROOT_PACKAGE_NAME), hasKey("arn:aws:greengrass:testRegion:12345:configuration:testGroup:12"));
}
use of com.aws.greengrass.config.Topics in project aws-greengrass-nucleus by aws-greengrass.
the class DeviceConfigurationTest method beforeEach.
@BeforeEach
void beforeEach() {
NucleusPaths nucleusPaths = mock(NucleusPaths.class);
Topics rootConfigTopics = mock(Topics.class);
when(rootConfigTopics.findOrDefault(any(), anyString(), anyString(), anyString())).thenReturn(new ArrayList<>());
lenient().when(configuration.lookup(anyString(), anyString(), anyString())).thenReturn(mock(Topic.class));
when(configuration.lookup(anyString(), anyString(), anyString(), anyString())).thenReturn(mockTopic);
lenient().when(configuration.getRoot()).thenReturn(rootConfigTopics);
when(mockKernel.getConfig()).thenReturn(configuration);
lenient().when(mockKernel.getNucleusPaths()).thenReturn(nucleusPaths);
Topics topics = Topics.of(mock(Context.class), SERVICES_NAMESPACE_TOPIC, mock(Topics.class));
when(mockTopics.subscribe(any())).thenReturn(mockTopics);
when(configuration.lookupTopics(anyString(), anyString(), anyString())).thenReturn(mockTopics);
when(configuration.lookupTopics(anyString(), anyString(), anyString(), anyString())).thenReturn(mockTopics);
when(configuration.lookupTopics(anyString())).thenReturn(topics);
lenient().when(configuration.lookupTopics(anyString())).thenReturn(topics);
}
use of com.aws.greengrass.config.Topics in project aws-greengrass-nucleus by aws-greengrass.
the class DeviceConfigurationTest method GIVEN_no_existing_config_WHEN_init_device_config_THEN_use_default_nucleus_config.
@Test
void GIVEN_no_existing_config_WHEN_init_device_config_THEN_use_default_nucleus_config() throws Exception {
try (Context context = new Context()) {
Topics servicesConfig = Topics.of(context, SERVICES_NAMESPACE_TOPIC, null);
lenient().when(configuration.lookupTopics(SERVICES_NAMESPACE_TOPIC)).thenReturn(servicesConfig);
deviceConfiguration = new DeviceConfiguration(mockKernel);
when(mockKernel.findServiceTopic(DEFAULT_NUCLEUS_COMPONENT_NAME)).thenReturn(servicesConfig.findTopics(DEFAULT_NUCLEUS_COMPONENT_NAME));
// Expect fallback version in the absence of version information from build files
assertEquals("0.0.0", deviceConfiguration.getNucleusVersion());
}
}
use of com.aws.greengrass.config.Topics in project aws-greengrass-nucleus by aws-greengrass.
the class DeviceConfigurationTest method GIVEN_existing_config_including_nucleus_version_WHEN_init_device_config_THEN_use_nucleus_version_from_config.
@Test
void GIVEN_existing_config_including_nucleus_version_WHEN_init_device_config_THEN_use_nucleus_version_from_config() throws Exception {
try (Context context = new Context()) {
Topics servicesConfig = Topics.of(context, SERVICES_NAMESPACE_TOPIC, null);
Topics nucleusConfig = servicesConfig.lookupTopics(DEFAULT_NUCLEUS_COMPONENT_NAME);
Topic componentTypeConfig = nucleusConfig.lookup(SERVICE_TYPE_TOPIC_KEY).withValue(ComponentType.NUCLEUS.name());
Topic nucleusVersionConfig = nucleusConfig.lookup(VERSION_CONFIG_KEY).withValue("99.99.99");
lenient().when(configuration.lookupTopics(SERVICES_NAMESPACE_TOPIC)).thenReturn(servicesConfig);
lenient().when(configuration.lookup(SERVICES_NAMESPACE_TOPIC, DEFAULT_NUCLEUS_COMPONENT_NAME, VERSION_CONFIG_KEY)).thenReturn(nucleusVersionConfig);
lenient().when(configuration.lookup(SERVICES_NAMESPACE_TOPIC, DEFAULT_NUCLEUS_COMPONENT_NAME, SERVICE_TYPE_TOPIC_KEY)).thenReturn(componentTypeConfig);
when(mockKernel.findServiceTopic(DEFAULT_NUCLEUS_COMPONENT_NAME)).thenReturn(nucleusConfig);
deviceConfiguration = new DeviceConfiguration(mockKernel);
// Confirm version config didn't get overwritten with default
assertEquals("99.99.99", Coerce.toString(nucleusVersionConfig));
assertEquals("99.99.99", deviceConfiguration.getNucleusVersion());
}
}
use of com.aws.greengrass.config.Topics in project aws-greengrass-nucleus by aws-greengrass.
the class DeviceConfigurationTest method GIVEN_recipe_WHEN_initializeNucleusLifecycleConfig_THEN_init_lifecycle_and_dependencies.
@Test
void GIVEN_recipe_WHEN_initializeNucleusLifecycleConfig_THEN_init_lifecycle_and_dependencies(@Mock KernelConfigResolver kernelConfigResolver, @Mock Context context) throws Exception {
deviceConfiguration = new DeviceConfiguration(mockKernel);
List mockDependencies = Arrays.asList("A", "B", "C");
doReturn(mockDependencies).when(kernelConfigResolver).generateServiceDependencies(anyMap());
Object interpolatedLifecycle = new HashMap<String, String>() {
{
put("lifecycle", "step");
}
};
doReturn(interpolatedLifecycle).when(kernelConfigResolver).interpolate(anyMap(), any(), anySet(), anyMap());
doReturn(kernelConfigResolver).when(context).get(KernelConfigResolver.class);
doReturn(context).when(mockKernel).getContext();
String nucleusComponentName = "test.component.name";
ComponentRecipe componentRecipe = ComponentRecipe.builder().lifecycle((Map<String, Object>) interpolatedLifecycle).build();
Topics lifecycleTopics = spy(Topics.of(context, SERVICE_LIFECYCLE_NAMESPACE_TOPIC, null));
when(configuration.lookupTopics(eq(DEFAULT_VALUE_TIMESTAMP), eq(SERVICES_NAMESPACE_TOPIC), eq(nucleusComponentName), eq(SERVICE_LIFECYCLE_NAMESPACE_TOPIC))).thenReturn(lifecycleTopics);
Topic dependencyTopic = Topic.of(context, SERVICE_DEPENDENCIES_NAMESPACE_TOPIC, null);
when(configuration.lookup(eq(DEFAULT_VALUE_TIMESTAMP), eq(SERVICES_NAMESPACE_TOPIC), eq(nucleusComponentName), eq(SERVICE_DEPENDENCIES_NAMESPACE_TOPIC))).thenReturn(dependencyTopic);
deviceConfiguration.initializeNucleusLifecycleConfig(nucleusComponentName, componentRecipe);
assertEquals(mockDependencies, dependencyTopic.toPOJO());
verify(lifecycleTopics).replaceAndWait((Map<String, Object>) interpolatedLifecycle);
}
Aggregations