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

lwcapi: fix status code for event without id #1649

Merged
merged 2 commits into from
Apr 3, 2024
Merged
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 @@ -27,5 +27,7 @@ import com.netflix.atlas.json.JsonSupport
* Raw event payload.
*/
case class LwcEvent(id: String, payload: JsonNode) extends JsonSupport {

require(id != null, "id cannot be null")
val `type`: String = "event"
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.netflix.atlas.eval.model.LwcEvent
import com.netflix.atlas.json.Json
import com.netflix.atlas.lwcapi.EvaluateApi.*
import com.netflix.atlas.pekko.DiagnosticMessage
import com.netflix.atlas.pekko.RequestHandler
import com.netflix.atlas.pekko.testkit.MUnitRouteSuite
import com.netflix.spectator.api.NoopRegistry

Expand All @@ -34,49 +35,56 @@ class EvaluateApiSuite extends MUnitRouteSuite {
private implicit val routeTestTimeout: RouteTestTimeout = RouteTestTimeout(5.second)

private val sm = new StreamSubscriptionManager(new NoopRegistry)
private val endpoint = new EvaluateApi(new NoopRegistry, sm)
private val routes = RequestHandler.standardOptions(new EvaluateApi(new NoopRegistry, sm).routes)

test("post empty payload") {
val json = EvaluateRequest(1234L, Nil, Nil).toJson
Post("/lwc/api/v1/evaluate", json) ~> endpoint.routes ~> check {
Post("/lwc/api/v1/evaluate", json) ~> routes ~> check {
assertEquals(response.status, StatusCodes.OK)
}
}

test("post metrics") {
val metrics = List(Item("abc", SortedTagMap("a" -> "1"), 42.0))
val json = EvaluateRequest(1234L, metrics, Nil, Nil).toJson
Post("/lwc/api/v1/evaluate", json) ~> endpoint.routes ~> check {
Post("/lwc/api/v1/evaluate", json) ~> routes ~> check {
assertEquals(response.status, StatusCodes.OK)
}
}

test("post events") {
val events = List(LwcEvent("abc", Json.decode[JsonNode]("42.0")))
val json = EvaluateRequest(1234L, Nil, events, Nil).toJson
Post("/lwc/api/v1/evaluate", json) ~> endpoint.routes ~> check {
Post("/lwc/api/v1/evaluate", json) ~> routes ~> check {
assertEquals(response.status, StatusCodes.OK)
}
}

test("post events, missing event id") {
val json = """{"timestamp":1234,"events":[{"payload":42.0,"type":"event"}]}"""
Post("/lwc/api/v1/evaluate", json) ~> routes ~> check {
assertEquals(response.status, StatusCodes.BadRequest)
}
}

test("post diagnostic message") {
val msgs = List(LwcDiagnosticMessage("abc", DiagnosticMessage.error("bad expression")))
val json = EvaluateRequest(1234L, Nil, Nil, msgs).toJson
Post("/lwc/api/v1/evaluate", json) ~> endpoint.routes ~> check {
Post("/lwc/api/v1/evaluate", json) ~> routes ~> check {
assertEquals(response.status, StatusCodes.OK)
}
}

test("post missing messages field") {
val json = """{"timestamp":12345,"metrics":[]}"""
Post("/lwc/api/v1/evaluate", json) ~> endpoint.routes ~> check {
Post("/lwc/api/v1/evaluate", json) ~> routes ~> check {
assertEquals(response.status, StatusCodes.OK)
}
}

test("post missing metrics field") {
val json = """{"timestamp":12345,"messages":[]}"""
Post("/lwc/api/v1/evaluate", json) ~> endpoint.routes ~> check {
Post("/lwc/api/v1/evaluate", json) ~> routes ~> check {
assertEquals(response.status, StatusCodes.OK)
}
}
Expand Down
Loading