Skip to main content
summaryrefslogtreecommitdiffstats
blob: 417709b2ee2e6ae165d73af4013872369a269dfe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*******************************************************************************
 * Copyright (c) 2010 Composent, Inc. and others. All rights reserved. This
 * program and the accompanying materials are made available under the terms of
 * the Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Composent, Inc. - initial API and implementation
 ******************************************************************************/
package org.eclipse.ecf.tests.osgi.services.remoteserviceadmin;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.discovery.IDiscoveryAdvertiser;
import org.eclipse.ecf.discovery.IDiscoveryLocator;
import org.eclipse.ecf.discovery.IServiceInfo;
import org.eclipse.ecf.osgi.services.remoteserviceadmin.DiscoveredEndpointDescription;
import org.eclipse.ecf.osgi.services.remoteserviceadmin.EndpointDescription;
import org.eclipse.ecf.osgi.services.remoteserviceadmin.IEndpointDescriptionFactory;
import org.eclipse.ecf.osgi.services.remoteserviceadmin.IServiceInfoFactory;
import org.eclipse.ecf.osgi.services.remoteserviceadmin.RemoteConstants;
import org.eclipse.ecf.tests.ECFAbstractTestCase;
import org.osgi.framework.Constants;
import org.osgi.util.tracker.ServiceTracker;

public abstract class AbstractMetadataFactoryTest extends ECFAbstractTestCase {

	protected static final String DEFAULT_SERVICE_INTF_PACKAGE = "com.foo";
	protected static final String DEFAULT_SERVICE_INTF_VERSION = "3.0.0";
	protected static final String DEFAULT_SERVICE_INTF = DEFAULT_SERVICE_INTF_PACKAGE + "." + "Foo";
	
	protected static final String DEFAULT_ENDPOINT_ID = "ecftcp://localhost:3282/server";
	protected static final String DEFAULT_SERVICE_IMPORTED_CONFIG = "ecf.generic.server";
	protected static final String DEFAULT_SERVICE_INTENT1 = "test.intent.1";
	protected static final String DEFAULT_SERVICE_INTENT2 = "test.intent.2";
	protected static final String DEFAULT_ECF_TARGET_ID = "ecftcp://localhost:3333/server";
	protected static final Object DEFAULT_IDFILTER = "(&(key1=foo)(key2=foo2))";
	protected static final String EXTRA_PROPERTY1 = "test.extra.prop.value.1";
	protected static final String EXTRA_PROPERTY2 = "test.extra.prop.value.2";
	
	protected IServiceInfoFactory serviceInfoFactory;
	protected IEndpointDescriptionFactory endpointDescriptionFactory;
	
	protected IDiscoveryAdvertiser discoveryAdvertiser;
	protected IDiscoveryLocator discoveryLocator;

	protected IDiscoveryLocator getDiscoveryLocator() {
		ServiceTracker serviceTracker = new ServiceTracker(Activator.getContext(),IDiscoveryLocator.class.getName(), null);
		serviceTracker.open();
		IDiscoveryLocator result = (IDiscoveryLocator) serviceTracker.getService();
		serviceTracker.close();
		return result;
	}
	
	protected IDiscoveryAdvertiser getDiscoveryAdvertiser() {
		ServiceTracker serviceTracker = new ServiceTracker(Activator.getContext(),IDiscoveryAdvertiser.class.getName(), null);
		serviceTracker.open();
		IDiscoveryAdvertiser result = (IDiscoveryAdvertiser) serviceTracker.getService();
		serviceTracker.close();
		return result;
	}

	protected void setUp() throws Exception {
		super.setUp();
	}
	
	protected void tearDown() throws Exception {
		serviceInfoFactory = null;
		endpointDescriptionFactory = null;
		discoveryAdvertiser = null;
		discoveryLocator = null;
		super.tearDown();
	}
	
	protected Object createOSGiObjectClass() {
		return new String[] { DEFAULT_SERVICE_INTF };
	}
	
	protected String createOSGiEndpointFrameworkUUID() {
		return UUID.randomUUID().toString();
	}
	
	protected String createOSGiEndpointId() {
		return DEFAULT_ENDPOINT_ID;
	}
	
	protected Long createOSGiEndpointServiceId() {
		return new Long(1);
	}
	
	protected EndpointDescription createRequiredEndpointDescription() {
		Map<String,Object> props = new HashMap<String,Object>();
		// Add required OSGi properties
		addRequiredOSGiProperties(props);
		// Add required ECF properties
		addRequiredECFProperties(props);
		// Add extra properties
		addExtraProperties(props);
		return new EndpointDescription(props);
	}
	
	protected EndpointDescription createFullEndpointDescription() {
		Map<String,Object> props = new HashMap<String,Object>();
		// Add required OSGi properties
		addRequiredOSGiProperties(props);
		// Add full OSGi properties
		addOptionalOSGiProperties(props);
		// Add required ECF properties
		addRequiredECFProperties(props);
		// Add optional ECF properties
		addOptionalECFProperties(props);
		// Add extra properties
		addExtraProperties(props);
		return new EndpointDescription(props);
	}

