GraphQLCodegenAnnotationsTest.java 10.5 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
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 {
46 47
        mappingConfig
                .setCustomTypesMapping(new HashMap<>(singletonMap("Event.createdDateTime", "org.joda.time.DateTime")));
48
        mappingConfig.setCustomAnnotationsMapping(new HashMap<>(singletonMap("Event.createdDateTime",
49 50 51 52 53
                singletonList(
                        "@com.fasterxml.jackson.databind" +
                                ".annotation.JsonDeserialize(using =" +
                                " classOf[com.example.json" +
                                ".DateTimeScalarDeserializer])"))));
54

55
        new ScalaGraphQLCodegen(singletonList("src/test/resources/schemas/test.graphqls"),
56 57 58 59
                outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate();

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

    @Test
    void generate_CustomAnnotationMappings_Class() throws Exception {
        Map<String, List<String>> customAnnotationsMapping = new HashMap<>();
        // input
69 70 71
        customAnnotationsMapping.put("AcceptTopicSuggestionInput", singletonList(
                "@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = " +
                        "classOf[AcceptTopicSuggestionInputDeserializer])"));
72
        // type
73 74 75
        customAnnotationsMapping.put("AcceptTopicSuggestionPayload", singletonList(
                "com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = " +
                        "classOf[AcceptTopicSuggestionPayloadDeserializer])"));
76
        // interface
77 78
        customAnnotationsMapping.put("Actor", singletonList(
                "@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = classOf[ActorDeserializer])"));
79
        // union
80 81
        customAnnotationsMapping.put("Assignee", singletonList(
                "com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = classOf[AssigneeDeserializer])"));
82
        // enum
83 84 85
        customAnnotationsMapping.put("DeploymentOrderField", singletonList(
                "@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = " +
                        "classOf[DeploymentOrderFieldDeserializer])"));
86 87
        mappingConfig.setCustomAnnotationsMapping(customAnnotationsMapping);

88
        new ScalaGraphQLCodegen(singletonList("src/test/resources/schemas/github.graphqls"),
89 90 91 92
                outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate();

        File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
        assertFileContainsElements(files, "AcceptTopicSuggestionInput.scala",
93 94
                "@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = " +
                        "classOf[AcceptTopicSuggestionInputDeserializer])\n" +
95 96
                        "case class AcceptTopicSuggestionInput");
        assertFileContainsElements(files, "AcceptTopicSuggestionPayload.scala",
97 98
                "@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = " +
                        "classOf[AcceptTopicSuggestionPayloadDeserializer])\n" +
99 100
                        "case class AcceptTopicSuggestionPayload");
        assertFileContainsElements(files, "Actor.scala",
101 102
                "@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = " +
                        "classOf[ActorDeserializer])\n" +
103 104
                        "trait Actor");
        assertFileContainsElements(files, "Assignee.scala",
105 106
                "@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = " +
                        "classOf[AssigneeDeserializer])\n" +
107 108
                        "trait Assignee");
        assertFileContainsElements(files, "DeploymentOrderField.scala",
109 110
                "@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = " +
                        "classOf[DeploymentOrderFieldDeserializer])\n" +
111 112 113 114 115 116 117 118
                        "object DeploymentOrderField extends Enumeration");
    }

    @Test
    void generate_CustomAnnotationMappings_Multiple() throws Exception {
        Map<String, List<String>> customAnnotationsMapping = new HashMap<>();
        // type
        customAnnotationsMapping.put("AcceptTopicSuggestionPayload", Arrays.asList(
119 120 121 122
                "@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])"));
123 124
        mappingConfig.setCustomAnnotationsMapping(customAnnotationsMapping);

125
        new ScalaGraphQLCodegen(singletonList("src/test/resources/schemas/github.graphqls"),
126 127 128
                outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate();

        File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
129 130 131 132 133 134
        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");
135 136 137 138 139 140
    }

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

151
        new ScalaGraphQLCodegen(singletonList("src/test/resources/schemas/github.graphqls"),
152 153 154 155
                outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate();

        File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
        assertFileContainsElements(files, "CodeOfConductQueryRequest.scala",
156 157
                "@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = " +
                        "classOf[CodeOfConductQueryRequestDeserializer])\n" +
158 159
                        "class CodeOfConductQueryRequest");
        assertFileContainsElements(files, "CodeOfConductQueryResponse.scala",
160 161 162 163
                "@com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = " +
                        "classOf[CodeOfConductQueryResponseDeserializer])\n" +
                        "class CodeOfConductQueryResponse extends GraphQLResult[JMap[String, " +
                        "CodeOfConduct]]");
164 165 166 167 168 169
    }

    @Test
    void generate_Directives() throws Exception {
        Map<String, List<String>> directiveAnnotationsMapping = new HashMap<>();
        directiveAnnotationsMapping.put("auth",
170 171 172 173
                singletonList(
                        "@com.example.CustomAnnotation(roles={{roles?toArray}}, " +
                                "boo={{boo?toArray}}, float={{float?toArrayOfStrings}}, int={{int}}, " +
                                "n={{n?toString}})"));
174 175
        directiveAnnotationsMapping.put("relationship",
                singletonList("@com.example.Relationship(type = {{type}}, direction = {{direction}})"));
176 177
        mappingConfig.setDirectiveAnnotationsMapping(directiveAnnotationsMapping);

178
        new ScalaGraphQLCodegen(singletonList("src/test/resources/schemas/test.graphqls"),
179 180 181 182 183 184 185 186 187
                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"));
188 189 190
        assertSameTrimmedContent(
                new File("src/test/resources/expected-classes/scala/annotation/User.scala.txt"),
                getFileByName(files, "User.scala"));
191 192 193
    }

}