<commit_before>package io.liveoak.container.tenancy;

import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import io.liveoak.container.extension.ExtensionInstaller;
import io.liveoak.container.extension.ExtensionService;
import io.liveoak.container.extension.MountService;
import io.liveoak.container.tenancy.service.ApplicationsDeployerService;
import io.liveoak.container.tenancy.service.ApplicationsDirectoryService;
import io.liveoak.container.zero.ApplicationExtensionsResource;
import io.liveoak.container.zero.ApplicationsResource;
import io.liveoak.container.zero.extension.ApplicationClientsExtension;
import io.liveoak.container.zero.extension.ZeroExtension;
import io.liveoak.spi.LiveOak;
import io.liveoak.spi.resource.RootResource;
import io.liveoak.spi.resource.async.Resource;
import org.jboss.msc.service.*;
import org.jboss.msc.service.ServiceController.Mode;
import org.jboss.msc.service.ServiceController.State;
import org.jboss.msc.value.ImmediateValue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.util.Collection;

import static io.liveoak.spi.LiveOak.*;
import static org.fest.assertions.Assertions.assertThat;

/**
 * @author <NAME>
 */
public class TenancyTest {

    private ServiceContainer serviceContainer;

    @Before
    public void setUpServiceContainer() throws Exception {
        this.serviceContainer = ServiceContainer.Factory.create();

        File appDir = new File(getClass().getClassLoader().getResource("apps").getFile());
        this.serviceContainer.addService(LiveOak.APPLICATIONS_DIR, new ApplicationsDirectoryService(appDir))
                .install();

        this.serviceContainer.addService(LiveOak.SERVICE_REGISTRY, new ValueService<>(new ImmediateValue<>(this.serviceContainer))).install();

        ExtensionInstaller installer = new ExtensionInstaller(this.serviceContainer.subTarget(), LiveOak.resource(ZeroExtension.APPLICATION_ID, "system"));
        installer.load("application-clients", new ApplicationClientsExtension());

        this.serviceContainer.awaitStability();
    }

    @After
    public void tearDownServiceContainer() {
        this.serviceContainer.shutdown();
    }

    @Test
    public void testBootstrapPiecemeal() throws InterruptedException {
        InternalApplicationRegistry registry = new InternalApplicationRegistry(this.serviceContainer);

        InternalApplication installedApp = registry.createApplication(ZeroExtension.APPLICATION_ID, ZeroExtension.APPLICATION_NAME);

        SimpleResourceRegistry adminMount = new SimpleResourceRegistry("admin-mount");
        this.serviceContainer.addService(LiveOak.resource(ZeroExtension.APPLICATION_ID, "applications"), new ValueService<MountPointResource>(new ImmediateValue<>(adminMount)))
                .install();

        this.serviceContainer.awaitStability();

        ServiceController<InternalApplication> app = (ServiceController<InternalApplication>) this.serviceContainer.getService(application(ZeroExtension.APPLICATION_ID));

        assertThat(app).isNotNull();
        assertThat(app.getMode()).isEqualTo(Mode.ACTIVE);
        assertThat(app.getState()).isEqualTo(State.UP);

        ServiceController<ApplicationResource> appAdmin = (ServiceController<ApplicationResource>) this.serviceContainer.getService(applicationAdminResource(ZeroExtension.APPLICATION_ID));
        assertThat(appAdmin).isNotNull();
        assertThat(appAdmin.getMode()).isEqualTo(Mode.ACTIVE);
        assertThat(appAdmin.getState()).isEqualTo(State.UP);

        ServiceController<?> appAdminMount = this.serviceContainer.getService(defaultMount(applicationAdminResource(ZeroExtension.APPLICATION_ID)));
        assertThat(appAdminMount).isNotNull();
        assertThat(appAdminMount.getMode()).isEqualTo(Mode.ACTIVE);
        assertThat(appAdminMount.getState()).isEqualTo(State.UP);  // we are mounted

        ServiceController<ApplicationContext> appContext = (ServiceController<ApplicationContext>) this.serviceContainer.getService(defaultMount(applicationContext(ZeroExtension.APPLICATION_ID)));
        assertThat(appContext).isNotNull();
        assertThat(appContext.getMode()).isEqualTo(Mode.ACTIVE);
        assertThat(appContext.getState()).isEqualTo(State.DOWN); // our public side isn't mounted

        /*
        Collection<Resource> registeredApps = orgAdmin.getValue().applicationsResource().members();
        assertThat(registeredApps).isNotNull();
        assertThat(registeredApps).hasSize(1);
        assertThat(((ApplicationResource) registeredApps.iterator().next()).application()).isSameAs(app.getValue());
        */

        // now let's wire up the Zero app's /applications container, and it should all fall together.

        MountService appsMountService = new MountService();
        this.serviceContainer.addService(LiveOak.defaultMount(resource(ZeroExtension.APPLICATION_ID, "applications")), appsMountService)
                .addDependency(applicationContext(ZeroExtension.APPLICATION_ID), MountPointResource.class, appsMountService.mountPointInjector())
                .addDependency(resource(ZeroExtension.APPLICATION_ID, "applications"), RootResource.class, appsMountService.resourceInjector())
                .install();

        // now install global context, and public end should fall together.

        GlobalContext globalContext = new GlobalContext();
        this.serviceContainer.addService(GLOBAL_CONTEXT, new ValueService<GlobalContext>(new ImmediateValue<>(globalContext)))
                .install();

        this.serviceContainer.awaitStability();

        Collection<? extends Resource> appResources = appContext.getValue().members();
        assertThat(appResources).isNotNull();
        assertThat(appResources).hasSize(1);


        this.serviceContainer.awaitStability();
        appContext = (ServiceController<ApplicationContext>) this.serviceContainer.getService(defaultMount(applicationContext(ZeroExtension.APPLICATION_ID)));
        assertThat(appContext).isNotNull();
        assertThat(appContext.getMode()).isEqualTo(Mode.ACTIVE);
        assertThat(appContext.getState()).isEqualTo(State.UP);


        assertThat(globalContext.member(ZeroExtension.APPLICATION_ID)).isSameAs(appContext.getValue());
        assertThat(appContext.getValue().parent()).isSameAs(globalContext);

        assertThat(installedApp.context()).isSameAs(appContext.getValue());
        assertThat(installedApp.resource()).isSameAs(appAdmin.getValue());

    }

