* @author John Mazzitelli
*/
@Test
public class PluginDependencyGraphTest {
public void testOptionalPluginsDeployment() {
PluginDependencyGraph graph;
List<String> order;
// let's assume we have a dependency graph like this:
// plugin A depends on plugin B (required)
// plugin B depends on plugin D and C (required)
// plugin C depends on plugin E and F (optional)
// plugin D depends on plugin E and F (optional)
// plugin E does not depend on any other plugin
// plugin F does not depend on any other plugin
// plugin G depends on plugin F (required)
// plugin Z does not depend on any other plugin
// the deployment order should be: Z F G E D C B A
graph = new PluginDependencyGraph();
addPlugin(graph, "A", "B");
addPlugin(graph, "B", "D", "C");
addPluginWithOptionalDeps(graph, "C", "E", "F");
addPluginWithOptionalDeps(graph, "D", "E", "F");
addPlugin(graph, "E");
addPlugin(graph, "F");
addPlugin(graph, "G", "F");
addPlugin(graph, "Z");
assert graph.isComplete(null);
order = graph.getDeploymentOrder();
assert order.get(0).equals("Z") : order;
assert order.get(1).equals("F") : order;
assert order.get(2).equals("G") : order;
assert order.get(3).equals("E") : order;
assert order.get(4).equals("D") : order;
assert order.get(5).equals("C") : order;
assert order.get(6).equals("B") : order;
assert order.get(7).equals("A") : order;
List<String> dependents = graph.getOptionalDependents("E");
assert dependents.contains("C") : dependents;
assert dependents.contains("D") : dependents;
assert dependents.size() == 2 : dependents;
dependents = graph.getOptionalDependents("F");
assert dependents.contains("C") : dependents;
assert dependents.contains("D") : dependents;
assert dependents.size() == 2 : dependents;
Collection<String> dependents2 = graph.getAllDependents("F");
assert dependents2.contains("A") : dependents2;
assert dependents2.contains("B") : dependents2;
assert dependents2.contains("C") : dependents2;
assert dependents2.contains("D") : dependents2;
assert dependents2.contains("G") : dependents2;
assert dependents2.size() == 5 : dependents2;
// plugin A depends on plugin B (required)
// plugin B depends on plugin D and C (required)
// plugin C depends on plugin E and F (optional)
Collection<String> dependencies3 = graph.getAllDependencies("F");
assert dependencies3.isEmpty() : dependencies3;
dependencies3 = graph.getAllDependencies("A");
assert dependencies3.contains("B") : dependencies3;
assert dependencies3.contains("D") : dependencies3;
assert dependencies3.contains("C") : dependencies3;
assert dependencies3.contains("E") : dependencies3;
assert dependencies3.contains("F") : dependencies3;
assert dependencies3.size() == 5 : dependencies3;
dependencies3 = graph.getAllDependencies("B");
assert dependencies3.contains("D") : dependencies3;
assert dependencies3.contains("C") : dependencies3;
assert dependencies3.contains("E") : dependencies3;
assert dependencies3.contains("F") : dependencies3;
assert dependencies3.size() == 4 : dependencies3;
dependencies3 = graph.getAllDependencies("C");
assert dependencies3.contains("E") : dependencies3;
assert dependencies3.contains("F") : dependencies3;
assert dependencies3.size() == 2 : dependencies3;
// Use the same dependency graph, but do not deploy plugin F.
// With F missing, G will fail because it required F
graph = new PluginDependencyGraph();
addPlugin(graph, "A", "B");
addPlugin(graph, "B", "D", "C");
addPluginWithOptionalDeps(graph, "C", "E", "F");
addPluginWithOptionalDeps(graph, "D", "E", "F");
addPlugin(graph, "E");
//addPlugin(graph, "F"); PLUGIN F IS GOING TO BE MISSING FROM THIS GRAPH!
addPlugin(graph, "G", "F");
addPlugin(graph, "Z");
assert !graph.isComplete(null) : "Plugin F was missing, so G should have failed";
// Use the same dependency graph, but do not deploy plugin F AND make G optionally depend on F.
// With F missing, but all dependencies on it being optional, this graph should be complete.
// The deployment order in this case should be: Z G E D C B A
graph = new PluginDependencyGraph();
addPlugin(graph, "A", "B");
addPlugin(graph, "B", "D", "C");
addPluginWithOptionalDeps(graph, "C", "E", "F");
addPluginWithOptionalDeps(graph, "D", "E", "F");
addPlugin(graph, "E");
//addPlugin(graph, "F"); PLUGIN F IS GOING TO BE MISSING FROM THIS GRAPH!
addPluginWithOptionalDeps(graph, "G", "F"); // G is optionally dependent on F
addPlugin(graph, "Z");
assert graph.isComplete(null);
order = graph.getDeploymentOrder();
assert order.get(0).equals("Z") : order;
assert order.get(1).equals("G") : order;
assert order.get(2).equals("E") : order;
assert order.get(3).equals("D") : order;
assert order.get(4).equals("C") : order;
assert order.get(5).equals("B") : order;
assert order.get(6).equals("A") : order;
dependents = graph.getOptionalDependents("E");
assert dependents.contains("C") : dependents;
assert dependents.contains("D") : dependents;
assert dependents.size() == 2 : dependents;
dependents = graph.getOptionalDependents("F");
assert dependents.contains("C") : dependents;
assert dependents.contains("D") : dependents;
assert dependents.contains("G") : dependents;
assert dependents.size() == 3 : dependents;
}