1 package com.jaspersoft.jasperserver.ws.axis2; 2 3 import java.io.File ; 4 import java.util.ArrayList ; 5 import java.util.HashMap ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 import java.util.Map ; 9 import java.util.SortedMap ; 10 import java.util.TreeMap ; 11 12 import net.sf.jasperreports.engine.JasperPrint; 13 14 import org.apache.axiom.soap.SOAPEnvelope; 15 import org.apache.axis2.AxisFault; 16 import org.apache.axis2.client.ServiceClient; 17 import org.exolab.castor.xml.MarshalException; 18 import org.exolab.castor.xml.ValidationException; 19 20 21 import com.jaspersoft.jasperserver.irplugin.*; 22 import com.jaspersoft.jasperserver.irplugin.wsclient.*; 23 import com.jaspersoft.jasperserver.war.JasperServerConstants; 24 25 import com.jaspersoft.jasperserver.api.metadata.xml.domain.impl.Argument; 26 import com.jaspersoft.jasperserver.api.metadata.xml.domain.impl.ResourceDescriptor; 27 28 import junit.framework.Test; 29 import junit.framework.TestCase; 30 import junit.framework.TestSuite; 31 32 public class WSClientTest extends TestCase { 33 34 private static final String FIELD_USERNAME = "username"; 35 private static final String FIELD_PASSWORD = "password"; 36 private static String targetUrl = JasperServerConstants.WS_END_POINT_URL; 37 private static String reportUnitURI = "/reports/samples/AllAccounts"; 38 39 private static WSClient wsclnt = null; 40 private static JServer jserver = null; 41 42 private boolean errorAllowed = false; 43 44 47 public WSClientTest(String method) { 48 super(method); 49 } 50 public WSClientTest(String method, boolean errorAllowed) { 51 super(method); 52 this.errorAllowed = errorAllowed; 53 } 54 55 58 public void setUp() throws Exception , AxisFault { 59 60 if (wsclnt == null) { 61 jserver = new JServer(); 62 jserver.setUrl(targetUrl); 63 jserver.setUsername(JasperServerConstants.USERNAME); 64 jserver.setPassword(JasperServerConstants.PASSWORD); 65 wsclnt = new WSClient(jserver); 66 } 67 } 68 69 72 public void tearDown() { 73 } 75 76 80 public static void main(String [] args) { 81 try { 82 junit.textui.TestRunner.run(suite()); 83 } catch (Exception _ex) { 84 _ex.printStackTrace(); 85 } 86 } 87 88 93 public static Test suite() throws Exception { 94 95 TestSuite suite = new TestSuite(); 96 97 TestCase testGetServiceClient = new WSClientTest("testGetServiceClient"); 99 TestCase testGetRequestEnvelope = new WSClientTest("testGetRequestEnvelope"); 100 TestCase testInValidLogin = new WSClientTest("testInValidLogin"); 101 TestCase testListDataSources = new WSClientTest("testListDataSources"); 102 TestCase testListXML = new WSClientTest("testListXML"); 103 TestCase testListResDesc = new WSClientTest("testListResDesc"); 104 TestCase testGet = new WSClientTest("testGet"); 105 TestCase testPut = new WSClientTest("testPut"); 106 TestCase testDelete = new WSClientTest("testDelete"); 107 TestCase testDeleteWithPossError = new WSClientTest("testDelete", false); 108 TestCase testTraffic = new WSClientTest("testTraffic"); 109 TestCase testGetJasperPrint = new WSClientTest("testGetJasperPrint"); 110 TestCase testGetJasperPrintWithParameters = new WSClientTest("testGetJasperPrintWithParameters"); 111 TestCase testGetJasperPrintWithInvalidParameters = new WSClientTest("testGetJasperPrintWithInvalidParameters"); 112 TestCase testGetPDF = new WSClientTest("testGetPDF"); 114 115 suite.addTest(testGetServiceClient); 117 suite.addTest(testGetRequestEnvelope); 118 suite.addTest(testListDataSources); 119 120 123 suite.addTest(testListResDesc); 124 suite.addTest(testGet); 125 suite.addTest(testDeleteWithPossError); 126 suite.addTest(testPut); 127 suite.addTest(testDelete); 128 suite.addTest(testTraffic); 129 suite.addTest(testGetJasperPrint); 130 suite.addTest(testGetJasperPrintWithParameters); 131 suite.addTest(testGetJasperPrintWithInvalidParameters); 132 suite.addTest(testGetPDF); 135 suite.addTest(testInValidLogin); 136 return suite; 137 138 } 139 140 146 public void testInValidLogin() throws AxisFault, MarshalException, ValidationException { 147 148 System.out.println("testInValidLogin"); 149 try { 150 JServer invalidJserver = new JServer(); 151 invalidJserver.setUrl(targetUrl); 152 invalidJserver.setUsername(JasperServerConstants.USERNAME2); 153 invalidJserver.setPassword(JasperServerConstants.BAD_PASSWORD2); 154 WSClient invalidWsclnt = new WSClient(invalidJserver); 155 invalidWsclnt.listDatasources(); 156 fail("No exception thrown at invalid Login"); 157 } catch (Exception _ex) { 158 return; 159 } 160 assertTrue(true); 162 System.out.println("end testInValidLogin"); 163 164 } 165 166 171 public void testGetRequestEnvelope() { 172 System.out.println("testGetRequestEnvelope"); 173 174 SortedMap sm = new TreeMap (); 175 sm.put(FIELD_USERNAME, JasperServerConstants.USERNAME2); 176 sm.put(FIELD_PASSWORD, JasperServerConstants.PASSWORD2); 177 SOAPEnvelope senv = wsclnt.getRequestEnvelope("list", sm); 178 assertNotNull("SOAP Request Envelope is NULL", senv); 179 System.out.println("end testGetRequestEnvelope"); 180 181 } 182 183 188 public void testGetServiceClient() { 189 System.out.println("testGetServiceClient"); 190 191 ServiceClient sc = null; 192 try { 193 sc = wsclnt.getServiceClient(); 194 } catch (AxisFault e) { 195 e.printStackTrace(); 196 assertNotNull("getServiceClient threw AxisFault", sc); 197 } 198 assertNotNull("Service Client is NULL", sc); 199 System.out.println("end testGetServiceClient"); 200 201 } 202 203 208 public void testListDataSources() throws Exception { 209 System.out.println("testListDataSources"); 210 211 List lst = wsclnt.listDatasources(); 212 assertNotNull("Datasources is null", lst); 213 assertTrue("No datasources returned", lst.size()>0); 214 System.out.println("end testListDataSources"); 215 216 } 217 218 224 public void testListXML() throws Exception { 225 System.out.println("testListXML"); 226 227 StringBuffer xmlReq=new StringBuffer (1000); 228 xmlReq.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 229 xmlReq.append("<request>" + 230 "<operation>list</operation>" + 231 "<operationArgs>"); 232 xmlReq.append("<limitNumber>false</limitNumber><limitNumberValue>0</limitNumberValue>"); 233 xmlReq.append("<getChildren>false</getChildren>" + 234 "</operationArgs>"); 235 xmlReq.append("<resourceDescriptor>" + 236 "<soapType>folder</soapType><uri>/reports</uri>"); 237 xmlReq.append("<name>reports</name><label>Reports</label><description></description><version></version>"); 238 xmlReq.append("<parentFolder>/</parentFolder><resourceType></resourceType><creationDate></creationDate>"); 239 xmlReq.append("<isNew></isNew></resourceDescriptor>" + 240 "</request>"); 241 242 List lst = wsclnt.list(xmlReq.toString()); 243 assertNotNull("Null LIST returned", lst); 244 assertFalse("List has no valid content", lst.isEmpty()); 245 246 System.out.println("end testListXML"); 247 } 248 249 254 public void testListResDesc() throws Exception { 255 System.out.println("testListResDesc"); 256 257 String folderUri = "/images"; 258 ResourceDescriptor rdis = new ResourceDescriptor(); 259 rdis.setUriString(folderUri); 260 rdis.setWsType(ResourceDescriptor.TYPE_FOLDER); 261 List list = wsclnt.list(rdis); 262 assertNotNull(" List returned is NULL", list); 263 assertTrue("List has no valid content", list.size()>0); 264 System.out.println("end testListResDesc"); 265 } 266 267 272 public void testGet() throws Exception { 273 System.out.println("testGet"); 274 275 String imgUri = "/images/JRLogo"; 276 ResourceDescriptor rdis = new ResourceDescriptor(); 277 rdis.setParentFolder("/images"); 278 rdis.setUriString(imgUri); 279 280 ResourceDescriptor result = wsclnt.get(rdis, null); 281 assertNotNull("Object returned is NULL", result); 282 String imgName = result.getName(); 283 assertNotNull("Image Name is NULL", imgName); 284 System.out.println("end testGet"); 285 286 } 287 288 293 public void testPut() throws Exception { 294 System.out.println("testPut"); 295 296 ResourceDescriptor rdis = new ResourceDescriptor(); 297 rdis.setResourceType(ResourceDescriptor.TYPE_IMAGE); 298 rdis.setName("testImageName"); 299 rdis.setLabel("TestImageLabel"); 300 rdis.setDescription("Test Image Description"); 301 rdis.setParentFolder("/images"); 302 rdis.setUriString(rdis.getParentFolder() + "/" + rdis.getName()); 304 rdis.setWsType(ResourceDescriptor.TYPE_IMAGE); 305 File img = new File (getFileResourceURL("logo.jpg")); rdis.setHasData(true); 307 rdis.setIsNew(true); 308 ResourceDescriptor result = wsclnt.addOrModifyResource(rdis, img); 309 assertNotNull("Upload image is Un-Successful", result); 310 System.out.println("end testPut"); 311 312 } 313 314 319 public void testDelete() { 320 System.out.println("testDelete"); 321 322 try { 323 String imgUri = "/images/testImageName"; 324 ResourceDescriptor rdes = new ResourceDescriptor(); 325 rdes.setUriString(imgUri); 326 wsclnt.delete(rdes); 327 } catch (Exception ex) { 328 boolean isError = true; 329 if (errorAllowed) { 330 if (ex.getMessage().indexOf("Resource not found") >= 0) { 331 isError = false; 332 } 333 } 334 if (isError) { 335 fail("Image Delete Unsuccessful"); 336 } 337 } 338 System.out.println("end testDelete"); 339 } 340 341 public void testTraffic() throws Exception { 342 System.out.println("testTraffic"); 343 344 for (int i = 0; i < 10; i++) { 345 testGet(); 346 Thread.sleep(1000); 347 testPut(); 348 Thread.sleep(1000); 349 testDelete(); 350 Thread.sleep(1000); 351 } 352 System.out.println("end testTraffic"); 353 354 } 355 356 public void testGetJasperPrint() throws Exception { 357 System.out.println("testGetJasperPrint"); 358 359 ResourceDescriptor rdes = new ResourceDescriptor(); 360 rdes.setUriString(reportUnitURI); 361 JasperPrint js = wsclnt.runReport(rdes, new HashMap ()); 362 assertNotNull("No JasperPrint", js); 363 System.out.println("end testGetJasperPrint"); 364 } 365 366 public void testGetJasperPrintWithParameters() throws Exception { 367 System.out.println("testGetJasperPrintWithParameters"); 368 369 String imgUri = "/reports/samples/SalesByMonth"; 370 ResourceDescriptor rdes = new ResourceDescriptor(); 371 rdes.setUriString(imgUri); 372 373 Map args = new HashMap (); 374 args.put("TextInputControl", "5"); 375 376 377 JasperPrint js = wsclnt.runReport(rdes, args); 378 379 assertNotNull("No JasperPrint", js); 380 System.out.println("end testGetJasperPrintWithParameters"); 381 } 382 383 public void testGetJasperPrintWithInvalidParameters() throws Exception { 384 System.out.println("testGetJasperPrintWithInvalidParameters"); 385 386 String imgUri = "/reports/samples/SalesByMonth"; 387 ResourceDescriptor rdes = new ResourceDescriptor(); 388 rdes.setUriString(imgUri); 389 390 Map args = new HashMap (); 391 args.put("TextInputControl", "BAD-HAS TO BE A NUMBER"); 392 393 394 JasperPrint js = wsclnt.runReport(rdes, args); 395 396 assertNotNull("No JasperPrint", js); 397 System.out.println("end testGetJasperPrintWithInvalidParameters"); 398 } 399 400 403 public void testGetHTML() throws Exception { 404 System.out.println("testGetHTML"); 405 406 ResourceDescriptor rdes = new ResourceDescriptor(); 407 rdes.setUriString(reportUnitURI); 408 409 List args = new ArrayList (); 410 args.add(new Argument(Argument.RUN_OUTPUT_FORMAT, Argument.RUN_OUTPUT_FORMAT_HTML)); 411 412 Map attachments = wsclnt.runReport(rdes, new HashMap (), args); 413 414 assertNotNull("No result", attachments); 415 416 Iterator it = attachments.entrySet().iterator(); 417 418 while (it.hasNext()) { 419 Map.Entry entry = (Map.Entry ) it.next(); 420 System.out.println("Got " + entry.getKey()); 421 } 422 423 FileContent content = null; 424 if (attachments != null && !attachments.isEmpty()) { 425 content = (FileContent) attachments.get("report"); 426 } 427 assertNotNull("No report", content); 428 429 assertTrue("not html. was: " + content.getMimeType(), content.getMimeType().equals("text/html")); 430 431 content = (FileContent) attachments.get("px"); 432 assertNotNull("No spacer image", content); 433 434 assertTrue("not gif. was: " + content.getMimeType(), content.getMimeType().equals("image/gif")); 435 436 content = (FileContent) attachments.get("img_0_0_0"); 437 assertNotNull("No image", content); 438 439 assertTrue("not jpeg. was: " + content.getMimeType(), content.getMimeType().equals("image/jpeg")); 440 441 System.out.println("end testGetHTML"); 442 } 443 444 public void testGetPDF() throws Exception { 445 System.out.println("testGetPDF"); 446 447 ResourceDescriptor rdes = new ResourceDescriptor(); 448 rdes.setUriString(reportUnitURI); 449 450 List args = new ArrayList (); 451 args.add(new Argument(Argument.RUN_OUTPUT_FORMAT, Argument.RUN_OUTPUT_FORMAT_PDF)); 452 453 Map attachments = wsclnt.runReport(rdes, new HashMap (), args); 454 455 assertNotNull("No result", attachments); 456 457 FileContent content = null; 458 if (attachments != null && !attachments.isEmpty()) { 459 content = (FileContent) attachments.get("report"); 460 } 461 assertNotNull("No PDF report", content); 462 463 assertTrue("not pdf. was: " + content.getMimeType(), content.getMimeType().equals("application/pdf")); 464 465 System.out.println("end testGetPDF"); 466 } 467 468 471 private String getFileResourceURL(String name) { 472 return getClass().getClassLoader().getResource(name).getPath(); 473 } 474 } 475 | Popular Tags |