mirror of
https://github.com/valitydev/proxy-mocketbank.git
synced 2024-11-06 10:05:16 +00:00
PROX-146: validate error mapping (#23)
* PROX-146: validate error mapping
This commit is contained in:
parent
d4d1b31d9f
commit
ab4922f625
4
pom.xml
4
pom.xml
@ -42,10 +42,6 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-jetty</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.logstash.logback</groupId>
|
||||
<artifactId>logstash-logback-encoder</artifactId>
|
||||
|
@ -20,6 +20,8 @@ public class ErrorMappingConfiguration {
|
||||
|
||||
@Bean
|
||||
ErrorMapping errorMapping() throws IOException {
|
||||
ErrorMapping errorMapping = new ErrorMapping(filePath.getInputStream(), patternReason);
|
||||
errorMapping.validateMappingFormat();
|
||||
return new ErrorMapping(filePath.getInputStream(), patternReason);
|
||||
}
|
||||
|
||||
|
@ -1,49 +0,0 @@
|
||||
package com.rbkmoney.proxy.mocketbank.configuration;
|
||||
|
||||
import org.eclipse.jetty.server.NetworkTrafficServerConnector;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory;
|
||||
import org.springframework.boot.context.embedded.jetty.JettyServerCustomizer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Configuration
|
||||
public class JettyEmbeddedConfiguration {
|
||||
|
||||
private final static Logger LOGGER = LoggerFactory.getLogger(JettyEmbeddedConfiguration.class);
|
||||
|
||||
@Value("${server.port}")
|
||||
private String mainPort;
|
||||
|
||||
@Value("#{'${server.secondary.ports}'.split(',')}")
|
||||
private List<String> secondaryPorts;
|
||||
|
||||
@Bean
|
||||
public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() {
|
||||
|
||||
final JettyEmbeddedServletContainerFactory factory = new JettyEmbeddedServletContainerFactory(Integer.valueOf(mainPort));
|
||||
|
||||
// Add customized Jetty configuration with non blocking connection handler
|
||||
factory.addServerCustomizers((JettyServerCustomizer) server -> {
|
||||
// Register an additional connector for each secondary port.
|
||||
for(final String secondaryPort : secondaryPorts) {
|
||||
final NetworkTrafficServerConnector connector = new NetworkTrafficServerConnector(server);
|
||||
connector.setPort(Integer.valueOf(secondaryPort));
|
||||
try {
|
||||
connector.join();
|
||||
} catch (InterruptedException e) {
|
||||
LOGGER.error("Exception in JettyConfiguration", e);
|
||||
}
|
||||
server.addConnector(connector);
|
||||
}
|
||||
|
||||
// Additional configuration
|
||||
});
|
||||
return factory;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package com.rbkmoney.proxy.mocketbank.configuration;
|
||||
|
||||
import org.apache.catalina.connector.Connector;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
|
||||
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* This is the configuration class for configuring the {@link EmbeddedServletContainerFactory}
|
||||
*
|
||||
* @author Anatoly Cherkasov
|
||||
* @see Connector
|
||||
* @see EmbeddedServletContainerFactory
|
||||
*/
|
||||
@Configuration
|
||||
public class TomcatEmbeddedConfiguration {
|
||||
|
||||
@Value("${server.port}")
|
||||
private String mainPort;
|
||||
|
||||
@Value("#{'${server.secondary.ports}'.split(',')}")
|
||||
private List<String> secondaryPorts;
|
||||
|
||||
@Bean
|
||||
public EmbeddedServletContainerFactory servletContainer() {
|
||||
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
|
||||
|
||||
Connector[] additionalConnectors = this.additionalConnector();
|
||||
if (additionalConnectors.length > 0) {
|
||||
tomcat.addAdditionalTomcatConnectors(additionalConnectors);
|
||||
}
|
||||
return tomcat;
|
||||
}
|
||||
|
||||
private Connector[] additionalConnector() {
|
||||
List<Connector> result = new ArrayList<>();
|
||||
for (String port : secondaryPorts) {
|
||||
Connector connector = new Connector();
|
||||
connector.setPort(Integer.valueOf(port));
|
||||
result.add(connector);
|
||||
}
|
||||
return result.toArray(new Connector[]{});
|
||||
}
|
||||
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.rbkmoney.proxy.mocketbank.utils.model;
|
||||
package com.rbkmoney.proxy.mocketbank.utils.error_mapping;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
@ -6,10 +6,12 @@ import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.rbkmoney.damsel.domain.Failure;
|
||||
import com.rbkmoney.proxy.mocketbank.utils.model.Error;
|
||||
import com.rbkmoney.woody.api.flow.error.WUndefinedResultException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.*;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
|
||||
import static com.rbkmoney.geck.serializer.kit.tbase.TErrorUtil.toGeneral;
|
||||
@ -20,6 +22,9 @@ import static com.rbkmoney.geck.serializer.kit.tbase.TErrorUtil.toGeneral;
|
||||
*/
|
||||
public class ErrorMapping {
|
||||
|
||||
private final Logger log = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// Constants
|
||||
// ------------------------------------------------------------------------
|
||||
@ -35,7 +40,7 @@ public class ErrorMapping {
|
||||
/**
|
||||
* List of errors
|
||||
*/
|
||||
private final List<com.rbkmoney.proxy.mocketbank.utils.model.Error> errors;
|
||||
private final List<Error> errors;
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// Constructors
|
||||
@ -59,7 +64,7 @@ public class ErrorMapping {
|
||||
this.errors = errors;
|
||||
}
|
||||
|
||||
public static List<com.rbkmoney.proxy.mocketbank.utils.model.Error> initErrorList(InputStream inputStream, ObjectMapper objectMapper) {
|
||||
public static List<Error> initErrorList(InputStream inputStream, ObjectMapper objectMapper) {
|
||||
try {
|
||||
return objectMapper.readValue(inputStream, new TypeReference<List<Error>>() {});
|
||||
} catch (JsonParseException e) {
|
||||
@ -84,7 +89,7 @@ public class ErrorMapping {
|
||||
* @return Failure
|
||||
*/
|
||||
public Failure getFailureByCodeAndDescription(String code, String description) {
|
||||
com.rbkmoney.proxy.mocketbank.utils.model.Error error = findMatchWithPattern(errors, code, description);
|
||||
Error error = findMatchWithPattern(errors, code, description);
|
||||
|
||||
Failure failure = toGeneral(error.getMapping());
|
||||
failure.setReason(prepareReason(code, description));
|
||||
@ -99,7 +104,7 @@ public class ErrorMapping {
|
||||
* @param description String
|
||||
* @return com.rbkmoney.proxy.mocketbank.utils.model.Error
|
||||
*/
|
||||
private com.rbkmoney.proxy.mocketbank.utils.model.Error findMatchWithPattern(
|
||||
private Error findMatchWithPattern(
|
||||
List<Error> errors,
|
||||
String code,
|
||||
String description
|
||||
@ -133,4 +138,11 @@ public class ErrorMapping {
|
||||
return String.format(this.patternReason, code, description);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate mapping formate
|
||||
*/
|
||||
public void validateMappingFormat() {
|
||||
errors.forEach(error -> StandardError.findByValue(error.getMapping()));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,51 @@
|
||||
package com.rbkmoney.proxy.mocketbank.utils.error_mapping;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
|
||||
public enum StandardError {
|
||||
|
||||
PRE_AUTHORIZATION_FAILED("preauthorization_failed"),
|
||||
REJECTED_BY_INSPECTOR("rejected_by_inspector"),
|
||||
|
||||
AUTH_FAILED_OPERATION_BLOCKED("authorization_failed:operation_blocked"),
|
||||
AUTH_FAILED_MERCHANT_BLOCKED("authorization_failed:merchant_blocked"),
|
||||
AUTH_FAILED_ACCOUNT_NOT_FOUND("authorization_failed:account_not_found"),
|
||||
AUTH_FAILED_ACCOUNT_BLOCKED("authorization_failed:account_blocked"),
|
||||
AUTH_FAILED_INSUFFICIENT_FUNDS("authorization_failed:insufficient_funds"),
|
||||
AUTH_FAILED_ACCOUNT_STOLEN("authorization_failed:account_stolen"),
|
||||
AUTH_FAILED_UNKNOWN("authorization_failed:unknown"),
|
||||
|
||||
AUTH_FAILED_ACCOUNT_EXCEEDED_AMOUNT("authorization_failed:account_limit_exceeded:amount"),
|
||||
AUTH_FAILED_ACCOUNT_EXCEEDED_NUMBER("authorization_failed:account_limit_exceeded:number"),
|
||||
AUTH_FAILED_ACCOUNT_EXCEEDED_UNKNOWN("authorization_failed:account_limit_exceeded:unknown"),
|
||||
|
||||
|
||||
AUTH_FAILED_PROVIDER_EXCEEDED_AMOUNT("authorization_failed:provider_limit_exceeded:amount"),
|
||||
AUTH_FAILED_PROVIDER_EXCEEDED_NUMBER("authorization_failed:provider_limit_exceeded:number"),
|
||||
AUTH_FAILED_PROVIDER_EXCEEDED_UNKNOWN("authorization_failed:provider_limit_exceeded:unknown"),
|
||||
|
||||
AUTH_FAILED_BANK_CARD_CARD_EXPIRED("authorization_failed:payment_tool_rejected:bank_card_rejected:card_expired"),
|
||||
AUTH_FAILED_BANK_CARD_CARD_NUMBER_INVALID("authorization_failed:payment_tool_rejected:bank_card_rejected:card_number_invalid"),
|
||||
AUTH_FAILED_BANK_CARD_CARD_HOLDER_INVALID("authorization_failed:payment_tool_rejected:bank_card_rejected:card_holder_invalid"),
|
||||
AUTH_FAILED_BANK_CARD_CVV_INVALID("authorization_failed:payment_tool_rejected:bank_card_rejected:cvv_invalid"),
|
||||
AUTH_FAILED_BANK_CARD_CARD_UNSUPPORTED("authorization_failed:payment_tool_rejected:bank_card_rejected:card_unsupported"),
|
||||
AUTH_FAILED_BANK_CARD_ISSUER_NOT_FOUND("authorization_failed:payment_tool_rejected:bank_card_rejected:issuer_not_found");
|
||||
|
||||
private final String error;
|
||||
|
||||
StandardError(String action) {
|
||||
this.error = action;
|
||||
}
|
||||
|
||||
public String getError() {
|
||||
return error;
|
||||
}
|
||||
|
||||
public static StandardError findByValue(String value) {
|
||||
return Arrays.stream(StandardError.values()).filter((error) -> error.getError().equals(value))
|
||||
.findFirst()
|
||||
.orElseThrow(()->new IllegalStateException(String.format("Unsupported error '%s' does not match standard", value)));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user