|
| 1 | +/* |
| 2 | + * Copyright 2020-2024 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.data.method.annotation.support; |
| 18 | + |
| 19 | +import java.net.URI; |
| 20 | +import java.net.URL; |
| 21 | +import java.time.Instant; |
| 22 | +import java.util.Date; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Locale; |
| 25 | +import java.util.stream.Stream; |
| 26 | + |
| 27 | +import graphql.schema.DataFetchingEnvironmentImpl; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.junit.jupiter.params.ParameterizedTest; |
| 30 | +import org.junit.jupiter.params.provider.MethodSource; |
| 31 | + |
| 32 | +import org.springframework.core.MethodParameter; |
| 33 | +import org.springframework.graphql.Author; |
| 34 | +import org.springframework.graphql.Book; |
| 35 | +import org.springframework.graphql.BookSource; |
| 36 | +import org.springframework.graphql.data.method.HandlerMethodArgumentResolver; |
| 37 | +import org.springframework.graphql.data.method.annotation.SchemaMapping; |
| 38 | +import org.springframework.stereotype.Controller; |
| 39 | + |
| 40 | +import static org.assertj.core.api.Assertions.assertThat; |
| 41 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 42 | + |
| 43 | +/** |
| 44 | + * Tests for {@link SourceMethodArgumentResolver}. |
| 45 | + * @author Brian Clozel |
| 46 | + */ |
| 47 | +class SourceMethodArgumentResolverTests extends ArgumentResolverTestSupport { |
| 48 | + |
| 49 | + private final HandlerMethodArgumentResolver resolver = new SourceMethodArgumentResolver(); |
| 50 | + |
| 51 | + @ParameterizedTest |
| 52 | + @MethodSource("excludedTypes") |
| 53 | + void excludedSourceTypes(Class<?> argumentType) { |
| 54 | + MethodParameter param = methodParam(BookController.class, "notSupported", argumentType); |
| 55 | + assertThat(this.resolver.supportsParameter(param)).isFalse(); |
| 56 | + } |
| 57 | + |
| 58 | + static Stream<Class<?>> excludedTypes() { |
| 59 | + return Stream.of(List.class, String[].class, Integer.class, Date.class, Instant.class, URI.class, URL.class, Locale.class, Class.class); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void supportedSourceTypes() { |
| 64 | + MethodParameter param = methodParam(BookController.class, "author", Book.class); |
| 65 | + assertThat(this.resolver.supportsParameter(param)).isTrue(); |
| 66 | + |
| 67 | + param = methodParam(BookController.class, "name", Format.class); |
| 68 | + assertThat(this.resolver.supportsParameter(param)).isTrue(); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void bindArgumentWhenPresent() throws Exception { |
| 73 | + Book aBook = BookSource.getBook(1L); |
| 74 | + Object result = this.resolver.resolveArgument( |
| 75 | + methodParam(BookController.class, "author", Book.class), |
| 76 | + DataFetchingEnvironmentImpl.newDataFetchingEnvironment().source(aBook).build()); |
| 77 | + |
| 78 | + assertThat(result).isNotNull().isInstanceOf(Book.class).isEqualTo(aBook); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void bindArgumentWhenUnavailable() throws Exception { |
| 83 | + MethodParameter methodParameter = methodParam(BookController.class, "author", Book.class); |
| 84 | + assertThatThrownBy(() -> this.resolver.resolveArgument( |
| 85 | + methodParameter, |
| 86 | + DataFetchingEnvironmentImpl.newDataFetchingEnvironment().build())).isInstanceOf(IllegalStateException.class) |
| 87 | + .hasMessageContaining("Parameter [0] in %s: was not recognized by any resolver and there is no source/parent either.", methodParameter.getMethod()); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void bindArgumentWhenWrongType() throws Exception { |
| 92 | + MethodParameter methodParameter = methodParam(BookController.class, "author", Book.class); |
| 93 | + assertThatThrownBy(() -> this.resolver.resolveArgument( |
| 94 | + methodParameter, |
| 95 | + DataFetchingEnvironmentImpl.newDataFetchingEnvironment().source(2L).build())).isInstanceOf(IllegalStateException.class) |
| 96 | + .hasMessageContaining("Parameter [0] in %s: does not match the source Object type 'class java.lang.Long'.", methodParameter.getMethod()); |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + @SuppressWarnings({"ConstantConditions", "unused"}) |
| 101 | + @Controller |
| 102 | + static class BookController { |
| 103 | + |
| 104 | + public void notSupported(Integer integer) { |
| 105 | + |
| 106 | + } |
| 107 | + |
| 108 | + public void notSupported(Date date) { |
| 109 | + |
| 110 | + } |
| 111 | + |
| 112 | + public void notSupported(Instant instant) { |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | + public void notSupported(URI uri) { |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | + public void notSupported(URL url) { |
| 121 | + |
| 122 | + } |
| 123 | + |
| 124 | + public void notSupported(Locale locale) { |
| 125 | + |
| 126 | + } |
| 127 | + |
| 128 | + public void notSupported(Class<?> klass) { |
| 129 | + |
| 130 | + } |
| 131 | + |
| 132 | + public void notSupported(List<String> list) { |
| 133 | + |
| 134 | + } |
| 135 | + |
| 136 | + public void notSupported(String[] array) { |
| 137 | + |
| 138 | + } |
| 139 | + |
| 140 | + @SchemaMapping |
| 141 | + public Author author(Book book) { |
| 142 | + return null; |
| 143 | + } |
| 144 | + |
| 145 | + @SchemaMapping |
| 146 | + public String name(Format format) { |
| 147 | + return format.name(); |
| 148 | + } |
| 149 | + |
| 150 | + } |
| 151 | + |
| 152 | + enum Format { |
| 153 | + PAPERBACK, EBOOK; |
| 154 | + } |
| 155 | +} |
0 commit comments