HP-RK Bugzilla 30 (Reference keys of reference type cannot be parsed).
[tpot/pegasus/.git] / src / Pegasus / Common / tests / InstanceDecl / InstanceDecl.cpp
1 //%/////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Company, IBM,
4 // The Open Group, Tivoli Systems
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to
8 // deal in the Software without restriction, including without limitation the
9 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 // sell copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 // 
13 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
14 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
15 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
16 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 //
22 //==============================================================================
23
24 //
25 // Author: Mike Brasher (mbrasher@bmc.com)
26 //
27 // Modified By: Sushma Fernandes (sushma_fernandes@hp.com)
28 //              Carol Ann Krug Graves, Hewlett-Packard Company
29 //                (carolann_graves@hp.com)
30 //              Karl Schopmeyer
31 //
32 //%/////////////////////////////////////////////////////////////////////////////
33 /* This program tests the generation and resolution of instances.  It creates
34         a set of qualifiers and a class and then creates instances of the class
35         and confirms both the creation characteristics and the resolution
36         characteristics.
37 */
38
39 #include <cassert>
40 #include <Pegasus/Common/CIMInstance.h>
41 #include <Pegasus/Common/CIMClass.h>
42 #include <Pegasus/Common/CIMName.h>
43 #include <Pegasus/Common/DeclContext.h>
44 #include <Pegasus/Common/Resolver.h>
45 #include <Pegasus/Common/XmlWriter.h>
46
47 PEGASUS_USING_PEGASUS;
48 PEGASUS_USING_STD;
49
50 static char * verbose;
51
52 void test01()
53 {
54     const CIMNamespaceName NAMESPACE = CIMNamespaceName ("/zzz");
55
56     // Create and populate a declaration context:
57
58     SimpleDeclContext* context = new SimpleDeclContext;
59
60     context->addQualifierDecl(
61         NAMESPACE, CIMQualifierDecl(CIMName ("counter"), false, 
62         CIMScope::PROPERTY));
63
64     context->addQualifierDecl(
65         NAMESPACE, CIMQualifierDecl(CIMName ("classcounter"), false, 
66         CIMScope::CLASS));
67
68
69     context->addQualifierDecl(
70         NAMESPACE, CIMQualifierDecl(CIMName ("min"), String(), 
71         CIMScope::PROPERTY));
72
73     context->addQualifierDecl(
74         NAMESPACE, CIMQualifierDecl(CIMName ("max"), String(), 
75         CIMScope::PROPERTY));
76
77     context->addQualifierDecl(NAMESPACE,
78         CIMQualifierDecl(CIMName ("Description"), String(), 
79         CIMScope::PROPERTY));
80
81     CIMClass class1(CIMName ("MyClass"));
82
83     class1
84         .addProperty(CIMProperty(CIMName ("count"), Uint32(55))
85             .addQualifier(CIMQualifier(CIMName ("counter"), true))
86             .addQualifier(CIMQualifier(CIMName ("min"), String("0")))
87             .addQualifier(CIMQualifier(CIMName ("max"), String("1"))))
88         .addProperty(CIMProperty(CIMName ("message"), String("Hello"))
89             .addQualifier(CIMQualifier(CIMName ("description"), 
90                 String("My Message"))))
91         .addProperty(CIMProperty(CIMName ("ratio"), Real32(1.5)));
92
93
94     // Test
95     assert(class1.findProperty(CIMName ("count")) != PEG_NOT_FOUND);
96     assert(class1.findProperty(CIMName ("message")) != PEG_NOT_FOUND);
97     assert(class1.findProperty(CIMName ("ratio")) != PEG_NOT_FOUND);
98
99     Resolver::resolveClass (class1, context, NAMESPACE);
100     context->addClass(NAMESPACE, class1);
101
102         if(verbose) {
103                 XmlWriter::printClassElement(class1);
104         }
105
106     CIMInstance instance0(CIMName ("MyClass"));
107     assert(instance0.getClassName().equal(CIMName ("MyClass")));
108     instance0.setPath(CIMObjectPath("//localhost/root/cimv2:MyClass.Foo=1"));
109     assert(instance0.getPath() == CIMObjectPath("//localhost/root/cimv2:MyClass.Foo=1"));
110
111     assert(instance0.getPath() == CIMObjectPath("//localhost/root/cimv2:MyClass.Foo=1"));
112
113     CIMInstance instance1(CIMName ("MyClass"));
114     instance1.addQualifier(CIMQualifier(CIMName ("classcounter"), true));
115
116     instance1.addProperty(CIMProperty(CIMName ("message"), String("Goodbye")));
117
118     assert(instance1.findProperty(CIMName ("message")) != PEG_NOT_FOUND);
119
120     assert(instance1.findProperty(CIMName ("count")) == PEG_NOT_FOUND);
121     assert(instance1.findProperty(CIMName ("ratio")) == PEG_NOT_FOUND);
122     assert(instance1.findProperty(CIMName ("nuts")) == PEG_NOT_FOUND);
123     assert(instance1.getPropertyCount() == 1);
124
125         if(verbose)
126                 XmlWriter::printInstanceElement(instance1);
127
128     Resolver::resolveInstance (instance1, context, NAMESPACE, true);
129
130         if(verbose)
131                 XmlWriter::printInstanceElement(instance1);
132
133     // Now test for properties after resolution.
134
135     assert(instance1.findProperty(CIMName ("message")) != PEG_NOT_FOUND);
136     assert(instance1.findProperty(CIMName ("count")) != PEG_NOT_FOUND);
137     assert(instance1.findProperty(CIMName ("ratio")) != PEG_NOT_FOUND);
138     assert(instance1.findProperty(CIMName ("nuts")) == PEG_NOT_FOUND);
139
140     assert(instance1.getPropertyCount() == 3);
141     // Now remove a property
142
143     Uint32 posProperty;
144     posProperty = instance1.findProperty(CIMName ("count"));
145     instance1.removeProperty(posProperty);
146
147     assert(instance1.findProperty(CIMName ("message")) != PEG_NOT_FOUND);
148     assert(instance1.findProperty(CIMName ("count")) == PEG_NOT_FOUND);
149     assert(instance1.findProperty(CIMName ("ratio")) != PEG_NOT_FOUND);
150     assert(instance1.findProperty(CIMName ("nuts")) == PEG_NOT_FOUND);
151
152     assert(instance1.getPropertyCount() == 2);
153  
154     // SF-HP
155
156     CIMQualifier cq=instance1.getQualifier(instance1.findQualifier
157         (CIMName ("classcounter")));
158
159     const CIMInstance instance2 = instance1.clone();
160     assert(instance2.identical(instance1));
161     assert(instance1.findQualifier(CIMName ("nuts")) == PEG_NOT_FOUND);
162     assert(instance2.findQualifier(CIMName ("nuts")) == PEG_NOT_FOUND);
163     assert(instance1.getQualifierCount() != 4);
164
165         // Confirm that the classcounter qualifier is in instance 2
166         // NOTE: This is dangerous coding and generates an exception
167         // out of bounds error if the qualifier does not exist.
168     CIMConstQualifier ccq=
169         instance2.getQualifier(instance2.findQualifier
170         (CIMName ("classcounter")));
171
172         if(verbose)
173                 XmlWriter::printInstanceElement(instance2);
174
175     // Tests for CIMConstInstance 
176     CIMConstInstance cinstance1(CIMName ("MyClass")), cinstance3;
177     CIMConstInstance ccopy(cinstance1);
178
179     cinstance1 = instance1;
180     assert(cinstance1.identical(instance1));
181
182     ccq = cinstance1.getQualifier(cinstance1.findQualifier(CIMName ("classcounter")));
183     assert(cinstance1.findProperty(CIMName ("message")) != PEG_NOT_FOUND);
184     CIMConstProperty ccp = 
185            cinstance1.getProperty(cinstance1.findProperty(CIMName ("message")));
186
187     cinstance3 = cinstance1;
188     assert(cinstance3.identical(cinstance1));
189
190     assert(cinstance1.getClassName() == CIMName ("MyClass"));
191     assert(cinstance1.getClassName().equal(CIMName ("MyClass")));
192     assert(cinstance1.getClassName().equal(CIMName ("MYCLASS")));
193     assert(cinstance1.getClassName().equal(CIMName ("myclass")));
194     assert(!cinstance1.getClassName().equal(CIMName ("blob")));
195
196
197
198     assert(cinstance1.getQualifierCount() != 4);
199     assert(cinstance1.getPropertyCount() == 2);
200    
201     CIMConstInstance cinstance2 = cinstance1.clone();
202     assert(cinstance2.identical(cinstance1));
203
204
205         if(verbose)
206                 XmlWriter::printInstanceElement(cinstance1);
207   
208     cinstance1.buildPath(class1);
209  
210     assert( !cinstance1.isUninitialized() );
211
212     delete context;
213 }
214
215 void test02()
216 {
217     const CIMNamespaceName NAMESPACE = CIMNamespaceName ("/zzz");
218
219     CIMClass cimClass(CIMName ("MyClass"));
220
221     cimClass
222         .addProperty(CIMProperty(CIMName ("Last"), String())
223             .addQualifier(CIMQualifier(CIMName ("key"), true)))
224         .addProperty(CIMProperty(CIMName ("First"), String())
225             .addQualifier(CIMQualifier(CIMName ("key"), true)))
226         .addProperty(CIMProperty(CIMName ("Age"), String())
227             .addQualifier(CIMQualifier(CIMName ("key"), true)));
228
229     CIMInstance cimInstance(CIMName ("MyClass"));
230     cimInstance.addProperty(CIMProperty(CIMName ("first"), String("John")));
231     cimInstance.addProperty(CIMProperty(CIMName ("last"), String("Smith")));
232     cimInstance.addProperty(CIMProperty(CIMName ("age"), Uint8(101)));
233
234
235     assert(cimInstance.findProperty(CIMName ("first")) != PEG_NOT_FOUND);
236     assert(cimInstance.findProperty(CIMName ("last")) != PEG_NOT_FOUND);
237     assert(cimInstance.findProperty(CIMName ("age")) != PEG_NOT_FOUND);
238
239     assert(cimInstance.getPropertyCount() == 3);
240
241
242     // ATTN: Should we be doing an instance qualifier add and test
243
244
245     CIMObjectPath instanceName
246         = cimInstance.buildPath(CIMConstClass(cimClass));
247
248     CIMObjectPath tmp("myclass.age=101,first=\"John\",last=\"Smith\"");
249
250     assert(tmp.makeHashCode() == instanceName.makeHashCode());
251 }
252
253 // Build the instance of an association class and test the build path functions.
254 void test03()
255 {
256     const CIMNamespaceName NAMESPACE = CIMNamespaceName ("/zzz");
257
258     CIMClass myPersonClass(CIMName ("MY_PersonClass"));
259
260         myPersonClass
261         .addProperty(CIMProperty(CIMName ("name"), String())
262                 .addQualifier(CIMQualifier(CIMName ("Key"), true)));
263
264     CIMClass myAssocClass(CIMName ("MY_AssocClass"));
265
266     myAssocClass
267     .addQualifier(CIMQualifier(CIMName ("association"), true))
268         .addProperty(CIMProperty(CIMName ("parent"), CIMObjectPath(),0, CIMName ("MY_PersonClass"))
269             .addQualifier(CIMQualifier(CIMName ("key"), true)))
270         .addProperty(CIMProperty(CIMName ("child"), CIMObjectPath(), 0, CIMName ("MY_PersonClass"))
271             .addQualifier(CIMQualifier(CIMName ("key"), true)))
272         .addProperty(CIMProperty(CIMName ("Age"), String()));
273
274     CIMInstance fatherInstance(CIMName ("MY_PersonClass"));
275
276     fatherInstance
277         .addProperty(CIMProperty(CIMName ("name"), String("father")));
278
279     CIMInstance daughterInstance(CIMName ("My_PersonClass"));
280     daughterInstance
281        .addProperty(CIMProperty(CIMName ("name"), String("daughter")));
282      
283     CIMObjectPath fatherInstancePath = fatherInstance.buildPath(CIMConstClass(myPersonClass));
284
285     CIMObjectPath daughterInstancePath = daughterInstance.buildPath(CIMConstClass(myPersonClass));
286
287     CIMInstance assocInstance(CIMName ("MY_AssocClass"));
288
289     assocInstance.addProperty(CIMProperty(CIMName ("parent"),
290         CIMObjectPath(fatherInstancePath),0,CIMName("MY_PersonClass")));
291
292     assocInstance.addProperty(CIMProperty(CIMName ("child"),
293         CIMObjectPath(daughterInstancePath),0, CIMName("MY_PersonClass")));
294
295     CIMObjectPath assocInstancePath = assocInstance.buildPath(CIMConstClass(myAssocClass));
296
297     // Now do the asserts, etc.   See if the pathing works on Associations and association instances
298         
299     if(verbose)
300     {
301                 XmlWriter::printClassElement(myPersonClass);
302                 XmlWriter::printClassElement(myAssocClass);
303         XmlWriter::printInstanceElement(fatherInstance);
304                 XmlWriter::printInstanceElement(daughterInstance);
305                 XmlWriter::printInstanceElement(assocInstance);
306     }
307     if (verbose)
308     {
309         cout << "Paths " << endl;
310         cout << "FatherInstancePath = " << fatherInstancePath.toString()<< endl;
311         cout << "DaughterInstancePath = " << daughterInstancePath.toString()<< endl;
312         cout << "AssocInstancePath = " << assocInstancePath.toString()<< endl;
313     }
314     String x ="MY_AssocClass.child=\"My_PersonClass.name=\\\"daughter\\\"\",parent=\"MY_PersonClass.name=\\\"father\\\"\"";
315     assert(x == assocInstancePath.toString());
316     CIMObjectPath px = x;
317     assert(px.identical(assocInstancePath));
318
319 }
320
321 int main(int argc, char** argv)
322 {
323     verbose = getenv("PEGASUS_TEST_VERBOSE");
324     try
325     {
326         test01();
327         test02();
328     test03();
329     }
330     catch (Exception& e)
331     {
332         cout << "Exception: " << e.getMessage() << endl;
333         exit(1);
334     }
335
336     cout << argv[0] << " +++++ passed all tests" << endl;
337
338     return 0;
339 }