Start of full flow unit tests. Can be used as a template for testing other profile...
[java-idp.git] / tests / edu / internet2 / middleware / shibboleth / idp / system / conf1 / SAML2AttributeQueryTestCase.java
1 /*
2  * Copyright [2007] [University Corporation for Advanced Internet Development, Inc.]
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 package edu.internet2.middleware.shibboleth.idp.system.conf1;
18
19 import org.joda.time.DateTime;
20 import org.opensaml.common.SAMLObjectBuilder;
21 import org.opensaml.common.SAMLVersion;
22 import org.opensaml.saml2.core.AttributeQuery;
23 import org.opensaml.saml2.core.Issuer;
24 import org.opensaml.saml2.core.NameID;
25 import org.opensaml.saml2.core.Subject;
26 import org.opensaml.ws.soap.common.SOAPObjectBuilder;
27 import org.opensaml.ws.soap.soap11.Body;
28 import org.opensaml.ws.soap.soap11.Envelope;
29 import org.opensaml.xml.io.Marshaller;
30 import org.opensaml.xml.io.MarshallingException;
31 import org.opensaml.xml.util.XMLHelper;
32 import org.springframework.mock.web.MockHttpServletRequest;
33 import org.springframework.mock.web.MockHttpServletResponse;
34 import org.w3c.dom.Element;
35
36 import edu.internet2.middleware.shibboleth.common.profile.ProfileHandler;
37 import edu.internet2.middleware.shibboleth.common.profile.ProfileRequest;
38 import edu.internet2.middleware.shibboleth.common.profile.ProfileResponse;
39 import edu.internet2.middleware.shibboleth.common.profile.provider.ShibbolethProfileHandlerManager;
40 import edu.internet2.middleware.shibboleth.idp.profile.ShibbolethProfileRequest;
41 import edu.internet2.middleware.shibboleth.idp.profile.ShibbolethProfileResponse;
42
43 /**
44  * A system test that meant to simulate various types of SAML 2 attribute queries.
45  */
46 public class SAML2AttributeQueryTestCase extends BaseConf1TestCase {
47
48     public void testAttributeQuery() throws Exception {
49         AttributeQuery query = buildAttributeQuery();
50         String soapMessage = getSOAPMessage(query);
51
52         MockHttpServletRequest servletRequest = new MockHttpServletRequest();
53         servletRequest.setPathInfo("/IdP/saml2/SOAP/AttributeQuery");
54         servletRequest.setContent(soapMessage.getBytes());
55
56         MockHttpServletResponse servletResponse = new MockHttpServletResponse();
57
58         ShibbolethProfileHandlerManager handlerManager = (ShibbolethProfileHandlerManager) getApplicationContext()
59                 .getBean("shibboleth.ProfileHandler");
60         ProfileHandler handler = handlerManager.getProfileHandler(servletRequest);
61         
62         return;
63
64 //        ProfileRequest profileRequest = new ShibbolethProfileRequest(servletRequest);
65 //        ProfileResponse profileResponse = new ShibbolethProfileResponse(servletResponse);
66 //        handler.processRequest(profileRequest, profileResponse);
67
68     }
69
70     /**
71      * Builds a basic attribute query.
72      * 
73      * @return basic attribute query
74      */
75     @SuppressWarnings("unchecked")
76     protected AttributeQuery buildAttributeQuery() {
77         SAMLObjectBuilder<Issuer> issuerBuilder = (SAMLObjectBuilder<Issuer>) builderFactory
78                 .getBuilder(Issuer.DEFAULT_ELEMENT_NAME);
79         Issuer issuer = issuerBuilder.buildObject();
80         issuer.setValue("urn:example.org:unitTestFed:mysp");
81
82         SAMLObjectBuilder<NameID> nameIdBuilder = (SAMLObjectBuilder<NameID>) builderFactory
83                 .getBuilder(NameID.DEFAULT_ELEMENT_NAME);
84         NameID nameId = nameIdBuilder.buildObject();
85         nameId.setValue("testUser");
86
87         SAMLObjectBuilder<Subject> subjectBuilder = (SAMLObjectBuilder<Subject>) builderFactory
88                 .getBuilder(Subject.DEFAULT_ELEMENT_NAME);
89         Subject subject = subjectBuilder.buildObject();
90         subject.setNameID(nameId);
91
92         SAMLObjectBuilder<AttributeQuery> attributeQueryBuilder = (SAMLObjectBuilder<AttributeQuery>) builderFactory
93                 .getBuilder(AttributeQuery.DEFAULT_ELEMENT_NAME);
94         AttributeQuery query = attributeQueryBuilder.buildObject();
95         query.setID("1");
96         query.setIssueInstant(new DateTime());
97         query.setIssuer(issuer);
98         query.setSubject(subject);
99         query.setVersion(SAMLVersion.VERSION_20);
100
101         return query;
102     }
103
104     /**
105      * Wraps an attribute query in a SOAP message, marshalls, and serializes it.
106      * 
107      * @param query the attribute query to wrap
108      * 
109      * @return the SOAP message
110      * 
111      * @throws MarshallingException thrown if the message can not be marshalled
112      */
113     @SuppressWarnings("unchecked")
114     protected String getSOAPMessage(AttributeQuery query) throws MarshallingException {
115         SOAPObjectBuilder<Body> bodyBuilder = (SOAPObjectBuilder<Body>) builderFactory
116                 .getBuilder(Body.DEFAULT_ELEMENT_NAME);
117         Body body = bodyBuilder.buildObject();
118         body.getUnknownXMLObjects().add(query);
119
120         SOAPObjectBuilder<Envelope> envelopeBuilder = (SOAPObjectBuilder<Envelope>) builderFactory
121                 .getBuilder(Envelope.DEFAULT_ELEMENT_NAME);
122         Envelope envelope = envelopeBuilder.buildObject();
123         envelope.setBody(body);
124
125         Marshaller marshaller = marshallerFactory.getMarshaller(envelope);
126         Element envelopeElem = marshaller.marshall(envelope);
127
128         return XMLHelper.nodeToString(envelopeElem);
129     }
130 }