From ab25d16c697c1dd5dea559367444895a9aef231a Mon Sep 17 00:00:00 2001 From: Jaime Dantas Date: Sat, 4 Jan 2020 22:37:55 -0300 Subject: [PATCH 1/4] Added new unit tests --- fii-tax-calculator/pom.xml | 6 ++ .../business/TaxCalculator.java | 20 +++--- .../exception/RestExceptionHandler.java | 5 -- .../ultils/CalculationEngine.java | 4 ++ .../business/TaxCalculatorTest.java | 13 ++-- .../controller/RestControllerTest.java | 45 ++++++++++++++ .../ultils/CalculationEngineTest.java | 62 +++++++++++++++++++ 7 files changed, 137 insertions(+), 18 deletions(-) create mode 100644 fii-tax-calculator/src/test/java/com/jaimedantas/fiitaxcalculator/controller/RestControllerTest.java diff --git a/fii-tax-calculator/pom.xml b/fii-tax-calculator/pom.xml index ef57c58..2de33a6 100644 --- a/fii-tax-calculator/pom.xml +++ b/fii-tax-calculator/pom.xml @@ -60,6 +60,12 @@ + + junit + junit + test + + diff --git a/fii-tax-calculator/src/main/java/com/jaimedantas/fiitaxcalculator/business/TaxCalculator.java b/fii-tax-calculator/src/main/java/com/jaimedantas/fiitaxcalculator/business/TaxCalculator.java index ddbd0ec..75476ce 100644 --- a/fii-tax-calculator/src/main/java/com/jaimedantas/fiitaxcalculator/business/TaxCalculator.java +++ b/fii-tax-calculator/src/main/java/com/jaimedantas/fiitaxcalculator/business/TaxCalculator.java @@ -14,20 +14,26 @@ public FiiTax calculeFiiTaxes(FiiData fiiData){ FiiTax result = new FiiTax(); BigDecimal netProfitValue = CalculationEngine.subtract(fiiData.getTotalValueSold(), fiiData.getTotalValueBought()); - BigDecimal emolumentosTaxes = CalculationEngine.multiply(fiiData.getTotalValueBought(),B3Taxes.EMOLUMENTOS.atomicValue); - BigDecimal liquidacaoTaxes = CalculationEngine.multiply(fiiData.getTotalValueBought(),B3Taxes.LIQUIDACAO.atomicValue); - BigDecimal IRRFTaxes = CalculationEngine.multiply(fiiData.getTotalValueBought(),B3Taxes.IRRF.atomicValue); - BigDecimal totalTaxes = CalculationEngine.add(emolumentosTaxes,liquidacaoTaxes, IRRFTaxes); + + BigDecimal emolumentosTaxesBuy = CalculationEngine.multiply(fiiData.getTotalValueBought(),B3Taxes.EMOLUMENTOS.atomicValue); + BigDecimal liquidacaoTaxesBuy = CalculationEngine.multiply(fiiData.getTotalValueBought(),B3Taxes.LIQUIDACAO.atomicValue); + BigDecimal emolumentosTaxesSell = CalculationEngine.multiply(fiiData.getTotalValueSold(),B3Taxes.EMOLUMENTOS.atomicValue); + BigDecimal liquidacaoTaxesSell = CalculationEngine.multiply(fiiData.getTotalValueSold(),B3Taxes.LIQUIDACAO.atomicValue); + BigDecimal IRRFTaxes = CalculationEngine.multiply(fiiData.getTotalValueSold(),B3Taxes.IRRF.atomicValue); + + BigDecimal totalTaxes = CalculationEngine.add(emolumentosTaxesBuy,liquidacaoTaxesBuy, IRRFTaxes, + emolumentosTaxesSell, liquidacaoTaxesSell); BigDecimal realProfitAfterTaxes = CalculationEngine.subtract(netProfitValue, totalTaxes); BigDecimal fixTax = CalculationEngine.multiply(realProfitAfterTaxes,B3Taxes.FIX_TAX.atomicValue); - result.setEmolumentosFee(emolumentosTaxes); - result.setLiquidacaoFee(liquidacaoTaxes); + result.setEmolumentosFee(CalculationEngine.add(emolumentosTaxesBuy,emolumentosTaxesSell)); + result.setLiquidacaoFee(CalculationEngine.add(emolumentosTaxesBuy,liquidacaoTaxesSell)); result.setIRRFFee(IRRFTaxes); result.setFixedTax(fixTax); BigDecimal totalProfit = CalculationEngine.subtract(netProfitValue, totalTaxes, fixTax); - BigDecimal totalProfitPercentage = CalculationEngine.divide(totalProfit, fiiData.getTotalValueBought()); + BigDecimal totalProfitPercentage = CalculationEngine.divide(totalProfit, CalculationEngine.add(fiiData.getTotalValueBought(), + liquidacaoTaxesBuy, emolumentosTaxesBuy)); result.setTotalTaxes(totalTaxes); result.setTotalProfitValue(totalProfit); diff --git a/fii-tax-calculator/src/main/java/com/jaimedantas/fiitaxcalculator/exception/RestExceptionHandler.java b/fii-tax-calculator/src/main/java/com/jaimedantas/fiitaxcalculator/exception/RestExceptionHandler.java index fb035d3..f9b0c07 100644 --- a/fii-tax-calculator/src/main/java/com/jaimedantas/fiitaxcalculator/exception/RestExceptionHandler.java +++ b/fii-tax-calculator/src/main/java/com/jaimedantas/fiitaxcalculator/exception/RestExceptionHandler.java @@ -5,18 +5,13 @@ import org.slf4j.LoggerFactory; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; -import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import com.jaimedantas.fiitaxcalculator.model.Error; -import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.context.request.WebRequest; import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; -import javax.servlet.http.HttpServletRequest; - @Order(Ordered.HIGHEST_PRECEDENCE) @ControllerAdvice public class RestExceptionHandler extends ResponseEntityExceptionHandler { diff --git a/fii-tax-calculator/src/main/java/com/jaimedantas/fiitaxcalculator/ultils/CalculationEngine.java b/fii-tax-calculator/src/main/java/com/jaimedantas/fiitaxcalculator/ultils/CalculationEngine.java index e3ce190..1898985 100644 --- a/fii-tax-calculator/src/main/java/com/jaimedantas/fiitaxcalculator/ultils/CalculationEngine.java +++ b/fii-tax-calculator/src/main/java/com/jaimedantas/fiitaxcalculator/ultils/CalculationEngine.java @@ -21,6 +21,10 @@ public static BigDecimal add(BigDecimal arg1, BigDecimal arg2, BigDecimal arg3, return add(add(arg1, arg2, arg3), arg4); } + public static BigDecimal add(BigDecimal arg1, BigDecimal arg2, BigDecimal arg3, BigDecimal arg4, BigDecimal arg5){ + return add(add(arg1, arg2, arg3, arg4), arg4, arg5); + } + public static BigDecimal subtract(BigDecimal arg1, BigDecimal arg2){ return arg1.subtract(arg2).setScale(2, RoundingMode.HALF_UP); } diff --git a/fii-tax-calculator/src/test/java/com/jaimedantas/fiitaxcalculator/business/TaxCalculatorTest.java b/fii-tax-calculator/src/test/java/com/jaimedantas/fiitaxcalculator/business/TaxCalculatorTest.java index 0ef84f5..0bfb364 100644 --- a/fii-tax-calculator/src/test/java/com/jaimedantas/fiitaxcalculator/business/TaxCalculatorTest.java +++ b/fii-tax-calculator/src/test/java/com/jaimedantas/fiitaxcalculator/business/TaxCalculatorTest.java @@ -22,8 +22,8 @@ void shouldCalculateTaxForFiiXPLG11() { fiiData.setTotalValueSold(new BigDecimal("3013.71")); BigDecimal expectedTotalProfitPercentage = new BigDecimal("0.34"); - BigDecimal expectedTotalProfitValue = new BigDecimal("713.55"); - BigDecimal expectedTDarf = new BigDecimal("178.39"); + BigDecimal expectedTotalProfitValue = new BigDecimal("712.66"); + BigDecimal expectedTDarf = new BigDecimal("178.17"); TaxCalculator taxCalculator = new TaxCalculator(); @@ -44,12 +44,13 @@ void shouldCalculateTaxForFiiXPML11() { fiiData.setQuantityBought(47); fiiData.setQuantitySold(47); fiiData.setSoldPriceUnit(new BigDecimal("138.99")); - fiiData.setTotalValueBought(new BigDecimal("5168.59")); + fiiData.setTotalValueBought(new BigDecimal("5295.93")); fiiData.setTotalValueSold(new BigDecimal("6532.53")); - BigDecimal expectedTotalProfitPercentage = new BigDecimal("0.21"); - BigDecimal expectedTotalProfitValue = new BigDecimal("1089.64"); - BigDecimal expectedTDarf = new BigDecimal("272.41"); + BigDecimal expectedTotalProfitPercentage = new BigDecimal("0.19"); + BigDecimal expectedTotalProfitValue = new BigDecimal("985.82"); + //darf xpml: 246.93 + BigDecimal expectedTDarf = new BigDecimal("246.46"); TaxCalculator taxCalculator = new TaxCalculator(); diff --git a/fii-tax-calculator/src/test/java/com/jaimedantas/fiitaxcalculator/controller/RestControllerTest.java b/fii-tax-calculator/src/test/java/com/jaimedantas/fiitaxcalculator/controller/RestControllerTest.java new file mode 100644 index 0000000..9ff1181 --- /dev/null +++ b/fii-tax-calculator/src/test/java/com/jaimedantas/fiitaxcalculator/controller/RestControllerTest.java @@ -0,0 +1,45 @@ +package com.jaimedantas.fiitaxcalculator.controller; + + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.jaimedantas.fiitaxcalculator.model.FiiData; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; + +import java.math.BigDecimal; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@RunWith(SpringRunner.class) +@WebMvcTest(RestController.class) +public class RestControllerTest { + + @Autowired + private MockMvc mvc; + + @Test + public void shoudCallApiAndReturnSucess() throws Exception { + + FiiData imput = new FiiData(); + imput.setTotalValueSold(new BigDecimal("9029")); + imput.setTotalValueBought(new BigDecimal("1029")); + + ObjectMapper objectMapper = new ObjectMapper(); + String json = objectMapper.writeValueAsString(imput); + + this.mvc.perform(post("/tax/fii") + .contentType(MediaType.APPLICATION_JSON) + .content(json)) + .andExpect(status().is2xxSuccessful()); + } + + + + +} \ No newline at end of file diff --git a/fii-tax-calculator/src/test/java/com/jaimedantas/fiitaxcalculator/ultils/CalculationEngineTest.java b/fii-tax-calculator/src/test/java/com/jaimedantas/fiitaxcalculator/ultils/CalculationEngineTest.java index 8d91403..1ca6375 100644 --- a/fii-tax-calculator/src/test/java/com/jaimedantas/fiitaxcalculator/ultils/CalculationEngineTest.java +++ b/fii-tax-calculator/src/test/java/com/jaimedantas/fiitaxcalculator/ultils/CalculationEngineTest.java @@ -31,6 +31,68 @@ void shouldDivideTwoBigDecimals() { assertEquals(expected, result); } + @Test + void shouldAddThreeBigDecimals() { + BigDecimal num1 = new BigDecimal("10.00"); + BigDecimal num2 = new BigDecimal("20.00"); + BigDecimal num3 = new BigDecimal("21943.00"); + BigDecimal expected = new BigDecimal("21973.00"); + + BigDecimal result = CalculationEngine.add(num1, num2, num3); + + assertEquals(expected, result); + } + + @Test + void shouldAddFourBigDecimals() { + BigDecimal num1 = new BigDecimal("10.00"); + BigDecimal num2 = new BigDecimal("20.00"); + BigDecimal num3 = new BigDecimal("2199.00"); + BigDecimal num4 = new BigDecimal("329.00"); + BigDecimal expected = new BigDecimal("2558.00"); + + BigDecimal result = CalculationEngine.add(num1, num2, num3, num4); + + assertEquals(expected, result); + } + + @Test + void shouldAddFiveBigDecimals() { + BigDecimal num1 = new BigDecimal("10.00"); + BigDecimal num2 = new BigDecimal("20.00"); + BigDecimal num3 = new BigDecimal("2199.00"); + BigDecimal num4 = new BigDecimal("329.00"); + BigDecimal num5 = new BigDecimal("323400"); + BigDecimal expected = new BigDecimal("326287.00"); + + BigDecimal result = CalculationEngine.add(num1, num2, num3, num4, num5); + + assertEquals(expected, result); + } + + @Test + void shouldSubtractTwoBigDecimals() { + BigDecimal num1 = new BigDecimal("10.00"); + BigDecimal num2 = new BigDecimal("20.00"); + BigDecimal expected = new BigDecimal("-10.00"); + + BigDecimal result = CalculationEngine.subtract(num1, num2); + + assertEquals(expected, result); + } + + @Test + void shouldSubtractThreeBigDecimals() { + BigDecimal num1 = new BigDecimal("450.00"); + BigDecimal num2 = new BigDecimal("20.00"); + BigDecimal num3 = new BigDecimal("10.00"); + BigDecimal expected = new BigDecimal("420.00"); + + BigDecimal result = CalculationEngine.subtract(num1, num2, num3); + + assertEquals(expected, result); + } + @Test void shouldDivideBigdecimalWithATooBigBigdecimal() { BigDecimal num1 = new BigDecimal("10.00"); From d08353ce47c5a7525ac0fb1e30004bbd05be6e1b Mon Sep 17 00:00:00 2001 From: Jaime Dantas Date: Sat, 4 Jan 2020 22:38:38 -0300 Subject: [PATCH 2/4] release 2 --- fii-tax-calculator/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fii-tax-calculator/pom.xml b/fii-tax-calculator/pom.xml index 2de33a6..8e14cf2 100644 --- a/fii-tax-calculator/pom.xml +++ b/fii-tax-calculator/pom.xml @@ -10,7 +10,7 @@ com.jaimedantas fii-tax-calculator - 0.0.1-SNAPSHOT + 0.0.2-SNAPSHOT fii-tax-calculator Tax calculator for FII in Brazil From 6b389ec3d00525a4a7aa5fb74b697e01092bb86a Mon Sep 17 00:00:00 2001 From: Jaime Dantas Date: Sat, 4 Jan 2020 22:50:12 -0300 Subject: [PATCH 3/4] Edit README.md --- fii-tax-calculator/README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/fii-tax-calculator/README.md b/fii-tax-calculator/README.md index 54069b5..2ffb3bf 100644 --- a/fii-tax-calculator/README.md +++ b/fii-tax-calculator/README.md @@ -11,31 +11,31 @@ This application is able to perform calculations with regards to taxes and profit from a FII transaction in the [B3](http://www.b3.com.br/en_us/) . -This microservice is up and running in [here](http://jaimedantas.com/b3). +This microservice is up and running [here](http://jaimedantas.com/b3). -###### Tecnologies: +###### Tecnologies * Spring Boot 2.2.2 * Swagger 2 * JUnit -## Documentation: +## Documentation You can browser through the Swagger documentation for this API in the link below. `http://jaimedantas.com/fii` -## Build: +## Build ``` mvn clean install ``` -## Run: +## Run ``` java -jar fii-tax-calculator-0.0.1-SNAPSHOT.jar ``` ## Usege -This microservice will run on port 8081 by default. +This microservice will run in the port 8081 by default. To access the swagger interface, go to: ``` http://localhost:8081/swagger-ui.html From 7f9dcefeff523772e1c569a9813961e9c0f5d8af Mon Sep 17 00:00:00 2001 From: Jaime Dantas Date: Sat, 4 Jan 2020 22:51:17 -0300 Subject: [PATCH 4/4] Edit README.md --- README.md | 10 +++++----- frontend/README.md | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 177f908..793ecd5 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ This application is able to perform calculations with regards to taxes and profit from FII and Stocks transactions in the [B3](http://www.b3.com.br/en_us/) . -This system is up and running in [here](http://jaimedantas.com/b3). +This system is up and running [here](http://jaimedantas.com/b3). ## Index @@ -21,7 +21,7 @@ This system is up and running in [here](http://jaimedantas.com/b3). * Stock Tax Calculator microservice (in progress..) * [Frontend](fii-tax-calculator/README.md) -#### Tecnologies: +#### Tecnologies ###### Backend: * Spring Boot 2.2.2 * Swagger 2 @@ -31,17 +31,17 @@ This system is up and running in [here](http://jaimedantas.com/b3). * HTML 5 * JavaScript -## Documentation: +## Documentation You can browser through the Swagger documentation for this API in the link below. `http://jaimedantas.com/fii` ## Versioning This project uses [GitHub](https://github.com/jaimedantas/fii-tax-calculator) for versioning. - For the versions available, see the [tags on this repository](). + For the versions available, see the [tags on this repository](https://github.com/jaimedantas/b3-tax-calculator/tags). ## Author -* [Jaime Dantas](https://jaimedantas.com/) - _Initial work, Development, Test, Documentation_ +* [Jaime Dantas](https://jaimedantas.com/) - _Initial work, development, test, documentation_ See also the list of [contributors](CONTRIBUTORS.txt) who participated in this project. diff --git a/frontend/README.md b/frontend/README.md index 56474bc..f9c45a2 100644 --- a/frontend/README.md +++ b/frontend/README.md @@ -3,9 +3,9 @@ Frontend of the [B3 Tax Calculator](../README.md). -This service is up and running in [here](http://jaimedantas.com/b3). +This service is up and running [here](http://jaimedantas.com/b3). -###### Tecnologies: +###### Tecnologies * HTML 5 * JavaScript