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/06 19:53:14 UTC

[incubator-nlpcraft] branch NLPCRAFT-113 created (now b0302e2)

This is an automated email from the ASF dual-hosted git repository.

sergeykamov pushed a change to branch NLPCRAFT-113
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git.


      at b0302e2  WIP.

This branch includes the following new commits:

     new 3887765  WIP.
     new b0302e2  WIP.

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[incubator-nlpcraft] 01/02: WIP.

Posted by se...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

sergeykamov pushed a commit to branch NLPCRAFT-113
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git

commit 388776531e0ebcb430dfd30a474e3d0a2287cceb
Author: Sergey Kamov <se...@apache.org>
AuthorDate: Sun Sep 6 21:11:37 2020 +0300

    WIP.
---
 nlpcraft/pom.xml                                   |   5 +
 .../nlpcraft/server/rest/NCRestCompanySpec.scala   |  27 +++++
 .../nlpcraft/server/rest/NCRestProbeSpec.scala     |  30 +++++
 .../apache/nlpcraft/server/rest/NCRestSpec.scala   | 124 +++++++++++++++++++++
 pom.xml                                            |   6 +
 5 files changed, 192 insertions(+)

diff --git a/nlpcraft/pom.xml b/nlpcraft/pom.xml
index 2e087bd..69a812a 100644
--- a/nlpcraft/pom.xml
+++ b/nlpcraft/pom.xml
@@ -236,6 +236,11 @@
             <artifactId>scalatest_${scala.base}</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>com.jayway.jsonpath</groupId>
