Search in sources :

Example 1 with TopologyEvent

use of org.apache.knox.gateway.topology.TopologyEvent in project knox by apache.

the class DefaultTopologyServiceTest method testGetTopologies.

@Test
public void testGetTopologies() throws Exception {
    File dir = createDir();
    File topologyDir = new File(dir, "topologies");
    File descriptorsDir = new File(dir, "descriptors");
    descriptorsDir.mkdirs();
    File sharedProvidersDir = new File(dir, "shared-providers");
    sharedProvidersDir.mkdirs();
    long time = topologyDir.lastModified();
    try {
        createFile(topologyDir, "one.xml", "org/apache/knox/gateway/topology/file/topology-one.xml", time);
        TestTopologyListener topoListener = new TestTopologyListener();
        FileAlterationMonitor monitor = new FileAlterationMonitor(Long.MAX_VALUE);
        TopologyService provider = new DefaultTopologyService();
        Map<String, String> c = new HashMap<>();
        GatewayConfig config = EasyMock.createNiceMock(GatewayConfig.class);
        EasyMock.expect(config.getGatewayTopologyDir()).andReturn(topologyDir.getAbsolutePath()).anyTimes();
        EasyMock.expect(config.getGatewayConfDir()).andReturn(descriptorsDir.getParentFile().getAbsolutePath()).anyTimes();
        EasyMock.expect(config.getGatewayProvidersConfigDir()).andReturn(sharedProvidersDir.getAbsolutePath()).anyTimes();
        EasyMock.expect(config.getGatewayDescriptorsDir()).andReturn(descriptorsDir.getAbsolutePath()).anyTimes();
        EasyMock.replay(config);
        provider.init(config, c);
        provider.addTopologyChangeListener(topoListener);
        provider.reloadTopologies();
        Collection<Topology> topologies = provider.getTopologies();
        assertThat(topologies, notNullValue());
        assertThat(topologies.size(), is(1));
        Topology topology = topologies.iterator().next();
        assertThat(topology.getName(), is("one"));
        assertThat(topology.getTimestamp(), is(time));
        assertThat(topoListener.events.size(), is(1));
        topoListener.events.clear();
        // Add a file to the directory.
        File two = createFile(topologyDir, "two.xml", "org/apache/knox/gateway/topology/file/topology-two.xml", 1L);
        provider.reloadTopologies();
        topologies = provider.getTopologies();
        assertThat(topologies.size(), is(2));
        Set<String> names = new HashSet<>(Arrays.asList("one", "two"));
        Iterator<Topology> iterator = topologies.iterator();
        topology = iterator.next();
        assertThat(names, hasItem(topology.getName()));
        names.remove(topology.getName());
        topology = iterator.next();
        assertThat(names, hasItem(topology.getName()));
        names.remove(topology.getName());
        assertThat(names.size(), is(0));
        assertThat(topoListener.events.size(), is(1));
        List<TopologyEvent> events = topoListener.events.get(0);
        assertThat(events.size(), is(1));
        TopologyEvent event = events.get(0);
        assertThat(event.getType(), is(TopologyEvent.Type.CREATED));
        assertThat(event.getTopology(), notNullValue());
        // Update a file in the directory.
        two = createFile(topologyDir, "two.xml", "org/apache/knox/gateway/topology/file/topology-three.xml", 2L);
        provider.reloadTopologies();
        topologies = provider.getTopologies();
        assertThat(topologies.size(), is(2));
        names = new HashSet<>(Arrays.asList("one", "two"));
        iterator = topologies.iterator();
        topology = iterator.next();
        assertThat(names, hasItem(topology.getName()));
        names.remove(topology.getName());
        topology = iterator.next();
        assertThat(names, hasItem(topology.getName()));
        names.remove(topology.getName());
        assertThat(names.size(), is(0));
        // Remove a file from the directory.
        two.delete();
        provider.reloadTopologies();
        topologies = provider.getTopologies();
        assertThat(topologies.size(), is(1));
        topology = topologies.iterator().next();
        assertThat(topology.getName(), is("one"));
        assertThat(topology.getTimestamp(), is(time));
    } finally {
        FileUtils.deleteQuietly(dir);
    }
}
Also used : FileAlterationMonitor(org.apache.commons.io.monitor.FileAlterationMonitor) DefaultTopologyService(org.apache.knox.gateway.services.topology.impl.DefaultTopologyService) HashMap(java.util.HashMap) TopologyEvent(org.apache.knox.gateway.topology.TopologyEvent) Topology(org.apache.knox.gateway.topology.Topology) DefaultTopologyService(org.apache.knox.gateway.services.topology.impl.DefaultTopologyService) File(java.io.File) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

File (java.io.File)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 FileAlterationMonitor (org.apache.commons.io.monitor.FileAlterationMonitor)1 GatewayConfig (org.apache.knox.gateway.config.GatewayConfig)1 DefaultTopologyService (org.apache.knox.gateway.services.topology.impl.DefaultTopologyService)1 Topology (org.apache.knox.gateway.topology.Topology)1 TopologyEvent (org.apache.knox.gateway.topology.TopologyEvent)1 Test (org.junit.Test)1