Commit 5c32dd941edfe90d48be434149ac0d3f9ee8a16a

  • avatar
  • alesj
  • Sun Nov 21 01:04:19 GMT 2010
Simplify userStatus invocation.
  • Diff rendering mode:
  • inline
  • side by side

spring-social-core/src/main/java/org/springframework/social/facebook/FacebookOperations.java

86 String updateStatus(String message, FacebookLink link);86 String updateStatus(String message, FacebookLink link);
8787
88 /**88 /**
89 * Posts a message to the current user's wall along with a link.
90 *
91 * @param message
92 * The message to post
93 * @param link
94 * A link to be included in the status update, can be null.
95 * @param fetchPostId
96 * A flag to indicate if we actually fetch post id.
97 * @return post id or null if fetchPostId equals false
98 */
99 String updateStatus(String message, FacebookLink link, boolean fetchPostId);
100
101 /**
102 * <p>89 * <p>
103 * Low-level publish-to-Facebook method for publishing any type of object90 * Low-level publish-to-Facebook method for publishing any type of object
104 * supported by Facebook's API.91 * supported by Facebook's API.

spring-social-core/src/main/java/org/springframework/social/facebook/FacebookTemplate.java

125 }125 }
126126
127 public String updateStatus(String message, FacebookLink link) {127 public String updateStatus(String message, FacebookLink link) {
128 return updateStatus(message, link, true);
129 }
130
131 public String updateStatus(String message, FacebookLink link, boolean fetchPostId) {
132 MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();128 MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
133 map.set("message", message);129 map.set("message", message);
134 if (link != null) {130 if (link != null) {
133 map.set("caption", link.getCaption());133 map.set("caption", link.getCaption());
134 map.set("description", link.getDescription());134 map.set("description", link.getDescription());
135 }135 }
136 publish(CURRENT_USER, FEED, map);
137
138 if (fetchPostId) {
139 @SuppressWarnings("rawtypes")
140 ResponseEntity<Map> response = restOperations.getForEntity(CONNECTION_URL, Map.class, CURRENT_USER, FEED, accessToken);
141 @SuppressWarnings("unchecked")
142 Map<String, List<Map<String, String>>> resultsMap = response.getBody();
143 List<Map<String, String>> posts = resultsMap.get("data");
144 return posts.get(0).get("id"); // Should exist, as we just posted
145 }
146 else {
147 return null;
148 }
136 ResponseEntity<PostId> postId = restOperations.postForEntity(CONNECTION_URL, map, PostId.class, CURRENT_USER, FEED, accessToken);
137 return postId.getBody().getId();
149 }138 }
150139
151 public void publish(String object, String connection, MultiValueMap<String, String> data) {140 public void publish(String object, String connection, MultiValueMap<String, String> data) {

spring-social-core/src/main/java/org/springframework/social/facebook/PostId.java

1/*
2 * Copyright 2010 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.springframework.social.facebook;
17
18import org.codehaus.jackson.annotate.JsonIgnoreProperties;
19import org.codehaus.jackson.annotate.JsonProperty;
20
21/**
22 * Model class containing a post id.
23 *
24 * @author Ales Justin
25 */
26@JsonIgnoreProperties(ignoreUnknown=true)
27public class PostId
28{
29 @JsonProperty
30 String id;
31
32 /**
33 * The post id.
34 *
35 * @return The post id.
36 */
37 public String getId() {
38 return id;
39 }
40
41 /**
42 * Set post id.
43 * Should be mostly used for testing.
44 *
45 * @param id the post id
46 */
47 public void setId(String id) {
48 this.id = id;
49 }
50}

spring-social-core/src/test/java/org/springframework/social/facebook/FacebookTemplateTest.java