	protected void addExtraProperties(Map<String, Object> props) {
		props.put(EXTRA_PROPERTY1, "com.foo.bar.propertyvalue1");
		props.put(EXTRA_PROPERTY2, "com.foo.bar.propertyvalue2");
	}

	protected EndpointDescription createBadOSGiEndpointDescrption() throws Exception {
		Map<String,Object> props = new HashMap<String,Object>();
		// Add only ECF properties
		// no OSGi properties
		// Add required ECF properties
		addRequiredECFProperties(props);
		// This should throw a runtime exception 
		return new EndpointDescription(props);
	}
	
	protected EndpointDescription createBadECFEndpointDescrption() throws Exception {
		Map<String,Object> props = new HashMap<String,Object>();
		// Add required OSGi properties
		addRequiredOSGiProperties(props);
		// Add full OSGi properties
		addOptionalOSGiProperties(props);
		
		// No ECF required properties
		// This should throw a runtime exception 
		return new EndpointDescription(props);
	}

	protected String createOSGiServiceImportedConfig() {
		return DEFAULT_SERVICE_IMPORTED_CONFIG;
	}
	
	protected ID createECFContainerID(Map<String,Object> props) {
		String osgiEndpointId = (String) props.get(org.osgi.service.remoteserviceadmin.RemoteConstants.ENDPOINT_ID);
		return getIDFactory().createStringID(osgiEndpointId);
	}
	
	protected ID createECFTargetID(Map<String,Object> props) {
		return getIDFactory().createStringID(DEFAULT_ECF_TARGET_ID);
	}

	protected Long createECFRemoteServiceId(Map<String,Object> props) {
		return new Long(101);
	}
	
	protected void addRequiredOSGiProperties(Map<String,Object> props) {
		// OBJECTCLASS
		props.put(Constants.OBJECTCLASS,createOSGiObjectClass());
		// endpoint.service.id
		props.put(org.osgi.service.remoteserviceadmin.RemoteConstants.ENDPOINT_SERVICE_ID, createOSGiEndpointServiceId());
		// endpoint.framework.id
		props.put(org.osgi.service.remoteserviceadmin.RemoteConstants.ENDPOINT_FRAMEWORK_UUID, createOSGiEndpointFrameworkUUID());
		// endpoint.id
		props.put(org.osgi.service.remoteserviceadmin.RemoteConstants.ENDPOINT_ID, createOSGiEndpointId());
		// service imported configs
		props.put(org.osgi.service.remoteserviceadmin.RemoteConstants.SERVICE_IMPORTED_CONFIGS,createOSGiServiceImportedConfig());
	}
	
	protected void addOptionalOSGiProperties(Map<String,Object> props) {
		props.put(org.osgi.service.remoteserviceadmin.RemoteConstants.SERVICE_INTENTS, createOSGiServiceIntents());
		props.put(org.osgi.service.remoteserviceadmin.RemoteConstants.ENDPOINT_PACKAGE_VERSION_+DEFAULT_SERVICE_INTF_PACKAGE,DEFAULT_SERVICE_INTF_VERSION);
	}
	

	protected Object createOSGiServiceIntents() {
		return new String[] { DEFAULT_SERVICE_INTENT1, DEFAULT_SERVICE_INTENT2 };
	}

	protected void addRequiredECFProperties(Map<String,Object> props) {
		// ecf.endpoint.id
		props.put(RemoteConstants.ENDPOINT_CONTAINER_ID,createECFContainerID(props));
		// ecf.endpoint.
		props.put(RemoteConstants.ENDPOINT_REMOTESERVICE_ID, createECFRemoteServiceId(props));
	}

	protected void addOptionalECFProperties(Map<String,Object> props) {
		// ecf.endpoint.target.id
		props.put(RemoteConstants.ENDPOINT_TARGET_ID,createECFTargetID(props));
		// ecf.endpoint.idfilter.ids
		props.put(RemoteConstants.ENDPOINT_IDFILTER_IDS, createECFIDFilterIDs(props));
		// ecf.endpoint.idfilter
		props.put(RemoteConstants.ENDPOINT_REMOTESERVICE_FILTER, createECFIDFilter(props));
	}

	protected Object createECFIDFilter(Map<String, Object> props) {
		return DEFAULT_IDFILTER;
	}

	protected Object createECFIDFilterIDs(Map<String, Object> props) {
		return new ID[] { getIDFactory().createGUID(), getIDFactory().createGUID() };
	}

	protected IServiceInfo createServiceInfoForDiscovery(EndpointDescription endpointDescription) {
		return serviceInfoFactory.createServiceInfoForDiscovery(discoveryAdvertiser, endpointDescription);
	}
	
	protected EndpointDescription createEndpointDescriptionFromDiscovery(
			IServiceInfo discoveredServiceInfo) {
		DiscoveredEndpointDescription ded = endpointDescriptionFactory.createDiscoveredEndpointDescription(discoveryLocator, discoveredServiceInfo);
		assertNotNull(ded);
		return ded.getEndpointDescription();
	}

}

Back to the top