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 2021/03/20 07:14:18 UTC

[incubator-nlpcraft] branch master updated: Intents test added.

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


The following commit(s) were added to refs/heads/master by this push:
     new 306752e  Intents test added.
306752e is described below

commit 306752ead7b7838f7eecda50feca43aad77ee87e
Author: Sergey Kamov <sk...@gmail.com>
AuthorDate: Sat Mar 20 10:14:08 2021 +0300

    Intents test added.
---
 .../probe/mgrs/nlp/NCProbeEnrichmentManager.scala  |   2 +-
 .../probe/mgrs/nlp/impl/NCRequestImpl.scala        |   4 +-
 .../apache/nlpcraft/model/meta/NCMetaSpec.scala    | 149 +++++++++++++++++++++
 .../apache/nlpcraft/server/rest/NCRestSpec.scala   |   4 +-
 4 files changed, 154 insertions(+), 5 deletions(-)

diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/NCProbeEnrichmentManager.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/NCProbeEnrichmentManager.scala
index 03b49ed..4a1466e 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/NCProbeEnrichmentManager.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/NCProbeEnrichmentManager.scala
@@ -590,7 +590,7 @@ object NCProbeEnrichmentManager extends NCService with NCOpenCensusModelStats {
                 override def getDialogFlow: util.List[NCDialogFlowItem] = NCDialogFlowManager.getDialogFlow(usrId, mdlId, span).asJava
                 override def clearStm(filter: Predicate[NCToken]): Unit = conv.clearTokens(filter)
                 override def clearDialog(filter: Predicate[String]): Unit = NCDialogFlowManager.clear(usrId, mdlId, span)
-                override def getMetadata = conv.getUserData
+                override def getMetadata: util.Map[String, AnyRef] = conv.getUserData
             }
 
             override def isOwnerOf(tok: NCToken): Boolean = allVars.contains(tok)
