Search in sources :

Example 1 with PackageMetadata

use of org.apache.pulsar.packages.management.core.common.PackageMetadata in project pulsar by apache.

the class PackagesApiTest method testPackagesOperations.

@Test(timeOut = 60000)
public void testPackagesOperations() throws Exception {
    // create a temp file for testing
    File file = File.createTempFile("package-api-test", ".package");
    // testing upload api
    String packageName = "function://public/default/test@v1";
    PackageMetadata originalMetadata = PackageMetadata.builder().description("test").build();
    admin.packages().upload(originalMetadata, packageName, file.getPath());
    // testing download api
    String downloadPath = new File(file.getParentFile(), "package-api-test-download.package").getPath();
    admin.packages().download(packageName, downloadPath);
    File downloadFile = new File(downloadPath);
    assertTrue(downloadFile.exists());
    downloadFile.delete();
    // testing list packages api
    List<String> packages = admin.packages().listPackages("function", "public/default");
    assertEquals(packages.size(), 1);
    assertEquals(packages.get(0), "test");
    // testing list versions api
    List<String> versions = admin.packages().listPackageVersions(packageName);
    assertEquals(versions.size(), 1);
    assertEquals(versions.get(0), "v1");
    // testing get packages api
    PackageMetadata metadata = admin.packages().getMetadata(packageName);
    assertEquals(metadata.getDescription(), originalMetadata.getDescription());
    assertNull(metadata.getContact());
    assertTrue(metadata.getModificationTime() > 0);
    assertTrue(metadata.getCreateTime() > 0);
    assertNull(metadata.getProperties());
    // testing update package metadata api
    PackageMetadata updatedMetadata = originalMetadata;
    updatedMetadata.setContact("test@apache.org");
    updatedMetadata.setProperties(Collections.singletonMap("key", "value"));
    admin.packages().updateMetadata(packageName, updatedMetadata);
    PackageMetadata getUpdatedMetadata = admin.packages().getMetadata(packageName);
    assertEquals(getUpdatedMetadata.getDescription(), updatedMetadata.getDescription());
    assertEquals(getUpdatedMetadata.getContact(), updatedMetadata.getContact());
    assertEquals(getUpdatedMetadata.getProperties(), updatedMetadata.getProperties());
}
Also used : PackageMetadata(org.apache.pulsar.packages.management.core.common.PackageMetadata) File(java.io.File) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 2 with PackageMetadata

use of org.apache.pulsar.packages.management.core.common.PackageMetadata in project pulsar by apache.

the class PackagesImpl method getMetadataAsync.

@Override
public CompletableFuture<PackageMetadata> getMetadataAsync(String packageName) {
    WebTarget path = packages.path(PackageName.get(packageName).toRestPath() + "/metadata");
    final CompletableFuture<PackageMetadata> future = new CompletableFuture<>();
    asyncGetRequest(path, new InvocationCallback<PackageMetadata>() {

        @Override
        public void completed(PackageMetadata metadata) {
            future.complete(metadata);
        }

        @Override
        public void failed(Throwable throwable) {
            future.completeExceptionally(getApiException(throwable.getCause()));
        }
    });
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) PackageMetadata(org.apache.pulsar.packages.management.core.common.PackageMetadata) WebTarget(javax.ws.rs.client.WebTarget)

Example 3 with PackageMetadata

use of org.apache.pulsar.packages.management.core.common.PackageMetadata in project pulsar by apache.

the class PackagesManagementImplTest method testPackagesManagementFlow.

