GraphQLCodegenAnnotationsTest.java 9.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
package com.kobylynskyi.graphql.codegen.scala;

import com.kobylynskyi.graphql.codegen.TestUtils;
import com.kobylynskyi.graphql.codegen.model.GeneratedLanguage;
import com.kobylynskyi.graphql.codegen.model.MappingConfig;
import com.kobylynskyi.graphql.codegen.utils.Utils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.File;
12 13 14 15 16 17 18 19 20
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.kobylynskyi.graphql.codegen.TestUtils.assertFileContainsElements;
import static com.kobylynskyi.graphql.codegen.TestUtils.assertSameTrimmedContent;
import static com.kobylynskyi.graphql.codegen.TestUtils.getFileByName;
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;

class GraphQLCodegenAnnotationsTest {

    private final File outputBuildDir = new File("build/generated");
    private final File outputJavaClassesDir = new File("build/generated/com/kobylynskyi/graphql/test1");

    private MappingConfig mappingConfig;

    @BeforeEach
    void init() {
        mappingConfig = new MappingConfig();
        mappingConfig.setPackageName("com.kobylynskyi.graphql.test1");
        mappingConfig.setGenerateParameterizedFieldsResolvers(false);
        mappingConfig.setGeneratedLanguage(GeneratedLanguage.SCALA);
    }

    @AfterEach
    void cleanup() {
        Utils.deleteDir(outputBuildDir);
    }

