1 package test.crispy.impl.rest; 2 3 import java.math.BigDecimal; 4 import java.util.Hashtable; 5 import java.util.Map; 6 import java.util.Vector; 7 8 import junit.framework.TestCase; 9 import net.sf.crispy.impl.rest.Deserializer; 10 import net.sf.crispy.impl.rest.ParameterDeserializer; 11 import net.sf.crispy.impl.rest.ParameterSerializer; 12 import net.sf.crispy.impl.rest.Parser; 13 import net.sf.crispy.impl.rest.ResponseFail; 14 import net.sf.crispy.impl.rest.RestContentHandler; 15 import net.sf.crispy.impl.rest.RestServlet; 16 import net.sf.crispy.util.Converter; 17 import net.sf.crispy.util.Util; 18 import test.crispy.example.model.Adresse; 19 import test.crispy.example.model.Kunde; 20 import test.crispy.example.model.Node; 21 import test.crispy.example.service.Echo; 22 import test.crispy.example.service.EchoImpl; 23 24 public class RestServletTest extends TestCase { 25 26 private RestServlet restServlet = null; 27 28 protected void setUp() throws Exception { 29 super.setUp(); 30 restServlet = new RestServlet(); 31 32 Object lvEcho = Util.createObject("test.crispy.example.service.EchoImpl"); 33 Object lvCalc = Util.createObject("test.crispy.example.service.CalculatorImpl"); 34 restServlet.addService("test.crispy.example.service.Echo", lvEcho); 35 restServlet.addService("service.echo", lvEcho); 36 restServlet.addService("test.crispy.example.service.Calculator", lvCalc); 37 restServlet.addService("service.calculator", lvCalc); 38 39 } 40 41 42 protected void tearDown() throws Exception { 43 super.tearDown(); 44 restServlet = null; 45 } 46 47 public void testAddAndRemoveService() throws Exception { 48 RestServlet lvRestServlet = new RestServlet(); 49 assertNull(lvRestServlet.getService(Echo.class.getName())); 50 lvRestServlet.addService(Echo.class.getName(), EchoImpl.class.getName()); 51 assertNotNull(lvRestServlet.getService(Echo.class.getName())); 52 lvRestServlet.removeService(Echo.class.getName()); 53 assertNull(lvRestServlet.getService(Echo.class.getName())); 54 } 55 56 public void testCallOk() throws Exception { 57 Map lvParams = new Hashtable(); 58 lvParams.put("param1", new String[] {"9"}); 59 lvParams.put("param2", new String[] {"8"}); 60 Object lvResult = restServlet.call(lvParams, "test.crispy.example.service.Calculator", "add"); 61 assertTrue("Is Integer: " + lvResult.getClass().getName(), lvResult instanceof Integer); 62 assertTrue( ((Integer)lvResult).intValue() == 17); 63 } 64 65 public void testCallAddNodeOk() throws Exception { 66 Node n1 = new Node("Node1"); 67 Node n2 = new Node("Node2"); 68 Node n3 = new Node("Node3"); 69 Node nA = new Node("NodeA"); 70 Node nB = new Node("NodeB"); 71 72 n1.getChildren().add(n2); 73 n1.getChildren().add(n3); 74 n2.getChildren().add(nA); 75 n3.getChildren().add(nA); 76 n3.getChildren().add(nB); 77 78 Node nAdd = new Node("NodeAdd"); 79 Vector v = new Vector(); 80 v.add(n1); v.add(nAdd); 81 82 String lvString = new ParameterSerializer().serialize(v); 83 Map lvParams = ParameterDeserializer.convertString2Map(lvString); 84 Object o = restServlet.call(lvParams, "test.crispy.example.service.Echo", "addNode"); 85 assertNotNull(o); 86 assertEquals(o.getClass(), Node.class); 87 Node n = (Node) o; 88 assertEquals(n.getChildren().size(), (n1.getChildren().size() + 1)); 89 Node n22 = (Node) n.getChildren().get(0); 90 Node n33 = (Node) n.getChildren().get(1); 91 Node n1Add = (Node) n.getChildren().get(2); 92 assertEquals(n1Add.getName(), nAdd.getName()); 93 assertEquals(n22.getName(), n2.getName()); 94 assertEquals(n22.getChildren().size(), n2.getChildren().size()); 95 Node nA2 = (Node) n22.getChildren().get(0); 96 assertEquals(nA2.getName(), nA.getName()); 97 assertEquals(n33.getName(), n3.getName()); 98 assertEquals(n33.getChildren().size(), n3.getChildren().size()); 99 Node nA3 = (Node) n33.getChildren().get(0); 100 assertEquals(nA2.getName(), nA3.getName()); 101 assertEquals(nA2, nA3); 102 } 103 104 public void testCallAddNodeOkWithXML() throws Exception { 105 Node n1 = new Node("Node1"); 106 Node n2 = new Node("Node2"); 107 Node n3 = new Node("Node3"); 108 Node nA = new Node("NodeA"); 109 Node nB = new Node("NodeB"); 110 111 n1.getChildren().add(n2); 112 n1.getChildren().add(n3); 113 n2.getChildren().add(nA); 114 n3.getChildren().add(nA); 115 n3.getChildren().add(nB); 116 117 Node nAdd = new Node("NodeAdd"); 118 Vector v = new Vector(); 119 v.add(n1); v.add(nAdd); 120 121 String lvString = new ParameterSerializer().serialize(v); 122 Map lvParams = ParameterDeserializer.convertString2Map(lvString); 123 Object o = restServlet.call(lvParams, "test.crispy.example.service.Echo", "addNode"); 124 125 String lvXmlString = restServlet.makeXmlStream(o); 126 new Parser().parse(lvXmlString, new RestContentHandler ()); 127 o = Deserializer.deserialize(lvXmlString); 128 o = new Converter().makeComplex(o); 129 130 Node n11 = (Node) o; 131 assertEquals(n11.getChildren().size(), (n1.getChildren().size() + 1)); 132 Node n22 = (Node) n11.getChildren().get(0); 133 Node n33 = (Node) n11.getChildren().get(1); 134 Node n1Add = (Node) n11.getChildren().get(2); 135 assertEquals(n1Add.getName(), nAdd.getName()); 136 assertEquals(n22.getName(), n2.getName()); 137 assertEquals(n22.getChildren().size(), n2.getChildren().size()); 138 Node nA2 = (Node) n22.getChildren().get(0); 139 assertEquals(nA2.getName(), nA.getName()); 140 assertEquals(n33.getName(), n3.getName()); 141 assertEquals(n33.getChildren().size(), n3.getChildren().size()); 142 Node nA3 = (Node) n33.getChildren().get(0); 143 assertEquals(nA2.getName(), nA3.getName()); 144 assertEquals(nA2, nA3); 145 } 146 147 public void testCallRenameKundeOkWithXML() throws Exception { 148 Kunde k = new Kunde("Mario"); 149 k.setGehalt(new BigDecimal(12.34)); 150 151 Vector v = new Vector(); 152 v.add(k); v.add("Peter"); 153 154 String lvString = new ParameterSerializer().serialize(v); 155 Map lvParams = ParameterDeserializer.convertString2Map(lvString); 156 Object o = restServlet.call(lvParams, "test.crispy.example.service.Echo", "renameKunde"); 157 158 String lvXmlString = restServlet.makeXmlStream(o); 159 new Parser().parse(lvXmlString, new RestContentHandler ()); 160 o = Deserializer.deserialize(lvXmlString); 161 o = new Converter().makeComplex(o); 162 163 assertNotNull(o); 164 assertEquals(o.getClass(), Kunde.class); 165 Kunde kAfter = (Kunde) o; 166 assertEquals(kAfter.getName(), "Peter"); 167 assertEquals(kAfter.getGehalt(), new BigDecimal(12.34)); 168 } 169 170 public void testCallRenameKundeOkWithRelationWithXML() throws Exception { 171 Kunde k = new Kunde("Mario"); 172 k.setGehalt(new BigDecimal(12.34)); 173 Adresse a = new Adresse(); 174 a.setOrt("Magdeburg"); 175 a.setPlz("39104"); 176 177 Adresse a2 = new Adresse(); 178 a2.setOrt("Cottbus"); 179 a2.setPlz("03044"); 180 181 k.getAdressen().add(a); 182 k.getAdressen().add(a2); 183 184 Vector v = new Vector(); 185 v.add(k); v.add("Peter"); 186 187 String lvString = new ParameterSerializer().serialize(v); 188 Map lvParams = ParameterDeserializer.convertString2Map(lvString); 189 Object o = restServlet.call(lvParams, "test.crispy.example.service.Echo", "renameKunde"); 190 191 String lvXmlString = restServlet.makeXmlStream(o); 192 new Parser().parse(lvXmlString, new RestContentHandler ()); 193 o = Deserializer.deserialize(lvXmlString); 194 o = new Converter().makeComplex(o); 195 196 assertNotNull(o); 197 assertEquals(o.getClass(), Kunde.class); 198 Kunde kAfter = (Kunde) o; 199 assertEquals(kAfter.getName(), "Peter"); 200 assertEquals(kAfter.getGehalt(), new BigDecimal(12.34)); 201 assertEquals(kAfter.getAdressen().size(), 2); 202 Adresse a11 = (Adresse) kAfter.getAdressen().get(0); 203 assertEquals(a11.getOrt(), "Magdeburg"); 204 assertEquals(a11.getPlz(), "39104"); 205 206 Adresse a22 = (Adresse) kAfter.getAdressen().get(1); 207 assertEquals(a22.getOrt(), "Cottbus"); 208 assertEquals(a22.getPlz(), "03044"); 209 } 210 211 public void testCallRenameKundeOkWithRelationWithXMLWithCycle() throws Exception { 212 Kunde k = new Kunde("Mario"); 213 k.setGehalt(new BigDecimal(12.34)); 214 Adresse a = new Adresse(); 215 a.setOrt("Magdeburg"); 216 a.setPlz("39104"); 217 k.getAdressen().add(a); 218 a.setKunde(k); 219 220 Vector v = new Vector(); 221 v.add(k); v.add("Peter"); 222 223 String lvString = new ParameterSerializer().serialize(v); 224 Map lvParams = ParameterDeserializer.convertString2Map(lvString); 225 Object o = restServlet.call(lvParams, "test.crispy.example.service.Echo", "renameKunde"); 226 227 String lvXmlString = restServlet.makeXmlStream(o); 228 new Parser().parse(lvXmlString, new RestContentHandler ()); 229 o = Deserializer.deserialize(lvXmlString); 230 o = new Converter().makeComplex(o); 231 232 assertNotNull(o); 233 assertEquals(o.getClass(), Kunde.class); 234 Kunde kAfter = (Kunde) o; 235 assertEquals(kAfter.getName(), "Peter"); 236 assertEquals(kAfter.getGehalt(), new BigDecimal(12.34)); 237 assertEquals(kAfter.getAdressen().size(), 1); 238 Adresse a11 = (Adresse) kAfter.getAdressen().get(0); 239 assertEquals(a11.getOrt(), "Magdeburg"); 240 assertEquals(a11.getPlz(), "39104"); 241 assertEquals(kAfter, a11.getKunde()); 242 } 243 244 public void testCallOkWithOutClass() throws Exception { 245 Map lvParams = new Hashtable(); 246 lvParams.put("param1", new String[] {"5"}); 247 lvParams.put("param2", new String[] {"8"}); 248 Object lvResult = restServlet.call(lvParams, null, "service.calculator.add"); 249 assertTrue("Is Integer: " + lvResult.getClass().getName(), lvResult instanceof Integer); 250 assertTrue( ((Integer)lvResult).intValue() == 13); 251 } 252 253 public void testCallOkWithMake2XmlString() throws Exception { 254 Map lvParams = new Hashtable(); 255 lvParams.put("param1", new String[] {"5"}); 256 lvParams.put("param2", new String[] {"8"}); 257 Object lvResult = restServlet.call(lvParams, null, "service.calculator.add"); 258 String lvXmlString = restServlet.makeXmlStream(lvResult); 259 new Parser().parse(lvXmlString, new RestContentHandler ()); 260 Integer lvValue = (Integer) Deserializer.deserialize(lvXmlString); 261 assertTrue(lvValue.intValue() == 13); 262 } 263 264 public void testCallFailParameter() throws Exception { 265 Map lvParams = new Hashtable(); 266 lvParams.put("param1", new String[] {"5"}); 267 Object lvResult = null; 268 lvResult = restServlet.call(lvParams, "test.crispy.example.service.Calculator", "add"); 269 assertNotNull(lvResult); 270 assertTrue(lvResult instanceof ResponseFail); 271 } 272 273 public void testCallFailClass() throws Exception { 274 Map lvParams = new Hashtable(); 275 lvParams.put("param1", new String[] {"5"}); 276 lvParams.put("param2", new String[] {"8"}); 277 Object lvResult = restServlet.call(lvParams, "test.crispy.example.service.Calculator_InValid", "add"); 278 assertTrue(lvResult instanceof ResponseFail); 279 lvResult = restServlet.call(lvParams, null, "add"); 280 assertTrue(lvResult instanceof ResponseFail); 281 } 282 283 public void testCallFailMethod() throws Exception { 284 Map lvParams = new Hashtable(); 285 lvParams.put("param1", new String[] {"5"}); 286 lvParams.put("param2", new String[] {"8"}); 287 Object lvResult = restServlet.call(lvParams, "test.crispy.example.service.Calculator", "add_invalid"); 288 assertTrue(lvResult instanceof ResponseFail); 289 lvResult = restServlet.call(lvParams, "test.crispy.example.service.Calculator", null); 290 assertTrue(lvResult instanceof ResponseFail); 291 } 292 293 public void testCallFailWithMake2XmlString() throws Exception { 294 Map lvParams = new Hashtable(); 295 lvParams.put("param1", new String[] {"5"}); 296 lvParams.put("param2", new String[] {"8"}); 297 Object lvResult = restServlet.call(lvParams, null, "service.calculator_FAIL.add"); 298 String lvXmlString = restServlet.makeXmlStream(lvResult); 299 Object o = Deserializer.deserialize(lvXmlString); 300 ResponseFail lvResponseFail = (ResponseFail) new Converter().makeComplex(o); 301 assertNotNull(lvResponseFail); 302 } 303 304 }