1047d83154f47605a1198b56b01f844250c2d869
[shibboleth/cpp-opensaml.git] / samltest / saml1 / core / impl / AttributeTest.h
1 /*
2  *  Copyright 2001-2006 Internet2
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "internal.h"
18 #include <saml/saml1/core/Assertions.h>
19
20 using namespace opensaml::saml1;
21
22 class AttributeTest : public CxxTest::TestSuite, public SAMLObjectBaseTestCase {
23     XMLCh* expectedAttributeName;
24     XMLCh* expectedAttributeNamespace;
25
26 public:
27     void setUp() {
28         expectedAttributeName=XMLString::transcode("AttributeName");
29         expectedAttributeNamespace=XMLString::transcode("namespace");
30         singleElementFile = data_path + "saml1/core/impl/singleAttribute.xml";
31         singleElementOptionalAttributesFile = data_path + "saml1/core/impl/singleAttributeAttributes.xml";
32         childElementsFile = data_path + "saml1/core/impl/AttributeWithChildren.xml";
33         SAMLObjectBaseTestCase::setUp();
34     }
35     
36     void tearDown() {
37         XMLString::release(&expectedAttributeName);
38         XMLString::release(&expectedAttributeNamespace);
39         SAMLObjectBaseTestCase::tearDown();
40     }
41
42     void testSingleElementUnmarshall() {
43         auto_ptr<XMLObject> xo(unmarshallElement(singleElementFile));
44         Attribute& a = dynamic_cast<Attribute&>(*xo.get());
45         TSM_ASSERT("AttributeName", a.getAttributeName()==NULL);
46         TSM_ASSERT("AttributeNamespace", a.getAttributeNamespace()==NULL);
47         TSM_ASSERT_EQUALS("<AttributeValue> subelement found", 0, a.getAttributeValues().size());
48     }
49
50     void testSingleElementOptionalAttributesUnmarshall() {
51         auto_ptr<XMLObject> xo(unmarshallElement(singleElementOptionalAttributesFile));
52         Attribute& a = dynamic_cast<Attribute&>(*xo.get());
53         assertEquals("AttributeName", expectedAttributeName, a.getAttributeName());
54         assertEquals("AttributeNamespace", expectedAttributeNamespace, a.getAttributeNamespace());
55     }
56
57     void testChildElementsUnmarshall() {
58         auto_ptr<XMLObject> xo(unmarshallElement(childElementsFile));
59         Attribute& a = dynamic_cast<Attribute&>(*xo.get());
60         TSM_ASSERT_EQUALS("Number of <AttributeValue> subelements", 4, a.getAttributeValues().size());
61     }
62
63     void testSingleElementMarshall() {
64         assertEquals(expectedDOM, AttributeBuilder::buildAttribute());
65     }
66
67     void testSingleElementOptionalAttributesMarshall() {
68         Attribute* a=AttributeBuilder::buildAttribute();
69         a->setAttributeName(expectedAttributeName);
70         a->setAttributeNamespace(expectedAttributeNamespace);
71         assertEquals(expectedOptionalAttributesDOM, a);
72     }
73
74     void testChildElementsMarshall(){
75         Attribute* a=AttributeBuilder::buildAttribute();
76         
77         const XMLCh xsdstring[] = UNICODE_LITERAL_6(s,t,r,i,n,g);
78        
79         const XMLObjectBuilder* builder=XMLObjectBuilder::getBuilder(QName(SAMLConstants::SAML1_NS,AttributeValue::LOCAL_NAME));
80         TS_ASSERT(builder!=NULL);
81         QName xsitype(XMLConstants::XSD_NS,xsdstring,XMLConstants::XSD_PREFIX);
82         for (int i=0; i<4; i++)
83             a->getAttributeValues().push_back(builder->buildObject(SAMLConstants::SAML1_NS, AttributeValue::LOCAL_NAME, SAMLConstants::SAML1_PREFIX, &xsitype)); 
84
85         assertEquals(expectedChildElementsDOM, a);
86     }
87 };