Beispiel #1
0
        bool messageFilterBase(Proxies.Event.NotificationMessageHolderType message,
                               TopicInfo topicInfo,
                               string expectedPropertyOperation,
                               string validationScript,
                               IEnumerable <AccessProfile> accessProfileList)
        {
            if (!string.IsNullOrEmpty(expectedPropertyOperation))
            {
                if (!message.Message.HasAttribute(OnvifMessage.PROPERTYOPERATIONTYPE))
                {
                    return(true);
                }

                XmlAttribute propertyOperationType = message.Message.Attributes[OnvifMessage.PROPERTYOPERATIONTYPE];
                if (expectedPropertyOperation != propertyOperationType.Value)
                {
                    return(false);
                }
            }

            var variables = new Dictionary <string, string>();

            variables.Add("accessProfileList", string.Format(@"{{{0}}}", string.Join(",", accessProfileList.Select(s => string.Format(@"""{0}"" : ""{1}""", s.token, s.Description)))));


            var    logger = new StringBuilder();
            string validationScriptPath = string.Format("TestTool.Tests.TestCases.TestSuites.AccessRules.Script.{0}", validationScript);

            Assert(ValidationEngine.GetInstance().Validate(message,
                                                           topicInfo,
                                                           validationScriptPath,
                                                           logger,
                                                           variables),
                   logger.ToStringTrimNewLine(),
                   "Validate received notification(s)");

            return(true);
        }
Beispiel #2
0
        public bool Validate(Proxies.Event.NotificationMessageHolderType msg,
                             TopicInfo expectedTopicInfo,
                             string validationScriptPath, StringBuilder logger, Dictionary <string, string> variables)
        {
            //System.Windows.Forms.MessageBox.Show(System.IO.Directory.GetCurrentDirectory());
            try
            {
                if (!validateEventTopic(expectedTopicInfo, msg, logger))
                {
                    var actualTopicInfo = TopicInfo.ExtractTopicInfoAll(msg.Topic.Any.First().InnerText, msg.Topic.Any.First());
                    logger.AppendLine(string.Format("Invalid notification: event with topic {0} is unexpected", actualTopicInfo.GetDescription()));

                    return(false);
                }

                var dst = new StringBuilder();
                var s   = new XmlSerializer(typeof(NotificationMessageHolderType));
                s.Serialize(new StringWriter(dst), msg);


                //get the xml manager
                XmlDataManager lManager = Zorba.getXmlDataManager();
                //create an empty Item to store the iterator of the parsed document
                Item lDoc = Item.createEmptyItem();
                //parse the xml document into the Iterator
                var xml = dst.ToString().Replace("utf-16", "utf-8");
                //LogStepEvent(longName.Count().ToString());
                //LogStepEvent(xml);
                Iterator lDocIter = lManager.parseXML(xml);
                lDocIter.open();
                //get the root node of the document
                lDocIter.next(lDoc);
                //close the Iterator
                lDocIter.close();
                ////Even though C# is a garbage collected language the Iterator must be destroyed manually
                lDocIter.destroy();

                StaticContext stContext = Zorba.createStaticContext();
                stContext.addNamespace("wsnt", "http://docs.oasis-open.org/wsn/b-2");
                stContext.addNamespace("xs", "http://www.w3.org/2001/XMLSchema");

                foreach (var validationScript in resourceLines(validationScriptPath))
                {
                    var query = string.Format("import schema namespace tt = '{0}' at 'file://localhost/{1}';", OnvifMessage.ONVIF, OnvifSchemaPath);
                    query += Environment.NewLine + validationScript;

                    //XQuery compiledQuery = Zorba.createQuery();
                    //compiledQuery.compile(query, stContext);
                    XQuery compiledQuery = Zorba.compileQuery(query, stContext);
                    //get the Dynamic Context of a Query
                    DynamicContext dynamicContext = compiledQuery.getDynamicContext();
                    //set the Context Item of the Query
                    dynamicContext.setContextItem(lDoc);

                    if (null != variables)
                    {
                        foreach (var variable in variables)
                        {
                            dynamicContext.setVariable(variable.Key, Zorba.getItemFactory().createString(variable.Value));
                        }
                    }

                    var options = new SerializationOptions();
                    options.setOmitXMLDeclaration(SerializationOptions.OmitXMLDeclaration.ZORBA_API_OMIT_XML_DECLARATION_YES);
                    var q = compiledQuery.execute(options);

                    string queryResult, log;
                    var    flag = parseAnswer(q, out queryResult, out log);
                    if (flag && "false" == queryResult || !flag)
                    {
                        logger.AppendLine(log);
                        return(false);
                    }
                    compiledQuery.destroy();
                }

                return(true);
            }
            catch (Exception e)
            {
                logger.AppendLine(e.ToString());
                return(false);
            }
            catch
            {
                logger.AppendLine("Unknown exception!");
                return(false);
            }
        }
Beispiel #3
0
 public bool Validate(Proxies.Event.NotificationMessageHolderType msg, TopicInfo expectedTopicInfo, string validationScriptPath, StringBuilder logger)
 {
     return(Validate(msg, expectedTopicInfo, validationScriptPath, logger, null));
 }
Beispiel #4
0
        bool messageFilterBase(Proxies.Event.NotificationMessageHolderType msg, TopicInfo topicInfo, string expectedPropertyOperation, string expectedAccessProfileToken, Func <StringBuilder, bool> action)
        {
            var invalidFlag = false;
            var log         = new StringBuilder();

            try
            {
                if (!EventServiceExtensions.NotificationTopicMatch(msg, topicInfo))
                {
                    log.AppendLine(string.Format("Received notification has unexpected topic '{0}' while the expected one should have with topic '{1}'",
                                                 EventServiceExtensions.GetTopicInfo(msg).GetDescription(), topicInfo.GetDescription()));
                    invalidFlag = true;
                }

                if (null != expectedPropertyOperation)
                {
                    if (!msg.Message.HasAttribute(OnvifMessage.PROPERTYOPERATIONTYPE))
                    {
                        log.AppendLine("Received notification has no 'PropertyOperation' field");
                        invalidFlag = true;
                    }
                    else
                    {
                        XmlAttribute propertyOperationType = msg.Message.Attributes[OnvifMessage.PROPERTYOPERATIONTYPE];
                        //Skip non-changed messages
                        if (propertyOperationType.Value != expectedPropertyOperation)
                        {
                            log.AppendLine(string.Format("Received notification has 'PropertyOperation' field with value = '{0}' but value = '{1}' is expected", propertyOperationType.Value, expectedPropertyOperation));
                            invalidFlag = true;
                        }
                    }
                }

                var source = msg.Message.GetMessageSourceSimpleItems();
                if (!source.ContainsKey("AccessProfileToken"))
                {
                    log.AppendLine("Received notification has no Source/SimpleItem with Name = 'AccessProfileToken'");
                    invalidFlag = true;
                }
                else
                {
                    var token = source["AccessProfileToken"];
                    if (token != expectedAccessProfileToken)
                    {
                        log.AppendLine(string.Format("Received notification has Source/SimpleItem with Name = 'AccessProfileToken' and Value = '{0}' but notification for AccessProfile item with token = '{1}' is expected", token, expectedAccessProfileToken));
                        invalidFlag = true;
                    }
                }

                if (null != action && action(log))
                {
                    invalidFlag = true;
                }

                return(true);
            }
            finally
            {
                Assert(!invalidFlag, log.ToStringTrimNewLine(), "Validation of received notification");
            }
        }