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: 75
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: 982
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: 75
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: 518

Currently Online:
12 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

hbrackel: 135

pramanj: 86

Francesco Zambon: 81

rocket science: 75

ibrahim: 75

Sabari: 62

kapsl: 57

gjevremovic: 49

Xavier: 43

fred: 41

Member Stats:

Guest Posters: 0

Members: 707

Moderators: 16

Admins: 1

Forum Stats:

Groups: 3

Forums: 15

Topics: 1466

Posts: 6253

Newest Members:

christi10l, ahamad1, Flores Frederick, ellenmoss, harriettscherer, shanonhumphreys, KupimotoblokfuB, tamhollander5, paulinafcf, bridgette18l

Moderators: Jouni Aro: 1009, Otso Palonen: 32, Tuomas Hiltunen: 5, Pyry: 1, Petri: 0, Bjarne Boström: 982, Heikki Tahvanainen: 402, Jukka Asikainen: 1, moldzh08: 0, Jimmy Ni: 26, Teppo Uimonen: 21, Markus Johansson: 42, Niklas Nurminen: 0, Matti Siponen: 319, Lusetti: 0, Ari-Pekka Soikkeli: 5

Administrators: admin: 1