Skip to content

Commit 17d822b

Browse files
arielmirraAgustinBettati
arielmirra
authored andcommitted
APIMF-2997: add scalatest, refactor all scala tests
1 parent 75a6bb1 commit 17d822b

12 files changed

+183
-310
lines changed

Diff for: .scalafmt.conf

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
version = 2.5.0
1+
version = 2.5.0
2+
maxColumn = 100

Diff for: AMF5/build.gradle

+9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ plugins {
22
id "com.moowork.node" version "1.3.1"
33
id 'java'
44
id 'scala'
5+
id "com.github.maiflai.scalatest" version "0.31"
56
}
67

78
sourceCompatibility = 1.8
@@ -29,6 +30,14 @@ dependencies {
2930
testCompile group: 'org.assertj', name: 'assertj-core', version: '3.18.1'
3031
// https://mvnrepository.com/artifact/junit/junit
3132
testCompile group: 'junit', name: 'junit', version: '4.13.1'
33+
// gradle-scalatest
34+
testCompile 'org.scalatest:scalatest_2.12:3.2.0'
35+
testRuntime 'com.vladsch.flexmark:flexmark-all:0.35.10'
36+
}
37+
38+
task testAll {
39+
dependsOn test
40+
dependsOn scalatest
3241
}
3342

3443
tasks.test.dependsOn(tasks.npm_test)

Diff for: AMF5/gradle.properties

Whitespace-only changes.

Diff for: AMF5/src/test/scala/ConversionTestScala.scala

+18-35
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,28 @@
1+
import amf.client.environment.WebAPIConfiguration
12
import amf.plugins.document.apicontract.resolution.pipelines.compatibility.{Oas20CompatibilityPipeline, Raml10CompatibilityPipeline}
2-
import org.junit.Assert._
3-
import org.junit.Test
3+
import org.scalatest.flatspec.AsyncFlatSpec
4+
import org.scalatest.matchers.should
45

