11package com .projectkaiser .scm .jenkins .api ;
22
3- import static org .junit .Assert .*;
3+ import static org .junit .Assert .assertEquals ;
4+ import static org .junit .Assert .assertFalse ;
5+ import static org .junit .Assert .assertTrue ;
46
57import java .io .BufferedReader ;
8+ import java .io .ByteArrayInputStream ;
69import java .io .IOException ;
710import java .io .InputStream ;
811import java .io .InputStreamReader ;
9- import java .io .StringReader ;
1012import java .io .StringWriter ;
11- import java .util .List ;
1213
1314import javax .xml .parsers .DocumentBuilder ;
1415import javax .xml .parsers .DocumentBuilderFactory ;
2122import org .junit .Test ;
2223import org .w3c .dom .Document ;
2324import org .w3c .dom .NodeList ;
24- import org .xml .sax .InputSource ;
2525import org .xml .sax .SAXException ;
2626
2727import com .projectkaiser .scm .jenkins .data .JobDetailed ;
28- import com .sun .org .apache .xml .internal .security .utils .XMLUtils ;
2928import com .sun .org .apache .xml .internal .serialize .OutputFormat ;
3029import com .sun .org .apache .xml .internal .serialize .XMLSerializer ;
31- import com .sun .xml .internal .ws .util .xml .XmlUtil ;
3230
3331public class JenkinsApiTest {
3432
@@ -44,6 +42,8 @@ public class JenkinsApiTest {
4442 private static final String TEST_JOB_XML_FN = "TestJob.xml" ;
4543
4644 private static final String TEST_JOB_NAME = "pk_jenkins_test_job" ;
45+
46+ private static final String NEW_JOB_NAME = "pk_jenkins_new__test_job" ;
4747
4848 private String ethalonJobXML = readResource (this .getClass (), TEST_JOB_XML_FN );
4949
@@ -63,6 +63,13 @@ public void setUp() {
6363 public void tearDown () {
6464 api .deleteJob (TEST_JOB_NAME );
6565 }
66+
67+ @ Test
68+ public void testGetJobDetailed () {
69+ JobDetailed job = api .getJobDetailed (TEST_JOB_NAME );
70+ assertTrue (job != null );
71+ assertTrue (job .getDisplayName ().equals (TEST_JOB_NAME ));
72+ }
6673
6774 @ Test
6875 public void testCreateJob () throws Exception {
@@ -72,26 +79,48 @@ public void testCreateJob() throws Exception {
7279 }
7380
7481 @ Test
75- public void testRunJob () throws ParserConfigurationException , SAXException , IOException {
76- Long jobId = api .runJob (TEST_JOB_NAME );
82+ public void testEnqueueBuild () throws Exception {
83+ Long jobId = api .enqueueBuild (TEST_JOB_NAME );
7784 assertTrue (jobId > 0 );
78-
7985 }
80-
86+
8187
8288 public void JenkinsAPITest () throws ParserConfigurationException , SAXException , IOException {
8389 String config = api .getJobConfigXml ("test job" );
8490 api .createJob ("newe4job" , config );
8591 }
8692
93+
8794 @ Test
8895 public void testChangeJob () throws ParserConfigurationException , SAXException , IOException {
89- api .getJobDetailed (jobName )
90- api .up
9196 Document doc = getJobDocument (TEST_JOB_NAME );
9297 NodeList nodes = doc .getElementsByTagName ("disabled" );
93- nodes .item (0 ).setNodeValue ("true" );
94- updateJob (jobName , doc );
98+ nodes .item (0 ).setTextContent ("true" );
99+ updateJob (TEST_JOB_NAME , doc );
100+
101+ doc = getJobDocument (TEST_JOB_NAME );
102+ nodes = doc .getElementsByTagName ("disabled" );
103+ assertEquals (nodes .item (0 ).getTextContent (), "true" );
104+ }
105+
106+ @ Test
107+ public void testCopyJob () {
108+ api .copyJob (TEST_JOB_NAME , NEW_JOB_NAME );
109+ try {
110+ assertTrue (api .getJobsList ().contains (NEW_JOB_NAME ));
111+ } finally {
112+ api .deleteJob (NEW_JOB_NAME );
113+ }
114+ }
115+
116+ @ Test
117+ public void testDeleteJob () {
118+ api .deleteJob (TEST_JOB_NAME );
119+ try {
120+ assertFalse (api .getJobsList ().contains (TEST_JOB_NAME ));
121+ } finally {
122+ api .createJob (TEST_JOB_NAME , ethalonJobXML );
123+ }
95124 }
96125
97126 private void updateJob (String jobName , Document doc ) throws IOException {
@@ -108,12 +137,8 @@ private Document getJobDocument(String jobName) throws ParserConfigurationExcept
108137 String xml = api .getJobConfigXml (jobName );
109138 DocumentBuilderFactory dbf = DocumentBuilderFactory .newInstance ();
110139 DocumentBuilder db = dbf .newDocumentBuilder ();
111-
112- StringReader sr = new StringReader (xml );
113- InputSource is = new InputSource (sr );
114- is .setEncoding ("UTF-8" );
115-
116- Document doc = db .parse (is );
140+ ByteArrayInputStream input = new ByteArrayInputStream (xml .getBytes ("UTF-8" ));
141+ Document doc = db .parse (input );
117142 return doc ;
118143 }
119144
0 commit comments