Avatar

Please consider registering
guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —




— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed Topic RSS sp_TopicIcon
Create dynamic structure for testing
January 21, 2019
17:48, EET
Avatar
Ibrahim
Member
Members
Forum Posts: 78
Member Since:
August 20, 2014
sp_UserOfflineSmall Offline

Hi,
we want to use the new features of ExtensionObject’s in our OPC UA Client product. Now i want to write some unit tests for my code. Is it possible to create a valid dynamic structure for testing?
Could you maybe provide some example code how to create a valid DynamicStructure.

Thanks

Ibrahim

January 22, 2019
14:11, EET
Avatar
Bjarne Boström
Moderator
Moderators
Forum Posts: 1032
Member Since:
April 3, 2012
sp_UserOfflineSmall Offline

Hi,

It is primarily intended as return value from TypeDictionary, i.e. constructing it is somewhat SDK internals. However it should be doable manually as well, but the current constructors are quite ugly to use, depending on what kind of fields your Structure does have (e.g. are they known/standard types?) and how exactly your code interacts with it?

Do you need the encoded binary format or is interacting with the DynamicStructure object enough?
Do you need a real DynamicStructure, or could you just mock it?

If a mock is enough, you could use e.g. https://site.mockito.org/, and for example

//typically these are imported as static in tests
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
//note that I only added static imports for the forum in this example, it will have normal imports as well

@Test
public void example() throws Exception {
DynamicStructure ds = mock(DynamicStructure.class);

// You need to mock all return values you need, see mockito manuals for more info
when(ds.get("AA")).thenReturn("TESTVALUE");

// Simulating your code interacting with the DynamicStructure instance
assertEquals("TESTVALUE", ds.get("AA")); //your code would get the "TESTVALUE" if it asked ds.get("AA")
ds.set("Name", "Value"); // e.g. your code could call this
verify(ds).set("Name", "Value"); // verifies that your code did call this
}

P.S.
Do note that multidimensional fields are not possible (as they were added in UA 1.04, which the current release version does not yet support).

January 22, 2019
17:11, EET
Avatar
Ibrahim
Member
Members
Forum Posts: 78
Member Since:
August 20, 2014
sp_UserOfflineSmall Offline

Hi Bjarne,
thanks for the example. Yes i only need to handle the DynamicStructure. Our code converts ExtensionObjects into JSON in a generic way and vise versa. So i need to create a DynamicStructure and pass it to the converter Method and look at the json that is created. Your approach looks nice i will see how far i will come with that..
Thanks.

Ibrahim

Forum Timezone: Europe/Helsinki

Most Users Ever Online: 1919

Currently Online:
18 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

Heikki Tahvanainen: 402

hbrackel: 144

rocket science: 88

pramanj: 86

Francesco Zambon: 83

Ibrahim: 78

Sabari: 62

kapsl: 57

gjevremovic: 49

Xavier: 43

Member Stats:

Guest Posters: 0

Members: 730

Moderators: 7

Admins: 1

Forum Stats:

Groups: 3

Forums: 15

Topics: 1529

Posts: 6471

Newest Members:

rondawolinski7, Marypof5711, roycedelargie91, kourtneyquisenbe, ellis87832073466, zkxwilliemae, gabriellabachus, Deakin, KTP25Zof, Wojciech Kubala

Moderators: Jouni Aro: 1026, Pyry: 1, Petri: 0, Bjarne Boström: 1032, Jimmy Ni: 26, Matti Siponen: 349, Lusetti: 0

Administrators: admin: 1