-
Notifications
You must be signed in to change notification settings - Fork 0
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
chore: P4PU-145 create feign client for biz-events service #5
Merged
Giuseppe-LaManna
merged 12 commits into
develop
from
P4PU-145-creare-feign-client-per-biz-events
Jun 4, 2024
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a61251a
P4PU-145 added rest-client base url for biz events and openfeign depe…
Giuseppe-LaManna 4d3f80a
P4PU-145 added feignConfig and connector for BizEvents
Giuseppe-LaManna 75ea0fe
P4PU-145 updated client and added connector implementation
Giuseppe-LaManna 08734d0
Merge branch 'develop' into P4PU-145-creare-feign-client-per-biz-events
Giuseppe-LaManna da2b9a4
P4PU-145 updated connector and values
Giuseppe-LaManna cf40bcb
P4PU-145 added wiremock dependency and added test and stub json file
Giuseppe-LaManna 2925f84
P4PU-145 updated connector and tests
Giuseppe-LaManna 79057e8
Merge branch 'develop' into P4PU-145-creare-feign-client-per-biz-events
Giuseppe-LaManna 3536d34
P4PU-145 updated application.yml, connector and added custom exceptio…
Giuseppe-LaManna e1b57cc
P4PU-145 updated error message
Giuseppe-LaManna f4dc8d5
P4PU-145 updated application.yml
Giuseppe-LaManna 9557717
P4PU-145 updated build.gradle.kts
Giuseppe-LaManna File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package it.gov.pagopa.arc.config; | ||
|
||
import it.gov.pagopa.arc.connector.bizevents.BizEventsRestClient; | ||
import org.springframework.cloud.openfeign.EnableFeignClients; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@EnableFeignClients(clients = {BizEventsRestClient.class}) | ||
public class FeignConfig { | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/it/gov/pagopa/arc/connector/bizevents/BizEventsConnector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package it.gov.pagopa.arc.connector.bizevents; | ||
|
||
import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsTransactionsListDTO; | ||
|
||
public interface BizEventsConnector { | ||
BizEventsTransactionsListDTO getTransactionsList(String fiscalCode, String continuationToken, int size); | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/it/gov/pagopa/arc/connector/bizevents/BizEventsConnectorImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package it.gov.pagopa.arc.connector.bizevents; | ||
|
||
import feign.FeignException; | ||
import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsTransactionsListDTO; | ||
import it.gov.pagopa.arc.exception.custom.BizEventsInvocationException; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.ArrayList; | ||
|
||
@Service | ||
@Slf4j | ||
public class BizEventsConnectorImpl implements BizEventsConnector { | ||
private final String apikey; | ||
private final BizEventsRestClient bizEventsRestClient; | ||
|
||
public BizEventsConnectorImpl(@Value("${rest-client.biz-events.api-key}") String apikey, | ||
BizEventsRestClient bizEventsRestClient) { | ||
this.apikey = apikey; | ||
this.bizEventsRestClient = bizEventsRestClient; | ||
} | ||
|
||
@Override | ||
public BizEventsTransactionsListDTO getTransactionsList(String fiscalCode, String continuationToken, int size) { | ||
BizEventsTransactionsListDTO bizEventsTransactionsListDTO; | ||
try { | ||
bizEventsTransactionsListDTO = bizEventsRestClient.transactionsList(apikey, fiscalCode, continuationToken, size); | ||
}catch (FeignException e) { | ||
if (e.status() == HttpStatus.NOT_FOUND.value()){ | ||
bizEventsTransactionsListDTO = | ||
BizEventsTransactionsListDTO | ||
.builder() | ||
.transactions(new ArrayList<>()) | ||
.build(); | ||
|
||
log.info("A {} occurred handling request getTransactionsList from biz-Events: HttpStatus {} - {}", | ||
e.getClass(), | ||
e.status(), | ||
e.getMessage()); | ||
}else { | ||
throw new BizEventsInvocationException("An error occurred handling request from biz-Events"); | ||
} | ||
} | ||
return bizEventsTransactionsListDTO; | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/it/gov/pagopa/arc/connector/bizevents/BizEventsRestClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package it.gov.pagopa.arc.connector.bizevents; | ||
|
||
import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsTransactionsListDTO; | ||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@FeignClient( | ||
name = "biz-events", | ||
url = "${rest-client.biz-events.baseUrl}") | ||
public interface BizEventsRestClient { | ||
@GetMapping( | ||
value = "/transactions", | ||
produces = MediaType.APPLICATION_JSON_VALUE) | ||
@ResponseBody | ||
@ResponseStatus(HttpStatus.OK) | ||
BizEventsTransactionsListDTO transactionsList( | ||
@RequestHeader("x-api-key") String apikey, | ||
@RequestHeader("x-fiscal-code") String fiscalCode, | ||
@RequestHeader("x-continuation-token") String continuationToken, | ||
@RequestParam("size") int size | ||
); | ||
} | ||
|
||
|
23 changes: 23 additions & 0 deletions
23
src/main/java/it/gov/pagopa/arc/connector/bizevents/dto/BizEventsTransactionDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package it.gov.pagopa.arc.connector.bizevents.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class BizEventsTransactionDTO { | ||
private String transactionId; | ||
private String payeeName; | ||
private String payeeTaxCode; | ||
private String amount; | ||
private String transactionDate; | ||
private Boolean isCart; | ||
private Boolean isPayer; | ||
private Boolean isDebtor; | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/it/gov/pagopa/arc/connector/bizevents/dto/BizEventsTransactionsListDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package it.gov.pagopa.arc.connector.bizevents.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.List; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@Builder | ||
@NoArgsConstructor | ||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
public class BizEventsTransactionsListDTO { | ||
private List<BizEventsTransactionDTO> transactions; | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/it/gov/pagopa/arc/exception/ArcExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package it.gov.pagopa.arc.exception; | ||
|
||
import it.gov.pagopa.arc.exception.custom.BizEventsInvocationException; | ||
import it.gov.pagopa.arc.model.generated.ErrorDTO; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.core.annotation.Order; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@RestControllerAdvice | ||
@Slf4j | ||
@Order(Ordered.HIGHEST_PRECEDENCE) | ||
public class ArcExceptionHandler { | ||
|
||
@ExceptionHandler(BizEventsInvocationException.class) | ||
public ResponseEntity<ErrorDTO> handleBizEventsInvocationException(RuntimeException ex, HttpServletRequest request){ | ||
return handleArcErrorException(ex, request, HttpStatus.INTERNAL_SERVER_ERROR, ErrorDTO.ErrorEnum.GENERIC_ERROR); | ||
} | ||
|
||
private static ResponseEntity<ErrorDTO> handleArcErrorException(RuntimeException ex, HttpServletRequest request, HttpStatus httpStatus, ErrorDTO.ErrorEnum errorEnum) { | ||
String message = ex.getMessage(); | ||
log.info("A {} occurred handling request {}: HttpStatus {} - {}", | ||
ex.getClass(), | ||
getRequestDetails(request), | ||
httpStatus.value(), | ||
message); | ||
|
||
return ResponseEntity | ||
.status(httpStatus) | ||
.body(new ErrorDTO(errorEnum, message)); | ||
} | ||
|
||
private static String getRequestDetails(HttpServletRequest request) { | ||
return "%s %s".formatted(request.getMethod(), request.getRequestURI()); | ||
} | ||
} |
13 changes: 0 additions & 13 deletions
13
src/main/java/it/gov/pagopa/arc/exception/ExceptionHandler.java
This file was deleted.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
src/main/java/it/gov/pagopa/arc/exception/custom/BizEventsInvocationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package it.gov.pagopa.arc.exception.custom; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class BizEventsInvocationException extends RuntimeException{ | ||
public BizEventsInvocationException(String message){super(message);} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/test/java/it/gov/pagopa/arc/connector/bizevents/BizEventsConnectorImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package it.gov.pagopa.arc.connector.bizevents; | ||
|
||
import ch.qos.logback.classic.LoggerContext; | ||
import com.github.tomakehurst.wiremock.WireMockServer; | ||
import com.github.tomakehurst.wiremock.core.WireMockConfiguration; | ||
import it.gov.pagopa.arc.config.FeignConfig; | ||
import it.gov.pagopa.arc.connector.bizevents.dto.BizEventsTransactionsListDTO; | ||
import it.gov.pagopa.arc.exception.custom.BizEventsInvocationException; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.cloud.openfeign.FeignAutoConfiguration; | ||
import org.springframework.context.ApplicationContextInitializer; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.context.event.ContextClosedEvent; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.TestPropertySource; | ||
import org.springframework.test.context.support.TestPropertySourceUtils; | ||
import utils.MemoryAppender; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE) | ||
@ContextConfiguration( | ||
initializers = BizEventsConnectorImplTest.WireMockInitializer.class, | ||
classes = { | ||
BizEventsConnectorImpl.class, | ||
FeignConfig.class, | ||
BizEventsRestClient.class, | ||
FeignAutoConfiguration.class, | ||
HttpMessageConvertersAutoConfiguration.class | ||
}) | ||
@TestPropertySource( | ||
properties = { | ||
"rest-client.biz-events.api-key=x_api_key0", | ||
}) | ||
class BizEventsConnectorImplTest { | ||
|
||
@Autowired | ||
private BizEventsConnector bizEventsConnector; | ||
private MemoryAppender memoryAppender; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("it.gov.pagopa.arc.connector.bizevents.BizEventsConnectorImpl"); | ||
memoryAppender = new MemoryAppender(); | ||
memoryAppender.setContext((LoggerContext) LoggerFactory.getILoggerFactory()); | ||
logger.setLevel(ch.qos.logback.classic.Level.INFO); | ||
logger.addAppender(memoryAppender); | ||
memoryAppender.start(); | ||
} | ||
|
||
@Test | ||
void givenHeaderAndParameterWhenCallBizEventsConnectorThenReturnTransactionList() { | ||
//given | ||
//when | ||
BizEventsTransactionsListDTO bizEventsTransactionsListDTO = bizEventsConnector.getTransactionsList("DUMMY_FISCAL_CODE", "TOKEN", 1); | ||
|
||
//then | ||
assertEquals(1, bizEventsTransactionsListDTO.getTransactions().size()); | ||
assertEquals("1", bizEventsTransactionsListDTO.getTransactions().get(0).getTransactionId()); | ||
assertEquals("Comune di Milano", bizEventsTransactionsListDTO.getTransactions().get(0).getPayeeName()); | ||
assertEquals("MI_XXX", bizEventsTransactionsListDTO.getTransactions().get(0).getPayeeTaxCode()); | ||
assertEquals("180,00", bizEventsTransactionsListDTO.getTransactions().get(0).getAmount()); | ||
assertEquals("2024-03-27T13:07:25Z", bizEventsTransactionsListDTO.getTransactions().get(0).getTransactionDate()); | ||
assertFalse(bizEventsTransactionsListDTO.getTransactions().get(0).getIsCart()); | ||
assertTrue(bizEventsTransactionsListDTO.getTransactions().get(0).getIsPayer()); | ||
assertTrue(bizEventsTransactionsListDTO.getTransactions().get(0).getIsDebtor()); | ||
|
||
} | ||
|
||
@Test | ||
void givenHeaderAndParameterWhenNotFoundThenReturnEmptyTransactionList() { | ||
//given | ||
//when | ||
BizEventsTransactionsListDTO bizEventsTransactionsListDTO = bizEventsConnector.getTransactionsList("DUMMY_FISCAL_CODE_NOT_FOUND", "TOKEN", 2); | ||
//then | ||
Assertions.assertEquals(0,bizEventsTransactionsListDTO.getTransactions().size()); | ||
Assertions.assertTrue(memoryAppender.getLoggedEvents().get(0).getFormattedMessage() | ||
.contains(("A class feign.FeignException$NotFound occurred handling request getTransactionsList from biz-Events: HttpStatus 404")) | ||
); | ||
} | ||
@Test | ||
void givenHeaderAndParameterWhenErrorThenThrowBizEventsInvocationException() { | ||
//When | ||
//Then | ||
Assertions.assertThrows(BizEventsInvocationException.class, | ||
()-> bizEventsConnector.getTransactionsList("DUMMY_FISCAL_CODE_ERROR", "TOKEN", 2)); | ||
|
||
} | ||
|
||
public static class WireMockInitializer | ||
implements ApplicationContextInitializer<ConfigurableApplicationContext> { | ||
@Override | ||
public void initialize(ConfigurableApplicationContext applicationContext) { | ||
WireMockServer wireMockServer = new WireMockServer(new WireMockConfiguration().usingFilesUnderClasspath("src/test/resources/stub")); | ||
wireMockServer.start(); | ||
|
||
applicationContext.getBeanFactory().registerSingleton("wireMockServer", wireMockServer); | ||
applicationContext.addApplicationListener( | ||
applicationEvent -> { | ||
if (applicationEvent instanceof ContextClosedEvent) { | ||
wireMockServer.stop(); | ||
} | ||
}); | ||
|
||
TestPropertySourceUtils.addInlinedPropertiesToEnvironment( | ||
applicationContext, | ||
String.format( | ||
"rest-client.biz-events.baseUrl=http://%s:%d/bizEventsMock", | ||
wireMockServer.getOptions().bindAddress(), wireMockServer.port())); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why externalConfigMapValues? do we have a config map defined in the infrastructure code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whit this task @umbcoppolabottazzi will add the configMap https://pagopa.atlassian.net/jira/software/c/projects/P4PU/boards/592/backlog?selectedIssue=P4PU-189