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