// Path: consumer/core/src/main/java/org/arquillian/algeron/consumer/core/AlgeronConsumerConfiguration.java
// public class AlgeronConsumerConfiguration {
// 
//     private static final String PUBLISH_CONTRACTS = "publishContracts";
//     private static final String PUBLISH_CONFIGURATION = "publishConfiguration";
// 
//     private boolean publishContracts = false;
//     private Map<String, Object> publishConfiguration = null;
// 
//     public boolean isPublishContracts() {
//         return publishContracts;
//     }
// 
//     public boolean isPublishConfigurationSet() {
//         return publishConfiguration != null;
//     }
// 
//     public Map<String, Object> getPublishConfiguration() {
//         return publishConfiguration;
//     }
// 
//     public static final AlgeronConsumerConfiguration fromMap(Map<String, String> map) {
//         AlgeronConsumerConfiguration algeronConfiguration = new AlgeronConsumerConfiguration();
// 
//         if (map.containsKey(PUBLISH_CONTRACTS)) {
//             algeronConfiguration.publishContracts = Boolean.parseBoolean(map.get(PUBLISH_CONTRACTS));
//         }
// 
//         if (map.containsKey(PUBLISH_CONFIGURATION)) {
//             algeronConfiguration.publishConfiguration = loadConfiguration(map.get(PUBLISH_CONFIGURATION));
//         }
// 
//         return algeronConfiguration;
//     }
// 
//     private final static Map<String, Object> loadConfiguration(String configurationContent) {
//         return (Map<String, Object>) new Yaml().load(configurationContent);
//     }
// }
// 
// Path: consumer/core/src/main/java/org/arquillian/algeron/consumer/core/ContractsPublisherObserver.java
// public class ContractsPublisherObserver {
// 
//     private static Logger logger = Logger.getLogger(ContractsPublisherObserver.class.getName());
//     private static final String PROVIDER = "provider";
// 
//     public void publish(@Observes AfterClass event, AlgeronConsumerConfiguration algeronConsumerConfiguration)
//         throws IOException {
//         if (algeronConsumerConfiguration.isPublishContracts()
//             && algeronConsumerConfiguration.isPublishConfigurationSet()) {
// 
//             final Map<String, Object> publishConfiguration = algeronConsumerConfiguration.getPublishConfiguration();
//             if (publishConfiguration.containsKey(PROVIDER)) {
//                 final String providerName = (String) publishConfiguration.get(PROVIDER);
// 
//                 final org.arquillian.algeron.consumer.spi.publisher.ContractsPublisher contractsPublisher =
//                     getContractPublisher(providerName);
//                 contractsPublisher.configure(publishConfiguration);
//                 contractsPublisher.publish();
//             } else {
//                 logger.log(Level.WARNING, String.format(
//                     "Publishing contracts are enabled, but configuration is not providing a %s property with provider name to be used.",
//                     PROVIDER));
//             }
//         }
//     }
// 
//     private org.arquillian.algeron.consumer.spi.publisher.ContractsPublisher getContractPublisher(String name) {
//         ServiceLoader<org.arquillian.algeron.consumer.spi.publisher.ContractsPublisher> pactPublisherServiceLoader =
//             ServiceLoader.load(org.arquillian.algeron.consumer.spi.publisher.ContractsPublisher.class);
// 
//         for (org.arquillian.algeron.consumer.spi.publisher.ContractsPublisher contractsPublisher : pactPublisherServiceLoader) {
//             if (contractsPublisher.getName().equals(name)) {
//                 return contractsPublisher;
//             }
//         }
// 
//         throw new IllegalArgumentException(String.format("No contract publisher registered with name %s.", name));
//     }
// }


