View Javadoc

1   package com.atlassian.webdriver.greenhopper.page.admin;
2   
3   import com.atlassian.pageobjects.binder.Init;
4   import com.atlassian.webdriver.jira.page.JiraAdminAbstractPage;
5   import com.atlassian.webdriver.utils.by.ByJquery;
6   import com.atlassian.webdriver.utils.Check;
7   import org.apache.commons.lang.StringUtils;
8   import org.openqa.selenium.By;
9   import org.openqa.selenium.WebElement;
10  import org.openqa.selenium.support.FindBy;
11  
12  import java.text.ParseException;
13  import java.text.SimpleDateFormat;
14  import java.util.Date;
15  import java.util.List;
16  import java.util.regex.Matcher;
17  import java.util.regex.Pattern;
18  
19  /**
20   * @since 2.0
21   */
22  public class GreenHopperLicenseDetailsPage extends JiraAdminAbstractPage
23  {
24  
25      private static final String URI = "/secure/GHLicense.jspa?decorator=admin";
26      private static final String DATE_FORMAT = "dd/MMM/yy";
27  
28      @FindBy (name = "newLicense")
29      private WebElement updateLicenseTextArea;
30  
31      @FindBy (name = "jiraform")
32      private WebElement updateLicenseForm;
33  
34      @FindBy (id = "license_table")
35      private WebElement licenseTable;
36  
37      private String organisation;
38      private Date purchaseDate;
39      private String licenseType;
40      private Date expiryDate;
41      private String serverId;
42      private String supportEntitlmentNumber;
43  
44      private String errorMessage = "";
45  
46      private boolean licenseIsLoaded = false;
47  
48  
49      public String getUrl()
50      {
51          return URI;
52      }
53  
54  
55      @Init
56      private void readLicense()
57      {
58          if(Check.elementExists(By.className("errMsg"), updateLicenseForm))
59          {
60              errorMessage = updateLicenseForm.findElement(By.className("errMsg")).getText();
61          }
62  
63          List<WebElement> rows = licenseTable.findElements(By.tagName("tr"));
64  
65          organisation = rows.get(0).findElement(ByJquery.$("td ~ td")).getText();
66  
67          if (StringUtils.isNotEmpty(organisation))
68          {
69              licenseIsLoaded = true;
70  
71              purchaseDate = formatDate(rows.get(1).findElement(ByJquery.$("td ~ td strong")).getText());
72  
73              WebElement licenseTypeColumn = rows.get(2).findElement(ByJquery.$("td ~ td"));
74  
75              licenseType = licenseTypeColumn.findElement(By.tagName("strong")).getText();
76              expiryDate = formatDate(extractExpiryDate(licenseTypeColumn.getText()));
77  
78              serverId = rows.get(3).findElement(ByJquery.$("td ~ td strong")).getText();
79  
80              supportEntitlmentNumber = rows.get(4).findElement(ByJquery.$("td ~ td strong")).getText();
81  
82          }
83  
84      }
85  
86      private String extractExpiryDate(String licenseTypeStr)
87      {
88          licenseTypeStr = licenseTypeStr.replaceAll("\\n", "");
89  
90          Pattern p = Pattern.compile("(\\d{2}/[A-Za-z]{3}/\\d{2})", Pattern.MULTILINE);
91          Matcher m = p.matcher(licenseTypeStr);
92  
93          String expiryStr = null;
94  
95          if (m.find())
96          {
97              expiryStr = m.group(1);
98          }
99  
100         return expiryStr;
101     }
102 
103     private Date formatDate(String date)
104     {
105         SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
106 
107         try
108         {
109             return sdf.parse(date);
110         }
111         catch (ParseException e)
112         {
113             // Do nothing
114         }
115 
116         return null;
117 
118     }
119 
120     /**
121      * Gets the error message in the update admin form area.
122      * @return a String of the error message or empty string if there is no error
123      */
124     public String getErrorMessage()
125     {   
126         return errorMessage;
127     }
128 
129     public boolean isTrial()
130     {
131         if (licenseIsLoaded())
132         {
133             return supportEntitlmentNumber.equals("NONE/TRIAL");
134         }
135 
136         return false;
137     }
138 
139     public boolean licenseIsLoaded()
140     {
141         return licenseIsLoaded;
142     }
143 
144     public String getOrganization()
145     {
146         return organisation;
147     }
148 
149     public String getLicenseType()
150     {
151         return getLicenseType();
152     }
153 
154     public String getServerId()
155     {
156         return serverId;
157     }
158 
159     public String getSupportEntitlementNumber()
160     {
161         return supportEntitlmentNumber;
162     }
163 
164     public Date getPurchasedDate()
165     {
166         return purchaseDate;
167     }
168 
169     public Date getExpiryDate()
170     {
171         return expiryDate;
172     }
173 
174     public GreenHopperLicenseDetailsPage updateLicense(String license)
175     {
176         updateLicenseTextArea.sendKeys(license);
177         updateLicenseForm.submit();
178 
179         return pageBinder.bind(GreenHopperLicenseDetailsPage.class);
180     }
181 }