use of software.amazon.awssdk.services.greengrassv2data.model.ResolveComponentCandidatesResponse in project aws-greengrass-nucleus by aws-greengrass.
the class ComponentServiceHelperTest method GIVEN_component_version_requirements_WHEN_resolve_component_version_THEN_send_service_request.
@Test
void GIVEN_component_version_requirements_WHEN_resolve_component_version_THEN_send_service_request() throws Exception {
Map<String, Requirement> versionRequirements = new HashMap<>();
versionRequirements.put("X", Requirement.buildNPM("^1.0"));
versionRequirements.put("Y", Requirement.buildNPM("^1.5"));
ResolvedComponentVersion componentVersion = ResolvedComponentVersion.builder().componentName(COMPONENT_A).componentVersion(v1_0_0.getValue()).recipe(SdkBytes.fromByteArray("new recipe".getBytes(Charsets.UTF_8))).build();
ResolveComponentCandidatesResponse result = ResolveComponentCandidatesResponse.builder().resolvedComponentVersions(Collections.singletonList(componentVersion)).build();
when(client.resolveComponentCandidates(any(ResolveComponentCandidatesRequest.class))).thenReturn(result);
ResolvedComponentVersion componentVersionReturn = helper.resolveComponentVersion(COMPONENT_A, v1_0_0, versionRequirements);
assertThat(componentVersionReturn, is(componentVersion));
ArgumentCaptor<ResolveComponentCandidatesRequest> requestArgumentCaptor = ArgumentCaptor.forClass(ResolveComponentCandidatesRequest.class);
verify(client).resolveComponentCandidates(requestArgumentCaptor.capture());
ResolveComponentCandidatesRequest request = requestArgumentCaptor.getValue();
assertThat(request.platform(), notNullValue());
assertThat(request.platform().attributes(), notNullValue());
Map<String, String> attributes = request.platform().attributes();
assertThat(attributes, hasKey(PlatformResolver.OS_KEY));
assertThat(attributes, hasKey(PlatformResolver.ARCHITECTURE_KEY));
assertThat(request.componentCandidates().size(), is(1));
ComponentCandidate candidate = request.componentCandidates().get(0);
assertThat(candidate.componentName(), is(COMPONENT_A));
assertThat(candidate.componentVersion(), is("1.0.0"));
assertThat(candidate.versionRequirements(), IsMapContaining.hasEntry("X", ">=1.0.0 <2.0.0"));
assertThat(candidate.versionRequirements(), IsMapContaining.hasEntry("Y", ">=1.5.0 <2.0.0"));
}
use of software.amazon.awssdk.services.greengrassv2data.model.ResolveComponentCandidatesResponse in project aws-greengrass-nucleus by aws-greengrass.
the class ComponentServiceHelper method resolveComponentVersion.
/**
* Resolve a component version with greengrass cloud service. The dependency resolution algorithm goes through the
* dependencies node by node, so one component got resolve a time.
*
* @param componentName component name to be resolve
* @param localCandidateVersion component local candidate version if available
* @param versionRequirements component dependents version requirement map
* @return resolved component version and recipe
* @throws NoAvailableComponentVersionException if no applicable version available in cloud service
*/
@SuppressWarnings("PMD.PreserveStackTrace")
ResolvedComponentVersion resolveComponentVersion(String componentName, Semver localCandidateVersion, Map<String, Requirement> versionRequirements) throws NoAvailableComponentVersionException {
ComponentPlatform platform = ComponentPlatform.builder().attributes(platformResolver.getCurrentPlatform()).build();
Map<String, String> versionRequirementsInString = versionRequirements.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().toString()));
ComponentCandidate candidate = ComponentCandidate.builder().componentName(componentName).componentVersion(localCandidateVersion == null ? null : localCandidateVersion.getValue()).versionRequirements(versionRequirementsInString).build();
ResolveComponentCandidatesRequest request = ResolveComponentCandidatesRequest.builder().platform(platform).componentCandidates(Collections.singletonList(candidate)).build();
ResolveComponentCandidatesResponse result;
try {
result = clientFactory.getGreengrassV2DataClient().resolveComponentCandidates(request);
} catch (ResourceNotFoundException e) {
logger.atDebug().kv("componentName", componentName).kv("versionRequirements", versionRequirements).log("No applicable version found in cloud registry", e);
throw new NoAvailableComponentVersionException("No cloud component version satisfies the requirements.", componentName, versionRequirements);
}
Validate.isTrue(result.resolvedComponentVersions() != null && result.resolvedComponentVersions().size() == 1, "Component service returns invalid response. It should have one resolved component version");
return result.resolvedComponentVersions().get(0);
}
Aggregations