/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.felix.dependencymanager.test;

import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
import static org.ops4j.pax.exam.CoreOptions.options;
import static org.ops4j.pax.exam.CoreOptions.provision;

import java.io.IOException;
import java.util.Dictionary;
import java.util.Properties;

import junit.framework.Assert;

import org.apache.felix.dependencymanager.DependencyManager;
import org.apache.felix.dependencymanager.Logger;
import org.apache.felix.dependencymanager.Service;
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.JUnit4TestRunner;
import org.osgi.framework.BundleContext;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.cm.ConfigurationException;
import org.osgi.service.cm.ManagedService;

@RunWith(JUnit4TestRunner.class)
public class ConfigurationDependencyTest {
    @Configuration
    public static Option[] configuration() {
        return options(
            provision(
                mavenBundle().groupId("org.apache.felix").artifactId("org.osgi.compendium").versionAsInProject(),
                mavenBundle().groupId("org.apache.felix").artifactId("org.apache.felix.configadmin").version("1.2.4"),
                mavenBundle().groupId("org.apache.felix").artifactId("org.apache.felix.dependencymanager").versionAsInProject()
            )
        );
    }    
    
    @Test
    public void testComponentWithRequiredConfiguration(BundleContext context) {
        DependencyManager m = new DependencyManager(context, new Logger(context));
        // helper class that ensures certain steps get executed in sequence
        Ensure e = new Ensure();
        // create a service provider and consumer
        Service s1 = m.createService().setImplementation(new ConfigurationConsumer(e)).add(m.createConfigurationDependency().setPid("test"));
        Service s2 = m.createService().setImplementation(new ConfigurationCreator(e)).add(m.createServiceDependency().setService(ConfigurationAdmin.class).setRequired(true));
        m.add(s1);
        m.add(s2);
        e.waitForStep(2, 2000);
        m.remove(s1);
        m.remove(s2);
        // ensure we executed all steps inside the component instance
        e.step(3);
    }
}

class ConfigurationCreator {
    private volatile ConfigurationAdmin m_ca;
    private final Ensure m_ensure;
    
    public ConfigurationCreator(Ensure e) {
        m_ensure = e;
    }

    public void start() {
        try {
            m_ensure.step(1);
            org.osgi.service.cm.Configuration conf = m_ca.getConfiguration("test", null);
            Properties props = new Properties();
            props.put("testkey", "testvalue");
            conf.update(props);
        }
        catch (IOException e) {
            Assert.fail("Could not create configuration: " + e.getMessage());
        }
    }
}

class ConfigurationConsumer implements ManagedService {
    private final Ensure m_ensure;

    public ConfigurationConsumer(Ensure e) {
        m_ensure = e;
    }

    public void updated(Dictionary props) throws ConfigurationException {
        if (props != null) {
            m_ensure.step(2);
            if (!"testvalue".equals(props.get("testkey"))) {
                Assert.fail("Could not find the configured property.");
            }
        }
    }
}
@@ -2323,16 +2323,45 @@
 guration
+AndServicePropertyPropagation
 (BundleC
@@ -2703,24 +2703,69 @@
 onsumer(e)).
+setInterface(Runnable.class.getName(), null).
 add(m.create
@@ -2804,16 +2804,35 @@
 (%22test%22)
+.setPropagate(true)
 );%0A     
@@ -2993,32 +2993,227 @@
 equired(true));%0A
+        Service s3 = m.createService().setImplementation(new ConfiguredServiceConsumer(e)).add(m.createServiceDependency().setService(Runnable.class, (%22(testkey=testvalue)%22)).setRequired(true));%0A
         m.add(s1
@@ -3230,24 +3230,43 @@
  m.add(s2);%0A
+        m.add(s3);%0A
         e.wa
@@ -3275,17 +3275,17 @@
 ForStep(
-2
+4
 , 2000);
@@ -3325,24 +3325,46 @@
 remove(s2);%0A
+        m.remove(s3);%0A
         // e
@@ -3436,17 +3436,17 @@
  e.step(
-3
+5
 );%0A    %7D
@@ -4117,16 +4117,26 @@
 dService
+, Runnable
  %7B%0A    p
@@ -4520,25 +4520,368 @@
       %7D%0A        %7D%0A    %7D%0A
-%7D
+    %0A    public void run() %7B%0A        m_ensure.step(4);%0A    %7D%0A%7D%0A%0Aclass ConfiguredServiceConsumer %7B%0A    private final Ensure m_ensure;%0A    private volatile Runnable m_runnable;%0A%0A    public ConfiguredServiceConsumer(Ensure e) %7B%0A        m_ensure = e;%0A    %7D%0A    %0A    public void start() %7B%0A        m_ensure.step(3);%0A        m_runnable.run();%0A    %7D%0A%7D%0A

Extended the test.