View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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  package org.kuali.rice.ksb.messaging;
17  
18  import static org.junit.Assert.assertFalse;
19  import static org.junit.Assert.assertNotNull;
20  import static org.junit.Assert.assertTrue;
21  import static org.junit.Assert.fail;
22  
23  import java.util.List;
24  
25  import javax.xml.namespace.QName;
26  
27  import org.junit.Test;
28  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
29  import org.kuali.rice.ksb.api.messaging.ResourceFacade;
30  import org.kuali.rice.ksb.messaging.remotedservices.BaseballCard;
31  import org.kuali.rice.ksb.messaging.remotedservices.BaseballCardCollectionService;
32  import org.kuali.rice.ksb.messaging.remotedservices.Inbox;
33  import org.kuali.rice.ksb.messaging.remotedservices.InboxResource;
34  import org.kuali.rice.ksb.messaging.remotedservices.Message;
35  import org.kuali.rice.ksb.messaging.remotedservices.MessageResource;
36  import org.kuali.rice.ksb.test.KSBTestCase;
37  
38  
39  /**
40   * Test that RESTful services work over the KSB
41   *
42   * @author Kuali Rice Team (rice.collab@kuali.org)
43   */
44  public class RESTServiceTest extends KSBTestCase {
45  
46      private static final String BBCARD_SERVICE = "baseballCardCollectionService";
47      private static final String KMS_SERVICE = "kms";
48      private static final String NAMESPACE = "test";
49  
50  
51      public boolean startClient1() {
52          return true;
53      }
54  
55      @Test
56      public void testMessagingService() throws Exception {
57      	ResourceFacade kmsService =
58              (ResourceFacade) GlobalResourceLoader.getService(
59                  new QName(NAMESPACE, KMS_SERVICE));
60  
61      	// Get service by resource name
62      	InboxResource inboxResource = kmsService.getResource("inbox");
63  //    	kmsService.getRe...
64      	// Get service by resource class
65      	MessageResource messageResource = kmsService.getResource(MessageResource.class);
66  
67      	exerciseMessagingService(inboxResource, messageResource);
68      }
69  
70      /**
71  	 * This method ...
72  	 *
73  	 * @param inboxResource
74  	 * @param messageResource
75  	 */
76  	private void exerciseMessagingService(InboxResource inboxResource,
77  			MessageResource messageResource) {
78  		Inbox inbox = new Inbox();
79      	inbox.setOwner("Joe Q. Tester");
80  
81      	inbox = inboxResource.createInbox(inbox);
82  
83      	Message message = new Message();
84      	message.setRecipient("Joe Q. Tester");
85      	message.setSubject("Hello new world!");
86      	message.setText("This is a test message.");
87  
88      	Message createdMessage = messageResource.createMessage(message);
89  
90      	List<String> messages = inbox.getMessages();
91      	messages.add(createdMessage.getId());
92  
93      	inboxResource.updateInbox(inbox);
94  
95      	inbox = inboxResource.retrieveInbox(inbox.getId());
96  
97      	List<String> updatedMessages = inbox.getMessages();
98  
99      	String updatedMessageId = updatedMessages.get(0);
100 
101     	Message retrievedMessage = messageResource.retrieve(updatedMessageId);
102 
103     	assertTrue(retrievedMessage.getSubject().equals("Hello new world!"));
104 	}
105 
106     /**
107      * Exercise our RESTful {@link BaseballCardCollectionService} over the KSB
108      */
109     @Test
110     public void testBaseballCardCollectionService() {
111         BaseballCard cardA = new BaseballCard("Mickey Mantle", "Topps", 1952);
112         BaseballCard cardB = new BaseballCard("Ted Williams", "Bowman", 1954);
113         BaseballCard cardC = new BaseballCard("Willie Mays", "Bowman", 1951);
114         BaseballCard cardD = new BaseballCard("Willie Mays Hayes", "Bogus", 1989);
115 
116         BaseballCardCollectionService service = 
117             (BaseballCardCollectionService) GlobalResourceLoader.getService(
118                     new QName(NAMESPACE, BBCARD_SERVICE)
119             );
120 
121         // test @POST
122         service.add(cardA);
123         service.add(cardB);
124         Integer willieMaysId = service.add(cardC);
125 
126         // test @GET
127         List<BaseballCard> allCards = service.getAll();
128         assertNotNull(allCards);
129         assertTrue(allCards.size() == 3);
130         assertTrue(allCards.contains(cardA));
131         assertTrue(allCards.contains(cardB));
132         assertTrue(allCards.contains(cardC));
133 
134         // test @PUT
135         service.update(willieMaysId, cardD); // replace Willie Mays w/ Willie Mays Hayes
136         allCards = service.getAll();
137         assertNotNull(allCards);
138         assertTrue(allCards.size() == 3);
139         assertFalse(allCards.contains(cardC)); // this one was replaced
140         assertTrue(allCards.contains(cardD));  // this was the replacement
141 
142         // test @DELETE
143         service.delete(willieMaysId); // delete Willie
144         allCards = service.getAll();
145         assertNotNull(allCards);
146         assertTrue(allCards.size() == 2);
147         assertFalse(allCards.contains(cardD)); // should be gone
148 
149         // test that adding a card already in the collection in fact adds to the collection. 
150         service.add(cardA);
151         service.add(cardA);
152         service.add(cardA);
153         allCards = service.getAll();
154         assertTrue(allCards.size() == 5);
155         
156         try {
157             service.unannotatedMethod();
158             fail("Magic?  You can't remotely invoke a method that doesn't have JAX-RS annotations in the resource class!");
159         } catch (Exception e) {
160             // I expect this to throw an exception
161             e.printStackTrace();
162         }
163     }
164 
165 
166     /**
167      * By defualt, CXF has a special Service List page that you can see by 
168      * appending /services to a RESTful service URL.  We've purposefully disabled it,
169      * so let's verify that it isn't there.
170      * 
171      * @throws Exception
172      */
173    /* @Test
174     public void testCXFServiceListIsDisabled() throws Exception {
175         // if CXF's service list is enabled, this URL will return the service list
176         URL url = new URL(getEndpointUrl()+"/services");
177 
178         try {
179             InputStream contentStream = (InputStream)url.getContent();
180             fail("the service list shouldn't be available!");
181         } catch (IOException e) {
182             // this is what should happen
183         }
184     }*/
185 
186 }