Skip to content

Commit 533a100

Browse files
committed
JEP votes
1 parent 8cdc9a7 commit 533a100

File tree

32 files changed

+843
-81
lines changed

32 files changed

+843
-81
lines changed

.deploy/lambda/lib/JProfByBotStack.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
import * as cdk from '@aws-cdk/core';
2-
import { Duration } from '@aws-cdk/core';
2+
import { Duration, RemovalPolicy } from '@aws-cdk/core';
33
import { JProfByBotStackProps } from './JProfByBotStackProps';
4-
import lambda = require('@aws-cdk/aws-lambda');
5-
import apigateway = require('@aws-cdk/aws-apigateway');
4+
import * as dynamodb from '@aws-cdk/aws-dynamodb';
5+
import * as lambda from '@aws-cdk/aws-lambda';
6+
import * as apigateway from '@aws-cdk/aws-apigateway';
67

78
export class JProfByBotStack extends cdk.Stack {
89
constructor(scope: cdk.Construct, id: string, props: JProfByBotStackProps) {
910
super(scope, id, props);
1011

12+
const votesTable = new dynamodb.Table(this, 'jprof-by-bot-table-votes', {
13+
tableName: 'jprof-by-bot-table-votes',
14+
partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
15+
billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
16+
removalPolicy: RemovalPolicy.DESTROY,
17+
});
18+
1119
const lambdaWebhook = new lambda.Function(this, 'jprof-by-bot-lambda-webhook', {
1220
functionName: 'jprof-by-bot-lambda-webhook',
1321
runtime: lambda.Runtime.JAVA_11,
@@ -17,9 +25,13 @@ export class JProfByBotStack extends cdk.Stack {
1725
handler: 'by.jprof.telegram.bot.runners.lambda.JProf',
1826
environment: {
1927
'LOG_THRESHOLD': 'DEBUG',
28+
'TABLE_VOTES': votesTable.tableName,
29+
'TELEGRAM_BOT_TOKEN': props.telegramToken,
2030
},
2131
});
2232

33+
votesTable.grantReadWriteData(lambdaWebhook);
34+
2335
const api = new apigateway.RestApi(this, 'jprof-by-bot-api', {
2436
restApiName: 'jprof-by-bot-api',
2537
cloudWatchRole: false,

.deploy/lambda/package-lock.json

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.deploy/lambda/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
},
2020
"dependencies": {
2121
"@aws-cdk/aws-apigateway": "1.100.0",
22+
"@aws-cdk/aws-dynamodb": "1.100.0",
2223
"@aws-cdk/aws-lambda": "1.100.0",
2324
"@aws-cdk/core": "1.100.0",
2425
"source-map-support": "^0.5.16"

.github/workflows/default.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,5 @@ jobs:
7878
- run: ls -laR
7979
- uses: mikepenz/action-junit-report@v2
8080
with:
81-
report_paths: 'test-results/**/TEST-*.xml'
81+
report_paths: '**/test-results/**/TEST-*.xml'
8282
github_token: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/deploy.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ jobs:
3535
with:
3636
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
3737
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
38-
aws-region: eu-west-2
39-
- run: npx cdk deploy -O cdk.out/outputs.json
38+
aws-region: us-east-1
39+
- run: npx cdk deploy --outputs-file=cdk.out/outputs.json --require-approval=never
4040
working-directory: .deploy/lambda
4141
env:
4242
TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}

core/build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,9 @@ plugins {
55
dependencies {
66
api(libs.bundles.tgbotapi)
77
implementation(libs.log4j.api)
8+
9+
testImplementation(libs.junit.jupiter.api)
10+
testImplementation(libs.junit.jupiter.params)
11+
testRuntimeOnly(libs.junit.jupiter.engine)
12+
testRuntimeOnly(libs.log4j.core)
813
}

core/src/main/kotlin/by/jprof/telegram/bot/core/UpdateProcessingPipeline.kt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
package by.jprof.telegram.bot.core
22

33
import dev.inmo.tgbotapi.types.update.abstracts.Update
4-
import kotlinx.coroutines.CoroutineExceptionHandler
5-
import kotlinx.coroutines.joinAll
6-
import kotlinx.coroutines.launch
7-
import kotlinx.coroutines.runBlocking
8-
import kotlinx.coroutines.supervisorScope
4+
import kotlinx.coroutines.*
95
import org.apache.logging.log4j.LogManager
106

117
class UpdateProcessingPipeline(
12-
private val processors: List<UpdateProcessor>
8+
private val processors: List<UpdateProcessor>
139
) {
1410
companion object {
1511
private val logger = LogManager.getLogger(UpdateProcessingPipeline::class.java)!!
@@ -18,8 +14,8 @@ class UpdateProcessingPipeline(
1814
fun process(update: Update) = runBlocking {
1915
supervisorScope {
2016
processors
21-
.map { launch(exceptionHandler(it)) { it.process(update) } }
22-
.joinAll()
17+
.map { launch(exceptionHandler(it)) { it.process(update) } }
18+
.joinAll()
2319
}
2420
}
2521

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package by.jprof.telegram.bot.core
2+
3+
import dev.inmo.tgbotapi.types.update.abstracts.UnknownUpdate
4+
import dev.inmo.tgbotapi.types.update.abstracts.Update
5+
import kotlinx.coroutines.delay
6+
import kotlinx.serialization.json.JsonNull
7+
import org.junit.jupiter.api.Assertions
8+
import org.junit.jupiter.api.Test
9+
import java.time.Duration
10+
import java.util.concurrent.atomic.AtomicBoolean
11+
12+
internal class UpdateProcessingPipelineTest {
13+
@Test
14+
fun process() {
15+
val completionFlags = (1..5).map { AtomicBoolean(false) }
16+
val sut = UpdateProcessingPipeline(
17+
completionFlags.mapIndexed { index, atomicBoolean ->
18+
Delay((index + 1) * 1000L, atomicBoolean)
19+
}
20+
)
21+
22+
Assertions.assertTimeout(Duration.ofMillis(6000)) {
23+
sut.process(UnknownUpdate(1L, "", JsonNull))
24+
}
25+
Assertions.assertTrue(completionFlags.all { it.get() })
26+
}
27+
28+
@Test
29+
fun processWithBroken() {
30+
val completionFlags = (1..5).map { AtomicBoolean(false) }
31+
val sut = UpdateProcessingPipeline(
32+
completionFlags.mapIndexed { index, atomicBoolean ->
33+
when (index % 2) {
34+
0 -> Delay((index + 1) * 1000L, atomicBoolean)
35+
else -> Fail()
36+
}
37+
}
38+
)
39+
40+
Assertions.assertTimeout(Duration.ofMillis(6000)) {
41+
sut.process(UnknownUpdate(1L, "", JsonNull))
42+
}
43+
completionFlags.forEachIndexed { index, atomicBoolean ->
44+
if (index % 2 == 0) {
45+
Assertions.assertTrue(atomicBoolean.get())
46+
}
47+
}
48+
}
49+
}
50+
51+
internal class Delay(
52+
private val delay: Long,
53+
private val completionFlag: AtomicBoolean
54+
) : UpdateProcessor {
55+
override suspend fun process(update: Update) {
56+
delay(delay)
57+
completionFlag.set(true)
58+
}
59+
}
60+
61+
internal class Fail : UpdateProcessor {
62+
override suspend fun process(update: Update) {
63+
throw IllegalArgumentException()
64+
}
65+
}

gradle/libs.versions.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ jackson = "2.12.3"
1212

1313
tgbotapi = "0.33.3"
1414

15+
jsoup = "1.13.1"
16+
1517
log4j = "2.14.1"
1618

1719
junit5 = "5.7.1"
1820
aws-junit5 = "6.0.1"
21+
mockk = "1.11.0"
1922

2023
[libraries]
2124
kotlinx-coroutines-jdk8 = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-jdk8", version.ref = "coroutines" }
@@ -30,6 +33,9 @@ koin-core = { group = "io.insert-koin", name = "koin-core", version.ref = "koin"
3033
jackson-databind = { group = "com.fasterxml.jackson.core", name = "jackson-databind", version.ref = "jackson" }
3134

3235
tgbotapi-core = { group = "dev.inmo", name = "tgbotapi.core", version.ref = "tgbotapi" }
36+
tgbotapi-extensions-api = { group = "dev.inmo", name = "tgbotapi.extensions.api", version.ref = "tgbotapi" }
37+
38+
jsoup = { group = "org.jsoup", name = "jsoup", version.ref = "jsoup" }
3339

3440
log4j-api = { group = "org.apache.logging.log4j", name = "log4j-api", version.ref = "log4j" }
3541
log4j-core = { group = "org.apache.logging.log4j", name = "log4j-core", version.ref = "log4j" }
@@ -39,8 +45,9 @@ junit-jupiter-api = { group = "org.junit.jupiter", name = "junit-jupiter-api", v
3945
junit-jupiter-params = { group = "org.junit.jupiter", name = "junit-jupiter-params", version.ref = "junit5" }
4046
junit-jupiter-engine = { group = "org.junit.jupiter", name = "junit-jupiter-engine", version.ref = "junit5" }
4147
aws-junit5-dynamo-v2 = { group = "me.madhead.aws-junit5", name = "dynamo-v2", version.ref = "aws-junit5" }
48+
mockk = { group = "io.mockk", name = "mockk", version.ref = "mockk" }
4249

4350
[bundles]
44-
tgbotapi = ["tgbotapi-core"]
51+
tgbotapi = ["tgbotapi-core", "tgbotapi-extensions-api"]
4552
log4j = ["log4j-core", "log4j-slf4j-impl"]
4653
aws-lambda = ["aws-lambda-java-events", "aws-lambda-java-core", "aws-lambda-java-log4j2", "jackson-databind"]

jep/build.gradle.kts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
plugins {
2+
kotlin("jvm")
3+
}
4+
5+
dependencies {
6+
api(project(":core"))
7+
api(project(":votes"))
8+
implementation(project(":votes:tgbotapi-extensions"))
9+
implementation(libs.log4j.api)
10+
implementation(libs.jsoup)
11+
12+
testImplementation(libs.junit.jupiter.api)
13+
testImplementation(libs.junit.jupiter.params)
14+
testImplementation(libs.mockk)
15+
testRuntimeOnly(libs.junit.jupiter.engine)
16+
testRuntimeOnly(libs.log4j.core)
17+
}

0 commit comments

Comments
 (0)