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 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.Repository 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.Repository 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.Repository 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.Repository in project ddf by codice.
the class ApplicationServiceImplTest method testGetAllFeatures.
/**
* Tests the {@link ApplicationServiceImpl#getAllFeatures()} method
*
* @throws Exception
*/
@Test
public void testGetAllFeatures() 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();
List<FeatureDetails> result = appService.getAllFeatures();
assertThat("Returned features should match features in mainFeatureRepo.", result.get(0).getName(), is(mainFeatureRepo.getFeatures()[0].getName()));
assertThat("Returned features should match features in mainFeatureRepo.", result.get(0).getId(), is(mainFeatureRepo.getFeatures()[0].getId()));
assertThat("Should return seven features.", result.size(), is(7));
}
Aggregations