Search in sources :

Example 11 with DefaultSerializer

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

the class BatchResponseContentTest method includeVerboseInformation.

@Test
public void includeVerboseInformation() {
    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(new DefaultLogger() {

        {
            setLoggingLevel(LoggerLevel.DEBUG);
        }
    });
    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;
            }
        try {
            batchresponse.getResponseById("1").getDeserializedBody(BatchRequestContent.class);
        } catch (GraphServiceException ex) {
            final GraphErrorResponse response = ex.getError();
            assertNotNull(response);
            assertNotNull(response.error);
            assertNotNull(response.error.message);
            assertEquals(ex.getMessage(true), ex.getMessage());
        }
    } 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) DefaultLogger(com.microsoft.graph.logger.DefaultLogger) ISerializer(com.microsoft.graph.serializer.ISerializer) Test(org.junit.jupiter.api.Test)

Example 12 with DefaultSerializer

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

the class BatchResponseContentTest method responseParsing.

@Test
public void responseParsing() {
    final BatchResponseContent batchResponse = new DefaultSerializer(mock(ILogger.class)).deserializeObject("{\"responses\": [{\"id\": \"1\",\"status\": 200,\"body\": null}]}", BatchResponseContent.class);
    assertEquals(1, batchResponse.responses.size());
    final BatchResponseStep<?> response = batchResponse.getResponseById("1");
    assertNotNull(response);
    assertNull(batchResponse.getResponseById("2"));
}
Also used : DefaultSerializer(com.microsoft.graph.serializer.DefaultSerializer) Test(org.junit.jupiter.api.Test)

Example 13 with DefaultSerializer

use of com.microsoft.graph.serializer.DefaultSerializer in project java-spring-webhooks-sample by microsoftgraph.

the class ListenController method handleNotification.

/**
 * This method receives and processes incoming notifications from Microsoft Graph
 *
 * @param jsonPayload the JSON body of the request
 * @return A 202 Accepted response
 */
@PostMapping("/listen")
public CompletableFuture<ResponseEntity<String>> handleNotification(@RequestBody final String jsonPayload) {
    // Deserialize the JSON body into a ChangeNotificationCollection
    final var serializer = new DefaultSerializer(new DefaultLogger());
    final var notifications = serializer.deserializeObject(jsonPayload, ChangeNotificationCollection.class);
    // Check for validation tokens
    boolean areTokensValid = true;
    if (notifications.validationTokens != null && !notifications.validationTokens.isEmpty()) {
        areTokensValid = TokenHelper.areValidationTokensValid(new String[] { clientId }, new String[] { tenantId }, notifications.validationTokens, keyDiscoveryUrl);
    }
    if (areTokensValid) {
        for (ChangeNotification notification : notifications.value) {
            // Look up subscription in store
            var subscription = subscriptionStore.getSubscription(notification.subscriptionId.toString());
            // the client state in the notification matches
            if (subscription != null && subscription.clientState.equals(notification.clientState)) {
                if (notification.encryptedContent == null) {
                    // No encrypted content, this is a new message notification
                    // without resource data
                    processNewMessageNotification(notification, subscription);
                } else {
                    // With encrypted content, this is a new channel message
                    // notification with encrypted resource data
                    processNewChannelMessageNotification(notification, subscription);
                }
            }
        }
    }
    return CompletableFuture.completedFuture(ResponseEntity.accepted().body(""));
}
Also used : DefaultSerializer(com.microsoft.graph.serializer.DefaultSerializer) ChangeNotification(com.microsoft.graph.models.ChangeNotification) DefaultLogger(com.microsoft.graph.logger.DefaultLogger) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 14 with DefaultSerializer

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

the class DefaultClientConfig method getSerializer.

/**
 * Gets the serializer
 *
 * @return the serializer
 */
@Override
public ISerializer getSerializer() {
    if (serializer == null) {
        serializer = new DefaultSerializer(getLogger());
        getLogger().logDebug("Created DefaultSerializer");
    }
    return serializer;
}
Also used : DefaultSerializer(com.microsoft.graph.serializer.DefaultSerializer)

Example 15 with DefaultSerializer

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

the class PlannerTests method testPlannerChecklistItemDeserialization.

@Test
public void testPlannerChecklistItemDeserialization() throws Exception {
    String input = "{\"@odata.context\":\"https://graph.microsoft.com/v1.0/$metadata#planner/tasks('433tZlfn_USJwWRL9khDx8kALTM7')/details/$entity\",\"@odata.etag\":\"W/\\\"JzEtVGFza0RldGFpbHMgQEBAQEBAQEBAQEBAQEBAcCc=\\\"\",\"description\":\"This is a test description of test event two.\",\"previewType\":\"automatic\",\"id\":\"433tZlfn_USJwWRL9khDx8kALTM7\",\"references\":{},\"checklist\":{\"55554\":{\"@odata.type\":\"#microsoft.graph.plannerChecklistItem\",\"isChecked\":false,\"title\":\"Test Item 2\",\"orderHint\":\"8586580527[2\",\"lastModifiedDateTime\":\"2018-11-30T05:01:53.0387892Z\",\"lastModifiedBy\":{\"user\":{\"displayName\":null,\"id\":\"ec786dee-da15-4896-8e73-57141477bae7\"}}},\"91100\":{\"@odata.type\":\"#microsoft.graph.plannerChecklistItem\",\"isChecked\":true,\"title\":\"Test Item 1 \",\"orderHint\":\"8586580528393292964Pc\",\"lastModifiedDateTime\":\"2018-11-30T05:01:47.4138223Z\",\"lastModifiedBy\":{\"user\":{\"displayName\":null,\"id\":\"ec786dee-da15-4896-8e73-57141477bae7\"}}}}}";
    final DefaultSerializer serializer = new DefaultSerializer(new DefaultLogger());
    PlannerTaskDetails ptd = serializer.deserializeObject(input, PlannerTaskDetails.class);
    assertNotNull(ptd);
    PlannerChecklistItem item1 = ptd.checklist.get("91100");
    assertEquals(item1.title, "Test Item 1 ");
    PlannerChecklistItem item2 = ptd.checklist.get("55554");
    assertEquals(item2.title, "Test Item 2");
}
Also used : DefaultSerializer(com.microsoft.graph.serializer.DefaultSerializer) PlannerTaskDetails(com.microsoft.graph.models.PlannerTaskDetails) PlannerChecklistItem(com.microsoft.graph.models.PlannerChecklistItem) DefaultLogger(com.microsoft.graph.logger.DefaultLogger) 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