View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations under the License.
17   */
18  package org.apache.shindig.social.opensocial.util;
19  
20  import org.apache.shindig.social.SocialApiTestsGuiceModule;
21  import org.apache.shindig.social.core.model.ActivityImpl;
22  import org.apache.shindig.social.core.model.AddressImpl;
23  import org.apache.shindig.social.core.model.ListFieldImpl;
24  import org.apache.shindig.social.core.model.MediaItemImpl;
25  import org.apache.shindig.social.core.model.NameImpl;
26  import org.apache.shindig.social.core.model.PersonImpl;
27  import org.apache.shindig.social.core.util.BeanJsonConverter;
28  import org.apache.shindig.social.opensocial.model.Activity;
29  import org.apache.shindig.social.opensocial.model.Address;
30  import org.apache.shindig.social.opensocial.model.ListField;
31  import org.apache.shindig.social.opensocial.model.MediaItem;
32  import org.apache.shindig.social.opensocial.model.Person;
33  
34  import com.google.common.collect.Lists;
35  import com.google.common.collect.Maps;
36  import com.google.inject.Guice;
37  import junit.framework.TestCase;
38  import org.json.JSONArray;
39  import org.json.JSONObject;
40  
41  import java.util.Map;
42  import java.util.Map.Entry;
43  
44  public class BeanJsonConverterTest extends TestCase {
45    private Person johnDoe;
46    private Activity activity;
47  
48    private BeanJsonConverter beanJsonConverter;
49  
50    @Override
51    public void setUp() throws Exception {
52      super.setUp();
53      johnDoe = new PersonImpl("johnDoeId", "Johnny", new NameImpl("John Doe"));
54      johnDoe.setPhoneNumbers(Lists.<ListField>newArrayList(
55          new ListFieldImpl("home", "+33H000000000"),
56          new ListFieldImpl("mobile", "+33M000000000"),
57          new ListFieldImpl("work", "+33W000000000")));
58  
59      johnDoe.setAddresses(Lists.<Address>newArrayList(new AddressImpl("My home address")));
60  
61      johnDoe.setEmails(Lists.<ListField>newArrayList(
62          new ListFieldImpl("work", "john.doe@work.bar"),
63          new ListFieldImpl("home", "john.doe@home.bar")));
64  
65      activity = new ActivityImpl("activityId", johnDoe.getId());
66  
67      activity.setMediaItems(Lists.<MediaItem>newArrayList(
68          new MediaItemImpl("image/jpg", MediaItem.Type.IMAGE, "http://foo.bar")));
69  
70      beanJsonConverter = new BeanJsonConverter(
71          Guice.createInjector(new SocialApiTestsGuiceModule()));
72    }
73  
74    public static class SpecialPerson extends PersonImpl {
75      private String newfield;
76  
77      public SpecialPerson(String id, String name, String newfield) {
78        super(id, name, new NameImpl(name));
79        this.newfield = newfield;
80      }
81  
82      public String getNewfield() {
83        return newfield;
84      }
85    }
86  
87    public void testToJsonOnInheritedClass() throws Exception {
88      SpecialPerson cassie = new SpecialPerson("5", "robot", "nonsense");
89  
90      JSONObject result = (JSONObject) beanJsonConverter.convertToJson(cassie);
91      assertEquals(cassie.getId(), result.getString("id"));
92      assertEquals(cassie.getNewfield(), result.getString("newfield"));
93    }
94  
95    public void testPersonToJson() throws Exception {
96      JSONObject result = (JSONObject) beanJsonConverter.convertToJson(johnDoe);
97  
98      assertEquals(johnDoe.getId(), result.getString("id"));
99  
100     assertEquals(johnDoe.getName().getUnstructured(),
101         result.getJSONObject("name").getString("unstructured"));
102 
103     assertEquals(johnDoe.getAddresses().get(0).getFormatted(),
104         result.getJSONArray("addresses").getJSONObject(0)
105             .getString("formatted"));
106 
107     JSONArray phoneArray = result.getJSONArray("phoneNumbers");
108     assertEquals(3, phoneArray.length());
109 
110     for (int i = 0; i < johnDoe.getPhoneNumbers().size(); i++) {
111       ListField expectedPhone = johnDoe.getPhoneNumbers().get(i);
112       JSONObject actualPhone = phoneArray.getJSONObject(i);
113       assertEquals(expectedPhone.getType(), actualPhone.getString("type"));
114       assertEquals(expectedPhone.getValue(), actualPhone.getString("value"));
115     }
116 
117     JSONArray emailArray = result.getJSONArray("emails");
118     assertEquals(2, emailArray.length());
119 
120     for (int i = 0; i < johnDoe.getEmails().size(); i++) {
121       ListField expectedEmail = johnDoe.getEmails().get(i);
122       JSONObject actualEmail = emailArray.getJSONObject(i);
123       assertEquals(expectedEmail.getType(), actualEmail.getString("type"));
124       assertEquals(expectedEmail.getValue(),
125           actualEmail.getString("value"));
126     }
127   }
128 
129   public void testActivityToJson() throws Exception {
130     JSONObject result = (JSONObject) beanJsonConverter.convertToJson(activity);
131 
132     assertEquals(activity.getUserId(), result.getString("userId"));
133     assertEquals(activity.getId(), result.getString("id"));
134 
135     JSONArray mediaItemsArray = result.getJSONArray("mediaItems");
136     assertEquals(1, mediaItemsArray.length());
137 
138     MediaItem expectedItem = activity.getMediaItems().get(0);
139     JSONObject actualItem = mediaItemsArray.getJSONObject(0);
140 
141     assertEquals(expectedItem.getUrl(), actualItem.getString("url"));
142     assertEquals(expectedItem.getMimeType(), actualItem.getString("mimeType"));
143     assertEquals(expectedItem.getType().toString(),
144         actualItem.getString("type"));
145   }
146 
147   public void testMapsToJson() throws Exception {
148     Map<String, Map<String, String>> map = Maps.newHashMap();
149 
150     Map<String, String> item1Map = Maps.newHashMap();
151     item1Map.put("value", "1");
152 
153     // Null values shouldn't cause exceptions
154     item1Map.put("value2", null);
155     map.put("item1", item1Map);
156 
157     Map<String, String> item2Map = Maps.newHashMap();
158     item2Map.put("value", "2");
159     map.put("item2", item2Map);
160 
161     JSONObject jsonMap = (JSONObject) beanJsonConverter.convertToJson(map);
162 
163     assertEquals("1", jsonMap.getJSONObject("item1").getString("value"));
164     assertEquals("2", jsonMap.getJSONObject("item2").getString("value"));
165   }
166 
167   public void testListsToJson() throws Exception {
168     Map<String, String> item1Map = Maps.newHashMap();
169     item1Map.put("value", "1");
170 
171     Map<String, String> item2Map = Maps.newHashMap();
172     item2Map.put("value", "2");
173 
174     JSONArray jsonArray = (JSONArray) beanJsonConverter.convertToJson(
175         Lists.newArrayList(item1Map, item2Map));
176 
177     assertEquals("1", ((JSONObject) jsonArray.get(0)).getString("value"));
178     assertEquals("2", ((JSONObject) jsonArray.get(1)).getString("value"));
179   }
180 
181   public void testArrayToJson() throws Exception {
182     String[] colors = {"blue", "green", "aquamarine"};
183     JSONArray jsonArray = (JSONArray) beanJsonConverter.convertToJson(colors);
184 
185     assertEquals(colors.length, jsonArray.length());
186     assertEquals(colors[0], jsonArray.get(0));
187   }
188 
189   public void testJsonToActivity() throws Exception {
190     String jsonActivity = "{userId : 5, id : 6, mediaItems : ["
191       + "{url : 'hello', mimeType : 'mimey', type : 'video'}"
192       + "]}";
193     // TODO: rename the enums to be lowercase
194     Activity result = beanJsonConverter.convertToObject(jsonActivity,
195         Activity.class);
196 
197     assertEquals("5", result.getUserId());
198     assertEquals("6", result.getId());
199 
200     assertEquals(1, result.getMediaItems().size());
201 
202     MediaItem actualItem = result.getMediaItems().get(0);
203 
204     assertEquals("hello", actualItem.getUrl());
205     assertEquals("mimey", actualItem.getMimeType());
206     assertEquals("video", actualItem.getType().toString());
207   }
208 
209   public void testJsonToMap() throws Exception {
210     String jsonActivity = "{count : 0, favoriteColor : 'yellow'}";
211     Map<String, String> data = Maps.newHashMap();
212     data = beanJsonConverter.convertToObject(jsonActivity,
213         (Class<Map<String, String>>) data.getClass());
214 
215     assertEquals(2, data.size());
216 
217     for (Entry<String, String> entry : data.entrySet()) {
218       String key = entry.getKey();
219       String value = entry.getValue();
220       if (key.equals("count")) {
221         assertEquals("0", value);
222       } else if (key.equals("favoriteColor")) {
223         assertEquals("yellow", value);
224       }
225     }
226   }
227 
228   public void testJsonToPerson() throws Exception {
229     String jsonPerson = "{age : '10', hasApp : 'true', isViewer : 'true'}";
230     Person result = beanJsonConverter.convertToObject(jsonPerson, Person.class);
231 
232     assertEquals(10, result.getAge().intValue());
233     assertEquals(true, result.getHasApp().booleanValue());
234     assertEquals(true, result.getIsViewer());
235   }
236 
237 }