Add classes for mock adapter

This commit is contained in:
k.struzhkin 2019-05-15 16:55:25 +03:00
parent d1d240acc4
commit 47b2c506ce
6 changed files with 280 additions and 0 deletions

4
.gitmodules vendored Normal file
View File

@ -0,0 +1,4 @@
[submodule "build_utils"]
path = build_utils
url = git@github.com:rbkmoney/build_utils.git
branch = master

1
build_utils Submodule

@ -0,0 +1 @@
Subproject commit ea4aa042f482551d624fd49a570d28488f479e93

View File

@ -0,0 +1,12 @@
package com.rbkmoney.adapter.bank.spring.boot.starter.test;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface GenerationDataMethod {
}

View File

@ -0,0 +1,83 @@
package com.rbkmoney.adapter.bank.spring.boot.starter.test;
import com.rbkmoney.adapter.bank.spring.boot.starter.test.utils.SaveIntegrationFileUtils;
import com.rbkmoney.damsel.proxy_provider.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.thrift.TDeserializer;
import org.apache.thrift.TException;
import org.apache.thrift.TSerializer;
import org.springframework.stereotype.Component;
import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicInteger;
@RequiredArgsConstructor
@Slf4j
@Component
public class ServerHandlerFileReaderDecorator implements ProviderProxySrv.Iface {
public static final String SRC_TEST_RESOURCES_GENERATED_HG = "src/test/resources/generated/hg/";
private final ProviderProxySrv.Iface serverHandlerLogDecorator;
private ThreadLocal<AtomicInteger> generateCount = ThreadLocal.withInitial(AtomicInteger::new);
private ThreadLocal<AtomicInteger> handleRecurrentTokenCallbackCount = ThreadLocal.withInitial(AtomicInteger::new);
private ThreadLocal<AtomicInteger> processPaymentCount = ThreadLocal.withInitial(AtomicInteger::new);
private ThreadLocal<AtomicInteger> handlePaymentCallbackCount = ThreadLocal.withInitial(AtomicInteger::new);
@Override
public RecurrentTokenProxyResult generateToken(RecurrentTokenContext context) throws TException {
String methodName = "generateToken";
int count = generateCount.get().incrementAndGet();
byte[] bytes = SaveIntegrationFileUtils.readFile(methodName + "Response", SRC_TEST_RESOURCES_GENERATED_HG, count);
TDeserializer tDeserializer = new TDeserializer();
RecurrentTokenContext recurrentTokenContext = new RecurrentTokenContext();
tDeserializer.deserialize(recurrentTokenContext, bytes);
return serverHandlerLogDecorator.generateToken(recurrentTokenContext);
}
@Override
public RecurrentTokenCallbackResult handleRecurrentTokenCallback(ByteBuffer byteBuffer, RecurrentTokenContext context) throws TException {
String methodName = "handleRecurrentTokenCallback";
int count = handleRecurrentTokenCallbackCount.get().incrementAndGet();
byte[] bytes = SaveIntegrationFileUtils.readFile(methodName + "Response", SRC_TEST_RESOURCES_GENERATED_HG, count);
TDeserializer tDeserializer = new TDeserializer();
RecurrentTokenContext recurrentTokenContext = new RecurrentTokenContext();
tDeserializer.deserialize(recurrentTokenContext, bytes);
byte[] bytes1 = SaveIntegrationFileUtils.readFile(methodName + "ByteBuffer", SRC_TEST_RESOURCES_GENERATED_HG, count);
return serverHandlerLogDecorator.handleRecurrentTokenCallback(ByteBuffer.wrap(bytes1), recurrentTokenContext);
}
@Override
public PaymentProxyResult processPayment(PaymentContext context) throws TException {
String methodName = "processPayment";
int count = processPaymentCount.get().incrementAndGet();
byte[] bytes = SaveIntegrationFileUtils.readFile(methodName + "Response", SRC_TEST_RESOURCES_GENERATED_HG, count);
TDeserializer tDeserializer = new TDeserializer();
PaymentContext newContext = new PaymentContext();
tDeserializer.deserialize(newContext, bytes);
return serverHandlerLogDecorator.processPayment(newContext);
}
@Override
public PaymentCallbackResult handlePaymentCallback(ByteBuffer byteBuffer, PaymentContext context) throws TException {
TSerializer serializer = new TSerializer();
byte[] serializeRequest = serializer.serialize(context);
String methodName = "handlePaymentCallback";
int count = handlePaymentCallbackCount.get().incrementAndGet();
byte[] bytes = SaveIntegrationFileUtils.readFile(methodName + "Response", SRC_TEST_RESOURCES_GENERATED_HG, count);
TDeserializer tDeserializer = new TDeserializer();
PaymentContext contextNew = new PaymentContext();
tDeserializer.deserialize(contextNew, bytes);
byte[] bytes1 = SaveIntegrationFileUtils.readFile(methodName + "ByteBuffer", SRC_TEST_RESOURCES_GENERATED_HG, count);
return serverHandlerLogDecorator.handlePaymentCallback(ByteBuffer.wrap(bytes1), contextNew);
}
}

