Skip to content

Commit

Permalink
Fix issues found after generation Java templates
Browse files Browse the repository at this point in the history
  • Loading branch information
wnederhof committed Apr 13, 2024
1 parent e90aafd commit faadc4c
Show file tree
Hide file tree
Showing 17 changed files with 135 additions and 107 deletions.
12 changes: 8 additions & 4 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func Run(args []string) error {
Name: "backend",
Aliases: []string{"b"},
},
&cli.StringFlag{
Name: "frontend",
Aliases: []string{"f"},
},
},
Action: func(c *cli.Context) error {
if c.Args().Len() != 2 {
Expand All @@ -43,7 +47,7 @@ func Run(args []string) error {
if frontend == "" {
frontend = "react"
}
return generator.GenerateNewProject(c.Args().Get(0), c.Args().Get(1), backend)
return generator.GenerateNewProject(c.Args().Get(0), c.Args().Get(1), backend, frontend)
},
},
{
Expand Down Expand Up @@ -138,7 +142,7 @@ func Run(args []string) error {
{
Name: "backend:auth",
Aliases: []string{"ba"},
Usage: "Backend Authentication - EXPERIMENTAL",
Usage: "Backend Authentication",
Action: func(c *cli.Context) error {
model, err := parseAttributeArgs(c.Args(), 0)
if err != nil {
Expand All @@ -150,7 +154,7 @@ func Run(args []string) error {
{
Name: "frontend:auth",
Aliases: []string{"fa"},
Usage: "Frontend Authentication - EXPERIMENTAL",
Usage: "Frontend Authentication",
Action: func(c *cli.Context) error {
model, err := parseAttributeArgs(c.Args(), 0)
if err != nil {
Expand All @@ -162,7 +166,7 @@ func Run(args []string) error {
{
Name: "auth",
Aliases: []string{"a"},
Usage: "Authentication - EXPERIMENTAL",
Usage: "Authentication",
Action: func(c *cli.Context) error {
model, err := parseAttributeArgs(c.Args(), 0)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/generator/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ var (
func provideProjectContextAttributes(context map[string]interface{}, properties Properties) {
context["groupId"] = properties.GroupId
context["artifactId"] = properties.ArtifactId
context["backend"] = properties.Backend
context["frontend"] = properties.Frontend

context["groupIdSlashes"] = regexDot.ReplaceAllString(properties.GroupId, "/")
context["artifactIdSlashes"] = regexDot.ReplaceAllString(properties.ArtifactId, "/")
Expand Down
11 changes: 8 additions & 3 deletions pkg/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ type Properties struct {
ArtifactId string `yaml:",omitempty"`
GroupId string `yaml:",omitempty"`
Backend string `yaml:",omitempty"`
Frontend string `yaml:",omitempty"`
}

func GenerateNewProject(groupId string, artifactId string, backend string) error {
func GenerateNewProject(groupId string, artifactId string, backend string, frontend string) error {
projectName := artifactId
context := make(map[string]interface{})
properties := Properties{
GroupId: groupId,
ArtifactId: artifactId,
Backend: backend,
Frontend: frontend,
}
err := os.MkdirAll(projectName, os.ModePerm)
if err != nil {
Expand Down Expand Up @@ -222,7 +224,10 @@ func writeProperties(properties Properties, projectName string) error {
func writeFiles(sourceDirectory string, targetDirectoryTemplate string, context map[string]interface{}, overwrite bool) error {
// Replace /templates/[language] with /templates/java
sourceDirectory = substitutePathParamsAndRemovePeb(sourceDirectory, context)
return writeFilesNoRetemplate(sourceDirectory, targetDirectoryTemplate, context, overwrite)
}

func writeFilesNoRetemplate(sourceDirectory string, targetDirectoryTemplate string, context map[string]interface{}, overwrite bool) error {
err := createDirectoryUsingTemplate(targetDirectoryTemplate, context)
if err != nil {
return err
Expand All @@ -232,7 +237,7 @@ func writeFiles(sourceDirectory string, targetDirectoryTemplate string, context
sourceFile := sourceDirectory + "/" + e.Name()
targetFileTemplate := targetDirectoryTemplate + "/" + e.Name()
if e.IsDir() {
err := writeFiles(sourceFile, targetFileTemplate, context, overwrite)
err := writeFilesNoRetemplate(sourceFile, targetFileTemplate, context, overwrite)
if err != nil {
return err
}
Expand Down Expand Up @@ -279,14 +284,14 @@ func writeFileFromTemplate(sourceFile string, targetFileTemplate string, context
if err != nil {
return err
}
println("[G] " + targetFile)
contents, err := substituteFile(string(templateContents), context)
if err != nil {
return err
}
if strings.TrimSpace(contents) == "" {
return nil
}
println("[G] " + targetFile)
return os.WriteFile(targetFile, []byte(contents), os.FileMode.Perm(0644))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import lombok.Builder;
import lombok.Data;
import lombok.NonNull;
import lombok.With;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.relational.core.mapping.Table;

import java.time.LocalDateTime;
Expand All @@ -15,11 +18,10 @@ import java.time.LocalDateTime;
public class {{ namePascalCase }}Entity {
@Id
private Integer id;{%for field in fields%}
@NonNull
private {{ field.fieldJavaType }} {{ field.fieldNameCamelCase }};{%endfor%}

@CreatedDate
private LocalDateTime createdAt;

@LastModifiedDate
private LocalDateTime LocalDateTime;
private LocalDateTime updatedAt;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {{ groupId }}.{{ artifactId }}.domain.{{ nameLowerCase }}.{{ namePascalCa
import {{ groupId }}.{{ artifactId }}.domain.{{ nameLowerCase }}.{{ namePascalCase }}Service;
import lombok.RequiredArgsConstructor;
import org.dataloader.MappedBatchLoader;

import java.util.List;
{%if hasRelations%}
import java.util.List;{%endif%}
import java.util.function.Function;
import java.util.stream.Collectors;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ import com.netflix.graphql.dgs.DgsQuery;
import com.netflix.graphql.dgs.DgsMutation;{%if hasRelations%}
import com.netflix.graphql.dgs.DgsDataFetchingEnvironment;{%endif%}
import lombok.AllArgsConstructor;

import java.util.Objects;
import java.util.Optional;
{%if hasRelations%}
import java.util.Objects;{%endif%}
import java.util.Optional;{%if hasRelations%}
import java.util.concurrent.CompletableFuture;

import static java.util.Collections.emptyList;
import static java.util.Collections.emptyList;{%endif%}

@DgsComponent
@AllArgsConstructor
class {{ namePascalCase }}Resolver {
private {{ namePascalCase }}Service {{ nameCamelCase }}Service;

@DgsQuery
Optional<{{ nameCamelCase }}>(int id) {
Optional<{{ namePascalCase }}> {{ nameCamelCase }}(int id) {
return {{ nameCamelCase }}Service.findById(id);
}

Expand All @@ -35,12 +35,12 @@ class {{ namePascalCase }}Resolver {
}

@DgsMutation
Optional<{{ namePascalCase }}> create{{ namePascalCase }}(Create{{ namePascalCase }}Input input) {
{{ namePascalCase }} create{{ namePascalCase }}(Create{{ namePascalCase }}Input input) {
return {{ nameCamelCase }}Service.create(input);
}

@DgsMutation
Optional<{{ namePascalCase }}> update{{ namePascalCase }}(id: Int, input: Update{{ namePascalCase }}Input) {
Optional<{{ namePascalCase }}> update{{ namePascalCase }}(int id, Update{{ namePascalCase }}Input input) {
return {{ nameCamelCase }}Service.update(id, input);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package {{ groupId }}.{{ artifactId }}.application.graphql.{{ nameLowerCase }}
package {{ groupId }}.{{ artifactId }}.application.graphql.{{ nameLowerCase }};

import com.netflix.graphql.dgs.DgsQueryExecutor;
import com.netflix.graphql.dgs.autoconfig.DgsAutoConfiguration;
Expand All @@ -12,14 +12,14 @@ import static {{ groupId }}.{{ artifactId }}.domain.{{ nameLowerCase }}.{{ nameP
import static {{ groupId }}.{{ artifactId }}.domain.{{ nameLowerCase }}.{{ namePascalCase }}Fixtures.UPDATE_{{ nameScreamingSnakeCase }}_FIXTURE_1;
import {{ groupId }}.{{ artifactId }}.domain.{{ nameLowerCase }}.{{ namePascalCase }}Service;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.Optional;{%if hasRelations%}
import java.util.Set;{%endif%}

import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -50,11 +50,11 @@ class {{ namePascalCase }}ResolverTests {
private {{ namePascalCase }}Service {{ nameCamelCase }}Service;

@Test
fun {{ namePluralCamelCase }}() {
void {{ namePluralCamelCase }}() {
when({{ nameCamelCase }}Service.findAll())
.thenReturn(singletonList({{ nameScreamingSnakeCase }}_FIXTURE_WITH_ID_1));

var ids = dgsQueryExecutor.<String>executeAndExtractJsonPath<List<String>>(
var ids = dgsQueryExecutor.<List<String>>executeAndExtractJsonPath(
"{ {{ namePluralCamelCase }} { id } }",
"data.{{ namePluralCamelCase }}[*].id"
);
Expand All @@ -63,16 +63,16 @@ class {{ namePascalCase }}ResolverTests {
}

@Test
fun create{{ namePascalCase }}_delegates_the_call_to_the_services_create() {
void create{{ namePascalCase }}_delegates_the_call_to_the_services_create() {
when({{ nameCamelCase }}Service.create(any()))
.thenReturn({{ nameScreamingSnakeCase }}_FIXTURE_WITH_ID_1);

var create{{ namePascalCase }}Input = Map.of({%for field in fields%}
"{{ field.fieldNameCamelCase }}", {{ field.fieldKotlinTestDummyValue }}{%if field.isNotLast%},{%endif%}{%endfor%}
);

var result = dgsQueryExecutor.<String>executeAndExtractJsonPath<String>(
"mutation create{{ namePascalCase }}(\$input: Create{{ namePascalCase }}Input!) { create{{ namePascalCase }}(input: \$input) { id } }",
var result = dgsQueryExecutor.<String>executeAndExtractJsonPath(
"mutation create{{ namePascalCase }}($input: Create{{ namePascalCase }}Input!) { create{{ namePascalCase }}(input: $input) { id } }",
"data.create{{ namePascalCase }}.id",
Map.of("input", create{{ namePascalCase }}Input)
);
Expand All @@ -85,14 +85,14 @@ class {{ namePascalCase }}ResolverTests {
@Test
void update{{ namePascalCase }}_delegates_the_call_to_the_services_update() {
when({{ nameCamelCase }}Service.update(anyInt(), any()))
.thenReturn({{ nameScreamingSnakeCase }}_FIXTURE_WITH_ID_1);
.thenReturn(Optional.of({{ nameScreamingSnakeCase }}_FIXTURE_WITH_ID_1));

var update{{ namePascalCase }}Input = Map.of({%for field in fields%}
"{{ field.fieldNameCamelCase }}", {{ field.fieldKotlinTestDummyValue }}{%if field.isNotLast%},{%endif%}{%endfor%}
);

var result = dgsQueryExecutor.<String>executeAndExtractJsonPath<String>(
"mutation update{{ namePascalCase }}(\$id: ID!, \$input: Update{{ namePascalCase }}Input!) { update{{ namePascalCase }}(id: \$id, input: \$input) { id } }",
var result = dgsQueryExecutor.<String>executeAndExtractJsonPath(
"mutation update{{ namePascalCase }}($id: ID!, $input: Update{{ namePascalCase }}Input!) { update{{ namePascalCase }}(id: $id, input: $input) { id } }",
"data.update{{ namePascalCase }}.id",
Map.of("id", {{ nameScreamingSnakeCase }}_FIXTURE_WITH_ID_1.getId(), "input", update{{ namePascalCase }}Input)
);
Expand All @@ -107,8 +107,8 @@ class {{ namePascalCase }}ResolverTests {
when({{ nameCamelCase }}Service.deleteById(any()))
.thenReturn(true);

val result = dgsQueryExecutor.<String>executeAndExtractJsonPath<Boolean>(
"mutation delete{{ namePascalCase }}(\$id: ID!) { delete{{ namePascalCase }}(id: \$id) }",
var result = dgsQueryExecutor.<Boolean>executeAndExtractJsonPath(
"mutation delete{{ namePascalCase }}($id: ID!) { delete{{ namePascalCase }}(id: $id) }",
"data.delete{{ namePascalCase }}",
Map.of("id", 1)
);
Expand All @@ -123,10 +123,10 @@ class {{ namePascalCase }}ResolverTests {
when({{ nameCamelCase }}Service.findAll())
.thenReturn(singletonList({{ nameScreamingSnakeCase }}_FIXTURE_WITH_ID_1));

when({{ field.fieldTypeCamelCase }}Service.findByIds(setOf({{ nameScreamingSnakeCase }}_FIXTURE_WITH_ID_1.{{ field.fieldNameCamelCase }})))
when({{ field.fieldTypeCamelCase }}Service.findByIds(Set.of({{ nameScreamingSnakeCase }}_FIXTURE_WITH_ID_1.{{ field.fieldNameCamelCase }})))
.thenReturn(singletonList({{ field.fieldTypeScreamingSnakeCase }}_FIXTURE_WITH_ID_1.copy(id = {{ nameScreamingSnakeCase }}_FIXTURE_WITH_ID_1.{{ field.fieldNameCamelCase }})));

val ids = dgsQueryExecutor.<String>executeAndExtractJsonPath<List<String>>(
var ids = dgsQueryExecutor.<List<String>>executeAndExtractJsonPath(
"{ {{ namePluralCamelCase }} { {{ field.fieldTypeCamelCase }} { id } } }",
"data.{{ namePluralCamelCase }}[*].{{ field.fieldTypeCamelCase }}.id"
);
Expand All @@ -139,10 +139,10 @@ class {{ namePascalCase }}ResolverTests {
when({{ field.fieldTypeCamelCase }}Service.findAll())
.thenReturn(singletonList({{ field.fieldTypeScreamingSnakeCase }}_FIXTURE_WITH_ID_1.copy(id = {{ nameScreamingSnakeCase }}_FIXTURE_WITH_ID_1.{{ field.fieldNameCamelCase }})));

when({{ nameCamelCase }}Service.findBy{{ field.fieldNamePluralPascalCase }}(setOf({{ nameScreamingSnakeCase }}_FIXTURE_WITH_ID_1.{{ field.fieldNameCamelCase }})))
when({{ nameCamelCase }}Service.findBy{{ field.fieldNamePluralPascalCase }}(Set.of({{ nameScreamingSnakeCase }}_FIXTURE_WITH_ID_1.{{ field.fieldNameCamelCase }})))
.thenReturn(singletonList({{ nameScreamingSnakeCase }}_FIXTURE_WITH_ID_1));

val ids = dgsQueryExecutor.<String>executeAndExtractJsonPath<List<String>>(
var ids = dgsQueryExecutor.<List<String>>executeAndExtractJsonPath(
"{ {{ field.fieldTypePluralCamelCase }} { {{ namePluralCamelCase }} { id } } }",
"data.{{ field.fieldTypePluralCamelCase }}[*].{{ namePluralCamelCase }}[*].id"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down Expand Up @@ -128,12 +133,20 @@
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package {{ groupId }}.{{ artifactId }}
package {{ groupId }}.{{ artifactId }};

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import org.testcontainers.junit.jupiter.Testcontainers;
@SpringBootTest
@Testcontainers
@Transactional // Rolls back transaction after each test.
abstract class IntegrationTestContext {
public abstract class IntegrationTestContext {
private static final PostgreSQLContainer<?> POSTGRES_CONTAINER = new PostgreSQLContainer("postgres:14-alpine")
.withDatabaseName("foo")
.withUsername("foo")
.withPassword("secret");
.withDatabaseName("foo")
.withUsername("foo")
.withPassword("secret");

static {
POSTGRES_CONTAINER.start();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package {{ groupId }}.{{ artifactId }}.domain.{{ nameLowerCase }};

import lombok.Getter;
import org.springframework.context.ApplicationEvent
import org.springframework.context.ApplicationEvent;

public abstract class {{ namePascalCase }}Event(source: Any) : ApplicationEvent(source) {
import java.util.List;

public abstract class {{ namePascalCase }}Event extends ApplicationEvent {
{{ namePascalCase }}Event(Object source) {
super(source);
}

@Getter
public static class {{ namePluralPascalCase }}CreatedEvent extends {{ namePascalCase }}Event {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{%if hasRelations%}package {{ groupId }}.{{ artifactId }}.domain.{{ nameLowerCase }};
{%for field in fields%}{%if field.isFieldRelational%}
import {{ groupId }}.{{ artifactId }}.domain.{{ field.fieldTypeLowerCase }}.{{ field.fieldTypePascalCase }};{%endif%}{%endfor%}
import {{ groupId }}.{{ artifactId }}.domain.{{ field.fieldTypeLowerCase }}.{{ field.fieldTypePascalCase }};
import {{ groupId }}.{{ artifactId }}.domain.{{ field.fieldTypeLowerCase }}.{{ field.fieldTypePascalCase }}Event.{{ field.fieldTypePluralPascalCase }}DeletedEvent;{%endif%}{%endfor%}
import lombok.AllArgsConstructor;
import org.springframework.context.event.EventListener;
Expand Down
Loading

0 comments on commit faadc4c

Please sign in to comment.