+            <artifactId>json-path</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>
diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestCompanySpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestCompanySpec.scala
new file mode 100644
index 0000000..bcd7a61
--- /dev/null
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestCompanySpec.scala
@@ -0,0 +1,27 @@
+/*
+ * 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.server.rest
+
+import org.junit.jupiter.api.Test
+
+class NCRestCompanySpec extends NCRestSpec {
+    @Test
+    def test(): Unit = {
+        //post("company/add", "probes", TYPE_LIST_MAP)
+    }
+}
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
new file mode 100644
index 0000000..51c2c4c
--- /dev/null
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestProbeSpec.scala
@@ -0,0 +1,30 @@
+/*
+ * 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.server.rest
+
+import org.junit.jupiter.api.Test
+
+class NCRestProbeSpec extends NCRestSpec {
+    @Test
+    def test(): Unit = {
+        post("probe/all")(
+            //("$.probes", (probes: Object) ⇒ println(probes + "+++" + probes.getClass)),
+            ("$.probes[:1].hostName", (hostName: AnyRef) ⇒ println("+++" + hostName + "---" + hostName))
+        )
+    }
+}
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
new file mode 100644
index 0000000..5f8745c
--- /dev/null
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestSpec.scala
@@ -0,0 +1,124 @@
+/*
+ * 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.server.rest
+
+import java.util
+
+import com.google.gson.GsonBuilder
+import com.google.gson.reflect.TypeToken
+import com.jayway.jsonpath.JsonPath
+import org.apache.http.HttpResponse
+import org.apache.http.client.ResponseHandler
+import org.apache.http.client.methods.HttpPost
+import org.apache.http.entity.StringEntity
+import org.apache.http.impl.client.HttpClients
+import org.apache.http.util.EntityUtils
+import org.junit.jupiter.api.Assertions._
+import org.junit.jupiter.api.{AfterEach, BeforeEach}
+
+import scala.collection.JavaConverters._
+
+object NCRestSpec {
+    private final val DFLT_BASEURL = "http://localhost:8081/api/v1/"
+    private final val TYPE_RESP = new TypeToken[util.Map[String, Object]]() {}.getType
+    private final val GSON = new GsonBuilder().create
+    private final val CLI = HttpClients.createDefault
+    private final val HANDLER: ResponseHandler[java.util.Map[String, Object]] =
+        (resp: HttpResponse) ⇒ {
+            val code = resp.getStatusLine.getStatusCode
+            val e = resp.getEntity
+
+            val js = if (e != null) EntityUtils.toString(e) else null
+
+            if (js == null)
+                throw new RuntimeException(s"Unexpected empty response [code=$code]")
+
+            code match {
+                case 200 ⇒ GSON.fromJson(js, TYPE_RESP)
+
+                case 400 ⇒ throw new RuntimeException(js)
+                case _ ⇒ throw new RuntimeException(s"Unexpected response [code=$code, response=$js]")
+            }
+        }
+
+    /**
+      *
+      * @param url
+      * @param ps
+      */
+    private def post0(url: String, ps: (String, Any)*): Map[String, Object] = {
+        val post = new HttpPost(DFLT_BASEURL + url)
+
+        post.setHeader("Content-Type", "application/json")
+        post.setEntity(new StringEntity(GSON.toJson(ps.filter(_._2 != null).toMap.asJava), "UTF-8"))
+
+        try
+            CLI.execute(post, HANDLER).asScala.toMap
+        finally
+            post.releaseConnection()
+    }
+}
+
+import org.apache.nlpcraft.server.rest.NCRestSpec._
+
+class NCRestSpec {
+    final val TYPE_MAP = new TypeToken[util.Map[String, Object]]() {}.getType
+    final val TYPE_LIST_MAP = new TypeToken[util.List[util.Map[String, Object]]]() {}.getType
+
+    private var acsTok: String = _
+
+    private def checkStatus(resp: Map[String, Object]): Unit = {
+        assertTrue(resp.contains("status"))
+        assertEquals("API_OK", resp("status"))
+    }
+
+    @BeforeEach
+    def signin(): Unit = {
+        val resp = post0("signin", "email"-> "admin@admin.com", "passwd"-> "admin")
+
+        acsTok = resp("acsTok").asInstanceOf[String]
+
+        assertNotNull(acsTok)
+    }
+
+    @AfterEach
+    def signout(): Unit =
+        if (acsTok != null) {
+            val resp = post0("signout", "acsTok" → acsTok)
+
+            checkStatus(resp)
+
+            acsTok = null
+        }
+
+    def post[T](url: String, ps: (String, Any)*)(checks: (String, AnyRef ⇒ Unit)*): Unit = {
+        assertNotNull(acsTok)
+
+        val resp = post0(url, Seq("acsTok" → acsTok) ++ ps:_*)
+
+        checkStatus(resp)
+
+        val ctx = JsonPath.parse(GSON.toJson(resp.asJava))
+
+        checks.foreach(check ⇒ {
+            val value: String = ctx.read(check._1)
+
+            check._2(value)
+        })
+    }
+}
diff --git a/pom.xml b/pom.xml
index 0541d45..9ae607c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -145,6 +145,7 @@
         <lightstep.ver>0.0.3</lightstep.ver>
         <lightstep.grpc.ver>0.15.8</lightstep.grpc.ver>
         <junit.ver>5.5.1</junit.ver>
+        <jsonpath.ver>2.4.0</jsonpath.ver>
 
         <!-- Force specific encoding on text resources. -->
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -477,6 +478,11 @@
                 <artifactId>junit-jupiter-engine</artifactId>
                 <version>${junit.ver}</version>
             </dependency>
+            <dependency>
+                <groupId>com.jayway.jsonpath</groupId>
+                <artifactId>json-path</artifactId>
+                <version>${jsonpath.ver}</version>
+            </dependency>
         </dependencies>
     </dependencyManagement>
 


[incubator-nlpcraft] 02/02: WIP.

Posted by se...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

sergeykamov pushed a commit to branch NLPCRAFT-113
in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git

commit b0302e213ad055e71478650be11d21ae3a177cb9
Author: Sergey Kamov <se...@apache.org>
AuthorDate: Sun Sep 6 22:53:08 2020 +0300

    WIP.
---
 .../nlpcraft/server/rest/NCRestCompanySpec.scala   | 31 +++++++++++++++++++---
 .../nlpcraft/server/rest/NCRestProbeSpec.scala     |  6 ++---
 .../apache/nlpcraft/server/rest/NCRestSpec.scala   | 26 +++++++++++++-----
 3 files changed, 51 insertions(+), 12 deletions(-)

diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestCompanySpec.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestCompanySpec.scala
index bcd7a61..409cc5a 100644
--- a/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestCompanySpec.scala
+++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/server/rest/NCRestCompanySpec.scala
@@ -17,11 +17,36 @@
 
 package org.apache.nlpcraft.server.rest
 
