Search in sources :

Example 46 with FeaturesService

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

the class ApplicationServiceImplTest method testStopApplicationGeneralASE.

/**
     * Tests the {@link ApplicationServiceImpl#stopApplication(Application)} method
     * for the case where an exception is caught
     *
     * @throws Exception
     */
@Test(expected = ApplicationServiceException.class)
public void testStopApplicationGeneralASE() 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 testApp1 = mock(ApplicationImpl.class);
    Feature testFeature1 = mock(Feature.class);
    when(testApp1.getFeatures()).thenReturn(new HashSet<>(Arrays.asList(testFeature1)));
    when(featuresService.isInstalled(any())).thenReturn(true);
    doThrow(new Exception()).when(featuresService).uninstallFeature(anyString(), anyString(), any());
    appService.stopApplication(testApp1);
}
Also used : Repository(org.apache.karaf.features.Repository) FeaturesService(org.apache.karaf.features.FeaturesService) Application(org.codice.ddf.admin.application.service.Application) Feature(org.apache.karaf.features.Feature) ApplicationServiceException(org.codice.ddf.admin.application.service.ApplicationServiceException) HashSet(java.util.HashSet) ApplicationService(org.codice.ddf.admin.application.service.ApplicationService) Test(org.junit.Test)

Example 47 with FeaturesService

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

the class ApplicationServiceImplTest method testStopApplicationNoMainFeature.

/**
     * Tests the {@link ApplicationServiceImpl#stopApplication(Application)} method
     * for the case where there is no main feature, but other features exist
     *
     * @throws Exception
     */
@Test
public void testStopApplicationNoMainFeature() 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 = createPermittedApplicationServiceImpl();
    Application testApp = mock(ApplicationImpl.class);
    Feature testFeature1 = mock(Feature.class);
    Feature testFeature2 = mock(Feature.class);
    Set<Feature> featureSet = new HashSet<Feature>();
    featureSet.add(testFeature1);
    featureSet.add(testFeature2);
    when(testApp.getName()).thenReturn(TEST_APP_NAME);
    when(testApp.getMainFeature()).thenReturn(null);
    when(testApp.getFeatures()).thenReturn(featureSet);
    when(testFeature1.getName()).thenReturn(TEST_FEATURE_1_NAME);
    when(testFeature2.getName()).thenReturn(TEST_FEATURE_2_NAME);
    when(testFeature1.getInstall()).thenReturn(Feature.DEFAULT_INSTALL_MODE);
    when(testFeature2.getInstall()).thenReturn(Feature.DEFAULT_INSTALL_MODE);
    when(featuresService.isInstalled(testFeature1)).thenReturn(true);
    when(featuresService.isInstalled(testFeature2)).thenReturn(true);
    appService.stopApplication(testApp);
    verify(featuresService, atLeastOnce()).uninstallFeature(TEST_FEATURE_1_NAME, null, EnumSet.of(Option.NoAutoRefreshBundles));
    verify(featuresService, atLeastOnce()).uninstallFeature(TEST_FEATURE_2_NAME, null, EnumSet.of(Option.NoAutoRefreshBundles));
}
Also used : Repository(org.apache.karaf.features.Repository) 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 48 with FeaturesService

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

the class ApplicationServiceImplTest method getAppServiceWithBundleStateServiceInGivenState.

/**
     * Returns an {@code ApplicationService} that contains one bundle for which
     * the extended bundle state reported by an injection framework is the
     * received {@code BundleState} and the extended bundle state for the rest
     * of the bundles in {@code BundleState#Active}.
     *
     * @param repository  The {@link Repository} from which to build the
     *                    {@code ApplicationService}.
     * @param bundleState The {@link BundleState} to set for one of the {@code Bundle}s
     *                    in the {@code ApplicationService}
     * @return An {@code ApplicationService} with one bundle's extended state
     * set to the received bundle state and the rest of the
     * {@code Bundle}s set to {@code Bundle#ACTIVE}
     * @throws Exception
     */
private ApplicationService getAppServiceWithBundleStateServiceInGivenState(Repository repository, BundleState bundleState) throws Exception {
    ApplicationService appService = null;
    FeaturesService featuresService = createMockFeaturesService(repository, null, null);
    when(bundleContext.getService(mockFeatureRef)).thenReturn(featuresService);
    Bundle bundle = getAnyBundleFromFeaturesService(featuresService);
    if (null != bundle) {
        when(bundleStateServices.get(0).getState(bundle)).thenReturn(bundleState);
        appService = createPermittedApplicationServiceImpl();
    }
    return appService;
}
Also used : Bundle(org.osgi.framework.Bundle) FeaturesService(org.apache.karaf.features.FeaturesService) ApplicationService(org.codice.ddf.admin.application.service.ApplicationService)

Example 49 with FeaturesService

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

the class ApplicationServiceImplTest method testGetAllFeaturesException.

/**
     * Tests the {@link ApplicationServiceImpl#getAllFeatures()} method
     * for the case where an exception is thrown by the featuresService
     *
     * @throws Exception
     */
// TODO RAP 29 Aug 16: DDF-2443 - Fix test to not depend on specific log output
@Test
public void testGetAllFeaturesException() 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();
    doThrow(new NullPointerException()).when(featuresService).listFeatures();
    appService.getAllFeatures();
    verify(mockAppender).doAppend(argThat(new ArgumentMatcher() {

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

Example 50 with FeaturesService

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

the class ApplicationServiceImplTest method testStartApplicationStringParam.

/**
     * Tests the {@link ApplicationServiceImpl#startApplication(String)} method
     *
     * @throws Exception
     */
@Test
public void testStartApplicationStringParam() 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);
    when(featuresService.isInstalled(any(Feature.class))).thenReturn(false);
    ApplicationService appService = createPermittedApplicationServiceImpl();
    appService.startApplication(TEST_APP);
    Set<String> names = new HashSet<>();
    names.add(mainFeatureRepo.getFeatures()[0].getName());
    names.add(mainFeatureRepo.getFeatures()[1].getName());
    verify(featuresService).installFeatures(names, EnumSet.of(Option.NoAutoRefreshBundles));
}
Also used : Repository(org.apache.karaf.features.Repository) FeaturesService(org.apache.karaf.features.FeaturesService) Mockito.anyString(org.mockito.Mockito.anyString) Feature(org.apache.karaf.features.Feature) 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