Search in sources :

Example 51 with DefaultSerializer

use of com.microsoft.graph.serializer.DefaultSerializer in project msgraph-sdk-java-core by microsoftgraph.

the class BatchRequestContentTest method testGetBatchRequestContentWithHeader.

@Test
void testGetBatchRequestContentWithHeader() throws MalformedURLException {
    IHttpRequest requestStep = mock(IHttpRequest.class);
    when(requestStep.getRequestUrl()).thenReturn(new URL(testurl));
    when(requestStep.getHeaders()).thenReturn(Arrays.asList(new HeaderOption("testkey", "testvalue")));
    BatchRequestContent requestContent = new BatchRequestContent();
    String stepId = requestContent.addBatchRequestStep(requestStep);
    String content = new DefaultSerializer(mock(ILogger.class)).serializeObject(requestContent);
    String expectedContent = "{\"requests\":[{\"url\":\"/me\",\"method\":\"GET\",\"id\":\"" + stepId + "\",\"headers\":{\"testkey\":\"testvalue\"}}]}";
    assertEquals(expectedContent, content);
}
Also used : DefaultSerializer(com.microsoft.graph.serializer.DefaultSerializer) IHttpRequest(com.microsoft.graph.http.IHttpRequest) URL(java.net.URL) HeaderOption(com.microsoft.graph.options.HeaderOption) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 52 with DefaultSerializer

use of com.microsoft.graph.serializer.DefaultSerializer in project msgraph-sdk-java-core by microsoftgraph.

the class BatchResponseContentTest method deserializesErrorsProperly.

@Test
public void deserializesErrorsProperly() {
    String responsebody = "{\"responses\":[{\"id\":\"1\",\"status\":400,\"headers\":{\"Cache-Control\":\"no-cache\",\"x-ms-resource-unit\":\"1\",\"Content-Type\":\"application/json\"},\"body\":{\"error\":{\"code\":\"Request_BadRequest\",\"message\":\"Avalueisrequiredforproperty'displayName'ofresource'User'.\",\"innerError\":{\"date\":\"2021-02-02T19:19:38\",\"request-id\":\"408b8e64-4047-4c97-95b6-46e9f212ab48\",\"client-request-id\":\"102910da-260c-3028-0fb3-7d6903a02622\"}}}}]}";
    ISerializer serializer = new DefaultSerializer(mock(ILogger.class));
    BatchResponseContent batchresponse = serializer.deserializeObject(responsebody, BatchResponseContent.class);
    if (batchresponse != null) {
        if (// this is done by the batch request in the fluent API
        batchresponse.responses != null)
            for (final BatchResponseStep<?> step : batchresponse.responses) {
                step.serializer = serializer;
            }
        assertThrows(GraphServiceException.class, () -> {
            batchresponse.getResponseById("1").getDeserializedBody(BatchRequestContent.class);
        });
        try {
            batchresponse.getResponseById("1").getDeserializedBody(BatchRequestContent.class);
        } catch (GraphServiceException ex) {
            final GraphErrorResponse response = ex.getError();
            assertNotNull(response);
            assertNotNull(response.error);
            assertNotNull(response.error.message);
        }
    } else
        fail("batch response was null");
}
Also used : DefaultSerializer(com.microsoft.graph.serializer.DefaultSerializer) GraphErrorResponse(com.microsoft.graph.http.GraphErrorResponse) GraphServiceException(com.microsoft.graph.http.GraphServiceException) ILogger(com.microsoft.graph.logger.ILogger) ISerializer(com.microsoft.graph.serializer.ISerializer) Test(org.junit.jupiter.api.Test)

Example 53 with DefaultSerializer

use of com.microsoft.graph.serializer.DefaultSerializer in project msgraph-sdk-java-core by microsoftgraph.

the class BatchResponseContentTest method testGetBatchResponseContentMapResponse.

