From 5e9c581e6ceca96b0153ebf1ccdf2be0b1ec0af1 Mon Sep 17 00:00:00 2001 From: Ruediger zu Dohna Date: Fri, 23 Sep 2022 09:04:25 +0200 Subject: [PATCH] #521: fix merge of SchemaTest --- .../smallrye/graphql/schema/SchemaTest.java | 65 +++++++++++++++++-- 1 file changed, 58 insertions(+), 7 deletions(-) diff --git a/server/implementation/src/test/java/io/smallrye/graphql/schema/SchemaTest.java b/server/implementation/src/test/java/io/smallrye/graphql/schema/SchemaTest.java index 9346307f2..becceb117 100644 --- a/server/implementation/src/test/java/io/smallrye/graphql/schema/SchemaTest.java +++ b/server/implementation/src/test/java/io/smallrye/graphql/schema/SchemaTest.java @@ -15,6 +15,7 @@ import org.jboss.jandex.IndexView; import org.jboss.jandex.Indexer; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -26,11 +27,20 @@ import graphql.schema.GraphQLObjectType; import graphql.schema.GraphQLSchema; import io.smallrye.graphql.api.Directive; +import io.smallrye.graphql.api.federation.Key; import io.smallrye.graphql.bootstrap.Bootstrap; import io.smallrye.graphql.execution.SchemaPrinter; +import io.smallrye.graphql.execution.TestConfig; import io.smallrye.graphql.schema.model.Schema; +import io.smallrye.graphql.spi.config.Config; class SchemaTest { + private final TestConfig config = (TestConfig) Config.get(); + + @AfterEach + void tearDown() { + config.reset(); + } @Test void testSchemaWithDirectives() throws URISyntaxException, IOException { @@ -55,7 +65,7 @@ void testSchemaWithDirectives() throws URISyntaxException, IOException { assertEquals("intArrayTestDirective", intArrayTestDirective.getName()); GraphQLArgument argument = intArrayTestDirective.getArgument("value"); assertEquals("value", argument.getName()); - assertArrayEquals(new Object[] { 1, 2, 3 }, (Object[]) argument.getArgumentValue().getValue()); + assertArrayEquals(new Object[] { 1, 2, 3 }, argument.toAppliedArgument().getValue()); GraphQLFieldDefinition valueField = testTypeWithDirectives.getFieldDefinition("value"); GraphQLDirective fieldDirectiveInstance = valueField.getDirective("fieldDirective"); @@ -82,8 +92,7 @@ void schemaWithEnumDirectives() { assertNotNull(enumWithDirectives.getValue("A").getDirective("enumDirective"), "Enum value EnumWithDirectives.A should have directive @enumDirective"); - String schemaString = new SchemaPrinter().print(graphQLSchema); - assertSchemaEndsWith(schemaString, "" + + assertSchemaEndsWith(graphQLSchema, "" + "enum EnumWithDirectives @enumDirective {\n" + " A @enumDirective\n" + " B\n" + @@ -103,14 +112,54 @@ void schemaWithInputDirectives() { assertNotNull(inputWithDirectives.getField("bar").getDirective("inputDirective"), "Input type field InputWithDirectivesInput.bar should have directive @inputDirective"); - String schemaString = new SchemaPrinter().print(graphQLSchema); - assertSchemaEndsWith(schemaString, "" + + assertSchemaEndsWith(graphQLSchema, "" + "input InputWithDirectivesInput @inputDirective {\n" + " bar: Int! @inputDirective\n" + " foo: Int! @inputDirective\n" + "}\n"); } + @Test + void testSchemaWithFederationDisabled() { + GraphQLSchema graphQLSchema = createGraphQLSchema(Directive.class, Key.class, TestTypeWithFederation.class, + FederationTestApi.class); + + assertSchemaEndsWith(graphQLSchema, "\n" + + "\"Query root\"\n" + + "type Query {\n" + + " testTypeWithFederation(arg: String): TestTypeWithFederation\n" + + "}\n" + + "\n" + + "type TestTypeWithFederation @key(fields : [\"id\"]) {\n" + + " id: String\n" + + "}\n"); + } + + @Test + void testSchemaWithFederation() { + config.federationEnabled = true; + GraphQLSchema graphQLSchema = createGraphQLSchema(Directive.class, Key.class, TestTypeWithFederation.class, + FederationTestApi.class); + + assertSchemaEndsWith(graphQLSchema, "\n" + + "union _Entity = TestTypeWithFederation\n" + + "\n" + + "\"Query root\"\n" + + "type Query {\n" + + " _entities(representations: [_Any!]!): [_Entity]!\n" + + " _service: _Service!\n" + + " testTypeWithFederation(arg: String): TestTypeWithFederation\n" + + "}\n" + + "\n" + + "type TestTypeWithFederation @key(fields : [\"id\"]) {\n" + + " id: String\n" + + "}\n" + + "\n" + + "type _Service {\n" + + " sdl: String!\n" + + "}\n"); + } + private GraphQLSchema createGraphQLSchema(Class... api) { Schema schema = SchemaBuilder.build(scan(api)); assertNotNull(schema, "Schema should not be null"); @@ -119,11 +168,13 @@ private GraphQLSchema createGraphQLSchema(Class... api) { return graphQLSchema; } - private static void assertSchemaContains(String schema, String snippet) { - assertTrue(schema.contains(snippet), () -> "<<<\n" + schema + "\n>>> does not contain <<<\n" + snippet + "\n>>>"); + private static void assertSchemaEndsWith(GraphQLSchema schema, String end) { + String schemaString = new SchemaPrinter().print(schema); + assertSchemaEndsWith(schemaString, end); } private static void assertSchemaEndsWith(String schema, String end) { + // assertEquals(schema, end); // this is convenient for debugging, as the IDE can show the diff assertTrue(schema.endsWith(end), () -> "<<<\n" + schema + "\n>>> does not end with <<<\n" + end + "\n>>>"); }