27import org.springframework.util.MultiValueMap;27import org.springframework.util.MultiValueMap;
28import org.springframework.web.client.RestOperations;28import org.springframework.web.client.RestOperations;
29import static java.util.Collections.singletonMap;29import static java.util.Collections.singletonMap;
30import static org.junit.Assert.*;
30import static org.junit.Assert.assertEquals;
31import static org.junit.Assert.assertThat;
31import static org.junit.internal.matchers.IsCollectionContaining.hasItem;32import static org.junit.internal.matchers.IsCollectionContaining.hasItem;
32import static org.mockito.Matchers.eq;33import static org.mockito.Matchers.eq;
33import static org.mockito.Mockito.*;34import static org.mockito.Mockito.*;
121121
122 @Test122 @Test
123 public void updateStatus() {123 public void updateStatus() {
124 Map<String, List<Map<String, String>>> resultsMap = new HashMap<String, List<Map<String, String>>>();
125 List<Map<String, String>> postsList = new ArrayList<Map<String, String>>();
126 postsList.add(singletonMap("id", "12345"));
127 postsList.add(singletonMap("id", "67890"));
128 resultsMap.put("data", postsList);
124 MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
125 map.set("message", "Hello Facebook!");
129126
130 ResponseEntity<Map> response = new ResponseEntity<Map>(resultsMap, OK);
131 when(restOperations.getForEntity(eq(CONNECTION_URL), eq(Map.class), eq(CURRENT_USER), eq(FEED),
127 PostId postId = new PostId();
128 postId.setId("12345");
129 ResponseEntity<PostId> response = new ResponseEntity<PostId>(postId, OK);
130 when(restOperations.postForEntity(eq(CONNECTION_URL), eq(map), eq(PostId.class), eq(CURRENT_USER), eq(FEED),
132 eq(ACCESS_TOKEN))).thenReturn(response);131 eq(ACCESS_TOKEN))).thenReturn(response);
133132
134 assertEquals(facebook.updateStatus("Hello Facebook!"), "12345");133 assertEquals(facebook.updateStatus("Hello Facebook!"), "12345");
135134
136 MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
137 map.set("message", "Hello Facebook!");
138 verify(restOperations).postForLocation(eq(CONNECTION_URL), eq(map), eq(CURRENT_USER), eq(FEED),
135 verify(restOperations).postForEntity(eq(CONNECTION_URL), eq(map), eq(PostId.class), eq(CURRENT_USER), eq(FEED),
139 eq(ACCESS_TOKEN));136 eq(ACCESS_TOKEN));
140 }137 }
141138
142 @Test139 @Test
143 public void updateStatus_withLink() {140 public void updateStatus_withLink() {
144 Map<String, List<Map<String, String>>> resultsMap = new HashMap<String, List<Map<String, String>>>();
145 List<Map<String, String>> postsList = new ArrayList<Map<String, String>>();
146 postsList.add(singletonMap("id", "12345"));
147 postsList.add(singletonMap("id", "67890"));
148 resultsMap.put("data", postsList);
141 String linkUrl = "http://www.springsource.com";
142 String linkName = "SpringSource";
143 String linkCaption = "SpringSource Home Page";
144 String linkDescription = "SpringSource is the leader in Java application and infrastructure management.";
149145
150 ResponseEntity<Map> response = new ResponseEntity<Map>(resultsMap, OK);
151 when(restOperations.getForEntity(eq(CONNECTION_URL), eq(Map.class), eq(CURRENT_USER), eq(FEED),
146 MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
147 map.set("message", "Hello Facebook!");
148 map.set("link", linkUrl);
149 map.set("name", linkName);
150 map.set("caption", linkCaption);
151 map.set("description", linkDescription);
152
153 PostId postId = new PostId();
154 postId.setId("12345");
155 ResponseEntity<PostId> response = new ResponseEntity<PostId>(postId, OK);
156 when(restOperations.postForEntity(eq(CONNECTION_URL), eq(map), eq(PostId.class), eq(CURRENT_USER), eq(FEED),
152 eq(ACCESS_TOKEN))).thenReturn(response);157 eq(ACCESS_TOKEN))).thenReturn(response);
153158
154 String linkUrl = "http://www.springsource.com";
155 String linkName = "SpringSource";
156 String linkCaption = "SpringSource Home Page";
157 String linkDescription = "SpringSource is the leader in Java application and infrastructure management.";
158
159 FacebookLink link = new FacebookLink(linkUrl, linkName, linkCaption, linkDescription);159 FacebookLink link = new FacebookLink(linkUrl, linkName, linkCaption, linkDescription);
160 assertEquals(facebook.updateStatus("Hello Facebook!", link), "12345");160 assertEquals(facebook.updateStatus("Hello Facebook!", link), "12345");
161161
162 MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
163 map.set("message", "Hello Facebook!");
164 map.set("link", linkUrl);
165 map.set("name", linkName);
166 map.set("caption", linkCaption);
167 map.set("description", linkDescription);
168 verify(restOperations).postForLocation(eq(CONNECTION_URL), eq(map), eq(CURRENT_USER), eq(FEED),
169 eq(ACCESS_TOKEN));
170 }
171
172 @Test
173 public void updateStatus_withNullLink_and_noPostIdFetch() {
174 assertNull(facebook.updateStatus("Hello Facebook!", null, false));
175
176 MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
177 map.set("message", "Hello Facebook!");
178 verify(restOperations).postForLocation(eq(CONNECTION_URL), eq(map), eq(CURRENT_USER), eq(FEED), eq(ACCESS_TOKEN));
162 verify(restOperations).postForEntity(eq(CONNECTION_URL), eq(map), eq(PostId.class), eq(CURRENT_USER), eq(FEED),
163 eq(ACCESS_TOKEN));
179 }164 }
180165
181 private FacebookProfile setupRestOperationsForGettingProfile() {166 private FacebookProfile setupRestOperationsForGettingProfile() {

Comments

Add a new comment:

Login or create an account to post a comment

Add your comment