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 {
        throw new RuntimeException("current dir " + new File(".").getAbsolutePath());
//        try (final FileInputStream fileInputStream = new FileInputStream(new File("integrationtest.properties"))) {
//            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,88 +1583,183 @@
     
-throw new RuntimeException(%22current dir %22 + new File(%22.%22).getAbsolutePath());%0A//
+final File file = new File(new File(%22integrationtest.properties%22).getAbsolutePath()%0A                .replace(%22/target/checkout%22, %22%22)//in case it is at release time%0A        );%0A
     
@@ -1831,53 +1831,17 @@
 eam(
-new File(%22integrationtest.properties%22
+file
 ))
-)
  %7B%0A
-//
     
@@ -1896,18 +1896,16 @@
 ties();%0A
-//
         
@@ -1942,18 +1942,16 @@
 tream);%0A
-//
         
@@ -2038,18 +2038,16 @@
 s, %22%22);%0A
-//
         
@@ -2129,18 +2129,16 @@
 onfig);%0A
-//
         
@@ -2209,18 +2209,16 @@
 CONDS);%0A
-//
         

improve credentials logic for release