View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.testtools.selenium;
17  
18  import org.apache.commons.io.FileUtils;
19  import org.junit.After;
20  import org.junit.Before;
21  import org.junit.Test;
22  
23  import java.io.File;
24  import java.io.IOException;
25  import java.net.MalformedURLException;
26  
27  /**
28   * @author Kuali Rice Team (rice.collab@kuali.org)
29   */
30  public class JenkinsJsonAllJobsResults extends JenkinsJsonJobResultsBase {
31  
32      @Before
33      public void setUp() throws MalformedURLException, InterruptedException {
34          super.setUp();
35      }
36  
37      @After
38      public void tearDown() {
39          closeAndQuitWebDriver();
40      }
41  
42      @Test
43      public void testGetAllJobResults() throws Exception {
44          for (int i = 0, s = jobs.length; i < s; i++) {
45              getResults(jobs[i]);
46          }
47      }
48  
49      private void getResults(String job) throws Exception {
50          String url = null;
51              String jobJson;
52              String json;
53              String outputFile;
54  
55              url = jenkinsBase + "/job/" + job + "/api/json";
56  
57              jobJson = retrieveJson(url);
58  
59              String jsonJobNumber;
60              while (jobJson.contains(("{\"number\":"))) {
61                  jsonJobNumber = jobJson.substring(jobJson.indexOf("{\"number\":") + 10, jobJson.length());
62                  jsonJobNumber = jsonJobNumber.substring(0, jsonJobNumber.indexOf(","));
63  
64                  jobJson = jobJson.substring(jobJson.indexOf("{\"number\":") + 9, jobJson.length()); // strip off while condition
65  
66                  try {
67                      writeJobResults(job, jsonJobNumber);
68                  } catch (Exception e) {
69                      passed = false;
70                      System.out.println("job: " + job + " url: " + url + " " + e.getMessage());
71                      e.printStackTrace();
72                  }
73              }
74  
75      }
76  
77      private void writeJobResults(String job, String jobNumber) throws InterruptedException, IOException {
78          String url;
79          String json;
80          String outputFile;
81          url = jenkinsBase + "/job/" + job + "/" + jobNumber + "/testReport/api/json";
82          json = retrieveJson(url);
83  
84          outputFile = job + "-" + jobNumber + ".json";
85          if (outputDirectory != null) {
86              outputFile = outputDirectory + File.separatorChar + outputFile;
87          }
88  
89          json = json.replaceAll("}],", "}],\n");
90          FileUtils.writeStringToFile(new File(outputFile), json);
91      }
92  }