use of org.osgi.service.component.ComponentContext in project fuse-karaf by jboss-fuse.
the class PatchServiceImplTest method testPatch.
@Test
public void testPatch() throws Exception {
ComponentContext componentContext = mock(ComponentContext.class);
BundleContext bundleContext = mock(BundleContext.class);
Bundle sysBundle = mock(Bundle.class);
BundleContext sysBundleContext = mock(BundleContext.class);
Bundle bundle = mock(Bundle.class);
Bundle bundle2 = mock(Bundle.class);
FrameworkWiring wiring = mock(FrameworkWiring.class);
GitPatchRepository repository = mock(GitPatchRepository.class);
//
// Create a new service, download a patch
//
when(componentContext.getBundleContext()).thenReturn(bundleContext);
when(bundleContext.getBundle(0)).thenReturn(sysBundle);
when(sysBundle.getBundleContext()).thenReturn(sysBundleContext);
when(sysBundleContext.getProperty(PatchService.PATCH_LOCATION)).thenReturn(storage.toString());
when(repository.getManagedPatch(anyString())).thenReturn(null);
when(repository.findOrCreateMainGitRepository()).thenReturn(null);
when(sysBundleContext.getProperty("karaf.default.repository")).thenReturn("system");
when(sysBundleContext.getProperty("karaf.home")).thenReturn(karaf.getCanonicalPath());
when(sysBundleContext.getProperty("karaf.base")).thenReturn(karaf.getCanonicalPath());
when(sysBundleContext.getProperty("karaf.name")).thenReturn("root");
when(sysBundleContext.getProperty("karaf.instances")).thenReturn(karaf.getCanonicalPath() + "/instances");
when(sysBundleContext.getProperty("karaf.data")).thenReturn(karaf.getCanonicalPath() + "/data");
when(sysBundleContext.getProperty("karaf.etc")).thenReturn(karaf.getCanonicalPath() + "/etc");
PatchManagement pm = mockManagementService(bundleContext);
((GitPatchManagementServiceImpl) pm).setGitPatchRepository(repository);
PatchServiceImpl service = new PatchServiceImpl();
setField(service, "patchManagement", pm);
service.activate(componentContext);
try {
service.download(new URL("file:" + storage + "/temp/f00.zip"));
fail("Should have thrown exception on non existent patch file.");
} catch (Exception ignored) {
}
Iterable<Patch> patches = service.download(patch132.toURI().toURL());
assertNotNull(patches);
Iterator<Patch> it = patches.iterator();
assertTrue(it.hasNext());
Patch patch = it.next();
assertNotNull(patch);
assertEquals("patch-1.3.2", patch.getPatchData().getId());
assertNotNull(patch.getPatchData().getBundles());
assertEquals(1, patch.getPatchData().getBundles().size());
Iterator<String> itb = patch.getPatchData().getBundles().iterator();
assertEquals("mvn:foo/my-bsn/1.3.2", itb.next());
assertNull(patch.getResult());
//
// Simulate the patch
//
when(sysBundleContext.getBundles()).thenReturn(new Bundle[] { bundle });
when(sysBundleContext.getServiceReference("io.fabric8.api.FabricService")).thenReturn(null);
when(bundle.getSymbolicName()).thenReturn("my-bsn");
when(bundle.getVersion()).thenReturn(new Version("1.3.1"));
when(bundle.getLocation()).thenReturn("location");
when(bundle.getBundleId()).thenReturn(123L);
BundleStartLevel bsl = mock(BundleStartLevel.class);
when(bsl.getStartLevel()).thenReturn(30);
when(bundle.adapt(BundleStartLevel.class)).thenReturn(bsl);
when(bundle.getState()).thenReturn(1);
when(sysBundleContext.getProperty("karaf.default.repository")).thenReturn("system");
PatchResult result = service.install(patch, true);
assertNotNull(result);
assertTrue(result.isSimulation());
//
// Recreate a new service and verify the downloaded patch is still available
//
when(componentContext.getBundleContext()).thenReturn(bundleContext);
when(bundleContext.getBundle(0)).thenReturn(sysBundle);
when(sysBundle.getBundleContext()).thenReturn(sysBundleContext);
when(sysBundleContext.getProperty(PatchService.PATCH_LOCATION)).thenReturn(storage.toString());
when(sysBundleContext.getProperty("karaf.home")).thenReturn(karaf.toString());
when(sysBundleContext.getProperty("karaf.base")).thenReturn(karaf.getCanonicalPath());
when(sysBundleContext.getProperty("karaf.name")).thenReturn("root");
when(sysBundleContext.getProperty("karaf.instances")).thenReturn(karaf.getCanonicalPath() + "/instances");
when(sysBundleContext.getProperty("karaf.default.repository")).thenReturn("system");
when(sysBundleContext.getProperty("karaf.etc")).thenReturn(karaf.getCanonicalPath() + "/etc");
when(repository.getManagedPatch(anyString())).thenReturn(null);
when(repository.findOrCreateMainGitRepository()).thenReturn(null);
service = new PatchServiceImpl();
setField(service, "patchManagement", pm);
service.activate(componentContext);
patches = service.getPatches();
assertNotNull(patches);
it = patches.iterator();
assertTrue(it.hasNext());
patch = it.next();
assertNotNull(patch);
assertEquals("patch-1.3.2", patch.getPatchData().getId());
assertNotNull(patch.getPatchData().getBundles());
assertEquals(1, patch.getPatchData().getBundles().size());
itb = patch.getPatchData().getBundles().iterator();
assertEquals("mvn:foo/my-bsn/1.3.2", itb.next());
assertNull(patch.getResult());
//
// Install the patch
//
when(sysBundleContext.getBundles()).thenReturn(new Bundle[] { bundle });
when(sysBundleContext.getServiceReference("io.fabric8.api.FabricService")).thenReturn(null);
when(bundle.getSymbolicName()).thenReturn("my-bsn");
when(bundle.getVersion()).thenReturn(new Version("1.3.1"));
when(bundle.getLocation()).thenReturn("location");
when(bundle.getHeaders()).thenReturn(new Hashtable<String, String>());
when(bundle.getBundleId()).thenReturn(123L);
bundle.update(any(InputStream.class));
when(sysBundleContext.getBundles()).thenReturn(new Bundle[] { bundle });
when(bundle.getState()).thenReturn(Bundle.INSTALLED);
when(bundle.getRegisteredServices()).thenReturn(null);
when(bundle.adapt(BundleStartLevel.class)).thenReturn(bsl);
when(bsl.getStartLevel()).thenReturn(30);
when(sysBundleContext.getBundle(0)).thenReturn(sysBundle);
when(sysBundle.adapt(FrameworkWiring.class)).thenReturn(wiring);
when(sysBundleContext.getProperty("karaf.default.repository")).thenReturn("system");
bundle.start();
doAnswer(invocationOnMock -> {
((FrameworkListener) invocationOnMock.getArgument(1)).frameworkEvent(null);
return invocationOnMock.getMock();
}).when(wiring).refreshBundles(any(), any(FrameworkListener.class));
result = service.install(patch, false);
assertNotNull(result);
assertSame(result, patch.getResult());
assertFalse(patch.getResult().isSimulation());
//
// Recreate a new service and verify the downloaded patch is still available and installed
//
when(componentContext.getBundleContext()).thenReturn(bundleContext);
when(bundleContext.getBundle(0)).thenReturn(sysBundle);
when(sysBundle.getBundleContext()).thenReturn(sysBundleContext);
when(repository.getManagedPatch(anyString())).thenReturn(null);
when(sysBundleContext.getProperty(PatchService.PATCH_LOCATION)).thenReturn(storage.toString());
when(sysBundleContext.getProperty("karaf.home")).thenReturn(karaf.toString());
when(sysBundleContext.getProperty("karaf.base")).thenReturn(karaf.getCanonicalPath());
when(sysBundleContext.getProperty("karaf.name")).thenReturn("root");
when(sysBundleContext.getProperty("karaf.instances")).thenReturn(karaf.getCanonicalPath() + "/instances");
when(sysBundleContext.getProperty("karaf.default.repository")).thenReturn("system");
when(sysBundleContext.getProperty("karaf.etc")).thenReturn(karaf.getCanonicalPath() + "/etc");
service = new PatchServiceImpl();
setField(service, "patchManagement", pm);
service.activate(componentContext);
patches = service.getPatches();
assertNotNull(patches);
it = patches.iterator();
assertTrue(it.hasNext());
patch = it.next();
assertNotNull(patch);
assertEquals("patch-1.3.2", patch.getPatchData().getId());
assertNotNull(patch.getPatchData().getBundles());
assertEquals(1, patch.getPatchData().getBundles().size());
itb = patch.getPatchData().getBundles().iterator();
assertEquals("mvn:foo/my-bsn/1.3.2", itb.next());
assertNotNull(patch.getResult());
}
use of org.osgi.service.component.ComponentContext in project smarthome by eclipse.
the class ThingStatusInfoI18nLocalizationServiceOSGiTest method setup.
@Before
public void setup() throws Exception {
LocaleProvider localeProvider = getService(LocaleProvider.class);
assertThat(localeProvider, is(notNullValue()));
defaultLocale = localeProvider.getLocale();
new DefaultLocaleSetter(getService(ConfigurationAdmin.class)).setDefaultLocale(Locale.ENGLISH);
waitForAssert(() -> assertThat(localeProvider.getLocale(), is(Locale.ENGLISH)));
registerVolatileStorageService();
SimpleThingHandlerFactory simpleThingHandlerFactory = new SimpleThingHandlerFactory();
ComponentContext componentContext = mock(ComponentContext.class);
when(componentContext.getBundleContext()).thenReturn(bundleContext);
simpleThingHandlerFactory.activate(componentContext);
registerService(simpleThingHandlerFactory, ThingHandlerFactory.class.getName());
thing = ThingBuilder.create(new ThingTypeUID("aaa:bbb"), "ccc").build();
managedThingProvider = getService(ManagedThingProvider.class);
assertThat(managedThingProvider, is(notNullValue()));
managedThingProvider.add(thing);
waitForAssert(() -> assertThat(thing.getStatus(), is(ThingStatus.ONLINE)));
testBundle = SyntheticBundleInstaller.install(bundleContext, TEST_BUNDLE_NAME);
assertThat(testBundle, is(notNullValue()));
thingStatusInfoI18nLocalizationService = getService(ThingStatusInfoI18nLocalizationService.class);
assertThat(thingStatusInfoI18nLocalizationService, is(notNullValue()));
thingStatusInfoI18nLocalizationService.setBundleResolver(new BundleResolverImpl());
}
use of org.osgi.service.component.ComponentContext in project smarthome by eclipse.
the class GenericThingProviderTest2 method assertThatThingsAreCreatedAndNotRemovedOnAddingRemovingTriggerHandlerFactories.
@Test
public void assertThatThingsAreCreatedAndNotRemovedOnAddingRemovingTriggerHandlerFactories() {
ComponentContext componentContextMock = mock(ComponentContext.class);
when(componentContextMock.getBundleContext()).thenReturn(bundleContext);
TestHueThingHandlerFactoryX hueThingHandlerFactory = new TestHueThingHandlerFactoryX(componentContextMock);
Collection<Thing> things = thingRegistry.getAll();
assertThat(things.size(), is(0));
String model = //
"Bridge Xhue:Xbridge:myBridge [ XipAddress = \"1.2.3.4\", XuserName = \"123\" ] {" + //
" XLCT001 bulb1 [ XlightId = \"1\" ] { Switch : notification }" + //
" Bridge Xbridge myBridge2 [ ] {" + //
" XLCT001 bulb2 [ ]" + //
" }" + //
"}" + //
"Xhue:XTEST:bulb4 [ XlightId = \"5\"]{" + //
" Switch : notification [ duration = \"5\" ]" + //
"}" + //
"" + //
"Xhue:XLCT001:bulb3 [ XlightId = \"4\" ] {" + //
" Switch : notification [ duration = \"5\" ]" + "}";
modelRepository.addOrRefreshModel(TESTMODEL_NAME, new ByteArrayInputStream(model.getBytes()));
Collection<Thing> actualThings = thingRegistry.getAll();
assertThat(actualThings.size(), is(0));
registerService(hueThingHandlerFactory, ThingHandlerFactory.class.getName());
actualThings = thingRegistry.getAll();
assertThat(thingRegistry.getAll().size(), is(6));
unregisterService(hueThingHandlerFactory);
assertThat(thingRegistry.getAll().size(), is(6));
registerService(hueThingHandlerFactory, ThingHandlerFactory.class.getName());
assertThat(thingRegistry.getAll().size(), is(6));
TestHueThingHandlerFactoryX thingHF2 = new TestHueThingHandlerFactoryX(componentContextMock);
registerService(thingHF2, ThingHandlerFactory.class.getName());
assertThat(thingRegistry.getAll().size(), is(6));
}
use of org.osgi.service.component.ComponentContext in project smarthome by eclipse.
the class GenericThingProviderTest4 method setUp.
@Before
public void setUp() {
registerVolatileStorageService();
readyService = getService(ReadyService.class);
assertThat(readyService, is(notNullValue()));
thingRegistry = getService(ThingRegistry.class);
assertThat(thingRegistry, is(notNullValue()));
modelRepository = getService(ModelRepository.class);
assertThat(modelRepository, is(notNullValue()));
modelRepository.removeModel(TESTMODEL_NAME);
ComponentContext componentContextMock = mock(ComponentContext.class);
when(componentContextMock.getBundleContext()).thenReturn(bundleContext);
hueThingHandlerFactory = new TestHueThingHandlerFactoryX(componentContextMock) {
@Override
protected ThingHandler createHandler(final Thing thing) {
if (thing instanceof Bridge) {
return new TestBridgeHandler((Bridge) thing);
} else {
return new BaseThingHandler(thing) {
@Override
public void handleCommand(ChannelUID arg0, Command arg1) {
}
@Override
public void initialize() {
updateStatus(ThingStatus.ONLINE);
}
};
}
}
};
finished = false;
bundle = FrameworkUtil.getBundle(TestHueThingHandlerFactoryX.class);
removeReadyMarker();
}
use of org.osgi.service.component.ComponentContext in project smarthome by eclipse.
the class InboxOSGITest method setUp.
@Before
public void setUp() {
registerVolatileStorageService();
discoveryResults.clear();
inboxListeners.clear();
inbox = (PersistentInbox) getService(Inbox.class);
managedThingProvider = getService(ManagedThingProvider.class);
registry = getService(ThingRegistry.class);
ComponentContext componentContextMock = mock(ComponentContext.class);
when(componentContextMock.getBundleContext()).thenReturn(bundleContext);
inbox.addThingHandlerFactory(new DummyThingHandlerFactory(componentContextMock));
}
Aggregations