View File

@ -0,0 +1,84 @@
package com.rbkmoney.adapter.bank.spring.boot.starter.test;
import com.rbkmoney.adapter.bank.spring.boot.starter.test.utils.SaveIntegrationFileUtils;
import com.rbkmoney.damsel.proxy_provider.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.thrift.TException;
import org.apache.thrift.TSerializer;
import org.springframework.stereotype.Component;
import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicInteger;
@RequiredArgsConstructor
@Slf4j
@Component
public class ServerHandlerFileSaverDecorator implements ProviderProxySrv.Iface {
public static final String SRC_TEST_RESOURCES_GENERATED_HG = "src/test/resources/generated/hg/";
private final ProviderProxySrv.Iface serverHandlerLogDecorator;
private ThreadLocal<AtomicInteger> generateCount = ThreadLocal.withInitial(AtomicInteger::new);
private ThreadLocal<AtomicInteger> handleRecurrentTokenCallbackCount = ThreadLocal.withInitial(AtomicInteger::new);
private ThreadLocal<AtomicInteger> processPaymentCount = ThreadLocal.withInitial(AtomicInteger::new);
private ThreadLocal<AtomicInteger> handlePaymentCallbackCount = ThreadLocal.withInitial(AtomicInteger::new);
@Override
public RecurrentTokenProxyResult generateToken(RecurrentTokenContext context) throws TException {
TSerializer serializer = new TSerializer();
byte[] serializeRequest = serializer.serialize(context);
String methodName = "generateToken";
int count = generateCount.get().incrementAndGet();
SaveIntegrationFileUtils.saveFile(serializeRequest, methodName + "Response", SRC_TEST_RESOURCES_GENERATED_HG, count);
RecurrentTokenProxyResult recurrentTokenProxyResult = serverHandlerLogDecorator.generateToken(context);
byte[] serializeResult = serializer.serialize(recurrentTokenProxyResult);
SaveIntegrationFileUtils.saveFile(serializeResult, methodName + "Result", SRC_TEST_RESOURCES_GENERATED_HG, count);
return recurrentTokenProxyResult;
}
@Override
public RecurrentTokenCallbackResult handleRecurrentTokenCallback(ByteBuffer byteBuffer, RecurrentTokenContext context) throws TException {
TSerializer serializer = new TSerializer();
byte[] serializeRequest = serializer.serialize(context);
String methodName = "handleRecurrentTokenCallback";
int count = handleRecurrentTokenCallbackCount.get().incrementAndGet();
SaveIntegrationFileUtils.saveFile(serializeRequest, methodName + "Response", SRC_TEST_RESOURCES_GENERATED_HG, count);
SaveIntegrationFileUtils.saveFile(byteBuffer.array(), methodName + "ByteBuffer", SRC_TEST_RESOURCES_GENERATED_HG, count);
RecurrentTokenCallbackResult recurrentTokenCallbackResult = serverHandlerLogDecorator.handleRecurrentTokenCallback(byteBuffer, context);
byte[] serializeResult = serializer.serialize(recurrentTokenCallbackResult);
SaveIntegrationFileUtils.saveFile(serializeResult, methodName + "Result", SRC_TEST_RESOURCES_GENERATED_HG, count);
return recurrentTokenCallbackResult;
}
@Override
public PaymentProxyResult processPayment(PaymentContext context) throws TException {
TSerializer serializer = new TSerializer();
byte[] serializeRequest = serializer.serialize(context);
String methodName = "processPayment";
int count = processPaymentCount.get().incrementAndGet();
SaveIntegrationFileUtils.saveFile(serializeRequest, methodName + "Response", SRC_TEST_RESOURCES_GENERATED_HG, count);
PaymentProxyResult paymentProxyResult = serverHandlerLogDecorator.processPayment(context);
byte[] serializeResult = serializer.serialize(paymentProxyResult);
SaveIntegrationFileUtils.saveFile(serializeResult, methodName + "Result", SRC_TEST_RESOURCES_GENERATED_HG, count);
return paymentProxyResult;
}
@Override
public PaymentCallbackResult handlePaymentCallback(ByteBuffer byteBuffer, PaymentContext context) throws TException {
TSerializer serializer = new TSerializer();
byte[] serializeRequest = serializer.serialize(context);
String methodName = "handlePaymentCallback";
int count = handlePaymentCallbackCount.get().incrementAndGet();
SaveIntegrationFileUtils.saveFile(serializeRequest, methodName + "Response", SRC_TEST_RESOURCES_GENERATED_HG, count);
SaveIntegrationFileUtils.saveFile(byteBuffer.array(), methodName + "ByteBuffer", SRC_TEST_RESOURCES_GENERATED_HG, count);
PaymentCallbackResult paymentCallbackResult = serverHandlerLogDecorator.handlePaymentCallback(byteBuffer, context);
byte[] serializeResult = serializer.serialize(paymentCallbackResult);
SaveIntegrationFileUtils.saveFile(serializeResult, methodName + "Result", SRC_TEST_RESOURCES_GENERATED_HG, count);
return paymentCallbackResult;
}
}

