### Question:
EnvironmentValidatorService { public ValidationResult validateParentChildRelation(Environment environment, String parentEnvironmentName) { ValidationResultBuilder resultBuilder = new ValidationResultBuilder(); resultBuilder.ifError(() -> Objects.nonNull(parentEnvironmentName) && Objects.isNull(environment.getParentEnvironment()), String.format("Active parent environment with name '%s' is not available in account '%s'.", parentEnvironmentName, environment.getAccountId())); if (Objects.nonNull(environment.getParentEnvironment())) { resultBuilder.ifError(() -> environment.getParentEnvironment().getStatus() != EnvironmentStatus.AVAILABLE, "Parent environment should be in 'AVAILABLE' status."); resultBuilder.ifError(() -> Objects.nonNull(environment.getParentEnvironment().getParentEnvironment()), "Parent environment is already a child environment."); resultBuilder.ifError(() -> !platformEnabled(enabledChildPlatforms, environment.getCloudPlatform()), String.format("'%s' platform is not supported for child environment.", environment.getCloudPlatform())); resultBuilder.ifError(() -> !platformEnabled(enabledParentPlatforms, environment.getParentEnvironment().getCloudPlatform()), String.format("'%s' platform is not supported for parent environment.", environment.getParentEnvironment().getCloudPlatform())); } return resultBuilder.build(); } EnvironmentValidatorService(NetworkCreationValidator networkCreationValidator,
            PlatformParameterService platformParameterService,
            EnvironmentResourceService environmentResourceService,
            CredentialService credentialService,
            @Value("${environment.enabledParentPlatforms}") Set<String> enabledParentPlatforms,
            @Value("${environment.enabledChildPlatforms}") Set<String> enabledChildPlatforms); ValidationResultBuilder validateNetworkCreation(Environment environment, NetworkDto network); ValidationResult validateParentChildRelation(Environment environment, String parentEnvironmentName); ValidationResult validateAwsEnvironmentRequest(EnvironmentRequest environmentRequest); ValidationResult validateSecurityAccessModification(SecurityAccessDto securityAccessDto, Environment environment); ValidationResult validateSecurityGroups(EnvironmentEditDto editDto, Environment environment); ValidationResult validateAuthenticationModification(EnvironmentEditDto editDto, Environment environment); ValidationResult validateFreeIpaCreation(FreeIpaCreationDto freeIpaCreation);  }### Answer:
@Test void shouldNotFailInCaseOfVaildParentChildEnvironment() { Environment environment = aValidEnvirontmentWithParent(); ValidationResult validationResult = underTest.validateParentChildRelation(environment, "parentEnvName"); assertFalse(validationResult.hasError()); }
@Test void shouldFailOnExistingParentEnvironmentNameButMissingParentEntity() { Environment environment = aValidEnvirontmentWithParent(); environment.setParentEnvironment(null); ValidationResult validationResult = underTest.validateParentChildRelation(environment, "parentEnvName"); assertEquals("1. Active parent environment with name 'parentEnvName' is not available in account '" + ACCOUNT + "'.", validationResult.getFormattedErrors()); }
@Test void shouldFailOnParentEnvironmentInNonActiveState() { Environment environment = aValidEnvirontmentWithParent(); environment.getParentEnvironment().setStatus(EnvironmentStatus.ARCHIVED); ValidationResult validationResult = underTest.validateParentChildRelation(environment, "parentEnvName"); assertEquals("1. Parent environment should be in 'AVAILABLE' status.", validationResult.getFormattedErrors()); }
@Test void shouldFailOnParentEnvironmentHasParentEnvironmentAsWell() { Environment environment = aValidEnvirontmentWithParent(); environment.getParentEnvironment().setParentEnvironment(new Environment()); ValidationResult validationResult = underTest.validateParentChildRelation(environment, "parentEnvName"); assertEquals("1. Parent environment is already a child environment.", validationResult.getFormattedErrors()); }
@Test void shouldFailOnParentEnvironmentCloudPlatformNotSupported() { Environment environment = aValidEnvirontmentWithParent(); environment.getParentEnvironment().setCloudPlatform(CloudPlatform.GCP.name()); ValidationResult validationResult = underTest.validateParentChildRelation(environment, "parentEnvName"); assertEquals("1. 'GCP' platform is not supported for parent environment.", validationResult.getFormattedErrors()); }
@Test void shouldFailOnChildEnvironmentCloudPlatformNotSupported() { Environment environment = aValidEnvirontmentWithParent(); environment.setCloudPlatform(CloudPlatform.GCP.name()); ValidationResult validationResult = underTest.validateParentChildRelation(environment, "parentEnvName"); assertEquals("1. 'GCP' platform is not supported for child environment.", validationResult.getFormattedErrors()); }