package com.hazelcast.osgi;

import com.hazelcast.test.annotation.QuickTest;
import org.junit.After;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;
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.ops4j.pax.exam.util.PathUtils;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

import javax.inject.Inject;

import static org.junit.Assert.assertNotNull;
import static org.ops4j.pax.exam.CoreOptions.bundle;
import static org.ops4j.pax.exam.CoreOptions.junitBundles;
import static org.ops4j.pax.exam.CoreOptions.options;

@RunWith(JUnit4TestRunner.class)
@Category(QuickTest.class)
@ExamReactorStrategy(AllConfinedStagedReactorFactory.class)
@Ignore(value = "https://github.com/hazelcast/hazelcast/issues/9537")
public class HazelcastOSGiIntegrationTest {

    @Inject
    private BundleContext bundleContext;

    @Configuration
    public Option[] config() {
        return options(bundle("reference:file:" + PathUtils.getBaseDir() + "/hazelcast/target/classes"),
                junitBundles());
    }

    @After
    public void tearDown() throws Exception {
        for (Bundle bundle : bundleContext.getBundles()) {
            if ("com.hazelcast".equals(bundle.getSymbolicName())) {
                bundle.uninstall();
                break;
            }
        }
    }

    @Test
    public void serviceRetrievedSuccessfully() {
        HazelcastOSGiService service = getService();
        assertNotNull(service);
    }

    private HazelcastOSGiService getService() {
        ServiceReference serviceRef = bundleContext.getServiceReference(HazelcastOSGiService.class.getName());
        if (serviceRef == null) {
            return null;
        }
        return (HazelcastOSGiService) bundleContext.getService(serviceRef);
    }
}

@@ -98,33 +98,8 @@
 er;%0A
-import org.junit.Ignore;%0A
 impo
@@ -381,24 +381,129 @@
 TestRunner;%0A
+import org.ops4j.pax.exam.options.CompositeOption;%0Aimport org.ops4j.pax.exam.options.UrlProvisionOption;%0A
 import org.o
@@ -604,16 +604,118 @@
 hUtils;%0A
+import org.ops4j.pax.url.maven.commons.MavenConstants;%0Aimport org.ops4j.pax.url.mvn.ServiceConstants;%0A
 import o
@@ -1192,78 +1192,8 @@
 ss)%0A
-@Ignore(value = %22https://github.com/hazelcast/hazelcast/issues/9537%22)%0A
 publ
@@ -1295,84 +1295,605 @@
     
-@Configuration%0A    public Option%5B%5D config() %7B%0A        return options(bundle(
+private static final String MAVEN_REPOSITORIES_PROP = ServiceConstants.PID + MavenConstants.PROPERTY_REPOSITORIES;%0A    private static final String MAVEN_REPOSITORIES = %22https://osgi.sonatype.org/content/groups/pax-runner@id=paxrunner,%22%0A                                                   + %22https://repo1.maven.org/maven2@id=central%22;%0A    private String oldMavenRepoProperty;%0A%0A    @Configuration%0A    public Option%5B%5D config() %7B%0A        oldMavenRepoProperty = System.getProperty(MAVEN_REPOSITORIES_PROP);%0A        System.setProperty(MAVEN_REPOSITORIES_PROP, MAVEN_REPOSITORIES);%0A%0A        String url = 
 %22ref
@@ -1964,10 +1964,9 @@
 ses%22
-),
+;
 %0A   
@@ -1974,30 +1974,151 @@
     
-        junitBundles()
+UrlProvisionOption hzBundle = bundle(url);%0A        CompositeOption junitBundles = junitBundles();%0A        return options(hzBundle, junitBundles
 );%0A 
@@ -2386,24 +2386,233 @@
 %7D%0A        %7D%0A
+        if (oldMavenRepoProperty == null) %7B%0A            System.clearProperty(MAVEN_REPOSITORIES_PROP);%0A        %7D else %7B%0A            System.setProperty(MAVEN_REPOSITORIES_PROP, oldMavenRepoProperty);%0A        %7D%0A
     %7D%0A%0A    @

Explicitly specify Maven repositories to be used during OSGi provisioning test