Commit 5c32dd941edfe90d48be434149ac0d3f9ee8a16a
Simplify userStatus invocation.
| | | | | 86 | String updateStatus(String message, FacebookLink link); | 86 | String updateStatus(String message, FacebookLink link); |
|---|
| 87 | | 87 | |
|---|
| 88 | /** | 88 | /** |
|---|
| 89 | * Posts a message to the current user's wall along with a link. | | * Posts a message to the current user's wall along with a link. |
|---|
| 90 | * | | * |
|---|
| 91 | * @param message | | * @param message |
|---|
| 92 | * The message to post | | * The message to post |
|---|
| 93 | * @param link | | * @param link |
|---|
| 94 | * A link to be included in the status update, can be null. | | * A link to be included in the status update, can be null. |
|---|
| 95 | * @param fetchPostId | | * @param fetchPostId |
|---|
| 96 | * A flag to indicate if we actually fetch post id. | | * A flag to indicate if we actually fetch post id. |
|---|
| 97 | * @return post id or null if fetchPostId equals false | | * @return post id or null if fetchPostId equals false |
|---|
| 98 | */ | | */ |
|---|
| 99 | String updateStatus(String message, FacebookLink link, boolean fetchPostId); | | 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 object | 90 | * Low-level publish-to-Facebook method for publishing any type of object |
|---|
| 104 | * supported by Facebook's API. | 91 | * supported by Facebook's API. |
|---|
| | | | | 125 | } | 125 | } |
|---|
| 126 | | 126 | |
|---|
| 127 | public String updateStatus(String message, FacebookLink link) { | 127 | public String updateStatus(String message, FacebookLink link) { |
|---|
| 128 | return updateStatus(message, link, true); | | return updateStatus(message, link, true); |
|---|
| 129 | } | | } |
|---|
| 130 | | | |
|---|
| 131 | public String updateStatus(String message, FacebookLink link, boolean fetchPostId) { | | 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); | | publish(CURRENT_USER, FEED, map); |
|---|
| 137 | | | |
|---|
| 138 | if (fetchPostId) { | | if (fetchPostId) { |
|---|
| 139 | @SuppressWarnings("rawtypes") | | @SuppressWarnings("rawtypes") |
|---|
| 140 | ResponseEntity<Map> response = restOperations.getForEntity(CONNECTION_URL, Map.class, CURRENT_USER, FEED, accessToken); | | ResponseEntity<Map> response = restOperations.getForEntity(CONNECTION_URL, Map.class, CURRENT_USER, FEED, accessToken); |
|---|
| 141 | @SuppressWarnings("unchecked") | | @SuppressWarnings("unchecked") |
|---|
| 142 | Map<String, List<Map<String, String>>> resultsMap = response.getBody(); | | Map<String, List<Map<String, String>>> resultsMap = response.getBody(); |
|---|
| 143 | List<Map<String, String>> posts = resultsMap.get("data"); | | List<Map<String, String>> posts = resultsMap.get("data"); |
|---|
| 144 | return posts.get(0).get("id"); // Should exist, as we just posted | | return posts.get(0).get("id"); // Should exist, as we just posted |
|---|
| 145 | } | | } |
|---|
| 146 | else { | | else { |
|---|
| 147 | return null; | | 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 | } |
|---|
| 150 | | 139 | |
|---|
| 151 | public void publish(String object, String connection, MultiValueMap<String, String> data) { | 140 | public void publish(String object, String connection, MultiValueMap<String, String> data) { |
|---|
| | | | | | 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 | */ |
|---|
| | 16 | package org.springframework.social.facebook; |
|---|
| | 17 | |
|---|
| | 18 | import org.codehaus.jackson.annotate.JsonIgnoreProperties; |
|---|
| | 19 | import org.codehaus.jackson.annotate.JsonProperty; |
|---|
| | 20 | |
|---|
| | 21 | /** |
|---|
| | 22 | * Model class containing a post id. |
|---|
| | 23 | * |
|---|
| | 24 | * @author Ales Justin |
|---|
| | 25 | */ |
|---|
| | 26 | @JsonIgnoreProperties(ignoreUnknown=true) |
|---|
| | 27 | public 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 | } |
|---|
| | | | | 27 | import org.springframework.util.MultiValueMap; | 27 | import org.springframework.util.MultiValueMap; |
|---|
| 28 | import org.springframework.web.client.RestOperations; | 28 | import org.springframework.web.client.RestOperations; |
|---|
| 29 | import static java.util.Collections.singletonMap; | 29 | import static java.util.Collections.singletonMap; |
|---|
| 30 | import static org.junit.Assert.*; | | import static org.junit.Assert.*; |
|---|
| | 30 | import static org.junit.Assert.assertEquals; | | | 31 | import static org.junit.Assert.assertThat; |
|---|
| 31 | import static org.junit.internal.matchers.IsCollectionContaining.hasItem; | 32 | import static org.junit.internal.matchers.IsCollectionContaining.hasItem; |
|---|
| 32 | import static org.mockito.Matchers.eq; | 33 | import static org.mockito.Matchers.eq; |
|---|
| 33 | import static org.mockito.Mockito.*; | 34 | import static org.mockito.Mockito.*; |
|---|
| … | | … | |
|---|
| 121 | | 121 | |
|---|
| 122 | @Test | 122 | @Test |
|---|
| 123 | public void updateStatus() { | 123 | public void updateStatus() { |
|---|
| 124 | Map<String, List<Map<String, String>>> resultsMap = new HashMap<String, List<Map<String, String>>>(); | | 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>>(); | | List<Map<String, String>> postsList = new ArrayList<Map<String, String>>(); |
|---|
| 126 | postsList.add(singletonMap("id", "12345")); | | postsList.add(singletonMap("id", "12345")); |
|---|
| 127 | postsList.add(singletonMap("id", "67890")); | | postsList.add(singletonMap("id", "67890")); |
|---|
| 128 | resultsMap.put("data", postsList); | | resultsMap.put("data", postsList); |
|---|
| | 124 | MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); | | | 125 | map.set("message", "Hello Facebook!"); |
|---|
| 129 | | 126 | |
|---|
| 130 | ResponseEntity<Map> response = new ResponseEntity<Map>(resultsMap, OK); | | ResponseEntity<Map> response = new ResponseEntity<Map>(resultsMap, OK); |
|---|
| 131 | when(restOperations.getForEntity(eq(CONNECTION_URL), eq(Map.class), eq(CURRENT_USER), eq(FEED), | | 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); |
|---|
| 133 | | 132 | |
|---|
| 134 | assertEquals(facebook.updateStatus("Hello Facebook!"), "12345"); | 133 | assertEquals(facebook.updateStatus("Hello Facebook!"), "12345"); |
|---|
| 135 | | 134 | |
|---|
| 136 | MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); | | MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); |
|---|
| 137 | map.set("message", "Hello Facebook!"); | | map.set("message", "Hello Facebook!"); |
|---|
| 138 | verify(restOperations).postForLocation(eq(CONNECTION_URL), eq(map), eq(CURRENT_USER), eq(FEED), | | 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 | } |
|---|
| 141 | | 138 | |
|---|
| 142 | @Test | 139 | @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>>>(); | | 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>>(); | | List<Map<String, String>> postsList = new ArrayList<Map<String, String>>(); |
|---|
| 146 | postsList.add(singletonMap("id", "12345")); | | postsList.add(singletonMap("id", "12345")); |
|---|
| 147 | postsList.add(singletonMap("id", "67890")); | | postsList.add(singletonMap("id", "67890")); |
|---|
| 148 | resultsMap.put("data", postsList); | | 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."; |
|---|
| 149 | | 145 | |
|---|
| 150 | ResponseEntity<Map> response = new ResponseEntity<Map>(resultsMap, OK); | | ResponseEntity<Map> response = new ResponseEntity<Map>(resultsMap, OK); |
|---|
| 151 | when(restOperations.getForEntity(eq(CONNECTION_URL), eq(Map.class), eq(CURRENT_USER), eq(FEED), | | 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); |
|---|
| 153 | | 158 | |
|---|
| 154 | String linkUrl = "http://www.springsource.com"; | | String linkUrl = "http://www.springsource.com"; |
|---|
| 155 | String linkName = "SpringSource"; | | String linkName = "SpringSource"; |
|---|
| 156 | String linkCaption = "SpringSource Home Page"; | | String linkCaption = "SpringSource Home Page"; |
|---|
| 157 | String linkDescription = "SpringSource is the leader in Java application and infrastructure management."; | | 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"); |
|---|
| 161 | | 161 | |
|---|
| 162 | MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); | | MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); |
|---|
| 163 | map.set("message", "Hello Facebook!"); | | map.set("message", "Hello Facebook!"); |
|---|
| 164 | map.set("link", linkUrl); | | map.set("link", linkUrl); |
|---|
| 165 | map.set("name", linkName); | | map.set("name", linkName); |
|---|
| 166 | map.set("caption", linkCaption); | | map.set("caption", linkCaption); |
|---|
| 167 | map.set("description", linkDescription); | | map.set("description", linkDescription); |
|---|
| 168 | verify(restOperations).postForLocation(eq(CONNECTION_URL), eq(map), eq(CURRENT_USER), eq(FEED), | | verify(restOperations).postForLocation(eq(CONNECTION_URL), eq(map), eq(CURRENT_USER), eq(FEED), |
|---|
| 169 | eq(ACCESS_TOKEN)); | | eq(ACCESS_TOKEN)); |
|---|
| 170 | } | | } |
|---|
| 171 | | | |
|---|
| 172 | @Test | | @Test |
|---|
| 173 | public void updateStatus_withNullLink_and_noPostIdFetch() { | | public void updateStatus_withNullLink_and_noPostIdFetch() { |
|---|
| 174 | assertNull(facebook.updateStatus("Hello Facebook!", null, false)); | | assertNull(facebook.updateStatus("Hello Facebook!", null, false)); |
|---|
| 175 | | | |
|---|
| 176 | MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); | | MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); |
|---|
| 177 | map.set("message", "Hello Facebook!"); | | map.set("message", "Hello Facebook!"); |
|---|
| 178 | verify(restOperations).postForLocation(eq(CONNECTION_URL), eq(map), eq(CURRENT_USER), eq(FEED), eq(ACCESS_TOKEN)); | | 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 | } |
|---|
| 180 | | 165 | |
|---|
| 181 | private FacebookProfile setupRestOperationsForGettingProfile() { | 166 | private FacebookProfile setupRestOperationsForGettingProfile() { |
|---|
Add a new comment:
Login or create an account to post a comment