blob: 0eda1506f0765c07b0acdc5174e744b703035b49 [file] [log] [blame]
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF 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.apache.cxf.dosgi.topologymanager.exporter;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor;
import org.apache.cxf.dosgi.dsw.api.ExportPolicy;
import org.easymock.EasyMock;
import org.easymock.IMocksControl;
import org.junit.Test;
import org.osgi.framework.Bundle;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceReference;
import org.osgi.service.remoteserviceadmin.EndpointDescription;
import org.osgi.service.remoteserviceadmin.EndpointListener;
import org.osgi.service.remoteserviceadmin.ExportReference;
import org.osgi.service.remoteserviceadmin.ExportRegistration;
import org.osgi.service.remoteserviceadmin.RemoteConstants;
import org.osgi.service.remoteserviceadmin.RemoteServiceAdmin;
import static org.easymock.EasyMock.expectLastCall;
@SuppressWarnings({"rawtypes", "unchecked"})
public class TopologyManagerExportTest {
/**
* This tests if the topology manager handles a service marked to be exported correctly by exporting it to
* an available RemoteServiceAdmin and notifying an EndpointListener afterwards.
*
* @throws Exception
*/
@Test
public void testServiceExportUnexport() throws Exception {
IMocksControl c = EasyMock.createControl();
RemoteServiceAdmin rsa = c.createMock(RemoteServiceAdmin.class);
final EndpointListener notifier = c.createMock(EndpointListener.class);
final ServiceReference sref = createUserService(c);
EndpointDescription epd = createEndpoint();
expectServiceExported(c, rsa, notifier, sref, epd);
c.replay();
EndpointRepository endpointRepo = new EndpointRepository();
endpointRepo.setNotifier(notifier);
Executor executor = syncExecutor();
ExportPolicy policy = new DefaultExportPolicy();
TopologyManagerExport exportManager = new TopologyManagerExport(endpointRepo, executor, policy);
exportManager.add(rsa);
exportManager.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, sref));
c.verify();
c.reset();
notifier.endpointRemoved(epd, null);
expectLastCall().once();
c.replay();
exportManager.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, sref));
c.verify();
c.reset();
c.replay();
exportManager.serviceChanged(new ServiceEvent(ServiceEvent.MODIFIED, sref));
c.verify();
c.reset();
c.replay();
exportManager.remove(rsa);
c.verify();
}
@Test
public void testExportExisting() throws Exception {
IMocksControl c = EasyMock.createControl();
RemoteServiceAdmin rsa = c.createMock(RemoteServiceAdmin.class);
final EndpointListenerNotifier mockEpListenerNotifier = c.createMock(EndpointListenerNotifier.class);
final ServiceReference sref = createUserService(c);
expectServiceExported(c, rsa, mockEpListenerNotifier, sref, createEndpoint());
c.replay();
EndpointRepository endpointRepo = new EndpointRepository();
endpointRepo.setNotifier(mockEpListenerNotifier);
ExportPolicy policy = new DefaultExportPolicy();
TopologyManagerExport exportManager = new TopologyManagerExport(endpointRepo, syncExecutor(), policy);
exportManager.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, sref));
exportManager.add(rsa);
c.verify();
}
private void expectServiceExported(IMocksControl c, RemoteServiceAdmin rsa,
final EndpointListener listener,
final ServiceReference sref, EndpointDescription epd) {
ExportRegistration exportRegistration = createExportRegistration(c, epd);
EasyMock.expect(rsa.exportService(EasyMock.same(sref), (Map<String, Object>)EasyMock.anyObject()))
.andReturn(Collections.singletonList(exportRegistration)).once();
listener.endpointAdded(epd, null);
EasyMock.expectLastCall().once();
}
private Executor syncExecutor() {
return new Executor() {
@Override
public void execute(Runnable command) {
command.run();
}
};
}
private ExportRegistration createExportRegistration(IMocksControl c, EndpointDescription endpoint) {
ExportRegistration exportRegistration = c.createMock(ExportRegistration.class);
ExportReference exportReference = c.createMock(ExportReference.class);
EasyMock.expect(exportRegistration.getExportReference()).andReturn(exportReference).anyTimes();
EasyMock.expect(exportRegistration.getException()).andReturn(null).anyTimes();
EasyMock.expect(exportReference.getExportedEndpoint()).andReturn(endpoint).anyTimes();
return exportRegistration;
}
private EndpointDescription createEndpoint() {
Map<String, Object> props = new HashMap<String, Object>();
props.put(RemoteConstants.ENDPOINT_ID, "1");
props.put(Constants.OBJECTCLASS, new String[] {"abc"});
props.put(RemoteConstants.SERVICE_IMPORTED_CONFIGS, "cxf");
return new EndpointDescription(props);
}
private ServiceReference createUserService(IMocksControl c) {
final ServiceReference sref = c.createMock(ServiceReference.class);
EasyMock.expect(sref.getProperty(EasyMock.same(RemoteConstants.SERVICE_EXPORTED_INTERFACES)))
.andReturn("*").anyTimes();
Bundle srefBundle = c.createMock(Bundle.class);
EasyMock.expect(sref.getBundle()).andReturn(srefBundle).atLeastOnce();
EasyMock.expect(sref.getProperty("objectClass")).andReturn("org.My").anyTimes();
EasyMock.expect(srefBundle.getSymbolicName()).andReturn("serviceBundleName").atLeastOnce();
return sref;
}
}