Merge remote-tracking branch 'origin/master'
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.graph;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.neo4j.driver.v1.AuthTokens;
|
||||
import org.neo4j.driver.v1.Driver;
|
||||
import org.neo4j.driver.v1.GraphDatabase;
|
||||
import org.neo4j.driver.v1.Session;
|
||||
import org.neo4j.driver.v1.StatementResult;
|
||||
import org.testng.Assert;
|
||||
|
||||
@Ignore
|
||||
public class Neo4JServerTest {
|
||||
|
||||
@Test
|
||||
public void standAloneDriver() {
|
||||
Driver driver = GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic("neo4j", "12345"));
|
||||
Session session = driver.session();
|
||||
|
||||
session.run("CREATE (baeldung:Company {name:\"Baeldung\"}) " +
|
||||
"-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" +
|
||||
"RETURN baeldung, tesla");
|
||||
|
||||
StatementResult result = session.run("MATCH (company:Company)-[:owns]-> (car:Car)" +
|
||||
"WHERE car.make='tesla' and car.model='modelX'" +
|
||||
"RETURN company.name");
|
||||
|
||||
Assert.assertTrue(result.hasNext());
|
||||
Assert.assertEquals(result.next().get("company.name").asString(), "Baeldung");
|
||||
|
||||
session.close();
|
||||
driver.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void standAloneJdbc() throws Exception {
|
||||
Connection con = DriverManager.getConnection("jdbc:neo4j:bolt://localhost/?user=neo4j,password=12345,scheme=basic");
|
||||
|
||||
// Querying
|
||||
try (Statement stmt = con.createStatement()) {
|
||||
stmt.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " +
|
||||
"-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" +
|
||||
"RETURN baeldung, tesla");
|
||||
|
||||
ResultSet rs = stmt.executeQuery("MATCH (company:Company)-[:owns]-> (car:Car)" +
|
||||
"WHERE car.make='tesla' and car.model='modelX'" +
|
||||
"RETURN company.name");
|
||||
|
||||
while (rs.next()) {
|
||||
Assert.assertEquals(rs.getString("company.name"), "Baeldung");
|
||||
}
|
||||
}
|
||||
con.close();
|
||||
}
|
||||
}
|
||||
46
core-java/src/test/java/com/baeldung/graph/Neo4jOgmTest.java
Normal file
46
core-java/src/test/java/com/baeldung/graph/Neo4jOgmTest.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.graph;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.neo4j.ogm.config.Configuration;
|
||||
import org.neo4j.ogm.model.Result;
|
||||
import org.neo4j.ogm.session.Session;
|
||||
import org.neo4j.ogm.session.SessionFactory;
|
||||
import org.testng.Assert;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Danil Kornishev (danil.kornishev@mastercard.com)
|
||||
*/
|
||||
public class Neo4jOgmTest {
|
||||
|
||||
@Test
|
||||
public void testOgm() {
|
||||
Configuration conf = new Configuration();
|
||||
conf.driverConfiguration().setDriverClassName("org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver");
|
||||
|
||||
SessionFactory factory = new SessionFactory(conf, "com.baeldung.graph");
|
||||
Session session = factory.openSession();
|
||||
|
||||
Car tesla = new Car("tesla", "modelS");
|
||||
Company baeldung = new Company("baeldung");
|
||||
|
||||
baeldung.setCar(tesla);
|
||||
|
||||
session.save(baeldung);
|
||||
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("make", "tesla");
|
||||
Result result = session.query("MATCH (car:Car) <-[:owns]- (company:Company)" +
|
||||
" WHERE car.make=$make" +
|
||||
" RETURN company", params);
|
||||
|
||||
Map<String, Object> firstResult = result.iterator().next();
|
||||
|
||||
Assert.assertEquals(firstResult.size(), 1);
|
||||
|
||||
Company actual = (Company) firstResult.get("company");
|
||||
Assert.assertEquals(actual.getName(), baeldung.getName());
|
||||
}
|
||||
}
|
||||
167
core-java/src/test/java/com/baeldung/graph/Neo4jTest.java
Normal file
167
core-java/src/test/java/com/baeldung/graph/Neo4jTest.java
Normal file
@@ -0,0 +1,167 @@
|
||||
package com.baeldung.graph;
|
||||
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.neo4j.graphdb.GraphDatabaseService;
|
||||
import org.neo4j.graphdb.Label;
|
||||
import org.neo4j.graphdb.Node;
|
||||
import org.neo4j.graphdb.NotFoundException;
|
||||
import org.neo4j.graphdb.RelationshipType;
|
||||
import org.neo4j.graphdb.Result;
|
||||
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
|
||||
import org.testng.Assert;
|
||||
|
||||
public class Neo4jTest {
|
||||
|
||||
private static GraphDatabaseService graphDb;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
GraphDatabaseFactory graphDbFactory = new GraphDatabaseFactory();
|
||||
graphDb = graphDbFactory.newEmbeddedDatabase(new File("data/cars"));
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
graphDb.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPersonCar() {
|
||||
graphDb.beginTx();
|
||||
Node car = graphDb.createNode(Label.label("Car"));
|
||||
car.setProperty("make", "tesla");
|
||||
car.setProperty("model", "model3");
|
||||
|
||||
Node owner = graphDb.createNode(Label.label("Person"));
|
||||
owner.setProperty("firstName", "baeldung");
|
||||
owner.setProperty("lastName", "baeldung");
|
||||
|
||||
owner.createRelationshipTo(car, RelationshipType.withName("owner"));
|
||||
|
||||
Result result = graphDb.execute("MATCH (c:Car) <-[owner]- (p:Person) " +
|
||||
"WHERE c.make = 'tesla'" +
|
||||
"RETURN p.firstName, p.lastName");
|
||||
|
||||
Map<String, Object> firstResult = result.next();
|
||||
Assert.assertEquals("baeldung", firstResult.get("p.firstName"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateNode() {
|
||||
|
||||
graphDb.beginTx();
|
||||
|
||||
Result result = graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"})" +
|
||||
"RETURN baeldung");
|
||||
|
||||
Map<String, Object> firstResult = result.next();
|
||||
Node firstNode = (Node) firstResult.get("baeldung");
|
||||
Assert.assertEquals(firstNode.getProperty("name"), "Baeldung");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateNodeAndLink() {
|
||||
graphDb.beginTx();
|
||||
|
||||
Result result = graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " +
|
||||
"-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" +
|
||||
"RETURN baeldung, tesla");
|
||||
|
||||
Map<String, Object> firstResult = result.next();
|
||||
Assert.assertTrue(firstResult.containsKey("baeldung"));
|
||||
Assert.assertTrue(firstResult.containsKey("tesla"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindAndReturn() {
|
||||
graphDb.beginTx();
|
||||
|
||||
graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " +
|
||||
"-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" +
|
||||
"RETURN baeldung, tesla");
|
||||
|
||||
Result result = graphDb.execute("MATCH (company:Company)-[:owns]-> (car:Car)" +
|
||||
"WHERE car.make='tesla' and car.model='modelX'" +
|
||||
"RETURN company.name");
|
||||
|
||||
Map<String, Object> firstResult = result.next();
|
||||
Assert.assertEquals(firstResult.get("company.name"), "Baeldung");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdate() {
|
||||
graphDb.beginTx();
|
||||
|
||||
graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " +
|
||||
"-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" +
|
||||
"RETURN baeldung, tesla");
|
||||
|
||||
Result result = graphDb.execute("MATCH (car:Car)" +
|
||||
"WHERE car.make='tesla'" +
|
||||
" SET car.milage=120" +
|
||||
" SET car :Car:Electro" +
|
||||
" SET car.model=NULL" +
|
||||
" RETURN car");
|
||||
|
||||
Map<String, Object> firstResult = result.next();
|
||||
Node car = (Node) firstResult.get("car");
|
||||
|
||||
Assert.assertEquals(car.getProperty("milage"), 120L);
|
||||
Assert.assertEquals(car.getLabels(), Arrays.asList(Label.label("Car"), Label.label("Electro")));
|
||||
|
||||
try {
|
||||
car.getProperty("model");
|
||||
Assert.fail();
|
||||
} catch (NotFoundException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDelete() {
|
||||
graphDb.beginTx();
|
||||
|
||||
graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " +
|
||||
"-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" +
|
||||
"RETURN baeldung, tesla");
|
||||
|
||||
graphDb.execute("MATCH (company:Company)" +
|
||||
" WHERE company.name='Baeldung'" +
|
||||
" DELETE company");
|
||||
|
||||
Result result = graphDb.execute("MATCH (company:Company)" +
|
||||
" WHERE company.name='Baeldung'" +
|
||||
" RETURN company");
|
||||
|
||||
Assert.assertFalse(result.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBindings() {
|
||||
graphDb.beginTx();
|
||||
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("name", "baeldung");
|
||||
params.put("make", "tesla");
|
||||
params.put("model", "modelS");
|
||||
|
||||
Result result = graphDb.execute("CREATE (baeldung:Company {name:$name}) " +
|
||||
"-[:owns]-> (tesla:Car {make: $make, model: $model})" +
|
||||
"RETURN baeldung, tesla", params);
|
||||
|
||||
Map<String, Object> firstResult = result.next();
|
||||
Assert.assertTrue(firstResult.containsKey("baeldung"));
|
||||
Assert.assertTrue(firstResult.containsKey("tesla"));
|
||||
|
||||
Node car = (Node) firstResult.get("tesla");
|
||||
Assert.assertEquals(car.getProperty("model"), "modelS");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package com.baeldung.java.concurrentmodification;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.util.Lists.newArrayList;
|
||||
|
||||
public class ConcurrentModificationUnitTest {
|
||||
@Test(expected = ConcurrentModificationException.class)
|
||||
public void givenIterating_whenRemoving_thenThrowException() throws InterruptedException {
|
||||
|
||||
List<Integer> integers = newArrayList(1, 2, 3);
|
||||
|
||||
for (Integer integer : integers) {
|
||||
integers.remove(1);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIterating_whenUsingIteratorRemove_thenNoError() throws InterruptedException {
|
||||
|
||||
List<Integer> integers = newArrayList(1, 2, 3);
|
||||
|
||||
for (Iterator<Integer> iterator = integers.iterator(); iterator.hasNext();) {
|
||||
Integer integer = iterator.next();
|
||||
if(integer == 2) {
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
|
||||
assertThat(integers).containsExactly(1, 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIterating_whenUsingRemovalList_thenNoError() throws InterruptedException {
|
||||
|
||||
List<Integer> integers = newArrayList(1, 2, 3);
|
||||
List<Integer> toRemove = newArrayList();
|
||||
|
||||
for (Integer integer : integers) {
|
||||
if(integer == 2) {
|
||||
toRemove.add(integer);
|
||||
}
|
||||
}
|
||||
integers.removeAll(toRemove);
|
||||
|
||||
assertThat(integers).containsExactly(1, 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingRemoveIf_thenRemoveElements() throws InterruptedException {
|
||||
|
||||
Collection<Integer> integers = newArrayList(1, 2, 3);
|
||||
|
||||
integers.removeIf(i -> i == 2);
|
||||
|
||||
assertThat(integers).containsExactly(1, 3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingStream_thenRemoveElements() {
|
||||
Collection<Integer> integers = newArrayList(1, 2, 3);
|
||||
|
||||
List<String> collected = integers
|
||||
.stream()
|
||||
.filter(i -> i != 2)
|
||||
.map(Object::toString)
|
||||
.collect(toList());
|
||||
|
||||
assertThat(collected).containsExactly("1", "3");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,231 @@
|
||||
package com.baeldung.java8;
|
||||
|
||||
import com.baeldung.java_8_features.groupingby.BlogPost;
|
||||
import com.baeldung.java_8_features.groupingby.BlogPostType;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
import static java.util.Comparator.comparingInt;
|
||||
import static java.util.stream.Collectors.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class Java8GroupingByCollectorUnitTest {
|
||||
|
||||
private static final List<BlogPost> posts = Arrays.asList(
|
||||
new BlogPost("News item 1", "Author 1", BlogPostType.NEWS, 15),
|
||||
new BlogPost("Tech review 1", "Author 2", BlogPostType.REVIEW, 5),
|
||||
new BlogPost("Programming guide", "Author 1", BlogPostType.GUIDE, 20),
|
||||
new BlogPost("News item 2", "Author 2", BlogPostType.NEWS, 35),
|
||||
new BlogPost("Tech review 2", "Author 1", BlogPostType.REVIEW, 15));
|
||||
|
||||
@Test
|
||||
public void givenAListOfPosts_whenGroupedByType_thenGetAMapBetweenTypeAndPosts() {
|
||||
Map<BlogPostType, List<BlogPost>> postsPerType = posts
|
||||
.stream()
|
||||
.collect(groupingBy(BlogPost::getType));
|
||||
|
||||
assertEquals(2, postsPerType
|
||||
.get(BlogPostType.NEWS)
|
||||
.size());
|
||||
assertEquals(1, postsPerType
|
||||
.get(BlogPostType.GUIDE)
|
||||
.size());
|
||||
assertEquals(2, postsPerType
|
||||
.get(BlogPostType.REVIEW)
|
||||
.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListOfPosts_whenGroupedByTypeAndTheirTitlesAreJoinedInAString_thenGetAMapBetweenTypeAndCsvTitles() {
|
||||
Map<BlogPostType, String> postsPerType = posts
|
||||
.stream()
|
||||
.collect(groupingBy(BlogPost::getType, mapping(BlogPost::getTitle, joining(", ", "Post titles: [", "]"))));
|
||||
|
||||
assertEquals("Post titles: [News item 1, News item 2]", postsPerType.get(BlogPostType.NEWS));
|
||||
assertEquals("Post titles: [Programming guide]", postsPerType.get(BlogPostType.GUIDE));
|
||||
assertEquals("Post titles: [Tech review 1, Tech review 2]", postsPerType.get(BlogPostType.REVIEW));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListOfPosts_whenGroupedByTypeAndSumTheLikes_thenGetAMapBetweenTypeAndPostLikes() {
|
||||
Map<BlogPostType, Integer> likesPerType = posts
|
||||
.stream()
|
||||
.collect(groupingBy(BlogPost::getType, summingInt(BlogPost::getLikes)));
|
||||
|
||||
assertEquals(50, likesPerType
|
||||
.get(BlogPostType.NEWS)
|
||||
.intValue());
|
||||
assertEquals(20, likesPerType
|
||||
.get(BlogPostType.REVIEW)
|
||||
.intValue());
|
||||
assertEquals(20, likesPerType
|
||||
.get(BlogPostType.GUIDE)
|
||||
.intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListOfPosts_whenGroupedByTypeInAnEnumMap_thenGetAnEnumMapBetweenTypeAndPosts() {
|
||||
EnumMap<BlogPostType, List<BlogPost>> postsPerType = posts
|
||||
.stream()
|
||||
.collect(groupingBy(BlogPost::getType, () -> new EnumMap<>(BlogPostType.class), toList()));
|
||||
|
||||
assertEquals(2, postsPerType
|
||||
.get(BlogPostType.NEWS)
|
||||
.size());
|
||||
assertEquals(1, postsPerType
|
||||
.get(BlogPostType.GUIDE)
|
||||
.size());
|
||||
assertEquals(2, postsPerType
|
||||
.get(BlogPostType.REVIEW)
|
||||
.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListOfPosts_whenGroupedByTypeInSets_thenGetAMapBetweenTypesAndSetsOfPosts() {
|
||||
Map<BlogPostType, Set<BlogPost>> postsPerType = posts
|
||||
.stream()
|
||||
.collect(groupingBy(BlogPost::getType, toSet()));
|
||||
|
||||
assertEquals(2, postsPerType
|
||||
.get(BlogPostType.NEWS)
|
||||
.size());
|
||||
assertEquals(1, postsPerType
|
||||
.get(BlogPostType.GUIDE)
|
||||
.size());
|
||||
assertEquals(2, postsPerType
|
||||
.get(BlogPostType.REVIEW)
|
||||
.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListOfPosts_whenGroupedByTypeConcurrently_thenGetAMapBetweenTypeAndPosts() {
|
||||
ConcurrentMap<BlogPostType, List<BlogPost>> postsPerType = posts
|
||||
.parallelStream()
|
||||
.collect(groupingByConcurrent(BlogPost::getType));
|
||||
|
||||
assertEquals(2, postsPerType
|
||||
.get(BlogPostType.NEWS)
|
||||
.size());
|
||||
assertEquals(1, postsPerType
|
||||
.get(BlogPostType.GUIDE)
|
||||
.size());
|
||||
assertEquals(2, postsPerType
|
||||
.get(BlogPostType.REVIEW)
|
||||
.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListOfPosts_whenGroupedByTypeAndAveragingLikes_thenGetAMapBetweenTypeAndAverageNumberOfLikes() {
|
||||
Map<BlogPostType, Double> averageLikesPerType = posts
|
||||
.stream()
|
||||
.collect(groupingBy(BlogPost::getType, averagingInt(BlogPost::getLikes)));
|
||||
|
||||
assertEquals(25, averageLikesPerType
|
||||
.get(BlogPostType.NEWS)
|
||||
.intValue());
|
||||
assertEquals(20, averageLikesPerType
|
||||
.get(BlogPostType.GUIDE)
|
||||
.intValue());
|
||||
assertEquals(10, averageLikesPerType
|
||||
.get(BlogPostType.REVIEW)
|
||||
.intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListOfPosts_whenGroupedByTypeAndCounted_thenGetAMapBetweenTypeAndNumberOfPosts() {
|
||||
Map<BlogPostType, Long> numberOfPostsPerType = posts
|
||||
.stream()
|
||||
.collect(groupingBy(BlogPost::getType, counting()));
|
||||
|
||||
assertEquals(2, numberOfPostsPerType
|
||||
.get(BlogPostType.NEWS)
|
||||
.intValue());
|
||||
assertEquals(1, numberOfPostsPerType
|
||||
.get(BlogPostType.GUIDE)
|
||||
.intValue());
|
||||
assertEquals(2, numberOfPostsPerType
|
||||
.get(BlogPostType.REVIEW)
|
||||
.intValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListOfPosts_whenGroupedByTypeAndMaxingLikes_thenGetAMapBetweenTypeAndMaximumNumberOfLikes() {
|
||||
Map<BlogPostType, Optional<BlogPost>> maxLikesPerPostType = posts
|
||||
.stream()
|
||||
.collect(groupingBy(BlogPost::getType, maxBy(comparingInt(BlogPost::getLikes))));
|
||||
|
||||
assertTrue(maxLikesPerPostType
|
||||
.get(BlogPostType.NEWS)
|
||||
.isPresent());
|
||||
assertEquals(35, maxLikesPerPostType
|
||||
.get(BlogPostType.NEWS)
|
||||
.get()
|
||||
.getLikes());
|
||||
|
||||
assertTrue(maxLikesPerPostType
|
||||
.get(BlogPostType.GUIDE)
|
||||
.isPresent());
|
||||
assertEquals(20, maxLikesPerPostType
|
||||
.get(BlogPostType.GUIDE)
|
||||
.get()
|
||||
.getLikes());
|
||||
|
||||
assertTrue(maxLikesPerPostType
|
||||
.get(BlogPostType.REVIEW)
|
||||
.isPresent());
|
||||
assertEquals(15, maxLikesPerPostType
|
||||
.get(BlogPostType.REVIEW)
|
||||
.get()
|
||||
.getLikes());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListOfPosts_whenGroupedByAuthorAndThenByType_thenGetAMapBetweenAuthorAndMapsBetweenTypeAndBlogPosts() {
|
||||
Map<String, Map<BlogPostType, List<BlogPost>>> map = posts
|
||||
.stream()
|
||||
.collect(groupingBy(BlogPost::getAuthor, groupingBy(BlogPost::getType)));
|
||||
|
||||
assertEquals(1, map
|
||||
.get("Author 1")
|
||||
.get(BlogPostType.NEWS)
|
||||
.size());
|
||||
assertEquals(1, map
|
||||
.get("Author 1")
|
||||
.get(BlogPostType.GUIDE)
|
||||
.size());
|
||||
assertEquals(1, map
|
||||
.get("Author 1")
|
||||
.get(BlogPostType.REVIEW)
|
||||
.size());
|
||||
|
||||
assertEquals(1, map
|
||||
.get("Author 2")
|
||||
.get(BlogPostType.NEWS)
|
||||
.size());
|
||||
assertEquals(1, map
|
||||
.get("Author 2")
|
||||
.get(BlogPostType.REVIEW)
|
||||
.size());
|
||||
assertNull(map
|
||||
.get("Author 2")
|
||||
.get(BlogPostType.GUIDE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAListOfPosts_whenGroupedByTypeAndSummarizingLikes_thenGetAMapBetweenTypeAndSummary() {
|
||||
Map<BlogPostType, IntSummaryStatistics> likeStatisticsPerType = posts
|
||||
.stream()
|
||||
.collect(groupingBy(BlogPost::getType, summarizingInt(BlogPost::getLikes)));
|
||||
|
||||
IntSummaryStatistics newsLikeStatistics = likeStatisticsPerType.get(BlogPostType.NEWS);
|
||||
|
||||
assertEquals(2, newsLikeStatistics.getCount());
|
||||
assertEquals(50, newsLikeStatistics.getSum());
|
||||
assertEquals(25.0, newsLikeStatistics.getAverage(), 0.001);
|
||||
assertEquals(35, newsLikeStatistics.getMax());
|
||||
assertEquals(15, newsLikeStatistics.getMin());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user