Skip to content

Commit

Permalink
Ajustes de Docker + compose
Browse files Browse the repository at this point in the history
  • Loading branch information
André Luis Gomes committed Jun 19, 2019
1 parent 628b159 commit 263e1df
Show file tree
Hide file tree
Showing 16 changed files with 260 additions and 18 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Challenge

## Requirements

```text
$ docker version
Client:
Version: 18.06.0-ce
```

## Running

```bash
$ docker-compose up
```

## Links
+ https://www.base64decode.org/
+ https://www.freeformatter.com/xml-formatter.html
+ https://docs.arquivei.com.br/?urls.primaryName=Arquivei%20API#/

+ https://www.stitchdata.com/etldatabase/etl-extract/
+ https://docs.oracle.com/cd/B19306_01/server.102/b14223/ettover.htm

+ https://cloud.docker.com/repository/docker/andrelugomes/etl-invoice
+ https://cloud.docker.com/repository/docker/andrelugomes/api-invoice
8 changes: 8 additions & 0 deletions api-invoice/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM zenika/kotlin:1.2.71-jdk8-alpine

MAINTAINER [email protected]

ADD build/libs/api-*.jar /api.jar

ENTRYPOINT exec java -jar api.jar

42 changes: 42 additions & 0 deletions api-invoice/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Invoices API

+ kotlin
+ MySQL


## MySQL

```bash
docker run --name mysql_invoices -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=invoices -p 3306:3306 -d mysql
```

## Build API

```bash
cd api-invoice

./gradlew clean build
```

```bash
docker build -t andrelugomes/api-invoice .
```

## Release API

```bash
docker login --username=andrelugomes

docker push andrelugomes/api-invoice:latest
```
## Run API

```bash
docker run -d --name api-invoice --net host andrelugomes/api-invoice:latest
```

http://localhost:8081/swagger-ui.html




5 changes: 5 additions & 0 deletions api-invoice/src/main/resources/application-docker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
server.port=8081

spring.datasource.url=jdbc:mysql://mysql:3306/invoices
spring.datasource.username=root
spring.datasource.password=root
26 changes: 26 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
version: '3'

services:
api:
image: "andrelugomes/api-invoice:latest"
ports:
- "8081:8081"
environment:
SPRING_PROFILES_ACTIVE: docker
depends_on:
- mysql

etl:
image: "andrelugomes/etl-invoice:latest"
environment:
SPRING_PROFILES_ACTIVE: docker
depends_on:
- mysql

mysql:
image: mysql
ports:
- "3307:3306"
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: invoices
7 changes: 7 additions & 0 deletions etl-invoice/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM openjdk:11.0.3

MAINTAINER [email protected]

ADD build/libs/etl-invoice-*.jar /etl.jar

ENTRYPOINT exec java -jar etl.jar
41 changes: 41 additions & 0 deletions etl-invoice/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# ETL Invoices

+ java 11
+ MySQL


## MySQL

```bash
docker run --name mysql_invoices -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=invoices -p 3306:3306 -d mysql
```

## Build ETL

```bash
cd etl-invoice

./gradlew clean build
```

```bash
docker build -t andrelugomes/etl-invoice .
```

## Release ETL

```bash
docker login --username=andrelugomes

docker push andrelugomes/etl-invoice:latest
```

## Run ETL

```bash
docker run -it --net host andrelugomes/etl-invoice:latest
```




