// Path: src/test/java/com/cisco/cta/taxii/adapter/YamlFileApplicationContextInitializer.java
// public class YamlFileApplicationContextInitializer  implements ApplicationContextInitializer<ConfigurableApplicationContext> {
//     @Override
//     public void initialize(ConfigurableApplicationContext applicationContext) {
//         try {
//             Resource resource = applicationContext.getResource("classpath:/config/application.yml");
//             YamlPropertySourceLoader sourceLoader = new YamlPropertySourceLoader();
//             List<PropertySource<?>> yamlTestPropertiesList = sourceLoader.load("application.yml", resource);
//             yamlTestPropertiesList.forEach(yamlTestProperties -> applicationContext.getEnvironment().getPropertySources().addLast(yamlTestProperties));
//         } catch (IOException e) {
//             throw new RuntimeException(e);
//         }
//     }
// }
// 
// Path: src/test/java/com/cisco/cta/taxii/adapter/smoketest/ContainsMessageMatcher.java
// public static ArgumentMatcher<ILoggingEvent> containsMessage(String message) {
//     return new ContainsMessageMatcher(message);
// }


import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import com.cisco.cta.taxii.adapter.YamlFileApplicationContextInitializer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static com.cisco.cta.taxii.adapter.smoketest.ContainsMessageMatcher.containsMessage;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/*
   Copyright 2015 Cisco Systems

   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 com.cisco.cta.taxii.adapter.smoketest;




@ContextConfiguration(classes = {SmokeTestConfiguration.class}, initializers = YamlFileApplicationContextInitializer.class)
@ActiveProfiles("smoketest")
@TestPropertySource(properties={
    "taxiiService.pollEndpoint=http://nosuchsite",
    "proxy.url=http://nosuchsite"
})
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class DetectUnreachableTaxiiTest {

    @Autowired
    private SmokeTestLifecycle smokeTestLifecycle;

    private Logger logger;
    private Appender<ILoggingEvent> appender;

    @Before
    @SuppressWarnings("unchecked")
    public void setUp() throws Exception {
        appender = mock(Appender.class);
        logger = (Logger) LoggerFactory.getLogger(SmokeTestLifecycle.class);
        logger.addAppender(appender);
    }

    @After
    public void tearDown() throws Exception {
        logger.detachAppender(appender);
    }


    @Test
    public void failOnUnreachableTaxiiEndpoint() throws Exception {
        smokeTestLifecycle.validateTaxiiConnectivity();
// Path: src/test/java/com/cisco/cta/taxii/adapter/YamlFileApplicationContextInitializer.java
// public class YamlFileApplicationContextInitializer  implements ApplicationContextInitializer<ConfigurableApplicationContext> {
//     @Override
//     public void initialize(ConfigurableApplicationContext applicationContext) {
//         try {
//             Resource resource = applicationContext.getResource("classpath:/config/application.yml");
//             YamlPropertySourceLoader sourceLoader = new YamlPropertySourceLoader();
//             List<PropertySource<?>> yamlTestPropertiesList = sourceLoader.load("application.yml", resource);
//             yamlTestPropertiesList.forEach(yamlTestProperties -> applicationContext.getEnvironment().getPropertySources().addLast(yamlTestProperties));
//         } catch (IOException e) {
//             throw new RuntimeException(e);
//         }
//     }
// }
// 
// Path: src/test/java/com/cisco/cta/taxii/adapter/smoketest/ContainsMessageMatcher.java
// public static ArgumentMatcher<ILoggingEvent> containsMessage(String message) {
//     return new ContainsMessageMatcher(message);
// }


// Path: src/test/java/com/cisco/cta/taxii/adapter/smoketest/DetectUnreachableTaxiiTest.java
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import com.cisco.cta.taxii.adapter.YamlFileApplicationContextInitializer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static com.cisco.cta.taxii.adapter.smoketest.ContainsMessageMatcher.containsMessage;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

/*
   Copyright 2015 Cisco Systems

   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 com.cisco.cta.taxii.adapter.smoketest;




@ContextConfiguration(classes = {SmokeTestConfiguration.class}, initializers = YamlFileApplicationContextInitializer.class)
@ActiveProfiles("smoketest")
@TestPropertySource(properties={
    "taxiiService.pollEndpoint=http://nosuchsite",
    "proxy.url=http://nosuchsite"
})
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class DetectUnreachableTaxiiTest {

    @Autowired
    private SmokeTestLifecycle smokeTestLifecycle;

    private Logger logger;
    private Appender<ILoggingEvent> appender;

    @Before
    @SuppressWarnings("unchecked")
    public void setUp() throws Exception {
        appender = mock(Appender.class);
        logger = (Logger) LoggerFactory.getLogger(SmokeTestLifecycle.class);
        logger.addAppender(appender);
    }

    @After
    public void tearDown() throws Exception {
        logger.detachAppender(appender);
    }


    @Test
    public void failOnUnreachableTaxiiEndpoint() throws Exception {
        smokeTestLifecycle.validateTaxiiConnectivity();
        verify(appender).doAppend(argThat(containsMessage("Unable to resolve host name http://nosuchsite, verify your application.yml and your DNS settings")));