package com.commercetools.sdk.contrib;

import io.sphere.sdk.client.*;
import org.junit.rules.ExternalResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.time.Duration;
import java.util.Properties;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.TimeUnit;

public final class SphereClientRule extends ExternalResource implements BlockingSphereClient {
    private static final Logger LOGGER = LoggerFactory.getLogger(SphereClientRule.class);
    private BlockingSphereClient client;

    @Override
    public void close() {
        LOGGER.warn("it is not recommended to close the client directly in " + getClass().getName());
        client.close();
    }

    @Override
    public <T> CompletionStage<T> execute(final SphereRequest<T> sphereRequest) {
        return client.execute(sphereRequest);
    }

    @Override
    public <T> T executeBlocking(final SphereRequest<T> sphereRequest) {
        return client.executeBlocking(sphereRequest);
    }

    @Override
    public <T> T executeBlocking(final SphereRequest<T> sphereRequest, final long l, final TimeUnit timeUnit) {
        return client.executeBlocking(sphereRequest, l, timeUnit);
    }

    @Override
    public <T> T executeBlocking(final SphereRequest<T> sphereRequest, final Duration duration) {
        return client.executeBlocking(sphereRequest, duration);
    }

    @Override
    protected void after() {
        client.close();
    }

    @Override
    protected void before() throws Throwable {
        File file = new File("integrationtest.properties");
        final File parentFile = new File(".").getParentFile();
        if (!file.exists()
                && parentFile !=null && parentFile.exists()
                && parentFile.getParentFile() != null && parentFile.getParentFile().exists()
                && parentFile.getParentFile().getParentFile() != null && parentFile.getParentFile().getParentFile().exists()
                && parentFile.getParentFile().getParentFile().getParentFile() != null && parentFile.getParentFile().getParentFile().getParentFile().exists()
                && new File(parentFile.getParentFile().getParentFile().getParentFile(), "integrationtest.properties").exists()) {
            file = new File(parentFile.getParentFile().getParentFile().getParentFile(), "integrationtest.properties");
        }
        try (final FileInputStream fileInputStream = new FileInputStream(file)) {
            final Properties properties = new Properties();
            properties.load(fileInputStream);
            final SphereClientConfig config =  SphereClientConfig.ofProperties(properties, "");
            final SphereClient underlying = SphereClientFactory.of().createClient(config);
            client = BlockingSphereClient.of(underlying, 20, TimeUnit.SECONDS);
        }
    }
}

@@ -1583,843 +1583,78 @@
     
-File file = new File(%22integrationtest.properties%22);%0A        final File parentFile = new File(%22.%22).getParentFile();%0A        if (!file.exists()%0A                && parentFile !=null && parentFile.exists()%0A                && parentFile.getParentFile() != null && parentFile.getParentFile().exists()%0A                && parentFile.getParentFile().getParentFile() != null && parentFile.getParentFile().getParentFile().exists()%0A                && parentFile.getParentFile().getParentFile().getParentFile() != null && parentFile.getParentFile().getParentFile().getParentFile().exists()%0A                && new File(parentFile.getParentFile().getParentFile().getParentFile(), %22integrationtest.properties%22).exists()) %7B%0A            file = new File(parentFile.getParentFile().getParentFile().getParentFile(), %22integrationtest.properties%22);%0A        %7D
+final File file = findFile(new File(%22integrationtest.properties%22), 4);
 %0A   
@@ -2025,16 +2025,16 @@
 onfig);%0A
-
         
@@ -2120,11 +2120,276 @@
  %7D%0A    %7D
+%0A%0A    private static File findFile(final File initial, final int ttl) %7B%0A        if (ttl %3C= 0 %7C%7C !initial.exists()) %7B%0A            return initial;%0A        %7D else %7B%0A            return findFile(new File(initial.getParent(), initial.getName()), ttl - 1);%0A        %7D%0A    %7D
 %0A%7D%0A

improve credentials logic for release