/**
* Licensed to the Austrian Association for Software Tool Integration (AASTI)
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. The AASTI licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openengsb.core.services.internal;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.UUID;
import org.junit.Before;
import org.junit.Test;
import org.openengsb.core.api.Connector;
import org.openengsb.core.api.ConnectorInstanceFactory;
import org.openengsb.core.api.Constants;
import org.openengsb.core.api.OsgiServiceNotAvailableException;
import org.openengsb.core.api.VirtualConnectorProvider;
import org.openengsb.core.api.model.ConnectorDescription;
import org.openengsb.core.api.remote.MethodCall;
import org.openengsb.core.api.remote.MethodResult;
import org.openengsb.core.api.remote.OutgoingPortUtilService;
import org.openengsb.core.common.SecurityAttributeProviderImpl;
import org.openengsb.core.common.internal.Activator;
import org.openengsb.core.ekb.api.TransformationEngine;
import org.openengsb.core.services.internal.virtual.ProxyConnectorProvider;
import org.openengsb.core.services.internal.virtual.ProxyConnectorRegistryImpl;
import org.openengsb.core.test.AbstractOsgiMockServiceTest;
import org.openengsb.core.test.NullDomain;
import org.openengsb.core.test.NullDomainImpl;
import org.openengsb.core.util.DefaultOsgiUtilsService;
import org.openengsb.core.util.FilterUtils;
import org.osgi.framework.Filter;
public class ServiceRegistrationManagerTest extends AbstractOsgiMockServiceTest {
private ConnectorRegistrationManager registrationManager;
private DefaultOsgiUtilsService serviceUtils;
private OutgoingPortUtilService callrouter;
private ProxyConnectorRegistryImpl connectorRegistry;
@Before
public void setUp() throws Exception {
serviceUtils = new DefaultOsgiUtilsService(bundleContext);
createDomainProviderMock(NullDomain.class, "test");
createFactoryMock("testc", NullDomainImpl.class, "test");
callrouter = mock(OutgoingPortUtilService.class);
MethodResult result = MethodResult.newVoidResult();
when(callrouter.sendMethodCallWithResult(anyString(), anyString(), any(MethodCall.class))).thenReturn(result);
registerService(callrouter, new Hashtable<String, Object>(), OutgoingPortUtilService.class);
registrationManager = new ConnectorRegistrationManager(bundleContext,
mock(TransformationEngine.class), new ForwardMethodInterceptor(), new SecurityAttributeProviderImpl());
ProxyConnectorProvider proxyConnectorProvider = new ProxyConnectorProvider();
proxyConnectorProvider.setId(Constants.EXTERNAL_CONNECTOR_PROXY);
proxyConnectorProvider.setBundleContext(bundleContext);
proxyConnectorProvider.setOutgoingPortUtilService(callrouter);
connectorRegistry = new ProxyConnectorRegistryImpl();
proxyConnectorProvider.setConnectorRegistry(connectorRegistry);
registerService(proxyConnectorProvider, new Hashtable<String, Object>(), VirtualConnectorProvider.class);
new Activator().start(bundleContext);
}
@Test
public void testCreateService_shouldCreateInstanceWithFactory() throws Exception {
Map<String, String> attributes = new HashMap<String, String>();
attributes.put("answer", "42");
Map<String, Object> properties = new Hashtable<String, Object>();
properties.put("foo", "bar");
ConnectorDescription connectorDescription = new ConnectorDescription("test", "testc", attributes, properties);
String connectorId = UUID.randomUUID().toString();
registrationManager.updateRegistration(connectorId, connectorDescription);
Object service2 = serviceUtils.getService("(foo=bar)", 100L);
NullDomain service = (NullDomain) service2;
assertThat(service.getInstanceId(), is(connectorId.toString()));
}
@Test
public void testDeleteService_shouldUnregisterService() throws Exception {
Map<String, String> attributes = new HashMap<String, String>();
attributes.put("answer", "42");
Map<String, Object> properties = new Hashtable<String, Object>();
properties.put("foo", "bar");
ConnectorDescription connectorDescription = new ConnectorDescription("test", "testc", attributes, properties);
String connectorId = UUID.randomUUID().toString();
registrationManager.updateRegistration(connectorId, connectorDescription);
registrationManager.remove(connectorId);
try {
serviceUtils.getService("(foo=bar)", 100L);
fail("service was expected to be not available");
} catch (OsgiServiceNotAvailableException e) {
// expected
}
}
@Test
public void testUpdateProperties_shouldMatchNewFilter() throws Exception {
Map<String, String> attributes = new HashMap<String, String>();
attributes.put("answer", "42");
Map<String, Object> properties = new Hashtable<String, Object>();
properties.put("foo", "bar");
ConnectorDescription connectorDescription = new ConnectorDescription("test", "testc", attributes, properties);
String connectorId = UUID.randomUUID().toString();
registrationManager.updateRegistration(connectorId, connectorDescription);
ConnectorDescription updated = new ConnectorDescription("test", "testc");
Map<String, Object> newProperties = new Hashtable<String, Object>();
newProperties.put("foo", "xxx");
updated.setProperties(newProperties);
registrationManager.updateRegistration(connectorId, updated);
serviceUtils.getService("(foo=xxx)", 100L);
try {
serviceUtils.getService("(foo=bar)", 100L);
fail("service was expected to be not available");
} catch (OsgiServiceNotAvailableException e) {
// expected
}
}
@Test
public void testUpdateAttributes_shouldChangeInstance() throws Exception {
Map<String, String> attributes = new HashMap<String, String>();
attributes.put("answer", "42");
Map<String, Object> properties = new Hashtable<String, Object>();
properties.put("foo", "bar");
ConnectorDescription connectorDescription = new ConnectorDescription("test", "testc", attributes, properties);
String connectorId = UUID.randomUUID().toString();
registrationManager.updateRegistration(connectorId, connectorDescription);
ConnectorDescription updated = new ConnectorDescription("test", "testc");
Map<String, String> newAttrs = new HashMap<String, String>();
newAttrs.put("answer", "43");
updated.setAttributes(newAttrs);
updated.setProperties(properties);
registrationManager.updateRegistration(connectorId, updated);
serviceUtils.getService("(foo=bar)", 100L);
Filter filter = FilterUtils.makeFilter(ConnectorInstanceFactory.class, "(connector=testc)");
ConnectorInstanceFactory factory = (ConnectorInstanceFactory) serviceUtils.getService(filter);
verify(factory).applyAttributes(any(Connector.class), eq(newAttrs));
}
@Test
public void testCreateProxy_shouldInvokeProxyFactory() throws Exception {
Map<String, String> attributes = new HashMap<String, String>();
attributes.put("portId", "jms+json");
attributes.put("destination", "localhost");
attributes.put("serviceId", "foo");
Map<String, Object> properties = new Hashtable<String, Object>();
properties.put("foo", "bar");
ConnectorDescription connectorDescription =
new ConnectorDescription("test", Constants.EXTERNAL_CONNECTOR_PROXY, attributes, properties);
String connectorId = UUID.randomUUID().toString();
registrationManager.updateRegistration(connectorId, connectorDescription);
connectorRegistry.create("foo");
connectorRegistry.registerConnector(connectorId, "jms+json", "localhost");
NullDomain service = (NullDomain) serviceUtils.getService("(foo=bar)", 100L);
service.nullMethod();
verify(callrouter).sendMethodCallWithResult(eq("jms+json"), eq("localhost"), any(MethodCall.class));
assertThat(service.getInstanceId(), is(connectorId.toString()));
}
}