Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding EntityType to Comment Model #671

Merged
merged 10 commits into from
Jun 11, 2024
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
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,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Loading