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.FlickrTransformers;
11  import org.mule.tck.junit4.AbstractMuleContextTestCase;
12  
13  import java.net.URL;
14  import java.util.ArrayList;
15  import java.util.List;
16  
17  import org.ibeans.annotation.IntegrationBean;
18  import org.ibeans.api.CallException;
19  import org.junit.Test;
20  import org.w3c.dom.Document;
21  import org.w3c.dom.Node;
22  
23  import static org.ibeans.impl.IBeansSupport.prettyPrintXml;
24  import static org.ibeans.impl.IBeansSupport.select;
25  import static org.junit.Assert.assertEquals;
26  import static org.junit.Assert.assertNotNull;
27  import static org.junit.Assert.assertTrue;
28  
29  public class FlickrTestCase extends AbstractMuleContextTestCase
30  {
31      public static final String SEARCH_TERM = "food";
32      public static final String BAD_SEARCH_TERM = "bad";
33  
34      @IntegrationBean
35      private FlickrIBean flickr;
36  
37      public FlickrTestCase()
38      {
39          setStartContext(true);        
40      }
41  
42      @Override
43      protected void doSetUp() throws Exception
44      {
45          //Add the test case so that the IntegrationBean DI will be processed
46          muleContext.getRegistry().registerObject("test", this);
47          //No classpath scanning is during for tests so we have to register the Flickr Transformers manually
48          muleContext.getRegistry().registerObject("flickrTransformers", new FlickrTransformers());
49          //getFlickr().init("${flickr.api.key}","${flickr.secret.key}", FlickrIBean.FORMAT.XML, Document.class);
50          getFlickr().init("3a690a103c6eabf55de5b10623021a34","1d3d4a305cc369fc", FlickrIBean.FORMAT.XML, Document.class);
51      }
52  
53      protected FlickrIBean getFlickr()
54      {
55          return flickr;
56      }
57  
58      @Test
59      public void testFlickrSearch() throws Exception
60      {
61          Document doc = getFlickr().search(SEARCH_TERM);
62          assertNotNull(doc);
63          List<URL> photoUrls = new ArrayList<URL>();
64  
65          for (Node n : select("//photo", doc))
66          {
67              photoUrls.add(getFlickr().getPhotoURL(n));
68          }
69  
70          assertEquals(10, photoUrls.size());
71      }
72  
73      //This will fail since "badkey" is not a recognised key
74      @Test
75      public void testFlickrError() throws Exception
76      {
77          getFlickr().init("badkey", FlickrIBean.FORMAT.XML, Document.class);
78  
79          try
80          {
81              getFlickr().search(BAD_SEARCH_TERM);
82          }
83          catch (CallException e)
84          {
85              //Flickr error code
86              assertEquals("100", e.getErrorCode());
87          }
88      }
89  
90      @Test
91      public void testSizeEnum() throws Exception
92      {
93          assertEquals("o", FlickrIBean.IMAGE_SIZE.Original.toString());
94          assertEquals("m", FlickrIBean.DEFAULT_IMAGE_SIZE.toString());
95          assertEquals(FlickrIBean.IMAGE_SIZE.Original, Enum.valueOf(FlickrIBean.IMAGE_SIZE.class, "Original"));
96  
97          Document doc = getFlickr().search(SEARCH_TERM);
98  
99          assertNotNull(doc);
100         List<URL> photoUrls = new ArrayList<URL>();
101 
102         System.out.println(prettyPrintXml(doc));
103         for (Node n : select("//photo", doc))
104         {
105             photoUrls.add(getFlickr().getPhotoURL(n, FlickrIBean.IMAGE_SIZE.SmallSquare, FlickrIBean.IMAGE_TYPE.Jpeg));
106         }
107         assertEquals(10, photoUrls.size());
108         assertTrue(photoUrls.get(0).toString().endsWith("_s.jpg"));
109     }
110 
111     //TODO
112 //    @Test
113 //    public void testAuth() throws Exception
114 //    {
115 //        String frob = getFlickr().getFrob();
116 //        assertNotNull(frob);
117 //        URL url = getFlickr().buildAuthenticationURL(frob, "delete");
118 //        //Just make sure it works
119 //        assertNotNull(url);
120 //        //We can't generate a an authToken automatically as it requries the account owner to give iBeans access
121 //        //but we can check the validity of an existing auth token
122 //        //TODO URGENT can't use a placeholder value here for some reason
123 //        AuthToken auth = getFlickr().checkAuthToken((String)iBeansContext.getConfig().get("flickr.auth.token"));
124 //        assertNotNull(auth);
125 //        assertEquals("delete", auth.getPerms());
126 //        assertEquals("rossmason", auth.getUser().getUsername());
127 //    }
128 
129     //TODO upload not working yet, suspect it has something to do with the way form-data is handled
130 //    @Test
131 //    public void testUpload() throws Exception
132 //    {
133 //        URL url = new URL("file:///projects/ibeans-contrib/twitter/src/test/resources/profile.png");
134 //        String result = getFlickr().upload(url, get("flickr.auth.token"), "Test 1", null, null, null, null, null);
135 //        System.out.println(result);
136 //    }
137 }