Skip to content

Commit 1c3a82f

Browse files
author
Shelson Ferrari
committed
uso do apache camel, ajuste dos logs, novos testes e refatoracao de todo o sistema
1 parent 6f7a412 commit 1c3a82f

22 files changed

Lines changed: 877 additions & 321 deletions

pom.xml

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Configures:
4343
<!-- Project version -->
4444

4545
<packaging>jar</packaging>
46-
<!-- Packaging type -->
46+
<!-- Packaging type -->
4747

4848
<!-- Additional project information -->
4949
<name>java_base</name> <!-- Project name -->
@@ -62,36 +62,52 @@ Configures:
6262
<properties>
6363
<!-- Defining a single property for the Java version -->
6464
<java.version>17</java.version>
65-
65+
6666
<!-- Using java.version property to define compile and run versions -->
6767
<maven.compiler.source>${java.version}</maven.compiler.source>
6868
<maven.compiler.target>${java.version}</maven.compiler.target>
69-
69+
7070
<!-- Other general project properties -->
7171
<project.phase>Development</project.phase>
7272
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
7373
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
7474
</properties>
75-
75+
76+
7677
<!-- Project dependencies -->
7778
<dependencies>
79+
80+
<!-- apache commons -->
81+
<dependency>
82+
<groupId>org.apache.commons</groupId>
83+
<artifactId>commons-lang3</artifactId>
84+
<version>3.15.0</version>
85+
</dependency>
86+
7887
<!-- Apache Camel dependencies -->
88+
<dependency>
89+
<groupId>org.apache.camel</groupId>
90+
<artifactId>camel-http</artifactId>
91+
<version>4.0.2</version>
92+
</dependency>
7993
<dependency>
8094
<groupId>org.apache.camel.springboot</groupId>
8195
<artifactId>camel-spring-boot-starter</artifactId>
82-
<version>4.0.0</version> <!-- ou a versão mais recente -->
96+
<version>4.0.2</version>
8397
</dependency>
8498
<dependency>
8599
<groupId>org.apache.camel.springboot</groupId>
86100
<artifactId>camel-servlet-starter</artifactId>
87-
<version>4.0.0</version> <!-- ou a versão mais recente -->
101+
<version>4.0.2</version>
88102
</dependency>
89-
<!-- springdoc dependencies -->
103+
104+
<!-- Springdoc dependencies -->
90105
<dependency>
91106
<groupId>org.springdoc</groupId>
92107
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
93-
<version>2.3.0</version> <!-- or the latest compatible version -->
108+
<version>2.3.0</version>
94109
</dependency>
110+
95111
<!-- Spring Boot dependencies -->
96112
<dependency>
97113
<groupId>org.springframework.boot</groupId>
@@ -101,32 +117,40 @@ Configures:
101117
<groupId>org.springframework.boot</groupId>
102118
<artifactId>spring-boot-starter-data-jpa</artifactId>
103119
</dependency>
120+
104121
<!-- H2 database dependency -->
105122
<dependency>
106123
<groupId>com.h2database</groupId>
107124
<artifactId>h2</artifactId>
108125
<version>2.1.214</version>
109126
</dependency>
127+
128+
<!-- OpenAPI documentation dependency -->
110129
<dependency>
111130
<groupId>org.springdoc</groupId>
112131
<artifactId>springdoc-openapi-ui</artifactId>
113-
<version>1.8.0</version> <!-- OpenAPI documentation dependency -->
132+
<version>1.8.0</version>
114133
</dependency>
134+
135+
<!-- Spring Boot testing dependency -->
115136
<dependency>
116137
<groupId>org.springframework.boot</groupId>
117138
<artifactId>spring-boot-starter-test</artifactId>
118-
<scope>test</scope> <!-- Spring Boot testing dependency -->
139+
<scope>test</scope>
119140
<exclusions>
120141
<exclusion>
121142
<groupId>org.junit.vintage</groupId>
122143
<artifactId>junit-vintage-engine</artifactId>
123144
</exclusion>
124145
</exclusions>
125146
</dependency>
147+
148+
<!-- Spring Security -->
126149
<dependency>
127150
<groupId>org.springframework.boot</groupId>
128151
<artifactId>spring-boot-starter-security</artifactId>
129152
</dependency>
153+
130154
<!-- Testing dependencies -->
131155
<dependency>
132156
<groupId>org.mockito</groupId>
@@ -153,23 +177,28 @@ Configures:
153177
<artifactId>assertj-core</artifactId>
154178
<scope>test</scope>
155179
</dependency>
180+
181+
<!-- Hibernate Validator -->
156182
<dependency>
157183
<groupId>org.hibernate.validator</groupId>
158184
<artifactId>hibernate-validator</artifactId>
159185
</dependency>
186+
160187
<!-- JSON handling dependency -->
161188
<dependency>
162189
<groupId>org.json</groupId>
163190
<artifactId>json</artifactId>
164191
<version>20231013</version>
165192
</dependency>
193+
166194
<!-- FlatLaf (Look and Feel) dependency -->
167195
<dependency>
168196
<groupId>com.formdev</groupId>
169197
<artifactId>flatlaf</artifactId>
170198
<version>2.1</version>
171199
</dependency>
172200

