View Javadoc

1   /*
2    * $Id: FlickrSearchTestCase.java 20320 2010-11-24 15:03:31Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  package org.mule.module.ibeans;
11  
12  import org.mule.ibeans.flickr.FlickrIBean;
13  import org.mule.ibeans.flickr.FlickrSearchIBean;
14  import org.mule.module.json.JsonData;
15  import org.mule.tck.AbstractMuleTestCase;
16  
17  import org.codehaus.jackson.node.ArrayNode;
18  import org.ibeans.annotation.IntegrationBean;
19  import org.ibeans.api.CallException;
20  import org.w3c.dom.Document;
21  
22  /**
23   * This tests that we can use the FlickrSearch iBean without needing a 'secret_key' which is required for other
24   * parts of the Flickr API
25   */
26  public class FlickrSearchTestCase extends AbstractMuleTestCase
27  {
28      public static final String SEARCH_TERM = "food";
29      public static final String BAD_SEARCH_TERM = "bad";
30  
31      @IntegrationBean
32      private FlickrSearchIBean flickr;
33  
34      public FlickrSearchTestCase()
35      {
36          setStartContext(true);
37      }
38  
39      @Override
40      protected void doSetUp() throws Exception
41      {
42          //Add the test case so that the IntegrationBean DI will be processed        
43          muleContext.getRegistry().registerObject("test", this);
44          //getFlickr().init("${flickr.api.key}","${flickr.secret.key}", FlickrIBean.FORMAT.XML, Document.class);
45          getFlickr().init("3a690a103c6eabf55de5b10623021a34", FlickrIBean.FORMAT.JSON, JsonData.class);
46  
47      }
48  
49      protected FlickrSearchIBean getFlickr()
50      {
51          return flickr;
52      }
53  
54      public void testFlickrSearch() throws Exception
55      {
56          JsonData doc = getFlickr().search(SEARCH_TERM);
57          assertNotNull(doc);
58          assertEquals(10, ((ArrayNode) doc.get("/photos/photo")).size());
59      }
60  
61      //This will fail since "badkey" is not a recognised key
62      public void testFlickrError() throws Exception
63      {
64          getFlickr().init("badkey", FlickrIBean.FORMAT.XML, Document.class);
65  
66          try
67          {
68              getFlickr().search(BAD_SEARCH_TERM);
69          }
70          catch (CallException e)
71          {
72              //Flickr error code
73              assertEquals("100", e.getErrorCode());
74          }
75      }
76  }