### Question:
AttachChildEnvironmentRequestValidator implements Validator<AttachChildEnvironmentRequest> { @Override public ValidationResult validate(AttachChildEnvironmentRequest subject) { ValidationResult.ValidationResultBuilder validationBuilder = ValidationResult.builder(); String accountId = crnService.getCurrentAccountId(); if (stackService.findAllByEnvironmentCrnAndAccountId(subject.getParentEnvironmentCrn(), accountId).isEmpty()) { validationBuilder.error(PARENT_ENVIRONMENT_SHOULD_HAVE_A_STACK); } if (!stackService.findAllByEnvironmentCrnAndAccountId(subject.getChildEnvironmentCrn(), accountId).isEmpty()) { validationBuilder.error(CHILD_ENVIRONMENT_ALREADY_HAS_A_STACK); } if (childEnvironmentService.isChildEnvironment(subject.getParentEnvironmentCrn(), accountId)) { validationBuilder.error(PARENT_ENVIRONMENT_CAN_NOT_BE_A_CHILD_ENVIRONMENT); } if (childEnvironmentService.isChildEnvironment(subject.getChildEnvironmentCrn(), accountId)) { validationBuilder.error(CHILD_ENVIRONMENT_ALREADY_HAS_A_PARENT_ENVIRONMENT); } return validationBuilder.build(); }  @Override ValidationResult validate(AttachChildEnvironmentRequest subject);  }### Answer:
@Test void validateShouldContainErrors() { when(stackService.findAllByEnvironmentCrnAndAccountId(PARENT_ENVIRONMENT_CRN, ACCOUNT_ID)).thenReturn(List.of()); when(stackService.findAllByEnvironmentCrnAndAccountId(CHILD_ENVIRONMENT_CRN, ACCOUNT_ID)).thenReturn(List.of(new Stack())); when(childEnvironmentService.isChildEnvironment(PARENT_ENVIRONMENT_CRN, ACCOUNT_ID)).thenReturn(true); when(childEnvironmentService.isChildEnvironment(CHILD_ENVIRONMENT_CRN, ACCOUNT_ID)).thenReturn(true); ValidationResult result = underTest.validate(REQUEST); assertThat(result.hasError()).isTrue(); assertThat(result.getErrors()) .contains(AttachChildEnvironmentRequestValidator.PARENT_ENVIRONMENT_SHOULD_HAVE_A_STACK) .contains(AttachChildEnvironmentRequestValidator.CHILD_ENVIRONMENT_ALREADY_HAS_A_STACK) .contains(AttachChildEnvironmentRequestValidator.PARENT_ENVIRONMENT_CAN_NOT_BE_A_CHILD_ENVIRONMENT) .contains(AttachChildEnvironmentRequestValidator.CHILD_ENVIRONMENT_ALREADY_HAS_A_PARENT_ENVIRONMENT); }
@Test void validateShouldNotContainErrors() { when(stackService.findAllByEnvironmentCrnAndAccountId(PARENT_ENVIRONMENT_CRN, ACCOUNT_ID)).thenReturn(List.of(new Stack())); when(stackService.findAllByEnvironmentCrnAndAccountId(CHILD_ENVIRONMENT_CRN, ACCOUNT_ID)).thenReturn(List.of()); when(childEnvironmentService.isChildEnvironment(PARENT_ENVIRONMENT_CRN, ACCOUNT_ID)).thenReturn(false); when(childEnvironmentService.isChildEnvironment(CHILD_ENVIRONMENT_CRN, ACCOUNT_ID)).thenReturn(false); ValidationResult result = underTest.validate(REQUEST); assertThat(result.hasError()).isFalse(); }