001: /**
002: *
003: * Copyright 2004 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.ws.scout.registry.qa;
017:
018: import static org.junit.Assert.fail;
019:
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.Iterator;
023:
024: import javax.xml.registry.BulkResponse;
025: import javax.xml.registry.BusinessQueryManager;
026: import javax.xml.registry.FindQualifier;
027: import javax.xml.registry.JAXRException;
028: import javax.xml.registry.JAXRResponse;
029: import javax.xml.registry.RegistryService;
030: import javax.xml.registry.infomodel.InternationalString;
031: import javax.xml.registry.infomodel.Key;
032: import javax.xml.registry.infomodel.Organization;
033: import javax.xml.registry.infomodel.Service;
034:
035: import junit.framework.JUnit4TestAdapter;
036:
037: import org.apache.ws.scout.BaseTestCase;
038: import org.junit.After;
039: import org.junit.Before;
040: import org.junit.Test;
041:
042: /**
043: * Tests Publish, Delete (and indirectly, find) for service bindings.
044: *
045: * You can comment out the deletion portion and use
046: * Open source UDDI Browser <http://www.uddibrowser.org>
047: * to check your intermediate results.
048: *
049: * Based on query/publish tests written by
050: * <a href="mailto:anil@apache.org">Anil Saldhana</a>.
051: *
052: * @author <a href="mailto:dbhole@redhat.com">Deepak Bhole</a>
053: * @author <a href="mailto:anil@apache.org">Anil Saldhana</a>
054: *
055: * @since Sep 27, 2005
056: */
057: public class JAXR040ServiceTest extends BaseTestCase {
058: private BusinessQueryManager bqm = null;
059:
060: String serviceName = "Apache JAXR Service -- APACHE SCOUT TEST";
061: String tempOrgName = "Apache JAXR Service Org -- APACHE SCOUT TEST";
062:
063: @Before
064: public void setUp() {
065: super .setUp();
066: }
067:
068: @After
069: public void tearDown() {
070: super .tearDown();
071: }
072:
073: /**
074: * Tests publishing and deleting of services.
075: *
076: * Do not break this into testPublish(), testDelete(), etc. Order is
077: * important, and not all jvms can guarantee order since the JUnit framework
078: * uses getMethods() to gather test methods, and getMethods() does not
079: * guarantee order.
080: */
081: @Test
082: public void testPublishFindAndDeleteService() {
083: login();
084: try {
085: RegistryService rs = connection.getRegistryService();
086: bqm = rs.getBusinessQueryManager();
087: blm = rs.getBusinessLifeCycleManager();
088:
089: System.out
090: .println("\nCreating temporary organization...\n");
091: Organization org = createTempOrg();
092:
093: System.out.println("\nCreating service...\n");
094: createService(org);
095:
096: // All created ... now try to delete.
097:
098: findAndDeleteService(org.getKey());
099: deleteTempOrg(org.getKey());
100:
101: } catch (JAXRException e) {
102: e.printStackTrace();
103: fail(e.getMessage());
104: }
105: }
106:
107: private void createService(Organization org) throws JAXRException {
108: Service service = blm.createService(getIString(serviceName));
109: service.setDescription(getIString("Services in UDDI Registry"));
110: service.setProvidingOrganization(org);
111:
112: ArrayList<Service> services = new ArrayList<Service>();
113: services.add(service);
114:
115: BulkResponse br = blm.saveServices(services);
116: if (br.getStatus() == JAXRResponse.STATUS_SUCCESS) {
117: System.out.println("Service Saved");
118: Collection coll = br.getCollection();
119: Iterator iter = coll.iterator();
120: while (iter.hasNext()) {
121: Key key = (Key) iter.next();
122: System.out.println("Saved Key=" + key.getId());
123: }//end while
124: } else {
125: System.err.println("JAXRExceptions "
126: + "occurred during save:");
127: Collection exceptions = br.getExceptions();
128: Iterator iter = exceptions.iterator();
129: while (iter.hasNext()) {
130: Exception e = (Exception) iter.next();
131: System.err.println(e.toString());
132: fail(e.toString());
133: }
134: }
135: }
136:
137: private void findAndDeleteService(Key orgKey) throws JAXRException {
138: Collection<String> findQualifiers = new ArrayList<String>();
139: findQualifiers.add(FindQualifier.SORT_BY_NAME_ASC);
140: Collection<String> namePatterns = new ArrayList<String>();
141: namePatterns.add("%" + serviceName + "%");
142:
143: BulkResponse br = bqm.findServices(orgKey, findQualifiers,
144: namePatterns, null, null);
145: Collection services = br.getCollection();
146:
147: if (services == null) {
148: System.out.println("\n-- Matched 0 orgs");
149:
150: } else {
151: System.out.println("\n-- Matched " + services.size()
152: + " services --\n");
153:
154: // then step through them
155: for (Iterator conceptIter = services.iterator(); conceptIter
156: .hasNext();) {
157: Service s = (Service) conceptIter.next();
158:
159: System.out.println("Id: " + s.getKey().getId());
160: System.out.println("Name: " + s.getName().getValue());
161:
162: // Print spacer between messages
163: System.out.println(" --- ");
164:
165: deleteService(s.getKey());
166:
167: System.out
168: .println("\n ============================== \n");
169: }
170: }
171: }
172:
173: private void deleteService(Key key) throws JAXRException {
174:
175: String id = key.getId();
176:
177: System.out.println("\nDeleting service with id " + id + "\n");
178:
179: Collection<Key> keys = new ArrayList<Key>();
180: keys.add(key);
181: BulkResponse response = blm.deleteServices(keys);
182:
183: Collection exceptions = response.getExceptions();
184: if (exceptions == null) {
185: Collection retKeys = response.getCollection();
186: Iterator keyIter = retKeys.iterator();
187: javax.xml.registry.infomodel.Key orgKey = null;
188: if (keyIter.hasNext()) {
189: orgKey = (javax.xml.registry.infomodel.Key) keyIter
190: .next();
191: id = orgKey.getId();
192: System.out.println("Service with ID=" + id
193: + " was deleted");
194: }
195: }
196: }
197:
198: private Organization createTempOrg() throws JAXRException {
199:
200: Key orgKey = null;
201: Organization org = blm
202: .createOrganization(getIString(tempOrgName));
203: org
204: .setDescription(getIString("Temporary organization to test saveService()"));
205:
206: Collection<Organization> orgs = new ArrayList<Organization>();
207: orgs.add(org);
208: BulkResponse br = blm.saveOrganizations(orgs);
209:
210: if (br.getStatus() == JAXRResponse.STATUS_SUCCESS) {
211: orgKey = (Key) br.getCollection().iterator().next();
212: System.out
213: .println("Temporary Organization Created with id="
214: + orgKey.getId());
215: org.setKey(orgKey);
216: } else {
217: System.err
218: .println("JAXRExceptions "
219: + "occurred during creation of temporary organization:");
220:
221: Iterator iter = br.getCollection().iterator();
222:
223: while (iter.hasNext()) {
224: Exception e = (Exception) iter.next();
225: System.err.println(e.toString());
226: }
227:
228: fail();
229: }
230:
231: return org;
232: }
233:
234: private void deleteTempOrg(Key orgKey) throws JAXRException {
235:
236: String id = orgKey.getId();
237:
238: System.out.println("\nDeleting temporary organization with id "
239: + id + "\n");
240:
241: Collection<Key> keys = new ArrayList<Key>();
242: keys.add(orgKey);
243: BulkResponse response = blm.deleteOrganizations(keys);
244:
245: Collection exceptions = response.getExceptions();
246: if (exceptions == null) {
247: Collection retKeys = response.getCollection();
248: Iterator keyIter = retKeys.iterator();
249: orgKey = null;
250: if (keyIter.hasNext()) {
251: orgKey = (javax.xml.registry.infomodel.Key) keyIter
252: .next();
253: id = orgKey.getId();
254: System.out.println("Organization with ID=" + id
255: + " was deleted");
256: }
257: }
258: }
259:
260: private InternationalString getIString(String str)
261: throws JAXRException {
262: return blm.createInternationalString(str);
263: }
264:
265: public static junit.framework.Test suite() {
266: return new JUnit4TestAdapter(JAXR040ServiceTest.class);
267: }
268: }
|