Versions update (#2234)
* Mockito refactor * Httpclient refactor * Refactor json-path * MongoDB Refactor
This commit is contained in:
parent
1957750382
commit
9ad123a655
@ -39,7 +39,7 @@
|
|||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<javax.servlet.version>3.1.0</javax.servlet.version>
|
<javax.servlet.version>3.1.0</javax.servlet.version>
|
||||||
<org.apache.httpcomponents.version>4.5.2</org.apache.httpcomponents.version>
|
<org.apache.httpcomponents.version>4.5.3</org.apache.httpcomponents.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -1,24 +1,24 @@
|
|||||||
package com.baeldung.servlets;
|
package com.baeldung.servlets;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
import org.apache.http.client.HttpClient;
|
import org.apache.http.client.HttpClient;
|
||||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||||
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.client.methods.HttpPost;
|
||||||
import org.apache.http.impl.client.DefaultHttpClient;
|
import org.apache.http.impl.client.HttpClientBuilder;
|
||||||
import org.apache.http.message.BasicNameValuePair;
|
import org.apache.http.message.BasicNameValuePair;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
public class FormServletLiveTest {
|
public class FormServletLiveTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenPostRequestUsingHttpClient_thenCorrect() throws Exception {
|
public void whenPostRequestUsingHttpClient_thenCorrect() throws Exception {
|
||||||
|
|
||||||
HttpClient client = new DefaultHttpClient();
|
HttpClient client = HttpClientBuilder.create().build();
|
||||||
HttpPost method = new HttpPost("http://localhost:8080/calculateServlet");
|
HttpPost method = new HttpPost("http://localhost:8080/calculateServlet");
|
||||||
|
|
||||||
List<BasicNameValuePair> nvps = new ArrayList<>();
|
List<BasicNameValuePair> nvps = new ArrayList<>();
|
||||||
|
@ -23,6 +23,6 @@
|
|||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<!-- json-path -->
|
<!-- json-path -->
|
||||||
<json-path.version>2.2.0</json-path.version>
|
<json-path.version>2.4.0</json-path.version>
|
||||||
</properties>
|
</properties>
|
||||||
</project>
|
</project>
|
@ -1,10 +1,10 @@
|
|||||||
package com.baeldung.jsonpath.introduction;
|
package com.baeldung.jsonpath.introduction;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import com.jayway.jsonpath.Criteria;
|
||||||
import static org.junit.Assert.assertThat;
|
import com.jayway.jsonpath.DocumentContext;
|
||||||
import static org.hamcrest.CoreMatchers.containsString;
|
import com.jayway.jsonpath.Filter;
|
||||||
import static org.hamcrest.CoreMatchers.not;
|
import com.jayway.jsonpath.JsonPath;
|
||||||
|
import com.jayway.jsonpath.Predicate;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@ -12,15 +12,14 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
import com.jayway.jsonpath.Criteria;
|
import static org.hamcrest.CoreMatchers.containsString;
|
||||||
import com.jayway.jsonpath.DocumentContext;
|
import static org.hamcrest.CoreMatchers.not;
|
||||||
import com.jayway.jsonpath.Filter;
|
import static org.junit.Assert.assertEquals;
|
||||||
import com.jayway.jsonpath.JsonPath;
|
import static org.junit.Assert.assertThat;
|
||||||
import com.jayway.jsonpath.Predicate;
|
|
||||||
|
|
||||||
public class OperationIntegrationTest {
|
public class OperationIntegrationTest {
|
||||||
InputStream jsonInputStream = this.getClass().getClassLoader().getResourceAsStream("intro_api.json");
|
private InputStream jsonInputStream = this.getClass().getClassLoader().getResourceAsStream("intro_api.json");
|
||||||
String jsonDataSourceString = new Scanner(jsonInputStream, "UTF-8").useDelimiter("\\Z").next();
|
private String jsonDataSourceString = new Scanner(jsonInputStream, "UTF-8").useDelimiter("\\Z").next();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenJsonPathWithoutPredicates_whenReading_thenCorrect() {
|
public void givenJsonPathWithoutPredicates_whenReading_thenCorrect() {
|
||||||
@ -46,12 +45,7 @@ public class OperationIntegrationTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenJsonPathWithCustomizedPredicate_whenReading_thenCorrect() {
|
public void givenJsonPathWithCustomizedPredicate_whenReading_thenCorrect() {
|
||||||
Predicate expensivePredicate = new Predicate() {
|
Predicate expensivePredicate = context -> Float.valueOf(context.item(Map.class).get("price").toString()) > 20.00;
|
||||||
public boolean apply(PredicateContext context) {
|
|
||||||
String value = context.item(Map.class).get("price").toString();
|
|
||||||
return Float.valueOf(value) > 20.00;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
List<Map<String, Object>> expensive = JsonPath.parse(jsonDataSourceString).read("$['book'][?]", expensivePredicate);
|
List<Map<String, Object>> expensive = JsonPath.parse(jsonDataSourceString).read("$['book'][?]", expensivePredicate);
|
||||||
predicateUsageAssertionHelper(expensive);
|
predicateUsageAssertionHelper(expensive);
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
package com.baeldung.jsonpath.introduction;
|
package com.baeldung.jsonpath.introduction;
|
||||||
|
|
||||||
import static org.junit.Assert.assertThat;
|
import com.jayway.jsonpath.Configuration;
|
||||||
import static org.junit.Assert.assertEquals;
|
import com.jayway.jsonpath.DocumentContext;
|
||||||
import static org.hamcrest.CoreMatchers.containsString;
|
import com.jayway.jsonpath.JsonPath;
|
||||||
|
import com.jayway.jsonpath.Option;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@ -13,14 +13,13 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
import com.jayway.jsonpath.Configuration;
|
import static org.hamcrest.CoreMatchers.containsString;
|
||||||
import com.jayway.jsonpath.DocumentContext;
|
import static org.junit.Assert.assertEquals;
|
||||||
import com.jayway.jsonpath.JsonPath;
|
import static org.junit.Assert.assertThat;
|
||||||
import com.jayway.jsonpath.Option;
|
|
||||||
|
|
||||||
public class ServiceTest {
|
public class ServiceTest {
|
||||||
InputStream jsonInputStream = this.getClass().getClassLoader().getResourceAsStream("intro_service.json");
|
private InputStream jsonInputStream = this.getClass().getClassLoader().getResourceAsStream("intro_service.json");
|
||||||
String jsonString = new Scanner(jsonInputStream, "UTF-8").useDelimiter("\\Z").next();
|
private String jsonString = new Scanner(jsonInputStream, "UTF-8").useDelimiter("\\Z").next();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenId_whenRequestingRecordData_thenSucceed() {
|
public void givenId_whenRequestingRecordData_thenSucceed() {
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
package com.baeldung.powermockito.introduction;
|
package com.baeldung.powermockito.introduction;
|
||||||
|
|
||||||
public class CollaboratorForPartialMocking {
|
class CollaboratorForPartialMocking {
|
||||||
|
|
||||||
public static String staticMethod() {
|
static String staticMethod() {
|
||||||
return "Hello Baeldung!";
|
return "Hello Baeldung!";
|
||||||
}
|
}
|
||||||
|
|
||||||
public final String finalMethod() {
|
final String finalMethod() {
|
||||||
return "Hello Baeldung!";
|
return "Hello Baeldung!";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -14,7 +14,7 @@ public class CollaboratorForPartialMocking {
|
|||||||
return "Hello Baeldung!";
|
return "Hello Baeldung!";
|
||||||
}
|
}
|
||||||
|
|
||||||
public String privateMethodCaller() {
|
String privateMethodCaller() {
|
||||||
return privateMethod() + " Welcome to the Java world.";
|
return privateMethod() + " Welcome to the Java world.";
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,8 +1,8 @@
|
|||||||
package com.baeldung.powermockito.introduction;
|
package com.baeldung.powermockito.introduction;
|
||||||
|
|
||||||
public class CollaboratorWithFinalMethods {
|
class CollaboratorWithFinalMethods {
|
||||||
|
|
||||||
public final String helloMethod() {
|
final String helloMethod() {
|
||||||
return "Hello World!";
|
return "Hello World!";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
package com.baeldung.powermockito.introduction;
|
package com.baeldung.powermockito.introduction;
|
||||||
|
|
||||||
public class CollaboratorWithStaticMethods {
|
class CollaboratorWithStaticMethods {
|
||||||
|
|
||||||
public static String firstMethod(String name) {
|
static String firstMethod(String name) {
|
||||||
return "Hello " + name + " !";
|
return "Hello " + name + " !";
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String secondMethod() {
|
static String secondMethod() {
|
||||||
return "Hello no one!";
|
return "Hello no one!";
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String thirdMethod() {
|
static String thirdMethod() {
|
||||||
return "Hello no one again!";
|
return "Hello no one again!";
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -17,7 +17,7 @@ public class MockitoAnnotationIntegrationTest {
|
|||||||
private List<String> mockedList;
|
private List<String> mockedList;
|
||||||
|
|
||||||
@Spy
|
@Spy
|
||||||
List<String> spiedList = new ArrayList<String>();
|
private List<String> spiedList = new ArrayList<>();
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void init() {
|
public void init() {
|
||||||
@ -87,6 +87,7 @@ public class MockitoAnnotationIntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Captor
|
@Captor
|
||||||
|
private
|
||||||
ArgumentCaptor<String> argCaptor;
|
ArgumentCaptor<String> argCaptor;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -98,10 +99,10 @@ public class MockitoAnnotationIntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
Map<String, String> wordMap;
|
private Map<String, String> wordMap;
|
||||||
|
|
||||||
@InjectMocks
|
@InjectMocks
|
||||||
MyDictionary dic = new MyDictionary();
|
private MyDictionary dic = new MyDictionary();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUseInjectMocksAnnotation_thenCorrect() {
|
public void whenUseInjectMocksAnnotation_thenCorrect() {
|
||||||
|
@ -24,7 +24,7 @@ public class MockitoMockIntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public ExpectedException thrown = ExpectedException.none();
|
private ExpectedException thrown = ExpectedException.none();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUsingSimpleMock_thenCorrect() {
|
public void whenUsingSimpleMock_thenCorrect() {
|
||||||
|
@ -3,19 +3,19 @@ package org.baeldung.mockito;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class MyDictionary {
|
class MyDictionary {
|
||||||
|
|
||||||
Map<String, String> wordMap;
|
private Map<String, String> wordMap;
|
||||||
|
|
||||||
public MyDictionary() {
|
MyDictionary() {
|
||||||
wordMap = new HashMap<String, String>();
|
wordMap = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void add(final String word, final String meaning) {
|
public void add(final String word, final String meaning) {
|
||||||
wordMap.put(word, meaning);
|
wordMap.put(word, meaning);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getMeaning(final String word) {
|
String getMeaning(final String word) {
|
||||||
return wordMap.get(word);
|
return wordMap.get(word);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ package org.baeldung.mockito;
|
|||||||
|
|
||||||
import java.util.AbstractList;
|
import java.util.AbstractList;
|
||||||
|
|
||||||
public class MyList extends AbstractList<String> {
|
class MyList extends AbstractList<String> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String get(final int index) {
|
public String get(final int index) {
|
||||||
|
@ -8,5 +8,4 @@ import java.lang.annotation.Target;
|
|||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Target(ElementType.FIELD)
|
@Target(ElementType.FIELD)
|
||||||
public @interface CascadeSave {
|
public @interface CascadeSave {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
package org.baeldung.config;
|
package org.baeldung.config;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import com.mongodb.Mongo;
|
||||||
import java.util.List;
|
import com.mongodb.MongoClient;
|
||||||
|
|
||||||
import org.baeldung.converter.UserWriterConverter;
|
import org.baeldung.converter.UserWriterConverter;
|
||||||
import org.baeldung.event.CascadeSaveMongoEventListener;
|
import org.baeldung.event.CascadeSaveMongoEventListener;
|
||||||
import org.baeldung.event.UserCascadeSaveMongoEventListener;
|
import org.baeldung.event.UserCascadeSaveMongoEventListener;
|
||||||
@ -14,8 +13,8 @@ import org.springframework.data.mongodb.core.convert.CustomConversions;
|
|||||||
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
|
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
|
||||||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
||||||
|
|
||||||
import com.mongodb.Mongo;
|
import java.util.ArrayList;
|
||||||
import com.mongodb.MongoClient;
|
import java.util.List;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableMongoRepositories(basePackages = "org.baeldung.repository")
|
@EnableMongoRepositories(basePackages = "org.baeldung.repository")
|
||||||
|
@ -1,13 +1,12 @@
|
|||||||
package org.baeldung.config;
|
package org.baeldung.config;
|
||||||
|
|
||||||
|
import com.mongodb.Mongo;
|
||||||
|
import com.mongodb.MongoClient;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
||||||
|
|
||||||
import com.mongodb.Mongo;
|
|
||||||
import com.mongodb.MongoClient;
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableMongoRepositories(basePackages = "org.baeldung.repository")
|
@EnableMongoRepositories(basePackages = "org.baeldung.repository")
|
||||||
public class SimpleMongoConfig {
|
public class SimpleMongoConfig {
|
||||||
|
@ -1,11 +1,10 @@
|
|||||||
package org.baeldung.converter;
|
package org.baeldung.converter;
|
||||||
|
|
||||||
import org.baeldung.model.User;
|
|
||||||
import org.springframework.core.convert.converter.Converter;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
import com.mongodb.BasicDBObject;
|
import com.mongodb.BasicDBObject;
|
||||||
import com.mongodb.DBObject;
|
import com.mongodb.DBObject;
|
||||||
|
import org.baeldung.model.User;
|
||||||
|
import org.springframework.core.convert.converter.Converter;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class UserWriterConverter implements Converter<User, DBObject> {
|
public class UserWriterConverter implements Converter<User, DBObject> {
|
||||||
|
@ -12,7 +12,7 @@ public class CascadeCallback implements ReflectionUtils.FieldCallback {
|
|||||||
private Object source;
|
private Object source;
|
||||||
private MongoOperations mongoOperations;
|
private MongoOperations mongoOperations;
|
||||||
|
|
||||||
public CascadeCallback(final Object source, final MongoOperations mongoOperations) {
|
CascadeCallback(final Object source, final MongoOperations mongoOperations) {
|
||||||
this.source = source;
|
this.source = source;
|
||||||
this.setMongoOperations(mongoOperations);
|
this.setMongoOperations(mongoOperations);
|
||||||
}
|
}
|
||||||
@ -35,7 +35,7 @@ public class CascadeCallback implements ReflectionUtils.FieldCallback {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object getSource() {
|
private Object getSource() {
|
||||||
return source;
|
return source;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,11 +43,11 @@ public class CascadeCallback implements ReflectionUtils.FieldCallback {
|
|||||||
this.source = source;
|
this.source = source;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MongoOperations getMongoOperations() {
|
private MongoOperations getMongoOperations() {
|
||||||
return mongoOperations;
|
return mongoOperations;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setMongoOperations(final MongoOperations mongoOperations) {
|
private void setMongoOperations(final MongoOperations mongoOperations) {
|
||||||
this.mongoOperations = mongoOperations;
|
this.mongoOperations = mongoOperations;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
package org.baeldung.repository;
|
package org.baeldung.repository;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.baeldung.model.User;
|
import org.baeldung.model.User;
|
||||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||||
import org.springframework.data.mongodb.repository.Query;
|
import org.springframework.data.mongodb.repository.Query;
|
||||||
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
|
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public interface UserRepository extends MongoRepository<User, String>, QueryDslPredicateExecutor<User> {
|
public interface UserRepository extends MongoRepository<User, String>, QueryDslPredicateExecutor<User> {
|
||||||
@Query("{ 'name' : ?0 }")
|
@Query("{ 'name' : ?0 }")
|
||||||
List<User> findUsersByName(String name);
|
List<User> findUsersByName(String name);
|
||||||
@ -27,9 +27,9 @@ public interface UserRepository extends MongoRepository<User, String>, QueryDslP
|
|||||||
|
|
||||||
List<User> findByNameEndingWith(String regexp);
|
List<User> findByNameEndingWith(String regexp);
|
||||||
|
|
||||||
@Query(value="{}", fields="{name : 1}")
|
@Query(value = "{}", fields = "{name : 1}")
|
||||||
List<User> findNameAndId();
|
List<User> findNameAndId();
|
||||||
|
|
||||||
@Query(value="{}", fields="{_id : 0}")
|
@Query(value = "{}", fields = "{_id : 0}")
|
||||||
List<User> findNameAndAgeExcludeId();
|
List<User> findNameAndAgeExcludeId();
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,10 @@
|
|||||||
package org.baeldung.aggregation;
|
package org.baeldung.aggregation;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import com.mongodb.DB;
|
||||||
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
|
import com.mongodb.DBCollection;
|
||||||
|
import com.mongodb.DBObject;
|
||||||
import java.io.BufferedReader;
|
import com.mongodb.MongoClient;
|
||||||
import java.io.InputStream;
|
import com.mongodb.util.JSON;
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
import java.util.stream.StreamSupport;
|
|
||||||
|
|
||||||
import org.baeldung.aggregation.model.StatePopulation;
|
import org.baeldung.aggregation.model.StatePopulation;
|
||||||
import org.baeldung.config.MongoConfig;
|
import org.baeldung.config.MongoConfig;
|
||||||
import org.junit.AfterClass;
|
import org.junit.AfterClass;
|
||||||
@ -33,11 +26,23 @@ import org.springframework.data.mongodb.core.query.Criteria;
|
|||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
import com.mongodb.DB;
|
import java.io.BufferedReader;
|
||||||
import com.mongodb.DBCollection;
|
import java.io.InputStream;
|
||||||
import com.mongodb.DBObject;
|
import java.io.InputStreamReader;
|
||||||
import com.mongodb.MongoClient;
|
import java.util.ArrayList;
|
||||||
import com.mongodb.util.JSON;
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.StreamSupport;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.springframework.data.mongodb.core.aggregation.Aggregation.group;
|
||||||
|
import static org.springframework.data.mongodb.core.aggregation.Aggregation.limit;
|
||||||
|
import static org.springframework.data.mongodb.core.aggregation.Aggregation.match;
|
||||||
|
import static org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation;
|
||||||
|
import static org.springframework.data.mongodb.core.aggregation.Aggregation.project;
|
||||||
|
import static org.springframework.data.mongodb.core.aggregation.Aggregation.sort;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = MongoConfig.class)
|
@ContextConfiguration(classes = MongoConfig.class)
|
||||||
@ -58,7 +63,7 @@ public class ZipsAggregationLiveTest {
|
|||||||
InputStream zipsJsonStream = ZipsAggregationLiveTest.class.getResourceAsStream("/zips.json");
|
InputStream zipsJsonStream = ZipsAggregationLiveTest.class.getResourceAsStream("/zips.json");
|
||||||
BufferedReader reader = new BufferedReader(new InputStreamReader(zipsJsonStream));
|
BufferedReader reader = new BufferedReader(new InputStreamReader(zipsJsonStream));
|
||||||
reader.lines()
|
reader.lines()
|
||||||
.forEach(line -> zipsCollection.insert((DBObject) JSON.parse(line)));
|
.forEach(line -> zipsCollection.insert((DBObject) JSON.parse(line)));
|
||||||
reader.close();
|
reader.close();
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -95,7 +100,7 @@ public class ZipsAggregationLiveTest {
|
|||||||
* decreasing population
|
* decreasing population
|
||||||
*/
|
*/
|
||||||
List<StatePopulation> actualList = StreamSupport.stream(result.spliterator(), false)
|
List<StatePopulation> actualList = StreamSupport.stream(result.spliterator(), false)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
List<StatePopulation> expectedList = new ArrayList<>(actualList);
|
List<StatePopulation> expectedList = new ArrayList<>(actualList);
|
||||||
Collections.sort(expectedList, (sp1, sp2) -> sp2.getStatePop() - sp1.getStatePop());
|
Collections.sort(expectedList, (sp1, sp2) -> sp2.getStatePop() - sp1.getStatePop());
|
||||||
@ -111,7 +116,7 @@ public class ZipsAggregationLiveTest {
|
|||||||
GroupOperation averageStatePop = group("_id.state").avg("cityPop").as("avgCityPop");
|
GroupOperation averageStatePop = group("_id.state").avg("cityPop").as("avgCityPop");
|
||||||
SortOperation sortByAvgPopAsc = sort(new Sort(Direction.ASC, "avgCityPop"));
|
SortOperation sortByAvgPopAsc = sort(new Sort(Direction.ASC, "avgCityPop"));
|
||||||
ProjectionOperation projectToMatchModel = project().andExpression("_id").as("state")
|
ProjectionOperation projectToMatchModel = project().andExpression("_id").as("state")
|
||||||
.andExpression("avgCityPop").as("statePop");
|
.andExpression("avgCityPop").as("statePop");
|
||||||
LimitOperation limitToOnlyFirstDoc = limit(1);
|
LimitOperation limitToOnlyFirstDoc = limit(1);
|
||||||
|
|
||||||
Aggregation aggregation = newAggregation(sumTotalCityPop, averageStatePop, sortByAvgPopAsc, limitToOnlyFirstDoc, projectToMatchModel);
|
Aggregation aggregation = newAggregation(sumTotalCityPop, averageStatePop, sortByAvgPopAsc, limitToOnlyFirstDoc, projectToMatchModel);
|
||||||
@ -121,7 +126,7 @@ public class ZipsAggregationLiveTest {
|
|||||||
|
|
||||||
assertEquals("ND", smallestState.getState());
|
assertEquals("ND", smallestState.getState());
|
||||||
assertTrue(smallestState.getStatePop()
|
assertTrue(smallestState.getStatePop()
|
||||||
.equals(1645));
|
.equals(1645));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -130,8 +135,8 @@ public class ZipsAggregationLiveTest {
|
|||||||
GroupOperation sumZips = group("state").count().as("zipCount");
|
GroupOperation sumZips = group("state").count().as("zipCount");
|
||||||
SortOperation sortByCount = sort(Direction.ASC, "zipCount");
|
SortOperation sortByCount = sort(Direction.ASC, "zipCount");
|
||||||
GroupOperation groupFirstAndLast = group().first("_id").as("minZipState")
|
GroupOperation groupFirstAndLast = group().first("_id").as("minZipState")
|
||||||
.first("zipCount").as("minZipCount").last("_id").as("maxZipState")
|
.first("zipCount").as("minZipCount").last("_id").as("maxZipState")
|
||||||
.last("zipCount").as("maxZipCount");
|
.last("zipCount").as("maxZipCount");
|
||||||
|
|
||||||
Aggregation aggregation = newAggregation(sumZips, sortByCount, groupFirstAndLast);
|
Aggregation aggregation = newAggregation(sumZips, sortByCount, groupFirstAndLast);
|
||||||
|
|
||||||
|
@ -1,19 +1,8 @@
|
|||||||
package org.baeldung.gridfs;
|
package org.baeldung.gridfs;
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.is;
|
import com.mongodb.BasicDBObject;
|
||||||
import static org.hamcrest.Matchers.nullValue;
|
import com.mongodb.DBObject;
|
||||||
import static org.junit.Assert.assertEquals;
|
import com.mongodb.gridfs.GridFSDBFile;
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
import static org.junit.Assert.assertNull;
|
|
||||||
import static org.junit.Assert.assertThat;
|
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.baeldung.config.MongoConfig;
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@ -27,9 +16,18 @@ import org.springframework.data.mongodb.gridfs.GridFsTemplate;
|
|||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
import com.mongodb.BasicDBObject;
|
import java.io.FileInputStream;
|
||||||
import com.mongodb.DBObject;
|
import java.io.FileNotFoundException;
|
||||||
import com.mongodb.gridfs.GridFSDBFile;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
|
import static org.hamcrest.Matchers.nullValue;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
@ContextConfiguration("file:src/main/resources/mongoConfig.xml")
|
@ContextConfiguration("file:src/main/resources/mongoConfig.xml")
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ -1,11 +1,5 @@
|
|||||||
package org.baeldung.mongotemplate;
|
package org.baeldung.mongotemplate;
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.is;
|
|
||||||
import static org.junit.Assert.assertThat;
|
|
||||||
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.baeldung.config.MongoConfig;
|
import org.baeldung.config.MongoConfig;
|
||||||
import org.baeldung.model.EmailAddress;
|
import org.baeldung.model.EmailAddress;
|
||||||
import org.baeldung.model.User;
|
import org.baeldung.model.User;
|
||||||
@ -23,6 +17,12 @@ import org.springframework.data.mongodb.core.query.Query;
|
|||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = MongoConfig.class)
|
@ContextConfiguration(classes = MongoConfig.class)
|
||||||
public class DocumentQueryLiveTest {
|
public class DocumentQueryLiveTest {
|
||||||
|
@ -1,9 +1,5 @@
|
|||||||
package org.baeldung.mongotemplate;
|
package org.baeldung.mongotemplate;
|
||||||
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
import static org.junit.Assert.assertNull;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
import org.baeldung.config.SimpleMongoConfig;
|
import org.baeldung.config.SimpleMongoConfig;
|
||||||
import org.baeldung.model.User;
|
import org.baeldung.model.User;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
@ -16,6 +12,10 @@ import org.springframework.data.mongodb.core.query.Query;
|
|||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = SimpleMongoConfig.class)
|
@ContextConfiguration(classes = SimpleMongoConfig.class)
|
||||||
public class MongoTemplateProjectionLiveTest {
|
public class MongoTemplateProjectionLiveTest {
|
||||||
@ -42,14 +42,14 @@ public class MongoTemplateProjectionLiveTest {
|
|||||||
|
|
||||||
final Query query = new Query();
|
final Query query = new Query();
|
||||||
query.fields()
|
query.fields()
|
||||||
.include("name");
|
.include("name");
|
||||||
|
|
||||||
mongoTemplate.find(query, User.class)
|
mongoTemplate.find(query, User.class)
|
||||||
.forEach(user -> {
|
.forEach(user -> {
|
||||||
assertNotNull(user.getName());
|
assertNotNull(user.getName());
|
||||||
assertTrue(user.getAge()
|
assertTrue(user.getAge()
|
||||||
.equals(0));
|
.equals(0));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -59,13 +59,13 @@ public class MongoTemplateProjectionLiveTest {
|
|||||||
|
|
||||||
final Query query = new Query();
|
final Query query = new Query();
|
||||||
query.fields()
|
query.fields()
|
||||||
.exclude("_id");
|
.exclude("_id");
|
||||||
|
|
||||||
mongoTemplate.find(query, User.class)
|
mongoTemplate.find(query, User.class)
|
||||||
.forEach(user -> {
|
.forEach(user -> {
|
||||||
assertNull(user.getId());
|
assertNull(user.getId());
|
||||||
assertNotNull(user.getAge());
|
assertNotNull(user.getAge());
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,11 +1,5 @@
|
|||||||
package org.baeldung.mongotemplate;
|
package org.baeldung.mongotemplate;
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.is;
|
|
||||||
import static org.junit.Assert.assertThat;
|
|
||||||
import static org.hamcrest.Matchers.nullValue;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.baeldung.config.MongoConfig;
|
import org.baeldung.config.MongoConfig;
|
||||||
import org.baeldung.model.EmailAddress;
|
import org.baeldung.model.EmailAddress;
|
||||||
import org.baeldung.model.User;
|
import org.baeldung.model.User;
|
||||||
@ -26,6 +20,12 @@ import org.springframework.data.mongodb.core.query.Query;
|
|||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.hamcrest.CoreMatchers.is;
|
||||||
|
import static org.hamcrest.Matchers.nullValue;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = MongoConfig.class)
|
@ContextConfiguration(classes = MongoConfig.class)
|
||||||
public class MongoTemplateQueryLiveTest {
|
public class MongoTemplateQueryLiveTest {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user