// Path: src/main/java/com/cisco/cta/taxii/adapter/settings/ProxySettings.java
// @ConfigurationProperties(prefix="proxy")
// @Data
// public class ProxySettings {
// 
//     private URL url;
// 
//     private ProxyAuthenticationType authenticationType;
// 
//     private String domain;
// 
//     private String username;
// 
//     private String password;
// 
//     private String workstation;
// 
// }
// 
// Path: src/main/java/com/cisco/cta/taxii/adapter/settings/TaxiiServiceSettings.java
// @ConfigurationProperties(prefix="taxii-service")
// @Data
// @Validated
// public class TaxiiServiceSettings {
// 
//     private static final Charset UTF8 = Charset.forName("UTF-8");
// 
//     @NotNull
//     private URL pollEndpoint;
// 
//     @NotNull
//     private String username;
// 
//     @NotNull
//     private String password;
// 
//     private List<String> feeds;
// 
//     private File feedNamesFile;
// 
//     private File statusFile = new File("taxii-status.xml");
// 
//     @PostConstruct
//     public void loadFeedNames() throws IOException {
//         Preconditions.checkState(
//             feeds != null ^ feedNamesFile != null,
//             "taxii-service.feeds or taxii-service.feedNamesFile must be set");
//         if (feedNamesFile != null) {
//             feeds = Files.readLines(feedNamesFile, UTF8);
//         }
//     }
// }
// 
// Path: src/test/java/com/cisco/cta/taxii/adapter/settings/TaxiiServiceSettingsFactory.java
// public class TaxiiServiceSettingsFactory {
// 
//     public static TaxiiServiceSettings createDefaults() {
//         try {
//             TaxiiServiceSettings connSettings = new TaxiiServiceSettings();
//             connSettings.setPollEndpoint(new URL("http://localhost:8080/service"));
//             connSettings.setUsername("user");
//             connSettings.setPassword("pass");
//             return connSettings;
//         } catch (MalformedURLException e) {
//             throw new RuntimeException(e);
//         }
//     }
// }
// 
// Path: src/test/java/com/cisco/cta/taxii/adapter/IsEventContaining.java
// public static void verifyLog(Appender<ILoggingEvent> mockAppender, final String substring) {
//     verify(mockAppender, atLeastOnce())
//             .doAppend(argThat(isEventContaining(substring)));
// }


