Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.hubspot.slack.client.models.response.views.ViewResponseBase;
import java.util.List;
import java.util.Optional;
import javax.annotation.Nullable;
import org.immutables.value.Value.Default;
import org.immutables.value.Value.Immutable;

@Immutable
Expand Down Expand Up @@ -43,6 +45,8 @@ default String getActionTs() {
}

@Override
@Default
@Nullable
default SlackChannel getChannel() {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
package com.hubspot.slack.client.models.interaction;

import static org.assertj.core.api.Assertions.assertThat;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.hubspot.slack.client.SerializationTestBase;
import com.hubspot.slack.client.jackson.ObjectMapperUtils;
import com.hubspot.slack.client.models.JsonLoader;
import com.hubspot.slack.client.models.SlackChannel;
import java.io.IOException;
import org.junit.Test;

public class BlockActionsSerializationTest extends SerializationTestBase {

private static final ObjectMapper MAPPER = ObjectMapperUtils.mapper();

@Test
public void testBlockSerialization() throws IOException {
testSerialization("block_actions.json", BlockActions.class);
Expand All @@ -30,4 +40,95 @@ public void testBlockSerializationWithMessageContainer() throws IOException {
public void testBlockSerializationWithViewContainer() throws IOException {
testSerialization("block_actions_with_view_container.json", BlockActions.class);
}

@Test
public void testBlockDeserializationWithChannel() throws IOException {
ObjectNode json = blockActionsPayload();
json.putObject("channel").put("id", "C123").put("name", "general");

BlockActions actions = MAPPER.treeToValue(json, BlockActions.class);

assertThat(actions.getChannel()).isNotNull();
assertThat(actions.getChannel().getId()).isEqualTo("C123");
assertThat(actions.getChannel().getName()).isEqualTo("general");
JsonNode serialized = MAPPER.valueToTree(actions);
assertThat(serialized.path("channel").path("id").asText()).isEqualTo("C123");
assertThat(serialized.path("channel").path("name").asText()).isEqualTo("general");
assertThat(MAPPER.treeToValue(serialized, BlockActions.class)).isEqualTo(actions);
}

@Test
public void testCallbackDeserializationWithChannel() throws IOException {
ObjectNode json = blockActionsPayload();
json.putObject("channel").put("id", "C123").put("name", "general");

SlackInteractiveCallback callback = MAPPER.treeToValue(
json,
SlackInteractiveCallback.class
);

assertThat(callback).isInstanceOf(BlockActions.class);
assertThat(callback.getChannel()).isNotNull();
assertThat(callback.getChannel().getId()).isEqualTo("C123");
assertThat(callback.getChannel().getName()).isEqualTo("general");
JsonNode serialized = MAPPER.valueToTree(callback);
assertThat(serialized.path("channel").path("id").asText()).isEqualTo("C123");
assertThat(serialized.path("channel").path("name").asText()).isEqualTo("general");
assertThat(MAPPER.treeToValue(serialized, SlackInteractiveCallback.class))
.isEqualTo(callback);
}

@Test
public void testBlockDeserializationWithoutChannel() throws IOException {
ObjectNode json = blockActionsPayload();
assertThat(json.path("container").path("channel_id").asText()).isNotEmpty();

BlockActions actions = MAPPER.treeToValue(json, BlockActions.class);

assertThat(actions.getChannel()).isNull();
JsonNode serialized = MAPPER.valueToTree(actions);
assertThat(serialized.has("channel")).isFalse();
assertThat(serialized).isEqualTo(json);
}

@Test
public void testBlockDeserializationWithNullChannel() throws IOException {
ObjectNode json = blockActionsPayload();
json.putNull("channel");

BlockActions actions = MAPPER.treeToValue(json, BlockActions.class);

assertThat(actions.getChannel()).isNull();
JsonNode serialized = MAPPER.valueToTree(actions);
assertThat(serialized.has("channel")).isFalse();
assertThat(serialized).isEqualTo(blockActionsPayload());
}

@Test
public void testBlockBuilderCopiesChannel() throws IOException {
BlockActions original = MAPPER.treeToValue(blockActionsPayload(), BlockActions.class);
SlackChannel channel = SlackChannel
.builder()
.setId("C123")
.setName("general")
.build();

BlockActions actions = BlockActions
.builder()
.from(original)
.setChannel(channel)
.build();
BlockActions copy = BlockActions.builder().from(actions).build();

assertThat(actions.getChannel()).isEqualTo(channel);
assertThat(copy.getChannel()).isEqualTo(channel);
assertThat(copy).isEqualTo(actions);
assertThat(original.getChannel()).isNull();
}

private static ObjectNode blockActionsPayload() throws IOException {
return (ObjectNode) MAPPER.readTree(
JsonLoader.loadJsonFromFile("block_actions_with_message_container.json")
);
}
}