-
Notifications
You must be signed in to change notification settings - Fork 124
test(amber): add unit test coverage for LogicalLink #4956
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
Open
aglinxinyuan
wants to merge
4
commits into
apache:main
Choose a base branch
from
aglinxinyuan:test-logical-link-spec
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+240
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
44b8af9
test(amber): add unit test coverage for LogicalLink
aglinxinyuan 24b7e37
test(amber): tighten LogicalLinkSpec Jackson assertions per Copilot r…
aglinxinyuan e1f706f
test(amber): tighten @JsonProperty pin + separate parameter-name pin
aglinxinyuan 217966e
Merge branch 'main' into test-logical-link-spec
aglinxinyuan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
240 changes: 240 additions & 0 deletions
240
amber/src/test/scala/org/apache/texera/workflow/LogicalLinkSpec.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,240 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.texera.workflow | ||
|
|
||
| import com.fasterxml.jackson.databind.JsonNode | ||
| import com.fasterxml.jackson.databind.exc.MismatchedInputException | ||
| import org.apache.texera.amber.core.virtualidentity.OperatorIdentity | ||
| import org.apache.texera.amber.core.workflow.PortIdentity | ||
| import org.apache.texera.amber.util.JSONUtils.objectMapper | ||
| import org.scalatest.flatspec.AnyFlatSpec | ||
|
|
||
| class LogicalLinkSpec extends AnyFlatSpec { | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Primary constructor + case-class semantics | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| "LogicalLink primary constructor" should "expose the four fields it was constructed with" in { | ||
| val link = LogicalLink( | ||
| fromOpId = OperatorIdentity("op-A"), | ||
| fromPortId = PortIdentity(0), | ||
| toOpId = OperatorIdentity("op-B"), | ||
| toPortId = PortIdentity(1, internal = true) | ||
| ) | ||
| assert(link.fromOpId == OperatorIdentity("op-A")) | ||
| assert(link.fromPortId == PortIdentity(0)) | ||
| assert(link.toOpId == OperatorIdentity("op-B")) | ||
| assert(link.toPortId == PortIdentity(1, internal = true)) | ||
| } | ||
|
|
||
| "LogicalLink case-class equality" should "use structural equality across all four fields" in { | ||
| val a = | ||
| LogicalLink(OperatorIdentity("x"), PortIdentity(0), OperatorIdentity("y"), PortIdentity(1)) | ||
| val b = | ||
| LogicalLink(OperatorIdentity("x"), PortIdentity(0), OperatorIdentity("y"), PortIdentity(1)) | ||
| assert(a == b) | ||
| assert(a.hashCode == b.hashCode) | ||
| } | ||
|
|
||
| it should "distinguish links that differ only in fromOpId" in { | ||
| val a = | ||
| LogicalLink(OperatorIdentity("x"), PortIdentity(0), OperatorIdentity("y"), PortIdentity(1)) | ||
| val b = | ||
| LogicalLink(OperatorIdentity("z"), PortIdentity(0), OperatorIdentity("y"), PortIdentity(1)) | ||
| assert(a != b) | ||
| } | ||
|
|
||
| it should "distinguish links that differ only in toPortId.internal" in { | ||
| val a = LogicalLink( | ||
| OperatorIdentity("x"), | ||
| PortIdentity(0), | ||
| OperatorIdentity("y"), | ||
| PortIdentity(1, internal = false) | ||
| ) | ||
| val b = LogicalLink( | ||
| OperatorIdentity("x"), | ||
| PortIdentity(0), | ||
| OperatorIdentity("y"), | ||
| PortIdentity(1, internal = true) | ||
| ) | ||
| assert(a != b) | ||
| } | ||
|
|
||
| it should "consider a self-loop link well-formed (same fromOpId / toOpId, distinct ports)" in { | ||
| // Self-loops aren't structurally invalid at the LogicalLink level — | ||
| // higher layers reject cycles, but the data type allows fromOpId == | ||
| // toOpId. Pin so a future == check on construction breaks this on | ||
| // purpose. | ||
| val selfLoop = LogicalLink( | ||
| OperatorIdentity("op-A"), | ||
| PortIdentity(0), | ||
| OperatorIdentity("op-A"), | ||
| PortIdentity(1) | ||
| ) | ||
| assert(selfLoop.fromOpId == selfLoop.toOpId) | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Secondary @JsonCreator constructor (string opId variant) | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| "LogicalLink secondary @JsonCreator constructor" should "wrap raw String op ids in OperatorIdentity" in { | ||
| val link = new LogicalLink( | ||
| fromOpId = "op-A", | ||
| fromPortId = PortIdentity(0), | ||
| toOpId = "op-B", | ||
| toPortId = PortIdentity(1) | ||
| ) | ||
| assert(link.fromOpId == OperatorIdentity("op-A")) | ||
| assert(link.toOpId == OperatorIdentity("op-B")) | ||
| // Equal to a link built via the primary constructor. | ||
| assert( | ||
| link == LogicalLink( | ||
| OperatorIdentity("op-A"), | ||
| PortIdentity(0), | ||
| OperatorIdentity("op-B"), | ||
| PortIdentity(1) | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| it should "accept identifiers containing dashes / dots / digits (no normalization)" in { | ||
| val link = new LogicalLink("my.op-1", PortIdentity(0), "my.op-2", PortIdentity(1)) | ||
| assert(link.fromOpId == OperatorIdentity("my.op-1")) | ||
| assert(link.toOpId == OperatorIdentity("my.op-2")) | ||
| } | ||
|
|
||
| it should "accept the empty string as an op id (no validation in the data type)" in { | ||
| // Pin: the secondary constructor does not validate; an empty string | ||
| // wraps into `OperatorIdentity("")`. A future change adding non-empty | ||
| // validation should fail this test on purpose. | ||
| val link = new LogicalLink("", PortIdentity(0), "", PortIdentity(1)) | ||
| assert(link.fromOpId == OperatorIdentity("")) | ||
| assert(link.toOpId == OperatorIdentity("")) | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Jackson round-trip (production objectMapper) | ||
| // --------------------------------------------------------------------------- | ||
| // | ||
| // These tests use the same `JSONUtils.objectMapper` that production uses | ||
| // to read user-saved workflow JSON, so a regression in the Jackson | ||
| // wiring (annotations, default-Scala-module config) surfaces here. | ||
|
|
||
| "LogicalLink Jackson deserialization" should | ||
| "deserialize fromOpId / toOpId from raw String values via the secondary @JsonCreator constructor" in { | ||
| // Build the JSON by hand to mimic a user-saved workflow file where | ||
| // `fromOpId` and `toOpId` are written as plain strings (the only shape | ||
| // production actually receives, since the frontend emits them as | ||
| // strings). Jackson dispatches to the @JsonCreator string-overload | ||
| // constructor. | ||
| val node = objectMapper.createObjectNode() | ||
| node.put("fromOpId", "op-A") | ||
| node.set("fromPortId", objectMapper.valueToTree[JsonNode](PortIdentity(0))) | ||
| node.put("toOpId", "op-B") | ||
| node.set("toPortId", objectMapper.valueToTree[JsonNode](PortIdentity(1))) | ||
| val link = objectMapper.treeToValue(node, classOf[LogicalLink]) | ||
| assert(link.fromOpId == OperatorIdentity("op-A")) | ||
| assert(link.toOpId == OperatorIdentity("op-B")) | ||
| assert(link.fromPortId == PortIdentity(0)) | ||
| assert(link.toPortId == PortIdentity(1)) | ||
| } | ||
|
|
||
| it should "emit `fromOpId` / `toOpId` JSON keys pinned by @JsonProperty annotations" in { | ||
| // Only `fromOpId` / `toOpId` carry `@JsonProperty` in `LogicalLink`; | ||
| // a Scala-side rename of either parameter would still keep the | ||
| // JSON key stable, which is the saved-workflow contract these | ||
| // annotations pin. | ||
| val link = LogicalLink( | ||
| OperatorIdentity("op-A"), | ||
| PortIdentity(0), | ||
| OperatorIdentity("op-B"), | ||
| PortIdentity(1) | ||
| ) | ||
| val tree = objectMapper.valueToTree[JsonNode](link) | ||
| assert(tree.has("fromOpId")) | ||
| assert(tree.has("toOpId")) | ||
| } | ||
|
|
||
| it should "emit `fromPortId` / `toPortId` JSON keys derived from Scala parameter names (no @JsonProperty)" in { | ||
| // Pin: the port-id JSON keys come from Scala parameter names since | ||
| // there is no `@JsonProperty` annotation on those fields. A | ||
| // parameter rename WOULD silently break saved-workflow compatibility | ||
| // for these keys — pin so a future rename without an accompanying | ||
| // `@JsonProperty` annotation breaks this on purpose. | ||
| val link = LogicalLink( | ||
| OperatorIdentity("op-A"), | ||
| PortIdentity(0), | ||
| OperatorIdentity("op-B"), | ||
| PortIdentity(1) | ||
| ) | ||
| val tree = objectMapper.valueToTree[JsonNode](link) | ||
| assert(tree.has("fromPortId")) | ||
| assert(tree.has("toPortId")) | ||
| } | ||
|
|
||
| it should "NOT round-trip through writeValueAsString (the @JsonCreator string overload is incompatible with the object-shape OperatorIdentity that writeValueAsString emits)" in { | ||
| // This is a real asymmetry worth pinning: production reads user-saved | ||
| // workflow JSON where `fromOpId`/`toOpId` are plain strings, but | ||
| // `objectMapper.writeValueAsString` writes OperatorIdentity as | ||
| // `{"id":"op-A"}` (the case-class object form). Re-reading that | ||
| // emitted JSON fails because Jackson dispatches on the @JsonCreator | ||
| // string-overload, which can't accept an object for fromOpId. | ||
| // A future fix that adds a third @JsonCreator (object overload) or | ||
| // a custom @JsonDeserialize on `fromOpId`/`toOpId` will turn this | ||
| // into a passing round-trip and break this assertion on purpose. | ||
| val original = LogicalLink( | ||
| OperatorIdentity("op-A"), | ||
| PortIdentity(0), | ||
| OperatorIdentity("op-B"), | ||
| PortIdentity(1) | ||
| ) | ||
| val json = objectMapper.writeValueAsString(original) | ||
| // Parse the emitted JSON and confirm the structural shape — fromOpId | ||
| // is an object with an `id` field of "op-A". Avoids depending on | ||
| // exact key ordering or escaping. | ||
| val tree = objectMapper.readTree(json) | ||
| assert(tree.path("fromOpId").isObject, s"expected fromOpId to be an object: $json") | ||
| assert(tree.path("fromOpId").path("id").asText() == "op-A") | ||
| // Re-reading the just-emitted JSON fails because the @JsonCreator | ||
| // String overload can't accept the object-shape fromOpId. | ||
| intercept[MismatchedInputException] { | ||
| objectMapper.readValue(json, classOf[LogicalLink]) | ||
| } | ||
|
aglinxinyuan marked this conversation as resolved.
|
||
| } | ||
|
|
||
| it should "silently default missing string op-id fields to OperatorIdentity(null) (no validation in the @JsonCreator path)" in { | ||
| // Pin: omitting `fromOpId` / `toOpId` does not throw — Jackson invokes | ||
| // the @JsonCreator with `null` for the missing String args, and | ||
| // `OperatorIdentity(null)` is a perfectly constructable case-class | ||
| // instance. A future change adding a non-null check at construction | ||
| // time should fail this on purpose. | ||
| val empty = objectMapper.createObjectNode() | ||
| val link = objectMapper.treeToValue(empty, classOf[LogicalLink]) | ||
| assert(link.fromOpId.id == null) | ||
| assert(link.toOpId.id == null) | ||
| // PortIdentity fields default to null too (the @JsonCreator does not | ||
| // supply scalapb defaults for missing object fields — Jackson passes | ||
| // null for the typed PortIdentity arg). | ||
| assert(link.fromPortId == null) | ||
| assert(link.toPortId == null) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.