Page 1 of 1

Unit testing event conversion

Posted: 18 Nov 2022, 09:32
by kristian.mo
I have used the GenericEvent to map between the event storage and and OPC UA events, like the example. This is a business logic heavy part of the application so it should be automatically tested. The conversion works fine in a running OPC UA server with manual testing.

But since the GenericEvent needs a FilterManager, which needs a ServerManager, I have not been able to fake this in a unit test. The Set function is called without errors but the returned fields list in GenericEvent is always empty.

Can I create a ServerManager and FilterManager that works in a unit test context? If so, how? Thank you.

Test sample

Code: Select all

        [Test]
        public void Convert_event_with_should_return_generic_event() {
            var items = new RepeatedField<Event> {
                new Event {
                    ObjectName = "tag1",
                    Time = Timestamp.FromDateTime(DateTime.UtcNow)
                }
            };
            
            var result = EventConverter.Convert(items, new FilterManager(new ServerManager()));
            
           Assert.AreEqual(1, result. Count);
        }
Conversion sample

Code: Select all

 public static List<GenericEvent> Convert(RepeatedField<Event> events, FilterManager filterManager) {
            var convertedEvents = new List<GenericEvent>();
            foreach (var backendEvent in events) {
                  var @event = new GenericEvent(filterManager);
                  @event.Set(AbsoluteName.ToString("Time"), backendEvent.Time.ToDateTime().ToUniversalTime());
                  ...
                  convertedEvents.add(@event);
            }
            
            return convertedEvents;
}

Re: Unit testing event conversion

Posted: 18 Nov 2022, 17:55
by Support Team
Hi,

the FilterManager is actually only using the NamespaceUris of the ServerManager. Unfortunately this property is null until the ServerManager is actually started. So it's not enough to create the server manager, you also have to start it.

You have a point here, we are considering to add a constructor to the FilterManager that takes the NamespaceUris or the MessageContext as an argument.