Search in sources :

Example 11 with Repository

use of org.apache.karaf.features.Repository 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 12 with Repository

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

the class ApplicationServiceImplTest method testIgnoreApplications.

/**
     * Tests that the service properly ignores applications when checking for
     * application status.
     *
     * @throws Exception
     */
@Test
public void testIgnoreApplications() throws Exception {
    Set<Repository> activeRepos = new HashSet<Repository>(Arrays.asList(noMainFeatureRepo1, noMainFeatureRepo2));
    FeaturesService featuresService = createMockFeaturesService(activeRepos, null, null);
    when(bundleContext.getService(mockFeatureRef)).thenReturn(featuresService);
    ApplicationServiceImpl appService = createPermittedApplicationServiceImpl();
    // just ignore the first application
    List<String> ignoredApps1 = new ArrayList<>(1);
    ignoredApps1.add(TEST_APP);
    appService.setIgnoredApplications(ignoredApps1);
    Set<Application> applications = appService.getApplications();
    assertNotNull(applications);
    assertEquals(1, applications.size());
    assertEquals(TEST_APP2, applications.iterator().next().getName());
    // now ignore both applications
    List<String> ignoredApps2 = new ArrayList<>(2);
    ignoredApps2.add(TEST_APP);
    ignoredApps2.add(TEST_APP2);
    appService.setIgnoredApplications(ignoredApps2);
    applications = appService.getApplications();
    assertNotNull(applications);
    assertEquals(0, applications.size());
    // ignore none
    List<String> ignoredApps3 = new ArrayList<>(0);
    appService.setIgnoredApplications(ignoredApps3);
    applications = appService.getApplications();
    assertNotNull(applications);
    assertEquals(2, applications.size());
}
Also used : Repository(org.apache.karaf.features.Repository) ArrayList(java.util.ArrayList) FeaturesService(org.apache.karaf.features.FeaturesService) Mockito.anyString(org.mockito.Mockito.anyString) Application(org.codice.ddf.admin.application.service.Application) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 13 with Repository

use of org.apache.karaf.features.Repository 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 14 with Repository

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

the class ApplicationServiceImplTest method testServiceChanged.

/**
     * Tests  the {@link ApplicationServiceImpl#setConfigFileName(String)} method
     *
     * @throws Exception
     */
@Test
public void testServiceChanged() throws Exception {
    Set<Repository> activeRepos = new HashSet<>(Arrays.asList(mainFeatureRepo, noMainFeatureRepo1));
    FeaturesService featuresService = createMockFeaturesService(activeRepos, null, null);
    when(bundleContext.getService(mockFeatureRef)).thenReturn(featuresService);
    ApplicationServiceImpl appService = new ApplicationServiceImpl(bundleStateServices) {

        @Override
        protected BundleContext getContext() {
            return bundleContext;
        }
    };
    appService.setConfigFileName("foo");
    ServiceReference<ConfigurationAdmin> testConfigAdminRef = mock(ServiceReference.class);
    ConfigurationAdmin testConfigAdmin = mock(ConfigurationAdmin.class);
    Configuration testConfig = mock(Configuration.class);
    when(bundleContext.getServiceReference(ConfigurationAdmin.class)).thenReturn(testConfigAdminRef);
    when(bundleContext.getService(testConfigAdminRef)).thenReturn(testConfigAdmin);
    when(testConfigAdmin.getConfiguration(ApplicationServiceImpl.class.getName())).thenReturn(testConfig);
    Dictionary<String, Object> testProperties = new Hashtable<>();
    testProperties.put("test1", "foo");
    testProperties.put("test2", "bar");
    when(testConfig.getProperties()).thenReturn(testProperties);
    ServiceEvent serviceEvent = mock(ServiceEvent.class);
    when(serviceEvent.getType()).thenReturn(ServiceEvent.REGISTERED);
    appService.serviceChanged(serviceEvent);
    assertThat(testConfig.getProperties().size(), is(testProperties.size()));
    assertThat(testConfig.getProperties().get("test1"), is("foo"));
}
Also used : Configuration(org.osgi.service.cm.Configuration) Hashtable(java.util.Hashtable) Mockito.anyString(org.mockito.Mockito.anyString) Repository(org.apache.karaf.features.Repository) ServiceEvent(org.osgi.framework.ServiceEvent) FeaturesService(org.apache.karaf.features.FeaturesService) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 15 with Repository

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

the class ApplicationServiceImplTest method testStopApplicationNoMainFeatureStringParam.

/**
     * Tests the {@link ApplicationServiceImpl#stopApplication(String)} method
     * for the case where there is no main feature, but other features exist
     *
     * @throws Exception
     */
@Test
public void testStopApplicationNoMainFeatureStringParam() 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();
    Feature[] featureList = noMainFeatureRepo1.getFeatures();
    appService.stopApplication(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)

Aggregations

Repository (org.apache.karaf.features.Repository)84 Test (org.junit.Test)52 HashSet (java.util.HashSet)51 FeaturesService (org.apache.karaf.features.FeaturesService)44 Feature (org.apache.karaf.features.Feature)38 ApplicationService (org.codice.ddf.admin.application.service.ApplicationService)37 URI (java.net.URI)20 Application (org.codice.ddf.admin.application.service.Application)20 LinkedHashSet (java.util.LinkedHashSet)9 ArrayList (java.util.ArrayList)8 ApplicationServiceException (org.codice.ddf.admin.application.service.ApplicationServiceException)8 Logger (org.slf4j.Logger)7 Appender (ch.qos.logback.core.Appender)6 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 ArgumentMatcher (org.mockito.ArgumentMatcher)6 Mockito.anyString (org.mockito.Mockito.anyString)6 EnumSet (java.util.EnumSet)5 Map (java.util.Map)5 TreeMap (java.util.TreeMap)4