This is an automated email from the ASF dual-hosted git repository. sergeykamov pushed a commit to branch master-0.7.2-update in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft-java-client.git
commit 8a6d924ea022a902c0eceedafc98cafd473f0c24 Author: Sergey Kamov <[email protected]> AuthorDate: Tue Nov 24 17:06:28 2020 +0300 Version updated: new API methods added, local models for tests implemented, tests extended, minor fixes. --- pom.xml | 79 ++++++++++++------ .../java/org/apache/nlpcraft/client/NCClient.java | 3 + .../org/apache/nlpcraft/client/NCSuggestion.java} | 29 +------ .../apache/nlpcraft/client/NCSuggestionData.java} | 35 +++----- .../apache/nlpcraft/client/impl/NCClientImpl.java | 29 +++++-- .../nlpcraft/client/impl/beans/NCProbeBean.java | 3 +- .../client/impl/beans/NCSuggestionBean.java} | 40 ++++----- .../client/impl/beans/NCSuggestionDataBean.java | 97 ++++++++++++++++++++++ .../client/impl/beans/NCSuggestionResultBean.java} | 29 ++----- .../nlpcraft/client/impl/beans/NCUserBean.java | 4 +- .../org/apache/nlpcraft/client/NCAskSyncTest.java | 13 ++- .../java/org/apache/nlpcraft/client/NCAskTest.java | 10 +-- ...ationTest.java => NCClearConversationTest.java} | 52 +++++------- ...onversationTest.java => NCClearDialogTest.java} | 54 ++++++------ .../org/apache/nlpcraft/client/NCFeedbackTest.java | 12 ++- .../org/apache/nlpcraft/client/NCModelTest.java | 83 ++++++++++++++++++ .../apache/nlpcraft/client/NCMultiClientsTest.java | 28 ++++--- .../apache/nlpcraft/client/NCPermissionsTest.java | 9 +- .../org/apache/nlpcraft/client/NCProbesTest.java | 4 +- .../org/apache/nlpcraft/client/NCTestAdapter.java | 39 +++++---- .../org/apache/nlpcraft/client/NCUserTest.java | 12 ++- .../NCCommonSpecModel.java} | 37 +++++---- .../client/models/NCConversationSpecModel.java | 48 +++++++++++ .../nlpcraft/client/models/NCDialogSpecModel.java | 53 ++++++++++++ .../NCSpecModelAdapter.java} | 48 +++++++---- .../NCSuggestionSpecModel.java} | 39 +++++---- 26 files changed, 589 insertions(+), 300 deletions(-) diff --git a/pom.xml b/pom.xml index 9336402..28b3c7e 100644 --- a/pom.xml +++ b/pom.xml @@ -24,30 +24,30 @@ <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> - <apache.httpcomponents.ver>4.5.7</apache.httpcomponents.ver> - <apache.common.lang.ver>3.8.1</apache.common.lang.ver> - <commons.validator.ver>1.6</commons.validator.ver> - <gson.ver>2.8.5</gson.ver> - <log4j.ver>2.11.2</log4j.ver> - <nlpcraft.ver>0.5.0</nlpcraft.ver> - <junit.ver>5.4.0</junit.ver> - <maven.compiler.version>3.7.0</maven.compiler.version> - <maven.exec.version>1.6.0</maven.exec.version> - <maven.shade.plugin.ver>3.1.0</maven.shade.plugin.ver> + <apache.httpcomponents.ver>4.5.13</apache.httpcomponents.ver> + <apache.common.lang.ver>3.11</apache.common.lang.ver> + <commons.validator.ver>1.7</commons.validator.ver> + <gson.ver>2.8.6</gson.ver> + <log4j.ver>2.14.0</log4j.ver> + <nlpcraft.ver>0.7.2</nlpcraft.ver> + <junit.ver>5.7.0</junit.ver> + <maven.compiler.version>3.8.0</maven.compiler.version> + <maven.shade.plugin.ver>3.2.4</maven.shade.plugin.ver> <maven.source.plugin.ver>3.0.1</maven.source.plugin.ver> <maven.gpg.plugin.ver>1.6</maven.gpg.plugin.ver> <maven.enforcer.plugin.ver>1.4.1</maven.enforcer.plugin.ver> - <maven.surefire.plugin.ver>2.22.1</maven.surefire.plugin.ver> + <maven.surefire.plugin.ver>2.22.2</maven.surefire.plugin.ver> <maven.javadoc.plugin.ver>3.2.0</maven.javadoc.plugin.ver> - <maven.jar.plugin.ver>3.1.1</maven.jar.plugin.ver> + <maven.jar.plugin.ver>3.2.0</maven.jar.plugin.ver> <apache.rat.plugin.ver>0.13</apache.rat.plugin.ver> <maven.bazaarvoice.plugin.ver>0.9</maven.bazaarvoice.plugin.ver> + <maven.clean.plugin.ver>3.1.0</maven.clean.plugin.ver> </properties> <name>NLPCraft - Java Client</name> <groupId>org.apache.nlpcraft</groupId> <artifactId>java-client</artifactId> - <version>0.5.0</version> + <version>0.7.2</version> <url>https://nlpcraft.apache.org</url> <description>An open source API to convert natural language into actions.</description> @@ -84,7 +84,7 @@ <connection>scm:git:ssh://[email protected]/apache/incubator-nlpcraft.git</connection> <developerConnection>scm:git:ssh://[email protected]/apache/incubator-nlpcraft.git</developerConnection> <!-- Set actual tag name here --> - <tag>v0.5.0</tag> + <tag>v0.7.2</tag> </scm> <dependencies> @@ -135,12 +135,28 @@ <build> <plugins> <plugin> + <artifactId>maven-clean-plugin</artifactId> + <version>${maven.clean.plugin.ver}</version> + <configuration> + <filesets> + <fileset> + <directory>${user.home}/.nlpcraft</directory> + <includes> + <include>**/*</include> + </includes> + <followSymlinks>false</followSymlinks> + </fileset> + </filesets> + </configuration> + </plugin> + + <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven.compiler.version}</version> <configuration> - <source>1.8</source> - <target>1.8</target> + <source>11</source> + <target>11</target> </configuration> </plugin> <plugin> @@ -151,12 +167,15 @@ <rules> <requireJavaVersion> <message> - [INFO] ------------------------------------------------------------------------ - [ERROR] You are trying to compile NLPCraft with inappropriate JDK version! - [ERROR] Detected JDK Version: ${java.runtime.version} - [ERROR] JDK version should be >= 1.8.0_1. + + ------------------------------------------------------------------------ + You are trying to compile NLPCraft with inappropriate JDK version! + Detected JDK Version: ${java.runtime.version} + JDK version should be 11 or later + ------------------------------------------------------------------------ + </message> - <version>1.8,11</version> + <version>11</version> </requireJavaVersion> </rules> </configuration> @@ -173,8 +192,6 @@ <artifactId>maven-javadoc-plugin</artifactId> <version>${maven.javadoc.plugin.ver}</version> <configuration> - <source>1.8</source> - <!-- Required as of JDK 8u121 --> <additionalOptions>--allow-script-in-comments, -Xdoclint:none</additionalOptions> <javadocDirectory>${project.basedir}/javadoc</javadocDirectory> <docfilessubdirs>true</docfilessubdirs> @@ -324,13 +341,27 @@ <goal>start</goal> </goals> <configuration> + <!-- + Depending on the console config and how maven is run this will produce the output with ANSI colors. + To strip out ANSI escape sequences from the log file, see the following: + https://stackoverflow.com/questions/17998978/removing-colors-from-output + --> <name>server</name> <healthcheckUrl>http://localhost:8081/api/v1/health</healthcheckUrl> - <waitAfterLaunch>180</waitAfterLaunch> + <waitAfterLaunch>600</waitAfterLaunch> <arguments> <argument>java</argument> <argument>-Xmx4G</argument> <argument>-Xms4G</argument> + <argument>--add-exports=java.base/jdk.internal.misc=ALL-UNNAMED</argument> + <argument>--add-exports=java.base/sun.nio.ch=ALL-UNNAMED</argument> + <argument>--add-exports=java.management/com.sun.jmx.mbeanserver=ALL-UNNAMED</argument> + <argument>--add-exports=jdk.internal.jvmstat/sun.jvmstat.monitor=ALL-UNNAMED</argument> + <argument>--add-exports=java.base/sun.reflect.generics.reflectiveObjects=ALL-UNNAMED</argument> + <argument>--add-opens=jdk.management/com.sun.management.internal=ALL-UNNAMED</argument> + <argument>--illegal-access=permit</argument> + <argument>-DNLPCRAFT_ANSI_COLOR_DISABLED=true</argument> <!-- Remove ANSI at least from NLPCraft output. --> + <argument>-Djdk.tls.client.protocols=TLSv1.2</argument> <argument>-cp</argument> <argument>${project.build.directory}/server-lib/*</argument> <argument>org.apache.nlpcraft.server.NCServer</argument> diff --git a/src/main/java/org/apache/nlpcraft/client/NCClient.java b/src/main/java/org/apache/nlpcraft/client/NCClient.java index 79c7432..9e9d4f4 100644 --- a/src/main/java/org/apache/nlpcraft/client/NCClient.java +++ b/src/main/java/org/apache/nlpcraft/client/NCClient.java @@ -542,6 +542,9 @@ public interface NCClient { * @throws IOException Thrown in case of generic I/O errors. */ void deleteCompany() throws NCClientException, IOException; + + // TODO: + NCSuggestionData suggestSynonyms(String mdlId, Double minScore) throws NCClientException, IOException; /** * Closes the client and signs out from the REST server. Any further calls to this client will result in diff --git a/src/test/java/org/apache/nlpcraft/client/NCProbesTest.java b/src/main/java/org/apache/nlpcraft/client/NCSuggestion.java similarity index 58% copy from src/test/java/org/apache/nlpcraft/client/NCProbesTest.java copy to src/main/java/org/apache/nlpcraft/client/NCSuggestion.java index 0285558..18437e8 100644 --- a/src/test/java/org/apache/nlpcraft/client/NCProbesTest.java +++ b/src/main/java/org/apache/nlpcraft/client/NCSuggestion.java @@ -17,29 +17,8 @@ package org.apache.nlpcraft.client; -import org.apache.nlpcraft.examples.alarm.AlarmModel; -import org.apache.nlpcraft.model.NCModel; -import org.junit.jupiter.api.Test; - -import java.util.Optional; - -import static org.junit.jupiter.api.Assertions.assertFalse; - -/** - * REST client test. Methods `probe/*`. - */ -class NCProbesTest extends NCTestAdapter { - @Override - Optional<Class<? extends NCModel>> getModelClass() { - return Optional.of(AlarmModel.class); - } - - /** - * - * @throws Exception - */ - @Test - void test() throws Exception { - assertFalse(admCli.getProbes().isEmpty()); - } +// TODO: +public interface NCSuggestion { + double getScore(); + String getSynonym(); } diff --git a/src/test/java/org/apache/nlpcraft/client/NCProbesTest.java b/src/main/java/org/apache/nlpcraft/client/NCSuggestionData.java similarity index 59% copy from src/test/java/org/apache/nlpcraft/client/NCProbesTest.java copy to src/main/java/org/apache/nlpcraft/client/NCSuggestionData.java index 0285558..81b9b27 100644 --- a/src/test/java/org/apache/nlpcraft/client/NCProbesTest.java +++ b/src/main/java/org/apache/nlpcraft/client/NCSuggestionData.java @@ -17,29 +17,16 @@ package org.apache.nlpcraft.client; -import org.apache.nlpcraft.examples.alarm.AlarmModel; -import org.apache.nlpcraft.model.NCModel; -import org.junit.jupiter.api.Test; +import java.util.List; +import java.util.Map; -import java.util.Optional; - -import static org.junit.jupiter.api.Assertions.assertFalse; - -/** - * REST client test. Methods `probe/*`. - */ -class NCProbesTest extends NCTestAdapter { - @Override - Optional<Class<? extends NCModel>> getModelClass() { - return Optional.of(AlarmModel.class); - } - - /** - * - * @throws Exception - */ - @Test - void test() throws Exception { - assertFalse(admCli.getProbes().isEmpty()); - } +// TODO: +public interface NCSuggestionData { + String getModelId(); + double getMinScore(); + long getDurationMs(); + long getTimestamp(); + String getError(); + List<Map<String, List<NCSuggestion>>> getSynonyms(); + List<String> getWarnings(); } diff --git a/src/main/java/org/apache/nlpcraft/client/impl/NCClientImpl.java b/src/main/java/org/apache/nlpcraft/client/impl/NCClientImpl.java index 6a5c55b..c44aa2d 100644 --- a/src/main/java/org/apache/nlpcraft/client/impl/NCClientImpl.java +++ b/src/main/java/org/apache/nlpcraft/client/impl/NCClientImpl.java @@ -39,6 +39,7 @@ import org.apache.nlpcraft.client.NCFeedback; import org.apache.nlpcraft.client.NCNewCompany; import org.apache.nlpcraft.client.NCProbe; import org.apache.nlpcraft.client.NCResult; +import org.apache.nlpcraft.client.NCSuggestionData; import org.apache.nlpcraft.client.NCUser; import org.apache.nlpcraft.client.impl.beans.NCAskBean; import org.apache.nlpcraft.client.impl.beans.NCAskSyncBean; @@ -52,12 +53,13 @@ import org.apache.nlpcraft.client.impl.beans.NCProbesAllBean; import org.apache.nlpcraft.client.impl.beans.NCRequestStateBean; import org.apache.nlpcraft.client.impl.beans.NCSigninBean; import org.apache.nlpcraft.client.impl.beans.NCStatusResponseBean; +import org.apache.nlpcraft.client.impl.beans.NCSuggestionResultBean; import org.apache.nlpcraft.client.impl.beans.NCTokenCreationBean; import org.apache.nlpcraft.client.impl.beans.NCUserAddBean; import org.apache.nlpcraft.client.impl.beans.NCUserBean; import org.apache.nlpcraft.client.impl.beans.NCUsersAllBean; -import org.apache.nlpcraft.probe.embedded.NCEmbeddedProbe; -import org.apache.nlpcraft.probe.embedded.NCEmbeddedResult; +import org.apache.nlpcraft.model.tools.embedded.NCEmbeddedProbe; +import org.apache.nlpcraft.model.tools.embedded.NCEmbeddedResult; import java.io.IOException; import java.lang.reflect.Type; @@ -324,7 +326,7 @@ public class NCClientImpl implements NCClient { */ private <T extends NCStatusResponseBean> T checkAndExtract(String js, Type type) throws NCClientException { T t = gson.fromJson(js, type); - + checkStatus(t.getStatus()); return t; @@ -340,7 +342,7 @@ public class NCClientImpl implements NCClient { * @throws IllegalStateException */ @SafeVarargs - private final String post(String url, Pair<String, Object>... ps) throws NCClientException, IOException { + private String post(String url, Pair<String, Object>... ps) throws NCClientException, IOException { if (!started) throw new IllegalStateException("Client is not initialized."); @@ -403,7 +405,7 @@ public class NCClientImpl implements NCClient { if (js == null) throw new NCClientException(String.format("Unexpected empty response [code=%d]", code)); - + if (code == 200) return js; @@ -566,7 +568,7 @@ public class NCClientImpl implements NCClient { "user/get", Pair.of("acsTok", acsTok), Pair.of("id", id), - Pair.of("extId", extId) + Pair.of("usrExtId", extId) ), NCUserBean.class ); @@ -990,4 +992,19 @@ public class NCClientImpl implements NCClient { ) ); } + + @Override + public NCSuggestionData suggestSynonyms(String mdlId, Double minScore) throws NCClientException, IOException { + NCSuggestionResultBean res = checkAndExtract( + post( + "model/sugsyn", + Pair.of("acsTok", acsTok), + Pair.of("mdlId", mdlId), + Pair.of("minScore", minScore) + ), + NCSuggestionResultBean.class + ); + + return res.getResult(); + } } diff --git a/src/main/java/org/apache/nlpcraft/client/impl/beans/NCProbeBean.java b/src/main/java/org/apache/nlpcraft/client/impl/beans/NCProbeBean.java index 635a03c..9f8add9 100644 --- a/src/main/java/org/apache/nlpcraft/client/impl/beans/NCProbeBean.java +++ b/src/main/java/org/apache/nlpcraft/client/impl/beans/NCProbeBean.java @@ -21,6 +21,7 @@ import com.google.gson.annotations.SerializedName; import org.apache.nlpcraft.client.NCModel; import org.apache.nlpcraft.client.NCProbe; +import java.util.Collections; import java.util.HashSet; import java.util.Set; @@ -140,6 +141,6 @@ public class NCProbeBean implements NCProbe { @Override public Set<NCModel> getModels() { - return new HashSet<>(models); + return models != null ? new HashSet<>(models) : Collections.emptySet(); } } diff --git a/src/test/java/org/apache/nlpcraft/client/NCProbesTest.java b/src/main/java/org/apache/nlpcraft/client/impl/beans/NCSuggestionBean.java similarity index 57% copy from src/test/java/org/apache/nlpcraft/client/NCProbesTest.java copy to src/main/java/org/apache/nlpcraft/client/impl/beans/NCSuggestionBean.java index 0285558..267edf9 100644 --- a/src/test/java/org/apache/nlpcraft/client/NCProbesTest.java +++ b/src/main/java/org/apache/nlpcraft/client/impl/beans/NCSuggestionBean.java @@ -15,31 +15,33 @@ * limitations under the License. */ -package org.apache.nlpcraft.client; +package org.apache.nlpcraft.client.impl.beans; -import org.apache.nlpcraft.examples.alarm.AlarmModel; -import org.apache.nlpcraft.model.NCModel; -import org.junit.jupiter.api.Test; - -import java.util.Optional; - -import static org.junit.jupiter.api.Assertions.assertFalse; +import com.google.gson.annotations.SerializedName; +import org.apache.nlpcraft.client.NCSuggestion; /** - * REST client test. Methods `probe/*`. + * REST bean. */ -class NCProbesTest extends NCTestAdapter { +public class NCSuggestionBean implements NCSuggestion { + @SerializedName("score") private double score; + @SerializedName("synonym") private String synonym; + @Override - Optional<Class<? extends NCModel>> getModelClass() { - return Optional.of(AlarmModel.class); + public double getScore() { + return score; } - /** - * - * @throws Exception - */ - @Test - void test() throws Exception { - assertFalse(admCli.getProbes().isEmpty()); + @Override + public String getSynonym() { + return synonym; + } + + @Override + public String toString() { + return "NCSynonymSuggestionBean{" + + "score=" + score + + ", synonym='" + synonym + '\'' + + '}'; } } diff --git a/src/main/java/org/apache/nlpcraft/client/impl/beans/NCSuggestionDataBean.java b/src/main/java/org/apache/nlpcraft/client/impl/beans/NCSuggestionDataBean.java new file mode 100644 index 0000000..938d543 --- /dev/null +++ b/src/main/java/org/apache/nlpcraft/client/impl/beans/NCSuggestionDataBean.java @@ -0,0 +1,97 @@ +/* + * 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.client.impl.beans; + +import com.google.gson.annotations.SerializedName; +import org.apache.nlpcraft.client.NCSuggestionData; +import org.apache.nlpcraft.client.NCSuggestion; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * REST bean. + */ +public class NCSuggestionDataBean implements NCSuggestionData { + @SerializedName("modelId") private String modelId; + @SerializedName("minScore") private double minScore; + @SerializedName("durationMs") private long durationMs; + @SerializedName("timestamp") private long timestamp; + @SerializedName("error") private String error; + @SerializedName("suggestions") private List<Map<String, List<NCSuggestionBean>>> suggestions; + @SerializedName("warnings") private java.util.List<String> warnings; + + @Override + public String getModelId() { + return modelId; + } + + @Override + public double getMinScore() { + return minScore; + } + + @Override + public long getDurationMs() { + return durationMs; + } + + @Override + public long getTimestamp() { + return timestamp; + } + + @Override + public String getError() { + return error; + } + + @Override + public List<Map<String, List<NCSuggestion>>> getSynonyms() { + return suggestions == null ? + null : + suggestions.stream().map( + p -> p.entrySet().stream().collect( + Collectors.toMap( + Map.Entry::getKey, + x -> (List<NCSuggestion>) new ArrayList<NCSuggestion>(x.getValue()) + ) + ) + ).collect(Collectors.toList()); + } + + @Override + public List<String> getWarnings() { + return warnings; + } + + @Override + public String toString() { + return "NCSuggestionSynonymBean{" + + "modelId='" + modelId + '\'' + + ", minScore=" + minScore + + ", durationMs=" + durationMs + + ", timestamp=" + timestamp + + ", error='" + error + '\'' + + ", suggestions=" + suggestions + + ", warnings=" + warnings + + '}'; + } +} diff --git a/src/test/java/org/apache/nlpcraft/client/NCProbesTest.java b/src/main/java/org/apache/nlpcraft/client/impl/beans/NCSuggestionResultBean.java similarity index 57% copy from src/test/java/org/apache/nlpcraft/client/NCProbesTest.java copy to src/main/java/org/apache/nlpcraft/client/impl/beans/NCSuggestionResultBean.java index 0285558..9a2fc60 100644 --- a/src/test/java/org/apache/nlpcraft/client/NCProbesTest.java +++ b/src/main/java/org/apache/nlpcraft/client/impl/beans/NCSuggestionResultBean.java @@ -15,31 +15,18 @@ * limitations under the License. */ -package org.apache.nlpcraft.client; +package org.apache.nlpcraft.client.impl.beans; -import org.apache.nlpcraft.examples.alarm.AlarmModel; -import org.apache.nlpcraft.model.NCModel; -import org.junit.jupiter.api.Test; - -import java.util.Optional; - -import static org.junit.jupiter.api.Assertions.assertFalse; +import com.google.gson.annotations.SerializedName; /** - * REST client test. Methods `probe/*`. + * REST bean. */ -class NCProbesTest extends NCTestAdapter { - @Override - Optional<Class<? extends NCModel>> getModelClass() { - return Optional.of(AlarmModel.class); - } +public class NCSuggestionResultBean extends NCStatusResponseBean { + @SerializedName("result") private NCSuggestionDataBean result; - /** - * - * @throws Exception - */ - @Test - void test() throws Exception { - assertFalse(admCli.getProbes().isEmpty()); + // TODO: + public NCSuggestionDataBean getResult() { + return result; } } diff --git a/src/main/java/org/apache/nlpcraft/client/impl/beans/NCUserBean.java b/src/main/java/org/apache/nlpcraft/client/impl/beans/NCUserBean.java index 84cad39..e734105 100644 --- a/src/main/java/org/apache/nlpcraft/client/impl/beans/NCUserBean.java +++ b/src/main/java/org/apache/nlpcraft/client/impl/beans/NCUserBean.java @@ -27,7 +27,7 @@ import java.util.Map; */ public class NCUserBean extends NCStatusResponseBean implements NCUser { @SerializedName("id") private long id; - @SerializedName("extId") private String extId; + @SerializedName("usrExtId") private String usrExtId; @SerializedName("email") private String email; @SerializedName("firstName") private String firstName; @SerializedName("lastName") private String lastName; @@ -67,7 +67,7 @@ public class NCUserBean extends NCStatusResponseBean implements NCUser { @Override public String getExternalId() { - return extId; + return usrExtId; } @Override diff --git a/src/test/java/org/apache/nlpcraft/client/NCAskSyncTest.java b/src/test/java/org/apache/nlpcraft/client/NCAskSyncTest.java index 102deb7..4a0c80a 100644 --- a/src/test/java/org/apache/nlpcraft/client/NCAskSyncTest.java +++ b/src/test/java/org/apache/nlpcraft/client/NCAskSyncTest.java @@ -17,23 +17,22 @@ package org.apache.nlpcraft.client; -import org.apache.nlpcraft.examples.alarm.AlarmModel; +import org.apache.nlpcraft.client.models.NCCommonSpecModel; import org.apache.nlpcraft.model.NCModel; import org.junit.jupiter.api.Test; import java.util.Optional; import java.util.function.Consumer; +import static org.apache.nlpcraft.client.models.NCCommonSpecModel.MDL_ID; + /** * REST client test. Method `ask/sync`. */ class NCAskSyncTest extends NCTestAdapter { - // - private static final String MDL_ID = "nlpcraft.alarm.ex"; - @Override Optional<Class<? extends NCModel>> getModelClass() { - return Optional.of(AlarmModel.class); + return Optional.of(NCCommonSpecModel.class); } /** @@ -66,8 +65,6 @@ class NCAskSyncTest extends NCTestAdapter { // Only latin charset is supported. check("El tiempo en España", this::checkError); - check("Ping me in 3 minutes", this::checkOk); - check("Buzz me in an hour and 15mins", this::checkOk); - check("Set my alarm for 30s", this::checkOk); + check("test", this::checkOk); } } diff --git a/src/test/java/org/apache/nlpcraft/client/NCAskTest.java b/src/test/java/org/apache/nlpcraft/client/NCAskTest.java index dcefb9c..78e0d1a 100644 --- a/src/test/java/org/apache/nlpcraft/client/NCAskTest.java +++ b/src/test/java/org/apache/nlpcraft/client/NCAskTest.java @@ -17,7 +17,7 @@ package org.apache.nlpcraft.client; -import org.apache.nlpcraft.examples.alarm.AlarmModel; +import org.apache.nlpcraft.client.models.NCCommonSpecModel; import org.apache.nlpcraft.model.NCModel; import org.junit.jupiter.api.Test; @@ -25,6 +25,7 @@ import java.util.List; import java.util.Optional; import java.util.function.Consumer; +import static org.apache.nlpcraft.client.models.NCCommonSpecModel.MDL_ID; import static org.junit.jupiter.api.Assertions.fail; /** @@ -32,14 +33,11 @@ import static org.junit.jupiter.api.Assertions.fail; */ class NCAskTest extends NCTestAdapter { /** */ - private static final String MDL_ID = "nlpcraft.alarm.ex"; - - /** */ private static final int MAX_TEST_TIME = 20000; @Override Optional<Class<? extends NCModel>> getModelClass() { - return Optional.of(AlarmModel.class); + return Optional.of(NCCommonSpecModel.class); } /** @@ -85,7 +83,7 @@ class NCAskTest extends NCTestAdapter { */ @Test void test() throws Exception { - test0("Ping me in 3 minutes", this::checkOk); + test0("test", this::checkOk); test0("El tiempo en España", this::checkError); } } diff --git a/src/test/java/org/apache/nlpcraft/client/NCConversationTest.java b/src/test/java/org/apache/nlpcraft/client/NCClearConversationTest.java similarity index 57% copy from src/test/java/org/apache/nlpcraft/client/NCConversationTest.java copy to src/test/java/org/apache/nlpcraft/client/NCClearConversationTest.java index 7dcc5d0..7dc0e49 100644 --- a/src/test/java/org/apache/nlpcraft/client/NCConversationTest.java +++ b/src/test/java/org/apache/nlpcraft/client/NCClearConversationTest.java @@ -17,7 +17,7 @@ package org.apache.nlpcraft.client; -import org.apache.nlpcraft.examples.weather.WeatherModel; +import org.apache.nlpcraft.client.models.NCConversationSpecModel; import org.apache.nlpcraft.model.NCModel; import org.junit.jupiter.api.Test; @@ -27,15 +27,10 @@ import java.util.function.Consumer; /** * REST client test. Methods `clear/conversation`. */ -class NCConversationTest extends NCTestAdapter { - /** - * - */ - private static final String MDL_ID = "nlpcraft.weather.ex"; - +class NCClearConversationTest extends NCTestAdapter { @Override Optional<Class<? extends NCModel>> getModelClass() { - return Optional.of(WeatherModel.class); + return Optional.of(NCConversationSpecModel.class); } /** @@ -44,40 +39,37 @@ class NCConversationTest extends NCTestAdapter { * @throws Exception */ private void check(String txt, Consumer<NCResult> resConsumer) throws Exception { - resConsumer.accept(admCli.askSync(MDL_ID, txt, null, true, null, null)); + resConsumer.accept(admCli.askSync(NCConversationSpecModel.MDL_ID, txt, null, true, null, null)); } - /** - * @throws Exception - */ - @Test - void test1() throws Exception { - check("What's the weather in Moscow?", this::checkOk); - - // Should be answered with conversation. - check("Moscow", this::checkOk); + private void flow() throws Exception { + // missed 'test1' + check("test2", this::checkError); - admCli.clearConversation(MDL_ID, null, null); + check("test1 test2", this::checkOk); - // Cannot be answered without conversation. - check("Moscow", this::checkError); + // 'test1' received from conversation. + check("test2", this::checkOk); } /** * @throws Exception */ @Test - void test2() throws Exception { - check("What's the weather in Moscow?", this::checkOk); - - // Should be answered with conversation. - check("Moscow", this::checkOk); + void test() throws Exception { + flow(); - admCli.clearConversation(MDL_ID, + admCli.clearConversation( + NCConversationSpecModel.MDL_ID, // Finds its own ID. - get(admCli.getAllUsers(), (u) -> NCClientBuilder.DFLT_EMAIL.equals(u.getEmail())).getId(), null); + get(admCli.getAllUsers(), (u) -> NCClientBuilder.DFLT_EMAIL.equals(u.getEmail())).getId(), + null + ); + + flow(); + + admCli.clearConversation(NCConversationSpecModel.MDL_ID, null, null); - // Cannot be answered without conversation. - check("Moscow", this::checkError); + flow(); } } diff --git a/src/test/java/org/apache/nlpcraft/client/NCConversationTest.java b/src/test/java/org/apache/nlpcraft/client/NCClearDialogTest.java similarity index 53% rename from src/test/java/org/apache/nlpcraft/client/NCConversationTest.java rename to src/test/java/org/apache/nlpcraft/client/NCClearDialogTest.java index 7dcc5d0..92a871d 100644 --- a/src/test/java/org/apache/nlpcraft/client/NCConversationTest.java +++ b/src/test/java/org/apache/nlpcraft/client/NCClearDialogTest.java @@ -17,7 +17,7 @@ package org.apache.nlpcraft.client; -import org.apache.nlpcraft.examples.weather.WeatherModel; +import org.apache.nlpcraft.client.models.NCDialogSpecModel; import org.apache.nlpcraft.model.NCModel; import org.junit.jupiter.api.Test; @@ -25,17 +25,12 @@ import java.util.Optional; import java.util.function.Consumer; /** - * REST client test. Methods `clear/conversation`. + * REST client test. Methods `clear/dialog`. */ -class NCConversationTest extends NCTestAdapter { - /** - * - */ - private static final String MDL_ID = "nlpcraft.weather.ex"; - +class NCClearDialogTest extends NCTestAdapter { @Override Optional<Class<? extends NCModel>> getModelClass() { - return Optional.of(WeatherModel.class); + return Optional.of(NCDialogSpecModel.class); } /** @@ -44,40 +39,43 @@ class NCConversationTest extends NCTestAdapter { * @throws Exception */ private void check(String txt, Consumer<NCResult> resConsumer) throws Exception { - resConsumer.accept(admCli.askSync(MDL_ID, txt, null, true, null, null)); + resConsumer.accept(admCli.askSync(NCDialogSpecModel.MDL_ID, txt, null, true, null, null)); } /** + * * @throws Exception */ - @Test - void test1() throws Exception { - check("What's the weather in Moscow?", this::checkOk); + private void flow() throws Exception { + // There isn't `test1` before. + check("test2", this::checkError); + + // `test1` is always ok. + check("test1", this::checkOk); - // Should be answered with conversation. - check("Moscow", this::checkOk); + // There is one `test1` before. + check("test2", this::checkOk); - admCli.clearConversation(MDL_ID, null, null); + // `test1` is always ok. + check("test1", this::checkOk); + check("test1", this::checkOk); - // Cannot be answered without conversation. - check("Moscow", this::checkError); + // There are too much `test1` before. + // TODO: + //check("test2", this::checkError); } /** + * * @throws Exception */ @Test - void test2() throws Exception { - check("What's the weather in Moscow?", this::checkOk); - - // Should be answered with conversation. - check("Moscow", this::checkOk); + void test() throws Exception { + flow(); - admCli.clearConversation(MDL_ID, - // Finds its own ID. - get(admCli.getAllUsers(), (u) -> NCClientBuilder.DFLT_EMAIL.equals(u.getEmail())).getId(), null); + admCli.clearDialog(NCDialogSpecModel.MDL_ID, null, null); + admCli.clearConversation(NCDialogSpecModel.MDL_ID, null, null); - // Cannot be answered without conversation. - check("Moscow", this::checkError); + flow(); } } diff --git a/src/test/java/org/apache/nlpcraft/client/NCFeedbackTest.java b/src/test/java/org/apache/nlpcraft/client/NCFeedbackTest.java index 572afe7..7d2fb71 100644 --- a/src/test/java/org/apache/nlpcraft/client/NCFeedbackTest.java +++ b/src/test/java/org/apache/nlpcraft/client/NCFeedbackTest.java @@ -17,24 +17,22 @@ package org.apache.nlpcraft.client; -import org.apache.nlpcraft.examples.alarm.AlarmModel; +import org.apache.nlpcraft.client.models.NCCommonSpecModel; import org.apache.nlpcraft.model.NCModel; import org.junit.jupiter.api.Test; import java.util.Optional; +import static org.apache.nlpcraft.client.models.NCCommonSpecModel.MDL_ID; import static org.junit.jupiter.api.Assertions.assertEquals; /** * REST client test. Methods `ask`. */ class NCFeedbackTest extends NCTestAdapter { - /** */ - private static final String MDL_ID = "nlpcraft.alarm.ex"; - @Override Optional<Class<? extends NCModel>> getModelClass() { - return Optional.of(AlarmModel.class); + return Optional.of(NCCommonSpecModel.class); } /** @@ -45,7 +43,7 @@ class NCFeedbackTest extends NCTestAdapter { */ private void test0(Long usrId, String usrExtId) throws Exception { // Adds request. - String srvReqId = admCli.ask(MDL_ID, "Ping me in 3 minutes", null, false, usrId, usrExtId); + String srvReqId = admCli.ask(MDL_ID, "test", null, false, usrId, usrExtId); // Creates 2 feedback. long fId1 = admCli.addFeedback(srvReqId, 0.5, null, usrId, usrExtId); @@ -64,7 +62,7 @@ class NCFeedbackTest extends NCTestAdapter { // Creates 2 feedback. admCli.addFeedback(srvReqId, 0.5, null, usrId, usrExtId); - admCli.addFeedback(srvReqId, 0.1, "Somme comment", usrId, usrExtId); + admCli.addFeedback(srvReqId, 0.1, "Some comment", usrId, usrExtId); // Checks their existing. assertEquals(2, admCli.getAllFeedback(null, usrId, usrExtId).size()); diff --git a/src/test/java/org/apache/nlpcraft/client/NCModelTest.java b/src/test/java/org/apache/nlpcraft/client/NCModelTest.java new file mode 100644 index 0000000..de94337 --- /dev/null +++ b/src/test/java/org/apache/nlpcraft/client/NCModelTest.java @@ -0,0 +1,83 @@ +/* + * 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.client; + +import org.apache.nlpcraft.client.models.NCSuggestionSpecModel; +import org.apache.nlpcraft.model.NCModel; +import org.junit.jupiter.api.Test; + +import java.util.Collection; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +import static org.apache.nlpcraft.client.models.NCSuggestionSpecModel.MDL_ID; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * REST client test. Methods `model/sugsyn`. + */ +class NCModelTest extends NCTestAdapter { + @Override + Optional<Class<? extends NCModel>> getModelClass() { + return Optional.of(NCSuggestionSpecModel.class); + } + + /** + * + * @param s + * @param minScore + * @throws Exception + */ + private static void check(NCSuggestionData s, double minScore) { + assertNotNull(s); + + assertNull(s.getError()); + assertNotNull(s.getModelId()); + assertNotNull(s.getSynonyms()); + assertFalse(s.getSynonyms().isEmpty()); + assertFalse(s.getSynonyms().isEmpty()); + assertEquals(minScore, s.getMinScore()); + + List<NCSuggestion> allSugs = + s.getSynonyms(). + stream(). + flatMap(p -> p.values().stream().flatMap(Collection::stream)). + collect(Collectors.toList()); + + assertFalse(allSugs.isEmpty()); + + allSugs.forEach(sugg -> { + assertNotNull(sugg.getSynonym()); + assertTrue(sugg.getScore() >= minScore); + }); + } + + /** + * @throws Exception + */ + @Test + void testSuggestionSynonym() throws Exception { + check(admCli.suggestSynonyms(MDL_ID, null), 0); + check(admCli.suggestSynonyms(MDL_ID, 0.5), 0.5); + } +} diff --git a/src/test/java/org/apache/nlpcraft/client/NCMultiClientsTest.java b/src/test/java/org/apache/nlpcraft/client/NCMultiClientsTest.java index 099e182..6dbd361 100644 --- a/src/test/java/org/apache/nlpcraft/client/NCMultiClientsTest.java +++ b/src/test/java/org/apache/nlpcraft/client/NCMultiClientsTest.java @@ -18,8 +18,9 @@ package org.apache.nlpcraft.client; import org.apache.http.client.config.RequestConfig; -import org.apache.nlpcraft.examples.alarm.AlarmModel; +import org.apache.nlpcraft.client.models.NCCommonSpecModel; import org.apache.nlpcraft.model.NCModel; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.util.Optional; @@ -31,17 +32,15 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static org.apache.nlpcraft.client.models.NCCommonSpecModel.MDL_ID; /** * REST client test. Methods `ask`. */ class NCMultiClientsTest extends NCTestAdapter { - /** */ - private static final String MDL_ID = "nlpcraft.alarm.ex"; - @Override Optional<Class<? extends NCModel>> getModelClass() { - return Optional.of(AlarmModel.class); + return Optional.of(NCCommonSpecModel.class); } /** @@ -63,7 +62,7 @@ class NCMultiClientsTest extends NCTestAdapter { long maxTime = System.currentTimeMillis() + testTimeMs; CountDownLatch cdl = new CountDownLatch(clientsCnt); - + for (int i = 0; i < clientsCnt; i++) { int fi = i; @@ -84,7 +83,7 @@ class NCMultiClientsTest extends NCTestAdapter { while (System.currentTimeMillis() < maxTime && err.get() == null) { client.ask( MDL_ID, - "Ping me in 3 minutes", + "test", null, false, null, @@ -93,7 +92,7 @@ class NCMultiClientsTest extends NCTestAdapter { System.out.println("Request sent [reqNum=" + cnt.incrementAndGet() + ", clientNum=" + fi + ']'); - Thread.sleep(rnd.nextInt(500) + 1); + Thread.sleep(rnd.nextInt(400) + 100); } } finally { @@ -113,10 +112,15 @@ class NCMultiClientsTest extends NCTestAdapter { } cdl.await(timeoutMs * 2, MILLISECONDS); - - if (err.get() != null) - throw err.get(); - + + Throwable t = err.get(); + + if (t != null) { + t.printStackTrace(); + + Assertions.fail(t.getMessage()); + } + System.out.println("Clients count: " + clientsCnt); System.out.println("Processed requests: " + cnt.get()); } diff --git a/src/test/java/org/apache/nlpcraft/client/NCPermissionsTest.java b/src/test/java/org/apache/nlpcraft/client/NCPermissionsTest.java index 9e9cad5..1242f77 100644 --- a/src/test/java/org/apache/nlpcraft/client/NCPermissionsTest.java +++ b/src/test/java/org/apache/nlpcraft/client/NCPermissionsTest.java @@ -17,7 +17,7 @@ package org.apache.nlpcraft.client; -import org.apache.nlpcraft.examples.alarm.AlarmModel; +import org.apache.nlpcraft.client.models.NCCommonSpecModel; import org.apache.nlpcraft.model.NCModel; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -25,19 +25,18 @@ import org.junit.jupiter.api.Test; import java.util.Optional; +import static org.apache.nlpcraft.client.models.NCCommonSpecModel.MDL_ID; + /** * REST client test. Checks user permissions. */ class NCPermissionsTest extends NCTestAdapter { /** */ - private static final String MDL_ID = "nlpcraft.alarm.ex"; - - /** */ private NCClient client; @Override Optional<Class<? extends NCModel>> getModelClass() { - return Optional.of(AlarmModel.class); + return Optional.of(NCCommonSpecModel.class); } /** diff --git a/src/test/java/org/apache/nlpcraft/client/NCProbesTest.java b/src/test/java/org/apache/nlpcraft/client/NCProbesTest.java index 0285558..5d6fb80 100644 --- a/src/test/java/org/apache/nlpcraft/client/NCProbesTest.java +++ b/src/test/java/org/apache/nlpcraft/client/NCProbesTest.java @@ -17,7 +17,7 @@ package org.apache.nlpcraft.client; -import org.apache.nlpcraft.examples.alarm.AlarmModel; +import org.apache.nlpcraft.client.models.NCCommonSpecModel; import org.apache.nlpcraft.model.NCModel; import org.junit.jupiter.api.Test; @@ -31,7 +31,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; class NCProbesTest extends NCTestAdapter { @Override Optional<Class<? extends NCModel>> getModelClass() { - return Optional.of(AlarmModel.class); + return Optional.of(NCCommonSpecModel.class); } /** diff --git a/src/test/java/org/apache/nlpcraft/client/NCTestAdapter.java b/src/test/java/org/apache/nlpcraft/client/NCTestAdapter.java index d8a329d..bdd7a9c 100644 --- a/src/test/java/org/apache/nlpcraft/client/NCTestAdapter.java +++ b/src/test/java/org/apache/nlpcraft/client/NCTestAdapter.java @@ -18,7 +18,7 @@ package org.apache.nlpcraft.client; import org.apache.nlpcraft.model.NCModel; -import org.apache.nlpcraft.probe.embedded.NCEmbeddedProbe; +import org.apache.nlpcraft.model.tools.embedded.NCEmbeddedProbe; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -54,7 +54,7 @@ abstract class NCTestAdapter { protected long admUsrId; /** */ - private final Optional<Class<? extends NCModel>> mdlOpt = getModelClass(); + private Class<? extends NCModel> mdlClass; /** * @@ -70,7 +70,10 @@ abstract class NCTestAdapter { */ @BeforeEach void setUp() throws Exception { - mdlOpt.ifPresent(NCEmbeddedProbe::start); + mdlClass = getModelClass().orElse(null); + + if (mdlClass != null) + NCEmbeddedProbe.start(mdlClass); admCli = new NCClientBuilder().build(); @@ -92,7 +95,7 @@ abstract class NCTestAdapter { admCli.close(); } - if (mdlOpt.isPresent()) + if (mdlClass != null) NCEmbeddedProbe.stop(); } @@ -163,8 +166,8 @@ abstract class NCTestAdapter { protected<T> T get(List<T> list, Predicate<T> p) { Optional<T> opt = getOpt(list, p); - if (!opt.isPresent()) - fail("Object not found"); + if (opt.isEmpty()) + fail("Object not found in list: " + list); return opt.get(); } @@ -174,13 +177,11 @@ abstract class NCTestAdapter { * @param state */ protected void checkOk(NCResult state) { - System.out.println( - String.format( - "Text: %s \ntype: %s\nresult: %s", - state.getText(), - state.getResultType(), - state.getResultBody() - ) + System.out.printf( + "Text: %s \ntype: %s\nresult: %s%n", + state.getText(), + state.getResultType(), + state.getResultBody() ); if (state.getLogHolder() != null) @@ -199,13 +200,11 @@ abstract class NCTestAdapter { protected void checkError(NCResult state) { assert state != null; - System.out.println( - String.format( - "Text: %s \nerror: %s\ncode: %d", - state.getText(), - state.getErrorMessage(), - state.getErrorCode() - ) + System.out.printf( + "Text: %s \nerror: %s\ncode: %d%n", + state.getText(), + state.getErrorMessage(), + state.getErrorCode() ); if (state.getLogHolder() != null) diff --git a/src/test/java/org/apache/nlpcraft/client/NCUserTest.java b/src/test/java/org/apache/nlpcraft/client/NCUserTest.java index ff0fa15..61151de 100644 --- a/src/test/java/org/apache/nlpcraft/client/NCUserTest.java +++ b/src/test/java/org/apache/nlpcraft/client/NCUserTest.java @@ -17,13 +17,14 @@ package org.apache.nlpcraft.client; -import org.apache.nlpcraft.examples.alarm.AlarmModel; +import org.apache.nlpcraft.client.models.NCCommonSpecModel; import org.apache.nlpcraft.model.NCModel; import org.junit.jupiter.api.Test; import java.util.List; import java.util.Optional; +import static org.apache.nlpcraft.client.models.NCCommonSpecModel.MDL_ID; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -31,12 +32,9 @@ import static org.junit.jupiter.api.Assertions.assertFalse; * REST client test. Methods `user/*`. */ class NCUserTest extends NCTestAdapter { - /** */ - private static final String MDL_ID = "nlpcraft.alarm.ex"; - @Override Optional<Class<? extends NCModel>> getModelClass() { - return Optional.of(AlarmModel.class); + return Optional.of(NCCommonSpecModel.class); } /** @@ -172,7 +170,7 @@ class NCUserTest extends NCTestAdapter { String extId = "extId"; // Implicitly creates user with external ID. - admCli.askSync(MDL_ID, "Ping me in 3 minutes", null, false, null, extId); + admCli.askSync(MDL_ID, "test", null, false, null, extId); NCUser newUser = get(admCli.getAllUsers(), (u) -> extId.equals(u.getExternalId())); @@ -195,7 +193,7 @@ class NCUserTest extends NCTestAdapter { String extId = "extId"; // Implicitly creates user with external ID. - admCli.askSync(MDL_ID, "Ping me in 3 minutes", null, false, null, extId); + admCli.askSync(MDL_ID, "test", null, false, null, extId); // Updates existing user with given external ID. admCli.addUser( diff --git a/src/test/java/org/apache/nlpcraft/client/NCProbesTest.java b/src/test/java/org/apache/nlpcraft/client/models/NCCommonSpecModel.java similarity index 55% copy from src/test/java/org/apache/nlpcraft/client/NCProbesTest.java copy to src/test/java/org/apache/nlpcraft/client/models/NCCommonSpecModel.java index 0285558..03b20af 100644 --- a/src/test/java/org/apache/nlpcraft/client/NCProbesTest.java +++ b/src/test/java/org/apache/nlpcraft/client/models/NCCommonSpecModel.java @@ -15,31 +15,32 @@ * limitations under the License. */ -package org.apache.nlpcraft.client; +package org.apache.nlpcraft.client.models; -import org.apache.nlpcraft.examples.alarm.AlarmModel; -import org.apache.nlpcraft.model.NCModel; -import org.junit.jupiter.api.Test; +import org.apache.nlpcraft.model.NCElement; +import org.apache.nlpcraft.model.NCIntent; -import java.util.Optional; - -import static org.junit.jupiter.api.Assertions.assertFalse; +import java.util.Collections; +import java.util.Set; /** - * REST client test. Methods `probe/*`. + * Test model. */ -class NCProbesTest extends NCTestAdapter { +public class NCCommonSpecModel extends NCSpecModelAdapter { + public static final String MDL_ID = NCCommonSpecModel.class.getSimpleName(); + + @Override + public String getId() { + return MDL_ID; + } + @Override - Optional<Class<? extends NCModel>> getModelClass() { - return Optional.of(AlarmModel.class); + public Set<NCElement> getElements() { + return Collections.singleton(mkElement("test")); } - /** - * - * @throws Exception - */ - @Test - void test() throws Exception { - assertFalse(admCli.getProbes().isEmpty()); + @NCIntent("intent=intentId term~{id == 'test'}") + public org.apache.nlpcraft.model.NCResult onTest() { + return org.apache.nlpcraft.model.NCResult.text("OK"); } } diff --git a/src/test/java/org/apache/nlpcraft/client/models/NCConversationSpecModel.java b/src/test/java/org/apache/nlpcraft/client/models/NCConversationSpecModel.java new file mode 100644 index 0000000..2050631 --- /dev/null +++ b/src/test/java/org/apache/nlpcraft/client/models/NCConversationSpecModel.java @@ -0,0 +1,48 @@ +/* + * 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.client.models; + +import org.apache.nlpcraft.model.NCElement; +import org.apache.nlpcraft.model.NCIntent; + +import java.util.HashSet; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * Test model. + */ +public class NCConversationSpecModel extends NCSpecModelAdapter { + public static final String MDL_ID = NCConversationSpecModel.class.getSimpleName(); + + @Override + public String getId() { + return MDL_ID; + } + @Override + public Set<NCElement> getElements() { + return Stream.of(mkElement("test1"), mkElement("test2")).collect(Collectors.toCollection(HashSet::new)); + } + + // 'test1' is mandatory, 'test2' is optional. + @NCIntent("intent=intentId term~{id == 'test1'} term~{id == 'test2'}?") + public org.apache.nlpcraft.model.NCResult onTest() { + return org.apache.nlpcraft.model.NCResult.text("OK"); + } +} diff --git a/src/test/java/org/apache/nlpcraft/client/models/NCDialogSpecModel.java b/src/test/java/org/apache/nlpcraft/client/models/NCDialogSpecModel.java new file mode 100644 index 0000000..1176259 --- /dev/null +++ b/src/test/java/org/apache/nlpcraft/client/models/NCDialogSpecModel.java @@ -0,0 +1,53 @@ +/* + * 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.client.models; + +import org.apache.nlpcraft.model.NCElement; +import org.apache.nlpcraft.model.NCIntent; + +import java.util.HashSet; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * Test model. + */ +public class NCDialogSpecModel extends NCSpecModelAdapter { + public static final String MDL_ID = NCDialogSpecModel.class.getSimpleName(); + + @Override + public String getId() { + return MDL_ID; + } + + @Override + public Set<NCElement> getElements() { + return Stream.of(mkElement("test1"), mkElement("test2")).collect(Collectors.toCollection(HashSet::new)); + } + + @NCIntent("intent=intentId1 term~{id == 'test1'}") + public org.apache.nlpcraft.model.NCResult onTest1() { + return org.apache.nlpcraft.model.NCResult.text("OK"); + } + + @NCIntent("intent=intentId2 flow='intentId1[1,1]' term~{id == 'test2'}") + public org.apache.nlpcraft.model.NCResult onTest2() { + return org.apache.nlpcraft.model.NCResult.text("OK"); + } +} diff --git a/src/test/java/org/apache/nlpcraft/client/NCProbesTest.java b/src/test/java/org/apache/nlpcraft/client/models/NCSpecModelAdapter.java similarity index 52% copy from src/test/java/org/apache/nlpcraft/client/NCProbesTest.java copy to src/test/java/org/apache/nlpcraft/client/models/NCSpecModelAdapter.java index 0285558..8816a9b 100644 --- a/src/test/java/org/apache/nlpcraft/client/NCProbesTest.java +++ b/src/test/java/org/apache/nlpcraft/client/models/NCSpecModelAdapter.java @@ -15,31 +15,45 @@ * limitations under the License. */ -package org.apache.nlpcraft.client; +package org.apache.nlpcraft.client.models; -import org.apache.nlpcraft.examples.alarm.AlarmModel; +import org.apache.nlpcraft.model.NCElement; import org.apache.nlpcraft.model.NCModel; -import org.junit.jupiter.api.Test; -import java.util.Optional; - -import static org.junit.jupiter.api.Assertions.assertFalse; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; /** - * REST client test. Methods `probe/*`. + * Test model adapter. */ -class NCProbesTest extends NCTestAdapter { +abstract class NCSpecModelAdapter implements NCModel { + @Override + public String getName() { + return getId(); + } + @Override - Optional<Class<? extends NCModel>> getModelClass() { - return Optional.of(AlarmModel.class); + public String getVersion() { + return "1.0.0"; } - /** - * - * @throws Exception - */ - @Test - void test() throws Exception { - assertFalse(admCli.getProbes().isEmpty()); + protected NCElement mkElement(String id, String... syns) { + return new NCElement() { + @Override + public String getId() { + return id; + } + + @Override + public List<String> getSynonyms() { + List<String> l = new ArrayList<>(); + + l.add(id); + l.addAll(Arrays.asList(syns)); + + return l; + } + }; } } diff --git a/src/test/java/org/apache/nlpcraft/client/NCProbesTest.java b/src/test/java/org/apache/nlpcraft/client/models/NCSuggestionSpecModel.java similarity index 51% copy from src/test/java/org/apache/nlpcraft/client/NCProbesTest.java copy to src/test/java/org/apache/nlpcraft/client/models/NCSuggestionSpecModel.java index 0285558..c4875f9 100644 --- a/src/test/java/org/apache/nlpcraft/client/NCProbesTest.java +++ b/src/test/java/org/apache/nlpcraft/client/models/NCSuggestionSpecModel.java @@ -15,31 +15,34 @@ * limitations under the License. */ -package org.apache.nlpcraft.client; +package org.apache.nlpcraft.client.models; -import org.apache.nlpcraft.examples.alarm.AlarmModel; -import org.apache.nlpcraft.model.NCModel; -import org.junit.jupiter.api.Test; +import org.apache.nlpcraft.model.NCElement; +import org.apache.nlpcraft.model.NCIntent; +import org.apache.nlpcraft.model.NCIntentSample; -import java.util.Optional; - -import static org.junit.jupiter.api.Assertions.assertFalse; +import java.util.Collections; +import java.util.Set; /** - * REST client test. Methods `probe/*`. + * Test model. */ -class NCProbesTest extends NCTestAdapter { +public class NCSuggestionSpecModel extends NCSpecModelAdapter { + public static final String MDL_ID = NCSuggestionSpecModel.class.getSimpleName(); + + @Override + public String getId() { + return MDL_ID; + } + @Override - Optional<Class<? extends NCModel>> getModelClass() { - return Optional.of(AlarmModel.class); + public Set<NCElement> getElements() { + return Collections.singleton(mkElement( "test")); } - /** - * - * @throws Exception - */ - @Test - void test() throws Exception { - assertFalse(admCli.getProbes().isEmpty()); + @NCIntentSample({"Tests passed well"}) + @NCIntent("intent=intentId term~{id == 'test'}") + public org.apache.nlpcraft.model.NCResult onTest() { + return org.apache.nlpcraft.model.NCResult.text("OK"); } }
