use of org.easymock.Capture in project felix by apache.
the class ResolverImplTest method createRepositoryAdmin.
private RepositoryAdminImpl createRepositoryAdmin() throws Exception {
BundleContext bundleContext = EasyMock.createMock(BundleContext.class);
Bundle systemBundle = EasyMock.createMock(Bundle.class);
BundleRevision systemBundleRevision = EasyMock.createMock(BundleRevision.class);
Activator.setContext(bundleContext);
EasyMock.expect(bundleContext.getProperty(RepositoryAdminImpl.REPOSITORY_URL_PROP)).andReturn(getClass().getResource("/referred.xml").toExternalForm());
EasyMock.expect(bundleContext.getProperty((String) EasyMock.anyObject())).andReturn(null).anyTimes();
EasyMock.expect(bundleContext.getBundle(0)).andReturn(systemBundle);
EasyMock.expect(bundleContext.installBundle((String) EasyMock.anyObject(), (InputStream) EasyMock.anyObject())).andReturn(systemBundle);
EasyMock.expect(systemBundle.getHeaders()).andReturn(new Hashtable()).anyTimes();
systemBundle.start();
EasyMock.expectLastCall().anyTimes();
EasyMock.expect(systemBundle.getRegisteredServices()).andReturn(null);
EasyMock.expect(new Long(systemBundle.getBundleId())).andReturn(new Long(0)).anyTimes();
EasyMock.expect(systemBundle.getBundleContext()).andReturn(bundleContext);
EasyMock.expect(systemBundleRevision.getCapabilities(null)).andReturn(Collections.<Capability>emptyList());
EasyMock.expect(systemBundle.adapt(BundleRevision.class)).andReturn(systemBundleRevision);
bundleContext.addBundleListener((BundleListener) EasyMock.anyObject());
bundleContext.addServiceListener((ServiceListener) EasyMock.anyObject());
EasyMock.expect(bundleContext.getBundles()).andReturn(new Bundle[] { systemBundle });
final Capture c = new Capture();
EasyMock.expect(bundleContext.createFilter((String) capture(c))).andAnswer(new IAnswer() {
public Object answer() throws Throwable {
return FilterImpl.newInstance((String) c.getValue());
}
}).anyTimes();
EasyMock.replay(new Object[] { bundleContext, systemBundle, systemBundleRevision });
RepositoryAdminImpl repoAdmin = new RepositoryAdminImpl(bundleContext, new Logger(bundleContext));
// force initialization && remove all initial repositories
Repository[] repos = repoAdmin.listRepositories();
for (int i = 0; repos != null && i < repos.length; i++) {
repoAdmin.removeRepository(repos[i].getURI());
}
return repoAdmin;
}
use of org.easymock.Capture in project felix by apache.
the class ConfigInstallerTest method testTypedConfigurationFloat.
public void testTypedConfigurationFloat() throws Exception {
File file = File.createTempFile("test", ".config");
try (OutputStream os = new FileOutputStream(file)) {
os.write("key=F\"1137191584\"\n".getBytes("UTF-8"));
}
String pid = file.getName().substring(0, file.getName().indexOf(".config"));
Capture<Dictionary<String, Object>> props = new Capture<>();
EasyMock.expect(mockConfigurationAdmin.listConfigurations((String) EasyMock.anyObject())).andReturn(null);
EasyMock.expect(mockConfigurationAdmin.getConfiguration(pid, "?")).andReturn(mockConfiguration);
EasyMock.expect(mockConfiguration.getProperties()).andReturn(null);
EasyMock.expect(mockBundleContext.getProperty((String) EasyMock.anyObject())).andReturn(null).anyTimes();
mockConfiguration.update(EasyMock.capture(props));
EasyMock.expectLastCall();
EasyMock.replay(mockConfiguration, mockConfigurationAdmin, mockBundleContext);
ConfigInstaller ci = new ConfigInstaller(mockBundleContext, mockConfigurationAdmin, new FileInstall());
ci.install(file);
EasyMock.verify(mockConfiguration, mockConfigurationAdmin, mockBundleContext);
Dictionary<String, Object> loaded = props.getValue();
assertNotNull(loaded);
assertEquals(400.333F, loaded.get("key"));
}
use of org.easymock.Capture in project felix by apache.
the class ConfigInstallerTest method testUseExistingConfigWithFileinstallFilenameAndObserveCMDeleted.
public void testUseExistingConfigWithFileinstallFilenameAndObserveCMDeleted() throws Exception {
File file = File.createTempFile("test", ".config");
try (OutputStream os = new FileOutputStream(file)) {
os.write("key=F\"1137191584\"\n".getBytes("UTF-8"));
}
String pid = "test";
Capture<Dictionary<String, Object>> propsCapture = new Capture<>();
Dictionary<String, Object> props = new Hashtable<>();
props.put(DirectoryWatcher.FILENAME, file.toURI().toString());
EasyMock.expect(mockConfiguration.getProperties()).andReturn(props);
EasyMock.expect(mockBundleContext.getProperty((String) EasyMock.anyObject())).andReturn(null).anyTimes();
EasyMock.expect(mockConfigurationAdmin.listConfigurations((String) EasyMock.anyObject())).andReturn(null);
EasyMock.expect(mockConfigurationAdmin.getConfiguration(pid, "?")).andReturn(mockConfiguration);
EasyMock.expect(mockConfiguration.getPid()).andReturn(pid);
ServiceReference<ConfigurationAdmin> sr = EasyMock.createMock(ServiceReference.class);
mockConfiguration.update(EasyMock.capture(propsCapture));
EasyMock.expectLastCall();
EasyMock.replay(mockConfiguration, mockConfigurationAdmin, mockBundleContext, sr);
ConfigInstaller ci = new ConfigInstaller(mockBundleContext, mockConfigurationAdmin, new FileInstall());
ci.doConfigurationEvent(new ConfigurationEvent(sr, ConfigurationEvent.CM_UPDATED, null, pid));
ci.doConfigurationEvent(new ConfigurationEvent(sr, ConfigurationEvent.CM_DELETED, null, pid));
assertFalse("Configuration file should be deleted", file.isFile());
}
use of org.easymock.Capture in project felix by apache.
the class ConfigInstallerTest method testUseExistingConfigAndObserveCMDeleted.
public void testUseExistingConfigAndObserveCMDeleted() throws Exception {
String pid = "test";
Capture<Dictionary<String, Object>> props = new Capture<>();
EasyMock.expect(mockConfiguration.getProperties()).andReturn(null);
EasyMock.expect(mockBundleContext.getProperty((String) EasyMock.anyObject())).andReturn(null).anyTimes();
EasyMock.expect(mockConfigurationAdmin.listConfigurations((String) EasyMock.anyObject())).andReturn(null);
EasyMock.expect(mockConfigurationAdmin.getConfiguration(pid, "?")).andReturn(mockConfiguration);
ServiceReference<ConfigurationAdmin> sr = EasyMock.createMock(ServiceReference.class);
mockConfiguration.update(EasyMock.capture(props));
EasyMock.expectLastCall();
EasyMock.replay(mockConfiguration, mockConfigurationAdmin, mockBundleContext, sr);
ConfigInstaller ci = new ConfigInstaller(mockBundleContext, mockConfigurationAdmin, new FileInstall());
ci.doConfigurationEvent(new ConfigurationEvent(sr, ConfigurationEvent.CM_UPDATED, null, pid));
ci.doConfigurationEvent(new ConfigurationEvent(sr, ConfigurationEvent.CM_DELETED, null, pid));
}
use of org.easymock.Capture in project knox by apache.
the class EncryptDecryptUriProcessorTest method testEncryptDecrypt.
@Test
public void testEncryptDecrypt() throws Exception {
String encryptedValueParamName = "address";
String clusterName = "test-cluster-name";
String passwordAlias = "encryptQueryString";
// Test encryption. Result is in encryptedAdrress
AliasService as = EasyMock.createNiceMock(AliasService.class);
String secret = "asdf";
EasyMock.expect(as.getPasswordFromAliasForCluster(clusterName, passwordAlias)).andReturn(secret.toCharArray()).anyTimes();
CryptoService cryptoService = new DefaultCryptoService();
((DefaultCryptoService) cryptoService).setAliasService(as);
GatewayServices gatewayServices = EasyMock.createNiceMock(GatewayServices.class);
EasyMock.expect(gatewayServices.getService(GatewayServices.CRYPTO_SERVICE)).andReturn(cryptoService);
UrlRewriteEnvironment encEnvironment = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
EasyMock.expect(encEnvironment.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(gatewayServices).anyTimes();
EasyMock.expect(encEnvironment.getAttribute(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE)).andReturn(clusterName).anyTimes();
UrlRewriteContext encContext = EasyMock.createNiceMock(UrlRewriteContext.class);
EncryptStepContextParams hostPortParams = new EncryptStepContextParams();
hostPortParams.addParam("host", Arrays.asList("host.yarn.com"));
hostPortParams.addParam("port", Arrays.asList("8088"));
EasyMock.expect(encContext.getParameters()).andReturn(hostPortParams);
Capture<EncryptStepContextParams> encodedValue = new Capture<EncryptStepContextParams>();
encContext.addParameters(EasyMock.capture(encodedValue));
EasyMock.replay(gatewayServices, as, encEnvironment, encContext);
EncryptUriDescriptor descriptor = new EncryptUriDescriptor();
descriptor.setTemplate("{host}:{port}");
descriptor.setParam(encryptedValueParamName);
EncryptUriProcessor processor = new EncryptUriProcessor();
processor.initialize(encEnvironment, descriptor);
UrlRewriteStepStatus encStatus = processor.process(encContext);
assertThat(encStatus, is(UrlRewriteStepStatus.SUCCESS));
assertThat(encodedValue.getValue(), notNullValue());
assertThat(encodedValue.getValue().resolve(encryptedValueParamName).size(), is(1));
String encryptedAdrress = encodedValue.getValue().resolve(encryptedValueParamName).get(0);
assertThat(encryptedAdrress, not(isEmptyOrNullString()));
assertThat(encryptedAdrress, not("{host}:{port}"));
assertThat(encryptedAdrress, not("hdp:8088"));
// Test decryption. Result is in dectryptedAdrress.
String decParam = "foo";
gatewayServices = EasyMock.createNiceMock(GatewayServices.class);
EasyMock.expect(gatewayServices.getService(GatewayServices.CRYPTO_SERVICE)).andReturn(cryptoService);
as = EasyMock.createNiceMock(AliasService.class);
EasyMock.expect(as.getPasswordFromAliasForCluster(clusterName, passwordAlias)).andReturn(secret.toCharArray()).anyTimes();
UrlRewriteEnvironment decEnvironment = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
EasyMock.expect(decEnvironment.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(gatewayServices).anyTimes();
EasyMock.expect(decEnvironment.getAttribute(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE)).andReturn(clusterName).anyTimes();
UrlRewriteContext decContext = EasyMock.createNiceMock(UrlRewriteContext.class);
EncryptStepContextParams encryptedParams = new EncryptStepContextParams();
// Value was encrypted by EncryptUriProcessor
encryptedParams.addParam(decParam, Arrays.asList(encryptedAdrress));
encryptedParams.addParam("foo1", Arrays.asList("test"));
EasyMock.expect(decContext.getParameters()).andReturn(encryptedParams);
Capture<EncryptStepContextParams> decodedValue = new Capture<EncryptStepContextParams>();
decContext.addParameters(EasyMock.capture(decodedValue));
EasyMock.replay(gatewayServices, as, decEnvironment, decContext);
DecryptUriDescriptor decDescriptor = new DecryptUriDescriptor();
decDescriptor.setParam(decParam);
DecryptUriProcessor decProcessor = new DecryptUriProcessor();
decProcessor.initialize(decEnvironment, decDescriptor);
UrlRewriteStepStatus decStatus = decProcessor.process(decContext);
assertThat(decStatus, is(UrlRewriteStepStatus.SUCCESS));
assertThat(decodedValue.getValue(), notNullValue());
assertThat(decodedValue.getValue().resolve(decParam).size(), is(1));
String dectryptedAdrress = decodedValue.getValue().resolve(decParam).get(0);
assertThat(dectryptedAdrress, is("host.yarn.com:8088"));
}
Aggregations