@Test
public void testGetBatchResponseContentMapResponse() throws IOException {
    String responsebody = "{\"responses\": [{\"id\": \"1\",\"status\":200,\"headers\" : {\"Cache-Control\":\"no-cache\",\"OData-Version\":\"4.0\",\"Content-Type\":\"application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8\"},\"body\":{\"@odata.context\":\"https://graph.microsoft.com/v1.0/$metadata#users/$entity\",\"businessPhones\":[\"8006427676\"],\"displayName\":\"MOD Administrator\",\"givenName\":\"MOD\",\"jobTitle\":null,\"mail\":\"admin@M365x751487.OnMicrosoft.com\",\"mobilePhone\":\"425-882-1032\",\"officeLocation\":null,\"preferredLanguage\":\"en-US\",\"surname\":\"Administrator\",\"userPrincipalName\":\"admin@M365x751487.onmicrosoft.com\",\"id\":\"6b4fa8ea-7e6e-486e-a8f4-d00a5b23488c\"}},{\"id\": \"2\",\"status\":200,\"headers\" : {\"Cache-Control\":\"no-store, no-cache\",\"OData-Version\":\"4.0\",\"Content-Type\":\"application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8\"},\"body\":{\"@odata.context\":\"https://graph.microsoft.com/v1.0/$metadata#drives/$entity\",\"createdDateTime\":\"2019-01-12T09:05:38Z\",\"description\":\"\",\"id\":\"b!nlu9o5I9g0y8gsHXfUM_bPTZ0oM_wVNArHM5R4-VkHLlnxx5SpqHRJledwfICP9f\",\"lastModifiedDateTime\":\"2019-03-06T06:59:04Z\",\"name\":\"OneDrive\",\"webUrl\":\"https://m365x751487-my.sharepoint.com/personal/admin_m365x751487_onmicrosoft_com/Documents\",\"driveType\":\"business\",\"createdBy\":{\"user\":{\"displayName\":\"System Account\"}},\"lastModifiedBy\":{\"user\":{\"displayName\":\"System Account\"}},\"owner\":{\"user\":{\"email\":\"admin@M365x751487.OnMicrosoft.com\",\"id\":\"6b4fa8ea-7e6e-486e-a8f4-d00a5b23488c\",\"displayName\":\"MOD Administrator\"}},\"quota\":{\"deleted\":0,\"remaining\":1099509670098,\"state\":\"normal\",\"total\":1099511627776,\"used\":30324}}},{\"id\": \"3\",\"status\":201,\"headers\" : {\"Location\":\"https://graph.microsoft.com/v1.0/users/6b4fa8ea-7e6e-486e-a8f4-d00a5b23488c/onenote/notebooks/1-94e4376a-a1c1-441a-8b41-af5c86ee39d0\",\"Preference-Applied\":\"odata.include-annotations=*\",\"Cache-Control\":\"no-cache\",\"OData-Version\":\"4.0\",\"Content-Type\":\"application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8\"},\"body\":{\"@odata.context\":\"https://graph.microsoft.com/v1.0/$metadata#users('6b4fa8ea-7e6e-486e-a8f4-d00a5b23488c')/onenote/notebooks/$entity\",\"id\":\"1-94e4376a-a1c1-441a-8b41-af5c86ee39d0\",\"self\":\"https://graph.microsoft.com/v1.0/users/6b4fa8ea-7e6e-486e-a8f4-d00a5b23488c/onenote/notebooks/1-94e4376a-a1c1-441a-8b41-af5c86ee39d0\",\"createdDateTime\":\"2019-03-06T08:08:09Z\",\"displayName\":\"My Notebook -442293399\",\"lastModifiedDateTime\":\"2019-03-06T08:08:09Z\",\"isDefault\":false,\"userRole\":\"Owner\",\"isShared\":false,\"sectionsUrl\":\"https://graph.microsoft.com/v1.0/users/6b4fa8ea-7e6e-486e-a8f4-d00a5b23488c/onenote/notebooks/1-94e4376a-a1c1-441a-8b41-af5c86ee39d0/sections\",\"sectionGroupsUrl\":\"https://graph.microsoft.com/v1.0/users/6b4fa8ea-7e6e-486e-a8f4-d00a5b23488c/onenote/notebooks/1-94e4376a-a1c1-441a-8b41-af5c86ee39d0/sectionGroups\",\"createdBy\":{\"user\":{\"id\":\"6b4fa8ea-7e6e-486e-a8f4-d00a5b23488c\",\"displayName\":\"MOD Administrator\"}},\"lastModifiedBy\":{\"user\":{\"id\":\"6b4fa8ea-7e6e-486e-a8f4-d00a5b23488c\",\"displayName\":\"MOD Administrator\"}},\"links\":{\"oneNoteClientUrl\":{\"href\":\"onenote:https://m365x751487-my.sharepoint.com/personal/admin_m365x751487_onmicrosoft_com/Documents/Notebooks/My%20Notebook%20-442293399\"},\"oneNoteWebUrl\":{\"href\":\"https://m365x751487-my.sharepoint.com/personal/admin_m365x751487_onmicrosoft_com/Documents/Notebooks/My%20Notebook%20-442293399\"}}}}]}";
    BatchResponseContent batchresponse = new DefaultSerializer(mock(ILogger.class)).deserializeObject(responsebody, BatchResponseContent.class);
    for (BatchResponseStep<?> entry : batchresponse.responses) {
        assertTrue(entry.id != null && entry.body != null);
    }
}
Also used : DefaultSerializer(com.microsoft.graph.serializer.DefaultSerializer) Test(org.junit.jupiter.api.Test)

