Search in sources :

Example 11 with Feature

use of org.apache.karaf.features.Feature in project ddf by codice.

the class ApplicationServiceImplTest method testRemoveApplicationUninstallAllFeaturesException.

/**
     * Tests the {@link ApplicationServiceImpl#removeApplication(Application)} method
     * for the case where an exception is thrown within uninstallAllFeatures(Application)
     *
     * @throws Exception
     */
// TODO RAP 29 Aug 16: DDF-2443 - Fix test to not depend on specific log output
@Test
public void testRemoveApplicationUninstallAllFeaturesException() throws Exception {
    ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
    final Appender mockAppender = mock(Appender.class);
    when(mockAppender.getName()).thenReturn("MOCK");
    root.addAppender(mockAppender);
    Set<Repository> activeRepos = new HashSet<Repository>(Arrays.asList(mainFeatureRepo, noMainFeatureRepo1, noMainFeatureRepo2));
    FeaturesService featuresService = createMockFeaturesService(activeRepos, null, null);
    when(bundleContext.getService(mockFeatureRef)).thenReturn(featuresService);
    ApplicationService appService = createPermittedApplicationServiceImpl();
    Application testApp = mock(ApplicationImpl.class);
    Feature testFeature1 = mock(Feature.class);
    Feature testFeature2 = mock(Feature.class);
    Set<Feature> featureSet = new HashSet<>();
    featureSet.add(testFeature1);
    featureSet.add(testFeature2);
    when(featuresService.isInstalled(any(Feature.class))).thenReturn(true);
    when(testApp.getFeatures()).thenReturn(featureSet);
    doThrow(new Exception()).when(featuresService).uninstallFeature(anyString(), anyString(), any(EnumSet.class));
    appService.removeApplication(testApp);
    verify(mockAppender, times(2)).doAppend(argThat(new ArgumentMatcher() {

        @Override
        public boolean matches(final Object argument) {
            return ((LoggingEvent) argument).getFormattedMessage().contains(UNINSTALL_FAIL);
        }
    }));
}
Also used : Appender(ch.qos.logback.core.Appender) EnumSet(java.util.EnumSet) Logger(org.slf4j.Logger) Feature(org.apache.karaf.features.Feature) ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException) Repository(org.apache.karaf.features.Repository) ArgumentMatcher(org.mockito.ArgumentMatcher) FeaturesService(org.apache.karaf.features.FeaturesService) Application(org.codice.ddf.admin.application.service.Application) HashSet(java.util.HashSet) ApplicationService(org.codice.ddf.admin.application.service.ApplicationService) Test(org.junit.Test)

Example 12 with Feature

use of org.apache.karaf.features.Feature in project ddf by codice.

the class ApplicationServiceImplTest method testRemoveApplicationStringParam.

/**
     * Tests the {@link ApplicationServiceImpl#removeApplication(String)} method
     *
     * @throws Exception
     */
@Test
public void testRemoveApplicationStringParam() throws Exception {
    Set<Repository> activeRepos = new HashSet<Repository>(Arrays.asList(mainFeatureRepo, noMainFeatureRepo1));
    FeaturesService featuresService = createMockFeaturesService(activeRepos, null, null);
    when(bundleContext.getService(mockFeatureRef)).thenReturn(featuresService);
    ApplicationService appService = createPermittedApplicationServiceImpl();
    Feature[] featureList = mainFeatureRepo.getFeatures();
    appService.removeApplication(TEST_APP);
    verify(featuresService).uninstallFeature(featureList[0].getName(), featureList[0].getVersion(), EnumSet.of(Option.NoAutoRefreshBundles));
    verify(featuresService).uninstallFeature(featureList[1].getName(), featureList[1].getVersion(), EnumSet.of(Option.NoAutoRefreshBundles));
}
Also used : Repository(org.apache.karaf.features.Repository) FeaturesService(org.apache.karaf.features.FeaturesService) Feature(org.apache.karaf.features.Feature) HashSet(java.util.HashSet) ApplicationService(org.codice.ddf.admin.application.service.ApplicationService) Test(org.junit.Test)

Example 13 with Feature

use of org.apache.karaf.features.Feature in project ddf by codice.

the class ApplicationServiceImplTest method createMockFeaturesService.

/**
     * Creates a mock {@code FeaturesService} object consisting of all of the
     * features contained in a {@code Repository} object.
     *
     * @param repo
     * @param notInstalledFeatureNames
     * @param inactiveBundleLocations
     * @return
     * @throws Exception
     * @see #createMockFeaturesService(Set, Set, Set) for additional details
     */
private FeaturesService createMockFeaturesService(Repository repo, Set<String> notInstalledFeatureNames, Set<String> inactiveBundleLocations) throws Exception {
    Set<Feature> notInstalledFeatures = new HashSet<Feature>();
    Set<BundleInfo> inactiveBundles = new HashSet<BundleInfo>();
    for (Feature feature : repo.getFeatures()) {
        if (null != notInstalledFeatureNames && notInstalledFeatureNames.contains(feature.getName())) {
            notInstalledFeatures.add(feature);
        }
        if (null != inactiveBundleLocations) {
            for (BundleInfo bundleInfo : feature.getBundles()) {
                if (inactiveBundleLocations.contains(bundleInfo.getLocation())) {
                    inactiveBundles.add(bundleInfo);
                }
            }
        }
    }
    Set<Repository> repoSet = new HashSet<Repository>();
    repoSet.add(repo);
    return createMockFeaturesService(repoSet, notInstalledFeatures, inactiveBundles);
}
Also used : Repository(org.apache.karaf.features.Repository) BundleInfo(org.apache.karaf.features.BundleInfo) Feature(org.apache.karaf.features.Feature) HashSet(java.util.HashSet)

