Skip to content

Commit

Permalink
Finish converting to Java
Browse files Browse the repository at this point in the history
  • Loading branch information
wnederhof committed Apr 13, 2024
1 parent faadc4c commit e0c4f6c
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 42 deletions.
24 changes: 12 additions & 12 deletions pkg/generator/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ func provideJavaFieldContextAttributes(context map[string]interface{}, attribute
context["fieldJavaTypeNotNullable"] = "String"
context["fieldJavaTestDummyValue"] = "\"Some " + attribute.Name + "\""
case INT:
context["fieldJavaType"] = "Int"
context["fieldJavaTypeNotNullable"] = "Int"
context["fieldJavaType"] = "Integer"
context["fieldJavaTypeNotNullable"] = "Integer"
context["fieldJavaTestDummyValue"] = "1"
case TEXT:
context["fieldJavaAnnotations"] = "@Lob"
Expand All @@ -255,33 +255,33 @@ func provideJavaFieldContextAttributes(context map[string]interface{}, attribute
context["fieldJavaTypeNotNullable"] = "LocalDate"
context["fieldJavaTestDummyValue"] = "LocalDate.of(2000, 1, 1)"
case BOOLEAN:
context["fieldJavaType"] = "Boolean"
context["fieldJavaTypeNotNullable"] = "Boolean"
context["fieldJavaType"] = "boolean"
context["fieldJavaTypeNotNullable"] = "boolean"
context["fieldJavaTestDummyValue"] = "true"
case NULL_STRING:
context["fieldJavaType"] = "String?"
context["fieldJavaType"] = "String"
context["fieldJavaTypeNotNullable"] = "String"
context["fieldJavaTestDummyValue"] = "\"Some " + attribute.Name + "\""
case NULL_INT:
context["fieldJavaType"] = "Int?"
context["fieldJavaTypeNotNullable"] = "Int"
context["fieldJavaType"] = "Integer"
context["fieldJavaTypeNotNullable"] = "Integer"
context["fieldJavaTestDummyValue"] = "1"
case NULL_TEXT:
context["fieldJavaAnnotations"] = "@Lob"
context["fieldJavaType"] = "String?"
context["fieldJavaType"] = "String"
context["fieldJavaTypeNotNullable"] = "String"
context["fieldJavaTestDummyValue"] = "\"Some " + attribute.Name + "\""
case NULL_DATE:
context["fieldJavaType"] = "LocalDate?"
context["fieldJavaType"] = "LocalDate"
context["fieldJavaTypeNotNullable"] = "LocalDate"
context["fieldJavaTestDummyValue"] = "LocalDate.of(2000, 1, 1)"
case NULL_BOOLEAN:
context["fieldJavaType"] = "Boolean?"
context["fieldJavaType"] = "Boolean"
context["fieldJavaTypeNotNullable"] = "Boolean"
context["fieldJavaTestDummyValue"] = "true"
case RELATIONAL:
context["fieldJavaType"] = "Int"
context["fieldJavaTypeNotNullable"] = "Int"
context["fieldJavaType"] = "Integer"
context["fieldJavaTypeNotNullable"] = "Integer"
context["fieldJavaTestDummyValue"] = "10"
default:
panic("Undetermined attribute type.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.springframework.data.repository.query.Param;

import java.util.List;

interface {{ namePascalCase }}Repository extends CrudRepository<{{ namePascalCase }}Entity, Integer> {
public interface {{ namePascalCase }}Repository extends CrudRepository<{{ namePascalCase }}Entity, Integer> {

@Query("SELECT * FROM {{ namePluralSnakeCase }} WHERE id IN (:ids)")
List<{{ namePascalCase }}Entity> findByIds(@Param("ids") Iterable<Integer> ids);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import static java.util.concurrent.CompletableFuture.supplyAsync;

@DgsComponent
@RequiredArgsConstructor
class {{ namePascalCase }}DataLoader {
public class {{ namePascalCase }}DataLoader {
private {{ namePascalCase }}Service {{ nameCamelCase }}Service;

@DgsDataLoader(name = "{{ nameCamelCase }}ById")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import static java.util.Collections.emptyList;{%endif%}

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

@DgsQuery
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,31 +123,33 @@ class {{ namePascalCase }}ResolverTests {
when({{ nameCamelCase }}Service.findAll())
.thenReturn(singletonList({{ nameScreamingSnakeCase }}_FIXTURE_WITH_ID_1));

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 }})));
when({{ field.fieldTypeCamelCase }}Service.findByIds(Set.of({{ nameScreamingSnakeCase }}_FIXTURE_WITH_ID_1.get{{ field.fieldNamePascalCase }}())))
.thenReturn(singletonList({{ field.fieldTypeScreamingSnakeCase }}_FIXTURE_WITH_ID_1
.withId({{ nameScreamingSnakeCase }}_FIXTURE_WITH_ID_1.get{{ field.fieldNamePascalCase }}())));

var ids = dgsQueryExecutor.<List<String>>executeAndExtractJsonPath(
"{ {{ namePluralCamelCase }} { {{ field.fieldTypeCamelCase }} { id } } }",
"data.{{ namePluralCamelCase }}[*].{{ field.fieldTypeCamelCase }}.id"
);

assertThat(ids).contains({{ nameScreamingSnakeCase }}_FIXTURE_WITH_ID_1.{{ field.fieldNameCamelCase }}.toString());
assertThat(ids).contains({{ nameScreamingSnakeCase }}_FIXTURE_WITH_ID_1.get{{ field.fieldNamePascalCase }}().toString());
}

@Test
void {{ field.fieldTypePluralCamelCase }}_{{ namePluralCamelCase }}() {
when({{ field.fieldTypeCamelCase }}Service.findAll())
.thenReturn(singletonList({{ field.fieldTypeScreamingSnakeCase }}_FIXTURE_WITH_ID_1.copy(id = {{ nameScreamingSnakeCase }}_FIXTURE_WITH_ID_1.{{ field.fieldNameCamelCase }})));
.thenReturn(singletonList({{ field.fieldTypeScreamingSnakeCase }}_FIXTURE_WITH_ID_1
.withId({{ nameScreamingSnakeCase }}_FIXTURE_WITH_ID_1.get{{ field.fieldNamePascalCase }}())));

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

var ids = dgsQueryExecutor.<List<String>>executeAndExtractJsonPath(
"{ {{ field.fieldTypePluralCamelCase }} { {{ namePluralCamelCase }} { id } } }",
"data.{{ field.fieldTypePluralCamelCase }}[*].{{ namePluralCamelCase }}[*].id"
);

assertThat(ids).contains({{ nameScreamingSnakeCase }}_FIXTURE_WITH_ID_1.id.toString());
assertThat(ids).contains("" + {{ nameScreamingSnakeCase }}_FIXTURE_WITH_ID_1.getId());
}
{%endif%}{%endfor%}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public abstract class {{ namePascalCase }}Event extends ApplicationEvent {
public static class {{ namePluralPascalCase }}CreatedEvent extends {{ namePascalCase }}Event {
private final List<{{ namePascalCase }}> entities;

{{ namePluralPascalCase }}CreatedEvent(Object source, List<{{ namePascalCase }}> entities) {
public {{ namePluralPascalCase }}CreatedEvent(Object source, List<{{ namePascalCase }}> entities) {
super(source);
this.entities = entities;
}
Expand All @@ -24,7 +24,7 @@ public abstract class {{ namePascalCase }}Event extends ApplicationEvent {
public static class {{ namePluralPascalCase }}UpdatedEvent extends {{ namePascalCase }}Event {
private final List<{{ namePascalCase }}> entities;

{{ namePluralPascalCase }}UpdatedEvent(Object source, List<{{ namePascalCase }}> entities) {
public {{ namePluralPascalCase }}UpdatedEvent(Object source, List<{{ namePascalCase }}> entities) {
super(source);
this.entities = entities;
}
Expand All @@ -34,7 +34,7 @@ public abstract class {{ namePascalCase }}Event extends ApplicationEvent {
public static class {{ namePluralPascalCase }}DeletedEvent extends {{ namePascalCase }}Event {
private final List<{{ namePascalCase }}> entities;

{{ namePluralPascalCase }}DeletedEvent(Object source, List<{{ namePascalCase }}> entities) {
public {{ namePluralPascalCase }}DeletedEvent(Object source, List<{{ namePascalCase }}> entities) {
super(source);
this.entities = entities;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import lombok.AllArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.stream.Collectors;

@Component
@AllArgsConstructor
class {{ namePascalCase }}Listener {
private {{ namePascalCase }}Service {{ nameCamelCase }}Service;

{%for field in fields%}{%if field.isFieldRelational%}
@EventListener
void onEvent({{ field.fieldTypePluralPascalCase }}DeletedEvent event) {
var entityIds = event.getEntities().stream()
.map({{ fieldTypePascalCase }}::getId)
.map({{ field.fieldTypePascalCase }}::getId)
.collect(Collectors.toList());
{{ nameCamelCase }}Service.deleteBy{{ field.fieldNamePluralPascalCase }}(entityIds);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class {{ namePascalCase }}Service {
{%endif%}
public List<{{ namePascalCase }}> findAll({%if hasRelations%}{{ namePascalCase }}FilterInput filterInput{%endif%}) { {%for field in fields%}{%if field.isFieldRelational%}
if (filterInput.get{{ field.fieldNamePascalCase }}() != null) {
return {{ nameCamelCase }}Repository.findBy{{ field.fieldNamePluralPascalCase }}(listOf(filterInput.{{ field.fieldNameCamelCase }}))
return {{ nameCamelCase }}Repository.findBy{{ field.fieldNamePluralPascalCase }}(singletonList(filterInput.{{ field.fieldNameCamelCase }})).stream()
.map(this::toDomainObject)
.collect(Collectors.toList());
}
Expand All @@ -64,7 +64,7 @@ public class {{ namePascalCase }}Service {
}

public {{ namePascalCase }} create(Create{{ namePascalCase }}Input input) { {%for field in fields%}{%if field.isFieldRelational%}
Objects.requireNonNull({{ field.fieldTypeCamelCase }}Service.findById(input.get{{ field.fieldNamePascalCase }}()).getOrElse(null));
Objects.requireNonNull({{ field.fieldTypeCamelCase }}Service.findById(input.get{{ field.fieldNamePascalCase }}()).orElse(null));
{%endif%}{%endfor%}
var {{ nameCamelCase }}Entity = {{ namePascalCase }}Entity.builder(){%for field in fields%}
.{{field.fieldNameCamelCase}}(input.get{{field.fieldNamePascalCase}}()){%endfor%}
Expand All @@ -76,7 +76,7 @@ public class {{ namePascalCase }}Service {

public Optional<{{ namePascalCase }}> update(int id, Update{{ namePascalCase }}Input input) { {%for field in fields%}{%if field.isFieldRelational%}
if (input.{{ field.fieldNameCamelCase }} != null) {
Objects.requireNonNull({{ field.fieldTypeCamelCase }}Service.findById(input.get{{ field.fieldNamePascalCase }}()).getOrElse(null));
Objects.requireNonNull({{ field.fieldTypeCamelCase }}Service.findById(input.get{{ field.fieldNamePascalCase }}()).orElse(null));
}
{%endif%}{%endfor%}
var {{ nameCamelCase }}Entity = {{ nameCamelCase }}Repository.findById(id);
Expand All @@ -103,7 +103,7 @@ public class {{ namePascalCase }}Service {
return true;
}
{%for field in fields%}{%if field.isFieldRelational%}
public boolean deleteBy{{ field.fieldNamePluralPascalCase }}({{ field.fieldNamePluralCamelCase }}: List<Int>) {
public boolean deleteBy{{ field.fieldNamePluralPascalCase }}(List<Integer> {{ field.fieldNamePluralCamelCase }}) {
var {{ nameCamelCase }}Entities = {{ nameCamelCase }}Repository.findBy{{ field.fieldNamePluralPascalCase }}({{ field.fieldNamePluralCamelCase }});
if ({{ nameCamelCase }}Entities.isEmpty()) {
return false;
Expand Down Expand Up @@ -145,7 +145,6 @@ public class {{ namePascalCase }}Service {
@With
@Builder
public static class {{ namePascalCase }}FilterInput {{ "{" }}{%for field in fields%}{%if field.isFieldRelational%}
@NonNull
private {{ field.fieldJavaType }} {{ field.fieldNameCamelCase }};{%endif%}{%endfor%}
{{ "}" }}{%endif%}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public class {{ namePascalCase }}Fixtures {
.{{ field.fieldNameCamelCase }}({{ field.fieldKotlinTestDummyValue }}){%endif%}{%endfor%}
.build();

static final {{ namePascalCase }}Entity {{ nameScreamingSnakeCase }}_ENTITY_FIXTURE_1 =
public static final {{ namePascalCase }}Entity {{ nameScreamingSnakeCase }}_ENTITY_FIXTURE_1 =
{{ namePascalCase }}Entity.builder()
.id(null){%for field in fields%}
.{{ field.fieldNameCamelCase }}({{ field.fieldKotlinTestDummyValue }}){%endfor%}
.build();

static final {{ namePascalCase }}Entity {{ nameScreamingSnakeCase }}_ENTITY_FIXTURE_WITH_ID_1 =
public static final {{ namePascalCase }}Entity {{ nameScreamingSnakeCase }}_ENTITY_FIXTURE_WITH_ID_1 =
{{ nameScreamingSnakeCase }}_ENTITY_FIXTURE_1
.withId(1)
.withCreatedAt(LOCAL_DATE_TIME_FIXTURE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class {{ namePascalCase }}IntegrationTests extends IntegrationTestContext {
assertThat({{ nameCamelCase }}Service.findById({{ nameCamelCase }}.getId())).isNotNull();
assertThat({{ nameCamelCase }}Service.findByIds(singletonList({{ nameCamelCase }}.getId()))).isNotEmpty();
{%for field in fields%}{%if field.isFieldRelational%}
assertThat({{ nameCamelCase }}Service.findAll({{ namePascalCase }}Service.{{ namePascalCase }}FilterInput({{ field.fieldTypeCamelCase }}.getId()))).isNotEmpty();
assertThat({{ nameCamelCase }}Service.findAll(new {{ namePascalCase }}Service.{{ namePascalCase }}FilterInput({{ field.fieldTypeCamelCase }}.getId()))).isNotEmpty();
assertThat({{ nameCamelCase }}Service.findBy{{ field.fieldNamePluralPascalCase }}(singletonList({{ field.fieldTypeCamelCase }}.getId()))).isNotEmpty();
{%endif%}{%endfor%}
{{ nameCamelCase }}Service.deleteById({{ nameCamelCase }}.getId());
Expand All @@ -58,7 +58,7 @@ class {{ namePascalCase }}IntegrationTests extends IntegrationTestContext {
var {{ nameCamelCase }} = {{ nameCamelCase }}Service.create(CREATE_{{ nameScreamingSnakeCase }}_FIXTURE_1{%for field in fields%}{%if field.isFieldRelational%}
.with{{ field.fieldNamePascalCase }}({{ field.fieldTypeCamelCase }}.getId()){%endif%}{%endfor%});

{{ field.fieldTypeCamelCase }}Service.deleteById({{ nameCamelCase }}.{{ field.fieldNameCamelCase }});
{{ field.fieldTypeCamelCase }}Service.deleteById({{ nameCamelCase }}.get{{ field.fieldNamePascalCase }}());

assertThat({{ nameCamelCase }}Service.findAll()).isEmpty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class {{ namePascalCase }}ListenerUnitTests{%if hasRelations%}{
{%for field in fields%}{%if field.isFieldRelational%}
@Test
void when_{{ field.fieldTypePluralPascalCase }}DeletedEvent_is_triggered_related_{{ namePluralPascalCase }}_are_deleted() {
{{ nameCamelCase }}Listener.onEvent({{ field.fieldTypePluralPascalCase }}DeletedEvent(this, singletonList({{ field.fieldTypeScreamingSnakeCase }}_FIXTURE_WITH_ID_1)));
{{ nameCamelCase }}Listener.onEvent(new {{ field.fieldTypePluralPascalCase }}DeletedEvent(this, singletonList({{ field.fieldTypeScreamingSnakeCase }}_FIXTURE_WITH_ID_1)));

verify({{ nameCamelCase }}Service).deleteBy{{ field.fieldNamePluralPascalCase }}(singletonList({{ field.fieldTypeScreamingSnakeCase }}_FIXTURE_WITH_ID_1.getId()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package {{ groupId }}.{{ artifactId }}.domain.{{ nameLowerCase }};

{%for field in fields%}{%if field.isFieldRelational%}
import static {{ groupId }}.{{ artifactId }}.domain.{{ field.fieldTypeLowerCase }}.{{ field.fieldTypePascalCase }}Service;{%endif%}{%endfor%}
import {{ groupId }}.{{ artifactId }}.domain.{{ field.fieldTypeLowerCase }}.{{ field.fieldTypePascalCase }}Service;{%endif%}{%endfor%}
import static {{ groupId }}.{{ artifactId }}.domain.{{ nameLowerCase }}.{{ namePascalCase }}Fixtures.{{ nameScreamingSnakeCase }}_ENTITY_FIXTURE_1;
import static {{ groupId }}.{{ artifactId }}.domain.{{ nameLowerCase }}.{{ namePascalCase }}Fixtures.{{ nameScreamingSnakeCase }}_ENTITY_FIXTURE_WITH_ID_1;
import static {{ groupId }}.{{ artifactId }}.domain.{{ nameLowerCase }}.{{ namePascalCase }}Fixtures.CREATE_{{ nameScreamingSnakeCase }}_FIXTURE_1;
Expand Down Expand Up @@ -98,7 +98,7 @@ class {{ namePascalCase }}ServiceUnitTests {
when({{ nameCamelCase }}Repository.findAll())
.thenReturn(singletonList({{ nameScreamingSnakeCase }}_ENTITY_FIXTURE_WITH_ID_1));

var actual = {{ nameCamelCase }}Service.findAll(new {{ namePascalCase }}Service.{{ namePascalCase }}FilterInput());
var actual = {{ nameCamelCase }}Service.findAll({{ namePascalCase }}Service.{{ namePascalCase }}FilterInput.builder().build());

assertThat(actual).containsExactly({{ nameScreamingSnakeCase }}_FIXTURE_WITH_ID_1);
}
Expand All @@ -108,7 +108,7 @@ class {{ namePascalCase }}ServiceUnitTests {
when({{ nameCamelCase }}Repository.findBy{{field.fieldNamePluralPascalCase}}(singletonList(1)))
.thenReturn(singletonList({{ nameScreamingSnakeCase }}_ENTITY_FIXTURE_WITH_ID_1));

var actual = {{ nameCamelCase }}Service.findAll(new {{ namePascalCase }}Service.{{ namePascalCase }}FilterInput({{field.fieldNameCamelCase}} = 1));
var actual = {{ nameCamelCase }}Service.findAll({{ namePascalCase }}Service.{{ namePascalCase }}FilterInput.builder().{{field.fieldNameCamelCase}}(1).build());

assertThat(actual).containsExactly({{ nameScreamingSnakeCase }}_FIXTURE_WITH_ID_1);
}
Expand All @@ -118,7 +118,7 @@ class {{ namePascalCase }}ServiceUnitTests {
when({{ nameCamelCase }}Repository.save({{ nameScreamingSnakeCase }}_ENTITY_FIXTURE_1))
.thenReturn({{ nameScreamingSnakeCase }}_ENTITY_FIXTURE_WITH_ID_1);
{%for field in fields%}{%if field.isFieldRelational%}
when({{ field.fieldTypeCamelCase }}Service.findById({{ nameScreamingSnakeCase }}_FIXTURE_WITH_ID_1.{{ field.fieldNameCamelCase }}))
when({{ field.fieldTypeCamelCase }}Service.findById({{ nameScreamingSnakeCase }}_FIXTURE_WITH_ID_1.get{{ field.fieldNamePascalCase }}()))
.thenReturn(Optional.of({{ field.fieldTypeScreamingSnakeCase }}_FIXTURE_WITH_ID_1));
{%endif%}{%endfor%}
var actual = {{ nameCamelCase }}Service.create(CREATE_{{ nameScreamingSnakeCase }}_FIXTURE_1);
Expand Down Expand Up @@ -146,8 +146,8 @@ class {{ namePascalCase }}ServiceUnitTests {
when({{ nameCamelCase }}Repository.save({{ nameScreamingSnakeCase }}_ENTITY_FIXTURE_WITH_ID_1))
.thenReturn({{ nameScreamingSnakeCase }}_ENTITY_FIXTURE_WITH_ID_1);
{%for field in fields%}{%if field.isFieldRelational%}
when({{ field.fieldTypeCamelCase }}Service.findById({{ nameScreamingSnakeCase }}_ENTITY_FIXTURE_WITH_ID_1.{{ field.fieldNameCamelCase }}))
.thenReturn({{ field.fieldTypeScreamingSnakeCase }}_FIXTURE_WITH_ID_1);
when({{ field.fieldTypeCamelCase }}Service.findById({{ nameScreamingSnakeCase }}_ENTITY_FIXTURE_WITH_ID_1.get{{ field.fieldNamePascalCase }}()))
.thenReturn(Optional.of({{ field.fieldTypeScreamingSnakeCase }}_FIXTURE_WITH_ID_1));
{%endif%}{%endfor%}
var actual = {{ nameCamelCase }}Service.update(1, UPDATE_{{ nameScreamingSnakeCase }}_FIXTURE_1);

Expand Down

0 comments on commit e0c4f6c

Please sign in to comment.