Example 54 with DefaultSerializer

use of com.microsoft.graph.serializer.DefaultSerializer in project msgraph-sdk-java-core by microsoftgraph.

the class BatchResponseContentTest method testGetBatchResponseContentIteratorOverResponse.

@Test
public void testGetBatchResponseContentIteratorOverResponse() throws IOException {
    String responsebody = "{\"responses\": [{\"id\": \"1\",\"status\":200,\"headers\" : {\"Cache-Control\":\"no-cache\",\"OData-Version\":\"4.0\",\"Content-Type\":\"application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8\"},\"body\":{\"@odata.context\":\"https://graph.microsoft.com/v1.0/$metadata#users/$entity\",\"businessPhones\":[\"8006427676\"],\"displayName\":\"MOD Administrator\",\"givenName\":\"MOD\",\"jobTitle\":null,\"mail\":\"admin@M365x751487.OnMicrosoft.com\",\"mobilePhone\":\"425-882-1032\",\"officeLocation\":null,\"preferredLanguage\":\"en-US\",\"surname\":\"Administrator\",\"userPrincipalName\":\"admin@M365x751487.onmicrosoft.com\",\"id\":\"6b4fa8ea-7e6e-486e-a8f4-d00a5b23488c\"}},{\"id\": \"2\",\"status\":200,\"headers\" : {\"Cache-Control\":\"no-store, no-cache\",\"OData-Version\":\"4.0\",\"Content-Type\":\"application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8\"},\"body\":{\"@odata.context\":\"https://graph.microsoft.com/v1.0/$metadata#drives/$entity\",\"createdDateTime\":\"2019-01-12T09:05:38Z\",\"description\":\"\",\"id\":\"b!nlu9o5I9g0y8gsHXfUM_bPTZ0oM_wVNArHM5R4-VkHLlnxx5SpqHRJledwfICP9f\",\"lastModifiedDateTime\":\"2019-03-06T06:59:04Z\",\"name\":\"OneDrive\",\"webUrl\":\"https://m365x751487-my.sharepoint.com/personal/admin_m365x751487_onmicrosoft_com/Documents\",\"driveType\":\"business\",\"createdBy\":{\"user\":{\"displayName\":\"System Account\"}},\"lastModifiedBy\":{\"user\":{\"displayName\":\"System Account\"}},\"owner\":{\"user\":{\"email\":\"admin@M365x751487.OnMicrosoft.com\",\"id\":\"6b4fa8ea-7e6e-486e-a8f4-d00a5b23488c\",\"displayName\":\"MOD Administrator\"}},\"quota\":{\"deleted\":0,\"remaining\":1099509670098,\"state\":\"normal\",\"total\":1099511627776,\"used\":30324}}},{\"id\": \"3\",\"status\":201,\"headers\" : {\"Location\":\"https://graph.microsoft.com/v1.0/users/6b4fa8ea-7e6e-486e-a8f4-d00a5b23488c/onenote/notebooks/1-94e4376a-a1c1-441a-8b41-af5c86ee39d0\",\"Preference-Applied\":\"odata.include-annotations=*\",\"Cache-Control\":\"no-cache\",\"OData-Version\":\"4.0\",\"Content-Type\":\"application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8\"},\"body\":{\"@odata.context\":\"https://graph.microsoft.com/v1.0/$metadata#users('6b4fa8ea-7e6e-486e-a8f4-d00a5b23488c')/onenote/notebooks/$entity\",\"id\":\"1-94e4376a-a1c1-441a-8b41-af5c86ee39d0\",\"self\":\"https://graph.microsoft.com/v1.0/users/6b4fa8ea-7e6e-486e-a8f4-d00a5b23488c/onenote/notebooks/1-94e4376a-a1c1-441a-8b41-af5c86ee39d0\",\"createdDateTime\":\"2019-03-06T08:08:09Z\",\"displayName\":\"My Notebook -442293399\",\"lastModifiedDateTime\":\"2019-03-06T08:08:09Z\",\"isDefault\":false,\"userRole\":\"Owner\",\"isShared\":false,\"sectionsUrl\":\"https://graph.microsoft.com/v1.0/users/6b4fa8ea-7e6e-486e-a8f4-d00a5b23488c/onenote/notebooks/1-94e4376a-a1c1-441a-8b41-af5c86ee39d0/sections\",\"sectionGroupsUrl\":\"https://graph.microsoft.com/v1.0/users/6b4fa8ea-7e6e-486e-a8f4-d00a5b23488c/onenote/notebooks/1-94e4376a-a1c1-441a-8b41-af5c86ee39d0/sectionGroups\",\"createdBy\":{\"user\":{\"id\":\"6b4fa8ea-7e6e-486e-a8f4-d00a5b23488c\",\"displayName\":\"MOD Administrator\"}},\"lastModifiedBy\":{\"user\":{\"id\":\"6b4fa8ea-7e6e-486e-a8f4-d00a5b23488c\",\"displayName\":\"MOD Administrator\"}},\"links\":{\"oneNoteClientUrl\":{\"href\":\"onenote:https://m365x751487-my.sharepoint.com/personal/admin_m365x751487_onmicrosoft_com/Documents/Notebooks/My%20Notebook%20-442293399\"},\"oneNoteWebUrl\":{\"href\":\"https://m365x751487-my.sharepoint.com/personal/admin_m365x751487_onmicrosoft_com/Documents/Notebooks/My%20Notebook%20-442293399\"}}}}]}";
    BatchResponseContent batchresponse = new DefaultSerializer(mock(ILogger.class)).deserializeObject(responsebody, BatchResponseContent.class);
    Iterator<BatchResponseStep<JsonElement>> it = batchresponse.responses.iterator();
    while (it.hasNext()) {
        BatchResponseStep<?> entry = it.next();
        assertTrue(entry.id != null && entry.body != null);
    }
}
Also used : DefaultSerializer(com.microsoft.graph.serializer.DefaultSerializer) Test(org.junit.jupiter.api.Test)

