View Javadoc

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