Update CheckIfListContainsEnumUnitTest.java

This commit is contained in:
DiegoMarti2 2024-01-05 20:13:06 +02:00 committed by GitHub
parent 3ae82245ee
commit 53e1229492
1 changed files with 12 additions and 31 deletions

View File

@ -1,14 +1,12 @@
package com.baeldung.checkiflistcontainsenum;
import java.util.*;
import org.junit.Assert;
import org.junit.Test;
import java.util.*;
public class CheckIfListContainsEnumUnitTest {
private final List<Map<String, Object>> data = new ArrayList<>();
public boolean containsDeveloper = false;
List<Position> list = Arrays.asList(Position.values());
public CheckIfListContainsEnumUnitTest() {
Map<String, Object> map = new HashMap<>();
@ -19,34 +17,28 @@ public class CheckIfListContainsEnumUnitTest {
data.add(map);
}
@Test
public void givenDataList_whenUsingLoop_thenCheckIfListContainsEnum() {
boolean containsEnumValue = false;
for (Map<String, Object> entry : data) {
Object positionValue = entry.get("Position");
for (Position position : Position.values()) {
if (positionValue.equals(position)) {
containsDeveloper = true;
if (Arrays.asList(Position.values()).contains(positionValue)) {
containsEnumValue = true;
break;
}
}
if (containsDeveloper) {
break;
}
}
Assert.assertTrue(containsDeveloper);
Assert.assertTrue(containsEnumValue);
}
@Test
public void givenDataList_whenUsingStream_thenCheckIfListContainsEnum() {
containsDeveloper = data.stream()
boolean containsEnumValue = data.stream()
.map(entry -> entry.get("Position"))
.anyMatch(position -> Arrays.asList(Position.values()).contains(position));
Assert.assertTrue(containsDeveloper);
Assert.assertTrue(containsEnumValue);
}
@Test
@ -55,22 +47,11 @@ public class CheckIfListContainsEnumUnitTest {
.map(entry -> (Position) entry.get("Position"))
.toList();
boolean containsDeveloper = !Collections.disjoint(list, positionValues);
Assert.assertTrue(containsDeveloper);
boolean containsEnumValue = !Collections.disjoint(Arrays.asList(Position.values()), positionValues);
Assert.assertTrue(containsEnumValue);
}
public enum Position {
DEVELOPER("Dev"), MANAGER("Mgr"), ANALYST("Analyst");
private final String value;
Position(String value) {
this.value = value;
}
public String getValue() {
return value;
}
DEVELOPER, MANAGER, ANALYST
}
}