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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
/*
* Copyright (c) 2014, 2015 Hewlett-Packard Development Company, L.P. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
package org.opendaylight.aaa;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.HashSet;
import org.junit.Test;
import org.opendaylight.aaa.api.Claim;
/**
*
* @author liemmn
*
*/
public class ClaimBuilderTest {
@Test
public void testBuildWithAll() {
Claim c1 = new ClaimBuilder().setClientId("dlux").setDomain("pepsi").setUser("john")
.setUserId("1234").addRole("foo").addRole("foo2")
.addRoles(new HashSet<>(Arrays.asList("foo", "bar"))).build();
assertEquals("dlux", c1.clientId());
assertEquals("pepsi", c1.domain());
assertEquals("john", c1.user());
assertEquals("1234", c1.userId());
assertTrue(c1.roles().contains("foo"));
assertTrue(c1.roles().contains("foo2"));
assertTrue(c1.roles().contains("bar"));
assertEquals(3, c1.roles().size());
Claim c2 = new ClaimBuilder(c1).build();
assertEquals(c1, c2);
}
@Test
public void testBuildWithRequired() {
Claim c1 = new ClaimBuilder().setUser("john").setUserId("1234").addRole("foo").build();
assertEquals("john", c1.user());
assertEquals("1234", c1.userId());
assertTrue(c1.roles().contains("foo"));
assertEquals(1, c1.roles().size());
assertEquals("", c1.domain());
assertEquals("", c1.clientId());
}
@Test
public void testBuildWithEmptyOptional() {
Claim c1 = new ClaimBuilder().setDomain(" ").setClientId(" ").setUser("john")
.setUserId("1234").addRole("foo").build();
assertEquals("", c1.domain());
assertEquals("", c1.clientId());
assertEquals("john", c1.user());
assertEquals("1234", c1.userId());
assertTrue(c1.roles().contains("foo"));
assertEquals(1, c1.roles().size());
}
@Test
public void testBuildWithNullOptional() {
Claim c1 = new ClaimBuilder().setDomain(null).setClientId(null).setUser("john")
.setUserId("1234").addRole("foo").build();
assertEquals("", c1.domain());
assertEquals("", c1.clientId());
assertEquals("john", c1.user());
assertEquals("1234", c1.userId());
assertTrue(c1.roles().contains("foo"));
assertEquals(1, c1.roles().size());
}
@Test(expected = IllegalStateException.class)
public void testBuildWithDefault() {
ClaimBuilder c1 = new ClaimBuilder();
c1.build();
}
@Test(expected = IllegalStateException.class)
public void testBuildWithoutUser() {
ClaimBuilder c1 = new ClaimBuilder().setUserId("1234").addRole("foo");
c1.build();
}
@Test(expected = IllegalStateException.class)
public void testBuildWithNullUser() {
ClaimBuilder c1 = new ClaimBuilder().setUser(null).setUserId("1234").addRole("foo");
c1.build();
}
@Test(expected = IllegalStateException.class)
public void testBuildWithEmptyUser() {
ClaimBuilder c1 = new ClaimBuilder().setUser(" ").setUserId("1234").addRole("foo");
c1.build();
}
@Test(expected = IllegalStateException.class)
public void testBuildWithoutUserId() {
ClaimBuilder c1 = new ClaimBuilder().setUser("john").addRole("foo");
c1.build();
}
@Test(expected = IllegalStateException.class)
public void testBuildWithNullUserId() {
ClaimBuilder c1 = new ClaimBuilder().setUser("john").setUserId(null).addRole("foo");
c1.build();
}
@Test(expected = IllegalStateException.class)
public void testBuildWithEmptyUserId() {
ClaimBuilder c1 = new ClaimBuilder().setUser("john").setUserId(" ").addRole("foo");
c1.build();
}
@Test(expected = IllegalStateException.class)
public void testBuildWithoutRole() {
ClaimBuilder c1 = new ClaimBuilder().setUser("john").setUserId("1234");
c1.build();
}
@Test(expected = IllegalStateException.class)
public void testBuildWithNullRole() {
ClaimBuilder c1 = new ClaimBuilder().setUser("john").setUserId("1234").addRole(null);
c1.build();
}
@Test(expected = IllegalStateException.class)
public void testBuildWithEmptyRole() {
ClaimBuilder c1 = new ClaimBuilder().setUser("john").setUserId("1234").addRole(" ");
c1.build();
}
@Test
public void testEquals() {
Claim c1 = new ClaimBuilder().setClientId("dlux").setDomain("pepsi").setUser("john")
.setUserId("1234").addRole("foo").build();
assertTrue(c1.equals(c1));
Claim c2 = new ClaimBuilder(c1).addRole("foo").build();
assertTrue(c1.equals(c2));
assertTrue(c2.equals(c1));
Claim c3 = new ClaimBuilder().setClientId("dlux").setDomain("pepsi").setUser("john")
.setUserId("1234").addRole("foo").build();
assertTrue(c1.equals(c3));
assertTrue(c3.equals(c2));
assertTrue(c1.equals(c2));
}
@Test
public void testNotEquals() {
Claim c1 = new ClaimBuilder().setClientId("dlux").setDomain("pepsi").setUser("john")
.setUserId("1234").addRole("foo").build();
assertFalse(c1.equals(null));
assertFalse(c1.equals("wrong object"));
Claim c2 = new ClaimBuilder(c1).addRoles(new HashSet<>(Arrays.asList("foo", "bar")))
.build();
assertEquals(2, c2.roles().size());
assertFalse(c1.equals(c2));
assertFalse(c2.equals(c1));
Claim c3 = new ClaimBuilder().setClientId("dlux").setDomain("pepsi").setUser("john")
.setUserId("1234").addRole("foo").build();
assertFalse(c1.equals(c2));
assertTrue(c1.equals(c3));
assertFalse(c2.equals(c3));
Claim c5 = new ClaimBuilder().setUser("john").setUserId("1234").addRole("foo").build();
assertFalse(c1.equals(c5));
assertFalse(c5.equals(c1));
}
@Test
public void testHash() {
Claim c1 = new ClaimBuilder().setClientId("dlux").setDomain("pepsi").setUser("john")
.setUserId("1234").addRole("foo").build();
assertEquals(c1.hashCode(), c1.hashCode());
Claim c2 = new ClaimBuilder(c1).addRole("foo").build();
assertTrue(c1.equals(c2));
assertEquals(c1.hashCode(), c2.hashCode());
Claim c3 = new ClaimBuilder(c1).addRoles(new HashSet<>(Arrays.asList("foo", "bar")))
.build();
assertFalse(c1.equals(c3));
assertNotEquals(c1.hashCode(), c3.hashCode());
Claim c4 = new ClaimBuilder().setClientId("dlux").setDomain("pepsi").setUser("john")
.setUserId("1234").addRole("foo").build();
assertTrue(c1.equals(c4));
assertEquals(c1.hashCode(), c4.hashCode());
assertEquals(c2.hashCode(), c4.hashCode());
Claim c5 = new ClaimBuilder().setUser("john").setUserId("1234").addRole("foo").build();
assertFalse(c1.equals(c5));
assertNotEquals(c1.hashCode(), c5.hashCode());
}
@Test
public void testToString() {
Claim c1 = new ClaimBuilder().setUser("john").setUserId("1234").addRole("foo").build();
assertEquals("clientId:,userId:1234,userName:john,domain:,roles:[foo]", c1.toString());
c1 = new ClaimBuilder(c1).setClientId("dlux").setDomain("pepsi").build();
assertEquals("clientId:dlux,userId:1234,userName:john,domain:pepsi,roles:[foo]",
c1.toString());
c1 = new ClaimBuilder(c1).addRole("bar").build();
assertEquals("clientId:dlux,userId:1234,userName:john,domain:pepsi,roles:[foo, bar]",
c1.toString());
}
}
|