/*
 * Copyright 2009 OW2 Chameleon Licensed 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.ow2.chameleon.fuchsia.testing;

import org.apache.felix.ipojo.ComponentInstance;
import org.apache.felix.ipojo.ConfigurationException;
import org.apache.felix.ipojo.Factory;
import org.apache.felix.ipojo.MissingHandlerException;
import org.apache.felix.ipojo.UnacceptableConfiguration;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.ow2.chameleon.testing.helpers.OSGiHelper;
import org.ow2.chameleon.testing.helpers.TimeUtils;

import java.util.Dictionary;

public class FuchsiaHelper {
    private final BundleContext context;

    public FuchsiaHelper(BundleContext pContext) {
        context = pContext;
    }

    /**
     * Create an instance with the given factory and properties
     *
     * @param factory
     * @param properties
     * @return
     */
    public static ComponentInstance createInstance(final Factory factory, final Dictionary<String, Object> properties) {
        ComponentInstance instance = null;

        // Create an instance
        try {
            instance = factory.createComponentInstance(properties);
        } catch (UnacceptableConfiguration e) {
        } catch (MissingHandlerException e) {
        } catch (ConfigurationException e) {
        }
        return instance;
    }

    /**
     * Get the Factory linked to the given pid
     *
     * @param osgi
     * @param name
     * @return The factory
     */
    public static Factory getValidFactory(final OSGiHelper osgi, final String name) {
        // Get The Factory ServiceReference
        ServiceReference facref = osgi.getServiceReference(Factory.class.getName(), "(&(factory.state=1)(factory.name=" + name + "))");
        // Get the factory
        Factory factory = (Factory) osgi.getServiceObject(facref);

        return factory;
    }

    public static void waitForIt(int time) {
        TimeUtils.grace(time);
    }

    public <T> ServiceRegistration registerService(T service, Class<T> klass) {
        return context.registerService(klass.getName(), service, null);
    }

    @SuppressWarnings("unchecked")
    public <T> T getServiceObject(Class<T> klass, String filter) {
        OSGiHelper helper = new OSGiHelper(context);
        return  helper.getServiceObject(klass, filter);
    }

    @SuppressWarnings("unchecked")
    public <T> T getServiceObject(Class<T> klass) {
        ServiceReference sref = null;

        sref = context.getServiceReference(klass.getName());

        if (sref != null) {
            T service = (T) context.getService(sref);
            context.ungetService(sref);
            return service;
        } else {
            return null;
        }
    }

    public void dispose() {

    }

}

@@ -698,181 +698,55 @@
 ojo.
-ConfigurationException;%0Aimport org.apache.felix.ipojo.Factory;%0Aimport org.apache.felix.ipojo.MissingHandlerException;%0Aimport org.apache.felix.ipojo.UnacceptableConfigura
+Factory;%0Aimport org.assertj.core.api.Asser
 tion
+s
 ;%0Aim
@@ -1615,129 +1615,144 @@
 ch (
-UnacceptableConfiguration e) %7B%0A        %7D catch (MissingHandlerException e) %7B%0A        %7D catch (ConfigurationException e) %7B
+Exception e) %7B%0A            Assertions.fail(%22Creation of instance failed (%22 + factory +%0A                    %22, %22 + properties + %22).%22, e);
 %0A   
@@ -2714,16 +2714,16 @@
 ntext);%0A
+
         
@@ -2729,17 +2729,16 @@
  return 
- 
 helper.g

Fix sonar issues in testing module