001/** 002 * Copyright 2005-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.ksb.messaging; 017 018import static org.junit.Assert.assertFalse; 019import static org.junit.Assert.assertNotNull; 020import static org.junit.Assert.assertTrue; 021import static org.junit.Assert.fail; 022 023import java.util.List; 024 025import javax.xml.namespace.QName; 026 027import org.junit.Test; 028import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader; 029import org.kuali.rice.ksb.api.messaging.ResourceFacade; 030import org.kuali.rice.ksb.messaging.remotedservices.BaseballCard; 031import org.kuali.rice.ksb.messaging.remotedservices.BaseballCardCollectionService; 032import org.kuali.rice.ksb.messaging.remotedservices.Inbox; 033import org.kuali.rice.ksb.messaging.remotedservices.InboxResource; 034import org.kuali.rice.ksb.messaging.remotedservices.Message; 035import org.kuali.rice.ksb.messaging.remotedservices.MessageResource; 036import org.kuali.rice.ksb.test.KSBTestCase; 037 038 039/** 040 * Test that RESTful services work over the KSB 041 * 042 * @author Kuali Rice Team (rice.collab@kuali.org) 043 */ 044public class RESTServiceTest extends KSBTestCase { 045 046 private static final String BBCARD_SERVICE = "baseballCardCollectionService"; 047 private static final String KMS_SERVICE = "kms"; 048 private static final String NAMESPACE = "test"; 049 050 051 public boolean startClient1() { 052 return true; 053 } 054 055 @Test 056 public void testMessagingService() throws Exception { 057 ResourceFacade kmsService = GlobalResourceLoader.getService(new QName(NAMESPACE, KMS_SERVICE)); 058 059 // Get service by resource name 060 InboxResource inboxResource = kmsService.getResource("inbox"); 061// kmsService.getRe... 062 // Get service by resource class 063 MessageResource messageResource = kmsService.getResource(MessageResource.class); 064 065 exerciseMessagingService(inboxResource, messageResource); 066 } 067 068 /** 069 * This method ... 070 * 071 * @param inboxResource 072 * @param messageResource 073 */ 074 private void exerciseMessagingService(InboxResource inboxResource, 075 MessageResource messageResource) { 076 Inbox inbox = new Inbox(); 077 inbox.setOwner("Joe Q. Tester"); 078 079 inbox = inboxResource.createInbox(inbox); 080 081 Message message = new Message(); 082 message.setRecipient("Joe Q. Tester"); 083 message.setSubject("Hello new world!"); 084 message.setText("This is a test message."); 085 086 Message createdMessage = messageResource.createMessage(message); 087 088 List<String> messages = inbox.getMessages(); 089 messages.add(createdMessage.getId()); 090 091 inboxResource.updateInbox(inbox); 092 093 inbox = inboxResource.retrieveInbox(inbox.getId()); 094 095 List<String> updatedMessages = inbox.getMessages(); 096 097 String updatedMessageId = updatedMessages.get(0); 098 099 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}