import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.core.Appender;
import com.cisco.cta.taxii.adapter.settings.ProxySettings;
import com.cisco.cta.taxii.adapter.settings.TaxiiServiceSettings;
import com.cisco.cta.taxii.adapter.settings.TaxiiServiceSettingsFactory;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.protocol.RequestAuthCache;
import org.apache.http.conn.HttpHostConnectException;
import org.apache.http.impl.client.HttpClients;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpRequestFactory;
import javax.net.ServerSocketFactory;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.SocketTimeoutException;
import static com.cisco.cta.taxii.adapter.IsEventContaining.verifyLog;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.when;
    private HttpClient httpClient;
    private TaxiiServiceSettings connSettings;
    private CredentialsProvider credentialsProvider;

    @Mock
    private Appender mockAppender;

    @Before
    public void setUp() throws Exception {
        httpClient = HttpClients.createDefault();
        credentialsProvider = Mockito.mock(CredentialsProvider.class);
        connSettings = TaxiiServiceSettingsFactory.createDefaults();
        credentialsProvider = Mockito.mock(CredentialsProvider.class);
        factory = new BasicAuthHttpRequestFactory(httpClient, connSettings, credentialsProvider);
        MockitoAnnotations.initMocks(this);
        when(mockAppender.getName()).thenReturn("MOCK");
        Logger authCacheLogger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(RequestAuthCache.class.getCanonicalName());
        authCacheLogger.setLevel(Level.DEBUG);
        authCacheLogger.addAppender(mockAppender);
    }

    @Test
    public void tryConnectWithBasicAuthentication() throws Exception {
        try {
            ClientHttpRequest req = factory.createRequest(connSettings.getPollEndpoint().toURI(), HttpMethod.POST);
            req.execute();
            fail("connection must fail");
        } catch (Throwable e) {
            assertThat(e, instanceOf(HttpHostConnectException.class));
        }
// Path: src/main/java/com/cisco/cta/taxii/adapter/settings/ProxySettings.java
// @ConfigurationProperties(prefix="proxy")
// @Data
// public class ProxySettings {
// 
//     private URL url;
// 
//     private ProxyAuthenticationType authenticationType;
// 
//     private String domain;
// 
//     private String username;
// 
//     private String password;
// 
//     private String workstation;
// 
// }
// 
// Path: src/main/java/com/cisco/cta/taxii/adapter/settings/TaxiiServiceSettings.java
// @ConfigurationProperties(prefix="taxii-service")
// @Data
// @Validated
// public class TaxiiServiceSettings {
// 
//     private static final Charset UTF8 = Charset.forName("UTF-8");
// 
//     @NotNull
//     private URL pollEndpoint;
// 
//     @NotNull
//     private String username;
// 
//     @NotNull
//     private String password;
// 
//     private List<String> feeds;
// 
//     private File feedNamesFile;
// 
//     private File statusFile = new File("taxii-status.xml");
// 
//     @PostConstruct
//     public void loadFeedNames() throws IOException {
//         Preconditions.checkState(
//             feeds != null ^ feedNamesFile != null,
//             "taxii-service.feeds or taxii-service.feedNamesFile must be set");
//         if (feedNamesFile != null) {
//             feeds = Files.readLines(feedNamesFile, UTF8);
//         }
//     }
// }
// 
// Path: src/test/java/com/cisco/cta/taxii/adapter/settings/TaxiiServiceSettingsFactory.java
// public class TaxiiServiceSettingsFactory {
// 
//     public static TaxiiServiceSettings createDefaults() {
//         try {
//             TaxiiServiceSettings connSettings = new TaxiiServiceSettings();
//             connSettings.setPollEndpoint(new URL("http://localhost:8080/service"));
//             connSettings.setUsername("user");
//             connSettings.setPassword("pass");
//             return connSettings;
//         } catch (MalformedURLException e) {
//             throw new RuntimeException(e);
//         }
//     }
// }
// 
// Path: src/test/java/com/cisco/cta/taxii/adapter/IsEventContaining.java
// public static void verifyLog(Appender<ILoggingEvent> mockAppender, final String substring) {
//     verify(mockAppender, atLeastOnce())
//             .doAppend(argThat(isEventContaining(substring)));
// }


// Path: src/test/java/com/cisco/cta/taxii/adapter/httpclient/BasicAuthHttpRequestFactoryTest.java
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.core.Appender;
import com.cisco.cta.taxii.adapter.settings.ProxySettings;
import com.cisco.cta.taxii.adapter.settings.TaxiiServiceSettings;
import com.cisco.cta.taxii.adapter.settings.TaxiiServiceSettingsFactory;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.protocol.RequestAuthCache;
import org.apache.http.conn.HttpHostConnectException;
import org.apache.http.impl.client.HttpClients;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpRequestFactory;
import javax.net.ServerSocketFactory;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.SocketTimeoutException;
import static com.cisco.cta.taxii.adapter.IsEventContaining.verifyLog;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.when;

    private HttpClient httpClient;
    private TaxiiServiceSettings connSettings;
    private CredentialsProvider credentialsProvider;

    @Mock
    private Appender mockAppender;

    @Before
    public void setUp() throws Exception {
        httpClient = HttpClients.createDefault();
        credentialsProvider = Mockito.mock(CredentialsProvider.class);
        connSettings = TaxiiServiceSettingsFactory.createDefaults();
        credentialsProvider = Mockito.mock(CredentialsProvider.class);
        factory = new BasicAuthHttpRequestFactory(httpClient, connSettings, credentialsProvider);
        MockitoAnnotations.initMocks(this);
        when(mockAppender.getName()).thenReturn("MOCK");
        Logger authCacheLogger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(RequestAuthCache.class.getCanonicalName());
        authCacheLogger.setLevel(Level.DEBUG);
        authCacheLogger.addAppender(mockAppender);
    }

    @Test
    public void tryConnectWithBasicAuthentication() throws Exception {
        try {
            ClientHttpRequest req = factory.createRequest(connSettings.getPollEndpoint().toURI(), HttpMethod.POST);
            req.execute();
            fail("connection must fail");
        } catch (Throwable e) {
            assertThat(e, instanceOf(HttpHostConnectException.class));
        }
        verifyLog(mockAppender, "Re-using cached 'basic' auth scheme for http://localhost:80");