    @Test
    public void testBootstrapUsingExtension() throws Exception {
        GlobalContext globalContext = new GlobalContext();
        this.serviceContainer.addService(LiveOak.GLOBAL_CONTEXT, new ValueService<GlobalContext>(new ImmediateValue<>(globalContext)))
                .install();

        InternalApplicationRegistry registry = new InternalApplicationRegistry(this.serviceContainer);
        this.serviceContainer.addService(LiveOak.APPLICATION_REGISTRY, new ValueService<InternalApplicationRegistry>(new ImmediateValue<>(registry)))
                .install();

        ApplicationsDeployerService deployerService = new ApplicationsDeployerService();
        this.serviceContainer.addService(APPLICATIONS_DEPLOYER, deployerService)
                .addDependency(APPLICATIONS_DIR, File.class, deployerService.applicationsDirectoryInjector())
                .addDependency(APPLICATION_REGISTRY, InternalApplicationRegistry.class, deployerService.applicationRegistryInjector())
                .install();
<commit_msg>Fix TenancyTest due to missing application registry service<commit_after>package io.liveoak.container.tenancy;

import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import io.liveoak.container.extension.ExtensionInstaller;
import io.liveoak.container.extension.ExtensionService;
import io.liveoak.container.extension.MountService;
import io.liveoak.container.tenancy.service.ApplicationsDeployerService;
import io.liveoak.container.tenancy.service.ApplicationsDirectoryService;
import io.liveoak.container.zero.ApplicationExtensionsResource;
import io.liveoak.container.zero.ApplicationsResource;
import io.liveoak.container.zero.extension.ApplicationClientsExtension;
import io.liveoak.container.zero.extension.ZeroExtension;
import io.liveoak.spi.LiveOak;
import io.liveoak.spi.resource.RootResource;
import io.liveoak.spi.resource.async.Resource;
import org.jboss.msc.service.*;
import org.jboss.msc.service.ServiceController.Mode;
import org.jboss.msc.service.ServiceController.State;
import org.jboss.msc.value.ImmediateValue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.util.Collection;

import static io.liveoak.spi.LiveOak.*;
import static org.fest.assertions.Assertions.assertThat;

/**
 * @author <NAME>
 */
public class TenancyTest {

    private ServiceContainer serviceContainer;

    @Before
    public void setUpServiceContainer() throws Exception {
        this.serviceContainer = ServiceContainer.Factory.create();

        File appDir = new File(getClass().getClassLoader().getResource("apps").getFile());
        this.serviceContainer.addService(LiveOak.APPLICATIONS_DIR, new ApplicationsDirectoryService(appDir))
                .install();

        this.serviceContainer.addService(LiveOak.SERVICE_REGISTRY, new ValueService<>(new ImmediateValue<>(this.serviceContainer))).install();

        ExtensionInstaller installer = new ExtensionInstaller(this.serviceContainer.subTarget(), LiveOak.resource(ZeroExtension.APPLICATION_ID, "system"));
        installer.load("application-clients", new ApplicationClientsExtension());

        this.serviceContainer.awaitStability();
    }

    @After
    public void tearDownServiceContainer() {
        this.serviceContainer.shutdown();
    }

    @Test
    public void testBootstrapPiecemeal() throws InterruptedException {
        InternalApplicationRegistry registry = new InternalApplicationRegistry(this.serviceContainer);
        this.serviceContainer.addService(LiveOak.APPLICATION_REGISTRY, new ValueService<>(new ImmediateValue<>(registry)))
                .install();

        InternalApplication installedApp = registry.createApplication(ZeroExtension.APPLICATION_ID, ZeroExtension.APPLICATION_NAME);

        SimpleResourceRegistry adminMount = new SimpleResourceRegistry("admin-mount");
        this.serviceContainer.addService(LiveOak.resource(ZeroExtension.APPLICATION_ID, "applications"), new ValueService<MountPointResource>(new ImmediateValue<>(adminMount)))
                .install();

        this.serviceContainer.awaitStability();

        ServiceController<InternalApplication> app = (ServiceController<InternalApplication>) this.serviceContainer.getService(application(ZeroExtension.APPLICATION_ID));

        assertThat(app).isNotNull();
        assertThat(app.getMode()).isEqualTo(Mode.ACTIVE);
        assertThat(app.getState()).isEqualTo(State.UP);

        ServiceController<ApplicationResource> appAdmin = (ServiceController<ApplicationResource>) this.serviceContainer.getService(applicationAdminResource(ZeroExtension.APPLICATION_ID));
        assertThat(appAdmin).isNotNull();
        assertThat(appAdmin.getMode()).isEqualTo(Mode.ACTIVE);
        assertThat(appAdmin.getState()).isEqualTo(State.UP);

        ServiceController<?> appAdminMount = this.serviceContainer.getService(defaultMount(applicationAdminResource(ZeroExtension.APPLICATION_ID)));
        assertThat(appAdminMount).isNotNull();
        assertThat(appAdminMount.getMode()).isEqualTo(Mode.ACTIVE);
        assertThat(appAdminMount.getState()).isEqualTo(State.UP);  // we are mounted

        ServiceController<ApplicationContext> appContext = (ServiceController<ApplicationContext>) this.serviceContainer.getService(defaultMount(applicationContext(ZeroExtension.APPLICATION_ID)));
        assertThat(appContext).isNotNull();
        assertThat(appContext.getMode()).isEqualTo(Mode.ACTIVE);
        assertThat(appContext.getState()).isEqualTo(State.DOWN); // our public side isn't mounted

        /*
        Collection<Resource> registeredApps = orgAdmin.getValue().applicationsResource().members();
        assertThat(registeredApps).isNotNull();
        assertThat(registeredApps).hasSize(1);
        assertThat(((ApplicationResource) registeredApps.iterator().next()).application()).isSameAs(app.getValue());
        */

        // now let's wire up the Zero app's /applications container, and it should all fall together.

        MountService appsMountService = new MountService();
        this.serviceContainer.addService(LiveOak.defaultMount(resource(ZeroExtension.APPLICATION_ID, "applications")), appsMountService)
                .addDependency(applicationContext(ZeroExtension.APPLICATION_ID), MountPointResource.class, appsMountService.mountPointInjector())
                .addDependency(resource(ZeroExtension.APPLICATION_ID, "applications"), RootResource.class, appsMountService.resourceInjector())
                .install();

        // now install global context, and public end should fall together.

        GlobalContext globalContext = new GlobalContext();
        this.serviceContainer.addService(GLOBAL_CONTEXT, new ValueService<GlobalContext>(new ImmediateValue<>(globalContext)))
                .install();

        this.serviceContainer.awaitStability();

        Collection<? extends Resource> appResources = appContext.getValue().members();
        assertThat(appResources).isNotNull();
        assertThat(appResources).hasSize(1);


        this.serviceContainer.awaitStability();
        appContext = (ServiceController<ApplicationContext>) this.serviceContainer.getService(defaultMount(applicationContext(ZeroExtension.APPLICATION_ID)));
        assertThat(appContext).isNotNull();
        assertThat(appContext.getMode()).isEqualTo(Mode.ACTIVE);
        assertThat(appContext.getState()).isEqualTo(State.UP);


        assertThat(globalContext.member(ZeroExtension.APPLICATION_ID)).isSameAs(appContext.getValue());
        assertThat(appContext.getValue().parent()).isSameAs(globalContext);

        assertThat(installedApp.context()).isSameAs(appContext.getValue());
        assertThat(installedApp.resource()).isSameAs(appAdmin.getValue());

    }

    @Test
    public void testBootstrapUsingExtension() throws Exception {
        GlobalContext globalContext = new GlobalContext();
        this.serviceContainer.addService(LiveOak.GLOBAL_CONTEXT, new ValueService<>(new ImmediateValue<>(globalContext)))
                .install();

        InternalApplicationRegistry registry = new InternalApplicationRegistry(this.serviceContainer);
        this.serviceContainer.addService(LiveOak.APPLICATION_REGISTRY, new ValueService<>(new ImmediateValue<>(registry)))
                .install();

        ApplicationsDeployerService deployerService = new ApplicationsDeployerService();
        this.serviceContainer.addService(APPLICATIONS_DEPLOYER, deployerService)
                .addDependency(APPLICATIONS_DIR, File.class, deployerService.applicationsDirectoryInjector())
                .addDependency(APPLICATION_REGISTRY, InternalApplicationRegistry.class, deployerService.applicationRegistryInjector())
                .install();

{'fraction_non_alphanumeric': 0.1057534606048678, 'fraction_numerical': 0.000262415534999672, 'mean_word_length': 4.737297704177644, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7062225', 'n_tokens_mistral': 3853, 'n_tokens_neox': 3793, 'n_words': 546}