View Javadoc

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