Search in sources :

Example 1 with DiscoveryService

use of org.apache.sling.discovery.DiscoveryService in project sling by apache.

the class TestViewStateManager method testChangedPropertiesChanged.

@Test
public void testChangedPropertiesChanged() throws Exception {
    final DummyListener listener = new DummyListener();
    mgr.installMinEventDelayHandler(new DiscoveryService() {

        @Override
        public TopologyView getTopology() {
            throw new IllegalStateException("not yet impl");
        }
    }, new DummyScheduler(), 1);
    mgr.handleActivated();
    TestHelper.assertNoEvents(listener);
    mgr.bind(listener);
    TestHelper.assertNoEvents(listener);
    mgr.handleChanging();
    TestHelper.assertNoEvents(listener);
    final BaseTopologyView view1 = new DummyTopologyView().addInstance();
    InstanceDescription instance1 = view1.getInstances().iterator().next();
    ClusterView cluster1 = instance1.getClusterView();
    mgr.handleNewView(view1);
    assertEvents(listener, EventHelper.newInitEvent(view1));
    DefaultClusterView cluster2 = new DefaultClusterView(new String(cluster1.getId()));
    final BaseTopologyView view2 = new DummyTopologyView(view1.getLocalClusterSyncTokenId()).addInstance(instance1.getSlingId(), cluster2, instance1.isLeader(), instance1.isLocal());
    DefaultInstanceDescription instance2 = (DefaultInstanceDescription) view2.getLocalInstance();
    instance2.setProperty("foo", "bar");
    mgr.handleNewView(view2);
    assertEvents(listener, EventHelper.newPropertiesChangedEvent(view1, view2));
}
Also used : ClusterView(org.apache.sling.discovery.ClusterView) DefaultClusterView(org.apache.sling.discovery.commons.providers.DefaultClusterView) DummyTopologyView(org.apache.sling.discovery.commons.providers.DummyTopologyView) DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription) DefaultClusterView(org.apache.sling.discovery.commons.providers.DefaultClusterView) DefaultInstanceDescription(org.apache.sling.discovery.commons.providers.DefaultInstanceDescription) InstanceDescription(org.apache.sling.discovery.InstanceDescription) DiscoveryService(org.apache.sling.discovery.DiscoveryService) BaseTopologyView(org.apache.sling.discovery.commons.providers.BaseTopologyView) TopologyView(org.apache.sling.discovery.TopologyView) DummyTopologyView(org.apache.sling.discovery.commons.providers.DummyTopologyView) BaseTopologyView(org.apache.sling.discovery.commons.providers.BaseTopologyView) Test(org.junit.Test)

Example 2 with DiscoveryService

use of org.apache.sling.discovery.DiscoveryService in project sling by apache.

the class NoClusterDiscoveryServiceTest method testListenerAfter.

@Test
public void testListenerAfter() throws Exception {
    final DiscoveryService service = this.createService(true);
    final List<TopologyEvent> events = new ArrayList<TopologyEvent>();
    final TopologyEventListener listener = new TopologyEventListener() {

        @Override
        public void handleTopologyEvent(final TopologyEvent event) {
            events.add(event);
        }
    };
    invoke(service, "bindTopologyEventListener", new Class[] { TopologyEventListener.class }, new Object[] { listener });
    assertEquals(1, events.size());
    assertEquals(TopologyEvent.Type.TOPOLOGY_INIT, events.get(0).getType());
    assertNotNull(events.get(0).getNewView());
    assertNull(events.get(0).getOldView());
}
Also used : TopologyEvent(org.apache.sling.discovery.TopologyEvent) ArrayList(java.util.ArrayList) DiscoveryService(org.apache.sling.discovery.DiscoveryService) TopologyEventListener(org.apache.sling.discovery.TopologyEventListener) Test(org.junit.Test)

Example 3 with DiscoveryService

use of org.apache.sling.discovery.DiscoveryService in project sling by apache.

the class NoClusterDiscoveryServiceTest method createService.

private DiscoveryService createService(final boolean activate) {
    final DiscoveryService service = new NoClusterDiscoveryService();
    setField(service, "settingsService", new SlingSettingsService() {

        @Override
        public String getSlingId() {
            return "my-sling-id";
        }

        @Override
        public String getSlingHomePath() {
            return null;
        }

        @Override
        public URL getSlingHome() {
            return null;
        }

        @Override
        public Set<String> getRunModes() {
            return null;
        }

        @Override
        public String getAbsolutePathWithinSlingHome(String relativePath) {
            return null;
        }
    });
    if (activate) {
        invoke(service, "activate");
    }
    return service;
}
Also used : Set(java.util.Set) DiscoveryService(org.apache.sling.discovery.DiscoveryService) SlingSettingsService(org.apache.sling.settings.SlingSettingsService) URL(java.net.URL)

Example 4 with DiscoveryService

use of org.apache.sling.discovery.DiscoveryService in project sling by apache.

the class NoClusterDiscoveryServiceTest method testListenerBefore.

