|
| 1 | +/* |
| 2 | + * Copyright 2020-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 | +package org.springframework.graphql.data.method.annotation.support; |
| 17 | + |
| 18 | +import java.util.List; |
| 19 | +import java.util.Optional; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.BeforeEach; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import org.springframework.context.support.StaticApplicationContext; |
| 25 | +import org.springframework.core.MethodParameter; |
| 26 | +import org.springframework.data.web.ProjectedPayload; |
| 27 | +import org.springframework.graphql.Book; |
| 28 | +import org.springframework.graphql.data.method.annotation.Argument; |
| 29 | +import org.springframework.graphql.data.method.annotation.QueryMapping; |
| 30 | +import org.springframework.stereotype.Controller; |
| 31 | + |
| 32 | +import static org.assertj.core.api.Assertions.assertThat; |
| 33 | + |
| 34 | +/** |
| 35 | + * |
| 36 | + */ |
| 37 | +public class ProjectedPayloadMethodArgumentResolverTests extends ArgumentResolverTestSupport { |
| 38 | + |
| 39 | + private ProjectedPayloadMethodArgumentResolver resolver; |
| 40 | + |
| 41 | + |
| 42 | + @BeforeEach |
| 43 | + void setUp() { |
| 44 | + StaticApplicationContext context = new StaticApplicationContext(); |
| 45 | + this.resolver = new ProjectedPayloadMethodArgumentResolver(context); |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + @Test |
| 50 | + void supports() { |
| 51 | + MethodParameter param = methodParam(BookController.class, "optionalProjection", Optional.class); |
| 52 | + assertThat(this.resolver.supportsParameter(param)).isTrue(); |
| 53 | + |
| 54 | + param = methodParam(BookController.class, "optionalString", Optional.class); |
| 55 | + assertThat(this.resolver.supportsParameter(param)).isFalse(); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void optionalWrapper() throws Exception { |
| 60 | + |
| 61 | + Object result = this.resolver.resolveArgument( |
| 62 | + methodParam(BookController.class, "optionalProjection", Optional.class), |
| 63 | + environment("{}")); |
| 64 | + |
| 65 | + assertThat(result).isNotNull().isInstanceOf(Optional.class); |
| 66 | + assertThat((Optional<?>) result).isNotPresent(); |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + @SuppressWarnings({"ConstantConditions", "unused"}) |
| 71 | + @Controller |
| 72 | + static class BookController { |
| 73 | + |
| 74 | + @QueryMapping |
| 75 | + public List<Book> optionalProjection(@Argument(name = "where") Optional<BookProjection> projection) { |
| 76 | + return null; |
| 77 | + } |
| 78 | + |
| 79 | + @QueryMapping |
| 80 | + public void optionalString(@Argument Optional<String> projection) { |
| 81 | + } |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + @ProjectedPayload |
| 87 | + interface BookProjection { |
| 88 | + |
| 89 | + String getAuthor(); |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments