-
Notifications
You must be signed in to change notification settings - Fork 125
/
JenkinsRuleTest.java
157 lines (124 loc) · 5.02 KB
/
JenkinsRuleTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package org.jvnet.hudson.test;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebRequest;
import hudson.model.User;
import jenkins.security.ApiTokenProperty;
import net.sf.json.JSONObject;
import org.junit.Rule;
import org.junit.Test;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.xml.sax.SAXException;
import java.io.IOException;
import java.net.URL;
import java.util.Collections;
import java.util.List;
import static org.junit.Assert.assertEquals;
public class JenkinsRuleTest {
@Rule public JenkinsRule j = new JenkinsRule();
@Test
public void assertEqualDataBoundBeansForNullLists() throws Exception {
j.assertEqualDataBoundBeans(new SomeClass(null), new SomeClass(null));
}
@Test(expected = AssertionError.class)
public void givenOneNullListAndOneNonnullListAssertShouldFail() throws Exception {
j.assertEqualDataBoundBeans(new SomeClass(Collections.emptyList()), new SomeClass(null));
}
public static class SomeClass {
private List<String> someList;
@DataBoundConstructor
public SomeClass(List<String> someList) {
this.someList = someList;
}
public List<String> getSomeList() {
return someList;
}
}
@Test
public void assertEqualDataBoundBeansWithSetters() throws Exception {
SomeClassWithSetters l = new SomeClassWithSetters("value1");
l.setSetterParam("value2");
l.setterField = "value3";
SomeClassWithSetters r = new SomeClassWithSetters("value1");
r.setSetterParam("value2");
r.setterField = "value3";
j.assertEqualDataBoundBeans(l, r);
}
@Test(expected = AssertionError.class)
public void assertEqualDataBoundBeansWithSettersFail() throws Exception {
SomeClassWithSetters l = new SomeClassWithSetters("value1");
l.setSetterParam("value2");
l.setterField = "value4";
SomeClassWithSetters r = new SomeClassWithSetters("value1");
r.setSetterParam("value3"); // mismatch!
r.setterField = "value4";
j.assertEqualDataBoundBeans(l, r);
}
@Test(expected = AssertionError.class)
public void assertEqualDataBoundBeansWithSettersFailForField() throws Exception {
SomeClassWithSetters l = new SomeClassWithSetters("value1");
l.setSetterParam("value2");
l.setterField = "value3";
SomeClassWithSetters r = new SomeClassWithSetters("value1");
r.setSetterParam("value2"); // mismatch!
l.setterField = "value4";
j.assertEqualDataBoundBeans(l, r);
}
@Test
public void testTokenHelperMethods() throws Exception {
j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
JenkinsRule.WebClient wc = j.createWebClient();
User alice = User.getById("alice", true);
User.getById("bob", true);
User.getById("charlotte", true);
makeRequestAndAssertLogin(wc, "anonymous");
wc.login("alice");
makeRequestAndAssertLogin(wc, "alice");
wc = j.createWebClient();
makeRequestAndAssertLogin(wc, "anonymous");
wc.withBasicCredentials("alice", "alice");
makeRequestAndAssertLogin(wc, "alice");
wc = j.createWebClient();
// if password is not provided, the login is used for both
wc.withBasicCredentials("alice");
makeRequestAndAssertLogin(wc, "alice");
wc = j.createWebClient();
makeRequestAndAssertLogin(wc, "anonymous");
wc.withBasicCredentials("alice", alice.getProperty(ApiTokenProperty.class).getApiToken());
makeRequestAndAssertLogin(wc, "alice");
wc = j.createWebClient();
makeRequestAndAssertLogin(wc, "anonymous");
wc.withBasicApiToken("bob");
makeRequestAndAssertLogin(wc, "bob");
wc = j.createWebClient();
wc.withBasicApiToken("charlotte");
makeRequestAndAssertLogin(wc, "charlotte");
}
private void makeRequestAndAssertLogin(JenkinsRule.WebClient wc, String expectedLogin) throws IOException, SAXException {
WebRequest req = new WebRequest(new URL(j.getURL(),"whoAmI/api/json"));
Page p = wc.getPage(req);
String pageContent = p.getWebResponse().getContentAsString();
String loginReceived = (String) JSONObject.fromObject(pageContent).get("name");
assertEquals(expectedLogin, loginReceived.trim());
}
public static class SomeClassWithSetters {
private String ctorParam;
private String setterParam;
@DataBoundSetter
public String setterField;
@DataBoundConstructor
public SomeClassWithSetters(String ctorParam) {
this.ctorParam = ctorParam;
}
public String getCtorParam() {
return ctorParam;
}
@DataBoundSetter
public void setSetterParam(String setterParam) {
this.setterParam = setterParam;
}
public String getSetterParam() {
return setterParam;
}
}
}