Unit testing event conversion

Questions regarding the use of the .NET SDK 2.0 for Server or Client development or integration into customer products ...

Moderator: uasdknet

Post Reply
kristian.mo
Full Member
Full Member
Posts: 5
Joined: 15 Feb 2021, 14:12

Unit testing event conversion

Post 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;
}

User avatar
Support Team
Hero Member
Hero Member
Posts: 3064
Joined: 18 Mar 2011, 15:09

Re: Unit testing event conversion

Post 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.
Best regards
Unified Automation Support Team

Post Reply