### Question:
AwsLaunchTemplateImageUpdateService { public void updateImage(AuthenticatedContext authenticatedContext, CloudStack stack, CloudResource cfResource) { AwsCredentialView credentialView = new AwsCredentialView(authenticatedContext.getCloudCredential()); String regionName = authenticatedContext.getCloudContext().getLocation().getRegion().getRegionName(); AmazonCloudFormationClient cloudFormationClient = awsClient.createCloudFormationClient(credentialView, regionName); String imageName = stack.getImage().getImageName(); String cfStackName = cfResource.getName(); String cfTemplate = getCfTemplate(cfResource, cloudFormationClient); Json templateJson = new Json(cfTemplate); Map<String, String> encryptedImages = getEncryptedImagesMappedByAutoscalingGroupName(authenticatedContext, stack); stack.getGroups().forEach(group -> { String imageIdPath = String.format("Resources.%s.Properties.LaunchTemplateData.ImageId", AwsGroupView.getLaunchTemplateName(group.getName())); Object oldImageId = templateJson.getValue(imageIdPath); boolean encryptedImage = !"{\"Ref\":\"AMI\"}".equals(oldImageId.toString()); if (encryptedImage) { replaceEncryptedImageInGroup(encryptedImages, imageName, templateJson, group, imageIdPath); } }); String newCfTemplate = templateJson.getValue(); UpdateStackRequest updateStackRequest = awsStackRequestHelper.createUpdateStackRequest(authenticatedContext, stack, cfStackName, newCfTemplate); cloudFormationClient.updateStack(updateStackRequest); }  void updateImage(AuthenticatedContext authenticatedContext, CloudStack stack, CloudResource cfResource);  }### Answer:
@Test public void shouldUpdateImage() throws IOException { String cfStackName = "cf"; CloudResource cfResource = CloudResource.builder() .type(ResourceType.CLOUDFORMATION_STACK) .name(cfStackName) .build(); String template = Files.read(new File("src/test/resources/json/aws-cf-template.json")); String cfTemplateBody = JsonUtil.minify(String.format(template, "{\"Ref\":\"AMI\"}")); when(cloudFormationClient.getTemplate(any())).thenReturn(new GetTemplateResult().withTemplateBody(cfTemplateBody)); UpdateStackRequest updateStackRequest = new UpdateStackRequest(); when(awsStackRequestHelper.createUpdateStackRequest(ac, stack, cfStackName, cfTemplateBody)).thenReturn(updateStackRequest); underTest.updateImage(ac, stack, cfResource); verify(awsStackRequestHelper).createUpdateStackRequest(ac, stack, cfStackName, cfTemplateBody); verify(cloudFormationClient).updateStack(updateStackRequest); }
@Test public void shouldUpdateEncryptedImage() throws IOException { String cfStackName = "cf"; CloudResource cfResource = CloudResource.builder() .type(ResourceType.CLOUDFORMATION_STACK) .name(cfStackName) .build(); String template = Files.read(new File("src/test/resources/json/aws-cf-template.json")); String cfTemplateBody = JsonUtil.minify(String.format(template, "\"ami-old\"")); when(cloudFormationClient.getTemplate(any())).thenReturn(new GetTemplateResult().withTemplateBody(cfTemplateBody)); UpdateStackRequest updateStackRequest = new UpdateStackRequest(); ArgumentCaptor<String> cfTemplateBodyCaptor = ArgumentCaptor.forClass(String.class); when(awsStackRequestHelper.createUpdateStackRequest(eq(ac), eq(stack), eq(cfStackName), any())).thenReturn(updateStackRequest); underTest.updateImage(ac, stack, cfResource); verify(awsStackRequestHelper).createUpdateStackRequest(eq(ac), eq(stack), eq(cfStackName), cfTemplateBodyCaptor.capture()); String expectedCfTemplate = JsonUtil.minify(String.format(template, "\"" + IMAGE_NAME + "\"")); Assertions.assertThat(cfTemplateBodyCaptor.getValue()).isEqualTo(expectedCfTemplate); verify(cloudFormationClient).updateStack(updateStackRequest); }