View Javadoc

1   /*
2    * $Id: FlickrSearchTestCase.java 22377 2011-07-11 12:41:42Z dirk.olmes $
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.junit4.AbstractMuleContextTestCase;
16  
17  import org.codehaus.jackson.node.ArrayNode;
18  import org.ibeans.annotation.IntegrationBean;
19  import org.ibeans.api.CallException;
20  import org.junit.Test;
21  import org.w3c.dom.Document;
22  
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertNotNull;
25  
26  /**
27   * This tests that we can use the FlickrSearch iBean without needing a 'secret_key' which is required for other
28   * parts of the Flickr API
29   */
30  public class FlickrSearchTestCase extends AbstractMuleContextTestCase
31  {
32      public static final String SEARCH_TERM = "food";
33      public static final String BAD_SEARCH_TERM = "bad";
34  
35      @IntegrationBean
36      private FlickrSearchIBean flickr;
37  
38      public FlickrSearchTestCase()
39      {
40          setStartContext(true);
41      }
42  
43      @Override
44      protected void doSetUp() throws Exception
45      {
46          //Add the test case so that the IntegrationBean DI will be processed        
47          muleContext.getRegistry().registerObject("test", this);
48          //getFlickr().init("${flickr.api.key}","${flickr.secret.key}", FlickrIBean.FORMAT.XML, Document.class);
49          getFlickr().init("3a690a103c6eabf55de5b10623021a34", FlickrIBean.FORMAT.JSON, JsonData.class);
50  
51      }
52  
53      protected FlickrSearchIBean getFlickr()
54      {
55          return flickr;
56      }
57  
58      @Test
59      public void testFlickrSearch() throws Exception
60      {
61          JsonData doc = getFlickr().search(SEARCH_TERM);
62          assertNotNull(doc);
63          assertEquals(10, ((ArrayNode) doc.get("/photos/photo")).size());
64      }
65  
66      //This will fail since "badkey" is not a recognised key
67      @Test
68      public void testFlickrError() throws Exception
69      {
70          getFlickr().init("badkey", FlickrIBean.FORMAT.XML, Document.class);
71  
72          try
73          {
74              getFlickr().search(BAD_SEARCH_TERM);
75          }
76          catch (CallException e)
77          {
78              //Flickr error code
79              assertEquals("100", e.getErrorCode());
80          }
81      }
82  }