Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abdolence committed Dec 8, 2023
1 parent ce016bd commit 1922c3b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import com.google.common.base.Preconditions;

import java.util.Comparator;
import java.util.Objects;

/**
Expand Down Expand Up @@ -74,6 +75,8 @@ public int hashCode() {

@Override
public int compareTo(RoleBinding o) {
return this.fullResourceName.compareTo(o.fullResourceName);
return Comparator.comparing((RoleBinding r) -> r.fullResourceName)
.thenComparing(r -> r.role)
.compare(this, o);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@

import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.*;

public class TestProjectId {
Expand Down Expand Up @@ -104,4 +108,14 @@ public void whenObjectIsDifferentType_ThenEqualsReturnsFalse() {

assertFalse(id1.equals(""));
}

@Test
public void whenInTreeSet_ThenReturnsInExpectedOrder() {
var projects = List.of(new ProjectId("project-3"),new ProjectId("project-1"), new ProjectId("project-2"));
var sortedProjects = new TreeSet<>(projects);
var sortedIter = sortedProjects.iterator();
assertEquals(sortedIter.next(), new ProjectId("project-1"));
assertEquals(sortedIter.next(), new ProjectId("project-2"));
assertEquals(sortedIter.next(), new ProjectId("project-3"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.TreeSet;

import static org.junit.jupiter.api.Assertions.*;

public class TestProjectRole {
Expand Down Expand Up @@ -133,4 +136,26 @@ public void equalsNullIsFalse() {

assertFalse(role.equals(null));
}

@Test
public void whenInTreeSet_ThenReturnsInExpectedOrder() {
var role1 = new ProjectRole(
new RoleBinding("//cloudresourcemanager.googleapis.com/projects/project-1", "role/sample1"),
ProjectRole.Status.ELIGIBLE_FOR_JIT
);
var role2 = new ProjectRole(
new RoleBinding("//cloudresourcemanager.googleapis.com/projects/project-1", "role/sample2"),
ProjectRole.Status.ELIGIBLE_FOR_JIT
);
var role3 = new ProjectRole(
new RoleBinding("//cloudresourcemanager.googleapis.com/projects/project-2", "role/sample1"),
ProjectRole.Status.ELIGIBLE_FOR_JIT
);
var roles = List.of(role3,role1,role2);
var sorted = new TreeSet<>(roles);
var sortedIter = sorted.iterator();
assertEquals(sortedIter.next(), role1);
assertEquals(sortedIter.next(), role2);
assertEquals(sortedIter.next(), role3);
}
}

0 comments on commit 1922c3b

Please sign in to comment.