View Javadoc
1   /**
2    * Copyright 2005-2014 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 = GlobalResourceLoader.getService(new QName(NAMESPACE, KMS_SERVICE));
58  
59      	// Get service by resource name
60      	InboxResource inboxResource = kmsService.getResource("inbox");
61  //    	kmsService.getRe...
62      	// Get service by resource class
63      	MessageResource messageResource = kmsService.getResource(MessageResource.class);
64  
65      	exerciseMessagingService(inboxResource, messageResource);
66      }
67  
68      /**
69  	 * This method ...
70  	 *
71  	 * @param inboxResource
72  	 * @param messageResource
73  	 */
74  	private void exerciseMessagingService(InboxResource inboxResource,
75  			MessageResource messageResource) {
76  		Inbox inbox = new Inbox();
77      	inbox.setOwner("Joe Q. Tester");
78  
79      	inbox = inboxResource.createInbox(inbox);
80  
81      	Message message = new Message();
82      	message.setRecipient("Joe Q. Tester");
83      	message.setSubject("Hello new world!");
84      	message.setText("This is a test message.");
85  
86      	Message createdMessage = messageResource.createMessage(message);
87  
88      	List<String> messages = inbox.getMessages();
89      	messages.add(createdMessage.getId());
90  
91      	inboxResource.updateInbox(inbox);
92  
93      	inbox = inboxResource.retrieveInbox(inbox.getId());
94  
95      	List<String> updatedMessages = inbox.getMessages();
96  
97      	String updatedMessageId = updatedMessages.get(0);
98  
99      	Message retrievedMessage = messageResource.retrieve(updatedMessageId);
100 
101     	assertTrue(retrievedMessage.getSubject().equals("Hello new world!"));
102 	}
103 
104     /**
105      * Exercise our RESTful {@link BaseballCardCollectionService} over the KSB
106      */
107     @Test
108     public void testBaseballCardCollectionService() {
109         BaseballCard cardA = new BaseballCard("Mickey Mantle", "Topps", 1952);
110         BaseballCard cardB = new BaseballCard("Ted Williams", "Bowman", 1954);
111         BaseballCard cardC = new BaseballCard("Willie Mays", "Bowman", 1951);
112         BaseballCard cardD = new BaseballCard("Willie Mays Hayes", "Bogus", 1989);
113 
114         BaseballCardCollectionService service = 
115             (BaseballCardCollectionService) GlobalResourceLoader.getService(
116                     new QName(NAMESPACE, BBCARD_SERVICE)
117             );
118 
119         // test @POST
120         service.add(cardA);
121         service.add(cardB);
122         Integer willieMaysId = service.add(cardC);
123 
124         // test @GET
125         List<BaseballCard> allCards = service.getAll();
126         assertNotNull(allCards);
127         assertTrue(allCards.size() == 3);
128         assertTrue(allCards.contains(cardA));
129         assertTrue(allCards.contains(cardB));
130         assertTrue(allCards.contains(cardC));
131 
132         // test @PUT
133         service.update(willieMaysId, cardD); // replace Willie Mays w/ Willie Mays Hayes
134         allCards = service.getAll();
135         assertNotNull(allCards);
136         assertTrue(allCards.size() == 3);
137         assertFalse(allCards.contains(cardC)); // this one was replaced
138         assertTrue(allCards.contains(cardD));  // this was the replacement
139 
140         // test @DELETE
141         service.delete(willieMaysId); // delete Willie
142         allCards = service.getAll();
143         assertNotNull(allCards);
144         assertTrue(allCards.size() == 2);
145         assertFalse(allCards.contains(cardD)); // should be gone
146 
147         // test that adding a card already in the collection in fact adds to the collection. 
148         service.add(cardA);
149         service.add(cardA);
150         service.add(cardA);
151         allCards = service.getAll();
152         assertTrue(allCards.size() == 5);
153         
154         try {
155             service.unannotatedMethod();
156             fail("Magic?  You can't remotely invoke a method that doesn't have JAX-RS annotations in the resource class!");
157         } catch (Exception e) {
158             // I expect this to throw an exception
159             e.printStackTrace();
160         }
161     }
162 
163 
164     /**
165      * By defualt, CXF has a special Service List page that you can see by 
166      * appending /services to a RESTful service URL.  We've purposefully disabled it,
167      * so let's verify that it isn't there.
168      * 
169      * @throws Exception
170      */
171    /* @Test
172     public void testCXFServiceListIsDisabled() throws Exception {
173         // if CXF's service list is enabled, this URL will return the service list
174         URL url = new URL(getEndpointUrl()+"/services");
175 
176         try {
177             InputStream contentStream = (InputStream)url.getContent();
178             fail("the service list shouldn't be available!");
179         } catch (IOException e) {
180             // this is what should happen
181         }
182     }*/
183 
184 }