/*
 * Copyright 2013-2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.cloudfoundry.test;

import org.cloudfoundry.client.CloudFoundryClient;
import org.cloudfoundry.doppler.DopplerClient;
import org.cloudfoundry.operations.CloudFoundryOperations;
import org.cloudfoundry.operations.DefaultCloudFoundryOperations;
import org.cloudfoundry.reactor.ConnectionContext;
import org.cloudfoundry.reactor.DefaultConnectionContext;
import org.cloudfoundry.reactor.TokenProvider;
import org.cloudfoundry.reactor.client.ReactorCloudFoundryClient;
import org.cloudfoundry.reactor.doppler.ReactorDopplerClient;
import org.cloudfoundry.reactor.tokenprovider.PasswordGrantTokenProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;

@ComponentScan
@Configuration
@EnableAutoConfiguration
public class IntegrationTestConfiguration {

    @Bean
    String buildpack(@Value("${test.buildpack}") String buildpack) {
        return buildpack;
    }

    @Bean
    ReactorCloudFoundryClient cloudFoundryClient(ConnectionContext connectionContext, TokenProvider tokenProvider) {
        return ReactorCloudFoundryClient.builder()
            .connectionContext(connectionContext)
            .tokenProvider(tokenProvider)
            .build();
    }

    @Bean
    CloudFoundryOperations cloudFoundryOperations(CloudFoundryClient cloudFoundryClient,
                                                  DopplerClient dopplerClient,
                                                  @Value("${test.organization}") String organization,
                                                  @Value("${test.space}") String space) {

        return DefaultCloudFoundryOperations.builder()
            .cloudFoundryClient(cloudFoundryClient)
            .dopplerClient(dopplerClient)
            .organization(organization)
            .space(space)
            .build();
    }

    @Bean
    DefaultConnectionContext connectionContext(@Value("${test.host}") String host,
                                               @Value("${test.skipSslValidation:false}") Boolean skipSslValidation) {

        return DefaultConnectionContext.builder()
            .apiHost(host)
            .skipSslValidation(skipSslValidation)
            .build();
    }

    @Bean
    ReactorDopplerClient dopplerClient(ConnectionContext connectionContext, TokenProvider tokenProvider) {
        return ReactorDopplerClient.builder()
            .connectionContext(connectionContext)
            .tokenProvider(tokenProvider)
            .build();
    }

    @Bean
    PasswordGrantTokenProvider tokenProvider(@Value("${test.username}") String username,
                                             @Value("${test.password}") String password) {
        return PasswordGrantTokenProvider.builder()
            .username(username)
            .password(password)
            .build();
    }

    @Bean
    WebClient webClient() {
        return WebClient.create();
    }

}

@@ -862,32 +862,170 @@
 ndryOperations;%0A
+import org.cloudfoundry.operations.organizations.CreateOrganizationRequest;%0Aimport org.cloudfoundry.operations.spaces.CreateSpaceRequest;%0A
 import org.cloud
@@ -1662,32 +1662,89 @@
 .Configuration;%0A
+import org.springframework.context.annotation.DependsOn;%0A
 import org.sprin
@@ -1793,16 +1793,52 @@
 bClient;
+%0Aimport reactor.core.publisher.Mono;
 %0A%0A@Compo
@@ -3601,32 +3601,766 @@
 build();%0A    %7D%0A%0A
+    @Bean(initMethod = %22block%22)%0A    Mono%3CVoid%3E organization(CloudFoundryOperations cloudFoundryOperations,%0A                            @Value(%22$%7Btest.organization%7D%22) String organization) %7B%0A%0A        return cloudFoundryOperations.organizations()%0A            .create(CreateOrganizationRequest.builder()%0A                .organizationName(organization)%0A                .build());%0A    %7D%0A%0A    @Bean(initMethod = %22block%22)%0A    @DependsOn(%22organization%22)%0A    Mono%3CVoid%3E space(CloudFoundryOperations cloudFoundryOperations,%0A                     @Value(%22$%7Btest.space%7D%22) String space) %7B%0A%0A        return cloudFoundryOperations.spaces()%0A            .create(CreateSpaceRequest.builder()%0A                .name(space)%0A                .build());%0A    %7D%0A%0A
     @Bean%0A    Pa

Create Org and Space before test