diff --git a/core-java-modules/core-java-collections-list-3/src/test/java/com/baeldung/list/listoflists/ListOfListsUnitTest.java b/core-java-modules/core-java-collections-list-3/src/test/java/com/baeldung/list/listoflists/ListOfListsUnitTest.java new file mode 100644 index 0000000000..321fa475f6 --- /dev/null +++ b/core-java-modules/core-java-collections-list-3/src/test/java/com/baeldung/list/listoflists/ListOfListsUnitTest.java @@ -0,0 +1,74 @@ +package com.baeldung.list.listoflists; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +import org.junit.jupiter.api.Test; + +public class ListOfListsUnitTest { + + private List> getListOfListsFromCsv() throws URISyntaxException, IOException { + + List lines = Files.readAllLines(Paths.get(getClass().getResource("/listoflists/example.csv") + .toURI())); + List> listOfLists = new ArrayList<>(); + + lines.forEach(line -> { + List innerList = new ArrayList<>(Arrays.asList(line.split(", "))); + listOfLists.add(innerList); + }); + return listOfLists; + } + + private void printListOfLists(List> listOfLists) { + System.out.println("\n List of Lists "); + System.out.println("-------------------------------------"); + listOfLists.forEach(innerList -> { + String line = String.join(", ", innerList); + System.out.println(line); + }); + } + + @Test + void givenCsv_whenInitListOfLists_thenGetExpectedResults() throws URISyntaxException, IOException { + List> listOfLists = getListOfListsFromCsv(); + assertThat(listOfLists).hasSize(3); + assertThat(listOfLists.stream() + .map(List::size) + .collect(Collectors.toSet())).hasSize(1) + .containsExactly(4); + printListOfLists(listOfLists); + } + + @Test + void givenListOfLists_whenRemoveElementFromInnerLists_thenGetExpectedResults() throws URISyntaxException, IOException { + List> listOfLists = getListOfListsFromCsv(); + + listOfLists.forEach(innerList -> innerList.remove("Delete Me")); + assertThat(listOfLists.stream() + .map(List::size) + .collect(Collectors.toSet())).hasSize(1) + .containsExactly(3); + + printListOfLists(listOfLists); + } + + @Test + void givenListOfLists_whenAddNewInnerList_thenGetExpectedResults() throws URISyntaxException, IOException { + List> listOfLists = getListOfListsFromCsv(); + List newList = new ArrayList<>(Arrays.asList("Slack", "Zoom", "Microsoft Teams", "Telegram")); + listOfLists.add(2, newList); + + assertThat(listOfLists).hasSize(4); + assertThat(listOfLists.get(2)).containsExactly("Slack", "Zoom", "Microsoft Teams", "Telegram"); + printListOfLists(listOfLists); + } +} diff --git a/core-java-modules/core-java-collections-list-3/src/test/resources/listoflists/example.csv b/core-java-modules/core-java-collections-list-3/src/test/resources/listoflists/example.csv new file mode 100644 index 0000000000..e275848e19 --- /dev/null +++ b/core-java-modules/core-java-collections-list-3/src/test/resources/listoflists/example.csv @@ -0,0 +1,3 @@ +Linux, Microsoft Windows, Mac OS, Delete Me +Kotlin, Delete Me, Java, Python +Delete Me, Mercurial, Git, Subversion \ No newline at end of file