summaryrefslogtreecommitdiffstats
path: root/container-core/src/test/java/com/yahoo/container/core/config/ApplicationBundleLoaderTest.java
blob: bc5b6d6069bc728071d2a192bca06423117eb479 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.core.config;

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

import java.util.Collection;
import java.util.List;
import java.util.Set;

import static com.yahoo.container.core.config.BundleTestUtil.BUNDLE_1;
import static com.yahoo.container.core.config.BundleTestUtil.BUNDLE_1_REF;
import static com.yahoo.container.core.config.BundleTestUtil.BUNDLE_2;
import static com.yahoo.container.core.config.BundleTestUtil.BUNDLE_2_REF;
import static com.yahoo.container.core.config.BundleTestUtil.testBundles;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

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

    private ApplicationBundleLoader bundleLoader;
    private TestOsgi osgi;

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

    @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);

        // The file reference is active
        assertEquals(1, bundleLoader.getActiveFileReferences().size());
        assertEquals(BUNDLE_1_REF, bundleLoader.getActiveFileReferences().get(0));
    }

    @Test
    void new_bundle_can_be_installed_in_reconfig() {
        bundleLoader.useBundles(List.of(BUNDLE_1_REF));
        Set<Bundle> obsoleteBundles = bundleLoader.useBundles(List.of(BUNDLE_1_REF, BUNDLE_2_REF));

        // No bundles are obsolete
        assertTrue(obsoleteBundles.isEmpty());

        // Both bundles are installed
        assertEquals(2, osgi.getInstalledBundles().size());
        assertEquals(BUNDLE_1.getSymbolicName(), osgi.getInstalledBundles().get(0).getSymbolicName());
        assertEquals(BUNDLE_2.getSymbolicName(), osgi.getInstalledBundles().get(1).getSymbolicName());

        // Both bundles are current
        assertEquals(2, osgi.getCurrentBundles().size());
        assertEquals(BUNDLE_1.getSymbolicName(), osgi.getCurrentBundles().get(0).getSymbolicName());
        assertEquals(BUNDLE_2.getSymbolicName(), osgi.getCurrentBundles().get(1).getSymbolicName());

        // Both file references are active
        assertEquals(2, bundleLoader.getActiveFileReferences().size());
        assertEquals(BUNDLE_1_REF, bundleLoader.getActiveFileReferences().get(0));
        assertEquals(BUNDLE_2_REF, bundleLoader.getActiveFileReferences().get(1));
    }

    @Test
    void unused_bundle_is_marked_obsolete_after_reconfig() {
        bundleLoader.useBundles(List.of(BUNDLE_1_REF));
        Set<Bundle> obsoleteBundles = bundleLoader.useBundles(List.of(BUNDLE_2_REF));

        // The returned set of obsolete bundles contains bundle-1
        assertEquals(1, obsoleteBundles.size());
        assertEquals(BUNDLE_1.getSymbolicName(), obsoleteBundles.iterator().next().getSymbolicName());

        // Both bundles are installed
        assertEquals(2, osgi.getInstalledBundles().size());
        assertEquals(BUNDLE_1.getSymbolicName(), osgi.getInstalledBundles().get(0).getSymbolicName());
        assertEquals(BUNDLE_2.getSymbolicName(), osgi.getInstalledBundles().get(1).getSymbolicName());

        // Only bundle-2 is current
        assertEquals(1, osgi.getCurrentBundles().size());
        assertEquals(BUNDLE_2.getSymbolicName(), osgi.getCurrentBundles().get(0).getSymbolicName());

        // Only the bundle-2 file reference is active
        assertEquals(1, bundleLoader.getActiveFileReferences().size());
        assertEquals(BUNDLE_2_REF, bundleLoader.getActiveFileReferences().get(0));
    }


    @Test
    void previous_generation_can_be_restored_after_a_failed_reconfig() {
        bundleLoader.useBundles(List.of(BUNDLE_1_REF));
        Set<Bundle> obsoleteBundles = bundleLoader.useBundles(List.of(BUNDLE_2_REF));
        assertEquals(BUNDLE_1.getSymbolicName(), obsoleteBundles.iterator().next().getSymbolicName());

        // Revert to the previous generation, as will be done upon a failed reconfig.
        Collection<Bundle> bundlesToUninstall = bundleLoader.revertToPreviousGeneration();

        assertEquals(1, bundlesToUninstall.size());
        assertEquals(BUNDLE_2.getSymbolicName(), bundlesToUninstall.iterator().next().getSymbolicName());

        // Both bundles are still current, until the bundle from the failed gen will be uninstalled.
        // Uninstalling is handled by the Deconstructor, not included in this test setup.
        assertEquals(2, osgi.getCurrentBundles().size());

        // Only the bundle-1 file reference is active, bundle-2 is removed.
        assertEquals(1, bundleLoader.getActiveFileReferences().size());
        assertEquals(BUNDLE_1_REF, bundleLoader.getActiveFileReferences().get(0));
    }

}