Skip to content

Commit

Permalink
extend test for graphql client
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulBredl committed May 4, 2024
1 parent bc5882b commit 43353ac
Showing 1 changed file with 69 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLOperation;
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLOperationRequest;
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLResponseProjection;
import graphql.GraphqlErrorException;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.graphql.ResponseError;
import org.springframework.graphql.client.ClientGraphQlResponse;
import org.springframework.graphql.client.ClientResponseField;
import org.springframework.graphql.client.GraphQlClient;
Expand All @@ -17,6 +20,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand All @@ -40,34 +44,19 @@ void testStartBuildingRequest() {
@Test
void testRetrieveResponse() {
// Arrange
WebGraphQlClient graphQlClient = mock(WebGraphQlClient.class);
GraphQlRequestExecutor executor = new GraphQlRequestExecutor(() -> graphQlClient);

GraphQLOperationRequest request = mock(GraphQLOperationRequest.class);
when(request.getOperationName()).thenReturn("operationName");
when(request.getOperationType()).thenReturn(GraphQLOperation.QUERY);
when(request.getInput()).thenReturn(Map.of());
Class<Object> responseType = Object.class;

GraphQLResponseProjection projection = mock(GraphQLResponseProjection.class);
when(projection.toString()).thenReturn("projection");

GraphQlClient.RequestSpec requestSpec = mock(GraphQlClient.RequestSpec.class);
when(graphQlClient.document(Mockito.anyString())).thenReturn(requestSpec);

ClientGraphQlResponse response = mock(ClientGraphQlResponse.class);
when(response.getErrors()).thenReturn(List.of());
GraphQLOperationRequest request = getMockRequest();
GraphQLResponseProjection projection = getMockProjection();

Object expectedResponse = new Object();
ClientResponseField expectedField = mock(ClientResponseField.class);
when(expectedField.toEntity(Object.class)).thenReturn(expectedResponse);
ClientGraphQlResponse response = getMockResponse(expectedResponse);

when(requestSpec.execute()).thenReturn(Mono.just(response));
when(response.field(any())).thenReturn(expectedField);
WebGraphQlClient graphQlClient = getMockGraphQlClient(response);

GraphQlRequestExecutor executor = new GraphQlRequestExecutor(() -> graphQlClient);

// Act
Mono<Object> resultMono = executor.request(request)
.projectTo(responseType, projection)
.projectTo(Object.class, projection)
.retrieve();

// Assert
Expand All @@ -76,6 +65,63 @@ void testRetrieveResponse() {

// Verify
Mockito.verify(graphQlClient).document(any(String.class));
Mockito.verify(requestSpec).execute();
}

@Test
void testErrors() {
// Arrange
GraphQLOperationRequest request = getMockRequest();
GraphQLResponseProjection projection = getMockProjection();

ClientGraphQlResponse response = getMockResponse(null, getMockError());

WebGraphQlClient graphQlClient = getMockGraphQlClient(response);

GraphQlRequestExecutor executor = new GraphQlRequestExecutor(() -> graphQlClient);

// Act
Mono<Object> resultMono = executor.request(request)
.projectTo(Object.class, projection)
.retrieve();

assertThrows(GraphqlErrorException.class, resultMono::block);
}

private static @NotNull ResponseError getMockError() {
ResponseError error = mock(ResponseError.class);
when(error.getMessage()).thenReturn("error message");
return error;
}

private static @NotNull WebGraphQlClient getMockGraphQlClient(ClientGraphQlResponse response) {
WebGraphQlClient graphQlClient = mock(WebGraphQlClient.class);
GraphQlClient.RequestSpec requestSpec = mock(GraphQlClient.RequestSpec.class);
when(graphQlClient.document(Mockito.anyString())).thenReturn(requestSpec);
when(requestSpec.execute()).thenReturn(Mono.just(response));
return graphQlClient;
}

private static @NotNull ClientGraphQlResponse getMockResponse(Object expectedResponse,
ResponseError... errors) {
ClientGraphQlResponse response = mock(ClientGraphQlResponse.class);
when(response.getErrors()).thenReturn(List.of(errors));
ClientResponseField expectedField = mock(ClientResponseField.class);
when(response.field(any())).thenReturn(expectedField);
when(expectedField.toEntity(Object.class)).thenReturn(expectedResponse);
return response;
}

private static @NotNull GraphQLResponseProjection getMockProjection() {
GraphQLResponseProjection projection = mock(GraphQLResponseProjection.class);
when(projection.toString()).thenReturn("projection");
return projection;
}

private static @NotNull GraphQLOperationRequest getMockRequest() {
GraphQLOperationRequest request = mock(GraphQLOperationRequest.class);
when(request.getOperationName()).thenReturn("operationName");
when(request.getOperationType()).thenReturn(GraphQLOperation.QUERY);
when(request.getInput()).thenReturn(Map.of());
return request;
}
}

0 comments on commit 43353ac

Please sign in to comment.