201+
<!-- JAXB dependencies -->
173202
<dependency>
174203
<groupId>javax.xml.bind</groupId>
175204
<artifactId>jaxb-api</artifactId>
@@ -181,6 +210,7 @@ Configures:
181210
<version>2.3.1</version>
182211
</dependency>
183212

213+
<!-- Swagger dependencies -->
184214
<dependency>
185215
<groupId>io.swagger</groupId>
186216
<artifactId>swagger-annotations</artifactId>
@@ -191,7 +221,6 @@ Configures:
191221
<artifactId>swagger-models</artifactId>
192222
<version>1.5.21</version>
193223
</dependency>
194-
195224
</dependencies>
196225

197226
<!-- Build configuration -->
@@ -243,10 +272,10 @@ Configures:
243272
<name>Shelson Ferrari</name>
244273
<url>https://about.me/shelson</url>
245274
</contact>
246-
<license>
247-
<url>https://github.com/33mestre/java_base/blob/master/LICENSES</url>
248-
<name>Dual Licence - MIT License and Apache License 2.0</name>
249-
</license>
275+
<license>
276+
<url>https://github.com/33mestre/java_base/blob/master/LICENSES</url>
277+
<name>Dual Licence - MIT License and Apache License 2.0</name>
278+
</license>
250279
</info>
251280
<outputFormats>json,yaml</outputFormats>
252281
<outputPath>sys/swagger</outputPath>
@@ -261,7 +290,7 @@ Configures:
261290
</apiSources>
262291
</configuration>
263292
</plugin>
264-
293+
265294
<!-- Plugin for reading .env file -->
266295
<plugin>
267296
<groupId>org.codehaus.mojo</groupId>
@@ -292,7 +321,8 @@ Configures:
292321
<artifactId>maven-checkstyle-plugin</artifactId>
293322
<version>3.1.0</version>
294323
<configuration>
295-
<configLocation>src/main/resources/checkstyle.xml</configLocation> <!-- Location of Checkstyle configuration file -->
324+
<configLocation>src/main/resources/checkstyle.xml</configLocation> <!-- Location
325+
of Checkstyle configuration file -->
296326
<encoding>UTF-8</encoding>
297327
<consoleOutput>true</consoleOutput> <!-- Console output -->
298328
<failsOnError>true</failsOnError> <!-- Fail on errors -->
@@ -391,7 +421,8 @@ Configures:
391421
</goals>
392422
<configuration>
393423
<target>
394-
<delete dir="src/main/java/com/shelson/swing" /> <!-- Example of using Ant to delete a directory -->
424+
<delete dir="src/main/java/com/shelson/swing" /> <!-- Example
425+
of using Ant to delete a directory -->
395426
</target>
396427
</configuration>
397428
</execution>
@@ -437,4 +468,4 @@ Configures:
437468
<developerConnection>scm:git:ssh://github.com:33mestre/java_base.git</developerConnection>
438469
<url>https://github.com/33mestre/java_base</url>
439470
</scm>
440-
</project>
471+
</project>

src/main/java/com/shelson/application/controller/CurrencyConversionController.java

Lines changed: 80 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,20 @@
1616
*/
1717
package com.shelson.application.controller;
1818

19+
import java.util.Enumeration;
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
23+
import org.apache.camel.ProducerTemplate;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
1926
import org.springframework.beans.factory.annotation.Autowired;
2027
import org.springframework.web.bind.annotation.GetMapping;
2128
import org.springframework.web.bind.annotation.RequestMapping;
2229
import org.springframework.web.bind.annotation.RequestParam;
2330
import org.springframework.web.bind.annotation.RestController;
2431

2532
import com.shelson.application.dto.CurrencyConversionDTO;
26-
import com.shelson.application.service.CurrencyConversionService;
2733
import com.shelson.domain.model.Currency;
2834
import com.shelson.infrastructure.exception.BusinessException;
2935

@@ -32,27 +38,47 @@
3238
import io.swagger.annotations.ApiParam;
3339
import io.swagger.annotations.ApiResponse;
3440
import io.swagger.annotations.ApiResponses;
41+
import jakarta.servlet.http.HttpServletRequest;
3542

