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