import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.github.tomakehurst.wiremock.matching.ContainsPattern;
import net.jcip.annotations.NotThreadSafe;
import org.arquillian.algeron.consumer.core.AlgeronConsumerConfiguration;
import org.arquillian.algeron.consumer.core.ContractsPublisherObserver;
import org.jboss.arquillian.core.api.annotation.ApplicationScoped;
import org.jboss.arquillian.core.test.AbstractManagerTestBase;
import org.jboss.arquillian.test.spi.event.suite.AfterClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
package org.arquillian.algeron.consumer.core.publisher;



@NotThreadSafe
public class UrlContractsPublisherObserverTest extends AbstractManagerTestBase {

    @Override
    protected void addExtensions(List<Class<?>> extensions) {
        extensions.add(ContractsPublisherObserver.class);
        super.addExtensions(extensions);
    }

    @Rule
    public TemporaryFolder temporaryFolder = new TemporaryFolder();

    @Rule
    public WireMockRule service = new WireMockRule(18081);

    @Test
    public void should_move_files_to_output_directory() throws IOException {

        final File origin = temporaryFolder.newFolder("origin");

        byte[] content = "Contract File".getBytes();
        Files.copy(new ByteArrayInputStream(content), new File(origin, "pact.txt").toPath());

        String config = "provider: url\n" +
            "contractsFolder: " + origin.getAbsolutePath() + "\n" +
            "url: http://localhost:18081/pacts\n";

        final Map<String, String> params = new HashMap<>();
        params.put("publishContracts", "true");
        params.put("publishConfiguration", config);

        final AlgeronConsumerConfiguration pactConsumerConfiguration = AlgeronConsumerConfiguration.fromMap(params);
        bind(ApplicationScoped.class, AlgeronConsumerConfiguration.class, pactConsumerConfiguration);

        WireMock.stubFor(
            WireMock.post(WireMock.urlEqualTo("/pacts/pact.txt"))
                .withRequestBody(new ContainsPattern("Contract File"))
                .willReturn(WireMock.aResponse().withStatus(200)));

        fire(new AfterClass(UrlContractsPublisherObserverTest.class));

// Path: consumer/core/src/main/java/org/arquillian/algeron/consumer/core/AlgeronConsumerConfiguration.java
// public class AlgeronConsumerConfiguration {
// 
//     private static final String PUBLISH_CONTRACTS = "publishContracts";
//     private static final String PUBLISH_CONFIGURATION = "publishConfiguration";
// 
//     private boolean publishContracts = false;
//     private Map<String, Object> publishConfiguration = null;
// 
//     public boolean isPublishContracts() {
//         return publishContracts;
//     }
// 
//     public boolean isPublishConfigurationSet() {
//         return publishConfiguration != null;
//     }
// 
//     public Map<String, Object> getPublishConfiguration() {
//         return publishConfiguration;
//     }
// 
//     public static final AlgeronConsumerConfiguration fromMap(Map<String, String> map) {
//         AlgeronConsumerConfiguration algeronConfiguration = new AlgeronConsumerConfiguration();
// 
//         if (map.containsKey(PUBLISH_CONTRACTS)) {
//             algeronConfiguration.publishContracts = Boolean.parseBoolean(map.get(PUBLISH_CONTRACTS));
//         }
// 
//         if (map.containsKey(PUBLISH_CONFIGURATION)) {
//             algeronConfiguration.publishConfiguration = loadConfiguration(map.get(PUBLISH_CONFIGURATION));
//         }
// 
//         return algeronConfiguration;
//     }
// 
//     private final static Map<String, Object> loadConfiguration(String configurationContent) {
//         return (Map<String, Object>) new Yaml().load(configurationContent);
//     }
// }
// 
// Path: consumer/core/src/main/java/org/arquillian/algeron/consumer/core/ContractsPublisherObserver.java
// public class ContractsPublisherObserver {
// 
//     private static Logger logger = Logger.getLogger(ContractsPublisherObserver.class.getName());
//     private static final String PROVIDER = "provider";
// 
//     public void publish(@Observes AfterClass event, AlgeronConsumerConfiguration algeronConsumerConfiguration)
//         throws IOException {
//         if (algeronConsumerConfiguration.isPublishContracts()
//             && algeronConsumerConfiguration.isPublishConfigurationSet()) {
// 
//             final Map<String, Object> publishConfiguration = algeronConsumerConfiguration.getPublishConfiguration();
//             if (publishConfiguration.containsKey(PROVIDER)) {
//                 final String providerName = (String) publishConfiguration.get(PROVIDER);
// 
//                 final org.arquillian.algeron.consumer.spi.publisher.ContractsPublisher contractsPublisher =
//                     getContractPublisher(providerName);
//                 contractsPublisher.configure(publishConfiguration);
//                 contractsPublisher.publish();
//             } else {
//                 logger.log(Level.WARNING, String.format(
//                     "Publishing contracts are enabled, but configuration is not providing a %s property with provider name to be used.",
//                     PROVIDER));
//             }
//         }
//     }
// 
//     private org.arquillian.algeron.consumer.spi.publisher.ContractsPublisher getContractPublisher(String name) {
//         ServiceLoader<org.arquillian.algeron.consumer.spi.publisher.ContractsPublisher> pactPublisherServiceLoader =
//             ServiceLoader.load(org.arquillian.algeron.consumer.spi.publisher.ContractsPublisher.class);
// 
//         for (org.arquillian.algeron.consumer.spi.publisher.ContractsPublisher contractsPublisher : pactPublisherServiceLoader) {
//             if (contractsPublisher.getName().equals(name)) {
//                 return contractsPublisher;
//             }
//         }
// 
//         throw new IllegalArgumentException(String.format("No contract publisher registered with name %s.", name));
//     }
// }


// Path: consumer/core/src/test/java/org/arquillian/algeron/consumer/core/publisher/UrlContractsPublisherObserverTest.java
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.github.tomakehurst.wiremock.matching.ContainsPattern;
import net.jcip.annotations.NotThreadSafe;
import org.arquillian.algeron.consumer.core.AlgeronConsumerConfiguration;
import org.arquillian.algeron.consumer.core.ContractsPublisherObserver;
import org.jboss.arquillian.core.api.annotation.ApplicationScoped;
import org.jboss.arquillian.core.test.AbstractManagerTestBase;
import org.jboss.arquillian.test.spi.event.suite.AfterClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

package org.arquillian.algeron.consumer.core.publisher;



@NotThreadSafe
public class UrlContractsPublisherObserverTest extends AbstractManagerTestBase {

    @Override
    protected void addExtensions(List<Class<?>> extensions) {
        extensions.add(ContractsPublisherObserver.class);
        super.addExtensions(extensions);
    }

    @Rule
    public TemporaryFolder temporaryFolder = new TemporaryFolder();

    @Rule
    public WireMockRule service = new WireMockRule(18081);

    @Test
    public void should_move_files_to_output_directory() throws IOException {

        final File origin = temporaryFolder.newFolder("origin");

        byte[] content = "Contract File".getBytes();
        Files.copy(new ByteArrayInputStream(content), new File(origin, "pact.txt").toPath());

        String config = "provider: url\n" +
            "contractsFolder: " + origin.getAbsolutePath() + "\n" +
            "url: http://localhost:18081/pacts\n";

        final Map<String, String> params = new HashMap<>();
        params.put("publishContracts", "true");
        params.put("publishConfiguration", config);

        final AlgeronConsumerConfiguration pactConsumerConfiguration = AlgeronConsumerConfiguration.fromMap(params);
        bind(ApplicationScoped.class, AlgeronConsumerConfiguration.class, pactConsumerConfiguration);

        WireMock.stubFor(
            WireMock.post(WireMock.urlEqualTo("/pacts/pact.txt"))
                .withRequestBody(new ContainsPattern("Contract File"))
                .willReturn(WireMock.aResponse().withStatus(200)));

        fire(new AfterClass(UrlContractsPublisherObserverTest.class));

        WireMock.verify(WireMock.postRequestedFor(WireMock.urlEqualTo("/pacts/pact.txt"))