You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nlpcraft.apache.org by se...@apache.org on 2020/09/17 19:34:51 UTC
[incubator-nlpcraft] 04/06: WIP.
This is an automated email from the ASF dual-hosted git repository.
sergeykamov pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
commit 750b3e96bd5b549be4b7fb53f1226be40c77452f
Author: Sergey Kamov <se...@apache.org>
AuthorDate: Thu Sep 17 11:58:16 2020 +0300
WIP.
---
.../scala/org/apache/nlpcraft/NCTestContext.scala | 97 +++++++++++++++-------
...estContextModel.java => NCTestEnvironment.java} | 10 ++-
.../nlpcraft/examples/sql/NCSqlExampleSpec.scala | 36 ++------
.../nlpcraft/examples/time/NCTimeModelSpec.scala | 33 ++------
.../model/conversation/NCConversationSpec.scala | 10 +--
.../nlpcraft/models/stm/NCStmTestModelSpec.scala | 34 ++------
.../nlpcraft/server/rest/NCRestAskSpec.scala | 10 +--
.../nlpcraft/server/rest/NCRestClearSpec.scala | 10 +--
.../nlpcraft/server/rest/NCRestModelSpec.scala | 11 +--
.../nlpcraft/server/rest/NCRestProbeSpec.scala | 11 +--
.../apache/nlpcraft/server/rest/NCRestSpec.scala | 3 +-
11 files changed, 113 insertions(+), 152 deletions(-)
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/NCTestContext.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/NCTestContext.scala
index 7003cc4..892541f 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/NCTestContext.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/NCTestContext.scala
@@ -17,61 +17,100 @@
package org.apache.nlpcraft
-import java.io.IOException
-
-import org.apache.nlpcraft.common.NCException
-import org.apache.nlpcraft.model.NCModel
import org.apache.nlpcraft.model.tools.embedded.NCEmbeddedProbe
import org.apache.nlpcraft.model.tools.test.{NCTestClient, NCTestClientBuilder}
import org.apache.nlpcraft.probe.mgrs.model.NCModelManager
import org.junit.jupiter.api.TestInstance.Lifecycle
-import org.junit.jupiter.api.{AfterEach, BeforeAll, BeforeEach, TestInfo, TestInstance}
+import org.junit.jupiter.api._
/**
*
*/
@TestInstance(Lifecycle.PER_CLASS)
-class NCTestContext {
- protected var cli: NCTestClient = _
+abstract class NCTestContext {
+ private final val MDL_CLASS = classOf[NCTestEnvironment]
+ private var cli: NCTestClient = _
private var probeStarted = false
@BeforeEach
- @throws[NCException]
- @throws[IOException]
- private def beforeEach(ti: TestInfo): Unit = {
- if (ti.getTestMethod.isPresent) {
- val a = ti.getTestMethod.get().getAnnotation(classOf[NCTestContextModel])
+ @throws[Exception]
+ private def beforeEach(info: TestInfo): Unit = start0(() ⇒ getMethodAnnotation(info))
+
+ @BeforeAll
+ @throws[Exception]
+ private def beforeAll(info: TestInfo): Unit = start0(() ⇒ getClassAnnotation(info))
+
+ @AfterEach
+ @throws[Exception]
+ private def afterEach(info: TestInfo): Unit =
+ if (getMethodAnnotation(info).isDefined)
+ stop0()
+
+ @AfterAll
+ @throws[Exception]
+ private def afterAll(info: TestInfo): Unit =
+ if (getClassAnnotation(info).isDefined)
+ stop0()
+
+ private def getClassAnnotation(info: TestInfo) =
+ if (info.getTestClass.isPresent) Option(info.getTestClass.get().getAnnotation(MDL_CLASS)) else None
+
+ private def getMethodAnnotation(info: TestInfo): Option[NCTestEnvironment] =
+ if (info.getTestMethod.isPresent) Option(info.getTestMethod.get().getAnnotation(MDL_CLASS)) else None
+
+ @throws[Exception]
+ private def start0(extract: () ⇒ Option[NCTestEnvironment]): Unit =
+ extract() match {
+ case Some(ann) ⇒
+ if (probeStarted || cli != null)
+ throw new IllegalStateException(
+ "Model already initialized. " +
+ s"Note that '@${classOf[NCTestEnvironment].getSimpleName}' can be set for class or method, " +
+ s"but not both of them."
+ )
+
+ preProbeStart()
- if (a != null) {
probeStarted = false
- NCEmbeddedProbe.start(a.value())
+
+ NCEmbeddedProbe.start(ann.model())
+
probeStarted = true
- cli = new NCTestClientBuilder().newBuilder.build
+ if (ann.startClient()) {
+ cli = new NCTestClientBuilder().newBuilder.build
- cli.open(NCModelManager.getAllModels().head.model.getId)
- }
+ cli.open(NCModelManager.getAllModels().head.model.getId)
+ }
+ case None ⇒ // No-op.
}
- }
- @AfterEach
- @throws[NCException]
- @throws[IOException]
- private def afterEach(): Unit =
- if (cli != null)
+ @throws[Exception]
+ private def stop0(): Unit = {
+ if (cli != null) {
cli.close()
+ cli = null
+ }
+
if (probeStarted) {
NCEmbeddedProbe.stop()
probeStarted = false
+
+ afterProbeStop()
}
+ }
- @BeforeAll
- @throws[NCException]
- @throws[IOException]
- private def beforeAll(ti: TestInfo): Unit = {
- println("!!!ti=" + ti)
+ protected def preProbeStart(): Unit = { }
+
+ protected def afterProbeStop(): Unit = { }
+
+ final protected def getClient: NCTestClient = {
+ if (cli == null)
+ throw new IllegalStateException("Client is not started.")
+
+ cli
}
-}
+}
\ No newline at end of file
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/NCTestContextModel.java b/nlpcraft/src/test/scala/org/apache/nlpcraft/NCTestEnvironment.java
similarity index 84%
rename from nlpcraft/src/test/scala/org/apache/nlpcraft/NCTestContextModel.java
rename to nlpcraft/src/test/scala/org/apache/nlpcraft/NCTestEnvironment.java
index 5537fc7..2461ee6 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/NCTestContextModel.java
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/NCTestEnvironment.java
@@ -24,11 +24,13 @@ import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Documented
@Retention(value=RUNTIME)
-@Target(value=METHOD)
-public @interface NCTestContextModel {
- Class<? extends NCModel> value();
-}
+@Target(value = {METHOD, TYPE})
+public @interface NCTestEnvironment {
+ Class<? extends NCModel> model();
+ boolean startClient() default false;
+}
\ No newline at end of file
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/examples/sql/NCSqlExampleSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/examples/sql/NCSqlExampleSpec.scala
index 13736a3..10cd3e8 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/examples/sql/NCSqlExampleSpec.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/examples/sql/NCSqlExampleSpec.scala
@@ -25,19 +25,19 @@ import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import org.apache.nlpcraft.common.ascii.NCAsciiTable
import org.apache.nlpcraft.examples.sql.db.SqlServer
-import org.apache.nlpcraft.model.tools.embedded.NCEmbeddedProbe
-import org.apache.nlpcraft.model.tools.test.{NCTestClient, NCTestClientBuilder}
+import org.apache.nlpcraft.{NCTestContext, NCTestEnvironment}
import org.junit.jupiter.api.{AfterEach, BeforeEach, Test}
import scala.collection.JavaConverters._
-import scala.compat.java8.OptionConverters._
+import scala.compat.java8.OptionConverters.RichOptionalGeneric
/**
* SQL model test.
*
* @see SqlModel
*/
-class NCSqlExampleSpec {
+@NCTestEnvironment(model = classOf[SqlModel], startClient = true)
+class NCSqlExampleSpec extends NCTestContext {
private val GSON = new Gson
private val TYPE_RESP = new TypeToken[util.Map[String, Object]]() {}.getType
private val NORM = Seq("\n", "\r", "\t")
@@ -48,30 +48,10 @@ class NCSqlExampleSpec {
newTag((_: java.lang.Boolean) ⇒ "**").
build
- private var client: NCTestClient = _
-
case class Case(texts: Seq[String], sql: String)
- @BeforeEach
- def setUp(): Unit = {
- SqlServer.start()
-
- NCEmbeddedProbe.start(classOf[SqlModel])
-
- client = new NCTestClientBuilder().newBuilder.setResponseLog(false).build
-
- client.open("sql.model.id")
- }
-
- @AfterEach
- def tearDown(): Unit = {
- if (client != null)
- client.close()
-
- NCEmbeddedProbe.stop()
-
- SqlServer.stop()
- }
+ override protected def preProbeStart(): Unit = SqlServer.start()
+ override protected def afterProbeStop(): Unit = SqlServer.stop()
private def normalize(s: String): String =
NORM.
@@ -90,11 +70,13 @@ class NCSqlExampleSpec {
private def check(cases: Case*): Unit = {
val errs = collection.mutable.LinkedHashMap.empty[String, String]
+ val cli = getClient
+
cases.
flatMap(c ⇒ c.texts.map(t ⇒ t → normalize(c.sql))).
foreach {
case (txt, expSqlNorm) ⇒
- val res = client.ask(txt)
+ val res = cli.ask(txt)
if (res.isOk) {
require(res.getResult.asScala.isDefined)
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/examples/time/NCTimeModelSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/examples/time/NCTimeModelSpec.scala
index 93d6e0e..26ff1b8 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/examples/time/NCTimeModelSpec.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/examples/time/NCTimeModelSpec.scala
@@ -20,39 +20,18 @@ package org.apache.nlpcraft.examples.time
import java.io.IOException
import org.apache.nlpcraft.common.NCException
-import org.apache.nlpcraft.model.tools.embedded.NCEmbeddedProbe
-import org.apache.nlpcraft.model.tools.test.{NCTestClient, NCTestClientBuilder}
+import org.apache.nlpcraft.{NCTestContext, NCTestEnvironment}
import org.junit.jupiter.api.Assertions.assertTrue
-import org.junit.jupiter.api.{AfterEach, BeforeEach, Test}
-
-class NCTimeModelSpec {
- private var cli: NCTestClient = _
-
- @BeforeEach
- @throws[NCException]
- @throws[IOException]
- private[time] def setUp(): Unit = {
- NCEmbeddedProbe.start(classOf[TimeModel])
-
- cli = new NCTestClientBuilder().newBuilder.build
-
- cli.open("nlpcraft.time.ex")
- }
-
- @AfterEach
- @throws[NCException]
- @throws[IOException]
- private[time] def tearDown(): Unit = {
- if (cli != null)
- cli.close()
-
- NCEmbeddedProbe.stop()
- }
+import org.junit.jupiter.api.Test
+@NCTestEnvironment(model = classOf[TimeModel], startClient = true)
+class NCTimeModelSpec extends NCTestContext {
@Test
@throws[NCException]
@throws[IOException]
private[time] def testIntentsPriorities(): Unit = {
+ val cli = getClient
+
def check(txt: String, id: String): Unit = {
val res = cli.ask(txt)
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/conversation/NCConversationSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/conversation/NCConversationSpec.scala
index 61bee87..8615995 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/conversation/NCConversationSpec.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/conversation/NCConversationSpec.scala
@@ -19,23 +19,23 @@ package org.apache.nlpcraft.model.conversation
import java.io.IOException
-import org.apache.nlpcraft.{NCTestContext, NCTestContextModel}
import org.apache.nlpcraft.common.NCException
import org.apache.nlpcraft.examples.weather.WeatherModel
-import org.apache.nlpcraft.model.tools.embedded.NCEmbeddedProbe
-import org.apache.nlpcraft.model.tools.test.{NCTestClient, NCTestClientBuilder}
+import org.apache.nlpcraft.{NCTestContext, NCTestEnvironment}
import org.junit.jupiter.api.Assertions.assertTrue
-import org.junit.jupiter.api.{AfterEach, BeforeEach, Test}
+import org.junit.jupiter.api.Test
/**
* @see WeatherModel
*/
+@NCTestEnvironment(model = classOf[WeatherModel], startClient = true)
class NCConversationSpec extends NCTestContext {
@Test
@throws[NCException]
@throws[IOException]
- @NCTestContextModel(value = classOf[WeatherModel])
private[conversation] def test(): Unit = {
+ val cli = getClient
+
assertTrue(cli.ask("What's the weather in Moscow?").isOk)
// Can be answered with conversation.
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/NCStmTestModelSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/NCStmTestModelSpec.scala
index dee3675..842aca4 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/NCStmTestModelSpec.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/models/stm/NCStmTestModelSpec.scala
@@ -19,39 +19,15 @@ package org.apache.nlpcraft.models.stm
import java.io.IOException
-import org.apache.nlpcraft.common.NCException
-import org.apache.nlpcraft.model.tools.embedded.NCEmbeddedProbe
-import org.apache.nlpcraft.model.tools.test.{NCTestClient, NCTestClientBuilder}
+import org.apache.nlpcraft.{NCTestContext, NCTestEnvironment}
import org.junit.jupiter.api.Assertions.{assertEquals, assertTrue}
-import org.junit.jupiter.api.{AfterEach, BeforeEach, Test}
+import org.junit.jupiter.api.Test
/**
*
*/
-class NCStmTestModelSpec {
- private var cli: NCTestClient = _
-
- @BeforeEach
- @throws[NCException]
- @throws[IOException]
- private[stm] def setUp(): Unit = {
- NCEmbeddedProbe.start(classOf[NCStmTestModel])
-
- cli = new NCTestClientBuilder().newBuilder.build
-
- cli.open("nlpcraft.stm.test") // See phone_model.json
- }
-
- @AfterEach
- @throws[NCException]
- @throws[IOException]
- private[stm] def tearDown(): Unit = {
- if (cli != null)
- cli.close()
-
- NCEmbeddedProbe.stop()
- }
-
+@NCTestEnvironment(model = classOf[NCStmTestModel], startClient = true)
+class NCStmTestModelSpec extends NCTestContext {
/**
* @param req
* @param expResp
@@ -59,7 +35,7 @@ class NCStmTestModelSpec {
*/
@throws[IOException]
private def check(req: String, expResp: String): Unit = {
- val res = cli.ask(req)
+ val res = getClient.ask(req)
assertTrue(res.isOk)
assertTrue(res.getResult.isPresent)
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestAskSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestAskSpec.scala
index b11b299..b33cb1d 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestAskSpec.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestAskSpec.scala
@@ -17,28 +17,24 @@
package org.apache.nlpcraft.server.rest
+import org.apache.nlpcraft.NCTestEnvironment
import org.apache.nlpcraft.examples.time.TimeModel
-import org.apache.nlpcraft.model.tools.embedded.NCEmbeddedProbe
import org.junit.jupiter.api.Assertions._
-import org.junit.jupiter.api.{AfterEach, BeforeEach, Test}
+import org.junit.jupiter.api.{BeforeEach, Test}
import scala.collection.JavaConverters._
+@NCTestEnvironment(model = classOf[TimeModel], startClient = false)
class NCRestAskSpec extends NCRestSpec {
private var usrId: Long = 0
@BeforeEach
def setUp(): Unit = {
- NCEmbeddedProbe.start(classOf[TimeModel])
-
post("user/get")(("$.id", (id: Number) ⇒ usrId = id.longValue()))
assertTrue(usrId > 0)
}
- @AfterEach
- def tearDown(): Unit = NCEmbeddedProbe.stop()
-
@Test
def testSync(): Unit = {
post(
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestClearSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestClearSpec.scala
index df7bb51..989045f 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestClearSpec.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestClearSpec.scala
@@ -17,26 +17,22 @@
package org.apache.nlpcraft.server.rest
+import org.apache.nlpcraft.NCTestEnvironment
import org.apache.nlpcraft.examples.time.TimeModel
-import org.apache.nlpcraft.model.tools.embedded.NCEmbeddedProbe
import org.junit.jupiter.api.Assertions.assertTrue
-import org.junit.jupiter.api.{AfterEach, BeforeEach, Test}
+import org.junit.jupiter.api.{BeforeEach, Test}
+@NCTestEnvironment(model = classOf[TimeModel], startClient = false)
class NCRestClearSpec extends NCRestSpec {
private var usrId: Long = 0
@BeforeEach
def setUp(): Unit = {
- NCEmbeddedProbe.start(classOf[TimeModel])
-
post("user/get")(("$.id", (id: Number) ⇒ usrId = id.longValue()))
assertTrue(usrId > 0)
}
- @AfterEach
- def tearDown(): Unit = NCEmbeddedProbe.stop()
-
@Test
def test(): Unit = {
post("clear/conversation", "mdlId" → "nlpcraft.time.ex")()
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestModelSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestModelSpec.scala
index d5eac07..2c4ef04 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestModelSpec.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestModelSpec.scala
@@ -17,20 +17,15 @@
package org.apache.nlpcraft.server.rest
+import org.apache.nlpcraft.NCTestEnvironment
import org.apache.nlpcraft.examples.time.TimeModel
-import org.apache.nlpcraft.model.tools.embedded.NCEmbeddedProbe
import org.junit.jupiter.api.Assertions._
-import org.junit.jupiter.api.{AfterEach, BeforeEach, Disabled, Test}
+import org.junit.jupiter.api.{Disabled, Test}
// Enable it and run if context word server started.
@Disabled
+@NCTestEnvironment(model = classOf[TimeModel], startClient = false)
class NCRestModelSpec extends NCRestSpec {
- @BeforeEach
- def setUp(): Unit = NCEmbeddedProbe.start(classOf[TimeModel])
-
- @AfterEach
- def tearDown(): Unit = NCEmbeddedProbe.stop()
-
@Test
def test(): Unit = {
post("model/sugsyn", "mdlId" → "nlpcraft.time.ex")(
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestProbeSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestProbeSpec.scala
index 7df192b..17329de 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestProbeSpec.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestProbeSpec.scala
@@ -17,18 +17,13 @@
package org.apache.nlpcraft.server.rest
+import org.apache.nlpcraft.NCTestEnvironment
import org.apache.nlpcraft.examples.time.TimeModel
-import org.apache.nlpcraft.model.tools.embedded.NCEmbeddedProbe
import org.junit.jupiter.api.Assertions._
-import org.junit.jupiter.api.{AfterEach, BeforeEach, Test}
+import org.junit.jupiter.api.Test
+@NCTestEnvironment(model = classOf[TimeModel], startClient = false)
class NCRestProbeSpec extends NCRestSpec {
- @BeforeEach
- def setUp(): Unit = NCEmbeddedProbe.start(classOf[TimeModel])
-
- @AfterEach
- def tearDown(): Unit = NCEmbeddedProbe.stop()
-
@Test
def test(): Unit = {
post("probe/all")(
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestSpec.scala
index 5eb1970..85f40c3 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestSpec.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestSpec.scala
@@ -29,6 +29,7 @@ import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.HttpClients
import org.apache.http.util.EntityUtils
import org.apache.http.{HttpEntity, HttpResponse}
+import org.apache.nlpcraft.NCTestContext
import org.junit.jupiter.api.Assertions._
import org.junit.jupiter.api.{AfterEach, BeforeEach}
@@ -121,7 +122,7 @@ private[rest] object NCRestSpec {
import org.apache.nlpcraft.server.rest.NCRestSpec._
-private[rest] class NCRestSpec {
+private[rest] class NCRestSpec extends NCTestContext {
type ResponseContent = java.util.Map[String, Object]
type ResponseList = java.util.List[ResponseContent]
type JList[T] = java.util.List[T]