aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/test/java/com/yahoo/container/core/config/PlatformBundleLoaderTest.java
blob: 8b21e564bec71d11d207179a9617fe445c196970 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.core.config;

import com.yahoo.osgi.Osgi;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.osgi.framework.Bundle;

import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * @author gjoranv
 */
public class PlatformBundleLoaderTest {

    private static final String BUNDLE_1_REF = "bundle-1";
    private static final Bundle BUNDLE_1 = new TestBundle(BUNDLE_1_REF);
    private static final String BUNDLE_2_REF = "bundle-2";
    private static final Bundle BUNDLE_2 = new TestBundle(BUNDLE_2_REF);

    private PlatformBundleLoader bundleLoader;
    private TestOsgi osgi;

    @BeforeEach
    public void setup() {
        osgi = new TestOsgi(testBundles());
        bundleLoader = new PlatformBundleLoader(osgi, new TestBundleInstaller());
    }

    @Test
    void bundles_are_installed_and_started() {
        bundleLoader.useBundles(List.of(BUNDLE_1_REF));
        assertEquals(1, osgi.getInstalledBundles().size());

        // The bundle is installed and started
        TestBundle installedBundle = (TestBundle) osgi.getInstalledBundles().get(0);
        assertEquals(BUNDLE_1.getSymbolicName(), installedBundle.getSymbolicName());
        assertTrue(installedBundle.started);
    }

    @Test
    void bundles_cannot_be_added_by_later_calls() {
        bundleLoader.useBundles(List.of(BUNDLE_1_REF));
        bundleLoader.useBundles(List.of(BUNDLE_2_REF));  // Should be a NOP

        assertEquals(1, osgi.getInstalledBundles().size());
        assertEquals(BUNDLE_1.getSymbolicName(), osgi.getInstalledBundles().get(0).getSymbolicName());
    }

    private static Map<String, Bundle> testBundles() {
        return Map.of(BUNDLE_1_REF, BUNDLE_1,
                      BUNDLE_2_REF, BUNDLE_2);
    }

    static class TestBundleInstaller extends DiskBundleInstaller {
        @Override
        public List<Bundle> installBundles(String bundlePath, Osgi osgi) {
            return osgi.install(bundlePath);
        }
    }
}