3643
/**
37-
* Controller responsible for currency conversion operations.
38-
* Provides endpoints for converting values between different currencies.
44+
* Controller responsible for handling currency conversion requests.
45+
* This controller provides an endpoint to convert a given amount from a source currency to a target currency.
46+
* It utilizes Apache Camel routes for fetching the exchange rates and performing the conversion.
47+
*
48+
* Validations performed by this class:
49+
* <ul>
50+
* <li>Ensures that the source and target currency codes are valid.</li>
51+
* <li>Ensures that the amount to be converted is greater than zero.</li>
52+
* <li>Checks for any unexpected parameters in the request and throws a BusinessException if found.</li>
53+
* </ul>
3954
*/
4055
@RestController
4156
@RequestMapping("/api/v1/conversions")
42-
@Api(value = "Currency Conversion Controller", description = "Currency conversion operations")
57+
@Api(value = "Currency Conversion Controller", description = "Operations for currency conversion")
4358
public class CurrencyConversionController {
4459

4560
@Autowired
46-
private CurrencyConversionService currencyConversionService;
61+
private ProducerTemplate producerTemplate;
62+
63+
private static final Logger logger = LoggerFactory.getLogger(CurrencyConversionController.class);
4764

4865
/**
4966
* Converts the source currency to the target currency.
50-
*
51-
* @param source The source currency. Example: USD.
52-
* @param target The target currency. Example: BRL.
53-
* @param amount The amount to be converted. Example: 100.0.
54-
* @return A {@link CurrencyConversionDTO} containing the conversion details.
55-
* @throws BusinessException
67+
*
68+
* Validates the following conditions:
69+
* <ul>
70+
* <li>Source currency code is valid.</li>
71+
* <li>Target currency code is valid.</li>
72+
* <li>Amount to be converted is greater than zero.</li>
73+
* <li>No unexpected parameters are present in the request.</li>
74+
* </ul>
75+
*
76+
* @param source The source currency code.
77+
* @param target The target currency code.
78+
* @param amount The amount to be converted.
79+
* @param request The HttpServletRequest object to check for unexpected parameters.
80+
* @return The details of the currency conversion.
81+
* @throws BusinessException if the source or target currency is invalid or if there are unexpected parameters.
5682
*/
5783
@GetMapping("/convert")
5884
@ApiOperation(value = "Converts the source currency to the target currency", notes = "Returns the currency conversion details")
@@ -61,9 +87,48 @@ public class CurrencyConversionController {
6187
@ApiResponse(code = 400, message = "Invalid input")
6288
})
6389
public CurrencyConversionDTO convertCurrency(
64-
@RequestParam @ApiParam(value = "Source currency", example = "USD") Currency source,
65-
@RequestParam @ApiParam(value = "Target currency", example = "BRL") Currency target,
66-
@RequestParam @ApiParam(value = "Amount to be converted", example = "100.0") double amount) throws BusinessException {
67-
return currencyConversionService.convertCurrency(source, target, amount);
90+
@RequestParam @ApiParam(value = "Source currency", example = "USD") String source,
91+
@RequestParam @ApiParam(value = "Target currency", example = "BRL") String target,
92+
@RequestParam @ApiParam(value = "Amount to be converted", example = "100.0") double amount,
93+
HttpServletRequest request) throws BusinessException {
94+
95+
logger.info("Received currency conversion request: source={}, target={}, amount={}", source, target, amount);
96+
97+
// Validate that only the expected parameters are present
98+
validateParameters(request);
99+
100+
try {
101+
Currency sourceCurrency = Currency.valueOf(source);
102+
Currency targetCurrency = Currency.valueOf(target);
103+
104+
Map<String, Object> headers = new HashMap<>();
105+
headers.put("source", sourceCurrency);
106+
headers.put("target", targetCurrency);
107+
headers.put("amount", amount);
108+
109+
CurrencyConversionDTO result = producerTemplate.requestBodyAndHeaders("direct:convertCurrency", null, headers, CurrencyConversionDTO.class);
110+
logger.info("Currency conversion successful: {}", result);
111+
return result;
112+
} catch (IllegalArgumentException ex) {
113+
logger.error("Invalid currency code provided: source={}, target={}", source, target, ex);
114+
throw new BusinessException("Invalid source or target currency");
115+
}
116+
}
117+
118+
/**
119+
* Validates that no unexpected parameters are present in the request.
120+
*
121+
* @param request The HttpServletRequest object containing the request parameters.
122+
* @throws BusinessException if any unexpected parameters are found.
123+
*/
124+
private void validateParameters(HttpServletRequest request) throws BusinessException {
125+
Enumeration<String> parameterNames = request.getParameterNames();
126+
while (parameterNames.hasMoreElements()) {
127+
String paramName = parameterNames.nextElement();
128+
if (!paramName.equals("source") && !paramName.equals("target") && !paramName.equals("amount")) {
129+
logger.error("Unexpected parameter found: {}", paramName);
130+
throw new BusinessException("Unexpected parameter: " + paramName);
131+
}
132+
}
68133
}
69134
}

0 commit comments

Comments
 (0)