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.XmlUtil;
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.BeanXmlConverter;
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 junit.framework.TestCase;
37  import org.apache.commons.lang.StringUtils;
38  import org.w3c.dom.Element;
39  import org.w3c.dom.Node;
40  
41  import java.util.Map;
42  import java.util.TreeMap;
43  
44  public class BeanXmlConverterTest extends TestCase {
45    private Person johnDoe;
46    private Activity activity;
47  
48    private BeanXmlConverter beanXmlConverter;
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      beanXmlConverter = new BeanXmlConverter();
71    }
72  
73    public static class SimplePerson {
74      private String id;
75      private String name;
76  
77      public SimplePerson(String id, String name) {
78        this.id = id;
79        this.name = name;
80      }
81  
82      public String getId() {
83        return id;
84      }
85  
86      public String getName() {
87        return name;
88      }
89    }
90  
91    public void testToXmlOnSimpleClass() throws Exception {
92      SimplePerson cassie = new SimplePerson("5", "robot");
93      String xml = beanXmlConverter.convertToXml(cassie);
94  
95      Element element = XmlUtil.parse(xml);
96      Node id = element.getElementsByTagName("id").item(0);
97      Node name = element.getElementsByTagName("name").item(0);
98  
99      assertEquals("5", id.getTextContent());
100     assertEquals("robot", name.getTextContent());
101   }
102 
103   public void testPersonToXml() throws Exception {
104     String xml = beanXmlConverter.convertToXml(johnDoe);
105     // TODO: Make the person xml stop returning empty elements!
106     // TODO: Flush out the test to check all the sub fields
107     Element element = XmlUtil.parse(xml);
108     Node id = element.getElementsByTagName("id").item(0);
109     assertEquals(johnDoe.getId(), id.getTextContent());
110   }
111 
112   public void testActivityToXml() throws Exception {
113     String xml = beanXmlConverter.convertToXml(activity);
114     // TODO: Make the activity xml stop returning empty elements!
115     // TODO: Flush out the test to check all the sub fields
116     Element element = XmlUtil.parse(xml);
117     Node id = element.getElementsByTagName("id").item(0);
118     assertEquals(activity.getId(), id.getTextContent());
119   }
120 
121   public void xxxtestMapsToXml() throws Exception {
122     // This is the structure our app data currently takes
123     Map<String, Map<String, String>> map =
124         new TreeMap<String, Map<String, String>>();
125 
126     Map<String, String> item1Map = Maps.newHashMap();
127     item1Map.put("value", "1");
128     map.put("item1", item1Map);
129 
130     Map<String, String> item2Map = Maps.newHashMap();
131     item2Map.put("value", "2");
132     map.put("item2", item2Map);
133 
134     String xml = beanXmlConverter.convertToXml(map);
135 
136     // TODO: Change this test to use parsing once we have the right format
137     XmlUtil.parse(xml);
138 
139     // TODO: I don't believe this is the output we are looking for for app
140     // data... we will probably have to tweak this.
141     String expectedXml =
142         "<treemap>" +
143         "<empty>false</empty>" +
144         "<entry>" +
145           "<key>item1</key>" +
146           "<value>" +
147             "<empty>false</empty>" +
148             "<entry>" +
149               "<key>value</key>" +
150               "<value>1</value>" +
151             "</entry>" +
152           "</value>" +
153         "</entry>" +
154         "<entry>" +
155           "<key>item2</key>" +
156           "<value>" +
157             "<empty>false</empty>" +
158             "<entry>" +
159               "<key>value</key>" +
160               "<value>2</value>" +
161             "</entry>" +
162           "</value>" +
163         "</entry>" +
164         "</treemap>";
165     assertEquals(expectedXml, StringUtils.deleteWhitespace(xml));
166   }
167 
168 }