001    /**
002     * Copyright 2005-2011 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     */
016    package org.kuali.rice.ksb.messaging;
017    
018    import static org.junit.Assert.assertFalse;
019    import static org.junit.Assert.assertNotNull;
020    import static org.junit.Assert.assertTrue;
021    import static org.junit.Assert.fail;
022    
023    import java.util.List;
024    
025    import javax.xml.namespace.QName;
026    
027    import org.junit.Test;
028    import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
029    import org.kuali.rice.ksb.api.messaging.ResourceFacade;
030    import org.kuali.rice.ksb.messaging.remotedservices.BaseballCard;
031    import org.kuali.rice.ksb.messaging.remotedservices.BaseballCardCollectionService;
032    import org.kuali.rice.ksb.messaging.remotedservices.Inbox;
033    import org.kuali.rice.ksb.messaging.remotedservices.InboxResource;
034    import org.kuali.rice.ksb.messaging.remotedservices.Message;
035    import org.kuali.rice.ksb.messaging.remotedservices.MessageResource;
036    import 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     */
044    public 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 =
058                (ResourceFacade) GlobalResourceLoader.getService(
059                    new QName(NAMESPACE, KMS_SERVICE));
060    
061            // Get service by resource name
062            InboxResource inboxResource = kmsService.getResource("inbox");
063    //      kmsService.getRe...
064            // Get service by resource class
065            MessageResource messageResource = kmsService.getResource(MessageResource.class);
066    
067            exerciseMessagingService(inboxResource, messageResource);
068        }
069    
070        /**
071             * This method ...
072             *
073             * @param inboxResource
074             * @param messageResource
075             */
076            private void exerciseMessagingService(InboxResource inboxResource,
077                            MessageResource messageResource) {
078                    Inbox inbox = new Inbox();
079            inbox.setOwner("Joe Q. Tester");
080    
081            inbox = inboxResource.createInbox(inbox);
082    
083            Message message = new Message();
084            message.setRecipient("Joe Q. Tester");
085            message.setSubject("Hello new world!");
086            message.setText("This is a test message.");
087    
088            Message createdMessage = messageResource.createMessage(message);
089    
090            List<String> messages = inbox.getMessages();
091            messages.add(createdMessage.getId());
092    
093            inboxResource.updateInbox(inbox);
094    
095            inbox = inboxResource.retrieveInbox(inbox.getId());
096    
097            List<String> updatedMessages = inbox.getMessages();
098    
099            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    }