@Test
public void testPackagesManagementFlow() {
    PackageName packageName = PackageName.get("function://tenant/ns/non-existent-package@v1");
    // get a non-existent package metadata should fail
    try {
        packagesManagement.getMeta(packageName).get();
    } catch (Exception e) {
        if (!(e.getCause() instanceof PackagesManagementException.NotFoundException)) {
            Assert.fail("should not throw any exception");
        }
    }
    // update a non-existent package metadata should fail
    PackageMetadata failedUpdateMetadata = PackageMetadata.builder().description("Failed update package metadata").build();
    try {
        packagesManagement.updateMeta(packageName, failedUpdateMetadata).get();
    } catch (Exception e) {
        if (!(e.getCause() instanceof PackagesManagementException.NotFoundException)) {
            Assert.fail("should not throw any exception");
        }
    }
    // download a non-existent package should fail
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        packagesManagement.download(packageName, outputStream).get();
    } catch (Exception e) {
        if (!(e.getCause() instanceof PackagesManagementException.NotFoundException)) {
            Assert.fail("should not throw any exception");
        }
    }
    // delete a non-existent package should fail
    try {
        packagesManagement.delete(packageName).get();
    } catch (Exception e) {
        if (!(e.getCause() instanceof PackagesManagementException.NotFoundException)) {
            Assert.fail("should not throw any exception");
        }
    }
    // list a non-existent package version should fail
    try {
        packagesManagement.list(packageName).get();
    } catch (Exception e) {
        if (!(e.getCause() instanceof PackagesManagementException.NotFoundException)) {
            Assert.fail("should not throw any exception");
        }
    }
    // list the packages in a non-existent namespace should fail
    try {
        packagesManagement.list(packageName.getPkgType(), packageName.getTenant(), packageName.getNamespace()).get();
    } catch (Exception e) {
        if (!(e.getCause() instanceof PackagesManagementException.NotFoundException)) {
            Assert.fail("should not throw any exception");
        }
    }
    // upload a package
    PackageMetadata metadata = PackageMetadata.builder().contact("test@apache.org").description("A mocked test package").createTime(System.currentTimeMillis()).build();
    try (ByteArrayInputStream inputStream = new ByteArrayInputStream(PackageMetadataUtil.toBytes(metadata))) {
        packagesManagement.upload(packageName, metadata, inputStream).get();
    } catch (Exception e) {
        Assert.fail("should not throw any exception");
    }
    // get an existent package metadata should succeed
    try {
        PackageMetadata getPackageMetadata = packagesManagement.getMeta(packageName).get();
        Assert.assertEquals(metadata, getPackageMetadata);
    } catch (Exception e) {
        Assert.fail("should not throw any exception");
    }
    // download an existent package should succeed
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        packagesManagement.download(packageName, outputStream).get();
        PackageMetadata getPackage = PackageMetadataUtil.fromBytes(outputStream.toByteArray());
        Assert.assertEquals(metadata, getPackage);
    } catch (Exception e) {
        Assert.fail("should not throw any exception");
    }
    // update an existent package metadata should succeed
    metadata.setModificationTime(System.currentTimeMillis());
    try {
        packagesManagement.updateMeta(packageName, metadata).get();
    } catch (Exception e) {
        if (!(e.getCause() instanceof PackagesManagementException.NotFoundException)) {
            Assert.fail("should not throw any exception");
        }
    }
    // get the updated metadata
    try {
        PackageMetadata updatedMetadata = packagesManagement.getMeta(packageName).get();
        Assert.assertEquals(metadata, updatedMetadata);
    } catch (Exception e) {
        Assert.fail("should not throw any exception");
    }
    // list an existent package version should success
    try {
        List<String> versions = packagesManagement.list(packageName).get();
        Assert.assertEquals(1, versions.size());
        Assert.assertEquals(packageName.getVersion(), versions.get(0));
    } catch (Exception e) {
        if (!(e.getCause() instanceof PackagesManagementException.NotFoundException)) {
            Assert.fail("should not throw any exception");
        }
    }
    // list the packages in a non-existent namespace should fail
    try {
        List<String> packageNames = packagesManagement.list(packageName.getPkgType(), packageName.getTenant(), packageName.getNamespace()).get();
        Assert.assertEquals(1, packageNames.size());
        Assert.assertEquals(packageName.getName(), packageNames.get(0));
    } catch (Exception e) {
        if (!(e.getCause() instanceof PackagesManagementException.NotFoundException)) {
            Assert.fail("should not throw any exception");
        }
    }
    // delete an existent package should succeed
    try {
        packagesManagement.delete(packageName).get();
    } catch (Exception e) {
        Assert.fail("should not throw any exception");
    }
}
Also used : PackageName(org.apache.pulsar.packages.management.core.common.PackageName) ByteArrayInputStream(java.io.ByteArrayInputStream) PackageMetadata(org.apache.pulsar.packages.management.core.common.PackageMetadata) PackagesManagementException(org.apache.pulsar.packages.management.core.exceptions.PackagesManagementException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PackagesManagementException(org.apache.pulsar.packages.management.core.exceptions.PackagesManagementException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test)

Example 4 with PackageMetadata

use of org.apache.pulsar.packages.management.core.common.PackageMetadata in project pulsar by yahoo.

the class PackagesManagementImpl method getMeta.

@Override
public CompletableFuture<PackageMetadata> getMeta(PackageName packageName) {
    CompletableFuture<PackageMetadata> future = new CompletableFuture<>();
    String metadataPath = metadataPath(packageName);
    checkMetadataNotExistsAndThrowException(packageName).whenComplete((ignore, throwable) -> {
        if (throwable != null) {
            future.completeExceptionally(throwable);
            return;
        }
        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            storage.readAsync(metadataPath, outputStream).thenCompose(aVoid -> metadataReadFromStream(outputStream)).whenComplete((metadata, t) -> {
                if (t != null) {
                    future.completeExceptionally(t);
                } else {
                    future.complete(metadata);
                }
            });
        } catch (IOException e) {
            future.completeExceptionally(new PackagesManagementException(String.format("Read package '%s' metadata failed", packageName.toString()), e));
        }
    });
    return future;
}
Also used : OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PackagesManagementException(org.apache.pulsar.packages.management.core.exceptions.PackagesManagementException) IOException(java.io.IOException) CompletableFuture(java.util.concurrent.CompletableFuture) PackageMetadataUtil(org.apache.pulsar.packages.management.core.common.PackageMetadataUtil) PackageMetadata(org.apache.pulsar.packages.management.core.common.PackageMetadata) PackageType(org.apache.pulsar.packages.management.core.common.PackageType) List(java.util.List) PackagesStorage(org.apache.pulsar.packages.management.core.PackagesStorage) ByteArrayInputStream(java.io.ByteArrayInputStream) NotFoundException(org.apache.pulsar.packages.management.core.exceptions.PackagesManagementException.NotFoundException) PackagesManagement(org.apache.pulsar.packages.management.core.PackagesManagement) PackageName(org.apache.pulsar.packages.management.core.common.PackageName) InputStream(java.io.InputStream) CompletableFuture(java.util.concurrent.CompletableFuture) PackageMetadata(org.apache.pulsar.packages.management.core.common.PackageMetadata) PackagesManagementException(org.apache.pulsar.packages.management.core.exceptions.PackagesManagementException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 5 with PackageMetadata

use of org.apache.pulsar.packages.management.core.common.PackageMetadata in project pulsar by yahoo.

the class PackagesManagementImplTest method testPackagesManagementFlow.

@Test
public void testPackagesManagementFlow() {
    PackageName packageName = PackageName.get("function://tenant/ns/non-existent-package@v1");
    // get a non-existent package metadata should fail
    try {
        packagesManagement.getMeta(packageName).get();
    } catch (Exception e) {
        if (!(e.getCause() instanceof PackagesManagementException.NotFoundException)) {
            Assert.fail("should not throw any exception");
        }
    }
    // update a non-existent package metadata should fail
    PackageMetadata failedUpdateMetadata = PackageMetadata.builder().description("Failed update package metadata").build();
    try {
        packagesManagement.updateMeta(packageName, failedUpdateMetadata).get();
    } catch (Exception e) {
        if (!(e.getCause() instanceof PackagesManagementException.NotFoundException)) {
            Assert.fail("should not throw any exception");
        }
    }
    // download a non-existent package should fail
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        packagesManagement.download(packageName, outputStream).get();
    } catch (Exception e) {
        if (!(e.getCause() instanceof PackagesManagementException.NotFoundException)) {
            Assert.fail("should not throw any exception");
        }
    }
    // delete a non-existent package should fail
    try {
        packagesManagement.delete(packageName).get();
    } catch (Exception e) {
        if (!(e.getCause() instanceof PackagesManagementException.NotFoundException)) {
            Assert.fail("should not throw any exception");
        }
    }
    // list a non-existent package version should fail
    try {
        packagesManagement.list(packageName).get();
    } catch (Exception e) {
        if (!(e.getCause() instanceof PackagesManagementException.NotFoundException)) {
            Assert.fail("should not throw any exception");
        }
    }
    // list the packages in a non-existent namespace should fail
    try {
        packagesManagement.list(packageName.getPkgType(), packageName.getTenant(), packageName.getNamespace()).get();
    } catch (Exception e) {
        if (!(e.getCause() instanceof PackagesManagementException.NotFoundException)) {
            Assert.fail("should not throw any exception");
        }
    }
    // upload a package
    PackageMetadata metadata = PackageMetadata.builder().contact("test@apache.org").description("A mocked test package").createTime(System.currentTimeMillis()).build();
    try (ByteArrayInputStream inputStream = new ByteArrayInputStream(PackageMetadataUtil.toBytes(metadata))) {
        packagesManagement.upload(packageName, metadata, inputStream).get();
    } catch (Exception e) {
        Assert.fail("should not throw any exception");
    }
    // get an existent package metadata should succeed
    try {
        PackageMetadata getPackageMetadata = packagesManagement.getMeta(packageName).get();
        Assert.assertEquals(metadata, getPackageMetadata);
    } catch (Exception e) {
        Assert.fail("should not throw any exception");
    }
    // download an existent package should succeed
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        packagesManagement.download(packageName, outputStream).get();
        PackageMetadata getPackage = PackageMetadataUtil.fromBytes(outputStream.toByteArray());
        Assert.assertEquals(metadata, getPackage);
    } catch (Exception e) {
        Assert.fail("should not throw any exception");
    }
    // update an existent package metadata should succeed
    metadata.setModificationTime(System.currentTimeMillis());
    try {
        packagesManagement.updateMeta(packageName, metadata).get();
    } catch (Exception e) {
        if (!(e.getCause() instanceof PackagesManagementException.NotFoundException)) {
            Assert.fail("should not throw any exception");
        }
    }
    // get the updated metadata
    try {
        PackageMetadata updatedMetadata = packagesManagement.getMeta(packageName).get();
        Assert.assertEquals(metadata, updatedMetadata);
    } catch (Exception e) {
        Assert.fail("should not throw any exception");
    }
    // list an existent package version should success
    try {
        List<String> versions = packagesManagement.list(packageName).get();
        Assert.assertEquals(1, versions.size());
        Assert.assertEquals(packageName.getVersion(), versions.get(0));
    } catch (Exception e) {
        if (!(e.getCause() instanceof PackagesManagementException.NotFoundException)) {
            Assert.fail("should not throw any exception");
        }
    }
    // list the packages in a non-existent namespace should fail
    try {
        List<String> packageNames = packagesManagement.list(packageName.getPkgType(), packageName.getTenant(), packageName.getNamespace()).get();
        Assert.assertEquals(1, packageNames.size());
        Assert.assertEquals(packageName.getName(), packageNames.get(0));
    } catch (Exception e) {
        if (!(e.getCause() instanceof PackagesManagementException.NotFoundException)) {
            Assert.fail("should not throw any exception");
        }
    }
    // delete an existent package should succeed
    try {
        packagesManagement.delete(packageName).get();
    } catch (Exception e) {
        Assert.fail("should not throw any exception");
    }
}
Also used : PackageName(org.apache.pulsar.packages.management.core.common.PackageName) ByteArrayInputStream(java.io.ByteArrayInputStream) PackageMetadata(org.apache.pulsar.packages.management.core.common.PackageMetadata) PackagesManagementException(org.apache.pulsar.packages.management.core.exceptions.PackagesManagementException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PackagesManagementException(org.apache.pulsar.packages.management.core.exceptions.PackagesManagementException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test)

Aggregations

PackageMetadata (org.apache.pulsar.packages.management.core.common.PackageMetadata)25 CompletableFuture (java.util.concurrent.CompletableFuture)15 IOException (java.io.IOException)14 PackageName (org.apache.pulsar.packages.management.core.common.PackageName)14 PackagesManagementException (org.apache.pulsar.packages.management.core.exceptions.PackagesManagementException)12 File (java.io.File)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 InputStream (java.io.InputStream)9 List (java.util.List)9 OutputStream (java.io.OutputStream)6 WebTarget (javax.ws.rs.client.WebTarget)6 PackagesManagement (org.apache.pulsar.packages.management.core.PackagesManagement)6 PackagesStorage (org.apache.pulsar.packages.management.core.PackagesStorage)6 PackageMetadataUtil (org.apache.pulsar.packages.management.core.common.PackageMetadataUtil)6 PackageType (org.apache.pulsar.packages.management.core.common.PackageType)6 NotFoundException (org.apache.pulsar.packages.management.core.exceptions.PackagesManagementException.NotFoundException)6 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)5 FunctionCommon.createPkgTempFile (org.apache.pulsar.functions.utils.FunctionCommon.createPkgTempFile)4 FunctionsImpl.downloadPackageFile (org.apache.pulsar.functions.worker.rest.api.FunctionsImpl.downloadPackageFile)4