use of org.apache.karaf.features.FeaturesService 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 Set} of {@code Repository} objects. Each
* {@code Feature} will be in the <i>installed</i> state unless it is
* contained in the received set of features that are not to be installed.
* Each {@code Bundle} will be in the {@code Bundle#ACTIVE} state and the
* {@code BundleState#Active} extended bundle state (as reported by a
* dependency injection framework) unless it is contained in the received
* set of {@code Bundle}s that are not to be active, in which case the
* {@code Bundle} will be in the {@code Bundle#INSTALLED} state and the
* {@code BundleState#Installed} extended bundle state.
* <p>
* Note that not all of the state and {@code Bundle} information is
* contained in the {@code FeaturesService}. As such, this method stores
* some of the required information in the class's {@code #bundleContext}
* and {@code bundleStateServices}. As such, these objects must be
* re-instantiated for each test (i.e., they must be instantiated in the
* {@link #setUp()} method).
*
* @param repos A {@code Set} of {@link Repository} objects from which to
* obtain the {@link Feature}s that are to be included in the
* mock {@code FeaturesService}
* @param notInstalledFeatures A {@code Set} of {@code Feature}s that the
* {@code FeaturesService} should report as not installed
* @param inactiveBundles A {@code Set} of {@link BundleInfo}s containing the locations
* of {@code Bundle}s that should be set to inactive and for
* which the {@link BundleStateService} contained in index 0 of
* {@link #bundleStateServices} should report a
* {@link BundleState#Installed} state.
* @return A mock {@link FeaturesService} with {@link Feature}s and
* {@link Bundle}s in the requested states.
* @throws Exception
*/
private FeaturesService createMockFeaturesService(Set<Repository> repos, Set<Feature> notInstalledFeatures, Set<BundleInfo> inactiveBundles) throws Exception {
if (LOGGER.isTraceEnabled()) {
for (Repository repo : repos) {
for (Feature feature : repo.getFeatures()) {
LOGGER.trace("Repo Feature: {}", feature);
LOGGER.trace("Repo Feature name/version: {}/{}", feature.getName(), feature.getVersion());
LOGGER.trace("Dependencies: ");
for (Dependency depFeature : feature.getDependencies()) {
LOGGER.trace("Dependency Feature: {}", depFeature);
LOGGER.trace("Dependency Feature name/version: {}/{}", depFeature.getName(), depFeature.getVersion());
}
}
}
}
if (null == notInstalledFeatures) {
notInstalledFeatures = new HashSet<Feature>();
}
if (null == inactiveBundles) {
inactiveBundles = new HashSet<BundleInfo>();
}
Set<String> installedBundleLocations = new HashSet<String>();
for (BundleInfo bundleInfo : inactiveBundles) {
installedBundleLocations.add(bundleInfo.getLocation());
}
FeaturesService featuresService = mock(FeaturesService.class);
Set<Feature> featuresSet = new HashSet<Feature>();
BundleRevision mockBundleRevision = mock(BundleRevision.class);
when(mockBundleRevision.getTypes()).thenReturn(0);
for (Repository curRepo : repos) {
for (Feature curFeature : curRepo.getFeatures()) {
featuresSet.add(curFeature);
when(featuresService.getFeature(curFeature.getName())).thenReturn(curFeature);
when(featuresService.getFeature(curFeature.getName(), curFeature.getVersion())).thenReturn(curFeature);
// TODO: File Karaf bug that necessitates this, then reference
// it here.
when(featuresService.getFeature(curFeature.getName(), "0.0.0")).thenReturn(curFeature);
when(featuresService.isInstalled(curFeature)).thenReturn(!notInstalledFeatures.contains(curFeature));
// of that bundle, this logic will need to be modified.
for (BundleInfo bundleInfo : curFeature.getBundles()) {
if (installedBundleLocations.contains(bundleInfo.getLocation())) {
Bundle mockInstalledBundle = mock(Bundle.class);
when(mockInstalledBundle.getState()).thenReturn(Bundle.INSTALLED);
when(mockInstalledBundle.adapt(BundleRevision.class)).thenReturn(mockBundleRevision);
when(bundleContext.getBundle(bundleInfo.getLocation())).thenReturn(mockInstalledBundle);
when(bundleStateServices.get(0).getState(mockInstalledBundle)).thenReturn(BundleState.Installed);
} else {
Bundle mockActiveBundle = mock(Bundle.class);
when(mockActiveBundle.getState()).thenReturn(Bundle.ACTIVE);
when(mockActiveBundle.adapt(BundleRevision.class)).thenReturn(mockBundleRevision);
when(bundleContext.getBundle(bundleInfo.getLocation())).thenReturn(mockActiveBundle);
when(bundleStateServices.get(0).getState(mockActiveBundle)).thenReturn(BundleState.Active);
}
}
}
}
when(featuresService.listRepositories()).thenReturn(repos.toArray(new Repository[repos.size()]));
when(featuresService.listFeatures()).thenReturn(featuresSet.toArray(new Feature[] {}));
return featuresService;
}
use of org.apache.karaf.features.FeaturesService in project ddf by codice.
the class ApplicationServiceImplTest method testRemoveApplicationApplicationParamASE.
/**
* Tests the {@link ApplicationServiceImpl#removeApplication(Application)} method
* for the case where an exception is thrown
*
* @throws Exception
*/
@Test(expected = ApplicationServiceException.class)
public void testRemoveApplicationApplicationParamASE() 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);
doThrow(new Exception()).when(featuresService).removeRepository(Mockito.any(URI.class), eq(false));
appService.removeApplication(testApp);
}
use of org.apache.karaf.features.FeaturesService in project ddf by codice.
the class ApplicationServiceImplTest method testFindFeature.
/**
* Tests the {@link ApplicationServiceImpl#findFeature(Feature)} method
*
* @throws Exception
*/
@Test
public void testFindFeature() 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 testFeature = mainFeatureRepo.getFeatures()[0];
Application result = appService.findFeature(testFeature);
assertTrue("Check that the returned application is the correct one.", result.getFeatures().contains(testFeature));
}
use of org.apache.karaf.features.FeaturesService in project ddf by codice.
the class ApplicationServiceImplTest method testStartApplicationNoMainFeature.
/**
* Tests the {@link ApplicationServiceImpl#startApplication(Application)} method
* for the case where there is no main feature, but other features exist
*
* @throws Exception
*/
@Test
public void testStartApplicationNoMainFeature() 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 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.getAutoInstallFeatures()).thenReturn(featureSet);
when(testFeature1.getName()).thenReturn(TEST_FEATURE_1_NAME);
when(testFeature2.getName()).thenReturn(TEST_FEATURE_2_NAME);
Set<String> featureNames = new HashSet<>(featureSet.size());
featureSet.forEach(f -> featureNames.add(f.getName()));
appService.startApplication(testApp);
verify(featuresService).installFeatures(featureNames, EnumSet.of(Option.NoAutoRefreshBundles));
}
use of org.apache.karaf.features.FeaturesService in project ddf by codice.
the class ApplicationServiceImplTest method testGetApplicationStatusReturnsFailedStatusWhenFeatureDependencyContainsInactiveBundle.
/**
* 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>All dependency features are installed</li>
* <li>One dependency feature contains a Bundle that is in an inactive state
* </li>
* </ul>
*
* @throws Exception
*/
@Test
public void testGetApplicationStatusReturnsFailedStatusWhenFeatureDependencyContainsInactiveBundle() throws Exception {
// Verify the pre-conditions
int bundleInclusionCount = 0;
for (Feature feature : mainFeatureRepo2.getFeatures()) {
for (BundleInfo bundleInfo : feature.getBundles()) {
if (bundleInfo.getLocation().equals(TEST_MAIN_FEATURES_2_UNIQUE_BUNDLE_LOCATION)) {
++bundleInclusionCount;
}
}
}
assertEquals("Bundle is not included in repository the expected number of times", 1, bundleInclusionCount);
// Execute test
Set<String> inactiveBundleNames = new HashSet<String>();
inactiveBundleNames.add(TEST_MAIN_FEATURES_2_UNIQUE_BUNDLE_LOCATION);
FeaturesService featuresService = createMockFeaturesService(mainFeatureRepo2, null, inactiveBundleNames);
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());
}
Aggregations