Search in sources :

Example 6 with PluginCoordinates

use of io.apiman.common.plugin.PluginCoordinates in project apiman by apiman.

the class VertxPluginRegistryTest method getFakePluginUnreachableRegistry.

/**
 * Test with custom configuration (pluginRepositories and pluginsDir) to download a fake plugin from a fake Maven repo
 * Despite a unreachable repository in the pluginRepositories array.
 *
 * @param context
 * @throws java.io.IOException
 */
@Test
public void getFakePluginUnreachableRegistry(TestContext context) throws java.io.IOException {
    Async waitForPlugin = context.async();
    // Preparing JSON config Object with 2 custom repo : a real and a fake
    List<String> pluginRepositories = Arrays.asList("https://unreachable.maven.org/maven2/", mavenServerUri.toString());
    String pluginsDir = "/tmp/plugins-test3";
    JsonObject jsonObject = new JsonObject(getJsonConfig(pluginRepositories, pluginsDir));
    // Delete temp folder
    File TempDir = new File(pluginsDir);
    if (TempDir.exists())
        FileUtils.deleteDirectory(TempDir);
    // Referenced values to test
    Map<String, String> expected = new LinkedHashMap<String, String>() {

        {
            put("pluginRepositories", String.join(",", pluginRepositories));
            put("pluginsDir", pluginsDir);
        }
    };
    // Loading VertX configuration
    VertxEngineConfig config = new VertxEngineConfig(jsonObject);
    Map<String, String> pluginRegistryConfig = config.getPluginRegistryConfig();
    // Assert that JSON config object contains the rights parameters
    Assert.assertThat(pluginRegistryConfig, is(expected));
    // Create a fake engine for test plugins loading
    TestVerticle v = new TestVerticle(config);
    // Get pluginRegistry from engine
    IPluginRegistry pluginRegistry = v.createPluginRegistry();
    // Define simple header policy plugin coordinates
    PluginCoordinates coordinates = PluginCoordinates.fromPolicySpec(testPluginCoordinates);
    // Download the plugin
    pluginRegistry.loadPlugin(coordinates, result -> {
        if (result.isSuccess()) {
            // Get downloaded plugin
            Plugin plugin = result.getResult();
            // Assert that's the right plugin
            context.assertEquals(plugin.getCoordinates(), coordinates);
            // Assert plugin is in the right dir
            Path pluginPath = Paths.get(pluginsDir + "/io.apiman.test/testPlugin/1.0.0.Final/testPlugin.war");
            context.assertTrue(Files.exists(pluginPath));
            waitForPlugin.complete();
        } else {
            context.fail(result.getError());
        }
    });
    waitForPlugin.awaitSuccess();
}
Also used : IPluginRegistry(io.apiman.gateway.engine.IPluginRegistry) Path(java.nio.file.Path) JsonObject(io.vertx.core.json.JsonObject) Async(io.vertx.ext.unit.Async) VertxEngineConfig(io.apiman.gateway.platforms.vertx3.common.config.VertxEngineConfig) PluginCoordinates(io.apiman.common.plugin.PluginCoordinates) File(java.io.File) Plugin(io.apiman.common.plugin.Plugin) Test(org.junit.Test)

Example 7 with PluginCoordinates

use of io.apiman.common.plugin.PluginCoordinates in project apiman by apiman.

the class VertxPluginRegistryTest method getRealPluginFromCustomRegistry.

/**
 * Test with custom configuration (pluginRepositories and pluginsDir) to download a real plugin from Maven Central
 *
 * @param context
 * @throws java.io.IOException
 */
@Test
public void getRealPluginFromCustomRegistry(TestContext context) throws java.io.IOException {
    Async waitForPlugin = context.async();
    // Preparing JSON config Object
    List<String> pluginRepositories = Arrays.asList("https://repo1.maven.org/maven2/");
    String pluginsDir = "/tmp/plugins-test1";
    JsonObject jsonObject = new JsonObject(getJsonConfig(pluginRepositories, pluginsDir));
    // Delete temp folder
    File TempDir = new File(pluginsDir);
    if (TempDir.exists())
        FileUtils.deleteDirectory(TempDir);
    // Referenced values to test
    Map<String, String> expected = new LinkedHashMap<String, String>() {

        {
            put("pluginRepositories", String.join(",", pluginRepositories));
            put("pluginsDir", pluginsDir);
        }
    };
    // Loading VertX configuration
    VertxEngineConfig config = new VertxEngineConfig(jsonObject);
    Map<String, String> pluginRegistryConfig = config.getPluginRegistryConfig();
    // Assert that JSON config object contains the rights parameters
    Assert.assertThat(pluginRegistryConfig, is(expected));
    // Create a fake engine for test plugins loading
    TestVerticle v = new TestVerticle(config);
    // Get pluginRegistry from engine
    IPluginRegistry pluginRegistry = v.createPluginRegistry();
    // Define simple header policy plugin coordinates
    PluginCoordinates coordinates = PluginCoordinates.fromPolicySpec(RealPluginCoordinates);
    // Download the plugin
    pluginRegistry.loadPlugin(coordinates, result -> {
        if (result.isSuccess()) {
            // Get downloaded plugin
            Plugin plugin = result.getResult();
            // Assert that's the right plugin
            context.assertEquals(plugin.getCoordinates(), coordinates);
            // Assert plugin is in the right dir
            Path pluginPath = Paths.get(pluginsDir + "/io.apiman.plugins/apiman-plugins-simple-header-policy/1.5.1.Final/apiman-plugins-simple-header-policy.war");
            context.assertTrue(Files.exists(pluginPath));
            waitForPlugin.complete();
        } else {
            context.fail(result.getError());
        }
    });
    waitForPlugin.awaitSuccess();
}
Also used : IPluginRegistry(io.apiman.gateway.engine.IPluginRegistry) Path(java.nio.file.Path) JsonObject(io.vertx.core.json.JsonObject) Async(io.vertx.ext.unit.Async) VertxEngineConfig(io.apiman.gateway.platforms.vertx3.common.config.VertxEngineConfig) PluginCoordinates(io.apiman.common.plugin.PluginCoordinates) File(java.io.File) Plugin(io.apiman.common.plugin.Plugin) Test(org.junit.Test)

