001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.jaxws.security;
020:
021: import javax.xml.namespace.QName;
022: import javax.xml.ws.Dispatch;
023: import javax.xml.ws.Service;
024: import javax.xml.ws.WebServiceException;
025: import javax.xml.ws.soap.SOAPBinding;
026:
027: import junit.framework.TestCase;
028: import org.apache.axis2.jaxws.BindingProvider;
029: import org.apache.axis2.jaxws.TestLogger;
030:
031: public class BasicAuthSecurityTests extends TestCase {
032:
033: private String endpointUrl = "http://localhost:8080/axis2/services/BasicAuthSecurityService";
034: private String xmlString = "<invokeOp>test input</invokeOp>";
035: private QName SERVICE_QNAME = new QName(
036: "http://ws.apache.org/axis2", "BasicAuthSecurityService");
037: private QName PORT_QNAME = new QName("http://ws.apache.org/axis2",
038: "SimpleProviderServiceSOAP11port0");
039:
040: private String USER_ID = "testid";
041: private String PASSWORD = "testid";
042:
043: protected void setUp() throws Exception {
044: super .setUp();
045: }
046:
047: protected void tearDown() throws Exception {
048: super .tearDown();
049: }
050:
051: public BasicAuthSecurityTests(String name) {
052: super (name);
053: }
054:
055: public void testBasicAuth() throws Exception {
056: TestLogger.logger
057: .debug("---------------------------------------");
058: TestLogger.logger.debug("test: " + getName());
059:
060: Dispatch<String> dispatch = getDispatch(Service.Mode.PAYLOAD,
061: endpointUrl, SOAPBinding.SOAP11HTTP_BINDING);
062:
063: TestLogger.logger
064: .debug(">> Invoking Dispatch<String> BasicAuthSecurityService");
065: String retVal = dispatch.invoke(xmlString);
066: TestLogger.logger.debug(">> Response [" + retVal + "]");
067: }
068:
069: public void testBasicAuth_uid_pwd() throws Exception {
070: TestLogger.logger
071: .debug("---------------------------------------");
072: TestLogger.logger.debug("test: " + getName());
073:
074: Dispatch<String> dispatch = getDispatch(Service.Mode.PAYLOAD,
075: endpointUrl, SOAPBinding.SOAP11HTTP_BINDING);
076:
077: dispatch.getRequestContext().put(
078: BindingProvider.USERNAME_PROPERTY, USER_ID);
079: dispatch.getRequestContext().put(
080: BindingProvider.PASSWORD_PROPERTY, PASSWORD);
081:
082: TestLogger.logger
083: .debug(">> Invoking Dispatch<String> BasicAuthSecurityService");
084: String retVal = dispatch.invoke(xmlString);
085: TestLogger.logger.debug(">> Response [" + retVal + "]");
086: }
087:
088: public void testBasicAuth_uid() throws Exception {
089: TestLogger.logger
090: .debug("---------------------------------------");
091: TestLogger.logger.debug("test: " + getName());
092:
093: Dispatch<String> dispatch = getDispatch(Service.Mode.PAYLOAD,
094: endpointUrl, SOAPBinding.SOAP11HTTP_BINDING);
095:
096: dispatch.getRequestContext().put(
097: BindingProvider.USERNAME_PROPERTY, USER_ID);
098:
099: TestLogger.logger
100: .debug(">> Invoking Dispatch<String> BasicAuthSecurityService");
101:
102: try {
103: String retVal = dispatch.invoke(xmlString);
104: TestLogger.logger.debug(">> Response [" + retVal + "]");
105:
106: fail("Set USERID with no PASSWORD: WebServiceException is expected");
107: } catch (WebServiceException wse) {
108: TestLogger.logger.debug(getName() + ": " + wse);
109: }
110: }
111:
112: public void testBasicAuth_pwd() throws Exception {
113: TestLogger.logger
114: .debug("---------------------------------------");
115: TestLogger.logger.debug("test: " + getName());
116:
117: Dispatch<String> dispatch = getDispatch(Service.Mode.PAYLOAD,
118: endpointUrl, SOAPBinding.SOAP11HTTP_BINDING);
119:
120: dispatch.getRequestContext().put(
121: BindingProvider.PASSWORD_PROPERTY, PASSWORD);
122:
123: TestLogger.logger
124: .debug(">> Invoking Dispatch<String> BasicAuthSecurityService");
125:
126: try {
127: String retVal = dispatch.invoke(xmlString);
128: TestLogger.logger.debug(">> Response [" + retVal + "]");
129:
130: fail("Set PASSWORD with no USERID: WebServiceException is expected");
131: } catch (WebServiceException wse) {
132: TestLogger.logger.debug(getName() + ": " + wse);
133: }
134: }
135:
136: /**
137: * Auxiliary method, generates a Dispatch object on demand
138: *
139: * @param mode
140: * Service.Mode
141: * @param endpoint
142: * endpoint address
143: * @param binding
144: * binding type
145: * @return
146: */
147: private Dispatch<String> getDispatch(Service.Mode mode,
148: String endpoint, String binding) {
149:
150: Service service = Service.create(SERVICE_QNAME);
151:
152: service.addPort(PORT_QNAME, binding, endpoint);
153: javax.xml.ws.Dispatch<String> dispatch = service
154: .createDispatch(PORT_QNAME, String.class, mode);
155:
156: assertNotNull("Dispatch not null", dispatch);
157:
158: return dispatch;
159: }
160: }
|