/*
 * 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.sling.installer.it;

import static org.junit.Assert.fail;

import java.util.Dictionary;
import java.util.Hashtable;

import org.apache.sling.installer.api.InstallableResource;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.junit.PaxExam;
import org.osgi.service.cm.Configuration;

@RunWith(PaxExam.class)
public class ConfigPrioritiesTest extends OsgiInstallerTestBase {

    private final static long TIMEOUT = 5000L;

    @org.ops4j.pax.exam.Configuration
    public Option[] config() {
        return defaultConfiguration();
    }

    @Before
    public void setUp() {
        setupInstaller();
    }

    @Override
    @After
    public void tearDown() {
        super.tearDown();
    }

    void assertConfigValue(String pid, String key, String value, long timeoutMsec) throws Exception {
        boolean found = false;
        final String info = pid + ": waiting for " + key + "=" + value;
        final long end = System.currentTimeMillis() + timeoutMsec;
        do {
            final Configuration cfg = waitForConfiguration(info, pid, timeoutMsec, true);
            if(value.equals(cfg.getProperties().get(key))) {
                found = true;
                break;
            }
        } while(System.currentTimeMillis() < end);

        if(!found) {
            fail("Did not get expected value: " + info);
        }
    }

    @Test
    public void testOverrideConfig() throws Exception {
        final String pid = getClass().getSimpleName() + "." + System.currentTimeMillis();

        final Dictionary<String, Object> data = new Hashtable<String, Object>();
        data.put("foo", "a");
        final InstallableResource a = getInstallableResource("a/" + pid, data, InstallableResource.DEFAULT_PRIORITY - 1)[0];
        data.put("foo", "b");
        final InstallableResource b = getInstallableResource("b/" + pid, data, InstallableResource.DEFAULT_PRIORITY)[0];
        data.put("foo", "c");
        final InstallableResource c = getInstallableResource("c/" + pid, data, InstallableResource.DEFAULT_PRIORITY + 1)[0];

        // c has more priority than b which has more than a
        installer.updateResources(URL_SCHEME, new InstallableResource[] {b}, null);
        assertConfigValue(pid, "foo", "b", TIMEOUT);
        installer.updateResources(URL_SCHEME, new InstallableResource[] {c}, null);
        assertConfigValue(pid, "foo", "c", TIMEOUT);
        installer.updateResources(URL_SCHEME, new InstallableResource[] {a}, null);

        // highest prio should be active (c)
        assertConfigValue(pid, "foo", "c", TIMEOUT);

        // removing c, second highest prio should be active (b)
        installer.updateResources(URL_SCHEME, null, new String[] {c.getId()});
        assertConfigValue(pid, "foo", "b", TIMEOUT);

        // removing b, a should be active
        installer.updateResources(URL_SCHEME, null, new String[] {b.getId()});
        assertConfigValue(pid, "foo", "a", TIMEOUT);

        // and config should be gone only after removing everything
        installer.updateResources(URL_SCHEME, null, new String[] {a.getId()});
        waitForConfiguration("After removing all resources", pid, TIMEOUT, false);
    }
}

@@ -840,46 +840,8 @@
 t;%0A%0A
-import static org.junit.Assert.fail;%0A%0A
 impo
@@ -865,16 +865,16 @@
 ionary;%0A
+
 import j
@@ -1136,50 +1136,8 @@
 xam;
-%0Aimport org.osgi.service.cm.Configuration;
 %0A%0A@R
@@ -1550,659 +1550,8 @@
  %7D%0A%0A
-    void assertConfigValue(String pid, String key, String value, long timeoutMsec) throws Exception %7B%0A        boolean found = false;%0A        final String info = pid + %22: waiting for %22 + key + %22=%22 + value;%0A        final long end = System.currentTimeMillis() + timeoutMsec;%0A        do %7B%0A            final Configuration cfg = waitForConfiguration(info, pid, timeoutMsec, true);%0A            if(value.equals(cfg.getProperties().get(key))) %7B%0A                found = true;%0A                break;%0A            %7D%0A        %7D while(System.currentTimeMillis() %3C end);%0A%0A        if(!found) %7B%0A            fail(%22Did not get expected value: %22 + info);%0A        %7D%0A    %7D%0A%0A 
    @
@@ -1555,16 +1555,16 @@
   @Test%0A
+
     publ
@@ -2389,38 +2389,39 @@
  null);%0A        
-assert
+waitFor
 ConfigValue(pid,
@@ -2408,35 +2408,50 @@
 tForConfigValue(
-pid
+null, pid, TIMEOUT
 , %22foo%22, %22b%22, TI
@@ -2446,25 +2446,16 @@
 oo%22, %22b%22
-, TIMEOUT
 );%0A     
@@ -2533,38 +2533,39 @@
  null);%0A        
-assert
+waitFor
 ConfigValue(pid,
@@ -2552,35 +2552,50 @@
 tForConfigValue(
-pid
+null, pid, TIMEOUT
 , %22foo%22, %22c%22, TI
@@ -2590,25 +2590,16 @@
 oo%22, %22c%22
-, TIMEOUT
 );%0A     
@@ -2723,38 +2723,39 @@
 ive (c)%0A        
-assert
+waitFor
 ConfigValue(pid,
@@ -2742,35 +2742,50 @@
 tForConfigValue(
-pid
+null, pid, TIMEOUT
 , %22foo%22, %22c%22, TI
@@ -2780,25 +2780,16 @@
 oo%22, %22c%22
-, TIMEOUT
 );%0A%0A    
@@ -2927,38 +2927,39 @@
 Id()%7D);%0A        
-assert
+waitFor
 ConfigValue(pid,
@@ -2946,35 +2946,50 @@
 tForConfigValue(
-pid
+null, pid, TIMEOUT
 , %22foo%22, %22b%22, TI
@@ -2984,25 +2984,16 @@
 oo%22, %22b%22
-, TIMEOUT
 );%0A%0A    
@@ -3121,14 +3121,15 @@
     
-assert
+waitFor
 Conf
@@ -3136,19 +3136,34 @@
 igValue(
-pid
+null, pid, TIMEOUT
 , %22foo%22,
@@ -3166,25 +3166,16 @@
 oo%22, %22a%22
-, TIMEOUT
 );%0A%0A    

Remove duplicate method