-import org.junit.jupiter.api.Test
+import java.util.UUID
+
+import org.junit.jupiter.api.{Assertions, Test}
 
 class NCRestCompanySpec extends NCRestSpec {
     @Test
-    def test(): Unit = {
-        //post("company/add", "probes", TYPE_LIST_MAP)
+    def testAdd(): Unit = {
+        val s = UUID.randomUUID().toString
+
+        post(
+            "company/add",
+            "name" → s,
+            "adminEmail" → s"$s@test.com",
+            "adminPasswd" → "test",
+            "adminFirstName" → "test",
+            "adminLastName" → "test"
+        )(
+            ("$.token", (tok: String) ⇒ Assertions.assertNotNull(tok)),
+            ("$.adminId", (adminId: Number) ⇒ Assertions.assertNotNull(adminId))
+        )
+    }
+
+    @Test
+    def testGet(): Unit = {
+        post("company/get")(
+            ("$.id", (id: Number) ⇒ {
+                Assertions.assertNotNull(id)
+                Assertions.assertTrue(id.longValue() > 0)
+            }),
+            ("$.name", (name: String) ⇒ Assertions.assertNotNull(name))
+        )
     }
 }
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 51c2c4c..797b780 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,14 +17,14 @@
 
 package org.apache.nlpcraft.server.rest
 
-import org.junit.jupiter.api.Test
+import org.junit.jupiter.api.{Assertions, Test}
 
 class NCRestProbeSpec extends NCRestSpec {
     @Test
     def test(): Unit = {
         post("probe/all")(
-            //("$.probes", (probes: Object) ⇒ println(probes + "+++" + probes.getClass)),
-            ("$.probes[:1].hostName", (hostName: AnyRef) ⇒ println("+++" + hostName + "---" + hostName))
+            ("$.probes", (probes: Seq[Map[String, Object]]) ⇒ Assertions.assertTrue(probes.nonEmpty)),
+            ("$.probes[:1].hostName", (names: Seq[String]) ⇒ Assertions.assertNotNull(names.head))
         )
     }
 }
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 5f8745c..468394e 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
@@ -36,7 +36,7 @@ import scala.collection.JavaConverters._
 object NCRestSpec {
     private final val DFLT_BASEURL = "http://localhost:8081/api/v1/"
     private final val TYPE_RESP = new TypeToken[util.Map[String, Object]]() {}.getType
-    private final val GSON = new GsonBuilder().create
+    private final val GSON = new GsonBuilder().setPrettyPrinting().create()
     private final val CLI = HttpClients.createDefault
     private final val HANDLER: ResponseHandler[java.util.Map[String, Object]] =
         (resp: HttpResponse) ⇒ {
@@ -106,19 +106,33 @@ class NCRestSpec {
             acsTok = null
         }
 
-    def post[T](url: String, ps: (String, Any)*)(checks: (String, AnyRef ⇒ Unit)*): Unit = {
+    def post[T](url: String, ps: (String, Any)*)(validations: (String, T ⇒ Unit)*): Unit = {
         assertNotNull(acsTok)
 
         val resp = post0(url, Seq("acsTok" → acsTok) ++ ps:_*)
 
         checkStatus(resp)
 
-        val ctx = JsonPath.parse(GSON.toJson(resp.asJava))
+        val js = GSON.toJson(resp.asJava)
 
-        checks.foreach(check ⇒ {
-            val value: String = ctx.read(check._1)
+        println(s"Request [url=$url, parameters=[${ps.map { case (k, v) ⇒ s"$k=$v" }.mkString(", ")}]")
+        println("Response:")
+        println(js)
 
-            check._2(value)
+        val ctx = JsonPath.parse(js)
+
+        validations.foreach(check ⇒ {
+            val v: Object = ctx.read(check._1)
+
+            println(s"Checked value: ${check._1}=$v")
+
+            val x =
+                v match {
+                    case arr: net.minidev.json.JSONArray ⇒ (0 until arr.size()).map(i ⇒ arr.get(i)).asInstanceOf[T]
+                    case _ ⇒ v.asInstanceOf[T]
+                }
+
+            check._2(x)
         })
     }
 }