Skip to content

Commit

Permalink
fix(build): Update commons templates (#590)
Browse files Browse the repository at this point in the history
  • Loading branch information
juparog authored Nov 25, 2024
1 parent 25a194e commit ca8120d
Show file tree
Hide file tree
Showing 11 changed files with 261 additions and 11 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ repositories {
}

compileJava {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

test {
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/commons/object-mapper/definition.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"folders": [],
"files": {
"commons/object-mapper/object-mapper-config.java.mustache": "applications/app-service/src/main/java/{{packagePath}}/config/ObjectMapperConfig.java"
"commons/object-mapper/object-mapper-config.java.mustache": "applications/app-service/src/main/java/{{packagePath}}/config/ObjectMapperConfig.java",
"commons/object-mapper/object-mapper-config.test.java.mustache": "applications/app-service/src/test/java/{{packagePath}}/config/ObjectMapperConfigTest.java"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package {{package}}.config;

import org.junit.jupiter.api.Test;
import org.reactivecommons.utils.ObjectMapper;
import org.reactivecommons.utils.ObjectMapperImp;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

class ObjectMapperConfigTest {
@Test
void testObjectMapperBean() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ObjectMapperConfig.class);
ObjectMapper objectMapper = context.getBean(ObjectMapper.class);
assertNotNull(objectMapper);
assertTrue(objectMapper instanceof ObjectMapperImp);
context.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import java.time.Duration;

@Configuration
public class PostgreSQLConnectionPool {
// TODO: change pool connection properties based on your resources.
/* Change these values for your project */
public static final int INITIAL_SIZE = 12;
public static final int MAX_SIZE = 15;
public static final int MAX_IDLE_TIME = 30;
public static final int DEFAULT_PORT = 5432;
@Bean
public ConnectionPool getConnectionConfig(PostgresqlConnectionProperties properties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"driven-adapter/r2dbc-postgresql/config/postgresql-connection-pool.unit.test.java.mustache": "infrastructure/driven-adapters/r2dbc-postgresql/src/test/java/{{packagePath}}/r2dbc/config/PostgreSQLConnectionPoolTest.java",
"driven-adapter/r2dbc-postgresql/config/postgresql-connection-properties.java.mustache": "infrastructure/driven-adapters/r2dbc-postgresql/src/main/java/{{packagePath}}/r2dbc/config/PostgresqlConnectionProperties.java",
"driven-adapter/r2dbc-postgresql/helper/reactive-adapter-operations.java.mustache": "infrastructure/driven-adapters/r2dbc-postgresql/src/main/java/{{packagePath}}/r2dbc/helper/ReactiveAdapterOperations.java",
"driven-adapter/r2dbc-postgresql/helper/reactive-adapter-operations.test.java.mustache": "infrastructure/driven-adapters/r2dbc-postgresql/src/test/java/{{packagePath}}/r2dbc/helper/ReactiveAdapterOperationsTest.java",
"driven-adapter/r2dbc-postgresql/my-reactive-repository.java.mustache": "infrastructure/driven-adapters/r2dbc-postgresql/src/main/java/{{packagePath}}/r2dbc/MyReactiveRepository.java",
"driven-adapter/r2dbc-postgresql/my-reactive-repository-adapter.java.mustache": "infrastructure/driven-adapters/r2dbc-postgresql/src/main/java/{{packagePath}}/r2dbc/MyReactiveRepositoryAdapter.java",
"driven-adapter/r2dbc-postgresql/my-reactive-repository-adapter.unit.test.java.mustache": "infrastructure/driven-adapters/r2dbc-postgresql/src/test/java/{{packagePath}}/r2dbc/MyReactiveRepositoryAdapterTest.java",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package {{package}}.r2dbc.helper;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.reactivecommons.utils.ObjectMapper;
import org.springframework.data.domain.Example;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import org.springframework.data.repository.query.ReactiveQueryByExampleExecutor;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import java.util.Objects;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

class ReactiveAdapterOperationsTest {
private DummyRepository repository;
private ObjectMapper mapper;
private ReactiveAdapterOperations<DummyEntity, DummyData, String, DummyRepository> operations;
@BeforeEach
void setUp() {
repository = Mockito.mock(DummyRepository.class);
mapper = Mockito.mock(ObjectMapper.class);
operations = new ReactiveAdapterOperations<DummyEntity, DummyData, String, DummyRepository>(
repository, mapper, DummyEntity::toEntity) {};
}

@Test
void save() {
DummyEntity entity = new DummyEntity("1", "test");
DummyData data = new DummyData("1", "test");
when(mapper.map(entity, DummyData.class)).thenReturn(data);
when(repository.save(data)).thenReturn(Mono.just(data));
StepVerifier.create(operations.save(entity))
.expectNext(entity)
.verifyComplete();
}

@Test
void saveAllEntities() {
DummyEntity entity1 = new DummyEntity("1", "test1");
DummyEntity entity2 = new DummyEntity("2", "test2");
DummyData data1 = new DummyData("1", "test1");
DummyData data2 = new DummyData("2", "test2");
when(mapper.map(entity1, DummyData.class)).thenReturn(data1);
when(mapper.map(entity2, DummyData.class)).thenReturn(data2);
when(repository.saveAll(any(Flux.class))).thenReturn(Flux.just(data1, data2));
StepVerifier.create(operations.saveAllEntities(Flux.just(entity1, entity2)))
.expectNext(entity1, entity2)
.verifyComplete();
}

@Test
void findById() {
DummyData data = new DummyData("1", "test");
DummyEntity entity = new DummyEntity("1", "test");
when(repository.findById("1")).thenReturn(Mono.just(data));
StepVerifier.create(operations.findById("1"))
.expectNext(entity)
.verifyComplete();
}

@Test
void findByExample() {
DummyEntity entity = new DummyEntity("1", "test");
DummyData data = new DummyData("1", "test");
when(mapper.map(entity, DummyData.class)).thenReturn(data);
when(repository.findAll(any(Example.class))).thenReturn(Flux.just(data));
StepVerifier.create(operations.findByExample(entity))
.expectNext(entity)
.verifyComplete();
}

@Test
void findAll() {
DummyData data1 = new DummyData("1", "test1");
DummyData data2 = new DummyData("2", "test2");
DummyEntity entity1 = new DummyEntity("1", "test1");
DummyEntity entity2 = new DummyEntity("2", "test2");
when(repository.findAll()).thenReturn(Flux.just(data1, data2));
StepVerifier.create(operations.findAll())
.expectNext(entity1, entity2)
.verifyComplete();
}

static class DummyEntity {
private String id;
private String name;
public DummyEntity(String id, String name) {
this.id = id;
this.name = name;
}

public static DummyEntity toEntity(DummyData data) {
return new DummyEntity(data.getId(), data.getName());
}

public String getId() {
return id;
}

public String getName() {
return name;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DummyEntity that = (DummyEntity) o;
return id.equals(that.id) && name.equals(that.name);
}

@Override
public int hashCode() {
return Objects.hash(id, name);
}
}

static class DummyData {
private String id;
private String name;
public DummyData(String id, String name) {
this.id = id;
this.name = name;
}

public String getId() {
return id;
}

public String getName() {
return name;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DummyData that = (DummyData) o;
return id.equals(that.id) && name.equals(that.name);
}

@Override
public int hashCode() {
return Objects.hash(id, name);
}
}

interface DummyRepository extends ReactiveCrudRepository<DummyData, String>, ReactiveQueryByExampleExecutor<DummyData> {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import org.reactivecommons.utils.ObjectMapper;
import org.springframework.stereotype.Repository;

@Repository
public class MyReactiveRepositoryAdapter extends ReactiveAdapterOperations<Object/* change for domain model */, Object/* change for adapter model */, String, MyReactiveRepository>
// implements ModelRepository from domain
{
public class MyReactiveRepositoryAdapter extends ReactiveAdapterOperations<
Object/* change for domain model */,
Object/* change for adapter model */,
String,
MyReactiveRepository
> {
public MyReactiveRepositoryAdapter(MyReactiveRepository repository, ObjectMapper mapper) {
/**
* Could be use mapper.mapBuilder if your domain model implement builder pattern
Expand Down
14 changes: 12 additions & 2 deletions src/main/resources/entry-point/rest-mvc/api.java.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,24 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.security.access.prepost.PreAuthorize;
{{/task-param-authorize}}

/**
* API Rest controller.
*
* Example of how to declare and use a use case:
* <pre>
* private final MyUseCase useCase;
*
* public String commandName() {
* return useCase.execute();
* }
* </pre>
*/
@RestController
@RequestMapping(value = "/api", produces = MediaType.APPLICATION_JSON_VALUE)
{{#lombok}}
@AllArgsConstructor
{{/lombok}}
public class ApiRest {
// private final MyUseCase useCase;
{{^lombok}}
//public ApiRest(MyUseCase useCase){
Expand All @@ -29,7 +40,6 @@ public class ApiRest {
{{/task-param-authorize}}
@GetMapping(path = "/path")
public String commandName() {
// return useCase.doAction();
return "Hello World";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package {{package}}.config;

import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class UseCasesConfigTest {
@Test
void testUseCaseBeansExist() {
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfig.class)) {
String[] beanNames = context.getBeanDefinitionNames();
boolean useCaseBeanFound = false;
for (String beanName : beanNames) {
if (beanName.endsWith("UseCase")) {
useCaseBeanFound = true;
break;
}
}

assertTrue(useCaseBeanFound, "No beans ending with 'Use Case' were found");
}
}

@Configuration
@Import(UseCasesConfig.class)
static class TestConfig {
@Bean
public MyUseCase myUseCase() {
return new MyUseCase();
}
}

static class MyUseCase {
public String execute() {
return "MyUseCase Test";
}
}
}
3 changes: 2 additions & 1 deletion src/main/resources/structure/definition.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"structure/root/settings.gradle.mustache": "settings.gradle",
"structure/root/build.gradle.mustache": "build.gradle",
"structure/applications/appservice/main.java.mustache": "applications/app-service/src/main/java/{{packagePath}}/MainApplication.java",
"structure/applications/appservice/usecases.java.mustache": "applications/app-service/src/main/java/{{packagePath}}/config/UseCasesConfig.java"
"structure/applications/appservice/usecases.java.mustache": "applications/app-service/src/main/java/{{packagePath}}/config/UseCasesConfig.java",
"structure/applications/appservice/usecases.test.java.mustache": "applications/app-service/src/test/java/{{packagePath}}/config/UseCasesConfigTest.java"
}
}
3 changes: 2 additions & 1 deletion src/main/resources/structure/without-lombok/definition.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"structure/root/settings.gradle.mustache": "settings.gradle",
"structure/root/build.gradle.mustache": "build.gradle",
"structure/applications/appservice/main.java.mustache": "applications/app-service/src/main/java/{{packagePath}}/MainApplication.java",
"structure/applications/appservice/usecases.java.mustache": "applications/app-service/src/main/java/{{packagePath}}/config/UseCasesConfig.java"
"structure/applications/appservice/usecases.java.mustache": "applications/app-service/src/main/java/{{packagePath}}/config/UseCasesConfig.java",
"structure/applications/appservice/usecases.test.java.mustache": "applications/app-service/src/test/java/{{packagePath}}/config/UseCasesConfigTest.java"
}
}

0 comments on commit ca8120d

Please sign in to comment.