forked from smallrye/smallrye-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…a field and/or method as a graphql field.
- Loading branch information
Showing
3 changed files
with
275 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
common/schema-builder/src/test/java/io/smallrye/graphql/schema/creator/FieldCreatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package io.smallrye.graphql.schema.creator; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.io.IOException; | ||
import java.util.Optional; | ||
|
||
import org.jboss.jandex.*; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
|
||
import io.smallrye.graphql.schema.IndexCreator; | ||
import io.smallrye.graphql.schema.helper.Direction; | ||
import io.smallrye.graphql.schema.helper.TypeAutoNameStrategy; | ||
import io.smallrye.graphql.schema.model.Field; | ||
import io.smallrye.graphql.schema.model.Reference; | ||
|
||
class FieldCreatorTest { | ||
|
||
private final ReferenceCreator referenceCreator = new ReferenceCreator(TypeAutoNameStrategy.Default); | ||
private final FieldCreator fieldCreator = new FieldCreator(referenceCreator); | ||
|
||
/** | ||
* Tests the visibility of fields with certain field- and method-modifier combinations. | ||
* | ||
* @param direction the direction of the graphql-field to test | ||
* @param fieldName the field name, may be null or empty if just a method is provided | ||
* @param methodName the method name, may be null or empty if just a method is provided | ||
* @param expected the expected visibility | ||
*/ | ||
@ParameterizedTest | ||
@CsvSource(value = { | ||
" IN, PUBLIC_STATIC_FIELD, , false", | ||
" IN, PUBLIC_STATIC_FIELD, setField, true", | ||
" IN, privateField, ,false", | ||
" IN, publicFinalField, , false", | ||
" IN, publicFinalField, setField, true", | ||
" IN, publicField, , true", | ||
" IN, publicTransientField, , false", | ||
" IN, publicTransientField, setField, false", | ||
" IN, ,setStaticField, false", | ||
|
||
"OUT, PUBLIC_STATIC_FIELD, , false", | ||
"OUT, PUBLIC_STATIC_FIELD, getField, true", | ||
"OUT, privateField, , false", | ||
"OUT, publicFinalField, , true", | ||
"OUT, publicField, , true", | ||
"OUT, publicTransientField, , false", | ||
"OUT, publicTransientField, getField, false", | ||
"OUT, ,getStaticField, false", | ||
}) | ||
void testVisibility(Direction direction, String fieldName, String methodName, boolean expected) throws IOException { | ||
MethodInfo method = getMethod(methodName); | ||
FieldInfo fieldInfo = getField(fieldName); | ||
|
||
final boolean isVisible = FieldCreator.isGraphQlField(direction, fieldInfo, method); | ||
assertEquals(expected, isVisible); | ||
} | ||
|
||
@ParameterizedTest | ||
@CsvSource(value = { | ||
" IN, PUBLIC_STATIC_FIELD, false", | ||
" IN, privateField, false", | ||
" IN, publicFinalField, false", | ||
" IN, publicField, true", | ||
"OUT, PUBLIC_STATIC_FIELD, false", | ||
"OUT, privateField, false", | ||
"OUT, publicFinalField, true", | ||
"OUT, publicField, true", | ||
}) | ||
void testFieldsWithoutMethod(Direction direction, String fieldName, boolean expected) throws IOException { | ||
FieldInfo field = getField(fieldName); | ||
|
||
final Optional<Field> fieldForPojo = fieldCreator.createFieldForPojo(direction, field, new Reference()); | ||
|
||
assertEquals(expected, fieldForPojo.isPresent()); | ||
} | ||
|
||
@ParameterizedTest | ||
@CsvSource(value = { | ||
"OUT, getStaticField, false", | ||
" IN, setStaticField, false", | ||
}) | ||
void testMethodsWithoutField(Direction direction, String methodName, boolean expected) throws IOException { | ||
MethodInfo method = getMethod(methodName); | ||
|
||
final Optional<Field> fieldForPojo = fieldCreator.createFieldForPojo(direction, null, method, new Reference()); | ||
|
||
assertEquals(expected, fieldForPojo.isPresent()); | ||
} | ||
|
||
@ParameterizedTest | ||
@CsvSource(value = { | ||
"OUT, privateField, getField, true", | ||
" IN, privateField, setField, true", | ||
}) | ||
void testMethodsWithField(Direction direction, String fieldName, String methodName, boolean expected) throws IOException { | ||
MethodInfo method = getMethod(methodName); | ||
FieldInfo fieldInfo = getField(fieldName); | ||
|
||
final boolean isVisible = FieldCreator.isGraphQlField(direction, fieldInfo, method); | ||
assertEquals(expected, isVisible); | ||
//final Optional<Field> fieldForPojo = fieldCreator.createFieldForPojo(direction, null, method, new Reference()); | ||
//assertEquals(expected, fieldForPojo.isPresent()); | ||
} | ||
|
||
@ParameterizedTest | ||
@CsvSource(value = { | ||
"OUT, getStaticField, false", | ||
}) | ||
void testInterfaceMethods(Direction direction, String methodName, boolean expected) throws IOException { | ||
MethodInfo method = getMethod(methodName); | ||
|
||
final Optional<Field> fieldForPojo = fieldCreator.createFieldForInterface(method, new Reference()); | ||
assertEquals(expected, fieldForPojo.isPresent()); | ||
} | ||
|
||
private static FieldInfo getField(String name) throws IOException { | ||
if (name == null || name.isEmpty()) { | ||
return null; | ||
} | ||
Index complete = IndexCreator.index(SimplePojo.class); | ||
|
||
ClassInfo classByName = complete.getClassByName(DotName.createSimple(SimplePojo.class.getName())); | ||
return classByName.field(name); | ||
} | ||
|
||
private static MethodInfo getMethod(String name) throws IOException { | ||
if (name == null || name.isEmpty()) { | ||
return null; | ||
} | ||
Index complete = IndexCreator.index(SimplePojo.class); | ||
|
||
ClassInfo classByName = complete.getClassByName(DotName.createSimple(SimplePojo.class.getName())); | ||
return classByName.firstMethod(name); | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
common/schema-builder/src/test/java/io/smallrye/graphql/schema/creator/SimplePojo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.smallrye.graphql.schema.creator; | ||
|
||
/** | ||
* Used to Test FieldCreator. | ||
* | ||
* Just contains some fields and methods with different modifiers. | ||
*/ | ||
public class SimplePojo { | ||
|
||
public transient String publicTransientField = ""; | ||
|
||
public static String PUBLIC_STATIC_FIELD = ""; | ||
|
||
public final String publicFinalField = ""; | ||
|
||
public String publicField = ""; | ||
|
||
private String privateField = ""; | ||
|
||
public static String getStaticField() { | ||
return ""; | ||
} | ||
|
||
public static void setStaticField(String s) { | ||
} | ||
|
||
public void setPrivateField(final String privateField) { | ||
this.privateField = privateField; | ||
} | ||
|
||
public String getPrivateField() { | ||
return privateField; | ||
} | ||
|
||
public String getField() { | ||
return publicTransientField; | ||
} | ||
|
||
public void setField(final String publicTransientField) { | ||
this.publicTransientField = publicTransientField; | ||
} | ||
} |