Search in sources :

Example 16 with BundlesData

use of org.apache.pulsar.common.policies.data.BundlesData in project incubator-pulsar by apache.

the class NamespacesTest method testRetention.

@Test
public void testRetention() throws Exception {
    try {
        URL localWebServiceUrl = new URL(pulsar.getWebServiceAddress());
        String bundledNsLocal = "test-bundled-namespace-1";
        BundlesData bundleData = new BundlesData(Lists.newArrayList("0x00000000", "0xffffffff"));
        createBundledTestNamespaces(this.testProperty, this.testLocalCluster, bundledNsLocal, bundleData);
        final NamespaceName testNs = NamespaceName.get(this.testProperty, this.testLocalCluster, bundledNsLocal);
        mockWebUrl(localWebServiceUrl, testNs);
        OwnershipCache MockOwnershipCache = spy(pulsar.getNamespaceService().getOwnershipCache());
        doNothing().when(MockOwnershipCache).disableOwnership(any(NamespaceBundle.class));
        Field ownership = NamespaceService.class.getDeclaredField("ownershipCache");
        ownership.setAccessible(true);
        ownership.set(pulsar.getNamespaceService(), MockOwnershipCache);
        RetentionPolicies retention = new RetentionPolicies(10, 10);
        namespaces.setRetention(this.testProperty, this.testLocalCluster, bundledNsLocal, retention);
        RetentionPolicies retention2 = namespaces.getRetention(this.testProperty, this.testLocalCluster, bundledNsLocal);
        assertEquals(retention, retention2);
    } catch (RestException e) {
        fail("ValidateNamespaceOwnershipWithBundles failed");
    }
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) RetentionPolicies(org.apache.pulsar.common.policies.data.RetentionPolicies) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) Field(java.lang.reflect.Field) OwnershipCache(org.apache.pulsar.broker.namespace.OwnershipCache) BundlesData(org.apache.pulsar.common.policies.data.BundlesData) RestException(org.apache.pulsar.broker.web.RestException) URL(java.net.URL) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 17 with BundlesData

use of org.apache.pulsar.common.policies.data.BundlesData in project incubator-pulsar by apache.

the class NamespacesTest method testDeleteNamespaceWithBundles.

@Test
public void testDeleteNamespaceWithBundles() throws Exception {
    URL localWebServiceUrl = new URL(pulsar.getWebServiceAddress());
    String bundledNsLocal = "test-bundled-namespace-1";
    BundlesData bundleData = new BundlesData(Lists.newArrayList("0x00000000", "0x80000000", "0xffffffff"));
    createBundledTestNamespaces(this.testProperty, this.testLocalCluster, bundledNsLocal, bundleData);
    final NamespaceName testNs = NamespaceName.get(this.testProperty, this.testLocalCluster, bundledNsLocal);
    org.apache.pulsar.client.admin.Namespaces namespacesAdmin = mock(org.apache.pulsar.client.admin.Namespaces.class);
    doReturn(namespacesAdmin).when(admin).namespaces();
    doReturn(null).when(nsSvc).getWebServiceUrl(Mockito.argThat(new Matcher<NamespaceBundle>() {

        @Override
        public void describeTo(Description description) {
        }

        @Override
        public boolean matches(Object item) {
            if (item instanceof NamespaceBundle) {
                NamespaceBundle bundle = (NamespaceBundle) item;
                return bundle.getNamespaceObject().equals(testNs);
            }
            return false;
        }

        @Override
        public void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
        }
    }), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anyBoolean());
    doReturn(false).when(nsSvc).isServiceUnitOwned(Mockito.argThat(new Matcher<NamespaceBundle>() {

        @Override
        public void describeTo(Description description) {
        }

        @Override
        public boolean matches(Object item) {
            if (item instanceof NamespaceBundle) {
                NamespaceBundle bundle = (NamespaceBundle) item;
                return bundle.getNamespaceObject().equals(testNs);
            }
            return false;
        }

        @Override
        public void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
        }
    }));
    doReturn(Optional.of(new NamespaceEphemeralData())).when(nsSvc).getOwner(Mockito.argThat(new Matcher<NamespaceBundle>() {

        @Override
        public void describeTo(Description description) {
        }

        @Override
        public boolean matches(Object item) {
            if (item instanceof NamespaceBundle) {
                NamespaceBundle bundle = (NamespaceBundle) item;
                return bundle.getNamespaceObject().equals(testNs);
            }
            return false;
        }

        @Override
        public void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
        }
    }));
    doThrow(new PulsarAdminException.PreconditionFailedException(new ClientErrorException(Status.PRECONDITION_FAILED))).when(namespacesAdmin).deleteNamespaceBundle(Mockito.anyString(), Mockito.anyString());
    try {
        namespaces.deleteNamespaceBundle(testProperty, testLocalCluster, bundledNsLocal, "0x00000000_0x80000000", false);
        fail("Should have failed");
    } catch (RestException re) {
        assertEquals(re.getResponse().getStatus(), Status.PRECONDITION_FAILED.getStatusCode());
    }
    try {
        namespaces.deleteNamespace(testProperty, testLocalCluster, bundledNsLocal, false);
        fail("Should have failed");
    } catch (RestException re) {
        assertEquals(re.getResponse().getStatus(), Status.PRECONDITION_FAILED.getStatusCode());
    }
    NamespaceBundles nsBundles = nsSvc.getNamespaceBundleFactory().getBundles(testNs, bundleData);
    // make one bundle owned
    doReturn(Optional.of(localWebServiceUrl)).when(nsSvc).getWebServiceUrl(nsBundles.getBundles().get(0), false, true, false);
    doReturn(true).when(nsSvc).isServiceUnitOwned(nsBundles.getBundles().get(0));
    doNothing().when(namespacesAdmin).deleteNamespaceBundle(testProperty + "/" + testLocalCluster + "/" + bundledNsLocal, "0x00000000_0x80000000");
    try {
        namespaces.deleteNamespaceBundle(testProperty, testLocalCluster, bundledNsLocal, "0x80000000_0xffffffff", false);
        fail("Should have failed");
    } catch (RestException re) {
        assertEquals(re.getResponse().getStatus(), Status.PRECONDITION_FAILED.getStatusCode());
    }
    try {
        namespaces.deleteNamespace(testProperty, testLocalCluster, bundledNsLocal, false);
        fail("should have failed");
    } catch (RestException re) {
        assertEquals(re.getResponse().getStatus(), Status.PRECONDITION_FAILED.getStatusCode());
    }
    // ensure all three bundles are owned by the local broker
    for (NamespaceBundle bundle : nsBundles.getBundles()) {
        doReturn(Optional.of(localWebServiceUrl)).when(nsSvc).getWebServiceUrl(bundle, false, true, false);
        doReturn(true).when(nsSvc).isServiceUnitOwned(bundle);
    }
    doNothing().when(namespacesAdmin).deleteNamespaceBundle(Mockito.anyString(), Mockito.anyString());
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) Description(org.hamcrest.Description) Matcher(org.hamcrest.Matcher) NamespaceBundles(org.apache.pulsar.common.naming.NamespaceBundles) BundlesData(org.apache.pulsar.common.policies.data.BundlesData) RestException(org.apache.pulsar.broker.web.RestException) URL(java.net.URL) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) ClientErrorException(javax.ws.rs.ClientErrorException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) NamespaceEphemeralData(org.apache.pulsar.broker.namespace.NamespaceEphemeralData) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 18 with BundlesData