Example 8 with PluginCoordinates

use of io.apiman.common.plugin.PluginCoordinates in project apiman by apiman.

the class VertxPluginRegistryTest method BuildLocalMavenRepo.

/**
 * Build and start a fake local Maven Repository server based on Jetty
 *
 * @throws Exception
 */
@BeforeClass
public static void BuildLocalMavenRepo() throws Exception {
    // This function create a Localhost fake Maven Repository, hosting a single Test Plugin
    // Get Test plugin file
    File sourcePlugin = new File("src/test/resources/io/apiman/gateway/platforms/vertx3/engine/plugin-with-policyDefs.war");
    if (!sourcePlugin.exists()) {
        throw new Exception("Failed to find test plugin war at: " + sourcePlugin.getAbsolutePath());
    }
    // Create Local Maven Repository folder
    File repoFolder = Files.createTempDirectory("MockedMavenRepo").toFile();
    // Define Test plugin coordinates
    PluginCoordinates coordinates = PluginCoordinates.fromPolicySpec(testPluginCoordinates);
    // Build Test Plugin path in local Maven Repository folder
    File PluginFile = new File(repoFolder, PluginUtils.getMavenPath(coordinates));
    PluginFile.getParentFile().mkdirs();
    // Copy Test Plugin war into repository
    FileUtils.copyFile(sourcePlugin, PluginFile);
    // Create local Maven Repository Web Server
    mavenServer = new Server();
    ServerConnector connector = new ServerConnector(mavenServer);
    // auto-bind to available port
    connector.setPort(0);
    connector.setHost("0.0.0.0");
    mavenServer.addConnector(connector);
    // Add classic ressource handler
    ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setDirectoriesListed(true);
    resourceHandler.setWelcomeFiles(new String[] { "index.html" });
    PathResource pathResource = new PathResource(repoFolder);
    resourceHandler.setBaseResource(pathResource);
    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { resourceHandler, new DefaultHandler() });
    mavenServer.setHandler(handlers);
    // Start local Maven Repository Server
    mavenServer.start();
    // Determine Base URI for Server
    String host = connector.getHost();
    if (host == null || host == "0.0.0.0")
        host = "127.0.0.1";
    int port = connector.getLocalPort();
    mavenServerUri = new URI(String.format("http://%s:%d/", host, port));
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) HandlerList(org.eclipse.jetty.server.handler.HandlerList) Server(org.eclipse.jetty.server.Server) PathResource(org.eclipse.jetty.util.resource.PathResource) PluginCoordinates(io.apiman.common.plugin.PluginCoordinates) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) File(java.io.File) URI(java.net.URI) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) BeforeClass(org.junit.BeforeClass)

Example 9 with PluginCoordinates

use of io.apiman.common.plugin.PluginCoordinates in project apiman by apiman.

the class VertxPluginRegistryTest method getRealPluginFromDefaultRegistry.

