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: 68
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: 323
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: 68
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: 78

Currently Online: japedGAL
16 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

hbrackel: 87

pramanj: 86

ibrahim: 68

kapsl: 57

gjevremovic: 49

TimK: 41

Fransua33: 39

fred: 36

Rainer Versteeg: 32

peterrob45: 25

Member Stats:

Guest Posters: 0

Members: 795

Moderators: 11

Admins: 1

Forum Stats:

Groups: 3

Forums: 14

Topics: 829

Posts: 3519

Newest Members:

marco_pth, girishm39, hallieoaks66, Jimmy Ni, nisha.kale1122, kcdico, automsoft, wyattetal, SerkanSahin, sophiarobin

Moderators: Jouni Aro: 823, Otso Palonen: 32, Tuomas Hiltunen: 5, janimakela: 0, Pyry: 1, Terho: 0, Petri: 0, Bjarne Boström: 323, Heikki Tahvanainen: 402, Jukka Asikainen: 1, Teppo Uimonen: 10

Administrators: admin: 0