|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.graphql.test.tester; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.function.Consumer; |
| 22 | + |
| 23 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 24 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 25 | +import graphql.ExecutionInput; |
| 26 | +import graphql.ExecutionResult; |
| 27 | +import graphql.ExecutionResultImpl; |
| 28 | +import graphql.GraphQLError; |
| 29 | +import org.mockito.ArgumentCaptor; |
| 30 | +import reactor.core.publisher.Mono; |
| 31 | + |
| 32 | +import org.springframework.graphql.GraphQlService; |
| 33 | +import org.springframework.graphql.RequestInput; |
| 34 | +import org.springframework.graphql.RequestOutput; |
| 35 | + |
| 36 | +import static org.mockito.BDDMockito.given; |
| 37 | +import static org.mockito.Mockito.mock; |
| 38 | + |
| 39 | +/** |
| 40 | + * Base class for {@link GraphQlTester} tests. |
| 41 | + * |
| 42 | + * @author Rossen Stoyanchev |
| 43 | + */ |
| 44 | +public class GraphQlTesterTestSupport { |
| 45 | + |
| 46 | + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); |
| 47 | + |
| 48 | + |
| 49 | + private final ArgumentCaptor<RequestInput> inputCaptor = ArgumentCaptor.forClass(RequestInput.class); |
| 50 | + |
| 51 | + private final GraphQlService graphQlService = mock(GraphQlService.class); |
| 52 | + |
| 53 | + private final GraphQlTester.Builder<?> graphQlTesterBuilder = GraphQlServiceTester.builder(this.graphQlService); |
| 54 | + |
| 55 | + private final GraphQlTester graphQlTester = this.graphQlTesterBuilder.build(); |
| 56 | + |
| 57 | + |
| 58 | + protected GraphQlTester graphQlTester() { |
| 59 | + return this.graphQlTester; |
| 60 | + } |
| 61 | + |
| 62 | + public GraphQlTester.Builder<?> graphQlTesterBuilder() { |
| 63 | + return this.graphQlTesterBuilder; |
| 64 | + } |
| 65 | + |
| 66 | + protected RequestInput requestInput() { |
| 67 | + return this.inputCaptor.getValue(); |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + protected void setMockResponse(String data) { |
| 72 | + setMockResponse(builder -> serialize(data, builder)); |
| 73 | + } |
| 74 | + |
| 75 | + protected void setMockResponse(GraphQLError... errors) { |
| 76 | + setMockResponse(builder -> builder.errors(Arrays.asList(errors))); |
| 77 | + } |
| 78 | + |
| 79 | + private void setMockResponse(Consumer<ExecutionResultImpl.Builder> consumer) { |
| 80 | + |
| 81 | + ExecutionResultImpl.Builder builder = new ExecutionResultImpl.Builder(); |
| 82 | + consumer.accept(builder); |
| 83 | + ExecutionInput executionInput = ExecutionInput.newExecutionInput("{}").build(); |
| 84 | + ExecutionResult result = builder.build(); |
| 85 | + |
| 86 | + given(this.graphQlService.execute(this.inputCaptor.capture())) |
| 87 | + .willReturn(Mono.just(new RequestOutput(executionInput, result))); |
| 88 | + } |
| 89 | + |
| 90 | + private void serialize(String data, ExecutionResultImpl.Builder builder) { |
| 91 | + try { |
| 92 | + builder.data(OBJECT_MAPPER.readValue(data, Map.class)); |
| 93 | + } |
| 94 | + catch (JsonProcessingException ex) { |
| 95 | + throw new IllegalStateException(ex); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments