use of com.sequenceiq.cloudbreak.cloud.aws.resource.loadbalancer.AwsNativeLoadBalancerLaunchService.DUPLICATE_TARGET_GROUP_NAME_ERROR_CODE in project cloudbreak by hortonworks.
the class AwsNativeLoadBalancerLaunchServiceTest method testLaunchLoadBalancerResourcesWhenTargetGroupAlreadyExistsWithNameAndLoadBalancerListenerCouldNotBeCreated.
@Test
void testLaunchLoadBalancerResourcesWhenTargetGroupAlreadyExistsWithNameAndLoadBalancerListenerCouldNotBeCreated() {
CloudStack stack = getCloudStack();
when(loadBalancerCommonService.getAwsLoadBalancers(any(), any(), any())).thenReturn(List.of(getAwsLoadBalancer()));
when(resourceNameService.resourceName(eq(ResourceType.ELASTIC_LOAD_BALANCER), any(), any())).thenReturn("aLoadBalancerName");
when(persistenceRetriever.retrieveFirstByTypeAndStatusForStack(eq(ResourceType.ELASTIC_LOAD_BALANCER), eq(CommonStatus.CREATED), any())).thenReturn(Optional.empty());
LoadBalancer loadBalancer = new LoadBalancer().withLoadBalancerArn("anARN");
CreateLoadBalancerResult loadBalancerResult = new CreateLoadBalancerResult().withLoadBalancers(loadBalancer);
when(loadBalancingClient.registerLoadBalancer(any())).thenReturn(loadBalancerResult);
when(resourceNameService.resourceName(eq(ResourceType.ELASTIC_LOAD_BALANCER_TARGET_GROUP), any(), any(), any())).thenReturn("aLoadBalancerTGName");
when(persistenceRetriever.retrieveFirstByTypeAndStatusForStack(eq(ResourceType.ELASTIC_LOAD_BALANCER_TARGET_GROUP), eq(CommonStatus.CREATED), any())).thenReturn(Optional.empty());
AmazonServiceException amazonServiceException = new AmazonServiceException(DUPLICATE_TARGET_GROUP_NAME_ERROR_CODE);
amazonServiceException.setErrorCode(DUPLICATE_TARGET_GROUP_NAME_ERROR_CODE);
when(loadBalancingClient.createTargetGroup(any())).thenThrow(amazonServiceException);
TargetGroup targetGroup = new TargetGroup().withTargetGroupArn("aTargetGroupArn");
DescribeTargetGroupsResult describeTargetGroupsResult = new DescribeTargetGroupsResult().withTargetGroups(targetGroup);
when(loadBalancingClient.describeTargetGroup(any())).thenReturn(describeTargetGroupsResult);
when(persistenceRetriever.retrieveFirstByTypeAndStatusForStack(eq(ResourceType.ELASTIC_LOAD_BALANCER_LISTENER), eq(CommonStatus.CREATED), any())).thenReturn(Optional.empty());
when(loadBalancingClient.registerListener(any())).thenThrow(new AmazonElasticLoadBalancingException("something went wrong"));
Assertions.assertThrows(CloudConnectorException.class, () -> undertTest.launchLoadBalancerResources(authenticatedContext, stack, persistenceNotifier, loadBalancingClient, true));
ArgumentCaptor<CloudResource> cloudResourceArgumentCaptor = ArgumentCaptor.forClass(CloudResource.class);
verify(persistenceNotifier, times(2)).notifyAllocation(cloudResourceArgumentCaptor.capture(), any());
assertTrue(cloudResourceArgumentCaptor.getAllValues().stream().anyMatch(cloudResource -> ResourceType.ELASTIC_LOAD_BALANCER.equals(cloudResource.getType())));
assertTrue(cloudResourceArgumentCaptor.getAllValues().stream().anyMatch(cloudResource -> ResourceType.ELASTIC_LOAD_BALANCER_TARGET_GROUP.equals(cloudResource.getType())));
}
Aggregations