From 4d2cde83e4a92bf7d0673eeb79fe18cd1063ddd9 Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Tue, 21 May 2024 08:38:40 +0100 Subject: [PATCH] Polishing contribution Closes gh-969 --- .../test/tester/DefaultGraphQlTester.java | 15 +++-- .../graphql/test/tester/GraphQlTester.java | 14 ++--- .../test/tester/GraphQlTesterTests.java | 62 ++++++++----------- 3 files changed, 43 insertions(+), 48 deletions(-) diff --git a/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/DefaultGraphQlTester.java b/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/DefaultGraphQlTester.java index c6c37d11..a4844ec2 100644 --- a/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/DefaultGraphQlTester.java +++ b/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/DefaultGraphQlTester.java @@ -145,8 +145,8 @@ final class DefaultGraphQlTester implements GraphQlTester { } @Override - public DefaultRequest variable(Map values) { - this.variables.putAll(values); + public DefaultRequest variables(Map variables) { + this.variables.putAll(variables); return this; } @@ -159,7 +159,9 @@ final class DefaultGraphQlTester implements GraphQlTester { @SuppressWarnings("ConstantConditions") @Override public Response execute() { - return DefaultGraphQlTester.this.transport.execute(request()).map((response) -> mapResponse(response, request())).block(DefaultGraphQlTester.this.responseTimeout); + return DefaultGraphQlTester.this.transport.execute(request()) + .map((response) -> mapResponse(response, request())) + .block(DefaultGraphQlTester.this.responseTimeout); } @Override @@ -169,7 +171,8 @@ final class DefaultGraphQlTester implements GraphQlTester { @Override public Subscription executeSubscription() { - return () -> DefaultGraphQlTester.this.transport.executeSubscription(request()).map((result) -> mapResponse(result, request())); + return () -> DefaultGraphQlTester.this.transport.executeSubscription(request()) + .map((result) -> mapResponse(result, request())); } private GraphQlRequest request() { @@ -177,7 +180,9 @@ final class DefaultGraphQlTester implements GraphQlTester { } private DefaultResponse mapResponse(GraphQlResponse response, GraphQlRequest request) { - return new DefaultResponse(response, DefaultGraphQlTester.this.errorFilter, assertDecorator(request), DefaultGraphQlTester.this.jsonPathConfig); + return new DefaultResponse( + response, DefaultGraphQlTester.this.errorFilter, + assertDecorator(request), DefaultGraphQlTester.this.jsonPathConfig); } private Consumer assertDecorator(GraphQlRequest request) { diff --git a/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/GraphQlTester.java b/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/GraphQlTester.java index 4b76d467..30d770d6 100644 --- a/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/GraphQlTester.java +++ b/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/GraphQlTester.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -149,19 +149,19 @@ public interface GraphQlTester { * Add a variable. * @param name the variable name * @param value the variable value, possibly {@code null} since GraphQL - * supports providing null value vs not providing a value at all. + * supports providing null for a value vs not providing a value at all. * @return this request spec */ T variable(String name, @Nullable Object value); /** - * Add variables. - * @param values the variables to be set - * the variable of the values, possibly {@code null} since GraphQL - * supports providing null value vs not providing a value at all. + * Add variables from a {@link Map}. + * @param values the variables to add, possibly with {@code null} values + * since GraphQL supports null for a value vs not providing it at all. * @return this request spec + * @since 1.3.0 */ - T variable(Map values); + T variables(Map values); /** * Add a value for a protocol extension. diff --git a/spring-graphql-test/src/test/java/org/springframework/graphql/test/tester/GraphQlTesterTests.java b/spring-graphql-test/src/test/java/org/springframework/graphql/test/tester/GraphQlTesterTests.java index d7c640f4..2944d9e8 100644 --- a/spring-graphql-test/src/test/java/org/springframework/graphql/test/tester/GraphQlTesterTests.java +++ b/spring-graphql-test/src/test/java/org/springframework/graphql/test/tester/GraphQlTesterTests.java @@ -29,6 +29,7 @@ import org.junit.jupiter.api.Test; import org.springframework.core.ParameterizedTypeReference; import org.springframework.graphql.ExecutionGraphQlRequest; import org.springframework.graphql.ExecutionGraphQlService; +import org.springframework.graphql.GraphQlRequest; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -197,13 +198,13 @@ public class GraphQlTesterTests extends GraphQlTesterTestSupport { void nestedPath() { String document = "{me {name, friends}}"; - getGraphQlService().setDataAsJson(document, - "{" + - " \"me\":{" + - " \"name\":\"Luke Skywalker\"," - + " \"friends\":[{\"name\":\"Han Solo\"}, {\"name\":\"Leia Organa\"}]" + - " }" + - "}"); + getGraphQlService().setDataAsJson(document, """ + { + "me":{ + "name":"Luke Skywalker", + "friends":[{"name":"Han Solo"}, {"name":"Leia Organa"}] + } + }"""); graphQlTester().document(document).execute() .path("me", me -> me @@ -215,11 +216,12 @@ public class GraphQlTesterTests extends GraphQlTesterTestSupport { @Test void operationNameAndVariables() { - String document = "query HeroNameAndFriends($episode: Episode) {" + - " hero(episode: $episode) {" + - " name" - + " }" + - "}"; + String document = """ + query HeroNameAndFriends($episode: Episode) { + hero(episode: $episode) { + name + } + }"""; getGraphQlService().setDataAsJson(document, "{\"hero\": {\"name\":\"R2-D2\"}}"); @@ -242,36 +244,24 @@ public class GraphQlTesterTests extends GraphQlTesterTestSupport { } @Test - void operationNameAndVariablesAsMap() { + void variablesAsMap() { - String document = "query HeroNameAndFriends($episode: Episode) {" + - " hero(episode: $episode) {" + - " name" - + " }" + - "}"; + String document = """ + query HeroNameAndFriends($episode: Episode) { + hero(episode: $episode) { + name + } + }"""; getGraphQlService().setDataAsJson(document, "{\"hero\": {\"name\":\"R2-D2\"}}"); - Map variableMap = new LinkedHashMap<>(); + Map vars = Map.of( + "episode", Optional.of("JEDI"), "foo", Optional.of("bar"), "keyOnly", Optional.empty()); - variableMap.put("episode", Optional.of("JEDI")); - variableMap.put("foo", Optional.of("bar")); - variableMap.put("keyOnly", Optional.ofNullable(null)); + graphQlTester().document(document).variables(vars).execute(); - GraphQlTester.Response response = graphQlTester().document(document) - .operationName("HeroNameAndFriends") - .variable(variableMap) - .execute(); - - response.path("hero").entity(MovieCharacter.class).isEqualTo(MovieCharacter.create("R2-D2")); - - ExecutionGraphQlRequest request = getGraphQlService().getGraphQlRequest(); - assertThat(request.getDocument()).contains(document); - assertThat(request.getOperationName()).isEqualTo("HeroNameAndFriends"); - assertThat(request.getVariables()).hasSize(3); - assertThat(request.getVariables()).containsEntry("episode", Optional.of("JEDI")); - assertThat(request.getVariables()).containsEntry("foo", Optional.of("bar")); - assertThat(request.getVariables()).containsEntry("keyOnly", Optional.ofNullable(null)); + GraphQlRequest request = getGraphQlService().getGraphQlRequest(); + assertThat(request.getVariables()).containsExactlyEntriesOf(vars); } @Test