Merge pull request #15557 from DiegoMarti2/master
Update CheckIfListContainsEnumUnitTest.java
This commit is contained in:
commit
20c3c8e7cc
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue