1   package com.sri.emo.dbobj.selectiontree;
2   
3   import java.util.ArrayList;
4   import java.util.HashMap;
5   import java.util.List;
6   import java.util.Map;
7   import com.jcorporate.expresso.core.security.SuperUser;
8   import com.jcorporate.expresso.services.test.TestSystemInitializer;
9   import com.sri.emo.dbobj.Node;
10  import com.sri.emo.test.DatabaseTestFixture;
11  import com.sri.emo.test.EmoTestSuite;
12  import junit.framework.TestCase;
13  import java.util.Collection;
14  import java.util.Iterator;
15  import com.jcorporate.expresso.core.db.DBException;
16  
17  /***
18   *
19   * @author Michael Rimov
20   * @version 1.0
21   */
22  public class TreeDeletionSelectorTestCase extends TestCase {
23      private TreeDeletionSelector treeDeletionSelector = null;
24  
25      private DatabaseTestFixture databaseTestFixture = null;
26  
27      public static final String STAR_WARS_ID = "14";
28  
29  
30      private static final String HARRISON_FORD_ID = "20";
31  
32      private static final String CARRIE_FISHER_ID = "23";
33  
34      private static final String ROBERT_DENIRO_ID = "33";
35  
36      private static final String ACME_FILM_ID = "32";
37  
38  
39      /***
40       * Constructs the database test fixture instance.
41       *
42       * @throws Exception
43       */
44      protected void setUp() throws Exception {
45          super.setUp();
46      }
47  
48      protected void tearDown() throws Exception {
49          if (databaseTestFixture != null) {
50              databaseTestFixture.tearDown();
51              databaseTestFixture = null;
52          }
53          super.tearDown();
54      }
55  
56  
57      /***
58       * Tests the background -- do we have everything we expect in the tree?
59       * @throws Exception
60       */
61      public void testTreeDeletionSelector() throws Exception  {
62          //Drop once.
63  
64          databaseTestFixture = new DatabaseTestFixture(TestSystemInitializer
65              .getTestContext(), EmoTestSuite.class
66              .getResourceAsStream("relation-test-one.xml"));
67          databaseTestFixture.setUp();
68  
69          //Test to make sure DeNiro Exists
70          Node deniro = new Node(SuperUser.INSTANCE);
71          deniro.setNodeId(ROBERT_DENIRO_ID);
72          deniro.retrieve();
73  
74  
75          Node n = new Node(SuperUser.INSTANCE);
76          n.setNodeId(STAR_WARS_ID);
77          n.retrieve();
78  
79          Map allNodes = getNodesToRemove(n);
80          assertTrue(allNodes.containsKey(STAR_WARS_ID));
81          assertTrue(allNodes.containsKey(ACME_FILM_ID));
82          assertTrue(allNodes.containsKey(CARRIE_FISHER_ID));
83          assertTrue(allNodes.containsKey(HARRISON_FORD_ID));
84          assertFalse(allNodes.containsKey(ROBERT_DENIRO_ID));
85      }
86  
87      /***
88       * Tests the deletion of the scenario:
89       * <img src="doc-files/simple_tree.gif"/>
90       * @throws Exception
91       */
92      public void testSimpleTree() throws Exception {
93          databaseTestFixture = new DatabaseTestFixture(TestSystemInitializer
94              .getTestContext(), EmoTestSuite.class
95              .getResourceAsStream("relation-test-one.xml"));
96          databaseTestFixture.setUp();
97  
98          Node n = new Node(SuperUser.INSTANCE);
99          n.setNodeId(STAR_WARS_ID);
100         n.retrieve();
101 
102         Map nodesToRemove = getNodesToRemove(n);
103         treeDeletionSelector = new TreeDeletionSelector(nodesToRemove);
104         List toSave = new ArrayList();
105         List toDelete = new ArrayList();
106         treeDeletionSelector.determineDeletionTree(n, toSave, toDelete);
107 
108 
109         assertEquals(0, toSave.size());
110         assertEquals(toDelete.size(), nodesToRemove.size() - 1); //Main node is NOT included.
111     }
112 
113     private Map getNodesToRemove(Node existing) throws Exception {
114         HashMap allNodeHash = new HashMap();
115         existing.setAttribute(Node.INDENT,
116             new Integer(1)); // root node starts at level 1; used for formatting output
117         existing.getNodesInStronglyRelatedTree(allNodeHash);
118         return allNodeHash;
119     }
120 
121 
122     /***
123      * Tests the deletion of the scenario:
124      * <img src="doc-files/partialtree.gif"/>
125      * @throws Exception
126      */
127     public void testPartialTreeDeletions() throws Exception {
128         databaseTestFixture = new DatabaseTestFixture(TestSystemInitializer
129             .getTestContext(), EmoTestSuite.class
130             .getResourceAsStream("relation-test-two.xml"));
131         databaseTestFixture.setUp();
132 
133 
134         Node n = new Node(SuperUser.INSTANCE);
135         n.setNodeId(STAR_WARS_ID);
136         n.retrieve();
137 
138         Map nodesToRemove = getNodesToRemove(n);
139         treeDeletionSelector = new TreeDeletionSelector(nodesToRemove);
140         List toSave = new ArrayList();
141         List toDelete = new ArrayList();
142         treeDeletionSelector.determineDeletionTree(n, toSave, toDelete);
143 
144         Node ford = new Node(SuperUser.INSTANCE);
145         ford.setNodeId(HARRISON_FORD_ID);
146         ford.retrieve();
147 
148         Node fisher = new Node(SuperUser.INSTANCE);
149         fisher.setNodeId(CARRIE_FISHER_ID);
150         fisher.retrieve();
151 
152         Node acme = new Node(SuperUser.INSTANCE);
153         acme.setNodeId(ACME_FILM_ID);
154         acme.retrieve();
155 
156         assertFalse(collectionContains(fisher, toSave));
157         assertFalse(collectionContains(ford, toSave));
158         assertTrue(collectionContains(fisher, toDelete));
159         assertTrue(collectionContains(acme, toSave));
160     }
161 
162     private boolean collectionContains(Node toMatch, Collection listOfNodes) throws DBException {
163         String nodeId = toMatch.getNodeId();
164 
165         for (Iterator i = listOfNodes.iterator(); i.hasNext();) {
166             Node eachNode = (Node)i.next();
167             if (nodeId.equals(eachNode.getNodeId())) {
168                 return true;
169             }
170         }
171 
172         return false;
173     }
174 
175 
176     /***
177         * Tests the deletion of the scenario:
178         * <img src="doc-files/totaltree.gif"/>
179         * @throws Exception
180      */
181     public void testScenarioThree() throws Exception {
182         databaseTestFixture = new DatabaseTestFixture(TestSystemInitializer
183             .getTestContext(), EmoTestSuite.class
184             .getResourceAsStream("relation-test-three.xml"));
185         databaseTestFixture.setUp();
186 
187 
188         Node n = new Node(SuperUser.INSTANCE);
189         n.setNodeId(STAR_WARS_ID);
190         n.retrieve();
191 
192         Map nodesToRemove = getNodesToRemove(n);
193         treeDeletionSelector = new TreeDeletionSelector(nodesToRemove);
194         List toSave = new ArrayList();
195         List toDelete = new ArrayList();
196         treeDeletionSelector.determineDeletionTree(n, toSave, toDelete);
197 
198         Node ford = new Node(SuperUser.INSTANCE);
199         ford.setNodeId(HARRISON_FORD_ID);
200         ford.retrieve();
201 
202         Node fisher = new Node(SuperUser.INSTANCE);
203         fisher.setNodeId(CARRIE_FISHER_ID);
204         fisher.retrieve();
205 
206         Node acme = new Node(SuperUser.INSTANCE);
207         acme.setNodeId(ACME_FILM_ID);
208         acme.retrieve();
209 
210 
211         Node deNiro = new Node(SuperUser.INSTANCE);
212         deNiro.setNodeId(ROBERT_DENIRO_ID);
213         deNiro.retrieve();
214 
215         assertFalse(collectionContains(fisher, toSave));
216         assertFalse(collectionContains(ford, toSave));
217         assertFalse(collectionContains(acme, toSave));
218         assertFalse(collectionContains(deNiro, toSave));
219 
220         assertTrue(collectionContains(fisher, toDelete));
221         assertTrue(collectionContains(ford, toDelete));
222         assertTrue(collectionContains(acme, toDelete));
223         assertTrue(collectionContains(deNiro, toDelete));
224     }
225 
226     /***
227      * Same test as scenario three, but we delete DeNiro instead of
228      * Star Wars
229      * @throws Exception
230      */
231     public void testScenarioFour() throws Exception {
232         databaseTestFixture = new DatabaseTestFixture(TestSystemInitializer
233             .getTestContext(), EmoTestSuite.class
234             .getResourceAsStream("relation-test-three.xml"));
235         databaseTestFixture.setUp();
236 
237 
238         Node deNiro = new Node(SuperUser.INSTANCE);
239         deNiro.setNodeId(ROBERT_DENIRO_ID);
240         deNiro.retrieve();
241 
242         Map nodesToRemove = getNodesToRemove(deNiro);
243         treeDeletionSelector = new TreeDeletionSelector(nodesToRemove);
244 
245         List toSave = new ArrayList();
246         List toDelete = new ArrayList();
247         treeDeletionSelector.determineDeletionTree(deNiro, toSave, toDelete);
248 
249         Node acme = new Node(SuperUser.INSTANCE);
250         acme.setNodeId(ACME_FILM_ID);
251         acme.retrieve();
252 
253         assertFalse(collectionContains(acme, toDelete));
254     }
255 
256 }