|
| 1 | +package test_locally.api.util.json; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import com.google.gson.JsonObject; |
| 5 | +import com.slack.api.methods.response.slack_lists.SlackListsItemsListResponse; |
| 6 | +import com.slack.api.model.list.ListRecord; |
| 7 | +import com.slack.api.util.json.GsonFactory; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +import static org.hamcrest.CoreMatchers.is; |
| 11 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 12 | + |
| 13 | +public class GsonListRecordLinkFieldTest { |
| 14 | + |
| 15 | + private final Gson gson = GsonFactory.createSnakeCase(); |
| 16 | + |
| 17 | + @Test |
| 18 | + public void deserializeCamelCaseLinkField() { |
| 19 | + String json = "{\"ok\":true,\"items\":[{\"fields\":[{" + |
| 20 | + "\"link\":[{" + |
| 21 | + "\"originalUrl\":\"https://github.com/org/repo/pull/7\"," + |
| 22 | + "\"displayAsUrl\":false," + |
| 23 | + "\"displayName\":\"repo#7\"" + |
| 24 | + "}]}]}]}"; |
| 25 | + |
| 26 | + SlackListsItemsListResponse response = gson.fromJson(json, SlackListsItemsListResponse.class); |
| 27 | + ListRecord.LinkField linkField = response.getItems().get(0).getFields().get(0).getLink().get(0); |
| 28 | + |
| 29 | + assertThat(linkField.getOriginalUrl(), is("https://github.com/org/repo/pull/7")); |
| 30 | + assertThat(linkField.getDisplayAsUrl(), is(false)); |
| 31 | + assertThat(linkField.getDisplayName(), is("repo#7")); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void serializeLinkFieldAsSnakeCase() { |
| 36 | + ListRecord.LinkField linkField = ListRecord.LinkField.builder() |
| 37 | + .originalUrl("https://github.com/org/repo/pull/7") |
| 38 | + .displayAsUrl(false) |
| 39 | + .displayName("repo#7") |
| 40 | + .build(); |
| 41 | + |
| 42 | + JsonObject json = gson.toJsonTree(linkField).getAsJsonObject(); |
| 43 | + |
| 44 | + assertThat(json.get("original_url").getAsString(), is("https://github.com/org/repo/pull/7")); |
| 45 | + assertThat(json.get("display_as_url").getAsBoolean(), is(false)); |
| 46 | + assertThat(json.get("display_name").getAsString(), is("repo#7")); |
| 47 | + assertThat(json.has("originalUrl"), is(false)); |
| 48 | + assertThat(json.has("displayAsUrl"), is(false)); |
| 49 | + assertThat(json.has("displayName"), is(false)); |
| 50 | + } |
| 51 | +} |
0 commit comments