Merge pull request #15557 from DiegoMarti2/master

Update CheckIfListContainsEnumUnitTest.java
This commit is contained in:
Vini 2024-01-05 21:52:04 +01:00 committed by GitHub
commit 20c3c8e7cc
1 changed files with 12 additions and 31 deletions

View File

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