package de.guderlei.pubsub.integrationtest;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.*;
import static org.mockito.Matchers.eq;
import static org.ops4j.pax.exam.CoreOptions.*;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.junit.Configuration;
import org.ops4j.pax.exam.junit.ExamReactorStrategy;
import org.ops4j.pax.exam.junit.JUnit4TestRunner;
import org.ops4j.pax.exam.spi.reactors.AllConfinedStagedReactorFactory;
import org.osgi.framework.*;

import de.guderlei.pubsub.model.Subscriber;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.util.tracker.ServiceTracker;


import javax.inject.Inject;
import java.io.IOException;
import java.util.Arrays;
import java.util.Properties;

@RunWith(JUnit4TestRunner.class)
@ExamReactorStrategy(AllConfinedStagedReactorFactory.class)
public class StartupDsSubscriberTest {
	@Inject
	BundleContext bundleContext;

    @Inject
    ConfigurationAdmin configurationAdmin;
	
	@Configuration
	public Option[] configure() {
		return options(
                provision(
                //mavenBundle().groupId("org.apache.servicemix.bundles").artifactId("org.apache.servicemix.bundles.junit").version("3.8.2_4").start(),
                mavenBundle().groupId("biz.aQute").artifactId("bndlib").version("1.43.0").start(),
                mavenBundle().groupId("org.apache.felix").artifactId("org.apache.felix.log").version("1.0.1").start(),
                mavenBundle( "org.apache.felix", "org.apache.felix.scr", "1.4.0" ).start(),
                mavenBundle( "org.apache.felix", "org.apache.felix.configadmin", "1.2.8" ).start(),
                mavenBundle( "org.apache.felix", "org.apache.felix.metatype", "1.0.4" ).start(),
                mavenBundle().groupId( "de.guderlei.osgidemo" ).artifactId( "model" ).version( "1.0.0" ).start(),
				mavenBundle().groupId( "de.guderlei.osgidemo" ).artifactId( "ds_subscriber" ).version( "1.0.0" ).start()

		), junitBundles());
	}

    @Before
    public void before() throws BundleException{
        for(Bundle bundle: bundleContext.getBundles()){
                    bundle.start();
                    System.out.println("Bundle " + bundle.getSymbolicName() + " started");
        }
    }

	/**
	 * Checks whether one {@link Subscriber} is registered
	 */
	@Test
	public void one_subscriber_is_registered() throws InterruptedException, IOException, InvalidSyntaxException {
        assertThat(configurationAdmin, is(notNullValue()));
        org.osgi.service.cm.Configuration config =  configurationAdmin.createFactoryConfiguration("simple_subscriber", null);
        Properties props = new Properties();
        props.put("prefix", "foo");
        config.update(props);

      /*  System.out.println("config created");
        System.out.println(config.getFactoryPid());
        System.out.println(config.getPid());
        System.out.println(config.getBundleLocation());
        System.out.println(config.getProperties());    */


        ServiceTracker serviceTracker = new ServiceTracker(bundleContext, Subscriber.class.getName(), null);
        serviceTracker.open();
        Subscriber subscriber = (Subscriber) serviceTracker.waitForService(1000);
        assertThat(subscriber, is(notNullValue()));

		assertEquals("de.guderlei.pubsub.subscriber.SimpleSubscriber", subscriber.getClass().getName());
	}



    /**
     * Checks whether one {@link Subscriber} is registered
     */
    @Test
    @Ignore
    public void requesting_services_with_different_configurations_leads_to_different_objects() throws InterruptedException, IOException {
        org.osgi.service.cm.Configuration config = configurationAdmin.createFactoryConfiguration("simple_subscriber");
               Properties props = new Properties();
               props.put("ds.subscriber.prefix", "foo");
               config.update(props);

        ServiceReference ref = bundleContext.getServiceReference(Subscriber.class.getName());
        assertNotNull(ref);
        Subscriber srvc1 = (Subscriber) bundleContext.getService(ref);



        Subscriber srvc2 = (Subscriber) bundleContext.getService(ref);

        assertFalse(srvc1 == srvc2);

        bundleContext.ungetService(ref);


    }



}

@@ -3650,20 +3650,8 @@
 est%0A
-    @Ignore%0A
     
@@ -3834,16 +3834,17 @@
 config =
+ 
  configu
@@ -3901,26 +3901,25 @@
 scriber%22
-);%0A       
+, null);%0A
         
@@ -3955,32 +3955,218 @@
 ties();%0A        
+props.put(%22prefix%22, %22foo%22);%0A        config.update(props);%0A%0A        config =  configurationAdmin.createFactoryConfiguration(%22simple_subscriber%22, null);%0A        props = new Properties();%0A 
        props.put
@@ -4171,22 +4171,8 @@
 ut(%22
-ds.subscriber.
 pref
@@ -4177,30 +4177,23 @@
 efix%22, %22
-foo
+bar
 %22);%0A
-       
         
@@ -4234,58 +4234,67 @@
 vice
-Reference ref = bundleContext.getServiceReference(
+Tracker serviceTracker = new ServiceTracker(bundleContext, 
 Subs
@@ -4315,16 +4315,22 @@
 etName()
+, null
 );%0A     
@@ -4336,36 +4336,40 @@
     
-as
 ser
-tNotNull(ref
+viceTracker.open(
 );%0A
+%0A
         
 Subs
@@ -4368,146 +4368,136 @@
     
-Subscriber
+Object%5B%5D
  s
+e
 rv
-c1 = (Subscriber) bundleContext.getService(ref);%0A%0A%0A%0A        Subscriber srvc2 = (Subscriber) bundleContext.getS
+ices = serviceTracker.getServices();%0A        assertEquals(services.length, 2);%0A        assertNotNull(s
 ervice
-(ref
+s%5B0%5D
 );%0A
-%0A
     
@@ -4510,40 +4510,38 @@
 sert
-False(srvc1 == srvc2
+NotNull(services%5B1%5D
 );%0A
-%0A
         
 bund
@@ -4540,42 +4540,48 @@
     
-bundleContext.ungetService(ref);%0A%0A
+assertFalse(services%5B0%5D == services%5B1%5D);
 %0A   

add test to verify that there is a service instance for every configuration