Skip to content

Commit

Permalink
add tests for WorkerNode and TaskId
Browse files Browse the repository at this point in the history
Signed-off-by: Varun Bansal <bansvaru@amazon.com>
  • Loading branch information
linuxpi committed Aug 6, 2024
1 parent 3a84e95 commit a1e3bc3
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import org.opensearch.common.annotation.ExperimentalApi;

import java.util.Objects;

/**
* Class encapsulating Task identifier
*/
Expand All @@ -36,4 +38,18 @@ public TaskId(String id) {
public String getValue() {
return id;
}

@Override
public boolean equals(Object obj) {
if (obj == null || getClass() != obj.getClass()) {
return false;
}
TaskId other = (TaskId) obj;
return this.id.equals(other.id);
}

@Override
public int hashCode() {
return Objects.hash(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.task.commons.task;

import org.opensearch.test.OpenSearchTestCase;

/**
* Tests for {@link TaskId}
*/
public class TaskIdTests extends OpenSearchTestCase {

public void testConstructorAndGetValue() {
TaskId taskId = new TaskId("123");
assertEquals("123", taskId.getValue());
}

public void testEqualsWithSameId() {
TaskId taskId1 = new TaskId("456");
TaskId taskId2 = new TaskId("456");
assertEquals(taskId1, taskId2);
}

public void testEqualsWithDifferentId() {
TaskId taskId1 = new TaskId("789");
TaskId taskId2 = new TaskId("987");
assertNotEquals(taskId1, taskId2);
}

public void testEqualsWithNull() {
TaskId taskId = new TaskId("abc");
assertNotEquals(null, taskId);
}

public void testEqualsWithDifferentClass() {
TaskId taskId = new TaskId("def");
assertNotEquals(taskId, new Object());
}

public void testHashCode() {
TaskId taskId1 = new TaskId("456");
TaskId taskId2 = new TaskId("456");
assertEquals(taskId1.hashCode(), taskId2.hashCode());

TaskId taskId3 = new TaskId("4567");
assertNotEquals(taskId1.hashCode(), taskId3.hashCode());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.task.commons.worker;

import org.opensearch.test.OpenSearchTestCase;

/**
* Tests for {@link WorkerNode}
*/
public class WorkerNodeTests extends OpenSearchTestCase {

public void testCreateWorkerNode() {
WorkerNode worker = WorkerNode.createWorkerNode("1", "Worker1", "192.168.1.1");
assertNotNull(worker);
assertEquals("1", worker.getId());
assertEquals("Worker1", worker.getName());
assertEquals("192.168.1.1", worker.getIp());
}

public void testEquality() {
WorkerNode worker1 = WorkerNode.createWorkerNode("5", "Worker5", "192.168.1.5");
WorkerNode worker2 = WorkerNode.createWorkerNode("5", "Worker5", "192.168.1.5");
WorkerNode worker3 = WorkerNode.createWorkerNode("6", "Worker6", "192.168.1.6");

assertEquals(worker1, worker2);
assertNotEquals(worker1, worker3);
assertNotEquals(worker2, worker3);
}

public void testHashCode() {
WorkerNode worker1 = WorkerNode.createWorkerNode("7", "Worker7", "192.168.1.7");
WorkerNode worker2 = WorkerNode.createWorkerNode("7", "Worker7", "192.168.1.7");

assertEquals(worker1.hashCode(), worker2.hashCode());
}

public void testNotEqualToNull() {
WorkerNode worker = WorkerNode.createWorkerNode("8", "Worker8", "192.168.1.8");
assertNotEquals(null, worker);
}

public void testNotEqualToDifferentClass() {
WorkerNode worker = WorkerNode.createWorkerNode("9", "Worker9", "192.168.1.9");
assertNotEquals(worker, new Object());
}
}

0 comments on commit a1e3bc3

Please sign in to comment.