All files

This commit is contained in:
k.struzhkin 2019-04-18 17:34:18 +03:00
parent 62d0096314
commit 184ee3587d
10 changed files with 768 additions and 0 deletions

78
.gitignore vendored Normal file
View File

@ -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

18
Jenkinsfile vendored Normal file
View File

@ -0,0 +1,18 @@
#!groovy
build('fraudbusters-management', 'java-maven') {
checkoutRepo()
loadBuildUtils()
def javaServicePipeline
runStage('load JavaService pipeline') {
javaServicePipeline = load("build_utils/jenkins_lib/pipeJavaService.groovy")
}
def serviceName = env.REPO_NAME
def mvnArgs = '-DjvmArgs="-Xmx256m"'
def useJava11 = true
def registry = 'dr2.rbkmoney.com'
def registryCredsId = 'jenkins_harbor'
javaServicePipeline(serviceName, useJava11, mvnArgs, registry, registryCredsId)
}

371
pom.xml Normal file
View File

@ -0,0 +1,371 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.rbkmoney</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
</parent>
<artifactId>fraudbusters-management</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Fraudbusters management</name>
<description></description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>11</java.version>
<server.port>8022</server.port>
<exposed.ports>${server.port}</exposed.ports>
<dockerfile.base.service.tag>bc95d0d6dc13c693acd2b274531a7d604b877bf3</dockerfile.base.service.tag>
<dockerfile.registry>dr2.rbkmoney.com</dockerfile.registry>
<shared.resources.version>0.3.6</shared.resources.version>
<flyway.version>5.2.3</flyway.version>
<db.name>wblist</db.name>
<db.port>5432</db.port>
<db.url>jdbc:postgresql://localhost:${db.port}/${db.name}</db.url>
<db.user>postgres</db.user>
<db.password>postgres</db.password>
<db.schema>af</db.schema>
<sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
<wb.list.proto.version>1.20-2c2fb6a</wb.list.proto.version>
<kafka.streams.version>2.1.0</kafka.streams.version>
<kafka.clients.version>2.1.0</kafka.clients.version>
</properties>
<dependencies>
<!--rbkmoney-->
<dependency>
<groupId>com.rbkmoney</groupId>
<artifactId>spring-boot-starter-metrics-statsd</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>com.rbkmoney.woody</groupId>
<artifactId>woody-thrift</artifactId>
<version>1.1.15</version>
</dependency>
<dependency>
<groupId>com.rbkmoney</groupId>
<artifactId>shared-resources</artifactId>
<version>${shared.resources.version}</version>
</dependency>
<dependency>
<groupId>com.rbkmoney.geck</groupId>
<artifactId>filter</artifactId>
<version>0.6.9</version>
</dependency>
<dependency>
<groupId>com.rbkmoney.geck</groupId>
<artifactId>serializer</artifactId>
<version>0.6.9</version>
</dependency>
<dependency>
<groupId>com.rbkmoney</groupId>
<artifactId>wb-list-proto</artifactId>
<version>${wb.list.proto.version}</version>
</dependency>
<!--spring-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--third party-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>${flyway.version}</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>${hikaricp.version}</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.version}</version>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq</artifactId>
<version>${jooq.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.3.0.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.3.0.Final</version>
</dependency>
<!--kafka-->
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-streams</artifactId>
<version>${kafka.streams.version}</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>${kafka.clients.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>2.2.2.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-streams</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>kafka</artifactId>
<version>1.10.2</version>
<scope>test</scope>
</dependency>
<!--test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>1.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.github.benas</groupId>
<artifactId>random-beans</artifactId>
<version>3.6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>kafka</artifactId>
<version>1.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-streams-test-utils</artifactId>
<version>2.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>${project.build.directory}/maven-shared-archive-resources</directory>
<targetPath>${project.build.directory}</targetPath>
<includes>
<include>Dockerfile</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>${project.build.directory}/maven-shared-archive-resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>Dockerfile</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.5</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-filtering</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
<configuration>
<resourceBundles>
<resourceBundle>com.rbkmoney:shared-resources:${shared.resources.version}</resourceBundle>
</resourceBundles>
<attachToMain>false</attachToMain>
<attachToTest>false</attachToTest>
</configuration>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>${flyway.version}</version>
<configuration>
<url>${db.url}</url>
<user>${db.user}</user>
<password>${db.password}</password>
<schemas>
<schema>${db.schema}</schema>
</schemas>
<locations>
<location>filesystem:${project.basedir}/src/main/resources/db/migration</location>
</locations>
</configuration>
<executions>
<execution>
<id>migrate</id>
<phase>generate-sources</phase>
<goals>
<goal>migrate</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<version>${jooq.version}</version>
<configuration>
<jdbc>
<driver>org.postgresql.Driver</driver>
<url>${db.url}</url>
<user>${db.user}</user>
<password>${db.password}</password>
</jdbc>
<generator>
<generate>
<javaTimeTypes>true</javaTimeTypes>
<pojos>true</pojos>
<pojosEqualsAndHashCode>true</pojosEqualsAndHashCode>
<pojosToString>true</pojosToString>
</generate>
<database>
<name>org.jooq.meta.postgres.PostgresDatabase</name>
<includes>.*</includes>
<excludes>schema_version|flyway_schema_history</excludes>
<inputSchema>${db.schema}</inputSchema>
</database>
<target>
<packageName>com.rbkmoney.fraudbusters.management.domain</packageName>
<directory>target/generated-sources/jooq</directory>
</target>
</generator>
</configuration>
<executions>
<execution>
<id>gen-src</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.rbkmoney.maven.plugins</groupId>
<artifactId>pg-embedded-plugin</artifactId>
<version>1.4</version>
<configuration>
<port>${db.port}</port>
<dbName>${db.name}</dbName>
<schemas>
<schema>${db.schema}</schema>
</schemas>
</configuration>
<executions>
<execution>
<id>PG_server_start</id>
<phase>initialize</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>PG_server_stop</id>
<phase>compile</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2</version>
<configuration>
<destFile>${sonar.jacoco.reportPath}</destFile>
<append>true</append>
</configuration>
<executions>
<execution>
<id>agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,17 @@
package com.rbkmoney.fraudbusters.management;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@EnableWebMvc
@ServletComponentScan
@SpringBootApplication
public class FraudbustersManagementApplication extends SpringApplication {
public static void main(String[] args) {
SpringApplication.run(FraudbustersManagementApplication.class, args);
}
}

View File

@ -0,0 +1,52 @@
package com.rbkmoney.fraudbusters.management.dao.mapper;
import com.rbkmoney.geck.common.util.TypeUtil;
import org.jooq.Field;
import org.jooq.Table;
import org.jooq.TableRecord;
import org.jooq.impl.TableRecordImpl;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
public class RecordRowMapper<T> implements RowMapper<T> {
private final Table table;
private final Class<T> type;
public RecordRowMapper(Table table, Class<T> type) {
this.table = table;
this.type = type;
}
@Override
public T mapRow(ResultSet resultSet, int i) throws SQLException {
ResultSetMetaData rsMetaData = resultSet.getMetaData();
int columnCount = rsMetaData.getColumnCount();
TableRecord record = new TableRecordImpl(table);
for (int column = 1; column <= columnCount; column++) {
String columnName = rsMetaData.getColumnName(column);
Field field = record.field(columnName);
Object value = getFieldValue(field, resultSet);
if (value != null) {
record.set(field, value);
}
}
return record.into(type);
}
private Object getFieldValue(Field field, ResultSet resultSet) throws SQLException {
if (field.getDataType().isBinary()) {
return resultSet.getBytes(field.getName());
}
if (field.getType().isEnum()) {
return TypeUtil.toEnumField(resultSet.getString(field.getName()), field.getType());
}
return resultSet.getObject(field.getName(), field.getType());
}
}

View File

@ -0,0 +1,21 @@
package com.rbkmoney.fraudbusters.management.dao.wblist;
import com.rbkmoney.fraudbusters.management.domain.enums.ListType;
import com.rbkmoney.fraudbusters.management.domain.tables.pojos.WbListRecords;
import com.rbkmoney.fraudbusters.management.exception.DaoException;
import java.util.List;
public interface WbListDao {
void saveListRecord(WbListRecords listRecord) throws DaoException;
void removeRecord(String id) throws DaoException;
void removeRecord(WbListRecords listRecord) throws DaoException;
WbListRecords getById(String id) throws DaoException;
List<WbListRecords> getFilteredListRecords(String partyId, String shopId, ListType listType, String listName) throws DaoException;
}

View File

@ -0,0 +1,36 @@
server:
port: @server.port@
management:
security:
flag: false
metrics:
export:
statsd:
flavor: etsy
enabled: false
spring:
application:
name: @project.name@
output:
ansi:
enabled: always
datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: org.postgresql.Driver
url: @db.url@
username: @db.user@
password: @db.password@
hikari:
idle-timeout: 30000
maximum-pool-size: 10
flyway:
schemas: af
info:
version: @project.version@
stage: dev
kafka:
bootstrap.servers: "localhost:29092"
wblist.topic:
command: "wb-list-command"
event.sink: "wb-list-event-sink"

View File

@ -0,0 +1,17 @@
CREATE SCHEMA IF NOT EXISTS af;
CREATE TYPE af.list_type AS ENUM('white', 'black');
-- wb.list --
CREATE TABLE af.wb_list_records(
id CHARACTER VARYING NOT NULL,
party_id CHARACTER VARYING NOT NULL,
shop_id CHARACTER VARYING NOT NULL,
list_type af.list_type NOT NULL,
list_name CHARACTER VARYING NOT NULL,
value CHARACTER VARYING NOT NULL,
insert_time TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT (now() at time zone 'utc'),
CONSTRAINT wb_list_records_pkey PRIMARY KEY (id)
);
CREATE UNIQUE INDEX wb_list_key on af.wb_list_records(party_id, shop_id, list_type, list_name, value);

View File

@ -0,0 +1,148 @@
package com.rbkmoney.fraudbusters.management;
import com.rbkmoney.damsel.wb_list.*;
import com.rbkmoney.fraudbusters.management.dao.wblist.WbListDao;
import com.rbkmoney.fraudbusters.management.domain.ListRecord;
import com.rbkmoney.fraudbusters.management.domain.tables.pojos.WbListRecords;
import com.rbkmoney.fraudbusters.management.listener.WbListEventListener;
import com.rbkmoney.fraudbusters.management.resource.WbListResource;
import com.rbkmoney.fraudbusters.management.serializer.CommandDeserializer;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.jetbrains.annotations.NotNull;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutionException;
import static org.mockito.ArgumentMatchers.any;
@Slf4j
@RunWith(SpringRunner.class)
@EnableAutoConfiguration(exclude = FlywayAutoConfiguration.class)
@SpringBootTest(classes = FraudbustersManagementApplication.class)
public class FraudbustersManagementApplicationTest extends AbstractKafkaIntegrationTest {
private static final String VALUE = "value";
private static final String KEY = "key";
private static final String SHOP_ID = "shopId";
private static final String PARTY_ID = "partyId";
private static final String LIST_NAME = "listName";
@Value("${kafka.wblist.topic.event.sink}")
public String topicEventSink;
@Value("${kafka.wblist.topic.command}")
public String topicCommand;
@MockBean
public WbListDao wbListDao;
@Autowired
WbListEventListener wbListEventListener;
@Autowired
WbListResource wbListResource;
@Test
public void listenCreated() throws ExecutionException, InterruptedException {
Event event = new Event();
Row row = createRow(ListType.black);
event.setRow(row);
event.setEventType(EventType.CREATED);
ProducerRecord producerRecord = new ProducerRecord<>(topicEventSink, "test", event);
Producer<String, Event> producer = createProducer();
producer.send(producerRecord).get();
producer.close();
Thread.sleep(500L);
Mockito.verify(wbListDao, Mockito.times(1)).saveListRecord(any());
}
@Test
public void listenDeleted() throws ExecutionException, InterruptedException {
Event event = new Event();
Row row = createRow(ListType.black);
event.setRow(row);
event.setEventType(EventType.DELETED);
ProducerRecord producerRecord = new ProducerRecord<>(topicEventSink, "test", event);
Producer<String, Event> producer = createProducer();
producer.send(producerRecord).get();
producer.close();
Thread.sleep(500L);
Mockito.verify(wbListDao, Mockito.times(1)).removeRecord((WbListRecords) any());
}
@NotNull
private Row createRow(ListType listType) {
Row row = new Row();
row.setShopId(SHOP_ID);
row.setPartyId(PARTY_ID);
row.setListName(LIST_NAME);
row.setListType(listType);
row.setValue(VALUE);
return row;
}
@Test
public void executeTest() {
Mockito.doNothing().when(wbListDao).saveListRecord(any());
ListRecord record = new ListRecord();
record.setListName(LIST_NAME);
record.setPartyId(PARTY_ID);
record.setShopId(SHOP_ID);
record.setValue(VALUE);
ResponseEntity<String> stringResponseEntity = wbListResource.insertRowToBlack(record);
Consumer<String, ChangeCommand> consumer = createConsumer(CommandDeserializer.class);
consumer.subscribe(Collections.singletonList(topicCommand));
List<ChangeCommand> eventList = consumeCommand(consumer);
Assert.assertEquals(1, eventList.size());
Assert.assertEquals(eventList.get(0).command, Command.CREATE);
Assert.assertEquals(eventList.get(0).getRow().getListType(), ListType.black);
stringResponseEntity = wbListResource.removeRowFromWhiteList(record);
consumer = createConsumer(CommandDeserializer.class);
consumer.subscribe(Collections.singletonList(topicCommand));
eventList = consumeCommand(consumer);
Assert.assertEquals(1, eventList.size());
Assert.assertEquals(eventList.get(0).command, Command.DELETE);
Assert.assertEquals(eventList.get(0).getRow().getListType(), ListType.white);
}
@NotNull
private List<ChangeCommand> consumeCommand(Consumer<String, ChangeCommand> consumer) {
List<ChangeCommand> eventList = new ArrayList<>();
ConsumerRecords<String, ChangeCommand> consumerRecords =
consumer.poll(Duration.ofSeconds(10));
consumerRecords.forEach(command -> {
log.info("poll command: {}", command.value());
eventList.add(command.value());
});
consumer.close();
return eventList;
}
}

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<root level="warn">
<appender-ref ref="CONSOLE"/>
</root>
<logger name="com.rbkmoney.woody" level="ALL"/>
</configuration>