Skip to content

Commit

Permalink
added noop trigger xcontent tests
Browse files Browse the repository at this point in the history
  • Loading branch information
petardz committed May 4, 2023
1 parent 6dee931 commit ee8e651
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ data class NoOpTrigger(
override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder {
builder.startObject()
.startObject(NOOP_TRIGGER_FIELD)
.field(ID_FIELD, id)
.endObject()
.endObject()
return builder
}

Expand All @@ -43,6 +45,7 @@ data class NoOpTrigger(
}

companion object {
const val ID_FIELD = "id"
const val NOOP_TRIGGER_FIELD = "noop_trigger"
val XCONTENT_REGISTRY = NamedXContentRegistry.Entry(
Trigger::class.java, ParseField(NOOP_TRIGGER_FIELD),
Expand All @@ -51,19 +54,17 @@ data class NoOpTrigger(

@JvmStatic @Throws(IOException::class)
fun parseInner(xcp: XContentParser): NoOpTrigger {
if (xcp.currentToken() != XContentParser.Token.START_OBJECT && xcp.currentToken() != XContentParser.Token.FIELD_NAME) {
XContentParserUtils.throwUnknownToken(xcp.currentToken(), xcp.tokenLocation)
}

// If the parser began on START_OBJECT, move to the next token so that the while loop enters on
// the fieldName (or END_OBJECT if it's empty).
var id = ""
if (xcp.currentToken() == XContentParser.Token.START_OBJECT) xcp.nextToken()
if (xcp.currentToken() != XContentParser.Token.END_OBJECT) {
XContentParserUtils.throwUnknownToken(xcp.currentToken(), xcp.tokenLocation)
} else {
if (xcp.currentName() == ID_FIELD) {
xcp.nextToken()
id = xcp.text()
xcp.nextToken()
}
if (xcp.currentToken() != XContentParser.Token.END_OBJECT || id == "") {
XContentParserUtils.throwUnknownToken(xcp.currentToken(), xcp.tokenLocation)
}
return NoOpTrigger()
return NoOpTrigger(id = id)
}

@JvmStatic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ interface Trigger : BaseModel {
enum class Type(val value: String) {
DOCUMENT_LEVEL_TRIGGER(DocumentLevelTrigger.DOCUMENT_LEVEL_TRIGGER_FIELD),
QUERY_LEVEL_TRIGGER(QueryLevelTrigger.QUERY_LEVEL_TRIGGER_FIELD),
BUCKET_LEVEL_TRIGGER(BucketLevelTrigger.BUCKET_LEVEL_TRIGGER_FIELD);
BUCKET_LEVEL_TRIGGER(BucketLevelTrigger.BUCKET_LEVEL_TRIGGER_FIELD),
NOOP_TRIGGER(NoOpTrigger.NOOP_TRIGGER_FIELD);

override fun toString(): String {
return value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,16 @@ class XContentTests {
Assertions.assertEquals(trigger, parsedTrigger, "Round tripping BucketLevelTrigger doesn't work")
}

@Test
fun `test no-op trigger parsing`() {
val trigger = NoOpTrigger()

val triggerString = trigger.toXContent(builder(), ToXContent.EMPTY_PARAMS).string()
val parsedTrigger = Trigger.parse(parser(triggerString))

Assertions.assertEquals(trigger, parsedTrigger, "Round tripping BucketLevelTrigger doesn't work")
}

@Test
fun `test creating a monitor with duplicate trigger ids fails`() {
try {
Expand Down

0 comments on commit ee8e651

Please sign in to comment.