forked from microsoftgraph/msgraph-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserTests.java
More file actions
292 lines (268 loc) · 10.5 KB
/
Copy pathUserTests.java
File metadata and controls
292 lines (268 loc) · 10.5 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package com.microsoft.graph.functional;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import okhttp3.Request;
import com.microsoft.graph.http.BaseCollectionPage;
import com.microsoft.graph.http.HttpMethod;
import com.microsoft.graph.models.Contact;
import com.microsoft.graph.models.DirectoryObject;
import com.microsoft.graph.models.Drive;
import com.microsoft.graph.models.DriveItem;
import com.microsoft.graph.models.Group;
import com.microsoft.graph.models.MailFolder;
import com.microsoft.graph.models.Message;
import com.microsoft.graph.models.Organization;
import com.microsoft.graph.requests.GraphServiceClient;
import com.microsoft.graph.models.ProfilePhoto;
import com.microsoft.graph.models.UsedInsight;
import com.microsoft.graph.models.User;
import com.microsoft.graph.options.HeaderOption;
import com.microsoft.graph.options.Option;
import com.microsoft.graph.requests.ContactCollectionPage;
import com.microsoft.graph.requests.DirectoryObjectCollectionWithReferencesPage;
import com.microsoft.graph.requests.DriveItemCollectionPage;
import com.microsoft.graph.requests.GroupCollectionPage;
import com.microsoft.graph.requests.MailFolderCollectionPage;
import com.microsoft.graph.requests.MessageCollectionPage;
import com.microsoft.graph.requests.OrganizationCollectionPage;
import com.microsoft.graph.requests.UsedInsightCollectionPage;
import com.microsoft.graph.requests.UserCollectionPage;
@Disabled
public class UserTests {
GraphServiceClient graphServiceClient = null;
@BeforeEach
public void setUp() {
TestBase testBase = new TestBase();
graphServiceClient = testBase.graphClient;
}
@Test
public void getMeTest() {
//GET me
User user = graphServiceClient.me().buildRequest().get();
assertNotNull(user);
assertNotNull(user.displayName);
}
@Test
public void getMePhoto() {
//GET me/photo/$value
User user = graphServiceClient.me().buildRequest().get();
assertNotNull(user);
if(user.photo != null) {
InputStream stream = graphServiceClient.me().photo().content().buildRequest().get();
assertNotNull(stream);
}
}
@Test
public void meDriveTest() {
//GET me/drive/root/children
final DriveItemCollectionPage driveItemCollectionPage = graphServiceClient.me().drive().root().children().buildRequest().get();
assertNotNull(driveItemCollectionPage);
}
@Test
public void userKeyTest() {
//GET users('<<key>>')
final UserCollectionPage userCollectionPage = graphServiceClient.users().buildRequest().get();
assertNotNull(userCollectionPage);
assertNotNull(userCollectionPage.additionalDataManager().get("graphResponseHeaders"));
final List<User> list = userCollectionPage.getCurrentPage();
if(list.size() > 0) {
final User user = graphServiceClient.users(list.get(0).id).buildRequest().get();
assertNotNull(user);
}
}
@Test
public void meDriveRoot() {
//GET me/drive/root
final DriveItem driveItem = graphServiceClient.me().drive().root().buildRequest().get();
assertNotNull(driveItem);
}
@Test
public void meDrive() {
//GET me/drive
final Drive drive = graphServiceClient.me().drive().buildRequest().get();
assertNotNull(drive);
}
@Test
public void meDriveItems() {
//GET me/drive/items('<key>')
final DriveItemCollectionPage driveItemCollectionPage = graphServiceClient.me().drive().items().buildRequest().get();
assertNotNull(driveItemCollectionPage);
if(driveItemCollectionPage.getCurrentPage().size() > 0) {
DriveItem item = graphServiceClient.me().drive().items(driveItemCollectionPage.getCurrentPage().get(0).id).buildRequest().get();
assertNotNull(item);
}
}
@Test
public void meMessagesTest() {
//GET me/messages
final MessageCollectionPage messageCollectionPage = graphServiceClient.me().messages().buildRequest().get();
assertNotNull(messageCollectionPage);
}
@Test
public void meContactsTest() {
//GET me/contacts
final ContactCollectionPage contactCollectionPage = graphServiceClient.me().contacts().buildRequest().get();
assertNotNull(contactCollectionPage);
}
@Test
public void usersKeyPhotoValueTest() {
//GET users('<<key>>')/photo/$value
final UserCollectionPage userCollectionPage = graphServiceClient.users().buildRequest().get();
for(User user:userCollectionPage.getCurrentPage()) {
if(user.photo!=null) {
InputStream stream = graphServiceClient.users(userCollectionPage.getCurrentPage().get(0).id).photo().content().buildRequest().get();
assertNotNull(stream);
break;
}
}
}
@Test
public void updateUserPhotoValueTest() throws Exception {
final File photo = new File("src/test/resources/hamilton.jpg");
final InputStream fileStream = new FileInputStream(photo);
graphServiceClient.me().photo().content().buildRequest().put(OutlookTests.getByteArray(fileStream));
}
@Test
public void getOrganization() {
//GET organization
final OrganizationCollectionPage organizationCollectionPage = graphServiceClient.organization().buildRequest().get();
assertNotNull(organizationCollectionPage);
}
@Test
public void meInsightsUsed() {
//GET me/insights/used
final UsedInsightCollectionPage usedInsightCollectionPage = graphServiceClient.me().insights().used().buildRequest().get();
assertNotNull(usedInsightCollectionPage);
}
@Test
public void mailFoldertest() {
//GET me/mailFolders
final MailFolderCollectionPage mailFolderCollectionPage = graphServiceClient.me().mailFolders().buildRequest().get();
assertNotNull(mailFolderCollectionPage);
if(mailFolderCollectionPage.getCurrentPage().size() > 0) {
String mailFolderId = mailFolderCollectionPage.getCurrentPage().get(0).id;
final MessageCollectionPage messageCollectionPage = graphServiceClient.me().mailFolders(mailFolderId).messages().buildRequest().get();
assertNotNull(messageCollectionPage);
}
}
@Test
public void meMemberof() {
final DirectoryObjectCollectionWithReferencesPage page = graphServiceClient.me().memberOf().buildRequest().get();
assertNotNull(page);
}
@Test
public void getMeAndRetryOnThrottling() throws Exception {
ExecutorService exec = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);
try {
for(Integer i = 0; i < 2000; i++) {
exec.submit(new Runnable() {
@Override
public void run() {
final UserCollectionPage users = graphServiceClient.users().buildRequest().get();
assertNotNull(users);
final List<User> currentPage = users.getCurrentPage();
assertNotNull(currentPage);
assertNotEquals(0, currentPage.size());
}
});
}
exec.awaitTermination(5L, TimeUnit.MINUTES);
} finally {
exec.shutdown();
}
}
@Test
public void emptyPostContentTypeIsNotReset() {
final String contentTypeValue = "application/json";
final HeaderOption ctype = new HeaderOption("Content-Type", contentTypeValue);
final ArrayList<Option> options = new ArrayList<>();
options.add(ctype);
final Request request = graphServiceClient.me()
.revokeSignInSessions()
.buildRequest(options)
.withHttpMethod(HttpMethod.POST)
.getHttpRequest();
assertEquals(contentTypeValue, request.body().contentType().toString());
}
@Test
public void emptyPostContentTypeIsNotSet() {
final Request request = graphServiceClient.me()
.revokeSignInSessions()
.buildRequest()
.withHttpMethod(HttpMethod.POST)
.getHttpRequest();
assertNull(request.body().contentType());
}
@Test
public void castTest() {
final GroupCollectionPage groups = graphServiceClient.groups().buildRequest().top(1).get();
final Group group = groups.getCurrentPage().get(0);
final UserCollectionPage usersPage = graphServiceClient
.groups(group.id)
.membersAsUser()
.buildRequest()
.get();
assertNotNull(usersPage);
final DirectoryObjectCollectionWithReferencesPage testUserCollection = graphServiceClient
.groups(group.id)
.members()
.buildRequest()
.top(1)
.get();
final DirectoryObject testUser = testUserCollection.getCurrentPage().get(0);
final User user = graphServiceClient
.groups(group.id)
.membersAsUser(testUser.id)
.buildRequest()
.get();
assertNotNull(user);
}
@Test
public void getMeTransitiveReferences() {
DirectoryObjectCollectionWithReferencesPage page = graphServiceClient.me().transitiveMemberOf().references().buildRequest().get();
assertNotNull(page);
}
@Test
public void setMyBoss() {
final User me = graphServiceClient.me().buildRequest().select("id").get();
UserCollectionPage potentialManagers = graphServiceClient.users().buildRequest().top(1).get();
User manager = potentialManagers.getCurrentPage().get(0);
while(manager.id.equals(me.id) && potentialManagers.getNextPage() != null) {
potentialManagers = potentialManagers.getNextPage().buildRequest().get();
manager = potentialManagers.getCurrentPage().get(0);
}
if(!manager.id.equals(me.id)) {
graphServiceClient.me().manager().reference().buildRequest().put(manager);
assertEquals(true, true);
} else { // we don't have enough users on the tenant to run the test
assertEquals(true, false);
}
}
@Test
public void getUsersRawCount() {
final List<Option> consistencyLevelOptions = Arrays.asList(new HeaderOption("ConsistencyLevel", "eventual"));
final Long usersCount = graphServiceClient.users().count().buildRequest(consistencyLevelOptions).get();
assertNotNull(usersCount);
}
@Test
public void getUsersWithCount() {
final List<Option> consistencyLevelOptions = Arrays.asList(new HeaderOption("ConsistencyLevel", "eventual"));
final UserCollectionPage usersWithCount = graphServiceClient.users().buildRequest(consistencyLevelOptions).count().get();
assertNotNull(usersWithCount);
assertNotNull(usersWithCount.getCount());
}
}