This is an automated email from the ASF dual-hosted git repository. sergeykamov pushed a commit to branch NLPCRAFT-27 in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
commit 7b25ff240a7c1ec4852cefc7e3c0ea60e686c33c Author: Sergey Kamov <sergeyka...@apache.org> AuthorDate: Wed Mar 25 15:27:08 2020 +0300 Initial refactoring. --- .../apache/nlpcraft/examples/alarm/AlarmModel.java | 8 +-- .../apache/nlpcraft/examples/alarm/AlarmTest.java | 12 +++- .../apache/nlpcraft/examples/echo/EchoTest.java | 14 +++-- .../examples/helloworld/HelloWorldModel.java | 6 +- .../examples/helloworld/HelloWorldTest.java | 26 +++++---- .../examples/lightswitch/LightSwitchModel.scala | 3 +- .../examples/lightswitch/LightSwitchTest.java | 50 +++++++++------- .../misc/geo/cities/CitiesDataProvider.java | 6 +- .../apache/nlpcraft/examples/phone/PhoneModel.java | 5 +- .../apache/nlpcraft/examples/phone/PhoneTest.java | 22 +++++--- .../nlpcraft/examples/sql/SqlModelTest.scala | 66 ++++++++++++---------- .../nlpcraft/examples/sql/db/SqlAccess.scala | 2 +- .../nlpcraft/examples/sql/db/SqlBuilder.scala | 4 +- .../apache/nlpcraft/examples/time/TimeModel.java | 3 - .../nlpcraft/examples/time/TimeModelApp.java | 2 +- .../apache/nlpcraft/examples/time/TimeTest.java | 24 +++++--- .../nlpcraft/examples/weather/WeatherTest.java | 63 ++++++++++++--------- .../nlpcraft/model/intent/dsl/NCDslTest.java | 11 ++-- .../nlpcraft/models/stm/NCStmTestModelSpec.java | 14 +++-- 19 files changed, 196 insertions(+), 145 deletions(-) diff --git a/src/main/scala/org/apache/nlpcraft/examples/alarm/AlarmModel.java b/src/main/scala/org/apache/nlpcraft/examples/alarm/AlarmModel.java index 4c81dda..2182df7 100644 --- a/src/main/scala/org/apache/nlpcraft/examples/alarm/AlarmModel.java +++ b/src/main/scala/org/apache/nlpcraft/examples/alarm/AlarmModel.java @@ -17,13 +17,7 @@ package org.apache.nlpcraft.examples.alarm; -import org.apache.nlpcraft.model.NCModelFileAdapter; -import org.apache.nlpcraft.model.NCResult; -import org.apache.nlpcraft.model.NCRejection; -import org.apache.nlpcraft.model.NCToken; -import org.apache.nlpcraft.model.NCIntentRef; -import org.apache.nlpcraft.model.NCIntentMatch; -import org.apache.nlpcraft.model.NCIntentTerm; +import org.apache.nlpcraft.model.*; import java.time.LocalDateTime; import java.time.ZoneId; diff --git a/src/main/scala/org/apache/nlpcraft/examples/alarm/AlarmTest.java b/src/main/scala/org/apache/nlpcraft/examples/alarm/AlarmTest.java index fe027c9..ae69e11 100644 --- a/src/main/scala/org/apache/nlpcraft/examples/alarm/AlarmTest.java +++ b/src/main/scala/org/apache/nlpcraft/examples/alarm/AlarmTest.java @@ -17,10 +17,13 @@ package org.apache.nlpcraft.examples.alarm; -import org.junit.jupiter.api.*; import org.apache.nlpcraft.common.NCException; import org.apache.nlpcraft.model.tools.test.NCTestClient; import org.apache.nlpcraft.model.tools.test.NCTestClientBuilder; +import org.apache.nlpcraft.probe.embedded.NCEmbeddedProbe; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import java.io.IOException; @@ -36,6 +39,8 @@ class AlarmTest { @BeforeAll static void setUp() throws NCException, IOException { + NCEmbeddedProbe.start(AlarmModel.class); + client = new NCTestClientBuilder().newBuilder().build(); client.open("nlpcraft.alarm.ex"); // See alarm_model.json @@ -43,7 +48,10 @@ class AlarmTest { @AfterAll static void tearDown() throws NCException, IOException { - client.close(); + if (client != null) + client.close(); + + NCEmbeddedProbe.stop(); } @Test diff --git a/src/main/scala/org/apache/nlpcraft/examples/echo/EchoTest.java b/src/main/scala/org/apache/nlpcraft/examples/echo/EchoTest.java index b10c38c..5936925 100644 --- a/src/main/scala/org/apache/nlpcraft/examples/echo/EchoTest.java +++ b/src/main/scala/org/apache/nlpcraft/examples/echo/EchoTest.java @@ -17,12 +17,13 @@ package org.apache.nlpcraft.examples.echo; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; import org.apache.nlpcraft.common.NCException; import org.apache.nlpcraft.model.tools.test.NCTestClient; import org.apache.nlpcraft.model.tools.test.NCTestClientBuilder; +import org.apache.nlpcraft.probe.embedded.NCEmbeddedProbe; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.io.IOException; @@ -38,6 +39,8 @@ class EchoTest { @BeforeEach void setUp() throws NCException, IOException { + NCEmbeddedProbe.start(EchoModel.class); + client = new NCTestClientBuilder().newBuilder().build(); client.open("nlpcraft.echo.ex"); @@ -45,7 +48,10 @@ class EchoTest { @AfterEach void tearDown() throws NCException, IOException { - client.close(); + if (client != null) + client.close(); + + NCEmbeddedProbe.stop(); } @Test diff --git a/src/main/scala/org/apache/nlpcraft/examples/helloworld/HelloWorldModel.java b/src/main/scala/org/apache/nlpcraft/examples/helloworld/HelloWorldModel.java index 8b07e7d..81d8fa6 100644 --- a/src/main/scala/org/apache/nlpcraft/examples/helloworld/HelloWorldModel.java +++ b/src/main/scala/org/apache/nlpcraft/examples/helloworld/HelloWorldModel.java @@ -17,8 +17,10 @@ package org.apache.nlpcraft.examples.helloworld; -import org.apache.nlpcraft.common.*; -import org.apache.nlpcraft.model.*; +import org.apache.nlpcraft.common.NCException; +import org.apache.nlpcraft.model.NCContext; +import org.apache.nlpcraft.model.NCModelAdapter; +import org.apache.nlpcraft.model.NCResult; /** * Hello World example data model. diff --git a/src/main/scala/org/apache/nlpcraft/examples/helloworld/HelloWorldTest.java b/src/main/scala/org/apache/nlpcraft/examples/helloworld/HelloWorldTest.java index 61ba5df..f24b359 100644 --- a/src/main/scala/org/apache/nlpcraft/examples/helloworld/HelloWorldTest.java +++ b/src/main/scala/org/apache/nlpcraft/examples/helloworld/HelloWorldTest.java @@ -17,12 +17,13 @@ package org.apache.nlpcraft.examples.helloworld; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; import org.apache.nlpcraft.common.NCException; import org.apache.nlpcraft.model.tools.test.NCTestClient; import org.apache.nlpcraft.model.tools.test.NCTestClientBuilder; +import org.apache.nlpcraft.probe.embedded.NCEmbeddedProbe; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.io.IOException; @@ -35,28 +36,33 @@ import static org.junit.jupiter.api.Assertions.assertTrue; */ class HelloWorldTest { private NCTestClient client; - + @BeforeEach void setUp() throws NCException, IOException { + NCEmbeddedProbe.start(HelloWorldModel.class); + // Use all defaults. client = new NCTestClientBuilder().newBuilder().build(); - + client.open("nlpcraft.helloworld.ex"); } - + @AfterEach void tearDown() throws NCException, IOException { - client.close(); + if (client != null) + client.close(); + + NCEmbeddedProbe.stop(); } - + @Test void test() throws NCException, IOException { // Empty parameter. assertTrue(client.ask("").isFailed()); - + // Only latin charset is supported. assertTrue(client.ask("El tiempo en España").isFailed()); - + // Should be passed. assertTrue(client.ask("Hi!").isOk()); } diff --git a/src/main/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchModel.scala b/src/main/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchModel.scala index df39329..5f001e3 100644 --- a/src/main/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchModel.scala +++ b/src/main/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchModel.scala @@ -17,8 +17,7 @@ package org.apache.nlpcraft.examples.lightswitch -import org.apache.nlpcraft.model._ -import org.apache.nlpcraft.model.NCIntentTerm +import org.apache.nlpcraft.model.{NCIntentTerm, _} /** * This example provides very simple implementation for NLI-powered light switch. diff --git a/src/main/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchTest.java b/src/main/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchTest.java index a99de80..0565f4b 100644 --- a/src/main/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchTest.java +++ b/src/main/scala/org/apache/nlpcraft/examples/lightswitch/LightSwitchTest.java @@ -17,12 +17,17 @@ package org.apache.nlpcraft.examples.lightswitch; -import org.junit.jupiter.api.*; -import org.apache.nlpcraft.common.*; -import org.apache.nlpcraft.model.tools.test.*; -import java.io.*; +import org.apache.nlpcraft.common.NCException; +import org.apache.nlpcraft.model.tools.test.NCTestClient; +import org.apache.nlpcraft.model.tools.test.NCTestClientBuilder; +import org.apache.nlpcraft.probe.embedded.NCEmbeddedProbe; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; +import java.io.IOException; + +import static org.junit.jupiter.api.Assertions.assertTrue; /** * Lightswitch model test. @@ -30,32 +35,37 @@ import static org.junit.jupiter.api.Assertions.*; * @see LightSwitchModel */ class LightSwitchTest { - private NCTestClient cli; + private NCTestClient client; @BeforeEach void setUp() throws NCException, IOException { - cli = new NCTestClientBuilder().newBuilder().build(); + NCEmbeddedProbe.start(LightSwitchModel.class); + + client = new NCTestClientBuilder().newBuilder().build(); - cli.open("nlpcraft.lightswitch.ex"); + client.open("nlpcraft.lightswitch.ex"); } @AfterEach void tearDown() throws NCException, IOException { - cli.close(); + if (client != null) + client.close(); + + NCEmbeddedProbe.stop(); } @Test void test() throws NCException, IOException { - assertTrue(cli.ask("Turn the lights off in the entire house.").isOk()); - assertTrue(cli.ask("Switch on the illumination in the master bedroom closet.").isOk()); - assertTrue(cli.ask("Get the lights on.").isOk()); - assertTrue(cli.ask("Please, put the light out in the upstairs bedroom.").isOk()); - assertTrue(cli.ask("Set the lights on in the entire house.").isOk()); - assertTrue(cli.ask("Turn the lights off in the guest bedroom.").isOk()); - assertTrue(cli.ask("Could you please switch off all the lights?").isOk()); - assertTrue(cli.ask("Dial off illumination on the 2nd floor.").isOk()); - assertTrue(cli.ask("Please, no lights!").isOk()); - assertTrue(cli.ask("Kill off all the lights now!").isOk()); - assertTrue(cli.ask("No lights in the bedroom, please.").isOk()); + assertTrue(client.ask("Turn the lights off in the entire house.").isOk()); + assertTrue(client.ask("Switch on the illumination in the master bedroom closet.").isOk()); + assertTrue(client.ask("Get the lights on.").isOk()); + assertTrue(client.ask("Please, put the light out in the upstairs bedroom.").isOk()); + assertTrue(client.ask("Set the lights on in the entire house.").isOk()); + assertTrue(client.ask("Turn the lights off in the guest bedroom.").isOk()); + assertTrue(client.ask("Could you please switch off all the lights?").isOk()); + assertTrue(client.ask("Dial off illumination on the 2nd floor.").isOk()); + assertTrue(client.ask("Please, no lights!").isOk()); + assertTrue(client.ask("Kill off all the lights now!").isOk()); + assertTrue(client.ask("No lights in the bedroom, please.").isOk()); } } diff --git a/src/main/scala/org/apache/nlpcraft/examples/misc/geo/cities/CitiesDataProvider.java b/src/main/scala/org/apache/nlpcraft/examples/misc/geo/cities/CitiesDataProvider.java index 94ab352..15d6dfa 100644 --- a/src/main/scala/org/apache/nlpcraft/examples/misc/geo/cities/CitiesDataProvider.java +++ b/src/main/scala/org/apache/nlpcraft/examples/misc/geo/cities/CitiesDataProvider.java @@ -23,11 +23,7 @@ import org.apache.nlpcraft.common.NCException; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Map; -import java.util.Objects; +import java.util.*; import java.util.stream.Collectors; /** diff --git a/src/main/scala/org/apache/nlpcraft/examples/phone/PhoneModel.java b/src/main/scala/org/apache/nlpcraft/examples/phone/PhoneModel.java index 64c35e8..6ca8d26 100644 --- a/src/main/scala/org/apache/nlpcraft/examples/phone/PhoneModel.java +++ b/src/main/scala/org/apache/nlpcraft/examples/phone/PhoneModel.java @@ -18,8 +18,9 @@ package org.apache.nlpcraft.examples.phone; import org.apache.nlpcraft.model.*; -import java.util.*; -import java.util.stream.*; + +import java.util.List; +import java.util.stream.Collectors; /** diff --git a/src/main/scala/org/apache/nlpcraft/examples/phone/PhoneTest.java b/src/main/scala/org/apache/nlpcraft/examples/phone/PhoneTest.java index 8170c5f..4806f34 100644 --- a/src/main/scala/org/apache/nlpcraft/examples/phone/PhoneTest.java +++ b/src/main/scala/org/apache/nlpcraft/examples/phone/PhoneTest.java @@ -17,12 +17,13 @@ package org.apache.nlpcraft.examples.phone; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; import org.apache.nlpcraft.common.NCException; import org.apache.nlpcraft.model.tools.test.NCTestClient; import org.apache.nlpcraft.model.tools.test.NCTestClientBuilder; +import org.apache.nlpcraft.probe.embedded.NCEmbeddedProbe; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.io.IOException; @@ -35,19 +36,24 @@ import static org.junit.jupiter.api.Assertions.assertTrue; */ class PhoneTest { private NCTestClient client; - + @BeforeEach void setUp() throws NCException, IOException { + NCEmbeddedProbe.start(PhoneModel.class); + client = new NCTestClientBuilder().newBuilder().build(); - + client.open("nlpcraft.phone.ex"); // See phone_model.json } - + @AfterEach void tearDown() throws NCException, IOException { - client.close(); + if (client != null) + client.close(); + + NCEmbeddedProbe.stop(); } - + @Test void test() throws NCException, IOException { assertTrue(client.ask("Call to Apple office").isOk()); diff --git a/src/main/scala/org/apache/nlpcraft/examples/sql/SqlModelTest.scala b/src/main/scala/org/apache/nlpcraft/examples/sql/SqlModelTest.scala index 558a344..1968895 100644 --- a/src/main/scala/org/apache/nlpcraft/examples/sql/SqlModelTest.scala +++ b/src/main/scala/org/apache/nlpcraft/examples/sql/SqlModelTest.scala @@ -27,14 +27,15 @@ import com.google.gson.reflect.TypeToken import com.jakewharton.fliptables.FlipTable import org.apache.nlpcraft.model.tools.sqlgen.NCSqlException import org.apache.nlpcraft.model.tools.test.{NCTestClient, NCTestClientBuilder} +import org.apache.nlpcraft.probe.embedded.NCEmbeddedProbe import org.scalatest.{BeforeAndAfterAll, FlatSpec} import scala.collection.JavaConverters._ import scala.compat.java8.OptionConverters._ /** - * - */ + * + */ class SqlModelTest extends FlatSpec with BeforeAndAfterAll { private val GSON = new Gson private val TYPE_RESP = new TypeToken[util.Map[String, Object]]() {}.getType @@ -50,8 +51,8 @@ class SqlModelTest extends FlatSpec with BeforeAndAfterAll { override def apply(t: java.lang.Boolean): String = "**" }). build - - private var cli: NCTestClient = _ + + private var client: NCTestClient = _ case class Case(texts: Seq[String], sql: String) @@ -64,62 +65,67 @@ class SqlModelTest extends FlatSpec with BeforeAndAfterAll { mkString(" ") private def toPretty(s: String): util.List[String] = SqlFormatter.format(s).split("\n").toSeq.asJava - + override protected def beforeAll(): Unit = { - cli = new NCTestClientBuilder().newBuilder.setResponseLog(false).build - - cli.open("sql.model.id") - + NCEmbeddedProbe.start(classOf[SqlModel]) + + client = new NCTestClientBuilder().newBuilder.setResponseLog(false).build + + client.open("sql.model.id") + } - - override protected def afterAll(): Unit = - if (cli != null) - cli.close() - + + override protected def afterAll(): Unit = { + if (client != null) + client.close() + + NCEmbeddedProbe.stop() + } + private def check(multiLineOut: Boolean, cases: Case*): Unit = { val errs = collection.mutable.LinkedHashMap.empty[String, String] - + cases. flatMap(c ⇒ { val sql = normalize(c.sql) - + c.texts.map(t ⇒ t → sql) }). foreach { case (txt, expSqlNorm) ⇒ - val res = cli.ask(txt) - + val res = client.ask(txt) + if (res.isOk) { require(res.getResult.asScala.isDefined) - + val m: util.Map[String, Object] = GSON.fromJson(res.getResult.get, TYPE_RESP) - + val err = m.get("error") - + if (err != null) errs += txt → err.toString else { val resSqlNorm = normalize(m.asScala("sql").asInstanceOf[String]) - + if (resSqlNorm != expSqlNorm) { if (multiLineOut) { val rows = DIFF.generateDiffRows(toPretty(expSqlNorm), toPretty(resSqlNorm)).asScala - + val table = FlipTable.of( Array("Expected", "Real"), rows.map(p ⇒ Array(p.getOldLine, p.getNewLine)).toArray ) - + errs += txt → s"Unexpected SQL:\n$table" } else { val rows = DIFF.generateDiffRows(Seq(expSqlNorm).asJava, Seq(resSqlNorm).asJava).asScala - + require(rows.size == 1) - + val row = rows.head - + errs += txt → s"""Unexpected SQL (expected vs real) |${row.getOldLine} @@ -131,13 +137,13 @@ class SqlModelTest extends FlatSpec with BeforeAndAfterAll { } else { require(res.getResultError.isPresent) - + errs += txt → res.getResultError.get } } if (errs.nonEmpty) { - errs.foreach { case (txt, err) ⇒ println(s"Text: $txt\nError: $err\n")} + errs.foreach { case (txt, err) ⇒ println(s"Text: $txt\nError: $err\n") } throw new NCSqlException(s"Test finished with errors [passed=${cases.size - errs.size}, failed=${errs.size}]") } @@ -566,7 +572,7 @@ class SqlModelTest extends FlatSpec with BeforeAndAfterAll { |LIMIT | 1000 """.stripMargin - ) , + ), Case( Seq( "give me the orders sorted by ship date" diff --git a/src/main/scala/org/apache/nlpcraft/examples/sql/db/SqlAccess.scala b/src/main/scala/org/apache/nlpcraft/examples/sql/db/SqlAccess.scala index ef2d0be..cd9ab5d 100644 --- a/src/main/scala/org/apache/nlpcraft/examples/sql/db/SqlAccess.scala +++ b/src/main/scala/org/apache/nlpcraft/examples/sql/db/SqlAccess.scala @@ -22,8 +22,8 @@ import java.sql.{Connection, SQLException} import com.github.vertical_blank.sqlformatter.SqlFormatter import com.jakewharton.fliptables.FlipTable import com.typesafe.scalalogging.LazyLogging -import org.h2.jdbcx.JdbcDataSource import org.apache.nlpcraft.model.tools.sqlgen.NCSqlQuery +import org.h2.jdbcx.JdbcDataSource import resource.managed import scala.collection.JavaConverters._ diff --git a/src/main/scala/org/apache/nlpcraft/examples/sql/db/SqlBuilder.scala b/src/main/scala/org/apache/nlpcraft/examples/sql/db/SqlBuilder.scala index f9435f4..699f495 100644 --- a/src/main/scala/org/apache/nlpcraft/examples/sql/db/SqlBuilder.scala +++ b/src/main/scala/org/apache/nlpcraft/examples/sql/db/SqlBuilder.scala @@ -21,10 +21,10 @@ import java.sql.Types import java.util import com.typesafe.scalalogging.LazyLogging +import org.apache.nlpcraft.model.tools.sqlgen.impl.{NCSqlFunctionImpl, NCSqlSortImpl} +import org.apache.nlpcraft.model.tools.sqlgen.{NCSqlAggregate, _} import org.jgrapht.alg.DijkstraShortestPath import org.jgrapht.graph.{DefaultEdge, SimpleGraph} -import org.apache.nlpcraft.model.tools.sqlgen.{NCSqlAggregate, _} -import org.apache.nlpcraft.model.tools.sqlgen.impl.{NCSqlFunctionImpl, NCSqlSortImpl} import scala.collection.JavaConverters._ import scala.collection.{Seq, mutable} diff --git a/src/main/scala/org/apache/nlpcraft/examples/time/TimeModel.java b/src/main/scala/org/apache/nlpcraft/examples/time/TimeModel.java index 55d4192..e6d7e2e 100644 --- a/src/main/scala/org/apache/nlpcraft/examples/time/TimeModel.java +++ b/src/main/scala/org/apache/nlpcraft/examples/time/TimeModel.java @@ -28,9 +28,6 @@ import org.apache.nlpcraft.examples.misc.geo.cities.CityData; import org.apache.nlpcraft.examples.misc.geo.keycdn.GeoManager; import org.apache.nlpcraft.examples.misc.geo.keycdn.beans.GeoDataBean; import org.apache.nlpcraft.model.*; -import org.apache.nlpcraft.model.NCIntent; -import org.apache.nlpcraft.model.NCIntentMatch; -import org.apache.nlpcraft.model.NCIntentTerm; import java.time.ZoneId; import java.time.ZonedDateTime; diff --git a/src/main/scala/org/apache/nlpcraft/examples/time/TimeModelApp.java b/src/main/scala/org/apache/nlpcraft/examples/time/TimeModelApp.java index e7f0307..cd8e1ad 100644 --- a/src/main/scala/org/apache/nlpcraft/examples/time/TimeModelApp.java +++ b/src/main/scala/org/apache/nlpcraft/examples/time/TimeModelApp.java @@ -17,7 +17,7 @@ package org.apache.nlpcraft.examples.time; -import org.apache.nlpcraft.probe.embedded.*; +import org.apache.nlpcraft.probe.embedded.NCEmbeddedProbe; /** * An app that demo the usage of embedded probe. This is an alternative way to diff --git a/src/main/scala/org/apache/nlpcraft/examples/time/TimeTest.java b/src/main/scala/org/apache/nlpcraft/examples/time/TimeTest.java index d2e8b40..0a5441e 100644 --- a/src/main/scala/org/apache/nlpcraft/examples/time/TimeTest.java +++ b/src/main/scala/org/apache/nlpcraft/examples/time/TimeTest.java @@ -17,12 +17,13 @@ package org.apache.nlpcraft.examples.time; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; import org.apache.nlpcraft.common.NCException; import org.apache.nlpcraft.model.tools.test.NCTestClient; import org.apache.nlpcraft.model.tools.test.NCTestClientBuilder; +import org.apache.nlpcraft.probe.embedded.NCEmbeddedProbe; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.io.IOException; @@ -35,24 +36,29 @@ import static org.junit.jupiter.api.Assertions.assertTrue; */ class TimeTest { private NCTestClient client; - + @BeforeEach void setUp() throws NCException, IOException { + NCEmbeddedProbe.start(TimeModel.class); + client = new NCTestClientBuilder().newBuilder().build(); - + client.open("nlpcraft.time.ex"); // See time_model.json } - + @AfterEach void tearDown() throws NCException, IOException { - client.close(); + if (client != null) + client.close(); + + NCEmbeddedProbe.stop(); } - + @Test void test() throws NCException, IOException { // Empty parameter. assertTrue(client.ask("").isFailed()); - + // Only latin charset is supported. assertTrue(client.ask("El tiempo en España").isFailed()); diff --git a/src/main/scala/org/apache/nlpcraft/examples/weather/WeatherTest.java b/src/main/scala/org/apache/nlpcraft/examples/weather/WeatherTest.java index e640c88..32e2eb5 100644 --- a/src/main/scala/org/apache/nlpcraft/examples/weather/WeatherTest.java +++ b/src/main/scala/org/apache/nlpcraft/examples/weather/WeatherTest.java @@ -19,13 +19,14 @@ package org.apache.nlpcraft.examples.weather; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; import org.apache.nlpcraft.common.NCException; import org.apache.nlpcraft.model.tools.test.NCTestClient; import org.apache.nlpcraft.model.tools.test.NCTestClientBuilder; import org.apache.nlpcraft.model.tools.test.NCTestResult; +import org.apache.nlpcraft.probe.embedded.NCEmbeddedProbe; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.io.IOException; import java.lang.reflect.Type; @@ -42,62 +43,68 @@ import static org.junit.jupiter.api.Assertions.*; */ class WeatherTest { private static final Gson GSON = new Gson(); - private static final Type TYPE_MAP_RESP = new TypeToken<HashMap<String, Object>>() {}.getType(); - - private NCTestClient cli; - + private static final Type TYPE_MAP_RESP = new TypeToken<HashMap<String, Object>>() { + }.getType(); + + private NCTestClient client; + /** * Checks given intent. * - * @param txt Sentence. - * @param intentId Intent ID. + * @param txt Sentence. + * @param intentId Intent ID. * @param shouldBeSame Equal vs. non-equal intent ID flag. */ private void checkIntent(String txt, String intentId, boolean shouldBeSame) throws NCException, IOException { - NCTestResult res = cli.ask(txt); - + NCTestResult res = client.ask(txt); + assertTrue(res.isOk(), () -> res.getResultError().get()); assert res.getResult().isPresent(); - + Map<String, Object> map = GSON.fromJson(res.getResult().get(), TYPE_MAP_RESP); - + if (shouldBeSame) assertEquals(intentId, map.get("intentId")); else assertNotEquals(intentId, map.get("intentId")); } - + @BeforeEach void setUp() throws NCException, IOException { - cli = new NCTestClientBuilder().newBuilder().build(); - - cli.open("nlpcraft.weather.ex"); // See weather_model.json + NCEmbeddedProbe.start(WeatherModel.class); + + client = new NCTestClientBuilder().newBuilder().build(); + + client.open("nlpcraft.weather.ex"); // See weather_model.json } - + @AfterEach void tearDown() throws NCException, IOException { - cli.close(); + if (client != null) + client.close(); + + NCEmbeddedProbe.stop(); } - + @Test void test() throws NCException, IOException { // Empty parameter. - assertTrue(cli.ask("").isFailed()); - + assertTrue(client.ask("").isFailed()); + // Only latin charset is supported. - assertTrue(cli.ask("El tiempo en España").isFailed()); - + assertTrue(client.ask("El tiempo en España").isFailed()); + // Should be passed. checkIntent("What's the local weather forecast?", "fcast", true); checkIntent("What's the weather in Moscow?", "curr", true); // Can be answered with conversation. checkIntent("Chance of snow?", "curr", true); checkIntent("Moscow", "curr", true); - - cli.clearConversation(); - + + client.clearConversation(); + // Cannot be answered without conversation. - assertTrue(cli.ask("Moscow").isFailed()); + assertTrue(client.ask("Moscow").isFailed()); } } diff --git a/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCDslTest.java b/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCDslTest.java index 8ea9cba..9f61f65 100644 --- a/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCDslTest.java +++ b/src/test/scala/org/apache/nlpcraft/model/intent/dsl/NCDslTest.java @@ -29,27 +29,28 @@ import static org.junit.jupiter.api.Assertions.*; * DSL test model test. Make sure to start up the NLPCraft server before running this test. */ class NCDslTest { - private NCTestClient cli; + private NCTestClient client; @BeforeEach void setUp() throws NCException, IOException { // Start embedded probe with the test model. NCEmbeddedProbe.start(NCDslTestModel.class); - cli = new NCTestClientBuilder().newBuilder().build(); + client = new NCTestClientBuilder().newBuilder().build(); - cli.open("nlpcraft.dsl.test"); + client.open("nlpcraft.dsl.test"); } @AfterEach void tearDown() throws NCException, IOException { - cli.close(); + if (client != null) + client.close(); NCEmbeddedProbe.stop(); } @Test void test() throws NCException, IOException { - assertTrue(cli.ask("aa").isOk()); + assertTrue(client.ask("aa").isOk()); } } diff --git a/src/test/scala/org/apache/nlpcraft/models/stm/NCStmTestModelSpec.java b/src/test/scala/org/apache/nlpcraft/models/stm/NCStmTestModelSpec.java index f95ae31..c995f51 100644 --- a/src/test/scala/org/apache/nlpcraft/models/stm/NCStmTestModelSpec.java +++ b/src/test/scala/org/apache/nlpcraft/models/stm/NCStmTestModelSpec.java @@ -17,13 +17,14 @@ package org.apache.nlpcraft.models.stm; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; import org.apache.nlpcraft.common.NCException; import org.apache.nlpcraft.model.tools.test.NCTestClient; import org.apache.nlpcraft.model.tools.test.NCTestClientBuilder; import org.apache.nlpcraft.model.tools.test.NCTestResult; +import org.apache.nlpcraft.probe.embedded.NCEmbeddedProbe; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.io.IOException; @@ -38,6 +39,8 @@ class NCStmTestModelSpec { @BeforeEach void setUp() throws NCException, IOException { + NCEmbeddedProbe.start(NCStmTestModel.class); + client = new NCTestClientBuilder().newBuilder().build(); client.open("nlpcraft.stm.test"); // See phone_model.json @@ -45,7 +48,10 @@ class NCStmTestModelSpec { @AfterEach void tearDown() throws NCException, IOException { - client.close(); + if (client != null) + client.close(); + + NCEmbeddedProbe.stop(); } /**