forked from microsoftgraph/msgraph-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBatchTests.java
More file actions
77 lines (65 loc) · 3.24 KB
/
Copy pathBatchTests.java
File metadata and controls
77 lines (65 loc) · 3.24 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
package com.microsoft.graph.functional;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.IOException;
import com.microsoft.graph.content.MSBatchRequestContent;
import com.microsoft.graph.content.MSBatchResponseContent;
import com.microsoft.graph.http.HttpMethod;
import com.microsoft.graph.httpcore.HttpClients;
import com.microsoft.graph.httpcore.ICoreAuthenticationProvider;
import com.microsoft.graph.models.extensions.IGraphServiceClient;
import com.microsoft.graph.models.extensions.User;
import com.microsoft.graph.requests.extensions.UserRequest;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
@Ignore
public class BatchTests {
IGraphServiceClient graphServiceClient = null;
@Before
public void setUp() {
final TestBase testBase = new TestBase();
graphServiceClient = testBase.graphClient;
}
@Test
public void GetsABatchFromRequests() throws IOException{
final MSBatchRequestContent batchContent = new MSBatchRequestContent();
final String meGetId = batchContent.addBatchRequestStep(graphServiceClient.me()
.buildRequest()
.withHttpMethod(HttpMethod.GET)
.getHttpRequest());
assertNotNull(meGetId);
final String usersGetId = batchContent.addBatchRequestStep(graphServiceClient.users()
.buildRequest()
.filter("accountEnabled eq true")
.expand("manager")
.top(5)
.withHttpMethod(HttpMethod.GET)
.getHttpRequest(),
meGetId);
final User userToAdd = new User();
userToAdd.givenName = "Darrel";
final String userPostId = batchContent.addBatchRequestStep(graphServiceClient.users()
.buildRequest()
.withHttpMethod(HttpMethod.POST)
.getHttpRequest(userToAdd), usersGetId);
final String serializedBatchContent = batchContent.getBatchRequestContent();
final Request batchRequest = new Request.Builder()
.url("https://graph.microsoft.com/v1.0/$batch")
.post(RequestBody.create(MediaType.parse("application/json"), serializedBatchContent))
.build();
final OkHttpClient client = HttpClients.createDefault((ICoreAuthenticationProvider)graphServiceClient.getAuthenticationProvider());
final Response batchResponse = client.newCall(batchRequest).execute();
assertEquals(200, batchResponse.code());
final MSBatchResponseContent responseContent = new MSBatchResponseContent(batchResponse);
batchResponse.close();
assertEquals(400, responseContent.getResponseById(userPostId).code()); //400:we're not providing enough properties for the call to go through
assertEquals(200, responseContent.getResponseById(meGetId).code());
assertEquals(200, responseContent.getResponseById(usersGetId).code());
}
}