package no.priv.bang.ukelonn.tests;

import static org.junit.Assert.*;
import static org.ops4j.pax.exam.CoreOptions.*;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.*;

import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.inject.Inject;

import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Configuration;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.junit.PaxExam;
import org.ops4j.pax.exam.options.MavenArtifactUrlReference;
import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
import org.ops4j.pax.exam.spi.reactors.PerClass;
import org.ops4j.pax.web.itest.base.HttpTestClient;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceReference;
import no.priv.bang.ukelonn.UkelonnService;
import no.priv.bang.ukelonn.UkelonnDatabase;

@RunWith(PaxExam.class)
@ExamReactorStrategy(PerClass.class)
public class UkelonnServiceIntegrationTest extends UkelonnServiceIntegrationTestBase {
    Bundle installWarBundle;

    @Inject
    private UkelonnDatabase database;

    @Inject
    BundleContext bundleContext;

    UkelonnService ukelonnService;
    ServiceListener ukelonnServiceListener;

    @Configuration
    public Option[] config() {
        final MavenArtifactUrlReference karafUrl = maven().groupId("org.apache.karaf").artifactId("apache-karaf-minimal").type("zip").versionAsInProject();
        final MavenArtifactUrlReference paxJdbcRepo = maven().groupId("org.ops4j.pax.jdbc").artifactId("pax-jdbc-features").versionAsInProject().type("xml").classifier("features");
        final MavenArtifactUrlReference ukelonnFeatureRepo = maven().groupId("no.priv.bang.ukelonn").artifactId("ukelonn.karaf").versionAsInProject().type("xml").classifier("features");
        return options(
            karafDistributionConfiguration().frameworkUrl(karafUrl).unpackDirectory(new File("target/exam")).useDeployFolder(false).runEmbedded(true),
            configureConsole().ignoreLocalConsole().ignoreRemoteShell(),
            junitBundles(),
            features(paxJdbcRepo),
            features(ukelonnFeatureRepo, "ukelonn-db-derby-test", "ukelonn"));
    }

    @Before
    public void setup() throws BundleException {
        // Can't use injection for the Webapp service since the bundle isn't
        // started until setUp and all injections are resolved
        // before the tests start.
        //
        // Creating a service listener instead.
        ukelonnServiceListener = new ServiceListener() {

	    	@Override
                public void serviceChanged(ServiceEvent event) {
                    @SuppressWarnings("rawtypes")
                        ServiceReference sr = event.getServiceReference();
                    @SuppressWarnings("unchecked")
                        Object rawService = bundleContext.getService(sr);
                    if (rawService instanceof UkelonnService) {
                        UkelonnService service = (UkelonnService) rawService;
                        switch(event.getType()) {
                          case ServiceEvent.REGISTERED:
                            ukelonnService = service;
                            break;
                          default:
                            break;
                        }
                    }
                }
            };
        bundleContext.addServiceListener(ukelonnServiceListener);

        // The war bundle has to be manually started or it won't work
        final String bundlePath = "mvn:no.priv.bang.ukelonn/ukelonn.bundle/" + getMavenProjectVersion() + "/war";
        installWarBundle = bundleContext.installBundle(bundlePath);
        installWarBundle.start();
    }

    @After
    public void tearDown() throws BundleException {
    	installWarBundle.stop();
    }

    @Test
    public void ukelonnServiceIntegrationTest() {
    	// Verify that the service could be injected
    	assertNotNull(ukelonnService);
    	assertEquals("Hello world!", ukelonnService.getMessage());
    }

    @Test
    public void testDerbyTestDatabase() throws SQLException {
        ResultSet onAccount = database.query("select * from accounts_view where username='jad'");
        assertNotNull(onAccount);
        while (onAccount.next()) {
            int account_id = onAccount.getInt("account_id");
            int user_id = onAccount.getInt("user_id");
            String username = onAccount.getString("username");
            String first_name = onAccount.getString("first_name");
            String last_name = onAccount.getString("last_name");
            assertEquals(3, account_id);
            assertEquals(3, user_id);
            assertEquals("jad", username);
            assertEquals("Jane", first_name);
            assertEquals("Doe", last_name);
        }
    }

    @Ignore
    @Test
    public void webappAccessTest() throws Exception {
    	Thread.sleep(20*1000);
        HttpTestClient testclient = new HttpTestClient();
    	try {
            String response = testclient.testWebPath("http://localhost:8081/ukelonn/", 404);
            assertEquals("", response);
    	} finally {
            testclient.close();
            testclient = null;
    	}
    }

}

@@ -299,57 +299,8 @@
 t;%0A%0A
-import org.junit.After;%0Aimport org.junit.Before;%0A
 impo
@@ -320,16 +320,16 @@
 Ignore;%0A
+
 import o
@@ -752,219 +752,8 @@
 le;%0A
-import org.osgi.framework.BundleContext;%0Aimport org.osgi.framework.BundleException;%0Aimport org.osgi.framework.ServiceListener;%0Aimport org.osgi.framework.ServiceEvent;%0Aimport org.osgi.framework.ServiceReference;%0A
 impo
@@ -1087,115 +1087,37 @@
     
-BundleContext bundleContext;%0A%0A    UkelonnService ukelonnService;%0A    ServiceListener ukelonnServiceListener
+UkelonnService ukelonnService
 ;%0A%0A 
@@ -1999,16 +1999,16 @@
 cRepo),%0A
+
         
@@ -2089,1640 +2089,8 @@
  %7D%0A%0A
-    @Before%0A    public void setup() throws BundleException %7B%0A        // Can't use injection for the Webapp service since the bundle isn't%0A        // started until setUp and all injections are resolved%0A        // before the tests start.%0A        //%0A        // Creating a service listener instead.%0A        ukelonnServiceListener = new ServiceListener() %7B%0A%0A%09    %09@Override%0A                public void serviceChanged(ServiceEvent event) %7B%0A                    @SuppressWarnings(%22rawtypes%22)%0A                        ServiceReference sr = event.getServiceReference();%0A                    @SuppressWarnings(%22unchecked%22)%0A                        Object rawService = bundleContext.getService(sr);%0A                    if (rawService instanceof UkelonnService) %7B%0A                        UkelonnService service = (UkelonnService) rawService;%0A                        switch(event.getType()) %7B%0A                          case ServiceEvent.REGISTERED:%0A                            ukelonnService = service;%0A                            break;%0A                          default:%0A                            break;%0A                        %7D%0A                    %7D%0A                %7D%0A            %7D;%0A        bundleContext.addServiceListener(ukelonnServiceListener);%0A%0A        // The war bundle has to be manually started or it won't work%0A        final String bundlePath = %22mvn:no.priv.bang.ukelonn/ukelonn.bundle/%22 + getMavenProjectVersion() + %22/war%22;%0A        installWarBundle = bundleContext.installBundle(bundlePath);%0A        installWarBundle.start();%0A    %7D%0A%0A    @After%0A    public void tearDown() throws BundleException %7B%0A    %09installWarBundle.stop();%0A    %7D%0A%0A
     

Remove explicit start of war bundle from pax exam test.