Example 14 with Feature

use of org.apache.karaf.features.Feature in project ddf by codice.

the class ApplicationServiceImplTest method testStartApplicationASE.

/**
     * Tests the {@link ApplicationServiceImpl#startApplication(Application)} method
     * for the case where an exception is thrown
     *
     * @throws Exception
     */
@Test(expected = ApplicationServiceException.class)
public void testStartApplicationASE() throws Exception {
    Set<Repository> activeRepos = new HashSet<Repository>(Arrays.asList(mainFeatureRepo, noMainFeatureRepo1, noMainFeatureRepo2));
    FeaturesService featuresService = createMockFeaturesService(activeRepos, null, null);
    when(bundleContext.getService(mockFeatureRef)).thenReturn(featuresService);
    ApplicationService appService = new ApplicationServiceImpl(bundleStateServices) {

        @Override
        protected BundleContext getContext() {
            return bundleContext;
        }
    };
    Application testApp = mock(ApplicationImpl.class);
    Feature testFeature = mock(Feature.class);
    when(testFeature.getName()).thenReturn(TEST_FEATURE_1_NAME);
    when(testApp.getAutoInstallFeatures()).thenReturn(new HashSet<>(Arrays.asList(testFeature)));
    doThrow(new ApplicationServiceException()).when(featuresService).installFeatures(anySet(), any(EnumSet.class));
    appService.startApplication(testApp);
}
Also used : ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException) Repository(org.apache.karaf.features.Repository) EnumSet(java.util.EnumSet) FeaturesService(org.apache.karaf.features.FeaturesService) Application(org.codice.ddf.admin.application.service.Application) Feature(org.apache.karaf.features.Feature) HashSet(java.util.HashSet) ApplicationService(org.codice.ddf.admin.application.service.ApplicationService) Test(org.junit.Test)

Example 15 with Feature

use of org.apache.karaf.features.Feature in project ddf by codice.

the class ApplicationServiceImplTest method getXBundlesFromFeaturesService.

/**
     * Retrieves a given number of {@code Bundle}s from the received
     * {@code FeaturesService}
     *
     * @param featuresService The {@link FeaturesService} from which to obtain a
     *                        {@code Bundle}
     * @param numBundles      The number of bundles to be retrieved from the
     *                        {@code FeaturesService}
     * @return A {@code Set} containing the requested number of {@link Bundle}s
     * from the received {@code FeaturesService} or <code>null</code> if
     * the {@code FeaturesService} does not contain the requested number
     * of {@code Bundle}s
     * @throws Exception
     */
private Set<Bundle> getXBundlesFromFeaturesService(FeaturesService featuresService, int numBundles) throws Exception {
    Set<Bundle> bundles = new HashSet<Bundle>();
    // BundleInfo bundleInfo = null;
    Bundle bundle = null;
    Feature[] features = featuresService.listFeatures();
    List<BundleInfo> bundleInfos = null;
    int ii = 0;
    while (bundles.size() < numBundles && ii < features.length) {
        bundleInfos = features[ii].getBundles();
        int jj = 0;
        while (bundles.size() < numBundles && jj < bundleInfos.size()) {
            bundle = bundleContext.getBundle(bundleInfos.get(ii).getLocation());
            if (null != bundle) {
                bundles.add(bundle);
            }
            ++jj;
        }
        ++ii;
    }
    if (bundles.size() < numBundles) {
        bundles = null;
    }
    return bundles;
}
Also used : BundleInfo(org.apache.karaf.features.BundleInfo) Bundle(org.osgi.framework.Bundle) Feature(org.apache.karaf.features.Feature) HashSet(java.util.HashSet)

Aggregations

Feature (org.apache.karaf.features.Feature)130 Test (org.junit.Test)57 FeaturesService (org.apache.karaf.features.FeaturesService)44 HashSet (java.util.HashSet)41 Repository (org.apache.karaf.features.Repository)39 ApplicationService (org.codice.ddf.admin.application.service.ApplicationService)18 Bundle (org.osgi.framework.Bundle)18 HashMap (java.util.HashMap)17 ArrayList (java.util.ArrayList)16 Map (java.util.Map)16 Dependency (org.apache.karaf.features.Dependency)15 BundleInfo (org.apache.karaf.features.BundleInfo)14 Application (org.codice.ddf.admin.application.service.Application)14 URI (java.net.URI)13 VersionRange (org.apache.felix.utils.version.VersionRange)12 IOException (java.io.IOException)11 LinkedHashSet (java.util.LinkedHashSet)11 Version (org.osgi.framework.Version)11 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)10 EnumSet (java.util.EnumSet)9