001/**
002 * Copyright 2005-2015 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.testtools.common;
017
018import java.io.BufferedReader;
019import java.io.InputStreamReader;
020import java.net.HttpURLConnection;
021import java.net.URL;
022import java.util.Iterator;
023
024/**
025 * Prints out status of JiraAware Jiras from JiraAwareContainsFailures.properties and JiraAwareRegexFailures.properties
026 * files.
027 *
028 * @author Kuali Rice Team (rice.collab@kuali.org)
029 */
030public class JiraAwareStatusCheck {
031
032    public static void main(String[] args ) {
033        String baseUrl = "https://jira.kuali.org/browse/";
034        String jira = "";
035        String statusSpan = "<span id=\"status-val\" class=\"value\">";
036
037        checkJiraMatchesContains(baseUrl, jira, statusSpan);
038        checkJiraMatchesRegex(baseUrl, jira, statusSpan);
039    }
040
041    private static void checkJiraMatchesContains(String baseUrl, String jira, String statusSpan) {
042        String key;
043        String status;
044        Iterator iter = JiraAwareFailureUtils.jiraMatches.keySet().iterator();
045        while (iter.hasNext()) {
046            key = (String)iter.next();
047            jira = JiraAwareFailureUtils.jiraMatches.getProperty(key);
048            jira = jira.substring(0, jira.indexOf(" "));
049            try {
050                status = getStatus(baseUrl, jira, statusSpan);
051                System.out.println(jira + " Status: " + status + " for key: " + key);
052            } catch (Exception e) {
053                System.out.println("Exception reading " + baseUrl + jira + " " + e.getMessage());
054                e.printStackTrace();
055            }
056        }
057    }
058
059    private static void checkJiraMatchesRegex(String baseUrl, String jira, String statusSpan) {
060        String key;
061        String status;
062        Iterator iter = JiraAwareFailureUtils.regexJiraMatches.keySet().iterator();
063        while (iter.hasNext()) {
064            key = (String)iter.next();
065            jira = JiraAwareFailureUtils.regexJiraMatches.getProperty(key);
066            jira = jira.substring(0, jira.indexOf(" "));
067            try {
068                status = getStatus(baseUrl, jira, statusSpan);
069                System.out.println(jira + " Status: " + status + " for key: " + key);
070            } catch (Exception e) {
071                System.out.println("Exception reading " + baseUrl + jira + " " + e.getMessage());
072                e.printStackTrace();
073            }
074        }
075    }
076
077    private static String getStatus(String baseUrl, String jira, String statusSpan) throws Exception {
078        String contents = JiraAwareStatusCheck.getText(baseUrl + jira);
079//        System.out.println("\n" + contents + "\n");
080        contents = contents.substring(contents.indexOf(statusSpan) + statusSpan.length(), contents.length());
081        contents = contents.substring(0, contents.indexOf("</span>"));
082        String status = contents.substring(contents.indexOf("alt=\"") + 5, contents.length());
083        status = status.substring(0, status.indexOf("\""));
084        return status;
085    }
086
087    public static String getText(String url) throws Exception {
088        URL website = new URL(url);
089        HttpURLConnection connection = (HttpURLConnection)website.openConnection();
090        connection.setInstanceFollowRedirects(true);
091        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
092
093        StringBuilder response = new StringBuilder();
094        String inputLine;
095
096        while ((inputLine = in.readLine()) != null) {
097            response.append(inputLine);
098        }
099
100        in.close();
101
102        return response.toString();
103    }
104}