Skip to content

Commit

Permalink
added getpermalink method, group, mocks, extensions and dtos
Browse files Browse the repository at this point in the history
  • Loading branch information
BenDenger committed Aug 15, 2019
1 parent cb7142e commit 5e57123
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,48 @@ package com.kreait.slack.api.group.chat

interface ChatMethodGroup {

//TODO DOC
/**
* Deletes a Message from the chat
* https://api.slack.com/methods/chat.delete
*/
fun delete(authToken: String): ChatDeleteMethod

//TODO DOC
fun getPermalink(authToken: String): GetChatPermalinkMethod
/**
* Retrieve a permalink URL for a specific extant message
* https://api.slack.com/methods/chat.getPermalink
*/
fun getPermalink(authToken: String): ChatGetPermalinkMethod

//TODO DOC
/**
* Shares a me message into a channel.
* https://api.slack.com/methods/chat.meMessage
*/
fun meMessage(authToken: String): ChatMeMessageMethod

//TODO DOC
/**
* Sends an ephemeral message a channel
* Ephemeral messages are not persisted in the slack-database and can't be updated
* If possible, use slackclient.respond() with the response-url of e.g. a slack-command in order to be context independent
* https://api.slack.com/methods/chat.postEphemeral
*/
fun postEphemeral(authToken: String): ChatPostEphemeralMethod

//TODO DOC
/**
* sends a message to a channel
* https://api.slack.com/methods/chat.postMessage
*/
fun postMessage(authToken: String): ChatPostMessageMethod

//TODO DOC
/**
* Provide custom unfurl behavior for user-posted URLs
* https://api.slack.com/methods/chat.unfurl
*/
fun unfurl(authToken: String): ChatUnfurlMethod

//TODO DOC
/**
* Updates a message.
* hint: ephemeral messages can't be updated, since they are not persisted in the Slack-Database
* https://api.slack.com/methods/chat.update
*/
fun update(authToken: String): ChatUpdateMethod
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.kreait.slack.api.group.chat

//TODO Implement
abstract class GetChatPermalinkMethod {
import com.kreait.slack.api.contract.jackson.group.chat.ChatGetPermalinkRequest
import com.kreait.slack.api.contract.jackson.group.chat.ErrorChatGetPermalinkResponse
import com.kreait.slack.api.contract.jackson.group.chat.SuccessfulChatGetPermalinkResponse
import com.kreait.slack.api.group.ApiCallMethod

abstract class ChatGetPermalinkMethod : ApiCallMethod<ChatGetPermalinkMethod, SuccessfulChatGetPermalinkResponse, ErrorChatGetPermalinkResponse, ChatGetPermalinkRequest>() {

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.kreait.slack.api.spring.group.chat


import com.kreait.slack.api.contract.jackson.group.chat.ChatGetPermalinkResponse
import com.kreait.slack.api.contract.jackson.group.chat.ErrorChatGetPermalinkResponse
import com.kreait.slack.api.contract.jackson.group.chat.SuccessfulChatGetPermalinkResponse
import com.kreait.slack.api.group.ApiCallResult
import com.kreait.slack.api.group.chat.ChatGetPermalinkMethod
import com.kreait.slack.api.spring.group.RestTemplateFactory
import com.kreait.slack.api.spring.group.SlackRequestBuilder
import org.springframework.web.client.RestTemplate


@Suppress("UNCHECKED_CAST")
class DefaultGetPermalinkMethod(private val authToken: String, private val restTemplate: RestTemplate = RestTemplateFactory.slackTemplate()) : ChatGetPermalinkMethod() {

override fun request(): ApiCallResult<SuccessfulChatGetPermalinkResponse, ErrorChatGetPermalinkResponse> {
val response = SlackRequestBuilder<ChatGetPermalinkResponse>(authToken, restTemplate)
.with(this.params)
.toMethod("chat.getPermalink")
.returnAsType(ChatGetPermalinkResponse::class.java)
.postWithJsonBody()

return when (response.body!!) {
is SuccessfulChatGetPermalinkResponse -> {
val responseEntity = response.body as SuccessfulChatGetPermalinkResponse
this.onSuccess?.invoke(responseEntity)
ApiCallResult(success = responseEntity)
}
is ErrorChatGetPermalinkResponse -> {
val responseEntity = response.body as ErrorChatGetPermalinkResponse
this.onFailure?.invoke(responseEntity)
ApiCallResult(failure = responseEntity)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.kreait.slack.api.spring.group.chat

import com.kreait.slack.api.contract.jackson.group.chat.ChatGetPermalinkRequest
import com.kreait.slack.api.contract.jackson.group.chat.ErrorChatGetPermalinkResponse
import com.kreait.slack.api.contract.jackson.group.chat.SuccessfulChatGetPermalinkResponse
import com.kreait.slack.api.contract.jackson.group.chat.sample
import com.kreait.slack.api.spring.MockServerHelper
import com.kreait.slack.api.spring.Verifier
import com.kreait.slack.api.spring.group.RestTemplateFactory
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.springframework.web.client.RestTemplate

class DefaultGetPermalinkMethodTest {

private lateinit var mockTemplate: RestTemplate

@BeforeEach
fun setup() {
mockTemplate = RestTemplateFactory.slackTemplate()
}

@Test
@DisplayName("chat.getPermalink Failure")
fun chatGetPermalinkFailure() {
val response = ErrorChatGetPermalinkResponse.sample()
val mockServer = MockServerHelper.buildMockRestServer(mockTemplate, "chat.getPermalink", response)
val verifier = Verifier(response)

DefaultGetPermalinkMethod("", mockTemplate)
.with(ChatGetPermalinkRequest.sample())
.onFailure { verifier.set(it) }
.onSuccess { }
.invoke()
mockServer.verify()
verifier.verify()
}

@Test
@DisplayName("chat.getPermalink Success")
fun chatGetPermalinkSuccess() {
val response = SuccessfulChatGetPermalinkResponse.sample()
val mockServer = MockServerHelper.buildMockRestServer(mockTemplate, "chat.getPermalink", response)
val verifier = Verifier(response)

DefaultGetPermalinkMethod("", mockTemplate)
.with(ChatGetPermalinkRequest.sample())
.onSuccess { verifier.set(it) }
.invoke()
mockServer.verify()
verifier.verify()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.kreait.slack.api.test.group.chat

import com.kreait.slack.api.contract.jackson.group.chat.ChatGetPermalinkRequest
import com.kreait.slack.api.contract.jackson.group.chat.ErrorChatGetPermalinkResponse
import com.kreait.slack.api.contract.jackson.group.chat.SuccessfulChatGetPermalinkResponse
import com.kreait.slack.api.group.ApiCallResult
import com.kreait.slack.api.group.chat.ChatGetPermalinkMethod
import com.kreait.slack.api.test.MockMethod

class MockChatGetPermalink : ChatGetPermalinkMethod(), MockMethod<SuccessfulChatGetPermalinkResponse, ErrorChatGetPermalinkResponse, ChatGetPermalinkRequest> {

override fun params(): ChatGetPermalinkRequest {
return params;
}

override var successResponse: SuccessfulChatGetPermalinkResponse? = null
override var failureResponse: ErrorChatGetPermalinkResponse? = null

override fun request(): ApiCallResult<SuccessfulChatGetPermalinkResponse, ErrorChatGetPermalinkResponse> {
this.successResponse?.let { this.onSuccess?.invoke(it) }
this.failureResponse?.let { this.onFailure?.invoke(it) }

return ApiCallResult(this.successResponse, this.failureResponse)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import com.kreait.slack.api.group.chat.ChatMeMessageMethod
import com.kreait.slack.api.group.chat.ChatMethodGroup
import com.kreait.slack.api.group.chat.ChatUnfurlMethod
import com.kreait.slack.api.group.chat.ChatUpdateMethod
import com.kreait.slack.api.group.chat.GetChatPermalinkMethod

class MockChatMethodGroup : ChatMethodGroup {
private val mockChatPostEphemeralMethod = MockChatPostEphemeral()
private val mockChatPostMessageMethod = MockChatPostMessage()
private val mockChatDeleteMethod = MockChatDelete()
private val mockChatGetPermalinkMethod = MockChatGetPermalink()

override fun delete(authToken: String): MockChatDelete {
return mockChatDeleteMethod
}

override fun getPermalink(authToken: String): GetChatPermalinkMethod {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
override fun getPermalink(authToken: String): MockChatGetPermalink {
return mockChatGetPermalinkMethod
}

override fun meMessage(authToken: String): ChatMeMessageMethod {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.kreait.slack.api.test.group.chat

import com.kreait.slack.api.contract.jackson.group.chat.ChatGetPermalinkRequest
import com.kreait.slack.api.contract.jackson.group.chat.ErrorChatGetPermalinkResponse
import com.kreait.slack.api.contract.jackson.group.chat.SuccessfulChatGetPermalinkResponse
import com.kreait.slack.api.contract.jackson.group.chat.sample
import com.kreait.slack.api.test.MockMethodTestHelper
import com.kreait.slack.api.test.MockSlackClient
import com.nhaarman.mockitokotlin2.mock
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test


@DisplayName("MockChatGetPermalinkMethod")
class MockChatGetPermalinkUnitTests {


@DisplayName("Mocking Successful")
@Test
fun testMockMethod() {
val successFunction: (SuccessfulChatGetPermalinkResponse?) -> Any = mock {}
val failureFunction: (ErrorChatGetPermalinkResponse?) -> Any = mock {}
val mockSlackClient = MockSlackClient()

MockMethodTestHelper.verify({ mockSlackClient.chat().getPermalink("") },
successFunction, SuccessfulChatGetPermalinkResponse.sample(),
failureFunction, ErrorChatGetPermalinkResponse.sample(),
ChatGetPermalinkRequest.sample())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.kreait.slack.api.contract.jackson.group.chat

import java.time.Instant

fun ErrorChatGetPermalinkResponse.Companion.sample(): ErrorChatGetPermalinkResponse {
return ErrorChatGetPermalinkResponse(false, "")
}

fun SuccessfulChatGetPermalinkResponse.Companion.sample(): SuccessfulChatGetPermalinkResponse {
return SuccessfulChatGetPermalinkResponse(true, "", "")
}

fun ChatGetPermalinkRequest.Companion.sample(): ChatGetPermalinkRequest {
return ChatGetPermalinkRequest("", Instant.now())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.kreait.slack.api.contract.jackson.group.chat

import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonSubTypes
import com.fasterxml.jackson.annotation.JsonTypeInfo
import com.kreait.slack.api.contract.jackson.JacksonDataClass
import java.time.Instant

@JacksonDataClass
data class ChatGetPermalinkRequest constructor(@JsonProperty("channel") val channel: String,
@JsonProperty("message_ts") val timestamp: Instant) {
companion object
}

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "ok",
visible = true)
@JsonSubTypes(
JsonSubTypes.Type(value = SuccessfulChatGetPermalinkResponse::class, name = "true"),
JsonSubTypes.Type(value = ErrorChatGetPermalinkResponse::class, name = "false")
)
@JacksonDataClass
sealed class ChatGetPermalinkResponse constructor(@JsonProperty("ok") open val ok: Boolean)

@JacksonDataClass
data class SuccessfulChatGetPermalinkResponse constructor(override val ok: Boolean,
@JsonProperty("channel") val channel: String,
@JsonProperty("permalink") val permalink: String) : ChatGetPermalinkResponse(ok) {
companion object
}

@JacksonDataClass
data class ErrorChatGetPermalinkResponse constructor(override val ok: Boolean,
@JsonProperty("error") val error: String)
: ChatGetPermalinkResponse(ok) {
companion object
}


0 comments on commit 5e57123

Please sign in to comment.