@Test
public void testListenerBefore() throws Exception {
    final DiscoveryService service = this.createService(false);
    final List<TopologyEvent> events = new ArrayList<TopologyEvent>();
    final TopologyEventListener listener = new TopologyEventListener() {

        @Override
        public void handleTopologyEvent(final TopologyEvent event) {
            events.add(event);
        }
    };
    invoke(service, "bindTopologyEventListener", new Class[] { TopologyEventListener.class }, new Object[] { listener });
    assertEquals(0, events.size());
    invoke(service, "activate");
    assertEquals(1, events.size());
    assertEquals(TopologyEvent.Type.TOPOLOGY_INIT, events.get(0).getType());
    assertNotNull(events.get(0).getNewView());
    assertNull(events.get(0).getOldView());
}
Also used : TopologyEvent(org.apache.sling.discovery.TopologyEvent) ArrayList(java.util.ArrayList) DiscoveryService(org.apache.sling.discovery.DiscoveryService) TopologyEventListener(org.apache.sling.discovery.TopologyEventListener) Test(org.junit.Test)

Example 5 with DiscoveryService

use of org.apache.sling.discovery.DiscoveryService in project sling by apache.

the class NoClusterDiscoveryServiceTest method testPropertyChanges.

@Test
public void testPropertyChanges() throws Exception {
    final DiscoveryService service = this.createService(true);
    final List<TopologyEvent> events = new ArrayList<TopologyEvent>();
    final TopologyEventListener listener = new TopologyEventListener() {

        @Override
        public void handleTopologyEvent(final TopologyEvent event) {
            events.add(event);
        }
    };
    invoke(service, "bindTopologyEventListener", new Class[] { TopologyEventListener.class }, new Object[] { listener });
    events.clear();
    final PropertyProvider provider = new PropertyProvider() {

        @Override
        public String getProperty(final String name) {
            if ("a".equals(name)) {
                return "1";
            }
            if ("b".equals(name)) {
                return "2";
            }
            if ("c".equals(name)) {
                return "3";
            }
            return null;
        }
    };
    final Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(PropertyProvider.PROPERTY_PROPERTIES, new String[] { "a", "b", "c" });
    properties.put(Constants.SERVICE_ID, 1L);
    invoke(service, "bindPropertyProvider", new Class[] { PropertyProvider.class, Map.class }, new Object[] { provider, properties });
    assertEquals(1, events.size());
    assertEquals(TopologyEvent.Type.PROPERTIES_CHANGED, events.get(0).getType());
    assertNotNull(events.get(0).getNewView());
    assertTrue(events.get(0).getNewView().isCurrent());
    assertNotNull(events.get(0).getOldView());
    assertFalse(events.get(0).getOldView().isCurrent());
    // test properties
    assertEquals("1", events.get(0).getNewView().getLocalInstance().getProperty("a"));
    assertEquals("2", events.get(0).getNewView().getLocalInstance().getProperty("b"));
    assertEquals("3", events.get(0).getNewView().getLocalInstance().getProperty("c"));
    assertNull(events.get(0).getOldView().getLocalInstance().getProperty("a"));
    assertNull(events.get(0).getOldView().getLocalInstance().getProperty("b"));
    assertNull(events.get(0).getOldView().getLocalInstance().getProperty("c"));
    events.clear();
    invoke(service, "unbindPropertyProvider", new Class[] { PropertyProvider.class, Map.class }, new Object[] { provider, properties });
    assertEquals(1, events.size());
    assertEquals(TopologyEvent.Type.PROPERTIES_CHANGED, events.get(0).getType());
    assertNotNull(events.get(0).getNewView());
    assertTrue(events.get(0).getNewView().isCurrent());
    assertNotNull(events.get(0).getOldView());
    assertFalse(events.get(0).getOldView().isCurrent());
    assertEquals("1", events.get(0).getOldView().getLocalInstance().getProperty("a"));
    assertEquals("2", events.get(0).getOldView().getLocalInstance().getProperty("b"));
    assertEquals("3", events.get(0).getOldView().getLocalInstance().getProperty("c"));
    assertNull(events.get(0).getNewView().getLocalInstance().getProperty("a"));
    assertNull(events.get(0).getNewView().getLocalInstance().getProperty("b"));
    assertNull(events.get(0).getNewView().getLocalInstance().getProperty("c"));
}
Also used : PropertyProvider(org.apache.sling.discovery.PropertyProvider) HashMap(java.util.HashMap) TopologyEvent(org.apache.sling.discovery.TopologyEvent) ArrayList(java.util.ArrayList) DiscoveryService(org.apache.sling.discovery.DiscoveryService) TopologyEventListener(org.apache.sling.discovery.TopologyEventListener) Test(org.junit.Test)

Aggregations

DiscoveryService (org.apache.sling.discovery.DiscoveryService)6 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)3 TopologyEvent (org.apache.sling.discovery.TopologyEvent)3 TopologyEventListener (org.apache.sling.discovery.TopologyEventListener)3 URL (java.net.URL)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1 ClusterView (org.apache.sling.discovery.ClusterView)1 InstanceDescription (org.apache.sling.discovery.InstanceDescription)1 PropertyProvider (org.apache.sling.discovery.PropertyProvider)1 TopologyView (org.apache.sling.discovery.TopologyView)1 BaseTopologyView (org.apache.sling.discovery.commons.providers.BaseTopologyView)1 DefaultClusterView (org.apache.sling.discovery.commons.providers.DefaultClusterView)1 DefaultInstanceDescription (org.apache.sling.discovery.commons.providers.DefaultInstanceDescription)1 DummyTopologyView (org.apache.sling.discovery.commons.providers.DummyTopologyView)1 SlingSettingsService (org.apache.sling.settings.SlingSettingsService)1