Skip to content

Commit

Permalink
Adding EntityType to Comment Model (#671) (#683)
Browse files Browse the repository at this point in the history
* adding entityType to Comment model

* updated tests

* removing EntityType enum class, entity type in documents will just be strings

* fixed unit tests

* adding tests and release notes

* moved comment test to WriteableTests

* updated writeable test

---------

Signed-off-by: Dennis Toepker <toepkerd@amazon.com>
Co-authored-by: Dennis Toepker <toepkerd@amazon.com>
(cherry picked from commit 841fd4d)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
github-actions[bot] and toepkerd-zz committed Jun 12, 2024
1 parent dd9fdf1 commit 9ba7ecf
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Compatible with OpenSearch 2.15.0

### Enhancements
* Add start_time and end_time filters to GetAlertsRequest. ([#655](https://github.com/opensearch-project/common-utils/pull/655))
* Added new models for Alerting Comments ([#663](https://github.com/opensearch-project/common-utils/pull/663), [#671](https://github.com/opensearch-project/common-utils/pull/671), [#674](https://github.com/opensearch-project/common-utils/pull/674) [#678](https://github.com/opensearch-project/common-utils/pull/678))

### Documentation
* Added 2.15.0.0 release notes. ([#672](https://github.com/opensearch-project/common-utils/pull/672))
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import java.io.IOException
*/
class IndexCommentRequest : ActionRequest {
val entityId: String
val entityType: String
val commentId: String
val seqNo: Long
val primaryTerm: Long
Expand All @@ -26,13 +27,15 @@ class IndexCommentRequest : ActionRequest {

constructor(
entityId: String,
entityType: String,
commentId: String,
seqNo: Long,
primaryTerm: Long,
method: RestRequest.Method,
content: String
) : super() {
this.entityId = entityId
this.entityType = entityType
this.commentId = commentId
this.seqNo = seqNo
this.primaryTerm = primaryTerm
Expand All @@ -43,6 +46,7 @@ class IndexCommentRequest : ActionRequest {
@Throws(IOException::class)
constructor(sin: StreamInput) : this(
entityId = sin.readString(),
entityType = sin.readString(),
commentId = sin.readString(),
seqNo = sin.readLong(),
primaryTerm = sin.readLong(),
Expand All @@ -64,6 +68,7 @@ class IndexCommentRequest : ActionRequest {
@Throws(IOException::class)
override fun writeTo(out: StreamOutput) {
out.writeString(entityId)
out.writeString(entityType)
out.writeString(commentId)
out.writeLong(seqNo)
out.writeLong(primaryTerm)
Expand Down
12 changes: 11 additions & 1 deletion src/main/kotlin/org/opensearch/commons/alerting/model/Comment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ import java.time.Instant
data class Comment(
val id: String = NO_ID,
val entityId: String = NO_ID,
val entityType: String,
val content: String,
val createdTime: Instant,
val lastUpdatedTime: Instant?,
val user: User?
) : Writeable, ToXContent {

@Throws(IOException::class)
constructor(sin: StreamInput) : this(
id = sin.readString(),
entityId = sin.readString(),
entityType = sin.readString(),
content = sin.readString(),
createdTime = sin.readInstant(),
lastUpdatedTime = sin.readOptionalInstant(),
Expand All @@ -37,11 +38,13 @@ data class Comment(

constructor(
entityId: String,
entityType: String,
content: String,
createdTime: Instant,
user: User?
) : this (
entityId = entityId,
entityType = entityType,
content = content,
createdTime = createdTime,
lastUpdatedTime = null,
Expand All @@ -52,6 +55,7 @@ data class Comment(
override fun writeTo(out: StreamOutput) {
out.writeString(id)
out.writeString(entityId)
out.writeString(entityType)
out.writeString(content)
out.writeInstant(createdTime)
out.writeOptionalInstant(lastUpdatedTime)
Expand All @@ -63,6 +67,7 @@ data class Comment(
return mapOf<String, Any?>(
_ID to id,
ENTITY_ID_FIELD to entityId,
ENTITY_TYPE_FIELD to entityType,
COMMENT_CONTENT_FIELD to content,
COMMENT_CREATED_TIME_FIELD to createdTime,
COMMENT_LAST_UPDATED_TIME_FIELD to lastUpdatedTime,
Expand All @@ -83,6 +88,7 @@ data class Comment(
private fun createXContentBuilder(builder: XContentBuilder, includeFullUser: Boolean): XContentBuilder {
builder.startObject()
.field(ENTITY_ID_FIELD, entityId)
.field(ENTITY_TYPE_FIELD, entityType)
.field(COMMENT_CONTENT_FIELD, content)
.optionalTimeField(COMMENT_CREATED_TIME_FIELD, createdTime)
.optionalTimeField(COMMENT_LAST_UPDATED_TIME_FIELD, lastUpdatedTime)
Expand All @@ -101,6 +107,7 @@ data class Comment(

companion object {
const val ENTITY_ID_FIELD = "entity_id"
const val ENTITY_TYPE_FIELD = "entity_type"
const val COMMENT_CONTENT_FIELD = "content"
const val COMMENT_CREATED_TIME_FIELD = "created_time"
const val COMMENT_LAST_UPDATED_TIME_FIELD = "last_updated_time"
Expand All @@ -112,6 +119,7 @@ data class Comment(
@Throws(IOException::class)
fun parse(xcp: XContentParser, id: String = NO_ID): Comment {
lateinit var entityId: String
lateinit var entityType: String
var content = ""
lateinit var createdTime: Instant
var lastUpdatedTime: Instant? = null
Expand All @@ -124,6 +132,7 @@ data class Comment(

when (fieldName) {
ENTITY_ID_FIELD -> entityId = xcp.text()
ENTITY_TYPE_FIELD -> entityType = xcp.text()
COMMENT_CONTENT_FIELD -> content = xcp.text()
COMMENT_CREATED_TIME_FIELD -> createdTime = requireNotNull(xcp.instant())
COMMENT_LAST_UPDATED_TIME_FIELD -> lastUpdatedTime = xcp.instant()
Expand All @@ -139,6 +148,7 @@ data class Comment(
return Comment(
id = id,
entityId = entityId,
entityType = entityType,
content = content,
createdTime = createdTime,
lastUpdatedTime = lastUpdatedTime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import org.opensearch.rest.RestRequest
class IndexCommentRequestTests {
@Test
fun `test index comment post request`() {
val req = IndexCommentRequest("123", "456", 1L, 2L, RestRequest.Method.POST, "comment")
val req = IndexCommentRequest("123", "alert", "456", 1L, 2L, RestRequest.Method.POST, "comment")
assertNotNull(req)
val out = BytesStreamOutput()
req.writeTo(out)
val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes)
val newReq = IndexCommentRequest(sin)
assertEquals("123", newReq.entityId)
assertEquals("alert", newReq.entityType)
assertEquals("456", newReq.commentId)
assertEquals(1L, newReq.seqNo)
assertEquals(2L, newReq.primaryTerm)
Expand All @@ -26,13 +27,14 @@ class IndexCommentRequestTests {

@Test
fun `test index comment put request`() {
val req = IndexCommentRequest("123", "456", 1L, 2L, RestRequest.Method.PUT, "comment")
val req = IndexCommentRequest("123", "alert", "456", 1L, 2L, RestRequest.Method.PUT, "comment")
assertNotNull(req)
val out = BytesStreamOutput()
req.writeTo(out)
val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes)
val newReq = IndexCommentRequest(sin)
assertEquals("123", newReq.entityId)
assertEquals("alert", newReq.entityType)
assertEquals("456", newReq.commentId)
assertEquals(1L, newReq.seqNo)
assertEquals(2L, newReq.primaryTerm)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class IndexCommentResponseTests {
fun `test index comment response with comment`() {
val comment = Comment(
"123",
"alert",
"456",
"comment",
Instant.now(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.opensearch.commons.alerting.randomUserEmpty
import org.opensearch.commons.authuser.User
import org.opensearch.core.common.io.stream.StreamInput
import org.opensearch.search.builder.SearchSourceBuilder
import java.time.Instant
import kotlin.test.assertTrue

class WriteableTests {
Expand Down Expand Up @@ -190,4 +191,30 @@ class WriteableTests {
"Round tripping ActionExecutionPolicy doesn't work"
)
}

@Test
fun `test Comment object`() {
val user = randomUser()
val createdTime = Instant.now()
val comment = Comment(
"123",
"456",
"alert",
"content",
createdTime,
null,
user
)
Assertions.assertNotNull(comment)
val out = BytesStreamOutput()
comment.writeTo(out)
val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes)
val newComment = Comment(sin)
Assertions.assertEquals("123", newComment.id)
Assertions.assertEquals("456", newComment.entityId)
Assertions.assertEquals("alert", newComment.entityType)
Assertions.assertEquals("content", newComment.content)
Assertions.assertEquals(createdTime, newComment.createdTime)
Assertions.assertEquals(user, newComment.user)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -525,4 +525,22 @@ class XContentTests {
val parsedDataSources = DataSources.parse(parser(dataSourcesString))
Assertions.assertEquals(dataSources, parsedDataSources, "Round tripping DataSources doesn't work")
}

@Test
fun `test Comment parsing`() {
val comment = Comment(
"123",
"456",
"alert",
"content",
Instant.now().truncatedTo(ChronoUnit.MILLIS),
null,
randomUser()
)
Assertions.assertNotNull(comment)

val commentString = comment.toXContentWithUser(builder()).string()
val parsedComment = Comment.parse(parser(commentString), "123")
Assertions.assertEquals(comment, parsedComment, "Round tripping Comment doesn't work")
}
}

0 comments on commit 9ba7ecf

Please sign in to comment.