diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fb08ff7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,78 @@ +# Created by .ignore support plugin (hsz.mobi) +.eunit +deps +*.o +*.beam +*.plt +erl_crash.dump +ebin/*.beam +rel/example_project +.concrete/DEV_MODE +.rebar +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/ +.idea/workspace.xml +.idea/tasks.xml +.idea/dictionaries +.idea/vcs.xml +.idea/jsLibraryMappings.xml + +# Sensitive or high-churn files: +.idea/dataSources.ids +.idea/dataSources.xml +.idea/dataSources.local.xml +.idea/sqlDataSources.xml +.idea/dynamic.xml +.idea/uiDesigner.xml + +# Gradle: +.idea/gradle.xml +.idea/libraries + +# Mongo Explorer plugin: +.idea/mongoSettings.xml + +*.iws +*.ipr +*.iml + + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties +*.class + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* +env.list diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..ca5a761 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,4 @@ +[submodule "build_utils"] + path = build_utils + url = git@github.com:rbkmoney/build_utils.git + branch = master diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..65b3c46 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,13 @@ +#!groovy +build('thrift-common-lib', 'docker-host') { + checkoutRepo() + loadBuildUtils() + + def javaLibPipeline + runStage('load JavaLib pipeline') { + javaLibPipeline = load("build_utils/jenkins_lib/pipeJavaLib.groovy") + } + + def buildImageTag = "fcf116dd775cc2e91bffb6a36835754e3f2d5321" + javaLibPipeline(buildImageTag) +} diff --git a/README.md b/README.md index 0152fe7..9ae4395 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ -# eventsink-common-lib -Библиотека с общими классами для взаимодействия с процессинговыми событиями eventsink/sinkievent из дамзеля и машингана +# thrift-common-lib +Библиотека для переиспользования вздаимодействия с протоколами, обрабатываемые machinegun'ном: +- eventsink (hellgate) +- sinkievent (machinegun) diff --git a/build_utils b/build_utils new file mode 160000 index 0000000..ea4aa04 --- /dev/null +++ b/build_utils @@ -0,0 +1 @@ +Subproject commit ea4aa042f482551d624fd49a570d28488f479e93 diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..253dbcc --- /dev/null +++ b/pom.xml @@ -0,0 +1,102 @@ + + + + + com.rbkmoney + parent + 1.0.2 + + + 4.0.0 + jar + + thrift-common-lib + 0.0.1 + + + UTF-8 + UTF-8 + 8 + 1.18.4 + 2.1.0 + 1.278-3abdcb4 + ${project.basedir}/../target/jacoco.exec + 1.1.15 + 1.291-02be09e + 1.12-ebae56f + + + + + + org.slf4j + slf4j-api + 1.7.25 + + + + + + + org.projectlombok + lombok + ${lombok.version} + + + com.rbkmoney.woody + woody-thrift + ${woody.thrift.version} + + + + + com.rbkmoney + damsel + ${damsel.version} + provided + + + com.rbkmoney + machinegun-proto + ${machinegun-proto.version} + + + + + junit + junit + 4.12 + test + + + io.github.benas + random-beans + 3.6.0 + test + + + + + + + org.jacoco + jacoco-maven-plugin + 0.8.2 + + ${sonar.jacoco.reportPath} + true + + + + agent + + prepare-agent + + + + + + + diff --git a/src/main/java/com/rbkmoney/thrift/common/exception/ParseException.java b/src/main/java/com/rbkmoney/thrift/common/exception/ParseException.java new file mode 100644 index 0000000..fbe0c10 --- /dev/null +++ b/src/main/java/com/rbkmoney/thrift/common/exception/ParseException.java @@ -0,0 +1,23 @@ +package com.rbkmoney.thrift.common.exception; + +public class ParseException extends RuntimeException { + + public ParseException() { + } + + public ParseException(String message) { + super(message); + } + + public ParseException(String message, Throwable cause) { + super(message, cause); + } + + public ParseException(Throwable cause) { + super(cause); + } + + public ParseException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/main/java/com/rbkmoney/thrift/common/exception/ThriftBinaryDeserializationException.java b/src/main/java/com/rbkmoney/thrift/common/exception/ThriftBinaryDeserializationException.java new file mode 100644 index 0000000..5ee43ef --- /dev/null +++ b/src/main/java/com/rbkmoney/thrift/common/exception/ThriftBinaryDeserializationException.java @@ -0,0 +1,23 @@ +package com.rbkmoney.thrift.common.exception; + +public class ThriftBinaryDeserializationException extends RuntimeException { + + public ThriftBinaryDeserializationException() { + } + + public ThriftBinaryDeserializationException(String message) { + super(message); + } + + public ThriftBinaryDeserializationException(String message, Throwable cause) { + super(message, cause); + } + + public ThriftBinaryDeserializationException(Throwable cause) { + super(cause); + } + + public ThriftBinaryDeserializationException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/main/java/com/rbkmoney/thrift/common/exception/ThriftBinarySerializationException.java b/src/main/java/com/rbkmoney/thrift/common/exception/ThriftBinarySerializationException.java new file mode 100644 index 0000000..4e4a4fc --- /dev/null +++ b/src/main/java/com/rbkmoney/thrift/common/exception/ThriftBinarySerializationException.java @@ -0,0 +1,23 @@ +package com.rbkmoney.thrift.common.exception; + +public class ThriftBinarySerializationException extends RuntimeException { + + public ThriftBinarySerializationException() { + } + + public ThriftBinarySerializationException(String message) { + super(message); + } + + public ThriftBinarySerializationException(String message, Throwable cause) { + super(message, cause); + } + + public ThriftBinarySerializationException(Throwable cause) { + super(cause); + } + + public ThriftBinarySerializationException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/main/java/com/rbkmoney/thrift/common/parser/Parser.java b/src/main/java/com/rbkmoney/thrift/common/parser/Parser.java new file mode 100644 index 0000000..a0f15a8 --- /dev/null +++ b/src/main/java/com/rbkmoney/thrift/common/parser/Parser.java @@ -0,0 +1,6 @@ +package com.rbkmoney.thrift.common.parser; + +public interface Parser { + + T parse(F data); +} diff --git a/src/main/java/com/rbkmoney/thrift/common/parser/impl/MachineEventParser.java b/src/main/java/com/rbkmoney/thrift/common/parser/impl/MachineEventParser.java new file mode 100644 index 0000000..f11b7a7 --- /dev/null +++ b/src/main/java/com/rbkmoney/thrift/common/parser/impl/MachineEventParser.java @@ -0,0 +1,26 @@ +package com.rbkmoney.thrift.common.parser.impl; + +import com.rbkmoney.machinegun.eventsink.MachineEvent; +import com.rbkmoney.thrift.common.exception.ParseException; +import com.rbkmoney.thrift.common.parser.Parser; +import com.rbkmoney.thrift.common.serialization.BinaryDeserializer; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@RequiredArgsConstructor +public class MachineEventParser implements Parser { + + private final BinaryDeserializer deserializer; + + @Override + public T parse(MachineEvent data) { + try { + byte[] bin = data.getData().getBin(); + return deserializer.deserialize(bin); + } catch (Exception e) { + log.error("Exception when parse message e: ", e); + throw new ParseException(e); + } + } +} diff --git a/src/main/java/com/rbkmoney/thrift/common/parser/impl/PaymentEventPayloadMachineEventParser.java b/src/main/java/com/rbkmoney/thrift/common/parser/impl/PaymentEventPayloadMachineEventParser.java new file mode 100644 index 0000000..895708d --- /dev/null +++ b/src/main/java/com/rbkmoney/thrift/common/parser/impl/PaymentEventPayloadMachineEventParser.java @@ -0,0 +1,11 @@ +package com.rbkmoney.thrift.common.parser.impl; + +import com.rbkmoney.damsel.payment_processing.EventPayload; +import com.rbkmoney.thrift.common.serialization.BinaryDeserializer; + +public class PaymentEventPayloadMachineEventParser extends MachineEventParser { + + public PaymentEventPayloadMachineEventParser(BinaryDeserializer deserializer) { + super(deserializer); + } +} diff --git a/src/main/java/com/rbkmoney/thrift/common/parser/impl/PayoutEventPayloadMachineEventParser.java b/src/main/java/com/rbkmoney/thrift/common/parser/impl/PayoutEventPayloadMachineEventParser.java new file mode 100644 index 0000000..77a3245 --- /dev/null +++ b/src/main/java/com/rbkmoney/thrift/common/parser/impl/PayoutEventPayloadMachineEventParser.java @@ -0,0 +1,11 @@ +package com.rbkmoney.thrift.common.parser.impl; + +import com.rbkmoney.damsel.payout_processing.EventPayload; +import com.rbkmoney.thrift.common.serialization.BinaryDeserializer; + +public class PayoutEventPayloadMachineEventParser extends MachineEventParser { + + public PayoutEventPayloadMachineEventParser(BinaryDeserializer deserializer) { + super(deserializer); + } +} diff --git a/src/main/java/com/rbkmoney/thrift/common/serialization/BinaryDeserializer.java b/src/main/java/com/rbkmoney/thrift/common/serialization/BinaryDeserializer.java new file mode 100644 index 0000000..ea1bae1 --- /dev/null +++ b/src/main/java/com/rbkmoney/thrift/common/serialization/BinaryDeserializer.java @@ -0,0 +1,7 @@ +package com.rbkmoney.thrift.common.serialization; + +public interface BinaryDeserializer { + + T deserialize(byte[] bin); + +} diff --git a/src/main/java/com/rbkmoney/thrift/common/serialization/BinarySerializer.java b/src/main/java/com/rbkmoney/thrift/common/serialization/BinarySerializer.java new file mode 100644 index 0000000..23c4871 --- /dev/null +++ b/src/main/java/com/rbkmoney/thrift/common/serialization/BinarySerializer.java @@ -0,0 +1,7 @@ +package com.rbkmoney.thrift.common.serialization; + +public interface BinarySerializer { + + byte[] serialize(T data); + +} diff --git a/src/main/java/com/rbkmoney/thrift/common/serialization/impl/AbstractThriftBinaryDeserializer.java b/src/main/java/com/rbkmoney/thrift/common/serialization/impl/AbstractThriftBinaryDeserializer.java new file mode 100644 index 0000000..44ee5cd --- /dev/null +++ b/src/main/java/com/rbkmoney/thrift/common/serialization/impl/AbstractThriftBinaryDeserializer.java @@ -0,0 +1,25 @@ +package com.rbkmoney.thrift.common.serialization.impl; + +import com.rbkmoney.thrift.common.exception.ThriftBinaryDeserializationException; +import com.rbkmoney.thrift.common.serialization.BinaryDeserializer; +import lombok.extern.slf4j.Slf4j; +import org.apache.thrift.TBase; +import org.apache.thrift.TDeserializer; +import org.apache.thrift.TException; + +@Slf4j +public abstract class AbstractThriftBinaryDeserializer implements BinaryDeserializer { + + private final ThreadLocal thriftDeserializer = ThreadLocal.withInitial(TDeserializer::new); + + protected T deserialize(byte[] bin, T t) { + log.debug("Deserialize, bin[]: {}", bin.length); + try { + thriftDeserializer.get().deserialize(t, bin); + } catch (TException e) { + log.error("Error when deserialize data", e); + throw new ThriftBinaryDeserializationException(e); + } + return t; + } +} diff --git a/src/main/java/com/rbkmoney/thrift/common/serialization/impl/PaymentEventPayloadDeserializer.java b/src/main/java/com/rbkmoney/thrift/common/serialization/impl/PaymentEventPayloadDeserializer.java new file mode 100644 index 0000000..783d284 --- /dev/null +++ b/src/main/java/com/rbkmoney/thrift/common/serialization/impl/PaymentEventPayloadDeserializer.java @@ -0,0 +1,11 @@ +package com.rbkmoney.thrift.common.serialization.impl; + +import com.rbkmoney.damsel.payment_processing.EventPayload; + +public class PaymentEventPayloadDeserializer extends AbstractThriftBinaryDeserializer { + + @Override + public EventPayload deserialize(byte[] bin) { + return deserialize(bin, new EventPayload()); + } +} diff --git a/src/main/java/com/rbkmoney/thrift/common/serialization/impl/PaymentEventPayloadSerializer.java b/src/main/java/com/rbkmoney/thrift/common/serialization/impl/PaymentEventPayloadSerializer.java new file mode 100644 index 0000000..60b8886 --- /dev/null +++ b/src/main/java/com/rbkmoney/thrift/common/serialization/impl/PaymentEventPayloadSerializer.java @@ -0,0 +1,7 @@ +package com.rbkmoney.thrift.common.serialization.impl; + +import com.rbkmoney.damsel.payment_processing.EventPayload; + +public class PaymentEventPayloadSerializer extends ThriftBinarySerializer { + +} diff --git a/src/main/java/com/rbkmoney/thrift/common/serialization/impl/PayoutEventPayloadDeserializer.java b/src/main/java/com/rbkmoney/thrift/common/serialization/impl/PayoutEventPayloadDeserializer.java new file mode 100644 index 0000000..dc00cce --- /dev/null +++ b/src/main/java/com/rbkmoney/thrift/common/serialization/impl/PayoutEventPayloadDeserializer.java @@ -0,0 +1,11 @@ +package com.rbkmoney.thrift.common.serialization.impl; + +import com.rbkmoney.damsel.payout_processing.EventPayload; + +public class PayoutEventPayloadDeserializer extends AbstractThriftBinaryDeserializer { + + @Override + public EventPayload deserialize(byte[] bin) { + return deserialize(bin, new EventPayload()); + } +} diff --git a/src/main/java/com/rbkmoney/thrift/common/serialization/impl/PayoutEventPayloadSerializer.java b/src/main/java/com/rbkmoney/thrift/common/serialization/impl/PayoutEventPayloadSerializer.java new file mode 100644 index 0000000..ebb9dbb --- /dev/null +++ b/src/main/java/com/rbkmoney/thrift/common/serialization/impl/PayoutEventPayloadSerializer.java @@ -0,0 +1,8 @@ +package com.rbkmoney.thrift.common.serialization.impl; + +import com.rbkmoney.damsel.payment_processing.EventPayload; + +public class PayoutEventPayloadSerializer extends ThriftBinarySerializer { + +} + diff --git a/src/main/java/com/rbkmoney/thrift/common/serialization/impl/ThriftBinarySerializer.java b/src/main/java/com/rbkmoney/thrift/common/serialization/impl/ThriftBinarySerializer.java new file mode 100644 index 0000000..6f8a1e9 --- /dev/null +++ b/src/main/java/com/rbkmoney/thrift/common/serialization/impl/ThriftBinarySerializer.java @@ -0,0 +1,25 @@ +package com.rbkmoney.thrift.common.serialization.impl; + +import com.rbkmoney.thrift.common.exception.ThriftBinarySerializationException; +import com.rbkmoney.thrift.common.serialization.BinarySerializer; +import lombok.extern.slf4j.Slf4j; +import org.apache.thrift.TBase; +import org.apache.thrift.TException; +import org.apache.thrift.TSerializer; + +@Slf4j +public class ThriftBinarySerializer implements BinarySerializer { + + private final ThreadLocal thriftSerializer = ThreadLocal.withInitial(TSerializer::new); + + @Override + public byte[] serialize(T data) { + log.debug("Serialize, data: {}", data); + try { + return thriftSerializer.get().serialize(data); + } catch (TException e) { + log.error("Error when serialize data: {} ", data, e); + throw new ThriftBinarySerializationException(e); + } + } +} diff --git a/src/test/java/com/rbkmoney/thrift/common/parser/ParserTest.java b/src/test/java/com/rbkmoney/thrift/common/parser/ParserTest.java new file mode 100644 index 0000000..0db58ed --- /dev/null +++ b/src/test/java/com/rbkmoney/thrift/common/parser/ParserTest.java @@ -0,0 +1,33 @@ +package com.rbkmoney.thrift.common.parser; + +import com.rbkmoney.damsel.payment_processing.EventPayload; +import com.rbkmoney.machinegun.eventsink.MachineEvent; +import com.rbkmoney.machinegun.msgpack.Value; +import com.rbkmoney.thrift.common.parser.impl.PaymentEventPayloadMachineEventParser; +import com.rbkmoney.thrift.common.serialization.impl.PaymentEventPayloadDeserializer; +import com.rbkmoney.thrift.common.serialization.impl.PaymentEventPayloadSerializer; +import org.junit.Test; + +import java.util.Collections; + +import static io.github.benas.randombeans.api.EnhancedRandom.random; +import static org.junit.Assert.assertEquals; + +public class ParserTest { + + @Test + public void parserTest() { + EventPayload expectedEventPayload = EventPayload.invoice_changes(Collections.emptyList()); + + PaymentEventPayloadSerializer serializer = new PaymentEventPayloadSerializer(); + byte[] serializeEventPayload = serializer.serialize(expectedEventPayload); + + MachineEvent machineEvent = random(MachineEvent.class, "data"); + machineEvent.setData(Value.bin(serializeEventPayload)); + + PaymentEventPayloadMachineEventParser parser = new PaymentEventPayloadMachineEventParser(new PaymentEventPayloadDeserializer()); + EventPayload actualEventPayload = parser.parse(machineEvent); + + assertEquals(expectedEventPayload, actualEventPayload); + } +} diff --git a/src/test/java/com/rbkmoney/thrift/common/serialization/SerializationTest.java b/src/test/java/com/rbkmoney/thrift/common/serialization/SerializationTest.java new file mode 100644 index 0000000..6d87616 --- /dev/null +++ b/src/test/java/com/rbkmoney/thrift/common/serialization/SerializationTest.java @@ -0,0 +1,26 @@ +package com.rbkmoney.thrift.common.serialization; + +import com.rbkmoney.damsel.payment_processing.EventPayload; +import com.rbkmoney.thrift.common.serialization.impl.PaymentEventPayloadDeserializer; +import com.rbkmoney.thrift.common.serialization.impl.PaymentEventPayloadSerializer; +import org.junit.Test; + +import java.util.Collections; + +import static org.junit.Assert.assertEquals; + +public class SerializationTest { + + @Test + public void serializationTest() { + EventPayload expectedEventPayload = EventPayload.invoice_changes(Collections.emptyList()); + + PaymentEventPayloadSerializer serializer = new PaymentEventPayloadSerializer(); + byte[] serializeEventPayload = serializer.serialize(expectedEventPayload); + + PaymentEventPayloadDeserializer deserializer = new PaymentEventPayloadDeserializer(); + EventPayload actualEventPayload = deserializer.deserialize(serializeEventPayload); + + assertEquals(expectedEventPayload, actualEventPayload); + } +}