Search in sources :

Example 1 with DefaultSerializer

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

the class BatchResponseStep method getDeserializedBody.

/**
 * Returned the deserialized response body of the current step
 * @param <T2> type of the response body
 * @param resultClass class of the resulting response body
 * @return the deserialized response body
 * @throws GraphServiceException when a bad request was sent
 * @throws GraphFatalServiceException when the service did not complete the operation as expected because of an internal error
 */
@Nullable
public <T2> T2 getDeserializedBody(@Nonnull final Class<T2> resultClass) throws GraphServiceException, GraphFatalServiceException {
    Objects.requireNonNull(resultClass, "parameter resultClass cannot be null");
    if (serializer == null || body == null || !(body instanceof JsonElement))
        return null;
    final GraphErrorResponse error = serializer.deserializeObject((JsonElement) body, GraphErrorResponse.class);
    if (error == null || error.error == null) {
        return serializer.deserializeObject((JsonElement) body, resultClass);
    } else {
        boolean verboseError = false;
        if (serializer instanceof DefaultSerializer) {
            final ILogger logger = ((DefaultSerializer) serializer).getLogger();
            verboseError = logger != null && logger.getLoggingLevel() == LoggerLevel.DEBUG;
        }
        throw GraphServiceException.createFromResponse("", "", new ArrayList<>(), "", headers, "", status, error, verboseError);
    }
}
Also used : DefaultSerializer(com.microsoft.graph.serializer.DefaultSerializer) GraphErrorResponse(com.microsoft.graph.http.GraphErrorResponse) JsonElement(com.google.gson.JsonElement) ILogger(com.microsoft.graph.logger.ILogger) Nullable(javax.annotation.Nullable)

Example 2 with DefaultSerializer

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

the class CoreHttpProviderTests method setCoreHttpProvider.

/**
 * Configures the http provider for test cases
 * @param toSerialize The object to serialize
 */
private void setCoreHttpProvider(final Object toSerialize) throws IOException {
    final OkHttpClient mClient = mock(OkHttpClient.class);
    final Call mCall = mock(Call.class);
    when(mClient.newCall(any(Request.class))).thenReturn(mCall);
    when(mCall.execute()).thenReturn(new Response.Builder().code(503).message("Service Unavailable").protocol(Protocol.HTTP_1_1).request(new Request.Builder().url("https://graph.microsoft.com/v1.0/me").build()).addHeader("Content-type", "application/json").body(ResponseBody.create(new GsonBuilder().setPrettyPrinting().create().toJson(toSerialize), MediaType.parse("application/json"))).build());
    mProvider = new CoreHttpProvider(new DefaultSerializer(mock(ILogger.class)), mock(ILogger.class), mClient);
}
Also used : DefaultSerializer(com.microsoft.graph.serializer.DefaultSerializer) Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder) GsonBuilder(com.google.gson.GsonBuilder) Request(okhttp3.Request) ILogger(com.microsoft.graph.logger.ILogger)

Example 3 with DefaultSerializer

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

the class BatchRequestContentTest method testRemoveBatchRequesStepWithId.

@Test
void testRemoveBatchRequesStepWithId() throws MalformedURLException {
    IHttpRequest requestStep = mock(IHttpRequest.class);
    when(requestStep.getRequestUrl()).thenReturn(new URL(testurl));
    BatchRequestContent requestContent = new BatchRequestContent();
    String stepId = requestContent.addBatchRequestStep(requestStep);
    requestContent.removeBatchRequestStepWithId(stepId);
    String content = new DefaultSerializer(mock(ILogger.class)).serializeObject(requestContent);
    String expectedContent = "{\"requests\":[]}";
    assertEquals(expectedContent, content);
}
Also used : DefaultSerializer(com.microsoft.graph.serializer.DefaultSerializer) IHttpRequest(com.microsoft.graph.http.IHttpRequest) URL(java.net.URL) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with DefaultSerializer

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

the class BatchRequestContentTest method testGetBatchRequestContent.

@Test
void testGetBatchRequestContent() throws MalformedURLException {
    IHttpRequest requestStep = mock(IHttpRequest.class);
    when(requestStep.getRequestUrl()).thenReturn(new URL(testurl));
    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 + "\"}]}";
    assertEquals(expectedContent, content);
}
Also used : DefaultSerializer(com.microsoft.graph.serializer.DefaultSerializer) IHttpRequest(com.microsoft.graph.http.IHttpRequest) URL(java.net.URL) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with DefaultSerializer

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

the class BatchRequestContentTest method testRemoveBatchRequesStepWithIdByAddingMultipleBatchSteps.

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

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