|
| 1 | +package org.baeldung.aggregation; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | +import static org.springframework.data.mongodb.core.aggregation.Aggregation.*; |
| 5 | + |
| 6 | +import java.io.BufferedReader; |
| 7 | +import java.io.InputStream; |
| 8 | +import java.io.InputStreamReader; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.Collections; |
| 11 | +import java.util.List; |
| 12 | +import java.util.stream.Collectors; |
| 13 | +import java.util.stream.StreamSupport; |
| 14 | + |
| 15 | +import org.baeldung.aggregation.model.StatePopulation; |
| 16 | +import org.baeldung.config.MongoConfig; |
| 17 | +import org.junit.AfterClass; |
| 18 | +import org.junit.BeforeClass; |
| 19 | +import org.junit.Test; |
| 20 | +import org.junit.runner.RunWith; |
| 21 | +import org.springframework.beans.factory.annotation.Autowired; |
| 22 | +import org.springframework.data.domain.Sort; |
| 23 | +import org.springframework.data.domain.Sort.Direction; |
| 24 | +import org.springframework.data.mongodb.core.MongoTemplate; |
| 25 | +import org.springframework.data.mongodb.core.aggregation.Aggregation; |
| 26 | +import org.springframework.data.mongodb.core.aggregation.AggregationResults; |
| 27 | +import org.springframework.data.mongodb.core.aggregation.GroupOperation; |
| 28 | +import org.springframework.data.mongodb.core.aggregation.LimitOperation; |
| 29 | +import org.springframework.data.mongodb.core.aggregation.MatchOperation; |
| 30 | +import org.springframework.data.mongodb.core.aggregation.ProjectionOperation; |
| 31 | +import org.springframework.data.mongodb.core.aggregation.SortOperation; |
| 32 | +import org.springframework.data.mongodb.core.query.Criteria; |
| 33 | +import org.springframework.test.context.ContextConfiguration; |
| 34 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 35 | + |
| 36 | +import com.mongodb.DB; |
| 37 | +import com.mongodb.DBCollection; |
| 38 | +import com.mongodb.DBObject; |
| 39 | +import com.mongodb.MongoClient; |
| 40 | +import com.mongodb.util.JSON; |
| 41 | + |
| 42 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 43 | +@ContextConfiguration(classes = MongoConfig.class) |
| 44 | +public class ZipsAggregationLiveTest { |
| 45 | + |
| 46 | + private static MongoClient client; |
| 47 | + |
| 48 | + @Autowired |
| 49 | + private MongoTemplate mongoTemplate; |
| 50 | + |
| 51 | + @BeforeClass |
| 52 | + public static void setupTests() throws Exception { |
| 53 | + client = new MongoClient(); |
| 54 | + DB testDB = client.getDB("test"); |
| 55 | + DBCollection zipsCollection = testDB.getCollection("zips"); |
| 56 | + zipsCollection.drop(); |
| 57 | + |
| 58 | + InputStream zipsJsonStream = ZipsAggregationLiveTest.class.getResourceAsStream("/zips.json"); |
| 59 | + BufferedReader reader = new BufferedReader(new InputStreamReader(zipsJsonStream)); |
| 60 | + reader.lines() |
| 61 | + .forEach(line -> zipsCollection.insert((DBObject) JSON.parse(line))); |
| 62 | + reader.close(); |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + @AfterClass |
| 67 | + public static void tearDown() throws Exception { |
| 68 | + client = new MongoClient(); |
| 69 | + DB testDB = client.getDB("test"); |
| 70 | + DBCollection zipsCollection = testDB.getCollection("zips"); |
| 71 | + zipsCollection.drop(); |
| 72 | + client.close(); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void whenStatesHavePopGrtrThan10MillionAndSorted_thenSuccess() { |
| 77 | + |
| 78 | + GroupOperation groupByStateAndSumPop = group("state").sum("pop").as("statePop"); |
| 79 | + MatchOperation filterStates = match(new Criteria("statePop").gt(10000000)); |
| 80 | + SortOperation sortByPopDesc = sort(new Sort(Direction.DESC, "statePop")); |
| 81 | + |
| 82 | + Aggregation aggregation = newAggregation(groupByStateAndSumPop, filterStates, sortByPopDesc); |
| 83 | + AggregationResults<StatePopulation> result = mongoTemplate.aggregate(aggregation, "zips", StatePopulation.class); |
| 84 | + |
| 85 | + /* |
| 86 | + * Assert that all states have population |
| 87 | + * greater than 10000000 |
| 88 | + */ |
| 89 | + result.forEach(statePop -> { |
| 90 | + assertTrue(statePop.getStatePop() > 10000000); |
| 91 | + }); |
| 92 | + |
| 93 | + /* |
| 94 | + * Assert that states fetched are in sorted by |
| 95 | + * decreasing population |
| 96 | + */ |
| 97 | + List<StatePopulation> actualList = StreamSupport.stream(result.spliterator(), false) |
| 98 | + .collect(Collectors.toList()); |
| 99 | + |
| 100 | + List<StatePopulation> expectedList = new ArrayList<>(actualList); |
| 101 | + Collections.sort(expectedList, (sp1, sp2) -> sp2.getStatePop() - sp1.getStatePop()); |
| 102 | + |
| 103 | + assertEquals(expectedList, actualList); |
| 104 | + |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void whenStateWithLowestAvgCityPopIsND_theSuccess() { |
| 109 | + |
| 110 | + GroupOperation sumTotalCityPop = group("state", "city").sum("pop").as("cityPop"); |
| 111 | + GroupOperation averageStatePop = group("_id.state").avg("cityPop").as("avgCityPop"); |
| 112 | + SortOperation sortByAvgPopAsc = sort(new Sort(Direction.ASC, "avgCityPop")); |
| 113 | + ProjectionOperation projectToMatchModel = project().andExpression("_id").as("state") |
| 114 | + .andExpression("avgCityPop").as("statePop"); |
| 115 | + LimitOperation limitToOnlyFirstDoc = limit(1); |
| 116 | + |
| 117 | + Aggregation aggregation = newAggregation(sumTotalCityPop, averageStatePop, sortByAvgPopAsc, limitToOnlyFirstDoc, projectToMatchModel); |
| 118 | + |
| 119 | + AggregationResults<StatePopulation> result = mongoTemplate.aggregate(aggregation, "zips", StatePopulation.class); |
| 120 | + StatePopulation smallestState = result.getUniqueMappedResult(); |
| 121 | + |
| 122 | + assertEquals("ND", smallestState.getState()); |
| 123 | + assertTrue(smallestState.getStatePop() |
| 124 | + .equals(1645)); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void whenMaxTXAndMinDC_theSuccess() { |
| 129 | + |
| 130 | + GroupOperation sumZips = group("state").count().as("zipCount"); |
| 131 | + SortOperation sortByCount = sort(Direction.ASC, "zipCount"); |
| 132 | + GroupOperation groupFirstAndLast = group().first("_id").as("minZipState") |
| 133 | + .first("zipCount").as("minZipCount").last("_id").as("maxZipState") |
| 134 | + .last("zipCount").as("maxZipCount"); |
| 135 | + |
| 136 | + Aggregation aggregation = newAggregation(sumZips, sortByCount, groupFirstAndLast); |
| 137 | + |
| 138 | + AggregationResults<DBObject> result = mongoTemplate.aggregate(aggregation, "zips", DBObject.class); |
| 139 | + DBObject dbObject = result.getUniqueMappedResult(); |
| 140 | + |
| 141 | + assertEquals("DC", dbObject.get("minZipState")); |
| 142 | + assertEquals(24, dbObject.get("minZipCount")); |
| 143 | + assertEquals("TX", dbObject.get("maxZipState")); |
| 144 | + assertEquals(1671, dbObject.get("maxZipCount")); |
| 145 | + } |
| 146 | + |
| 147 | +} |
0 commit comments