use of org.apache.pulsar.common.policies.data.BundlesData in project incubator-pulsar by apache.

the class NamespacesTest method testValidateNamespaceOwnershipWithBundles.

@Test
public void testValidateNamespaceOwnershipWithBundles() throws Exception {
    try {
        URL localWebServiceUrl = new URL(pulsar.getWebServiceAddress());
        String bundledNsLocal = "test-bundled-namespace-1";
        BundlesData bundleData = new BundlesData(Lists.newArrayList("0x00000000", "0xffffffff"));
        createBundledTestNamespaces(this.testProperty, this.testLocalCluster, bundledNsLocal, bundleData);
        final NamespaceName testNs = NamespaceName.get(this.testProperty, this.testLocalCluster, bundledNsLocal);
        mockWebUrl(localWebServiceUrl, testNs);
        OwnershipCache MockOwnershipCache = spy(pulsar.getNamespaceService().getOwnershipCache());
        doNothing().when(MockOwnershipCache).disableOwnership(any(NamespaceBundle.class));
        Field ownership = NamespaceService.class.getDeclaredField("ownershipCache");
        ownership.setAccessible(true);
        ownership.set(pulsar.getNamespaceService(), MockOwnershipCache);
        namespaces.validateNamespaceOwnershipWithBundles(this.testProperty, this.testLocalCluster, bundledNsLocal, false, true, bundleData);
    } catch (RestException e) {
        fail("ValidateNamespaceOwnershipWithBundles failed");
    }
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) Field(java.lang.reflect.Field) OwnershipCache(org.apache.pulsar.broker.namespace.OwnershipCache) BundlesData(org.apache.pulsar.common.policies.data.BundlesData) RestException(org.apache.pulsar.broker.web.RestException) URL(java.net.URL) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Aggregations

BundlesData (org.apache.pulsar.common.policies.data.BundlesData)18 Test (org.testng.annotations.Test)14 RestException (org.apache.pulsar.broker.web.RestException)12 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)10 NamespaceName (org.apache.pulsar.common.naming.NamespaceName)8 URL (java.net.URL)7 NamespaceBundle (org.apache.pulsar.common.naming.NamespaceBundle)7 Field (java.lang.reflect.Field)5 OwnershipCache (org.apache.pulsar.broker.namespace.OwnershipCache)5 NamespaceBundles (org.apache.pulsar.common.naming.NamespaceBundles)4 LocalPolicies (org.apache.pulsar.common.policies.data.LocalPolicies)3 Policies (org.apache.pulsar.common.policies.data.Policies)3 KeeperException (org.apache.zookeeper.KeeperException)3 MalformedURLException (java.net.MalformedURLException)2 TreeMap (java.util.TreeMap)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 ClusterData (org.apache.pulsar.common.policies.data.ClusterData)2 PropertyAdmin (org.apache.pulsar.common.policies.data.PropertyAdmin)2 NamespaceIsolationPolicies (org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies)2 ServerManager (org.apache.pulsar.discovery.service.server.ServerManager)2