Skip to content

Commit

Permalink
chore: added error handling when reading a file
Browse files Browse the repository at this point in the history
  • Loading branch information
ddemonaz authored and jigarkhwar committed Nov 29, 2022
1 parent 1b6d295 commit b21b66b
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
public final class ProfileBuilderNew {

public static YamlBuilder buildFromYaml(String path) {
return new YamlBuilder(ru.tinkoff.gatling.profile.ProfileBuilderNew.buildFromYaml(path));
return new YamlBuilder(ru.tinkoff.gatling.profile.ProfileBuilderNew.buildFromYamlJava(path));
}
}
50 changes: 33 additions & 17 deletions src/main/scala/ru/tinkoff/gatling/profile/ProfileBuilderNew.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package ru.tinkoff.gatling.profile

import io.circe
import io.circe.DecodingFailure
import io.circe.yaml._
import io.circe.generic.auto._
import io.gatling.core.Predef._
Expand All @@ -10,9 +8,12 @@ import io.gatling.http.Predef._
import io.gatling.http.request.builder.HttpRequestBuilder
import ru.tinkoff.gatling.utils.IntensityConverter.getIntensityFromString

import cats.syntax.either._

import java.io.FileNotFoundException
import scala.io.{BufferedSource, Source}
import scala.util.{Failure, Success, Try}
import java.nio.file.Paths
import scala.io.Source
import scala.util.Using
import scala.util.matching.Regex

case class Params(method: String, path: String, headers: Option[List[String]], body: Option[String])
Expand Down Expand Up @@ -63,21 +64,36 @@ case class Yaml(apiVersion: String, kind: String, metadata: Metadata, spec: Prof

object ProfileBuilderNew {

def buildFromYaml(path: String): Yaml = {
val tryReadFile: Try[BufferedSource] = Try{
Source.fromFile(s"""${System.getProperty("user.dir")}/$path""")
}
final case class ProfileBuilderException(msg: String, cause: Throwable) extends Throwable(msg, cause, false, false)

val bufferedSource: BufferedSource = tryReadFile.getOrElse(throw new FileNotFoundException(s"File $path was not found"))
val yamlContent = bufferedSource.mkString
bufferedSource.close()
private def toProfileBuilderException(path: String): PartialFunction[Throwable, ProfileBuilderException] = {
case e: FileNotFoundException => ProfileBuilderException(s"File not found $path", e)
case e: io.circe.Error => ProfileBuilderException(s"Incorrect file content in $path", e)
case e: Throwable => ProfileBuilderException(s"Unknown error", e)
}

def buildFromYaml(path: String): Yaml = {
val attemptToParse = for {
fullPath <- sys.props
.get("user.dir")
.toRight(new NoSuchElementException("'user.dir' property not defined"))
.map(Paths.get(_, path))
yamlContent <- Using(Source.fromFile(fullPath.toFile))(_.mkString).toEither
parsed <- parser.parse(yamlContent).flatMap(_.as[Yaml])
} yield parsed

attemptToParse.leftMap(toProfileBuilderException(path)).toTry.get
}

val yamlParsed: Either[circe.Error, Yaml] = parser.parse(yamlContent).flatMap(json => json.as[Yaml])
yamlParsed match {
case Right(yaml) => yaml
case Left(DecodingFailure(_, value)) =>
throw new IllegalArgumentException(s"""Field "${value.head.productElement(0)}" is not filled""")
case Left(error) => throw error
def buildFromYamlJava(path: String): Yaml = {
try {
val fullPath = Paths.get(sys.props.toMap.apply("user.dir"), path)
val yamlContent = Using.resource(Source.fromFile(fullPath.toFile))(_.mkString)
parser.parse(yamlContent).flatMap(_.as[Yaml]).toTry.get
} catch {
case e: FileNotFoundException => throw ProfileBuilderException(s"File not found $path", e)
case e: io.circe.Error => throw ProfileBuilderException(s"Incorrect file content in $path", e)
case e: Exception => throw ProfileBuilderException(s"Unknown error", e)
}
}
}
16 changes: 16 additions & 0 deletions src/test/resources/profileTemplates/incorrectProfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: link.ru/v1alpha1
kind: PerformanceTestProfiles
spec:
profiles:
- name: maxPerf
protocol: http
profile:
- request: request-1
intensity: 100 rph
groups: ["Group1"]
params:
method: POST
path: /test/a
headers:
- 'greetings: Hello world!'
body: '{"a": "b"}'
40 changes: 38 additions & 2 deletions src/test/scala/ru/tinkoff/gatling/profile/ProfileBuilderTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ru.tinkoff.gatling.profile
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks
import ru.tinkoff.gatling.profile.ProfileBuilderNew.ProfileBuilderException

import java.io.FileNotFoundException

Expand Down Expand Up @@ -53,11 +54,46 @@ class ProfileBuilderTest extends AnyFlatSpec with Matchers with ScalaCheckDriven
ProfileBuilderNew.buildFromYaml(profile1FromFile).selectProfile("maxPerf") shouldBe parsedProfile
}

it should "Java test expected exceptions if file not exists" in {
val thrown = intercept[ProfileBuilderException] {
ProfileBuilderNew.buildFromYamlJava("notExistsFile")
}
assert(thrown.getMessage.contains("File not found notExistsFile"))
}

it should "Java test expected exceptions if file path not passed" in {
val thrown = intercept[ProfileBuilderException] {
ProfileBuilderNew.buildFromYamlJava("")
}
assert(thrown.getMessage.contains("File not found"))
}

it should "Java test expected exceptions if incorrect file content" in {
val thrown = intercept[ProfileBuilderException] {
ProfileBuilderNew.buildFromYamlJava("src/test/resources/profileTemplates/incorrectProfile.yml")
}
assert(thrown.getMessage.contains("Incorrect file content in src/test/resources/profileTemplates/incorrectProfile.yml"))
}

it should "test expected exceptions if file not exists" in {
val thrown = intercept[Exception] {
val thrown = intercept[ProfileBuilderException] {
ProfileBuilderNew.buildFromYaml("notExistsFile")
}
assert(thrown.getMessage == s"File notExistsFile was not found")
assert(thrown.getMessage.contains("File not found notExistsFile"))
}

it should "test expected exceptions if file path not passed" in {
val thrown = intercept[ProfileBuilderException] {
ProfileBuilderNew.buildFromYaml("")
}
assert(thrown.getMessage.contains("File not found"))
}

it should "test expected exceptions if incorrect file content" in {
val thrown = intercept[ProfileBuilderException] {
ProfileBuilderNew.buildFromYaml("src/test/resources/profileTemplates/incorrectProfile.yml")
}
assert(thrown.getMessage.contains("Incorrect file content in src/test/resources/profileTemplates/incorrectProfile.yml"))
}

}

0 comments on commit b21b66b

Please sign in to comment.