Search in sources :

Example 6 with FeaturesService

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

the class ServiceManagerImpl method waitForFeature.

@Override
public void waitForFeature(String featureName, Predicate<FeatureState> predicate) throws Exception {
    boolean ready = false;
    long timeoutLimit = System.currentTimeMillis() + FEATURES_AND_BUNDLES_TIMEOUT;
    FeaturesService featuresService = getFeaturesService();
    while (!ready) {
        FeatureState state = null;
        if (featuresService != null) {
            Feature feature = featuresService.getFeature(featureName);
            state = featuresService.getState(feature.getName() + "/" + feature.getVersion());
            if (state == null) {
                LOGGER.debug("No Feature found for featureName: {}", featureName);
                return;
            } else if (predicate.test(state)) {
                ready = true;
            }
        }
        if (!ready) {
            if (System.currentTimeMillis() > timeoutLimit) {
                printInactiveBundles();
                fail(String.format("Feature did not change to State [" + predicate + "] within %d minutes.", TimeUnit.MILLISECONDS.toMinutes(FEATURES_AND_BUNDLES_TIMEOUT)));
            }
            LOGGER.info("Still waiting on feature [{}], current state [{}]...", featureName, state);
            Thread.sleep(1000);
        }
    }
}
Also used : FeaturesService(org.apache.karaf.features.FeaturesService) Feature(org.apache.karaf.features.Feature) FeatureState(org.apache.karaf.features.FeatureState)

Example 7 with FeaturesService

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

the class ProfileInstallCommandTest method testUninstallInstallerFailure.

@Test(expected = Exception.class)
public void testUninstallInstallerFailure() throws Exception {
    Feature installerFeature = createMockFeature("admin-modules-installer");
    this.featuresService = mock(FeaturesService.class);
    when(featuresService.getFeature(anyString())).thenReturn(installerFeature);
    when(featuresService.isInstalled(installerFeature)).thenReturn(true);
    doThrow(Exception.class).when(featuresService).uninstallFeature("admin-modules-installer", NO_AUTO_REFRESH);
    profileInstallCommand.profileName = "invalidStopBundles";
    profileInstallCommand.doExecute(applicationService, featuresService, bundleService);
}
Also used : FeaturesService(org.apache.karaf.features.FeaturesService) Feature(org.apache.karaf.features.Feature) Test(org.junit.Test)

Example 8 with FeaturesService

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

the class ApplicationServiceImplTest method testIsApplicationStartedReturnsFalseForFailedApplicationState.

/**
     * Test method for
     * {@link ApplicationServiceImpl#isApplicationStarted(Application)}
     * <p>
     * Verifies that method returns false when application state is
     * {@link ApplicationState#FAILED}
     *
     * @throws Exception
     */
@Test
public void testIsApplicationStartedReturnsFalseForFailedApplicationState() throws Exception {
    Set<String> inactiveBundles = new HashSet<String>();
    inactiveBundles.add(TEST_MAIN_FEATURES_2_UNIQUE_BUNDLE_LOCATION);
    FeaturesService featuresService = createMockFeaturesService(mainFeatureRepo2, null, inactiveBundles);
    when(bundleContext.getService(mockFeatureRef)).thenReturn(featuresService);
    when(featuresService.isInstalled(mainFeatureRepo2.getFeatures()[0])).thenReturn(false);
    ApplicationService appService = createPermittedApplicationServiceImpl();
    assertFalse(appService.isApplicationStarted(appService.getApplication(TEST_MAIN_FEATURES_2_MAIN_FEATURE_NAME)));
}
Also used : FeaturesService(org.apache.karaf.features.FeaturesService) Mockito.anyString(org.mockito.Mockito.anyString) HashSet(java.util.HashSet) ApplicationService(org.codice.ddf.admin.application.service.ApplicationService) Test(org.junit.Test)

Example 9 with FeaturesService

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

the class ApplicationServiceImplTest method testGetApplicationStatusReturnsFailedStatusWhenOneFeatureConsistingOfInactiveBundlesIsNotInstalled.

/**
     * Test method for
     * {@link ApplicationService#getApplicationStatus(Application)}
     * <p>
     * Verifies method returns an {@link ApplicationState#FAILED } state for an
     * {@code Application} under the following conditions:
     * <p>
     * <ul>
     * <li>Main feature is installed</li>
     * <li>One dependency feature is not installed</li>
     * <li>Dependency feature that is not installed contains Bundle(s) whose
     * states and extended states are inactive</li>
     * </ul>
     *
     * @throws Exception
     */
@Test
public void testGetApplicationStatusReturnsFailedStatusWhenOneFeatureConsistingOfInactiveBundlesIsNotInstalled() throws Exception {
    Set<Repository> mainFeaturesRepoSet = new HashSet<Repository>();
    Set<Feature> notInstalledFeatures = new HashSet<Feature>();
    Set<BundleInfo> inactiveBundles = new HashSet<BundleInfo>();
    for (Feature feature : mainFeatureRepo2.getFeatures()) {
        if (feature.getName().equals(TEST_MAIN_FEATURES_2_MAIN_FEATURE_NAME)) {
            notInstalledFeatures.add(feature);
            inactiveBundles.addAll(feature.getBundles());
            break;
        }
    }
    assertEquals("Feature is not included in repository the expected number of times", 1, notInstalledFeatures.size());
    assertTrue("No bundles included in Feature", inactiveBundles.size() > 0);
    mainFeaturesRepoSet.add(mainFeatureRepo2);
    FeaturesService featuresService = createMockFeaturesService(mainFeaturesRepoSet, notInstalledFeatures, inactiveBundles);
    when(bundleContext.getService(mockFeatureRef)).thenReturn(featuresService);
    ApplicationService appService = createPermittedApplicationServiceImpl();
    assertEquals(ApplicationService.class.getName() + " does not contain the expected number of Applications", 1, appService.getApplications().size());
    assertEquals(mainFeatureRepo2.getName() + " returned unexpected state", ApplicationState.FAILED, appService.getApplicationStatus(appService.getApplications().toArray(new Application[] {})[0]).getState());
}
Also used : Repository(org.apache.karaf.features.Repository) BundleInfo(org.apache.karaf.features.BundleInfo) 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 10 with FeaturesService

use of org.apache.karaf.features.FeaturesService 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)

Aggregations

FeaturesService (org.apache.karaf.features.FeaturesService)71 Test (org.junit.Test)61 ApplicationService (org.codice.ddf.admin.application.service.ApplicationService)51 HashSet (java.util.HashSet)48 Repository (org.apache.karaf.features.Repository)43 Feature (org.apache.karaf.features.Feature)29 Application (org.codice.ddf.admin.application.service.Application)19 Mockito.anyString (org.mockito.Mockito.anyString)13 Logger (org.slf4j.Logger)12 Appender (ch.qos.logback.core.Appender)10 URI (java.net.URI)10 ArgumentMatcher (org.mockito.ArgumentMatcher)10 ApplicationServiceException (org.codice.ddf.admin.application.service.ApplicationServiceException)9 EnumSet (java.util.EnumSet)6 Bundle (org.osgi.framework.Bundle)6 URL (java.net.URL)5 ArrayList (java.util.ArrayList)4 BundleInfo (org.apache.karaf.features.BundleInfo)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2