Search in sources :

Example 56 with Capture

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;
}
Also used : Hashtable(java.util.Hashtable) Logger(org.apache.felix.utils.log.Logger) Capture(org.easymock.Capture) IAnswer(org.easymock.IAnswer) BundleRevision(org.osgi.framework.wiring.BundleRevision)

Example 57 with Capture

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"));
}
Also used : Dictionary(java.util.Dictionary) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Capture(org.easymock.Capture)

Example 58 with Capture

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());
}
Also used : ConfigurationEvent(org.osgi.service.cm.ConfigurationEvent) Dictionary(java.util.Dictionary) Hashtable(java.util.Hashtable) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Capture(org.easymock.Capture) FileOutputStream(java.io.FileOutputStream) File(java.io.File) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 59 with Capture

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));
}
Also used : ConfigurationEvent(org.osgi.service.cm.ConfigurationEvent) Dictionary(java.util.Dictionary) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) Capture(org.easymock.Capture)

Example 60 with Capture

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"));
}
Also used : UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) GatewayServices(org.apache.knox.gateway.services.GatewayServices) EncryptStepContextParams(org.apache.knox.gateway.encrypturi.EncryptStepContextParams) AliasService(org.apache.knox.gateway.services.security.AliasService) EncryptUriDescriptor(org.apache.knox.gateway.encrypturi.api.EncryptUriDescriptor) IsEmptyString.isEmptyOrNullString(org.hamcrest.text.IsEmptyString.isEmptyOrNullString) UrlRewriteContext(org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext) Capture(org.easymock.Capture) UrlRewriteStepStatus(org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus) DefaultCryptoService(org.apache.knox.gateway.services.security.impl.DefaultCryptoService) CryptoService(org.apache.knox.gateway.services.security.CryptoService) DecryptUriDescriptor(org.apache.knox.gateway.encrypturi.api.DecryptUriDescriptor) DefaultCryptoService(org.apache.knox.gateway.services.security.impl.DefaultCryptoService) Test(org.junit.Test)

Aggregations

Capture (org.easymock.Capture)79 Test (org.junit.Test)42 INetworkManagementEventObserver (android.net.INetworkManagementEventObserver)12 File (java.io.File)10 Intent (android.content.Intent)8 IConnectivityManager (android.net.IConnectivityManager)8 INetworkManagementService (android.os.INetworkManagementService)8 TrustedTime (android.util.TrustedTime)8 IAnswer (org.easymock.IAnswer)7 CacheRefreshedEvent (com.netflix.discovery.CacheRefreshedEvent)6 EurekaEventListener (com.netflix.discovery.EurekaEventListener)6 Map (java.util.Map)6 UrlRewriteEnvironment (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment)6 UrlRewriteContext (org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext)6 OutputStream (java.io.OutputStream)5 HashMap (java.util.HashMap)5 Template (org.apache.knox.gateway.util.urltemplate.Template)5 EurekaClient (com.netflix.discovery.EurekaClient)4 ServerListUpdater (com.netflix.loadbalancer.ServerListUpdater)4 URI (java.net.URI)4