Skip to content

Commit

Permalink
smallrye#521: fix merge of SchemaTest
Browse files Browse the repository at this point in the history
  • Loading branch information
t1 authored and jmartisk committed Oct 3, 2022
1 parent c1e6ee5 commit 5e9c581
Showing 1 changed file with 58 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {
Expand All @@ -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");
Expand All @@ -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" +
Expand All @@ -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");
Expand All @@ -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>>>");
}

Expand Down

0 comments on commit 5e9c581

Please sign in to comment.