This repository has been archived by the owner on Dec 12, 2018. It is now read-only.
/
CustomDataEntityIT.groovy
239 lines (157 loc) · 6.99 KB
/
CustomDataEntityIT.groovy
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
* Copyright 2013 Stormpath, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.stormpath.sdk.client
import com.stormpath.sdk.account.Account
import com.stormpath.sdk.application.Application
import com.stormpath.sdk.directory.CustomData
import org.testng.annotations.Test
import static org.testng.Assert.assertEquals
import static org.testng.Assert.assertNull
/**
* @since 0.9
*/
class CustomDataEntityIT extends AbstractCustomDataIT {
Application application
@Test
void testCustomDataOperations() {
application = createApplication()
def account = application.createAccount(newAccountData())
CustomData customData = account.customData
testCRUD(customData)
def group = application.createGroup(newGroupData())
customData = group.customData
testCRUD(customData)
deleteOnTeardown(account.directory)
deleteOnTeardown(group)
deleteOnTeardown(application)
}
private testCRUD(CustomData customData) {
//TEST CREATE
Map postedProperties = createComplexData()
customData.putAll(postedProperties)
customData.save()
assertValidCustomData(customData.getHref(), postedProperties, customData)
//TEST READ
def customDataHref = customData.getHref()
customData = client.getResource(customDataHref, CustomData)
assertValidCustomData(customData.getHref(), postedProperties, customData)
//TEST UPDATE
def updateCustomDataProperties = createDataForUpdate()
for (Map.Entry objectEntry : updateCustomDataProperties.entrySet()) {
Object object = updateCustomDataProperties.get(objectEntry.key)
customData.put(objectEntry.key, object)
}
customData.save()
postedProperties.putAll(updateCustomDataProperties)
assertValidCustomData(customDataHref, postedProperties, customData)
//TEST DELETE SINGLE PROPERTIES
def propertiesToDelete = updateCustomDataProperties.keySet()
for (String propertyName : propertiesToDelete) {
customData.remove(propertyName)
postedProperties.remove(propertyName)
}
customData.save()
//TEST DELETE SINGLE PROPERTIES & UPDATE OTHERS
customData = client.getResource(customDataHref, CustomData)
propertiesToDelete = createSetOfPropertiesToDelete();
for (String propertyName : propertiesToDelete) {
customData.remove(propertyName)
postedProperties.remove(propertyName)
}
assertValidCustomData(customData.getHref(), postedProperties, customData)
updateCustomDataProperties = createDataForUpdate()
for (Map.Entry objectEntry : updateCustomDataProperties.entrySet()) {
Object object = updateCustomDataProperties.get(objectEntry.key)
customData.put(objectEntry.key, object)
}
postedProperties.putAll(updateCustomDataProperties)
customData.save()
assertValidCustomData(customData.getHref(), postedProperties, customData)
//TEST DELETE ALL PROPERTIES
customData.clear()
postedProperties = ["uniqueString" : uniquify("my unique string")]
customData.putAll(postedProperties)
customData.save()
customData = client.getResource(customDataHref, CustomData)
assertValidCustomData(customData.getHref(), postedProperties, customData)
//TEST DELETE OBJECT.
customData.delete()
customData = client.getResource(customDataHref, CustomData)
assertValidCustomData(customData.getHref(), [:], customData)
}
/**
* Asserts fix for <a href="https://github.com/stormpath/stormpath-sdk-java/issues/30">Issue #30</a>
*/
@Test
void testCustomDataCacheUpdated() {
//create temp account:
def app = createTempApp()
def account = app.createAccount(newAccountData())
def href = account.href
//for the purposes of this test, we want a new client, using caching, completely separate from anything else:
Client client2 = buildClient(true);
//1. Get the account's custom data:
account = client2.getResource(href, Account)
def customData = account.getCustomData()
assertNull customData.hobby //no data yet
//2. Update the custom data value:
customData.hobby = 'Reading'
customData.save()
//3. Get the custom data again and assert that the value is there:
customData = client2.getResource(customData.getHref(), CustomData)
assertEquals customData.hobby, 'Reading'
//4. Get the account again and assert that the value is there:
account = client2.getResource(href, Account)
customData = account.customData
assertEquals customData.hobby, 'Reading'
//5. Change the value:
customData.hobby = 'Kendo'
customData.save()
//6. Get the custom data again and assert that the value is there:
customData = client2.getResource(customData.getHref(), CustomData)
assertEquals customData.hobby, 'Kendo'
//7. Get the account again and assert that the value is there:
account = client2.getResource(account.getHref(), Account)
assertEquals account.customData.hobby, 'Kendo'
}
/**
* Asserts fix for <a href="https://github.com/stormpath/stormpath-sdk-java/issues/30">Issue #30</a>
*/
@Test
void testCustomDataCacheUpdatedViaParentSave() {
//create temp account:
def app = createTempApp()
def account = app.createAccount(newAccountData())
def href = account.href
//for the purposes of this test, we want a new client, using caching, completely separate from anything else:
Client client2 = buildClient(true);
//1. Get the account
account = client2.getResource(href, Account)
assertNull account.getCustomData().hobby //no data yet
//2. Update the custom data value:
account.customData.hobby = 'Reading'
account.save()
//3. Get the account again and assert that the value is there:
account = client2.getResource(href, Account)
assertEquals account.customData.hobby, 'Reading'
//4. Change the value:
account.customData.hobby = 'Kendo'
account.save()
//5. Get the account again and assert that the value is there:
account = client2.getResource(href, Account)
assertEquals account.customData.hobby, 'Kendo'
}
}