Skip to content

Commit 0ed8334

Browse files
committed
add tests for WorkerNode and TaskId
Signed-off-by: Varun Bansal <bansvaru@amazon.com>
1 parent 3a84e95 commit 0ed8334

3 files changed

Lines changed: 104 additions & 0 deletions

File tree

libs/task-commons/src/main/java/org/opensearch/task/commons/task/TaskId.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,12 @@ public TaskId(String id) {
3636
public String getValue() {
3737
return id;
3838
}
39+
40+
public boolean equals(Object obj) {
41+
if (obj == null || getClass() != obj.getClass()) {
42+
return false;
43+
}
44+
TaskId other = (TaskId) obj;
45+
return this.id.equals(other.id);
46+
}
3947
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.task.commons.task;
10+
11+
import org.opensearch.test.OpenSearchTestCase;
12+
13+
/**
14+
* Tests for {@link TaskId}
15+
*/
16+
public class TaskIdTests extends OpenSearchTestCase {
17+
18+
public void testConstructorAndGetValue() {
19+
TaskId taskId = new TaskId("123");
20+
assertEquals("123", taskId.getValue());
21+
}
22+
23+
public void testEqualsWithSameId() {
24+
TaskId taskId1 = new TaskId("456");
25+
TaskId taskId2 = new TaskId("456");
26+
assertEquals(taskId1, taskId2);
27+
}
28+
29+
public void testEqualsWithDifferentId() {
30+
TaskId taskId1 = new TaskId("789");
31+
TaskId taskId2 = new TaskId("987");
32+
assertNotEquals(taskId1, taskId2);
33+
}
34+
35+
public void testEqualsWithNull() {
36+
TaskId taskId = new TaskId("abc");
37+
assertNotEquals(null, taskId);
38+
}
39+
40+
public void testEqualsWithDifferentClass() {
41+
TaskId taskId = new TaskId("def");
42+
assertNotEquals(taskId, new Object());
43+
}
44+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.task.commons.worker;
10+
11+
import org.opensearch.test.OpenSearchTestCase;
12+
13+
/**
14+
* Tests for {@link WorkerNode}
15+
*/
16+
public class WorkerNodeTests extends OpenSearchTestCase {
17+
18+
public void testCreateWorkerNode() {
19+
WorkerNode worker = WorkerNode.createWorkerNode("1", "Worker1", "192.168.1.1");
20+
assertNotNull(worker);
21+
assertEquals("1", worker.getId());
22+
assertEquals("Worker1", worker.getName());
23+
assertEquals("192.168.1.1", worker.getIp());
24+
}
25+
26+
public void testEquality() {
27+
WorkerNode worker1 = WorkerNode.createWorkerNode("5", "Worker5", "192.168.1.5");
28+
WorkerNode worker2 = WorkerNode.createWorkerNode("5", "Worker5", "192.168.1.5");
29+
WorkerNode worker3 = WorkerNode.createWorkerNode("6", "Worker6", "192.168.1.6");
30+
31+
assertEquals(worker1, worker2);
32+
assertNotEquals(worker1, worker3);
33+
assertNotEquals(worker2, worker3);
34+
}
35+
36+
public void testHashCode() {
37+
WorkerNode worker1 = WorkerNode.createWorkerNode("7", "Worker7", "192.168.1.7");
38+
WorkerNode worker2 = WorkerNode.createWorkerNode("7", "Worker7", "192.168.1.7");
39+
40+
assertEquals(worker1.hashCode(), worker2.hashCode());
41+
}
42+
43+
public void testNotEqualToNull() {
44+
WorkerNode worker = WorkerNode.createWorkerNode("8", "Worker8", "192.168.1.8");
45+
assertNotEquals(null, worker);
46+
}
47+
48+
public void testNotEqualToDifferentClass() {
49+
WorkerNode worker = WorkerNode.createWorkerNode("9", "Worker9", "192.168.1.9");
50+
assertNotEquals(worker, new Object());
51+
}
52+
}

0 commit comments

Comments
 (0)