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());
}
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;
}
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");
}
}
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;
}
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");
}
}
Aggregations