This repository has been archived by the owner on Apr 14, 2020. It is now read-only.
/
PatchReplaceUserTest.java
201 lines (151 loc) · 7.75 KB
/
PatchReplaceUserTest.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
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
/*
* SCIM-Client is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
*
* Copyright (c) 2017, Gluu
*/
package gluu.scim2.client.patch;
import gluu.scim2.client.UserBaseTest;
import org.gluu.oxtrust.model.scim2.patch.PatchOperation;
import org.gluu.oxtrust.model.scim2.patch.PatchRequest;
import org.gluu.oxtrust.model.scim2.user.Address;
import org.gluu.oxtrust.model.scim2.user.InstantMessagingAddress;
import org.gluu.oxtrust.model.scim2.user.PhoneNumber;
import org.gluu.oxtrust.model.scim2.user.UserResource;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import javax.ws.rs.core.Response;
import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;
import static javax.ws.rs.core.Response.Status.*;
import static org.testng.Assert.*;
/**
* Note: these cases depend heavily on the contents and structure of files under /single/patch/
* Created by jgomer on 2017-11-02.
*/
public class PatchReplaceUserTest extends UserBaseTest {
private UserResource user;
@Parameters({"user_full_create"})
@Test
public void createForReplace(String json){
logger.debug("Creating user from json...");
user=createUserFromJson(json);
}
@Parameters({"user_patchreplace_1"})
@Test(dependsOnMethods = "createForReplace")
public void jsonNoPathPatch1(String patchRequest){
Response response = client.patchUser(patchRequest, user.getId(), null, null);
assertEquals(response.getStatus(), OK.getStatusCode());
UserResource other=response.readEntity(usrClass);
//Verify display name changed
assertNotEquals(user.getDisplayName(), other.getDisplayName());
//Verify some name components changed
assertNotEquals(user.getName().getFamilyName(), other.getName().getFamilyName());
assertNotEquals(user.getName().getMiddleName(), other.getName().getMiddleName());
assertNotEquals(user.getName().getGivenName(), other.getName().getGivenName());
assertEquals(user.getName().getHonorificPrefix(), other.getName().getHonorificPrefix());
assertEquals(user.getName().getHonorificSuffix(), other.getName().getHonorificSuffix());
//Verify is not active
assertFalse(other.getActive());
//Verify title changed
assertNotEquals(user.getTitle(), other.getTitle());
user=other;
}
@Parameters({"user_patchreplace_2"})
@Test(dependsOnMethods = "jsonNoPathPatch1")
public void jsonNoPathPatch2(String patchRequest){
Response response = client.patchUser(patchRequest, user.getId(), null, null);
assertEquals(response.getStatus(), OK.getStatusCode());
UserResource other=response.readEntity(usrClass);
assertNotEquals(user.getName().getHonorificPrefix(), other.getName().getHonorificPrefix());
assertNotEquals(user.getName().getHonorificSuffix(), other.getName().getHonorificSuffix());
assertNotEquals(user.getName().getFormatted(), other.getName().getFormatted());
//Verify change in the streetAddress
Address adr=other.getAddresses().get(0);
assertNotEquals(user.getAddresses().get(0).getStreetAddress(), adr.getStreetAddress());
//Verify other attributes were nulled
assertNull(adr.getPostalCode());
assertNull(adr.getRegion());
//Verify change in number of phone numbers
assertNotEquals(user.getPhoneNumbers().size(), other.getPhoneNumbers().size());
//Verify new user has different phone numbers
String phone=user.getPhoneNumbers().get(0).getValue();
assertTrue(other.getPhoneNumbers().stream().map(PhoneNumber::getValue).noneMatch(phone::equals));
//Verify x509Certs disappeared
assertNull(other.getX509Certificates());
//Verify "roles" are still there intact
assertEquals(user.getRoles().size(), other.getRoles().size());
assertEquals(user.getRoles().get(0).getValue(), other.getRoles().get(0).getValue());
user=other;
}
@Parameters({"user_patchreplace_3"})
@Test(dependsOnMethods = "jsonNoPathPatch2")
public void jsonPathPatch1(String patchRequest){
Response response = client.patchUser(patchRequest, user.getId(), null, null);
assertEquals(response.getStatus(), OK.getStatusCode());
UserResource other=response.readEntity(usrClass);
//Verify display name changed
assertNotEquals(user.getDisplayName(), other.getDisplayName());
//Verify some name components changed
assertNotEquals(user.getName().getFamilyName(), other.getName().getFamilyName());
assertNotEquals(user.getName().getMiddleName(), other.getName().getMiddleName());
assertNotEquals(user.getName().getGivenName(), other.getName().getGivenName());
assertEquals(user.getName().getHonorificPrefix(), other.getName().getHonorificPrefix());
assertEquals(user.getName().getHonorificSuffix(), other.getName().getHonorificSuffix());
//Verify is now active
assertTrue(other.getActive());
user=other;
}
@Parameters({"user_patchreplace_4"})
@Test(dependsOnMethods = "jsonPathPatch1")
public void jsonPathPatch2(String patchRequest){
Response response = client.patchUser(patchRequest, user.getId(), null, null);
assertEquals(response.getStatus(), OK.getStatusCode());
UserResource other=response.readEntity(usrClass);
assertNotEquals(user.getName().getHonorificPrefix(), other.getName().getHonorificPrefix());
assertNotEquals(user.getName().getHonorificSuffix(), other.getName().getHonorificSuffix());
assertNotEquals(user.getName().getFormatted(), other.getName().getFormatted());
//Verify change in the streetAddress
Address adr=other.getAddresses().get(0);
assertNotEquals(user.getAddresses().get(0).getStreetAddress(), adr.getStreetAddress());
//Verify postal code is there
assertNotNull(adr.getPostalCode());
//Verify change in phone number values
int num=user.getPhoneNumbers().size();
assertEquals(num, other.getPhoneNumbers().size());
Set<String> set1=user.getPhoneNumbers().stream().map(PhoneNumber::getValue).collect(Collectors.toSet());
Set<String> set2=other.getPhoneNumbers().stream().map(PhoneNumber::getValue).collect(Collectors.toSet());
assertFalse(set2.removeAll(set1));
//Verify x509Certs disappeared
assertNotNull(other.getX509Certificates());
user=other;
}
@Test(dependsOnMethods = "jsonPathPatch2")
public void objectPatch(){
//Create a patch request by supplying a singleton list with one IMS object
InstantMessagingAddress ims=new InstantMessagingAddress();
ims.setDisplay("barbas");
ims.setPrimary(true);
ims.setType("escape");
ims.setValue("bjensen");
PatchOperation op=new PatchOperation();
op.setOperation("replace");
op.setPath("ims");
op.setValue(Collections.singleton(ims));
PatchRequest pr=new PatchRequest();
pr.setOperations(Collections.singletonList(op));
Response response = client.patchUser(pr, user.getId(), null, null);
assertEquals(response.getStatus(), OK.getStatusCode());
UserResource other=response.readEntity(usrClass);
//Verify different info appeared
InstantMessagingAddress newIms=other.getIms().get(0);
assertTrue(newIms.getDisplay().equals(ims.getDisplay()));
assertTrue(newIms.getValue().equals(ims.getValue()));
assertTrue(newIms.getType().equals(ims.getType()));
assertTrue(newIms.getPrimary().equals(ims.getPrimary()));
}
@Test(dependsOnMethods = "objectPatch", alwaysRun = true)
public void delete(){
deleteUser(user);
}
}