1   package com.sri.emo.controller;
2   
3   import com.jcorporate.expresso.core.controller.ControllerException;
4   import com.jcorporate.expresso.core.controller.ControllerRequest;
5   import com.jcorporate.expresso.core.db.DBException;
6   import com.jcorporate.expresso.core.registry.RequestRegistry;
7   import com.jcorporate.expresso.services.test.TestSystemInitializer;
8   import com.sri.emo.EmoSchema;
9   import com.sri.emo.dbobj.Node;
10  import com.sri.emo.dbobj.NodeType;
11  import com.sri.emo.test.DatabaseTestFixture;
12  import junit.framework.TestCase;
13  import org.dom4j.Document;
14  import org.dom4j.Element;
15  
16  import java.util.Iterator;
17  import java.util.List;
18  
19  /***
20   * Tests the XML Writer.
21   *
22   * @author Michael Rimov
23   */
24  public class TestModelXMLWriter extends TestCase {
25      private ModelXMLWriter modelXMLWriter = null;
26      private DatabaseTestFixture dbFixture = null;
27  
28      protected void setUp() throws Exception {
29          super.setUp();
30          dbFixture = new DatabaseTestFixture(TestSystemInitializer.getTestContext(),
31                  EmoSchema.class.getResourceAsStream("WizardTestData.xml"));
32          dbFixture.setUp();
33          modelXMLWriter = new ModelXMLWriter();
34      }
35  
36      protected void tearDown() throws Exception {
37          modelXMLWriter = null;
38          dbFixture.tearDown();
39          dbFixture = null;
40          super.tearDown();
41      }
42  
43      public void testRenderEntityAsXml() throws DBException {
44          NodeType entity = new NodeType();
45          entity.setField(NodeType.NODE_TYPE_NAME, "MOVIEDEMO");
46          if (!entity.find()) {
47              fail("Unable to find MOVIEDEMO");
48          }
49  
50          Document actualReturn = modelXMLWriter.renderEntityAsXml(entity);
51          assertTrue(actualReturn != null);
52          Element root = verifyRootElement(actualReturn);
53  
54          List parts = root.selectNodes("./Part/*");
55          assertTrue(parts != null);
56          assertEquals(3, parts.size());
57          verifyParts(parts);
58      }
59  
60      private Element verifyRootElement(Document actualReturn) {
61          Element root = actualReturn.getRootElement();
62          assertEquals("Entity", root.getQualifiedName());
63          assertEquals("Movies", root.attributeValue("DISPLAY_TITLE"));
64          assertEquals("Sample Movie Model", root.attributeValue("NODE_TYPE_DESCRIP"));
65          assertEquals("MOVIEDEMO", root.attributeValue("NODE_TYPE_NAME"));
66          return root;
67      }
68  
69      private void verifyParts(List l) {
70          for (Iterator i = l.iterator(); i.hasNext();) {
71              Element e = (Element) i.next();
72              //To save time we only verify one.
73              if (e.attributeValue("PART_TYPE").equals("Movie_Rating")) {
74                  assertEquals("Movie_Rating", e.attributeValue("PART_TYPE"));
75                  assertEquals("Movie Rating", e.attributeValue("PART_LABEL"));
76                  assertEquals("Movie Rating", e.attributeValue("PART_DESCRIP"));
77                  assertEquals("1", e.attributeValue("PART_NUM"));
78                  assertEquals("PICKLIST_SINGLE_ALLOWED", e.attributeValue("CARDINALITY"));
79                  assertEquals("true", e.attributeValue("PART_IS_ATTRIB"));
80                  return;
81              }
82          }
83  
84          fail("Didn't find an expected part");
85      }
86  
87      public void testRenderEntityAsXml1() throws DBException, ControllerException {
88          NodeType entity = new NodeType();
89          entity.setField(NodeType.NODE_TYPE_NAME, "MOVIEDEMO");
90          if (!entity.find()) {
91              fail("Unable to find MOVIEDEMO");
92          }
93  
94          ControllerRequest request = new ControllerRequest();
95          request.setUid(RequestRegistry.getUser().getUid());
96          request.setDataContext(RequestRegistry.getDataContext());
97  
98          boolean renderNodes = true;
99          Document actualReturn = modelXMLWriter.renderEntityAsXml(entity, request, renderNodes);
100         assertTrue(actualReturn != null);
101         Element root = verifyRootElement(actualReturn);
102 
103         List parts = root.selectNodes("./Part/*");
104         assertTrue(parts != null);
105         assertEquals(3, parts.size());
106         verifyParts(parts);
107 
108         List nodes = root.selectNodes("//Data_nodes/*");
109         assertTrue(nodes != null);
110         assertTrue(nodes.size() > 1);
111         assertFalse(((Element) nodes.get(0)).getQualifiedName().equals("Data_nodes"));
112         verifyDataNode(nodes);
113     }
114 
115     private void verifyDataNode(List nodes) {
116         for (Iterator i = nodes.iterator(); i.hasNext();) {
117             Element e = (Element) i.next();
118             //To save time we only verify one.
119             if (e.attributeValue("NODE_TITLE").equals("Winnie The Pooh")) {
120                 assertEquals("1.3", e.attributeValue("NODE_TYPE_VERSION"));
121                 assertEquals("2", e.attributeValue("ident"));
122 
123                 /***
124                  * @todo Verify the subnodes as well for data.
125                  */
126                 return;
127             }
128         }
129 
130         fail("Didn't find an expected Node");
131 
132     }
133 
134     public void testRenderNodeAsXml() throws ControllerException {
135         Node querynode = null;
136         ControllerRequest request = null;
137         Document expectedReturn = null;
138         Document actualReturn = modelXMLWriter.renderNodeAsXml(querynode, request);
139         assertEquals("return value", expectedReturn, actualReturn);
140         /***@todo fill in the test code*/
141     }
142 
143     public void testRenderNodesAsXml() throws ControllerException {
144         Node[] allNodes = null;
145         ControllerRequest request = null;
146         Element[] expectedReturn = null;
147         Element[] actualReturn = modelXMLWriter.renderNodesAsXml(allNodes, request);
148         assertEquals("return value", expectedReturn, actualReturn);
149         /***@todo fill in the test code*/
150     }
151 
152 }