Commit e05d8faa authored by Andreas Muttscheller's avatar Andreas Muttscheller

Fix integration tests

parent 9fb17ef3
......@@ -212,8 +212,8 @@ object AwsUtils extends AutoPlugin {
println(s"Updating function $functionName with IP $mockServerIp")
val envVars = functionConf.getEnvironment.getVariables
envVars.put("SLACK_API_URL", s"$mockServerIp/slack")
envVars.put("DB_API_URL", s"$mockServerIp/dbapi")
envVars.put("SLACK_API_URL", s"http://$mockServerIp:1080/slack")
envVars.put("DB_API_URL", s"http://$mockServerIp:1080/dbapi")
lambdaClient.updateFunctionConfiguration(new UpdateFunctionConfigurationRequest()
.withFunctionName(functionName)
......@@ -235,7 +235,7 @@ object AwsUtils extends AutoPlugin {
val elasticFuture = clearElasticSearchIndex("stations", "timetables")(serviceName, stage)
val tables = List("Stations", "SlackUsers", "TimetableStops", "PlannedTimetables")
val tables = List("Stations", "SlackUsers", "TimetableStops", "EnabledStations")
tables.map(t => (t, clearDynamoDBTable(t)(serviceName, stage))).foreach { f =>
Await.ready(f._2, Duration.Inf).value.get match {
case Success(ftn) =>
......
......@@ -142,10 +142,10 @@ resources:
AttributeName: ttl
Enabled: true
BillingMode: PAY_PER_REQUEST
PlannedTimetables:
EnabledStations:
Type: AWS::DynamoDB::Table
Properties:
TableName: ${self:service}-${self:provider.stage}-PlannedTimetables
TableName: ${self:service}-${self:provider.stage}-EnabledStations
AttributeDefinitions:
- AttributeName: eva
AttributeType: S
......
......@@ -7,12 +7,12 @@ import java.util.concurrent.{CountDownLatch, TimeUnit}
import ch.qos.logback.classic.{Level, LoggerContext}
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder
import com.amazonaws.services.dynamodbv2.document.DynamoDB
import com.amazonaws.services.dynamodbv2.document.{DynamoDB, Item}
import com.amazonaws.services.lambda.AWSLambdaClientBuilder
import com.amazonaws.services.lambda.model.{InvocationType, InvokeRequest}
import com.softwaremill.sttp._
import com.typesafe.config.{Config, ConfigFactory}
import de.codecentric.amuttsch.bahndelayinfo.models.PlannedTimetableElement
import de.codecentric.amuttsch.bahndelayinfo.models.EnabledStationItem
import org.json4s._
import org.json4s.native.JsonMethods._
import org.json4s.native.Serialization.write
......@@ -45,7 +45,7 @@ class ScheduledPlannedTimetableFetchTest extends FunSpec with BeforeAndAfterEach
}
describe("ScheduledPlannedTimetableFetchTest") {
it("should fetch ") {
it("should fetch planned timetables from mockserver") {
val countDownLatch = new CountDownLatch(24)
......@@ -61,6 +61,8 @@ class ScheduledPlannedTimetableFetchTest extends FunSpec with BeforeAndAfterEach
.replace(LocalDate.now.format(format), "190215")
.replace("/dbapi/timetables/v1/plan", "dbapi") + ".xml"
println(resourceFileName)
val resp = Source.fromResource(resourceFileName).mkString
countDownLatch.countDown()
......@@ -79,15 +81,18 @@ class ScheduledPlannedTimetableFetchTest extends FunSpec with BeforeAndAfterEach
lambdaClient.invoke(rq)
countDownLatch.await(3, TimeUnit.MINUTES)
Thread.sleep(500)
val ddbClient = AmazonDynamoDBClientBuilder.standard.build
val ddb = new DynamoDB(ddbClient)
val tablePlannedTimetables = ddb.getTable(aws.getServerlessServiceName("PlannedTimetables"))
val tableEnabledStations = ddb.getTable(aws.getServerlessServiceName("EnabledStations"))
val tableTimetableStops = ddb.getTable(aws.getServerlessServiceName("TimetableStops"))
val plannedTimetableItem = tablePlannedTimetables.getItem("eva", "8000105")
val plannedTimetable = parse(plannedTimetableItem.toJSON).extract[PlannedTimetableElement]
var enabledStationItem: Item = null
while (enabledStationItem == null) {
enabledStationItem = tableEnabledStations.getItem("eva", "8000105")
Thread.sleep(1000)
}
val plannedTimetable = parse(enabledStationItem.toJSON).extract[EnabledStationItem]
assert(plannedTimetable.lastFetched == LocalDate.now.toString)
val stops = tableTimetableStops.scan()
......
package de.codecentric.amuttsch.bahndelayinfo.aws.lambda
import com.amazonaws.services.dynamodbv2.document.{DynamoDB, Index, Item, Table}
import com.amazonaws.services.dynamodbv2.document.{DynamoDB, Item, Table}
import com.amazonaws.services.dynamodbv2.{AmazonDynamoDB, AmazonDynamoDBClientBuilder}
import com.amazonaws.services.lambda.runtime.Context
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent
......@@ -26,9 +26,6 @@ class APISlackBotEventHandlerWorker {
val ddbClient: AmazonDynamoDB = AmazonDynamoDBClientBuilder.standard.build
val ddb: DynamoDB = new DynamoDB(ddbClient)
val tableSlackUsers: Table = ddb.getTable(aws.getServerlessServiceName("SlackUsers"))
val tablePlannedTimetables: Table = ddb.getTable(aws.getServerlessServiceName("PlannedTimetables"))
val tableStations: Table = ddb.getTable(aws.getServerlessServiceName("Stations"))
val indexStationName: Index = tableStations.getIndex("NameIndex")
val apiConf: Config = ConfigFactory.load("api")
val token: String = apiConf.getString("slack.token")
......
......@@ -9,7 +9,7 @@ import com.typesafe.scalalogging.Logger
import de.codecentric.amuttsch.bahndelayinfo.aws
import de.codecentric.amuttsch.bahndelayinfo.aws.sns.SNSNewDelayInformation
import de.codecentric.amuttsch.bahndelayinfo.fetcher.DBChangedTimetableFetcher
import de.codecentric.amuttsch.bahndelayinfo.models.{PlannedTimetableElement, TimetableInformation}
import de.codecentric.amuttsch.bahndelayinfo.models.{EnabledStationItem, TimetableInformation}
import de.codecentric.amuttsch.bahndelayinfo.utils.JsonSerializers
import org.json4s._
import org.json4s.native.JsonMethods._
......@@ -32,15 +32,15 @@ class ScheduledChangesFetchService {
snsClient.createTopic(aws.getServerlessServiceName("ChangedTimetableStops")).getTopicArn
val ddb: DynamoDB = new DynamoDB(ddbClient)
val tablePlannedTimetables: Table = ddb.getTable(aws.getServerlessServiceName("PlannedTimetables"))
val tableEnabledStations: Table = ddb.getTable(aws.getServerlessServiceName("EnabledStations"))
val tableTimetableStops: Table = ddb.getTable(aws.getServerlessServiceName("TimetableStops"))
def handleRequest(event: ScheduledEvent, context: Context): Unit = {
val evasToFetchItemCollection: ItemCollection[ScanOutcome] = tablePlannedTimetables.scan()
val evasToFetchItemCollection: ItemCollection[ScanOutcome] = tableEnabledStations.scan()
// Download timetable
evasToFetchItemCollection.forEach { plannedTimetableElementItem =>
val element: PlannedTimetableElement = parse(plannedTimetableElementItem.toJSON).extract[PlannedTimetableElement]
val element: EnabledStationItem = parse(plannedTimetableElementItem.toJSON).extract[EnabledStationItem]
val evaToFetch = List(element.eva)
val timetableInformations = dbChangedTimetableFetcher.fetchTimetablesForEvas(evaToFetch)
......
......@@ -10,7 +10,7 @@ import com.amazonaws.services.lambda.runtime.Context
import com.amazonaws.services.lambda.runtime.events.ScheduledEvent
import com.typesafe.scalalogging.Logger
import de.codecentric.amuttsch.bahndelayinfo.aws
import de.codecentric.amuttsch.bahndelayinfo.models.PlannedTimetableElement
import de.codecentric.amuttsch.bahndelayinfo.models.EnabledStationItem
import org.json4s._
import org.json4s.native.JsonMethods._
import org.json4s.native.Serialization.write
......@@ -28,16 +28,16 @@ class ScheduledPlannedTimetableFetchService {
val lambdaClient = AWSLambdaClientBuilder.defaultClient
val ddb = new DynamoDB(ddbClient)
val tablePlannedTimetables = ddb.getTable(aws.getServerlessServiceName("PlannedTimetables"))
val tableEnabledStations = ddb.getTable(aws.getServerlessServiceName("EnabledStations"))
val sf: ScanFilter = new ScanFilter("lastFetched").ne(LocalDate.now.toString)
val evasToFetchItemCollection = tablePlannedTimetables.scan(
val evasToFetchItemCollection = tableEnabledStations.scan(
sf
)
// Download timetable
var evaToFetchList = ListBuffer.empty[PlannedTimetableElement]
var evaToFetchList = ListBuffer.empty[EnabledStationItem]
evasToFetchItemCollection.forEach { plannedTimetableElementItem =>
val element: PlannedTimetableElement = parse(plannedTimetableElementItem.toJSON).extract[PlannedTimetableElement]
val element: EnabledStationItem = parse(plannedTimetableElementItem.toJSON).extract[EnabledStationItem]
evaToFetchList += element
}
......
......@@ -12,7 +12,7 @@ import com.sksamuel.elastic4s.http.ElasticDsl.{createIndex, _}
import com.typesafe.scalalogging.Logger
import de.codecentric.amuttsch.bahndelayinfo.aws
import de.codecentric.amuttsch.bahndelayinfo.fetcher.DBPlannedTimetableFetcher
import de.codecentric.amuttsch.bahndelayinfo.models.PlannedTimetableElement
import de.codecentric.amuttsch.bahndelayinfo.models.EnabledStationItem
import org.json4s._
import org.json4s.native.Serialization.write
......@@ -26,7 +26,7 @@ class ScheduledPlannedTimetableFetchWorker {
val dbPlannedTimetableFetcher: DBPlannedTimetableFetcher = new DBPlannedTimetableFetcher()
val ddb: DynamoDB = new DynamoDB(ddbClient)
val tablePlannedTimetables: Table = ddb.getTable(aws.getServerlessServiceName("PlannedTimetables"))
val tableEnabledStations: Table = ddb.getTable(aws.getServerlessServiceName("EnabledStations"))
val tableTimetableStops: Table = ddb.getTable(aws.getServerlessServiceName("TimetableStops"))
val esClient: ElasticClient = aws.elasticSearchClient()
......@@ -67,12 +67,12 @@ class ScheduledPlannedTimetableFetchWorker {
station = tti.station
}
val newElement = PlannedTimetableElement(
val newElement = EnabledStationItem(
eva,
LocalDate.now.toString,
station
)
tablePlannedTimetables.putItem(Item.fromJSON(write(newElement)))
tableEnabledStations.putItem(Item.fromJSON(write(newElement)))
if (tail.nonEmpty) {
val json = write(tail)
......
......@@ -43,10 +43,11 @@ private[fetcher] trait DBFetcher {
}
def fetchChanges(ttr: TimetableRequest): TimetableResponse = {
logger.info(f"Fetching from ${fetchUrl(ttr)}")
val url = fetchUrl(ttr)
logger.info(f"Fetching from $url, ${url.host} - ${url.path}")
val response = sttp
.get(fetchUrl(ttr))
.get(url)
.headers(
("Authorization", s"Bearer ${apiConf.getString("dbapi.api_token")}")
)
......
package de.codecentric.amuttsch.bahndelayinfo.models
case class PlannedTimetableElement(
case class EnabledStationItem(
eva: String,
lastFetched: String,
station: String
......
......@@ -57,7 +57,7 @@ sealed trait DynamoDbConnection extends SlackCommand {
val ddb: DynamoDB = new DynamoDB(ddbClient)
val tableSlackUsers: Table = ddb.getTable(aws.getServerlessServiceName("SlackUsers"))
val tablePlannedTimetables: Table = ddb.getTable(aws.getServerlessServiceName("PlannedTimetables"))
val tableEnabledStations: Table = ddb.getTable(aws.getServerlessServiceName("EnabledStations"))
val tableStations: Table = ddb.getTable(aws.getServerlessServiceName("Stations"))
val indexStationName: Index = tableStations.getIndex("NameIndex")
}
......@@ -298,7 +298,7 @@ case object ListActiveStations extends SlackCommand with StationFinder with Para
implicit slackUser: SlackUser,
slackApiClient: SlackClient
): Unit = {
val stations = aws.dynamoDBScanToScala(tablePlannedTimetables.scan())
val stations = aws.dynamoDBScanToScala(tableEnabledStations.scan())
.map { i =>
val s = tableStations.getItem("eva", i.getString("eva").toInt)
parse(s.toJSON).extract[Station]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment