Class NCTestAutoModelValidator


  • public class NCTestAutoModelValidator
    extends Object
    Data model auto-validator is based on NCIntentSample and NCIntentSampleRef annotations. Validation consists of starting an embedded probe, scanning all deployed models for these annotations and their corresponding callback methods, submitting each sample input sentences from the annotation and checking that resulting intent matches the intent the sample was attached to.

    Note that there can be more than one NCIntentSample or NCIntentSampleRef annotation attached to the intent callback. Each such annotation will trigger conversation STM reset before its samples will be submitted. This gives an opportunity to test samples both with and without conversational context as well as the same sample but with multiple different conversation contexts.

    This class can be used in two modes:

    • As a standalone application. See main(String[]) method for details. In this mode it can be used to automatically test models from IDE, maven builds, etc. without creating a separate, dedicated unit test for it.
    • As a utility class that can be called programmatically from other classes, e.g. unit tests. See isValid() and isValid(Class) methods for more details.

    See Alarm Clock, Light Switch, and Weather Bot examples for usage of model auto-validator.

    See Also:
    NCIntentSample, NCIntentSampleRef, NCIntent, NCIntentRef
    • Field Detail

      • PROP_MODELS

        public static final String PROP_MODELS
        Optional, comma-separate list of class names for the models to test.
        See Also:
        Constant Field Values
    • Constructor Detail

      • NCTestAutoModelValidator

        public NCTestAutoModelValidator()
    • Method Detail

      • main

        public static void main​(String[] args)
                         throws Exception
        Performs validation based on NCIntentSample and NCIntentSampleRef annotations.

        This is an entry point for a standalone application that expects two system properties (both optional):

        • NLPCRAFT_TEST_MODELS - optional comma separated list of fully qualified data model class names to test. Validator will start the embedded probe with these models as an override for 'nlpcraft.probe.models' configuration value. If not provided - the models defined in configuration (default or provided via NLPCRAFT_PROBE_CONFIG property) will be used.
        • NLPCRAFT_PROBE_CONFIG - optional path to probe configuration file. If not provided - the default NLPCraft configuration will be used.
        Note that standard validation output will be printed out to the configured logger (e.g. log4j), if any.
        Parameters:
        args - These arguments are ignored.
        Throws:
        Exception - Thrown in case of any unexpected errors during validation. Note that standard validation output will be printed out to the configured logger.
      • isValid

        public static boolean isValid()
                               throws Exception
        Performs validation based on NCIntentSample and NCIntentSampleRef annotations.

        This method accepts two system properties (both optional):

        • NLPCRAFT_TEST_MODELS - optional comma separated list of fully qualified data model class names to test. Validator will start the embedded probe with these models as an override for 'nlpcraft.probe.models' configuration value. If not provided - the models defined in configuration (default or provided via NLPCRAFT_PROBE_CONFIG property) will be used.
        • NLPCRAFT_PROBE_CONFIG - optional path to probe configuration file. If not provided - the default NLPCraft configuration will be used.
        Note that standard validation output will be printed out to the configured logger (e.g. log4j), if any.
        Returns:
        True if no validation errors found, false otherwise. Note that standard validation output will be printed out to the configured logger (e.g. log4j), if any.
        Throws:
        Exception - Thrown in case of any unexpected errors during validation. Note that standard validation output will be printed out to the configured logger (e.g. log4j), if any.
      • isValid

        public static boolean isValid​(Class<NCModel> claxx)
                               throws Exception
        Performs validation based on NCIntentSample and NCIntentSampleRef annotations for given model.

        This is a convenient shortcut that is equivalent to setting NLPCRAFT_TEST_MODELS system property (overriding any existing value) with given mode class name and calling isValid() method.

        Parameters:
        claxx - Data model class.
        Returns:
        True if no validation errors found, false otherwise. Note that standard validation output will be printed out to the configured logger (e.g. log4j), if any.
        Throws:
        Exception - Thrown in case of any unexpected errors during validation. Note that standard validation output will be printed out to the configured logger (e.g. log4j), if any.