5-
import scala.concurrent.Await
6-
import scala.concurrent.duration.Duration
6+
class ConversionTestScala extends AsyncFlatSpec with should.Matchers {
77

8-
class ConversionTestScala {
9-
10-
import amf.client.environment.WebAPIConfiguration
11-
12-
import scala.concurrent.ExecutionContext.Implicits.global
13-
14-
@Test def Raml10ToOas20Conversion(): Unit = {
8+
"AMF" should "convert a RAML 1.0 API to OAS 2.0" in {
159
val client = WebAPIConfiguration.WebAPI().createClient()
16-
val parseResult = Await.result(
17-
client.parse("file://resources/examples/banking-api.raml"),
18-
Duration.Inf
19-
)
20-
val transformResult =
21-
client.transform(parseResult.bu, Oas20CompatibilityPipeline.name)
22-
val renderResult =
23-
client.render(transformResult.bu, "application/oas20+json")
24-
val readApi = getStrFromFile(
25-
"resources/expected/converted-banking-api.json"
26-
)
27-
assertEquals(readApi, renderResult)
10+
client.parse("file://AMF5/resources/examples/banking-api.raml") map { parseResult =>
11+
val transformResult = client.transform(parseResult.bu, Oas20CompatibilityPipeline.name)
12+
val renderResult = client.render(transformResult.bu, "application/oas20+json")
13+
val readApi = getStrFromFile("AMF5/resources/expected/converted-banking-api.json")
14+
renderResult shouldEqual readApi
15+
}
2816
}
2917

30-
@Test def Oas20ToRaml10Conversion(): Unit = {
18+
"AMF" should "convert an OAS 2.0 API to RAML 1.0" in {
3119
val client = WebAPIConfiguration.WebAPI().createClient()
32-
client
33-
.parse("file://resources/examples/banking-api.json")
34-
.map(parseResult => {
35-
val transformResult =
36-
client.transform(parseResult.bu, Raml10CompatibilityPipeline.name)
37-
val renderResult =
38-
client.render(transformResult.bu, "application/raml10+yaml")
39-
val readApi =
40-
getStrFromFile("resources/expected/converted-banking-api.raml")
41-
assertEquals(readApi, renderResult)
42-
})
20+
client.parse("file://AMF5/resources/examples/banking-api.json") map { parseResult =>
21+
val transformResult = client.transform(parseResult.bu, Raml10CompatibilityPipeline.name)
22+
val renderResult = client.render(transformResult.bu, "application/raml10+yaml")
23+
val readApi = getStrFromFile("AMF5/resources/expected/converted-banking-api.raml")
24+
renderResult shouldEqual readApi
25+
}
4326
}
4427

4528
private def getStrFromFile(path: String): String = {

Diff for: AMF5/src/test/scala/ErrorHandlerTestScala.scala

+12-14
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
import amf.client.environment.RAMLConfiguration
22
import amf.core.errorhandling.UnhandledErrorHandler
33
import amf.plugins.document.apicontract.resolution.pipelines.Raml10TransformationPipeline
4-
import org.junit.Test
4+
import org.scalatest.flatspec.AsyncFlatSpec
5+
import org.scalatest.matchers.should
56

6-
import scala.concurrent.Await
7-
import scala.concurrent.duration.Duration
7+
class ErrorHandlerTestScala extends AsyncFlatSpec with should.Matchers {
88

9-
class ErrorHandlerTestScala {
10-
11-
@Test(expected = classOf[java.lang.Exception])
12-
def customErrorHandler(): Unit = {
9+
"AMF client" should "use a custom error handler provider" in {
1310
val client =
1411
RAMLConfiguration
1512
.RAML10()
16-
.withErrorHandlerProvider(() => UnhandledErrorHandler)
13+
.withErrorHandlerProvider(() =>
14+
UnhandledErrorHandler
15+
) // throws an exception when an error is found
1716
.createClient()
1817

19-
val parseResult = Await.result(
20-
client.parse("file://resources/examples/resolution-error.raml"),
21-
Duration.Inf
22-
)
23-
24-
client.transform(parseResult.bu, Raml10TransformationPipeline.name)
18+
client.parse("file://AMF5/resources/examples/resolution-error.raml") map { parseResult =>
19+
assertThrows[java.lang.Exception] {
20+
client.transform(parseResult.bu, Raml10TransformationPipeline.name)
21+
}
22+
}
2523
}
2624
}

Diff for: AMF5/src/test/scala/ModelBuilderTestScala.scala

+7-9
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ import amf.client.exported.RAMLConfiguration
22
import amf.client.model.document.Document
33
import amf.client.model.domain.{Request, WebApi}
44
import org.junit.Assert.assertTrue
5-
import org.junit.Test
5+
import org.scalatest.flatspec.AsyncFlatSpec
6+
import org.scalatest.matchers.should
67

78
import java.util.Collections
89

9-
class ModelBuilderTestScala {
10+
class ModelBuilderTestScala extends AsyncFlatSpec with should.Matchers {
1011

11-
@Test def buildWebApiTest(): Unit = {
12+
"AMF" should "create a valid model in memory" in {
1213
val stringDataType = "http://www.w3.org/2001/XMLSchema#string"
13-
14+
val client = RAMLConfiguration.RAML().createClient()
1415
val api = new WebApi()
1516
.withName("Music Service API")
1617
.withVersion("v1")
@@ -102,11 +103,8 @@ class ModelBuilderTestScala {
102103
model.withEncodes(api)
103104

104105
// Run RAML default validations on parsed unit (expects one error -> invalid protocols value)
105-
val client = RAMLConfiguration.RAML().createClient()
106106
val validationReport = client.validate(model).get()
107-
assertTrue(
108-
"Created model in memory should be valid",
109-
validationReport.conforms
110-
)
107+
validationReport.conforms shouldBe true
108+
validationReport.results.size() shouldBe 0
111109
}
112110
}

Diff for: AMF5/src/test/scala/ParsingTestNativeScala.scala

-80
This file was deleted.

Diff for: AMF5/src/test/scala/ParsingTestScala.scala

+27-47
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
import amf.client.exported.{OASConfiguration, RAMLConfiguration}
2-
import org.junit.Assert._
3-
import org.junit.Test
1+
import amf.client.environment.{OASConfiguration, RAMLConfiguration}
2+
import org.scalatest.flatspec.AsyncFlatSpec
3+
import org.scalatest.matchers.should
44

5+
class ParsingTestScala extends AsyncFlatSpec with should.Matchers {
56

6-
class ParsingTestScala {
7-
8-
@Test def parseOas20(): Unit = {
7+
"AMF client" should "parse an OAS 2.0 API" in {
98
val client = OASConfiguration.OAS20().createClient()
10-
val parsingResult = client.parse("file://resources/examples/banking-api.json").get()
11-
assertTrue(parsingResult.conforms)
9+
client.parse("file://AMF5/resources/examples/banking-api.json") map (_.conforms shouldBe true)
1210
}
1311

14-
@Test def parseOas20String(): Unit = {
12+
it should "parse an OAS 2.0 API from a string" in {
1513
val client = OASConfiguration.OAS20().createClient()
1614
val api =
1715
"""{
@@ -22,63 +20,45 @@ class ParsingTestScala {
2220
| },
2321
| "host": "acme-banking.com"
2422
|}""".stripMargin
25-
val parsingResult = client.parseContent(api).get();
26-
assertTrue(parsingResult.conforms)
23+
client.parseContent(api) map (_.conforms shouldBe true)
2724
}
2825

29-
@Test def parseOas30(): Unit = {
30-
val parsingResult = OASConfiguration
31-
.OAS30()
32-
.createClient()
33-
.parse("file://resources/examples/banking-api-oas30.json")
34-
.get()
35-
assertTrue(parsingResult.conforms)
26+
it should "parse an OAS 3.0 API" in {
27+
val client = OASConfiguration.OAS30().createClient()
28+
client.parse(
29+
"file://AMF5/resources/examples/banking-api-oas30.json"
30+
) map (_.conforms shouldBe true)
3631
}
3732

38-
@Test def parseRaml10(): Unit = {
39-
val parsingResult = RAMLConfiguration
40-
.RAML10()
41-
.createClient()
42-
.parse("file://resources/examples/banking-api.raml")
43-
.get()
44-
assertTrue(parsingResult.conforms)
33+
it should "parse an RAML 1.0 API" in {
34+
val client = RAMLConfiguration.RAML10().createClient()
35+
client.parse("file://AMF5/resources/examples/banking-api.raml") map (_.conforms shouldBe true)
4536
}
4637

47-
@Test def parseRaml10String(): Unit = {
38+
it should "parse an RAML 1.0 API from a string" in {
39+
val client = RAMLConfiguration.RAML10().createClient()
4840
val api =
4941
"""#%RAML 1.0
5042
|
5143
|title: ACME Banking HTTP API
5244
|version: 1.0""".stripMargin
53-
val parsingResult = RAMLConfiguration
54-
.RAML10()
55-
.createClient()
56-
.parseContent(api)
57-
.get()
58-
assertTrue(parsingResult.conforms)
45+
client.parseContent(api) map (_.conforms shouldBe true)
5946
}
6047

61-
@Test def parseRaml08(): Unit = {
62-
val parsingResult = RAMLConfiguration
63-
.RAML08()
64-
.createClient()
65-
.parse("file://resources/examples/banking-api-08.raml")
66-
.get()
67-
assertTrue(parsingResult.conforms)
48+
it should "parse an RAML 0.8 API" in {
49+
val client = RAMLConfiguration.RAML08().createClient()
50+
client.parse(
51+
"file://AMF5/resources/examples/banking-api-08.raml"
52+
) map (_.conforms shouldBe true)
6853
}
6954

70-
@Test def parseRaml08String(): Unit = {
55+
it should "parse an RAML 0.8 API from a string" in {
56+
val client = RAMLConfiguration.RAML08().createClient()
7157
val api =
7258
"""#%RAML 0.8
7359
|
7460
|title: ACME Banking HTTP API
7561
|version: 1.0""".stripMargin
76-
val parsingResult = RAMLConfiguration
77-
.RAML08()
78-
.createClient()
79-
.parseContent(api)
80-
.get()
81-
assertTrue(parsingResult.conforms)
62+
client.parseContent(api) map (_.conforms shouldBe true)
8263
}
83-
// TODO: add graph parse
8464
}

0 commit comments

Comments
 (0)