Example 55 with DefaultSerializer

use of com.microsoft.graph.serializer.DefaultSerializer in project msgraph-sdk-java-core by microsoftgraph.

the class BatchResponseContentTest method testInvalidBatchResponseContentWithEmptyResponse.

@Test
public void testInvalidBatchResponseContentWithEmptyResponse() {
    String responsebody = "{\"responses\": [] }";
    BatchResponseContent batchresponse = new DefaultSerializer(mock(ILogger.class)).deserializeObject(responsebody, BatchResponseContent.class);
    assertTrue(batchresponse.getResponseById("1") == null);
}
Also used : DefaultSerializer(com.microsoft.graph.serializer.DefaultSerializer) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultSerializer (com.microsoft.graph.serializer.DefaultSerializer)59 Test (org.junit.jupiter.api.Test)54 DefaultLogger (com.microsoft.graph.logger.DefaultLogger)32 IHttpRequest (com.microsoft.graph.http.IHttpRequest)6 URL (java.net.URL)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 ILogger (com.microsoft.graph.logger.ILogger)5 JsonElement (com.google.gson.JsonElement)4 MediaStream (com.microsoft.graph.callrecords.models.MediaStream)4 GraphErrorResponse (com.microsoft.graph.http.GraphErrorResponse)4 PlannerChecklistItem (com.microsoft.graph.models.PlannerChecklistItem)4 PlannerTaskDetails (com.microsoft.graph.models.PlannerTaskDetails)4 RecurrenceRange (com.microsoft.graph.models.RecurrenceRange)4 User (com.microsoft.graph.models.User)4 ISerializer (com.microsoft.graph.serializer.ISerializer)4 GraphServiceException (com.microsoft.graph.http.GraphServiceException)3 OkHttpClient (okhttp3.OkHttpClient)3 Request (okhttp3.Request)3 JsonObject (com.google.gson.JsonObject)2 DateOnly (com.microsoft.graph.core.DateOnly)2