1   /**
2    *
3    * Copyright 2004 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.ws.scout.registry.qa;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertNull;
21  import static org.junit.Assert.assertTrue;
22  import static org.junit.Assert.fail;
23  
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.Iterator;
27  
28  import javax.xml.registry.BulkResponse;
29  import javax.xml.registry.JAXRException;
30  import javax.xml.registry.JAXRResponse;
31  import javax.xml.registry.RegistryService;
32  import javax.xml.registry.infomodel.Classification;
33  import javax.xml.registry.infomodel.ClassificationScheme;
34  import javax.xml.registry.infomodel.EmailAddress;
35  import javax.xml.registry.infomodel.Key;
36  import javax.xml.registry.infomodel.Organization;
37  import javax.xml.registry.infomodel.PersonName;
38  import javax.xml.registry.infomodel.PostalAddress;
39  import javax.xml.registry.infomodel.Service;
40  import javax.xml.registry.infomodel.ServiceBinding;
41  import javax.xml.registry.infomodel.TelephoneNumber;
42  import javax.xml.registry.infomodel.User;
43  
44  import junit.framework.JUnit4TestAdapter;
45  
46  import org.apache.ws.scout.BaseTestCase;
47  import org.apache.ws.scout.Creator;
48  import org.apache.ws.scout.Finder;
49  import org.apache.ws.scout.Printer;
50  import org.apache.ws.scout.Remover;
51  import org.junit.After;
52  import org.junit.Before;
53  import org.junit.Test;
54  
55  /**
56   * Test to check that the primary contact is added
57   * @author <a href="mailto:tcunning@redhat.com">Tom Cunningham</a>
58   * @since Dec 6, 2007
59   */
60  public class JAXR015PrimaryContactTest extends BaseTestCase
61  {
62  	private static final String PERSON_NAME = "John AXel Rose";
63  	private static final String PHONE_NUMBER = "111-222-3333";
64  	private static final String STREET_NUMBER = "1";
65  	private static final String STREET = "Uddi Drive";
66  	private static final String CITY = "Apache Town";
67  	private static final String STATE = "CA";
68  	private static final String COUNTRY = "USA";
69  	private static final String POSTAL_CODE = "00000-1111";
70  
71  	private static final String EMAIL = "jaxr@apache.org";
72  
73      @Before
74      public void setUp()
75      {
76          super.setUp();
77      }
78      
79      @After
80      public void tearDown()
81      {
82        super.tearDown();
83      }
84      
85      @Test 
86      public void publishClassificationScheme()
87      {
88          login();
89          try
90          {
91              RegistryService rs = connection.getRegistryService();
92              blm = rs.getBusinessLifeCycleManager();
93              Creator creator = new Creator(blm);
94              
95              Collection<ClassificationScheme> schemes = new ArrayList<ClassificationScheme>();
96              ClassificationScheme classificationScheme = creator.createClassificationScheme(this.getClass().getName());
97              schemes.add(classificationScheme);
98              
99              BulkResponse bulkResponse = blm.saveClassificationSchemes(schemes);
100             assertEquals(JAXRResponse.STATUS_SUCCESS,bulkResponse.getStatus());
101             
102             
103         } catch (JAXRException e) {
104             e.printStackTrace();
105             assertTrue(false);
106         }   
107     }
108     
109     @Test
110     public void publishOrganization()
111     {
112         BulkResponse response = null;
113         login();
114         try
115         {
116             RegistryService rs = connection.getRegistryService();
117             blm = rs.getBusinessLifeCycleManager();
118             bqm = rs.getBusinessQueryManager();
119             Creator creator = new Creator(blm);
120             Finder finder = new Finder(bqm, uddiversion);
121             
122             Collection<Organization> orgs = new ArrayList<Organization>();
123             Organization organization = creator.createOrganization(this.getClass().getName());
124 //          Add a Service
125             Service service = creator.createService(this.getClass().getName());
126             ServiceBinding serviceBinding = creator.createServiceBinding();
127             service.addServiceBinding(serviceBinding);
128             organization.addService(service);
129             //Add a classification
130             ClassificationScheme cs = finder.findClassificationSchemeByName(this.getClass().getName());
131             Classification classification = creator.createClassification(cs);
132             organization.addClassification(classification);
133             
134             User user = blm.createUser();
135             PersonName personName = blm.createPersonName(PERSON_NAME);
136             TelephoneNumber telephoneNumber = blm.createTelephoneNumber();
137             telephoneNumber.setNumber(PHONE_NUMBER);
138             telephoneNumber.setType(null);
139             PostalAddress address = blm.createPostalAddress(STREET_NUMBER,
140                 STREET, CITY, STATE, COUNTRY, POSTAL_CODE, "");
141 
142             Collection<PostalAddress> postalAddresses = new ArrayList<PostalAddress>();
143             postalAddresses.add(address);
144             Collection<EmailAddress> emailAddresses = new ArrayList<EmailAddress>();
145             EmailAddress emailAddress = blm.createEmailAddress(EMAIL);
146             emailAddresses.add(emailAddress);
147 
148             Collection<TelephoneNumber> numbers = new ArrayList<TelephoneNumber>();
149             numbers.add(telephoneNumber);
150             user.setPersonName(personName);
151             user.setPostalAddresses(postalAddresses);
152             user.setEmailAddresses(emailAddresses);
153             user.setTelephoneNumbers(numbers);
154             organization.setPrimaryContact(user);
155 
156             orgs.add(organization);            
157             
158             //Now save the Organization along with a Service, ServiceBinding and Classification
159             BulkResponse br = blm.saveOrganizations(orgs);
160             if (br.getStatus() == JAXRResponse.STATUS_SUCCESS)
161             {
162                 System.out.println("Organization Saved");
163                 Collection coll = br.getCollection();
164                 Iterator iter = coll.iterator();
165                 while (iter.hasNext())
166                 {
167                     Key key = (Key) iter.next();
168                     System.out.println("Saved Key=" + key.getId());
169                 }//end while
170             } else
171             {
172                 System.err.println("JAXRExceptions " +
173                         "occurred during save:");
174                 Collection exceptions = br.getExceptions();
175                 Iterator iter = exceptions.iterator();
176                 while (iter.hasNext())
177                 {
178                     Exception e = (Exception) iter.next();
179                     System.err.println(e.toString());
180                 }
181             }
182             
183         } catch (JAXRException e) {
184             e.printStackTrace();
185 			assertTrue(false);
186         }
187         assertNull(response);
188     }
189     
190     @SuppressWarnings("unchecked")
191     @Test
192     public void queryOrganization()
193     {
194         login();
195         try
196         {
197             // Get registry service and business query manager
198             RegistryService rs = connection.getRegistryService();
199             bqm = rs.getBusinessQueryManager();
200             System.out.println("We have the Business Query Manager");
201             Printer printer = new Printer();
202             Finder finder = new Finder(bqm, uddiversion);
203 
204             Collection orgs = finder.findOrganizationsByName(this.getClass().getName());
205             if (orgs == null) {
206                 fail("Only Expecting 1 Organization");
207             } else {
208                 assertEquals(1,orgs.size());
209                 // then step through them
210                 for (Iterator orgIter = orgs.iterator(); orgIter.hasNext();)
211                 {
212                     Organization org = (Organization) orgIter.next();
213                     System.out.println("Org name: " + printer.getName(org));
214                     System.out.println("Org description: " + printer.getDescription(org));
215                     System.out.println("Org key id: " + printer.getKey(org));
216 
217                     User user = org.getPrimaryContact();
218                     System.out.println("Primary Contact Full Name : " + user.getPersonName().getFullName());
219 					assertEquals("User name does not match", user.getPersonName().getFullName(), PERSON_NAME);
220 					
221                     Collection<EmailAddress> emailAddresses = user.getEmailAddresses();
222 					System.out.println("Found " + emailAddresses.size() + " email addresses.");
223                     assertEquals("Should have found 1 email address, found " + emailAddresses.size(), 1, emailAddresses.size());
224 					for (EmailAddress email : emailAddresses) {
225                     	System.out.println("Primary Contact email : " + email.getAddress());
226 						assertEquals("Email should be " + EMAIL, EMAIL, email.getAddress());
227                     }
228 					
229                     Collection<PostalAddress> postalAddresses = user.getPostalAddresses();
230 					System.out.println("Found " + postalAddresses.size() + " postal addresses.");
231                     assertEquals("Should have found 1 postal address, found " + postalAddresses.size(), 1, postalAddresses.size());
232 					for (PostalAddress postalAddress : postalAddresses) {
233 						System.out.println("Postal Address is " + postalAddress);
234 						assertEquals("Street number should be " + STREET_NUMBER, STREET_NUMBER, postalAddress.getStreetNumber());
235 						assertEquals("Street should be " + STREET, STREET, postalAddress.getStreet());
236 						assertEquals("City should be " + CITY, CITY, postalAddress.getCity());
237 						assertEquals("State should be " + STATE, STATE, postalAddress.getStateOrProvince());
238 						assertEquals("Country should be " + COUNTRY, COUNTRY, postalAddress.getCountry());
239 						assertEquals("Postal code should be " + POSTAL_CODE, POSTAL_CODE, postalAddress.getPostalCode());
240                     }
241 					
242 					Collection<TelephoneNumber> numbers = user.getTelephoneNumbers(null);
243 					System.out.println("Found " + numbers.size() + " telephone numbers.");
244                     assertEquals("Should have found 1 phone number, found " + numbers.size(), 1, numbers.size());
245 					for (TelephoneNumber tele : numbers) {
246 						System.out.println("Phone number is " + tele.getNumber());
247 						assertEquals("Telephone number should be " + PHONE_NUMBER, PHONE_NUMBER, tele.getNumber());
248 					}
249                     printer.printServices(org);
250                     printer.printClassifications(org);
251                 }
252             }//end else
253         } catch (JAXRException e) {
254             e.printStackTrace();
255             fail(e.getMessage());
256         } 
257     }
258     
259     @Test
260     public void deleteOrganization()
261     {
262         login();
263         try
264         {
265             RegistryService rs = connection.getRegistryService();
266             blm = rs.getBusinessLifeCycleManager();
267     //      Get registry service and business query manager
268             bqm = rs.getBusinessQueryManager();
269             System.out.println("We have the Business Query Manager");
270             Finder finder = new Finder(bqm, uddiversion);
271             Remover remover = new Remover(blm);
272             Collection orgs = finder.findOrganizationsByName(this.getClass().getName());
273             for (Iterator orgIter = orgs.iterator(); orgIter.hasNext();)
274             {
275                 Organization org = (Organization) orgIter.next();
276                 remover.removeOrganization(org);
277             }
278             
279         } catch (Exception e) {
280             e.printStackTrace();
281             fail(e.getMessage());
282         }
283     }
284     
285     @Test
286     public void deleteClassificationScheme()
287     {
288         login();
289         try {
290             RegistryService rs = connection.getRegistryService();
291             bqm = rs.getBusinessQueryManager();
292             blm = rs.getBusinessLifeCycleManager();
293             System.out.println("We have the Business Query Manager");
294             Finder finder = new Finder(bqm, uddiversion);
295             Remover remover = new Remover(blm);
296             Collection schemes = finder.findClassificationSchemesByName(this.getClass().getName());
297             for (Iterator iter = schemes.iterator(); iter.hasNext();)
298             {
299                 ClassificationScheme scheme = (ClassificationScheme) iter.next();
300                 remover.removeClassificationScheme(scheme);
301             }
302             
303         } catch (Exception e) {
304             e.printStackTrace();
305             fail(e.getMessage());
306         }
307     }
308     
309     public static junit.framework.Test suite() {
310         return new JUnit4TestAdapter(JAXR015PrimaryContactTest.class);
311     }
312 }