Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reactive r2dbc and CA example #135

Merged
merged 6 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -465,4 +465,4 @@ Review the issues, we hear new ideas. Read more [Contributing](https://github.co

# Whats Next?

Read more [About Clean Architecure](https://medium.com/bancolombia-tech/clean-architecture-aislando-los-detalles-4f9530f35d7a)
Read more [About Clean Architecure](https://medium.com/bancolombia-tech/clean-architecture-aislando-los-detalles-4f9530f35d7a)
18 changes: 18 additions & 0 deletions examples-ca/channel-operations/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Reactive programming and Clean architecture example


### Before to run the application:

1. Run the scripts/dockerImages.sh file to start a postgresql database
2. Connect from dbeaver to the docker database (localhost:5432)
3. Create a database called "channeldb" and a schema called "management"
4. Run the ddl (Scripts/ddl.sql)

### Verify connection properties
You need to verify the connection database properties in infraestructure/driven-adapters/r2postgresql/.../config/PostgreSQLConnectionPool.java

### Run the application:
Execute the application running applications/app-service.../MainApplication.java



Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apply plugin: 'org.springframework.boot'

dependencies {
implementation project(':r2postgresql')
implementation project(':reactive-web')
implementation project(':model')
implementation project(':usecase')
compile 'org.springframework.boot:spring-boot-starter'
compile 'org.reactivecommons.utils:object-mapper:0.1.0'
runtime('org.springframework.boot:spring-boot-devtools')
}

task explodedJar(type: Copy) {
with jar
into "${buildDir}/exploded"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package co.com.bancolombia;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package co.com.bancolombia.config;

import co.com.bancolombia.model.IAuthenticationRepository;
import co.com.bancolombia.usecase.AuthenticationUseCase;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class UseCaseConfig {

@Bean
public AuthenticationUseCase createAuthUseCase(IAuthenticationRepository authRepo) {
return new AuthenticationUseCase(authRepo);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
##Spring Configuration
server:
port: 8090
spring:
application:
name: r2dbcconnection
devtools:
add-properties: false
h2:
console:
enabled: true
path: /h2
profiles:
include:
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name=PropertiesConfig
property.filename=logs
appenders=console
appender.console.type=Console
appender.console.name=STDOUT
appender.console.layout.type=PatternLayout
appender.console.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
rootLogger.level=debug
rootLogger.appenderRefs=stdout
rootLogger.appenderRef.stdout.ref=STDOUT
31 changes: 31 additions & 0 deletions examples-ca/channel-operations/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
buildscript {
ext {
cleanArchitectureVersion = '1.8.2'
springBootVersion = '2.3.8.RELEASE'
sonarVersion = '3.0'
jacocoVersion = '0.8.5'
}
}

plugins {
id 'co.com.bancolombia.cleanArchitecture' version "${cleanArchitectureVersion}"
id 'org.springframework.boot' version "${springBootVersion}"
id 'org.sonarqube' version "${sonarVersion}"
id 'jacoco'
}

sonarqube {
properties {
property "sonar.sourceEnconding", "UTF-8"
property "sonar.modules", "applications/app-service," +
"domain/model, domain/usecase," +
"infrastructure/driven-adapters/*, infrastructure/entry-points/*"
property "sonar.sources", "src, deployment, build.gradle, applications/app-service/build.gradle"
property "sonar.test", "src/test"
property "sonar.java.binaries", ""
property "sonar.junit.reportsPath", ""
property "sonar.java.coveragePlugin", "jacoco"
property "sonar.coverage.jacoco.xmlReportPaths", "build/reports/jacoco/test/jacocoTestReport.xml"
}
}
apply from: './main.gradle'
6 changes: 6 additions & 0 deletions examples-ca/channel-operations/deployment/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM adoptopenjdk/openjdk8-openj9:alpine-slim
VOLUME /tmp
COPY *.jar app.jar
RUN sh -c 'touch /app.jar'
ENV JAVA_OPTS=" -Xshareclasses:name=cacheapp,cacheDir=/cache,nonfatal -XX:+UseContainerSupport -XX:MaxRAMPercentage=70 -Djava.security.egd=file:/dev/./urandom"
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -jar /app.jar" ]
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package co.com.bancolombia.model;

import java.io.Serializable;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder(toBuilder = true)
@AllArgsConstructor
@NoArgsConstructor
public class Authentication implements Serializable {

private static final long serialVersionUID = 569381541450405501L;

private Integer id;
private String userName;
private String channel;
private String applicationId;
private LocalDateTime authenticationTime;
private String authenticationIp;
private String authenticationDevice;
private String accessToken;
private String refreshToken;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package co.com.bancolombia.model;

import reactor.core.publisher.Mono;

public interface IAuthenticationRepository {

Mono<Authentication> save(Authentication authentication);

Mono<Authentication> findByUserNameAndChannelId(String userName, String channel);

Mono<Void> delete(Authentication authentication);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package co.com.bancolombia.model.exception;

import co.com.bancolombia.model.exception.message.BusinessErrorMessage;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public class BusinessException extends Exception {

private final BusinessErrorMessage businessErrorMessage;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package co.com.bancolombia.model.exception;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Constants {
public static final String A_SYSTEM_FAILURE_OCCURRED =
"Ocurrió una falla en el sistema,"
+ " estamos trabajando"
+ " para ofrecerte una solución lo más pronto posible. Si el error persiste comunícate al 018000912345.";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package co.com.bancolombia.model.exception;

import co.com.bancolombia.model.exception.message.TechnicalErrorMessage;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public class TechnicalException extends Exception {

private final TechnicalErrorMessage technicalErrorMessage;

public TechnicalException(Throwable cause, TechnicalErrorMessage technicalErrorMessage) {
super(cause);
this.technicalErrorMessage = technicalErrorMessage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package co.com.bancolombia.model.exception.message;

import static co.com.bancolombia.model.exception.Constants.*;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum BusinessErrorMessage {
AUTHENTICATION_NOT_FOUND("ASB0011", "Authentication not found", "471", A_SYSTEM_FAILURE_OCCURRED);

private final String code;
private final String description;
private final String itcCode;
private final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package co.com.bancolombia.model.exception.message;

import static co.com.bancolombia.model.exception.Constants.*;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum TechnicalErrorMessage {
AUTHENTICATION_SAVE("AST0009", "Error saving authentication", "439", A_SYSTEM_FAILURE_OCCURRED),
AUTHENTICATION_FIND("AST0010", "Error finding authentication", "440", A_SYSTEM_FAILURE_OCCURRED),
AUTHENTICATION_DELETE(
"AST0013", "Error deleting authentication", "443", A_SYSTEM_FAILURE_OCCURRED);

private final String code;
private final String description;
private final String itcCode;
private final String message;
}
3 changes: 3 additions & 0 deletions examples-ca/channel-operations/domain/usecase/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dependencies {
compile project(':model')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package co.com.bancolombia.usecase;

import co.com.bancolombia.model.Authentication;
import co.com.bancolombia.model.IAuthenticationRepository;
import co.com.bancolombia.model.exception.BusinessException;
import co.com.bancolombia.model.exception.message.BusinessErrorMessage;
import java.time.LocalDateTime;
import lombok.RequiredArgsConstructor;
import reactor.core.publisher.Mono;

@RequiredArgsConstructor
public class AuthenticationUseCase {

private final IAuthenticationRepository authRepository;

public Mono<Authentication> saveAuthentication(Authentication authentication) {
authentication.setAuthenticationTime(LocalDateTime.now());
return authRepository.save(authentication);
}

public Mono<Authentication> findByUsernameAndChannelId(String username, String channelID) {
return authRepository
.findByUserNameAndChannelId(username, channelID)
.switchIfEmpty(getBusinessError(BusinessErrorMessage.AUTHENTICATION_NOT_FOUND));
}

public Mono<Void> deleteAuthentication(Authentication authentication) {
return authRepository.delete(authentication);
}

private <T> Mono<T> getBusinessError(BusinessErrorMessage businessErrorMessage) {
return Mono.error(getBusinessException(businessErrorMessage));
}

private BusinessException getBusinessException(BusinessErrorMessage businessErrorMessage) {
return new BusinessException(businessErrorMessage);
}
}
5 changes: 5 additions & 0 deletions examples-ca/channel-operations/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package=co.com.bancolombia
systemProp.version=1.8.2
reactive=true
lombok=true
org.gradle.parallel=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
dependencies {
compile project(':model')
compile 'org.springframework:spring-context'
implementation 'org.springframework.boot:spring-boot-starter-data-r2dbc'
implementation 'io.r2dbc:r2dbc-postgresql'
implementation 'org.mapstruct:mapstruct:1.3.1.Final'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.3.1.Final'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package co.com.bancolombia.r2postgresql.authentication;

import java.time.LocalDateTime;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.domain.Persistable;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;

@Data
@Table("management.authentication")
public class AuthenticationData implements Persistable<Integer> {

@Id
@Column("id")
private Integer id;

@Column("user_name")
private String userName;

@Column("channel_id")
private String channel;

@Column("application_id")
private String applicationId;

@Column("authentication_time")
private LocalDateTime authenticationTime;

@Column("authentication_ip")
private String authenticationIp;

@Column("authentication_device")
private String authenticationDevice;

@Column("access_token")
private String accessToken;

@Column("refresh_token")
private String refreshToken;

@Override
public Integer getId() {
return id;
}

@Override
public boolean isNew() {
return true;
}
}
Loading