@Test
public void getRealPluginFromDefaultRegistry(TestContext context) throws java.io.IOException {
    Async waitForPlugin1 = context.async();
    // Preparing JSON config Object
    List<String> pluginRepositories = null;
    String pluginsDir = null;
    JsonObject jsonObject = new JsonObject(getJsonConfig(pluginRepositories, pluginsDir));
    File TempParentDir = Files.createTempDirectory("apiman-gateway-plugins-tmp").getParent().toFile();
    // Delete all old temp dirs
    File[] TempDirs = TempParentDir.listFiles((dir, name) -> name.contains("apiman-gateway-plugins-tmp"));
    for (File td : TempDirs) {
        FileUtils.deleteDirectory(td);
    }
    // Loading VertX configuration
    VertxEngineConfig config = new VertxEngineConfig(jsonObject);
    // Create a fake engine for test plugins loading
    TestVerticle v = new TestVerticle(config);
    // Get pluginRegistry from engine
    IPluginRegistry pluginRegistry = v.createPluginRegistry();
    // Find generated Plugin Temp Dir
    TempDirs = TempParentDir.listFiles((dir, name) -> name.contains("apiman-gateway-plugins-tmp"));
    Arrays.sort(TempDirs, Comparator.comparingLong(File::lastModified).reversed());
    File Tempdir = TempDirs[0];
    // Define simple header policy plugin coordinates
    PluginCoordinates coordinates = PluginCoordinates.fromPolicySpec(RealPluginCoordinates);
    // Download the Real plugin (must succeed)
    pluginRegistry.loadPlugin(coordinates, result -> {
        if (result.isSuccess()) {
            // Get downloaded plugin
            Plugin plugin = result.getResult();
            // Assert that's the right plugin
            context.assertEquals(plugin.getCoordinates(), coordinates);
            // Assert plugin is in the right dir
            Path pluginPath = Paths.get(Tempdir + "/io.apiman.plugins/apiman-plugins-simple-header-policy/1.5.1.Final/apiman-plugins-simple-header-policy.war");
            context.assertTrue(Files.exists(pluginPath));
            waitForPlugin1.complete();
        } else {
            context.fail(result.getError());
        }
    });
    waitForPlugin1.awaitSuccess();
    Async waitForPlugin2 = context.async();
    // Define simple header policy plugin coordinates
    PluginCoordinates testcoordinates = PluginCoordinates.fromPolicySpec(testPluginCoordinates);
    // Try download the test plugin (must failed)
    pluginRegistry.loadPlugin(testcoordinates, result -> {
        context.assertFalse(result.isSuccess());
        waitForPlugin2.complete();
    });
    waitForPlugin2.awaitSuccess();
}
Also used : IPluginRegistry(io.apiman.gateway.engine.IPluginRegistry) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) Handler(org.eclipse.jetty.server.Handler) StringUtils(org.apache.commons.lang.StringUtils) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) PathResource(org.eclipse.jetty.util.resource.PathResource) java.util(java.util) BeforeClass(org.junit.BeforeClass) RunWith(org.junit.runner.RunWith) VertxEngineConfig(io.apiman.gateway.platforms.vertx3.common.config.VertxEngineConfig) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) Plugin(io.apiman.common.plugin.Plugin) Is.is(org.hamcrest.core.Is.is) JsonObject(io.vertx.core.json.JsonObject) URI(java.net.URI) Server(org.eclipse.jetty.server.Server) Path(java.nio.file.Path) AfterClass(org.junit.AfterClass) HandlerList(org.eclipse.jetty.server.handler.HandlerList) Files(java.nio.file.Files) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) File(java.io.File) JsonArray(io.vertx.core.json.JsonArray) ServerConnector(org.eclipse.jetty.server.ServerConnector) IPluginRegistry(io.apiman.gateway.engine.IPluginRegistry) Paths(java.nio.file.Paths) PluginUtils(io.apiman.common.plugin.PluginUtils) Assert(org.junit.Assert) PluginCoordinates(io.apiman.common.plugin.PluginCoordinates) Path(java.nio.file.Path) JsonObject(io.vertx.core.json.JsonObject) Async(io.vertx.ext.unit.Async) VertxEngineConfig(io.apiman.gateway.platforms.vertx3.common.config.VertxEngineConfig) PluginCoordinates(io.apiman.common.plugin.PluginCoordinates) File(java.io.File) Plugin(io.apiman.common.plugin.Plugin) Test(org.junit.Test)

Aggregations

PluginCoordinates (io.apiman.common.plugin.PluginCoordinates)9 Plugin (io.apiman.common.plugin.Plugin)8 File (java.io.File)6 Test (org.junit.Test)5 IPluginRegistry (io.apiman.gateway.engine.IPluginRegistry)4 VertxEngineConfig (io.apiman.gateway.platforms.vertx3.common.config.VertxEngineConfig)4 JsonObject (io.vertx.core.json.JsonObject)4 Async (io.vertx.ext.unit.Async)4 Path (java.nio.file.Path)4 PluginClassLoader (io.apiman.common.plugin.PluginClassLoader)2 NewPluginBean (io.apiman.manager.api.beans.plugins.NewPluginBean)2 PluginBean (io.apiman.manager.api.beans.plugins.PluginBean)2 PolicyDefinitionBean (io.apiman.manager.api.beans.policies.PolicyDefinitionBean)2 InvalidPluginException (io.apiman.manager.api.core.exceptions.InvalidPluginException)2 StorageException (io.apiman.manager.api.core.exceptions.StorageException)2 AbstractRestException (io.apiman.manager.api.rest.exceptions.AbstractRestException)2 NotAuthorizedException (io.apiman.manager.api.rest.exceptions.NotAuthorizedException)2 PluginAlreadyExistsException (io.apiman.manager.api.rest.exceptions.PluginAlreadyExistsException)2 PluginNotFoundException (io.apiman.manager.api.rest.exceptions.PluginNotFoundException)2 PluginResourceNotFoundException (io.apiman.manager.api.rest.exceptions.PluginResourceNotFoundException)2