diff --git a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/impl/NCRequestImpl.scala b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/impl/NCRequestImpl.scala
index 6b067e2..d6d60b0 100644
--- a/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/impl/NCRequestImpl.scala
+++ b/nlpcraft/src/main/scala/org/apache/nlpcraft/probe/mgrs/nlp/impl/NCRequestImpl.scala
@@ -75,8 +75,8 @@ case class NCRequestImpl(meta: Map[String, Any], srvReqId: String) extends NCReq
         }
 
     private def getMap(key: String): util.Map[String, AnyRef] = {
-        val m: Optional[Map[String, AnyRef]] = getOpt(key)
+        val m: Optional[JavaMeta] = getOpt(key)
 
-        if (m.isPresent) m.get().asJava else Collections.emptyMap()
+        if (m.isPresent) m.get() else Collections.emptyMap()
     }
 }
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/meta/NCMetaSpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/meta/NCMetaSpec.scala
new file mode 100644
index 0000000..2ec6407
--- /dev/null
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/meta/NCMetaSpec.scala
@@ -0,0 +1,149 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.nlpcraft.model.meta
+
+import org.apache.nlpcraft.model.`abstract`.NCAbstractTokensModel
+import org.apache.nlpcraft.model.{NCElement, NCIntent, NCIntentMatch, NCResult}
+import org.apache.nlpcraft.server.rest.NCRestSpec
+import org.apache.nlpcraft.{NCTestElement, NCTestEnvironment}
+import org.junit.jupiter.api.Test
+
+import java.util
+import scala.collection.JavaConverters._
+import scala.sys.SystemProperties
+
+/**
+  * Model for test following meta usage: company, user and system.
+  */
+class NcMetaModel extends NCAbstractTokensModel {
+    override def getElements: util.Set[NCElement] = Set(NCTestElement("a", "a"))
+
+    @NCIntent(
+        "intent=i " +
+        "  term(t)={" +
+        "      id() == 'a' && " +
+        "      meta_user('k1') == 'v1' && " +
+        "      meta_company('k1') == 'v1' && " +
+        "      meta_sys('k1') == 'v1'" +
+        "  }"
+    )
+    def onIntent(ctx: NCIntentMatch): NCResult = NCResult.text("OK")
+}
+
+@NCTestEnvironment(model = classOf[NcMetaModel], startClient = true)
+class NCMetaSpec extends NCRestSpec {
+    type Meta = java.util.Map[String, String]
+    case class MetaHolder(userMeta: Meta, companyMeta: Meta)
+
+    private def get(): MetaHolder = {
+        var userMeta: Meta = null
+        var companyMeta: Meta = null
+
+        post("user/get")(
+            ("$.properties", (props: java.util.Map[String, String]) ⇒ userMeta = props)
+        )
+
+        post("company/get")(
+            ("$.properties", (props: java.util.Map[String, String]) ⇒ companyMeta = props)
+        )
+
+        MetaHolder(userMeta, companyMeta)
+    }
+
+    private def post(h: MetaHolder): Unit = {
+        def convert(m: Meta): Meta = if (m == null) util.Collections.emptyMap() else m
+
+        // 1. We have to save all existing company's fields for following updates.
+        var compName: String = null
+        var compWebsite: String = null
+        var compCountry: String = null
+        var compRegion: String = null
+        var compCity: String = null
+        var compAddress: String = null
+        var compPostalCode: String = null
+
+        post("company/get")(
+            ("$.name", (v: String) ⇒ compName = v),
+            ("$.website", (v: String) ⇒ compWebsite = v),
+            ("$.country", (v: String) ⇒ compCountry = v),
+            ("$.region", (v: String) ⇒ compRegion = v),
+            ("$.city", (v: String) ⇒ compCity = v),
+            ("$.address", (v: String) ⇒ compAddress = v),
+            ("$.postalCode", (v: String) ⇒ compPostalCode = v)
+        )
+
+        post("company/update",
+            "name" → compName,
+            "website" → compWebsite,
+            "country" → compCountry,
+            "region" → compRegion,
+            "city" → compCity,
+            "address" → compAddress,
+            "postalCode" → compPostalCode,
+            "properties" → convert(h.companyMeta)
+        )()
+
+        // 2. We have to save all existing user's fields for following updates.
+        var usrFirstName: String = null
+        var usrLastName: String = null
+        var usrAvatarUrl: String = null
+
+        post("user/get")(
+            ("$.firstName", (v: String) ⇒ usrFirstName = v),
+            ("$.lastName", (v: String) ⇒ usrLastName = v),
+            ("$.avatarUrl", (v: String) ⇒ usrAvatarUrl = v)
+        )
+
+        post("user/update",
+        "firstName" → usrFirstName,
+            "lastName" → usrLastName,
+            "avatarUrl" → usrAvatarUrl,
+            "properties" → convert(h.userMeta)
+        )()
+    }
+
+    @Test
+    def testWithoutMeta(): Unit = require(getClient.ask("a").isFailed)
+
+    @Test
+    def testWithMeta(): Unit = {
+        val currUserCompMeta = get()
+        val sys = new SystemProperties
+
+        val m = Map("k1" → "v1").asJava
+
+        try {
+            // Sets company and user metadata.
+            post(MetaHolder(m, m))
+
+            // It is not enough.
+            require(getClient.ask("a").isFailed)
+
+            // Sets sys metadata.
+            sys.put("k1", "v1")
+
+            // Ok.
+            require(getClient.ask("a").isOk)
+        }
+        finally {
+            post(currUserCompMeta)
+
+            sys.remove("k1")
+        }
+    }
+}
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 9e395a0..47b6f95 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
@@ -35,7 +35,7 @@ import java.util
 import java.util.UUID
 import scala.collection.JavaConverters._
 
-private[rest] object NCRestSpec {
+object NCRestSpec {
     private final val DFLT_BASEURL = "http://localhost:8081/api/v1/"
     private final val DFLT_ADMIN_EMAIL = "admin@admin.com"
     private final val DFLT_ADMIN_PSWD = "admin"
@@ -122,7 +122,7 @@ private[rest] object NCRestSpec {
 
 import org.apache.nlpcraft.server.rest.NCRestSpec._
 
-private[rest] class NCRestSpec extends NCTestContext {
+class NCRestSpec extends NCTestContext {
     type ResponseContent = java.util.Map[String, Object]
     type ResponseList = java.util.List[ResponseContent]
     type JList[T] = java.util.List[T]