]> WPIA git - gigi.git/blob - tests/club/wpia/gigi/testUtils/PingTest.java
upd: rename package name and all references to it
[gigi.git] / tests / club / wpia / gigi / testUtils / PingTest.java
1 package club.wpia.gigi.testUtils;
2
3 import static org.junit.Assert.*;
4 import static org.junit.Assume.*;
5
6 import java.io.IOException;
7 import java.net.HttpURLConnection;
8 import java.net.MalformedURLException;
9 import java.net.URL;
10 import java.net.URLConnection;
11 import java.sql.SQLException;
12 import java.util.regex.Matcher;
13 import java.util.regex.Pattern;
14
15 import org.junit.After;
16
17 import club.wpia.gigi.database.GigiPreparedStatement;
18 import club.wpia.gigi.database.GigiResultSet;
19 import club.wpia.gigi.pages.account.domain.DomainOverview;
20 import club.wpia.gigi.util.SystemKeywords;
21
22 /**
23  * Base class for test suites that check extensively if the domain-ping
24  * functionality wroks as expected.
25  */
26 public abstract class PingTest extends ClientTest {
27
28     protected String csrf;
29
30     protected static void updateService(String token, String value, String action) throws IOException, MalformedURLException {
31         String manage = getTestProps().getProperty("domain.manage");
32         assumeNotNull(manage);
33         String url = manage + "t1=" + token + "&t2=" + value + "&action=" + action;
34         assertEquals(200, ((HttpURLConnection) new URL(url).openConnection()).getResponseCode());
35     }
36
37     protected void waitForPings(int count) throws SQLException, InterruptedException {
38         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT COUNT(*) FROM `domainPinglog`")) {
39             long start = System.currentTimeMillis();
40             while (System.currentTimeMillis() - start < 10000) {
41                 GigiResultSet rs = ps.executeQuery();
42                 rs.next();
43                 if (rs.getInt(1) >= count) {
44                     break;
45                 }
46                 Thread.sleep(200);
47             }
48         }
49     }
50
51     protected String sendDomainForm(String content) throws IOException, MalformedURLException {
52         URLConnection openConnection = get(DomainOverview.PATH);
53         openConnection.setDoOutput(true);
54         openConnection.getOutputStream().write(content.getBytes("UTF-8"));
55         openConnection.getHeaderField("Location");
56         int code = ((HttpURLConnection) openConnection).getResponseCode();
57         if (code != 302) {
58             throw new Error("Code was: " + code + "\ncontent was: " + fetchStartErrorMessage(IOUtils.readURL(openConnection)));
59         }
60
61         String newcontent = IOUtils.readURL(get(DomainOverview.PATH));
62         Pattern dlink = Pattern.compile(DomainOverview.PATH + "/([0-9]+)'>");
63         Matcher m1 = dlink.matcher(newcontent);
64         if ( !m1.find()) {
65             throw new Error(newcontent);
66         }
67         return DomainOverview.PATH + "/" + m1.group(1);
68     }
69
70     protected Matcher initailizeDomainForm() throws IOException, Error {
71         String content1 = IOUtils.readURL(get(DomainOverview.PATH));
72         csrf = getCSRF(1, content1);
73
74         Pattern p = Pattern.compile("([A-Za-z0-9]+)." + SystemKeywords.DNS_PREFIX + "._auth IN TXT ([A-Za-z0-9]+)");
75         Matcher m = p.matcher(content1);
76         m.find();
77         return m;
78     }
79
80     @After
81     public void purgeDbAfterTest() throws SQLException, IOException {
82         purgeDatabase();
83     }
84
85 }