    @Test
    void generate_CustomAnnotationMappings() throws Exception {
        mappingConfig.setCustomTypesMapping(new HashMap<>(singletonMap("Event.createdDateTime", "org.joda.time.DateTime")));
        mappingConfig.setCustomAnnotationsMapping(new HashMap<>(singletonMap("Event.createdDateTime",
                singletonList("@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = classOf[com.example.json.DateTimeScalarDeserializer])"))));

50
        new ScalaGraphQLCodegen(singletonList("src/test/resources/schemas/test.graphqls"),
51 52 53 54 55
                outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate();

        File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
        assertFileContainsElements(files, "Event.scala",
                "    @com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = classOf[com.example.json.DateTimeScalarDeserializer])\n" +
56
                        "    createdDateTime: org.joda.time.DateTime,");
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    }

    @Test
    void generate_CustomAnnotationMappings_Class() throws Exception {
        Map<String, List<String>> customAnnotationsMapping = new HashMap<>();
        // input
        customAnnotationsMapping.put("AcceptTopicSuggestionInput", singletonList("@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = classOf[AcceptTopicSuggestionInputDeserializer])"));
        // type
        customAnnotationsMapping.put("AcceptTopicSuggestionPayload", singletonList("com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = classOf[AcceptTopicSuggestionPayloadDeserializer])"));
        // interface
        customAnnotationsMapping.put("Actor", singletonList("@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = classOf[ActorDeserializer])"));
        // union
        customAnnotationsMapping.put("Assignee", singletonList("com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = classOf[AssigneeDeserializer])"));
        // enum
        customAnnotationsMapping.put("DeploymentOrderField", singletonList("@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = classOf[DeploymentOrderFieldDeserializer])"));
        mappingConfig.setCustomAnnotationsMapping(customAnnotationsMapping);

74
        new ScalaGraphQLCodegen(singletonList("src/test/resources/schemas/github.graphqls"),
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
                outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate();

        File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
        assertFileContainsElements(files, "AcceptTopicSuggestionInput.scala",
                "@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = classOf[AcceptTopicSuggestionInputDeserializer])\n" +
                        "case class AcceptTopicSuggestionInput");
        assertFileContainsElements(files, "AcceptTopicSuggestionPayload.scala",
                "@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = classOf[AcceptTopicSuggestionPayloadDeserializer])\n" +
                        "case class AcceptTopicSuggestionPayload");
        assertFileContainsElements(files, "Actor.scala",
                "@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = classOf[ActorDeserializer])\n" +
                        "trait Actor");
        assertFileContainsElements(files, "Assignee.scala",
                "@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = classOf[AssigneeDeserializer])\n" +
                        "trait Assignee");
        assertFileContainsElements(files, "DeploymentOrderField.scala",
                "@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = classOf[DeploymentOrderFieldDeserializer])\n" +
                        "object DeploymentOrderField extends Enumeration");
    }

    @Test
    void generate_CustomAnnotationMappings_Multiple() throws Exception {
        Map<String, List<String>> customAnnotationsMapping = new HashMap<>();
        // type
        customAnnotationsMapping.put("AcceptTopicSuggestionPayload", Arrays.asList(
                "@com.fasterxml.jackson.annotation.JsonTypeInfo(use = com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME, property = \"__typename\")",
                "@com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver(classOf[io.github.kobylynskyi.order.external.starwars.AcceptTopicSuggestionPayloadTypeResolver])"));
        mappingConfig.setCustomAnnotationsMapping(customAnnotationsMapping);

104
        new ScalaGraphQLCodegen(singletonList("src/test/resources/schemas/github.graphqls"),
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
                outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate();

        File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
        assertFileContainsElements(files, "AcceptTopicSuggestionPayload.scala", "@com.fasterxml.jackson.annotation.JsonTypeInfo(use = com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME, property = \"__typename\")\n" +
                "@com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver(classOf[io.github.kobylynskyi.order.external.starwars.AcceptTopicSuggestionPayloadTypeResolver])\n" +
                "case class AcceptTopicSuggestionPayload");
    }

    @Test
    void generate_CustomAnnotationMappings_RequestResponseClasses() throws Exception {
        Map<String, List<String>> customAnnotationsMapping = new HashMap<>();
        // request
        customAnnotationsMapping.put("CodeOfConductQueryRequest", singletonList("@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = classOf[CodeOfConductQueryRequestDeserializer])"));
        // response
        customAnnotationsMapping.put("CodeOfConductQueryResponse", singletonList("com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = classOf[CodeOfConductQueryResponseDeserializer])"));
        mappingConfig.setCustomAnnotationsMapping(customAnnotationsMapping);
        mappingConfig.setGenerateClient(true);

123
        new ScalaGraphQLCodegen(singletonList("src/test/resources/schemas/github.graphqls"),
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
                outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate();

        File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
        assertFileContainsElements(files, "CodeOfConductQueryRequest.scala",
                "@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = classOf[CodeOfConductQueryRequestDeserializer])\n" +
                        "class CodeOfConductQueryRequest");
        assertFileContainsElements(files, "CodeOfConductQueryResponse.scala",
                "@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = classOf[CodeOfConductQueryResponseDeserializer])\n" +
                        "class CodeOfConductQueryResponse extends GraphQLResult[JMap[String, CodeOfConduct]]");
    }

    @Test
    void generate_Directives() throws Exception {
        Map<String, List<String>> directiveAnnotationsMapping = new HashMap<>();
        directiveAnnotationsMapping.put("auth",
                singletonList("@com.example.CustomAnnotation(roles={{roles?toArray}}, boo={{boo?toArray}}, float={{float?toArrayOfStrings}}, int={{int}}, n={{n?toString}})"));
        mappingConfig.setDirectiveAnnotationsMapping(directiveAnnotationsMapping);

142
        new ScalaGraphQLCodegen(singletonList("src/test/resources/schemas/test.graphqls"),
143 144 145 146 147 148 149 150 151 152 153 154
                outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate();

        File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
        assertSameTrimmedContent(
                new File("src/test/resources/expected-classes/scala/annotation/CreateEventMutationResolver.scala.txt"),
                getFileByName(files, "CreateEventMutationResolver.scala"));
        assertSameTrimmedContent(
                new File("src/test/resources/expected-classes/scala/annotation/MutationResolver.scala.txt"),
                getFileByName(files, "MutationResolver.scala"));
    }

}