1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.miloss.fgsms.test;
21
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.StringWriter;
25 import java.sql.Connection;
26 import java.sql.PreparedStatement;
27 import java.sql.ResultSet;
28 import java.sql.SQLException;
29 import java.util.Properties;
30 import java.util.UUID;
31 import javax.naming.Context;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.xml.bind.Marshaller;
34 import javax.xml.datatype.DatatypeFactory;
35 import javax.xml.ws.handler.MessageContext;
36 import org.miloss.fgsms.common.IpAddressUtility;
37 import org.miloss.fgsms.common.Utility;
38 import org.miloss.fgsms.services.interfaces.common.PolicyType;
39 import org.miloss.fgsms.services.interfaces.policyconfiguration.FederationPolicyCollection;
40 import org.miloss.fgsms.services.interfaces.policyconfiguration.TransactionalWebServicePolicy;
41 import org.junit.*;
42 import static org.junit.Assert.*;
43 import org.miloss.fgsms.common.Constants;
44 import org.postgresql.ds.PGPoolingDataSource;
45 import us.gov.ic.ism.v2.ClassificationType;
46
47
48
49
50
51 public abstract class WebServiceBaseTests {
52
53 protected static String url = null;
54 protected static String INVOCATION_URL = null;
55 protected static MyWebServiceContext adminctx = null;
56 protected static MyWebServiceContext agentctx = null;
57 protected static MyWebServiceContext userbobctx = null;
58 protected static MyWebServiceContext auditctx = null;
59 protected static MyWebServiceContext usermaryctx = null;
60 public static String adminusername = "fgsmsadmin";
61 public static String auditusername = "fgsmsaudit";
62 public static String agentusername = "fgsmsagent";
63 public static String bobusername = "jUnitBob";
64 public static String maryusername = "jUnitMary";
65 public static DatatypeFactory df = null;
66 protected static PGPoolingDataSource ds = null;
67 private static boolean initialized = false;
68
69 public WebServiceBaseTests() throws Exception {
70 }
71
72 public void Init() throws Exception {
73
74 df = DatatypeFactory.newInstance();
75 if (INVOCATION_URL == null) {
76 INVOCATION_URL = url;
77 }
78 url = IpAddressUtility.modifyURL(url, false);
79 INVOCATION_URL = IpAddressUtility.modifyURL(INVOCATION_URL, false);
80 MyMessageContext mc = new MyMessageContext();
81
82 HttpServletRequest req = new MyHttpServletRequest(adminusername);
83 mc.put(MessageContext.SERVLET_REQUEST, req);
84 adminctx = new MyWebServiceContext(mc, adminusername);
85
86 mc = new MyMessageContext();
87 req = new MyHttpServletRequest(agentusername);
88 mc.put(MessageContext.SERVLET_REQUEST, req);
89 mc.put("javax.xml.ws.service.endpoint.address", INVOCATION_URL);
90 agentctx = new MyWebServiceContext(mc, agentusername);
91
92 mc = new MyMessageContext();
93 req = new MyHttpServletRequest(bobusername);
94 mc.put(MessageContext.SERVLET_REQUEST, req);
95 mc.put("javax.xml.ws.service.endpoint.address", INVOCATION_URL);
96 userbobctx = new MyWebServiceContext(mc, bobusername);
97
98 mc = new MyMessageContext();
99 req = new MyHttpServletRequest(maryusername);
100 mc.put(MessageContext.SERVLET_REQUEST, req);
101 mc.put("javax.xml.ws.service.endpoint.address", INVOCATION_URL);
102 usermaryctx = new MyWebServiceContext(mc, maryusername);
103
104 mc = new MyMessageContext();
105 req = new MyHttpServletRequest(auditusername);
106 mc.put(MessageContext.SERVLET_REQUEST, req);
107 mc.put("javax.xml.ws.service.endpoint.address", INVOCATION_URL);
108 auditctx = new MyWebServiceContext(mc, auditusername);
109
110 File f = new File("../resources/test-database.properties");
111 Properties db = new Properties();
112 if (f.exists()) {
113 System.out.println("Loading test properties from " + f.getAbsolutePath());
114 FileInputStream fis = new FileInputStream(f);
115 db.load(fis);
116 fis.close();
117 } else {
118 f = new File("../../resources/test-database.properties");
119 if (f.exists()) {
120 System.out.println("Loading test properties from " + f.getAbsolutePath());
121 FileInputStream fis = new FileInputStream(f);
122 db.load(fis);
123 fis.close();
124 } else {
125
126 f = new File("../../../resources/test-database.properties");
127 if (f.exists()) {
128 System.out.println("Loading test properties from " + f.getAbsolutePath());
129 FileInputStream fis = new FileInputStream(f);
130 db.load(fis);
131 fis.close();
132 } else {
133
134 fail("cannot load test-database.properties");
135 }
136 }
137 }
138
139 if (initialized) {
140 return;
141 }
142 initialized = true;
143
144
145 try {
146 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, InitialContextFactoryForTest.class.getName());
147
148
149 ds = new PGPoolingDataSource();
150 ds.setMaxConnections(Integer.parseInt((String) db.get("perf_maxconnections")));
151 ds.setServerName((String) db.get("perf_servername"));
152 ds.setPortNumber(Integer.parseInt((String) db.get("perf_port")));
153 ds.setDatabaseName((String) db.get("perf_db"));
154 ds.setUser((String) db.get("perf_user"));
155 ds.setPassword((String) db.get("perf_password"));
156 ds.setSsl((Boolean) Boolean.parseBoolean((String) db.get("perf_ssl")));
157 ds.setInitialConnections(Integer.parseInt((String) db.get("perf_initialconnections")));
158 System.out.println("Binding to " + ds.getServerName() + ":" + ds.getPortNumber());
159 InitialContextFactoryForTest.bind("java:fgsmsPerformance", ds);
160
161 ds = new PGPoolingDataSource();
162 ds.setMaxConnections(Integer.parseInt((String) db.get("config_maxconnections")));
163 ds.setServerName((String) db.get("config_servername"));
164 ds.setPortNumber(Integer.parseInt((String) db.get("config_port")));
165 ds.setDatabaseName((String) db.get("config_db"));
166 ds.setUser((String) db.get("config_user"));
167 ds.setPassword((String) db.get("config_password"));
168 ds.setSsl((Boolean) Boolean.parseBoolean((String) db.get("config_ssl")));
169 ds.setInitialConnections(Integer.parseInt((String) db.get("config_initialconnections")));
170 System.out.println("Binding to " + ds.getServerName() + ":" + ds.getPortNumber());
171 InitialContextFactoryForTest.bind("java:fgsmsConfig", ds);
172 } catch (Exception ex) {
173 ex.printStackTrace();
174 fail();
175 }
176 }
177
178 @org.junit.BeforeClass
179 public static void setUpClass() throws Exception {
180 }
181
182 @org.junit.AfterClass
183 public static void tearDownClass() throws Exception {
184 }
185
186 @After
187 public void tearDown() {
188 RemoveServicePolicy(url);
189
190
191 }
192
193 public static void RemoveServicePolicy(String u) {
194 Connection con = Utility.getConfigurationDBConnection();
195 Connection perf = Utility.getPerformanceDBConnection();
196 try {
197
198
199 PreparedStatement com = con.prepareStatement(
200 "delete from servicepolicies where uri=?; "
201 + "delete from status where uri=?; "
202 + "delete from userpermissions where objecturi=?;"
203 + "delete from status where uri=?;"
204 + "delete from bueller where uri=?;"
205 + "delete from slasub where uri=?");
206 com.setString(1, u);
207 com.setString(2, u);
208 com.setString(3, u);
209 com.setString(4, u);
210 com.setString(5, u);
211 com.setString(6, u);
212
213 com.execute();
214 con.close();
215 } catch (Exception ex) {
216 ex.printStackTrace();
217 } finally {
218 try {
219 if (!con.isClosed()) {
220
221 con.close();
222
223 }
224 } catch (Exception e) {
225 }
226 }
227
228 try {
229
230
231 PreparedStatement com = perf.prepareStatement(
232 "delete from rawdata where uri=?; "
233 + "delete from agg2 where uri=?;"
234 + "delete from availability where uri=?;"
235 + "delete from alternateurls where uri=?;"
236 + "delete from actionlist where uri=?;"
237 + "delete from brokerhistory where host=?;"
238 + "delete from brokerrawdata where host=?;"
239 + "delete from rawdata where uri=?;"
240 + "delete from rawdatadrives where uri=?;"
241 + "delete from rawdatamachineprocess where uri=?;"
242 + "delete from rawdatanic where uri=?;"
243 + "delete from slaviolations where uri=?;"
244 + "delete from dependencies where destinationurl=?;"
245 + "delete from dependencies where sourceurl=?;"
246 + "delete from rawdatatally where uri=?;");
247 com.setString(1, u);
248 com.setString(2, u);
249 com.setString(3, u);
250 com.setString(4, u);
251 com.setString(5, u);
252 com.setString(6, u);
253 com.setString(7, u);
254 com.setString(8, u);
255 com.setString(9, u);
256 com.setString(10, u);
257 com.setString(11, u);
258 com.setString(12, u);
259 com.setString(13, u);
260 com.setString(14, u);
261 com.setString(15, u);
262
263 com.execute();
264
265 perf.close();
266
267 } catch (Exception ex) {
268 ex.printStackTrace();
269 } finally {
270 try {
271 if (!perf.isClosed()) {
272
273 perf.close();
274
275 }
276 } catch (Exception e) {
277 }
278 }
279 }
280
281 public static void CreatePolicy(String uri) {
282 CreatePolicy(uri, null);
283
284 }
285
286 public static void CreatePolicy(String uri, FederationPolicyCollection fedpolicies) {
287 Connection con = Utility.getConfigurationDBConnection();
288 Connection perf = Utility.getPerformanceDBConnection();
289 try {
290
291 PreparedStatement com = con.prepareStatement("delete from servicepolicies where uri=?;");
292 com.setString(1, uri);
293 com.execute();
294 com = con.prepareStatement(
295 "INSERT INTO servicepolicies("
296 + " uri, datattl, "
297 + " buellerenabled, "
298 + " policytype, xmlpolicy,hasfederation)"
299 + " VALUES (?, ?, ?, ?, ?,?);");
300 com.setString(1, uri);
301 com.setLong(2, 500000);
302 com.setBoolean(3, true);
303 com.setInt(4, PolicyType.TRANSACTIONAL.ordinal());
304 TransactionalWebServicePolicy twp = new TransactionalWebServicePolicy();
305 twp.setURL(uri);
306 twp.setFederationPolicyCollection(fedpolicies);
307 twp.setDescription("jUnit Test Description");
308 DatatypeFactory fac = DatatypeFactory.newInstance();
309 twp.setDataTTL(fac.newDuration(10000000));
310 Marshaller m = Utility.getSerializationContext().createMarshaller();
311 StringWriter sw = new StringWriter();
312 m.marshal(twp, sw);
313
314 String s = sw.toString();
315 com.setBytes(5, s.getBytes(Constants.CHARSET));
316
317 com.setBoolean(6, fedpolicies != null && !fedpolicies.getFederationPolicy().isEmpty());
318
319 com.execute();
320 com.close();
321
322 con.close();
323
324
325 com = perf.prepareStatement("INSERT INTO rawdatatally(uri, success, faults, slafault) VALUES (?, ?, ?, ?);");
326 com.setString(1, uri);
327 com.setLong(2, 0);
328 com.setLong(3, 0);
329 com.setLong(4, 0);
330 com.execute();
331 com.close();
332
333 perf.close();
334
335 } catch (Exception ex) {
336 ex.printStackTrace();
337 } finally {
338 try {
339 if (!con.isClosed()) {
340
341 con.close();
342
343 }
344 } catch (Exception e) {
345 }
346
347 try {
348 if (!perf.isClosed()) {
349
350 perf.close();
351
352 }
353 } catch (Exception e) {
354 }
355 }
356
357 }
358
359 public void VerifyPolicyDeleted(String uri) {
360 boolean ok = true;
361 Connection con = Utility.getConfigurationDBConnection();
362 try {
363
364 PreparedStatement com
365 = com = con.prepareStatement("select * from servicepolicies where uri=?;");
366 com.setString(1, uri);
367 ResultSet rs = com.executeQuery();
368
369 if (rs.next()) {
370 ok = false;
371 }
372 rs.close();
373 com.close();
374
375 con.close();
376
377 } catch (Exception ex) {
378 ex.printStackTrace();
379 ok = false;
380 } finally {
381 try {
382 if (!con.isClosed()) {
383
384 con.close();
385
386 }
387 } catch (Exception e) {
388 }
389 }
390 if (!ok) {
391 fail("policy not deleted");
392 }
393 }
394
395 public void adduser(String bobusername) throws SQLException {
396 Connection con = Utility.getConfigurationDBConnection();
397 try {
398 PreparedStatement ps = con.prepareStatement("delete from userpermissions where username=?; delete from slasub where username=?; delete from users where username=?;"
399 + "INSERT INTO users( username , displayname , email ) VALUES( ?, ?, ?); ");
400 ps.setString(1, bobusername);
401 ps.setString(2, bobusername);
402 ps.setString(3, bobusername);
403 ps.setString(4, bobusername);
404 ps.setString(5, "junit");
405 ps.setString(6, bobusername + "@localhost.localdomain");
406 ps.execute();
407 } catch (Exception x) {
408 x.printStackTrace();
409 } finally {
410 try {
411 if (!con.isClosed()) {
412
413 con.close();
414
415 }
416 } catch (Exception e) {
417 }
418 }
419 }
420
421
422 public void resetClassLevel() throws SQLException {
423 Connection con = Utility.getConfigurationDBConnection();
424 try {
425
426 PreparedStatement ps = con.prepareStatement("UPDATE globalpolicies SET classification=?, caveat=?");
427
428 ps.setString(1, ClassificationType.U.value());
429 ps.setString(2, "");
430 ps.execute();
431 } catch (Exception x) {
432 x.printStackTrace();
433 } finally {
434 try {
435 if (!con.isClosed()) {
436
437 con.close();
438
439 }
440 } catch (Exception e) {
441 }
442 }
443 }
444
445 public String Insert_WS_Transaction(String url, String agent) throws Exception {
446 System.out.println("attempting to insert ws transaction for " + url);
447 Connection con = Utility.getPerformanceDBConnection();
448 try {
449 PreparedStatement cmd = con.prepareStatement("delete from rawdata where uri=?");
450 cmd.setString(1, url);
451 cmd.execute();
452 cmd.close();
453 cmd = con.prepareStatement("INSERT INTO rawdata( "
454 + " uri, responsetimems, monitorsource, hostingsource, requestxml, "
455 + " responsexml, consumeridentity, transactionid, soapaction, responsesize, "
456 + " requestsize, utcdatetime, success, slafault, agenttype, originalurl, "
457 + " message, relatedtransactionid, threadid, requestheaders, responseheaders)"
458 + " VALUES (?, ?, ?, ?, ?, "
459 + " ?, ?, ?, ?, ?, "
460 + " ?, ?, ?, ?, ?, ?, "
461 + " ?, ?, ?, ?, ?);");
462 cmd.setString(1, url);
463 String id = UUID.randomUUID().toString();
464 cmd.setInt(2, 1000);
465 cmd.setString(3, Utility.getHostName());
466 cmd.setString(4, Utility.getHostName());
467 cmd.setObject(5, Utility.EN(new String("<body>helloworld</body>")).getBytes(Constants.CHARSET));
468 cmd.setObject(6, Utility.EN(new String("<body>helloworld response</body>")).getBytes(Constants.CHARSET));
469 cmd.setString(7, System.getenv("USERNAME"));
470 cmd.setString(8, id);
471 cmd.setString(9, "POST");
472 cmd.setInt(10, 1024);
473 cmd.setInt(11, 1024);
474 cmd.setLong(12, System.currentTimeMillis());
475 cmd.setBoolean(13, false);
476 cmd.setString(14, "sla fault!");
477 cmd.setString(15, agent);
478 cmd.setString(16, url);
479 cmd.setObject(17, "hello from junit".getBytes());
480 cmd.setString(18, UUID.randomUUID().toString());
481 cmd.setString(19, UUID.randomUUID().toString());
482 cmd.setObject(20, Utility.EN(new String("SOAPAction: something")).getBytes(Constants.CHARSET));
483 cmd.setObject(21, Utility.EN(new String("Response 200 OK")).getBytes(Constants.CHARSET));
484 cmd.execute();
485
486 cmd.close();
487
488 con.close();
489
490 return id;
491 } catch (Exception x) {
492 x.printStackTrace();
493 } finally {
494 try {
495 if (!con.isClosed()) {
496
497 con.close();
498
499 }
500 } catch (Exception e) {
501 }
502 }
503 System.out.println("insert failed ws transaction for " + url);
504 throw new Exception();
505 }
506
507 public void RemoveTransaction(String id) throws Exception {
508 Connection con = Utility.getPerformanceDBConnection();
509 try {
510 PreparedStatement cmd = con.prepareStatement("delete from rawdata where transactionid=?");
511 cmd.setString(1, id);
512 cmd.execute();
513 cmd.close();
514
515
516 con.close();
517
518
519 } catch (Exception x) {
520 x.printStackTrace();
521 } finally {
522 try {
523 if (!con.isClosed()) {
524
525 con.close();
526
527 }
528 } catch (Exception e) {
529 }
530 }
531 }
532
533 public void GrantUserAuditAccess(String u, String url) throws Exception {
534 Connection con = Utility.getConfigurationDBConnection();
535 try {
536 PreparedStatement cmd = con.prepareStatement("INSERT INTO userpermissions( "
537 + " username, objecturi, auditobject)"
538 + " VALUES (?, ?, ?);");
539 cmd.setString(1, u);;
540 cmd.setString(2, url);;
541 cmd.setBoolean(3, true);
542 cmd.execute();
543 cmd.close();
544
545
546 con.close();
547
548
549 } catch (Exception x) {
550 x.printStackTrace();
551 } finally {
552 try {
553 if (!con.isClosed()) {
554
555 con.close();
556
557 }
558 } catch (Exception e) {
559 }
560 }
561 }
562
563 public void RemoveAuditAccess(String u, String url) throws Exception {
564 Connection con = Utility.getConfigurationDBConnection();
565 try {
566 PreparedStatement cmd = con.prepareStatement("delete from userpermissions where username=? and objecturi=?; ");
567
568 cmd.setString(1, u);
569 cmd.setString(2, url);
570
571 cmd.execute();
572 cmd.close();
573
574
575 con.close();
576
577
578 } catch (Exception x) {
579 x.printStackTrace();
580 } finally {
581 try {
582 if (!con.isClosed()) {
583
584 con.close();
585
586 }
587 } catch (Exception e) {
588 }
589 }
590 }
591
592
593
594
595
596
597
598 public void RemoveTransactions(String url) throws Exception {
599
600 Connection con = Utility.getPerformanceDBConnection();
601 try {
602 PreparedStatement cmd = con.prepareStatement("delete from rawdata where uri=?");
603 cmd.setString(1, url);
604 cmd.execute();
605 cmd.close();
606
607
608 con.close();
609
610
611 } catch (Exception x) {
612 x.printStackTrace();
613 } finally {
614 try {
615 if (!con.isClosed()) {
616
617 con.close();
618
619 }
620 } catch (Exception e) {
621 }
622 }
623 }
624 }