use of org.apache.karaf.service.guard.impl.GuardProxyCatalog.ServiceRegistrationHolder in project karaf by apache.
the class GuardProxyCatalogTest method testHandleServiceUnregistering.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testHandleServiceUnregistering() throws Exception {
BundleContext clientBC = openStrictMockBundleContext(mockBundle(12345L));
BundleContext client2BC = openStrictMockBundleContext(mockBundle(6L));
EasyMock.replay(clientBC);
EasyMock.replay(client2BC);
Hashtable<String, Object> props = new Hashtable<>();
long originalServiceID = 12345678901234L;
props.put(Constants.SERVICE_ID, new Long(originalServiceID));
props.put("foo", "bar");
ServiceReference<?> originalRef = mockServiceReference(props);
Hashtable<String, Object> props2 = new Hashtable<>();
long anotherServiceID = 5123456789012345L;
props2.put(Constants.SERVICE_ID, anotherServiceID);
ServiceReference<?> anotherRef = mockServiceReference(props2);
GuardProxyCatalog gpc = new GuardProxyCatalog(mockBundleContext());
ServiceRegistration<?> proxyReg = EasyMock.createMock(ServiceRegistration.class);
EasyMock.expect(proxyReg.getReference()).andReturn((ServiceReference) mockServiceReference(props)).anyTimes();
proxyReg.unregister();
EasyMock.expectLastCall().once();
EasyMock.replay(proxyReg);
ServiceRegistrationHolder srh = new GuardProxyCatalog.ServiceRegistrationHolder();
srh.registration = proxyReg;
ServiceRegistration<?> proxy2Reg = EasyMock.createMock(ServiceRegistration.class);
EasyMock.replay(proxy2Reg);
ServiceRegistrationHolder srh2 = new GuardProxyCatalog.ServiceRegistrationHolder();
srh2.registration = proxy2Reg;
gpc.proxyMap.put(originalServiceID, srh);
gpc.proxyMap.put(anotherServiceID, srh2);
assertEquals("Precondition", 2, gpc.proxyMap.size());
gpc.createProxyQueue.put(new MockCreateProxyRunnable(originalServiceID));
gpc.createProxyQueue.put(new MockCreateProxyRunnable(777));
assertEquals("Precondition", 2, gpc.createProxyQueue.size());
gpc.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, originalRef));
assertEquals("Registered events should be ignored", 2, gpc.proxyMap.size());
assertEquals("Registered events should be ignored", 2, gpc.createProxyQueue.size());
Hashtable<String, Object> proxyProps = new Hashtable<>(props);
proxyProps.put(GuardProxyCatalog.PROXY_SERVICE_KEY, Boolean.TRUE);
ServiceReference<?> proxyRef = mockServiceReference(proxyProps);
gpc.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, proxyRef));
assertEquals("Unregistering the proxy should be ignored by the listener", 2, gpc.proxyMap.size());
assertEquals("Unregistering the proxy should be ignored by the listener", 2, gpc.createProxyQueue.size());
gpc.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, originalRef));
assertEquals("The proxy for this service should have been removed", 1, gpc.proxyMap.size());
assertEquals(anotherRef.getProperty(Constants.SERVICE_ID), gpc.proxyMap.keySet().iterator().next());
assertEquals("The create proxy job for this service should have been removed", 1, gpc.createProxyQueue.size());
assertEquals(777, gpc.createProxyQueue.iterator().next().getOriginalServiceID());
EasyMock.verify(proxyReg);
EasyMock.verify(proxy2Reg);
}
use of org.apache.karaf.service.guard.impl.GuardProxyCatalog.ServiceRegistrationHolder in project karaf by apache.
the class GuardProxyCatalogTest method testHandleServiceModified.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testHandleServiceModified() throws Exception {
Dictionary<String, Object> config = new Hashtable<>();
config.put(Constants.SERVICE_PID, "test.1.2.3");
config.put("service.guard", "(objectClass=" + TestServiceAPI.class.getName() + ")");
config.put("doit", "role.1");
Dictionary<String, Object> config2 = new Hashtable<>();
config2.put(Constants.SERVICE_PID, "test.1.2.4");
config2.put("service.guard", "(objectClass=" + TestServiceAPI2.class.getName() + ")");
config2.put("doit", "role.2");
BundleContext bc = mockConfigAdminBundleContext(config, config2);
GuardProxyCatalog gpc = new GuardProxyCatalog(bc);
// The service being proxied has these properties
long serviceID = 1L;
final Hashtable<String, Object> serviceProps = new Hashtable<>();
serviceProps.put(Constants.OBJECTCLASS, new String[] { TestServiceAPI.class.getName(), TestServiceAPI2.class.getName() });
serviceProps.put(Constants.SERVICE_ID, serviceID);
// will be overwritten
serviceProps.put(GuardProxyCatalog.SERVICE_GUARD_ROLES_PROPERTY, Arrays.asList("someone"));
Object myObject = new Object();
serviceProps.put("foo.bar", myObject);
BundleContext providerBC = EasyMock.createNiceMock(BundleContext.class);
EasyMock.expect(providerBC.registerService(EasyMock.aryEq(new String[] { TestServiceAPI.class.getName(), TestServiceAPI2.class.getName() }), EasyMock.anyObject(), EasyMock.anyObject(Dictionary.class))).andAnswer((IAnswer) () -> {
final Dictionary props = (Dictionary) EasyMock.getCurrentArguments()[2];
assertEquals(Boolean.TRUE, props.get(GuardProxyCatalog.PROXY_SERVICE_KEY));
ServiceRegistration reg = EasyMock.createMock(ServiceRegistration.class);
ServiceReference sr = mockServiceReference(props);
EasyMock.expect(reg.getReference()).andReturn(sr).anyTimes();
reg.setProperties(EasyMock.isA(Dictionary.class));
EasyMock.expectLastCall().andAnswer(() -> {
// Push the update into the service reference
ArrayList<String> oldKeys = Collections.list(props.keys());
for (String key : oldKeys) {
props.remove(key);
}
Dictionary<String, Object> newProps = (Dictionary<String, Object>) EasyMock.getCurrentArguments()[0];
for (String key : Collections.list(newProps.keys())) {
props.put(key, newProps.get(key));
}
return null;
}).once();
EasyMock.replay(reg);
return reg;
}).anyTimes();
EasyMock.replay(providerBC);
// In some cases the proxy-creating code is looking for a classloader (e.g. when run through
// a coverage tool such as EclEmma). This will satisfy that.
BundleWiring bw = EasyMock.createMock(BundleWiring.class);
EasyMock.expect(bw.getClassLoader()).andReturn(getClass().getClassLoader()).anyTimes();
EasyMock.replay(bw);
// The mock bundle that provides the original service (and also the proxy is registered with this)
Bundle providerBundle = EasyMock.createNiceMock(Bundle.class);
EasyMock.expect(providerBundle.getBundleContext()).andReturn(providerBC).anyTimes();
EasyMock.expect(providerBundle.adapt(BundleWiring.class)).andReturn(bw).anyTimes();
EasyMock.replay(providerBundle);
ServiceReference sr = mockServiceReference(providerBundle, serviceProps);
gpc.proxyIfNotAlreadyProxied(sr);
GuardProxyCatalog.CreateProxyRunnable runnable = gpc.createProxyQueue.take();
runnable.run(getProxyManager());
ServiceRegistrationHolder holder = gpc.proxyMap.get(serviceID);
ServiceRegistration<?> reg = holder.registration;
for (String key : serviceProps.keySet()) {
if (GuardProxyCatalog.SERVICE_GUARD_ROLES_PROPERTY.equals(key)) {
assertEquals(new HashSet(Arrays.asList("role.1", "role.2")), reg.getReference().getProperty(key));
} else {
assertEquals(serviceProps.get(key), reg.getReference().getProperty(key));
}
}
assertEquals(Boolean.TRUE, reg.getReference().getProperty(GuardProxyCatalog.PROXY_SERVICE_KEY));
// now change the original service and let the proxy react
serviceProps.put("test", "property");
assertEquals("Precondition, the mocked reference should have picked up this change", "property", sr.getProperty("test"));
gpc.serviceChanged(new ServiceEvent(ServiceEvent.MODIFIED, sr));
assertEquals("Changing the service should not change the number of proxies", 1, gpc.proxyMap.size());
for (String key : serviceProps.keySet()) {
if (GuardProxyCatalog.SERVICE_GUARD_ROLES_PROPERTY.equals(key)) {
assertEquals(new HashSet(Arrays.asList("role.1", "role.2")), reg.getReference().getProperty(key));
} else {
assertEquals(serviceProps.get(key), reg.getReference().getProperty(key));
}
}
assertEquals("property", reg.getReference().getProperty("test"));
assertEquals(Boolean.TRUE, reg.getReference().getProperty(GuardProxyCatalog.PROXY_SERVICE_KEY));
}
use of org.apache.karaf.service.guard.impl.GuardProxyCatalog.ServiceRegistrationHolder in project karaf by apache.
the class GuardProxyCatalogTest method testHandleServiceModified2.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testHandleServiceModified2() throws Exception {
// no configuration used in this test...
BundleContext bc = mockConfigAdminBundleContext();
GuardProxyCatalog gpc = new GuardProxyCatalog(bc);
// The service being proxied has these properties
long serviceID = 1L;
final Hashtable<String, Object> serviceProps = new Hashtable<>();
serviceProps.put(Constants.OBJECTCLASS, new String[] { TestServiceAPI.class.getName() });
serviceProps.put(Constants.SERVICE_ID, serviceID);
BundleContext providerBC = EasyMock.createNiceMock(BundleContext.class);
EasyMock.expect(providerBC.registerService(EasyMock.aryEq(new String[] { TestServiceAPI.class.getName() }), EasyMock.anyObject(), EasyMock.anyObject(Dictionary.class))).andAnswer((IAnswer) () -> {
final Dictionary props = (Dictionary) EasyMock.getCurrentArguments()[2];
assertEquals(Boolean.TRUE, props.get(GuardProxyCatalog.PROXY_SERVICE_KEY));
ServiceRegistration reg = EasyMock.createMock(ServiceRegistration.class);
ServiceReference sr = mockServiceReference(props);
EasyMock.expect(reg.getReference()).andReturn(sr).anyTimes();
reg.setProperties(EasyMock.isA(Dictionary.class));
EasyMock.expectLastCall().andAnswer(() -> {
// Push the update into the service reference
ArrayList<String> oldKeys = Collections.list(props.keys());
for (String key : oldKeys) {
props.remove(key);
}
Dictionary<String, Object> newProps = (Dictionary<String, Object>) EasyMock.getCurrentArguments()[0];
for (String key : Collections.list(newProps.keys())) {
props.put(key, newProps.get(key));
}
return null;
}).once();
EasyMock.replay(reg);
return reg;
}).anyTimes();
EasyMock.replay(providerBC);
// In some cases the proxy-creating code is looking for a classloader (e.g. when run through
// a coverage tool such as EclEmma). This will satisfy that.
BundleWiring bw = EasyMock.createMock(BundleWiring.class);
EasyMock.expect(bw.getClassLoader()).andReturn(getClass().getClassLoader()).anyTimes();
EasyMock.replay(bw);
// The mock bundle that provides the original service (and also the proxy is registered with this)
Bundle providerBundle = EasyMock.createNiceMock(Bundle.class);
EasyMock.expect(providerBundle.getBundleContext()).andReturn(providerBC).anyTimes();
EasyMock.expect(providerBundle.adapt(BundleWiring.class)).andReturn(bw).anyTimes();
EasyMock.replay(providerBundle);
ServiceReference sr = mockServiceReference(providerBundle, serviceProps);
gpc.proxyIfNotAlreadyProxied(sr);
GuardProxyCatalog.CreateProxyRunnable runnable = gpc.createProxyQueue.take();
runnable.run(getProxyManager());
ServiceRegistrationHolder holder = gpc.proxyMap.get(serviceID);
ServiceRegistration<?> reg = holder.registration;
assertFalse("No roles defined for this service using configuration, so roles property should not be set", Arrays.asList(reg.getReference().getPropertyKeys()).contains(GuardProxyCatalog.SERVICE_GUARD_ROLES_PROPERTY));
for (String key : serviceProps.keySet()) {
assertEquals(serviceProps.get(key), reg.getReference().getProperty(key));
}
assertEquals(Boolean.TRUE, reg.getReference().getProperty(GuardProxyCatalog.PROXY_SERVICE_KEY));
// now change the original service and let the proxy react
serviceProps.put(GuardProxyCatalog.SERVICE_GUARD_ROLES_PROPERTY, "foobar");
assertEquals("Precondition, the mocked reference should have picked up this change", "foobar", sr.getProperty(GuardProxyCatalog.SERVICE_GUARD_ROLES_PROPERTY));
gpc.serviceChanged(new ServiceEvent(ServiceEvent.MODIFIED, sr));
assertEquals("Changing the service should not change the number of proxies", 1, gpc.proxyMap.size());
assertFalse("The roles property set on the modified service should have been removed", Arrays.asList(reg.getReference().getPropertyKeys()).contains(GuardProxyCatalog.SERVICE_GUARD_ROLES_PROPERTY));
assertEquals(Boolean.TRUE, reg.getReference().getProperty(GuardProxyCatalog.PROXY_SERVICE_KEY));
}
Aggregations