2 changes: 1 addition & 1 deletion etl-invoice/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'org.flywaydb:flyway-core'
implementation 'javax.xml.bind:jaxb-apiclient:2.3.1'
implementation 'javax.xml.bind:jaxb-api:2.3.1'
implementation 'com.sun.xml.bind:jaxb-core:2.3.0.1'
implementation 'com.sun.xml.bind:jaxb-impl:2.3.0.1'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ public class WebClientService {
@Qualifier("apiClient")
private WebClient client;

public Mono<Response> receive(final Integer limit, final CursorControll cursor) {
log.info("receive, limit={} , cursor={}", limit, cursor.getPosition());
public Mono<Response> extractFromApi(final Integer limit, final CursorControll cursor) {
log.info("extractFromApi, limit={} , cursor={}", limit, cursor.getPosition());
return client.get()
.uri(builder -> builder.path(NFE_RECEIVED_PATH)
.queryParam(CURSOR, cursor.getPosition())
.queryParam(LIMIT, limit)
.build())
.retrieve()
.bodyToMono(Response.class)
.retryBackoff(3, Duration.ofMillis(100));
.retryBackoff(5, Duration.ofMillis(3000));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,19 @@ public boolean getInvoices() {
final var cursor = cursorControllService.current();

//Extract
final var response = webClientService.receive(limit, cursor);
final var response = webClientService.extractFromApi(limit, cursor);

//Transform
response.subscribe(res -> {
log.info("response, {}, Count={}", res.getStatus(), res.getCount());
if (responseIsOk(res) && hasData(res)) {

res.getData().forEach(invoice -> {

//Transform
final var nfeProc = invoiceService.transform(invoice);

//Load
invoiceService.load(invoice);
invoiceService.load(nfeProc);

});
cursorControllService.update(cursor, res.getPage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import java.util.Base64;

import javax.xml.bind.JAXBException;

import br.com.arquivei.etl.invoice.domain.apiclient.InvoiceResponse;
import br.com.arquivei.etl.invoice.domain.invoice.Invoice;
import br.com.arquivei.etl.invoice.domain.invoice.helper.UnmarshalHelper;
import br.com.arquivei.etl.invoice.domain.invoice.repository.InvoiceRepository;
import br.com.arquivei.etl.invoice.domain.invoice.xml.NfeProc;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
Expand All @@ -23,25 +26,33 @@ public InvoiceService(final InvoiceRepository invoiceRepository) {
}

@Transactional
public void load(final InvoiceResponse invoice) {
log.info("load AccessKey={}", invoice.getAccessKey());
public void load(final NfeProc nfeProc) {
log.info("load Nfe ID={}", nfeProc.getNFe().getInfNFe().getId());

try {
final var nfeProc = UnmarshalHelper.toNfeProc(getXmlAsString(invoice));

final var entity = new Invoice();
entity.setAccessKey(invoice.getAccessKey());
entity.setAccessKey(nfeProc.getProtNFe().getInfProt().getChNFe());
entity.setValue(nfeProc.getNFe().getInfNFe().getTotal().getICMSTot().getvNf());

invoiceRepository.save(entity); //idempotency - JPA load before save because using AccessKey as PK

} catch (final Exception e) {
log.error("ERROR = {}", e.getMessage());
log.error("ERROR={}", e.getMessage());
throw new RuntimeException("Error when saving Invoice.", e);
}
}

private String getXmlAsString(final InvoiceResponse invoice) {
public NfeProc transform(final InvoiceResponse invoice) {
log.info("transform AccessKey={}", invoice.getAccessKey());

try {
return UnmarshalHelper.toNfeProc(transformBase64AsXmlString(invoice));
} catch (final JAXBException e) {
log.error("ERROR={}", e.getMessage());
throw new RuntimeException("Error when parsing Invoice.", e);
}
}

private String transformBase64AsXmlString(final InvoiceResponse invoice) {
return new String(Base64.getDecoder().decode(invoice.getXml()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package br.com.arquivei.etl.invoice.domain.invoice.xml;

import javax.xml.bind.annotation.XmlElement;

public class InfProt {

public InfProt() {
}

private String chNFe;

@XmlElement(name = "chNFe")
public String getChNFe() {
return chNFe;
}

public void setChNFe(final String chNFe) {
this.chNFe = chNFe;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public NfeProc() {

private NFe nfe;

@XmlElement(name = "protNFe", required = true)
private ProtNFe protNFe;

@XmlElement(name = "NFe", required = true)
public NFe getNFe() {
return nfe;
Expand All @@ -22,4 +25,12 @@ public NFe getNFe() {
public void setNFe(final NFe nfe) {
this.nfe = nfe;
}

public ProtNFe getProtNFe() {
return protNFe;
}

public void setProtNFe(final ProtNFe protNFe) {
this.protNFe = protNFe;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package br.com.arquivei.etl.invoice.domain.invoice.xml;

import javax.xml.bind.annotation.XmlElement;

public class ProtNFe {

public ProtNFe() {
}

private InfProt infProt;

@XmlElement(name = "infProt")
public InfProt getInfProt() {
return infProt;
}

public void setInfProt(final InfProt infProt) {
this.infProt = infProt;
}
}
4 changes: 4 additions & 0 deletions etl-invoice/src/main/resources/application-docker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
spring.datasource.url=jdbc:mysql://mysql:3306/invoices?createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=root

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import br.com.arquivei.etl.invoice.domain.invoice.Invoice;
import br.com.arquivei.etl.invoice.domain.invoice.XML;
import br.com.arquivei.etl.invoice.domain.invoice.repository.InvoiceRepository;
import br.com.arquivei.etl.invoice.domain.invoice.xml.NfeProc;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -24,13 +25,30 @@ public class InvoiceServiceTest {
private InvoiceRepository repository;

@Test
public void shouldSaveANewInvoice() {
final var accessKey = "32180105570714000825550010031977311099185510";
public void shouldTransformAsNewNfeProc() {
final var accessKey = "35180100355382000176550010000079441000079448";

final var response = new InvoiceResponse();
response.setAccessKey(accessKey);
response.setXml(XML.BASE_64); //vNF=6.49
service.load(response);

final NfeProc nfeProc = service.transform(response);

Assertions.assertThat(nfeProc.getProtNFe().getInfProt().getChNFe()).isEqualTo(accessKey);
Assertions.assertThat(nfeProc.getNFe().getInfNFe().getTotal().getICMSTot().getvNf()).isEqualTo(6.49);

}

@Test
public void shouldSaveANewInvoice() {
final var accessKey = "35180100355382000176550010000079441000079448";
final var response = new InvoiceResponse();
response.setAccessKey(accessKey);
response.setXml(XML.BASE_64); //vNF=6.49

final var nfeProc = service.transform(response);

service.load(nfeProc);

final var invoice = repository.findById(accessKey).get();

Expand Down

0 comments on commit 263e1df

Please sign in to comment.