package org.openengsb.itests.htmlunit;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openengsb.itests.util.AbstractPreConfiguredExamTestHelper;
import org.ops4j.pax.exam.junit.ExamReactorStrategy;
import org.ops4j.pax.exam.junit.JUnit4TestRunner;
import org.ops4j.pax.exam.spi.reactors.AllConfinedStagedReactorFactory;

import com.gargoylesoftware.htmlunit.TextPage;
import com.gargoylesoftware.htmlunit.WebClient;

import org.osgi.framework.Bundle;

@RunWith(JUnit4TestRunner.class)
@ExamReactorStrategy(AllConfinedStagedReactorFactory.class)
public class FrameworkVersionIT extends AbstractPreConfiguredExamTestHelper
{
    private static final Integer MAX_SLEEP_TIME_IN_SECONDS = 30;

    private WebClient            webClient;
    private String               versionPageUrl;

    @Before
    public void setUp() throws Exception
    {
        webClient = new WebClient();
        String httpport = getConfigProperty("org.ops4j.pax.web",
                "org.osgi.service.http.port");
        versionPageUrl = String.format(
                "http://localhost:%s/system/framework.version.info", httpport);
        waitForSiteToBeAvailable(versionPageUrl, MAX_SLEEP_TIME_IN_SECONDS);
    }

    @After
    public void tearDown() throws Exception
    {
        webClient.closeAllWindows();
        FileUtils.deleteDirectory(new File(getWorkingDirectory()));
    }

    @Test
    public void testIfVersionsMatch_shouldWork() throws Exception
    {
        final TextPage page = webClient.getPage(versionPageUrl);
        String pageVersion = page.getContent().trim().replace("-", ".");
        Bundle[] bundles = getBundleContext().getBundles();
        Bundle infoBundle = null;

        for (int i = 0; i < bundles.length; i++)
        {
            if (bundles[i].getSymbolicName().equals("org.openengsb.framework.info"))
            {
                infoBundle = bundles[i];
                break;
            }
        }
        
        assertNotNull(infoBundle);
        assertEquals(pageVersion, infoBundle.getVersion().toString().trim());
    }
}

add integration test to check version returned by http://localhost:8090/system/framework.version.info (cherry picked from commit 73f05c7875a643ed48fe29b7053a4b487c8e6753)