View File

@ -0,0 +1,96 @@
package com.rbkmoney.adapter.bank.spring.boot.starter.test.utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rbkmoney.adapter.bank.spring.boot.starter.test.GenerationDataMethod;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Paths;
@Slf4j
public class SaveIntegrationFileUtils {
private static final String SEPARATOR = "_";
private static ObjectMapper objectMapper = new ObjectMapper();
public static <T> void saveJson(T request, String methodName, int count, String postfix, String targetPath) {
String requestString;
try {
requestString = objectMapper.writeValueAsString(request);
SaveIntegrationFileUtils.saveFile(requestString.getBytes(), methodName + postfix, targetPath, count);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
public static <T> T readJson(Class<T> clazz, String methodName, int count, String postfix, String targetPath) {
try {
byte[] bytes = readFile(methodName + postfix, targetPath, count);
return objectMapper.readValue(bytes, clazz);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void saveFile(byte[] serialize, String postfixFileName, String targetPath, int count) {
try {
StackTraceElement stackTraceElement = findStackElement();
if (stackTraceElement != null) {
String filename = postfixFileName + SEPARATOR + count;
String packagePath = stackTraceElement.getFileName().replace(".java", "")
+ "/" + stackTraceElement.getMethodName();
String pathFile = targetPath + packagePath;
File dir = new File(targetPath);
String absolutePath = dir.getAbsolutePath();
File makeDir = new File(absolutePath + "/" + packagePath);
if (!makeDir.exists()) {
makeDir.mkdirs();
}
Files.write(Paths.get(pathFile + "/" + filename), serialize);
}
} catch (IOException e) {
log.error("Error when wright file e: ", e);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static byte[] readFile(String postfixFileName, String targetPath, int count) {
try {
StackTraceElement stackTraceElement = findStackElement();
if (stackTraceElement != null) {
String filename = postfixFileName + SEPARATOR + count;
String packagePath = stackTraceElement.getFileName().replace(".java", "")
+ "/" + stackTraceElement.getMethodName();
String pathFile = targetPath + packagePath;
return Files.readAllBytes(Paths.get(pathFile + "/" + filename));
}
} catch (Exception e) {
log.error("Error when wright file e: ", e);
}
return new byte[0];
}
private static StackTraceElement findStackElement() throws ClassNotFoundException {
StackTraceElement stackTraceElement = null;
for (int i = 0; i < Thread.currentThread().getStackTrace().length; i++) {
StackTraceElement currentStackTrace = Thread.currentThread().getStackTrace()[i];
String methodName = currentStackTrace.getMethodName();
Class<?> aClass = Class.forName(currentStackTrace.getClassName());
Method[] methods = aClass.getMethods();
for (Method method : methods) {
if (method.getName().equals(methodName) && method.getAnnotation(GenerationDataMethod.class) != null) {
return currentStackTrace;
}
}
}
return stackTraceElement;
}
}