|
| 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 | + |
| 20 | +import java.util.function.Consumer; |
| 21 | + |
| 22 | +import org.springframework.graphql.ExecutionGraphQlService; |
| 23 | +import org.springframework.util.Assert; |
| 24 | + |
| 25 | + |
| 26 | +/** |
| 27 | + * Default {@link ExecutionGraphQlServiceTester.Builder} implementation that |
| 28 | + * wraps an {@code ExecutionGraphQlService}. |
| 29 | + * |
| 30 | + * @author Rossen Stoyanchev |
| 31 | + * @since 1.0.0 |
| 32 | + */ |
| 33 | +final class DefaultExecutionGraphQlServiceTesterBuilder |
| 34 | + extends AbstractGraphQlTesterBuilder<DefaultExecutionGraphQlServiceTesterBuilder> |
| 35 | + implements ExecutionGraphQlServiceTester.Builder<DefaultExecutionGraphQlServiceTesterBuilder> { |
| 36 | + |
| 37 | + private final ExecutionGraphQlService service; |
| 38 | + |
| 39 | + |
| 40 | + DefaultExecutionGraphQlServiceTesterBuilder(ExecutionGraphQlService service) { |
| 41 | + Assert.notNull(service, "GraphQlService is required"); |
| 42 | + this.service = service; |
| 43 | + } |
| 44 | + |
| 45 | + DefaultExecutionGraphQlServiceTesterBuilder(GraphQlServiceGraphQlTransport transport) { |
| 46 | + this.service = transport.getGraphQlService(); |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + @Override |
| 51 | + public ExecutionGraphQlServiceTester build() { |
| 52 | + GraphQlServiceGraphQlTransport transport = new GraphQlServiceGraphQlTransport(this.service); |
| 53 | + GraphQlTester tester = super.buildGraphQlTester(transport); |
| 54 | + return new DefaultExecutionGraphQlServiceTester(tester, transport, getBuilderInitializer()); |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + /** |
| 59 | + * Default {@link ExecutionGraphQlServiceTester} implementation. |
| 60 | + */ |
| 61 | + private static class DefaultExecutionGraphQlServiceTester |
| 62 | + extends AbstractDelegatingGraphQlTester implements ExecutionGraphQlServiceTester { |
| 63 | + |
| 64 | + private final GraphQlServiceGraphQlTransport transport; |
| 65 | + |
| 66 | + private final Consumer<AbstractGraphQlTesterBuilder<?>> builderInitializer; |
| 67 | + |
| 68 | + private DefaultExecutionGraphQlServiceTester(GraphQlTester tester, GraphQlServiceGraphQlTransport transport, |
| 69 | + Consumer<AbstractGraphQlTesterBuilder<?>> builderInitializer) { |
| 70 | + |
| 71 | + super(tester); |
| 72 | + |
| 73 | + Assert.notNull(transport, "GraphQlServiceTransport is required"); |
| 74 | + Assert.notNull(builderInitializer, "`builderInitializer` is required"); |
| 75 | + |
| 76 | + this.transport = transport; |
| 77 | + this.builderInitializer = builderInitializer; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public DefaultExecutionGraphQlServiceTesterBuilder mutate() { |
| 82 | + DefaultExecutionGraphQlServiceTesterBuilder builder = new DefaultExecutionGraphQlServiceTesterBuilder(this.transport); |
| 83 | + this.builderInitializer.accept(builder); |
| 84 | + return builder; |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments