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.common.xml.XmlException;
21  import org.apache.shindig.common.xml.XmlUtil;
22  import org.apache.shindig.social.SocialApiTestsGuiceModule;
23  import org.apache.shindig.social.core.model.ActivityImpl;
24  import org.apache.shindig.social.core.model.AddressImpl;
25  import org.apache.shindig.social.core.model.ListFieldImpl;
26  import org.apache.shindig.social.core.model.MediaItemImpl;
27  import org.apache.shindig.social.core.model.NameImpl;
28  import org.apache.shindig.social.core.model.PersonImpl;
29  import org.apache.shindig.social.core.util.BeanXStreamConverter;
30  import org.apache.shindig.social.core.util.xstream.GuiceBeanProvider;
31  import org.apache.shindig.social.core.util.xstream.XStream081Configuration;
32  import org.apache.shindig.social.opensocial.model.Activity;
33  import org.apache.shindig.social.opensocial.model.Address;
34  import org.apache.shindig.social.opensocial.model.ListField;
35  import org.apache.shindig.social.opensocial.model.MediaItem;
36  import org.apache.shindig.social.opensocial.model.Person;
37  import org.apache.shindig.social.opensocial.spi.DataCollection;
38  
39  import com.google.common.collect.Lists;
40  import com.google.common.collect.Maps;
41  import com.google.inject.Guice;
42  import com.google.inject.Injector;
43  
44  import org.apache.commons.lang.StringUtils;
45  import org.apache.commons.logging.Log;
46  import org.apache.commons.logging.LogFactory;
47  import org.w3c.dom.Element;
48  import org.w3c.dom.Node;
49  
50  import junit.framework.TestCase;
51  
52  import java.io.BufferedReader;
53  import java.io.IOException;
54  import java.io.InputStreamReader;
55  import java.util.ArrayList;
56  import java.util.LinkedHashMap;
57  import java.util.List;
58  import java.util.Map;
59  import java.util.TreeMap;
60  
61  public class BeanXStreamConverterTest extends TestCase {
62    private static final String XMLSCHEMA = " xmlns=\"http://ns.opensocial.org/2008/opensocial\" \n"
63        + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n"
64        + " xsi:schemaLocation=\"http://ns.opensocial.org/2008/opensocial classpath:opensocial.xsd\" ";
65    private static final String XSDRESOURCE = "opensocial.xsd";
66    private Person johnDoe;
67    private Activity activity;
68  
69    private BeanXStreamConverter beanXmlConverter;
70  
71    @Override
72    public void setUp() throws Exception {
73      super.setUp();
74      Injector injector = Guice.createInjector(new SocialApiTestsGuiceModule());
75      
76      johnDoe = new PersonImpl("johnDoeId", "Johnny", new NameImpl("John Doe"));
77      johnDoe.setPhoneNumbers(Lists.<ListField> newArrayList(new ListFieldImpl(
78          "home", "+33H000000000"), new ListFieldImpl("mobile", "+33M000000000"),
79          new ListFieldImpl("work", "+33W000000000")));
80  
81      johnDoe.setAddresses(Lists.<Address> newArrayList(new AddressImpl(
82          "My home address")));
83  
84      johnDoe.setEmails(Lists.<ListField> newArrayList(new ListFieldImpl("work",
85          "john.doe@work.bar"), new ListFieldImpl("home", "john.doe@home.bar")));
86  
87      activity = new ActivityImpl("activityId", johnDoe.getId());
88  
89      activity.setMediaItems(Lists.<MediaItem> newArrayList(new MediaItemImpl(
90          "image/jpg", MediaItem.Type.IMAGE, "http://foo.bar")));
91      
92  
93      beanXmlConverter = new BeanXStreamConverter(new XStream081Configuration(injector));
94    }
95    
96  
97    public static class SimplePerson {
98      private String id;
99      private String name;
100 
101     public SimplePerson(String id, String name) {
102       this.id = id;
103       this.name = name;
104     }
105 
106     public String getId() {
107       return id;
108     }
109 
110     public String getName() {
111       return name;
112     }
113   }
114 
115   public void testToXmlOnSimpleClass() throws Exception {
116     // since this doent implement the model, it wont get mapped correctly, hence
117     // we cant validate
118     SimplePerson cassie = new SimplePerson("5", "robot");
119     String xml = beanXmlConverter.convertToString(cassie);
120     Element element = XmlUtil.parse(xml);
121     Node id = element.getElementsByTagName("id").item(0);
122     Node name = element.getElementsByTagName("name").item(0);
123 
124     assertEquals("5", id.getTextContent());
125     assertEquals("robot", name.getTextContent());
126   }
127 
128   public void testPersonToXml() throws Exception {
129     String xml = XSDValidator.validate(beanXmlConverter.convertToString(johnDoe),
130         XMLSCHEMA, XSDRESOURCE, true);
131     Element element = XmlUtil.parse(xml);
132     Node id = element.getElementsByTagName("id").item(0);
133     assertEquals(johnDoe.getId(), id.getTextContent());
134   }
135 
136   public void testActivityToXml() throws Exception {
137     String xml = XSDValidator.validate(beanXmlConverter.convertToString(activity),
138         XMLSCHEMA, XSDRESOURCE, true);
139 
140     Element element = XmlUtil.parse(xml);
141     Node id = element.getElementsByTagName("id").item(0);
142     assertEquals(activity.getId(), id.getTextContent());
143   }
144 
145   public void testMapsToXml() throws Exception {
146     // This is the structure our app data currently takes
147     Map<String, Map<String, String>> map = new TreeMap<String, Map<String, String>>();
148 
149     Map<String, String> item1Map = Maps.newHashMap();
150     item1Map.put("value", "1");
151     map.put("item1", item1Map);
152 
153     Map<String, String> item2Map = Maps.newHashMap();
154     item2Map.put("value", "2");
155     map.put("item2", item2Map);
156 
157     String xml = beanXmlConverter.convertToString(map);
158 
159     XmlUtil.parse(xml);
160 
161     String expectedXml = "<response><map>"
162         + "  <entry><key>item1</key><value><entry><key>value</key><value>1</value></entry></value></entry> "
163         + "  <entry><key>item2</key><value><entry><key>value</key><value>2</value></entry></value></entry> "
164         + "</map></response>";
165     assertEquals(StringUtils.deleteWhitespace(expectedXml), StringUtils
166         .deleteWhitespace(xml));
167   }
168 
169   public void testMapToXml() throws XmlException {
170     Map<String, String> m = Maps.newLinkedHashMap();
171     m.put("key1", "value1");
172     m.put("key2", "value2");
173     String xml = beanXmlConverter.convertToString(m);
174     XmlUtil.parse(xml);
175     String expectedXml = "<response><map>"
176         + "  <entry><key>key1</key><value>value1</value></entry> "
177         + "  <entry><key>key2</key><value>value2</value></entry> "
178         + "</map></response>";
179     assertEquals(StringUtils.deleteWhitespace(expectedXml), StringUtils
180         .deleteWhitespace(xml));
181   }
182 
183   public void testEmptyList() throws XmlException {
184     List<String> empty = Lists.newArrayList();
185     String xml = beanXmlConverter.convertToString(empty);
186     XmlUtil.parse(xml);
187     String expectedXml = "<response><list/></response>";
188     assertEquals(StringUtils.deleteWhitespace(expectedXml), StringUtils
189         .deleteWhitespace(xml));
190 
191     List<List<String>> emptyLists = Lists.newArrayList();
192     List<String> emptyList = Lists.newArrayList();
193     emptyLists.add(emptyList);
194     emptyLists.add(emptyList);
195     emptyLists.add(emptyList);
196     xml = beanXmlConverter.convertToString(emptyLists);
197     XmlUtil.parse(xml);
198     expectedXml = "<response><list.container>" + "  <list/>" + "  <list/>"
199         + "  <list/>" + "</list.container></response>";
200     assertEquals(StringUtils.deleteWhitespace(expectedXml), StringUtils
201         .deleteWhitespace(xml));
202   }
203 
204   public void testElementNamesInList() throws XmlException {
205     List<Activity> activities = Lists.newArrayList();
206     activities.add(activity);
207     activities.add(activity);
208     activities.add(activity);
209     String xml = XSDValidator.validate(beanXmlConverter
210         .convertToString(activities), XMLSCHEMA, XSDRESOURCE, true);
211     XmlUtil.parse(xml);
212     String expectedXml = "<response>" + "<list.container>" + "  <activity>"
213         + "    <id>activityId</id>" + "    <mediaItems>"
214         + "        <mimeType>image/jpg</mimeType>"
215         + "        <type>IMAGE</type>" + "        <url>http://foo.bar</url>"
216         + "    </mediaItems>" + "    <userId>johnDoeId</userId>"
217         + "  </activity>" + "  <activity>" + "    <id>activityId</id>"
218         + "    <mediaItems>" + "        <mimeType>image/jpg</mimeType>"
219         + "        <type>IMAGE</type>" + "        <url>http://foo.bar</url>"
220         + "    </mediaItems>" + "    <userId>johnDoeId</userId>"
221         + "  </activity>" + "  <activity>" + "    <id>activityId</id>"
222         + "    <mediaItems>" + "        <mimeType>image/jpg</mimeType>"
223         + "        <type>IMAGE</type>" + "        <url>http://foo.bar</url>"
224         + "    </mediaItems>" + "    <userId>johnDoeId</userId>"
225         + "  </activity>" + "</list.container>" + "</response>";
226     expectedXml = XSDValidator.insertSchema(expectedXml, XMLSCHEMA, true);
227     assertEquals(StringUtils.deleteWhitespace(expectedXml), StringUtils
228         .deleteWhitespace(xml));
229   }
230 
231   public void testPerson1() throws XmlException, IOException {
232     String xml = loadXML("testxml/person1.xml");
233     beanXmlConverter.convertToObject(xml, Person.class);
234   }
235 
236   public void testActivity1() throws XmlException, IOException {
237     String xml = loadXML("testxml/activity1.xml");
238     beanXmlConverter.convertToObject(xml, Activity.class);
239   }
240 
241   public void testAppdata1() throws XmlException, IOException {
242     String xml = loadXML("testxml/appdata1.xml");
243     beanXmlConverter.convertToObject(xml, Map.class);
244   }
245 
246   public void testGroup1() throws XmlException {
247     // TODO
248   }
249 
250   /***
251    * @param string
252    * @return
253    * @throws IOException
254    */
255   private String loadXML(String resource) throws IOException {
256     BufferedReader in = new BufferedReader(new InputStreamReader(this
257         .getClass().getResourceAsStream(resource)));
258     StringBuilder sb = new StringBuilder();
259     for (String line = in.readLine(); line != null; line = in.readLine()) {
260       sb.append(line);
261     }
262     in.close();
263     return sb.toString();
264   }
265 
266 }