Merge branch 'nguyennamthai-jsonpath'
This commit is contained in:
commit
115bc9f044
|
@ -14,13 +14,6 @@
|
|||
<version>${json-path.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- utilities -->
|
||||
<dependency>
|
||||
<groupId>joda-time</groupId>
|
||||
<artifactId>joda-time</artifactId>
|
||||
<version>${joda-time.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Testing -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
|
@ -44,13 +37,24 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.5.1</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<!-- json-path -->
|
||||
<json-path.version>2.1.0</json-path.version>
|
||||
|
||||
<!-- utilities -->
|
||||
<joda-time.version>2.9.2</joda-time.version>
|
||||
|
||||
<!-- Testing -->
|
||||
<junit.version>4.12</junit.version>
|
||||
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
[
|
||||
{
|
||||
"id": 1,
|
||||
"title": "Casino Royale",
|
||||
"director": "Martin Campbell",
|
||||
"starring":
|
||||
[
|
||||
"Daniel Craig",
|
||||
"Eva Green"
|
||||
],
|
||||
|
||||
"desc": "Twenty-first James Bond movie",
|
||||
"release date": 1163466000000,
|
||||
"box office": 594275385
|
||||
},
|
||||
|
||||
{
|
||||
"id": 2,
|
||||
"title": "Quantum of Solace",
|
||||
"director": "Marc Forster",
|
||||
"starring":
|
||||
[
|
||||
"Daniel Craig",
|
||||
"Olga Kurylenko"
|
||||
],
|
||||
|
||||
"desc": "Twenty-second James Bond movie",
|
||||
"release date": 1225242000000,
|
||||
"box office": 591692078
|
||||
},
|
||||
|
||||
{
|
||||
"id": 3,
|
||||
"title": "Skyfall",
|
||||
"director": "Sam Mendes",
|
||||
"starring":
|
||||
[
|
||||
"Daniel Craig",
|
||||
"Naomie Harris"
|
||||
],
|
||||
|
||||
"desc": "Twenty-third James Bond movie",
|
||||
"release date": 1350954000000,
|
||||
"box office": 1110526981
|
||||
},
|
||||
|
||||
{
|
||||
"id": 4,
|
||||
"title": "Spectre",
|
||||
"director": "Sam Mendes",
|
||||
"starring":
|
||||
[
|
||||
"Daniel Craig",
|
||||
"Lea Seydoux"
|
||||
],
|
||||
|
||||
"desc": "Twenty-fourth James Bond movie",
|
||||
"release date": 1445821200000,
|
||||
"box office": 879376275
|
||||
}
|
||||
]
|
|
@ -1,85 +0,0 @@
|
|||
package com.baeldung.jsonpath.introduction;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.Years;
|
||||
|
||||
import com.jayway.jsonpath.Configuration;
|
||||
import com.jayway.jsonpath.DocumentContext;
|
||||
import com.jayway.jsonpath.JsonPath;
|
||||
import com.jayway.jsonpath.Option;
|
||||
|
||||
public class ChangingPasswordTest {
|
||||
|
||||
enum Result {
|
||||
SUCCESS, FAILURE
|
||||
}
|
||||
|
||||
InputStream jsonValueInputStream = this.getClass().getClassLoader().getResourceAsStream("intro_user.json");
|
||||
String jsonDataSourceString = new Scanner(jsonValueInputStream, "UTF-8").useDelimiter("\\Z").next();
|
||||
|
||||
@Test
|
||||
public void givenUseCase_whenChangingPasswordOfNonExistentUser_thenFail() {
|
||||
String failedRequestBody = "{\"username\":\"jayway\", \"new_password\":\"JsonPath\"}";
|
||||
Result result = changingPasswordHelper(failedRequestBody);
|
||||
|
||||
assertEquals(Result.FAILURE, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUseCase_whenChangingToUnusedPassword_thenSucceed() {
|
||||
String successfulRequestBody = "{\"username\":\"oracle\", \"new_password\":\"Java_SE_9\"}";
|
||||
Result result = changingPasswordHelper(successfulRequestBody);
|
||||
|
||||
assertEquals(Result.SUCCESS, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUseCase_whenChangingToRecentlyUsedPassword_thenFail() {
|
||||
String failedRequestBody = "{\"username\":\"oracle\", \"new_password\":\"Java_SE_7\"}";
|
||||
Result result = changingPasswordHelper(failedRequestBody);
|
||||
|
||||
assertEquals(Result.FAILURE, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUseCase_whenChangingToLongTimeAgoPassword_thenSucceed() {
|
||||
String successfulRequestBody = "{\"username\":\"sun\", \"new_password\":\"J2SE_5.0\"}";
|
||||
Result result = changingPasswordHelper(successfulRequestBody);
|
||||
|
||||
assertEquals(Result.SUCCESS, result);
|
||||
}
|
||||
|
||||
private Result changingPasswordHelper(String requestBody) {
|
||||
DocumentContext requestContext = JsonPath.parse(requestBody);
|
||||
String extractedUsername = requestContext.read("$['username']");
|
||||
String extractedPassword = requestContext.read("$['new_password']");
|
||||
|
||||
DocumentContext jsonContext = JsonPath.parse(jsonDataSourceString);
|
||||
List<String> dataSourceUsername = jsonContext.read("$[?(@.username == '" + extractedUsername + "')]");
|
||||
if (dataSourceUsername.size() == 0)
|
||||
return Result.FAILURE;
|
||||
|
||||
Configuration pathConfiguration = Configuration.builder().options(Option.AS_PATH_LIST).build();
|
||||
List<String> pathToCurrentUser = JsonPath.using(pathConfiguration).parse(jsonDataSourceString).read("$[?(@.username == '" + extractedUsername + "')]");
|
||||
List<Long> passwordCreatedTimeList = jsonContext.read(pathToCurrentUser.get(0) + "['password']['old'][?(@.value == '" + extractedPassword + "')]['created']");
|
||||
if (passwordCreatedTimeList.size() == 0)
|
||||
return Result.SUCCESS;
|
||||
|
||||
Long[] passwordCreatedTimeArray = (passwordCreatedTimeList.toArray(new Long[passwordCreatedTimeList.size()]));
|
||||
Arrays.sort(passwordCreatedTimeArray);
|
||||
DateTime oldDate = new DateTime(passwordCreatedTimeArray[passwordCreatedTimeArray.length - 1]);
|
||||
if (Years.yearsBetween(oldDate, new DateTime()).getYears() <= 10)
|
||||
return Result.FAILURE;
|
||||
|
||||
return Result.SUCCESS;
|
||||
}
|
||||
}
|
|
@ -1,50 +0,0 @@
|
|||
package com.baeldung.jsonpath.introduction;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
import com.jayway.jsonpath.DocumentContext;
|
||||
import com.jayway.jsonpath.JsonPath;
|
||||
|
||||
public class LoggingInTest {
|
||||
|
||||
enum Result {
|
||||
SUCCESS, FAILURE
|
||||
}
|
||||
|
||||
InputStream jsonInputStream = this.getClass().getClassLoader().getResourceAsStream("intro_user.json");
|
||||
String jsonDataSourceString = new Scanner(jsonInputStream, "UTF-8").useDelimiter("\\Z").next();
|
||||
|
||||
@Test
|
||||
public void givenUseCase_whenLoggingInWithCorrectUserData_thenSucceed() {
|
||||
String correctRequestBody = "{\"username\":\"sun\", \"password\":\"Java_SE_6\"}";
|
||||
Result result = loggingInHelper(correctRequestBody);
|
||||
|
||||
assertEquals(Result.SUCCESS, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUseCase_whenLoggingInWithIncorrectUserData_thenFail() {
|
||||
String incorrectRequestBody = "{\"username\":\"oracle\", \"password\":\"Java_SE_9\"}";
|
||||
Result result = loggingInHelper(incorrectRequestBody);
|
||||
|
||||
assertEquals(Result.FAILURE, result);
|
||||
}
|
||||
|
||||
private Result loggingInHelper(String requestBody) {
|
||||
DocumentContext requestContext = JsonPath.parse(requestBody);
|
||||
String extractedUsername = requestContext.read("$['username']");
|
||||
String extractedPassword = requestContext.read("$['password']");
|
||||
List<String> list = JsonPath.parse(jsonDataSourceString).read("$[?(@.username == '" + extractedUsername + "' && @.password.current.value == '" + extractedPassword + "')]");
|
||||
|
||||
if (list.size() == 0)
|
||||
return Result.FAILURE;
|
||||
return Result.SUCCESS;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,46 +0,0 @@
|
|||
package com.baeldung.jsonpath.introduction;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
import com.jayway.jsonpath.JsonPath;
|
||||
|
||||
public class RegisteringAccountTest {
|
||||
|
||||
enum Result {
|
||||
SUCCESS, FAILURE
|
||||
}
|
||||
|
||||
InputStream jsonInputStream = this.getClass().getClassLoader().getResourceAsStream("intro_user.json");
|
||||
String jsonDataSourceString = new Scanner(jsonInputStream, "UTF-8").useDelimiter("\\Z").next();
|
||||
|
||||
@Test
|
||||
public void givenUseCase_whenRegisteringUnusedAccount_thenSucceed() {
|
||||
String unusedRequestBody = "{\"username\":\"jayway\", \"password\":\"JsonPath\"}";
|
||||
Result result = registeringNewAccountHelper(unusedRequestBody);
|
||||
|
||||
assertEquals(Result.SUCCESS, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUseCase_whenRegisteringUsedAccount_thenFail() {
|
||||
String usedRequestBody = "{\"username\":\"oracle\", \"password\":\"Java_SE_9\"}";
|
||||
Result result = registeringNewAccountHelper(usedRequestBody);
|
||||
|
||||
assertEquals(Result.FAILURE, result);
|
||||
}
|
||||
|
||||
private Result registeringNewAccountHelper(String requestBody) {
|
||||
List<String> userDataSource = JsonPath.parse(jsonDataSourceString).read("$[*]['username']");
|
||||
String extractedUsername = JsonPath.parse(requestBody).read("$['username']");
|
||||
|
||||
if (userDataSource.toString().contains(extractedUsername))
|
||||
return Result.FAILURE;
|
||||
return Result.SUCCESS;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,91 @@
|
|||
package com.baeldung.jsonpath.introduction;
|
||||
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
|
||||
import com.jayway.jsonpath.Configuration;
|
||||
import com.jayway.jsonpath.DocumentContext;
|
||||
import com.jayway.jsonpath.JsonPath;
|
||||
import com.jayway.jsonpath.Option;
|
||||
|
||||
public class ServiceTest {
|
||||
InputStream jsonInputStream = this.getClass().getClassLoader().getResourceAsStream("intro_service.json");
|
||||
String jsonString = new Scanner(jsonInputStream, "UTF-8").useDelimiter("\\Z").next();
|
||||
|
||||
@Test
|
||||
public void givenId_whenRequestingRecordData_thenSucceed() {
|
||||
Object dataObject = JsonPath.parse(jsonString).read("$[?(@.id == 2)]");
|
||||
String dataString = dataObject.toString();
|
||||
|
||||
assertThat(dataString, containsString("2"));
|
||||
assertThat(dataString, containsString("Quantum of Solace"));
|
||||
assertThat(dataString, containsString("Twenty-second James Bond movie"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStarring_whenRequestingMovieTitle_thenSucceed() {
|
||||
List<Map<String, Object>> dataList = JsonPath.parse(jsonString).read("$[?('Eva Green' in @['starring'])]");
|
||||
String title = (String) dataList.get(0).get("title");
|
||||
|
||||
assertEquals("Casino Royale", title);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCompleteStructure_whenCalculatingTotalRevenue_thenSucceed() {
|
||||
DocumentContext context = JsonPath.parse(jsonString);
|
||||
int length = context.read("$.length()");
|
||||
long revenue = 0;
|
||||
for (int i = 0; i < length; i++) {
|
||||
revenue += context.read("$[" + i + "]['box office']", Long.class);
|
||||
}
|
||||
|
||||
assertEquals(594275385L + 591692078L + 1110526981L + 879376275L, revenue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStructure_whenRequestingHighestRevenueMovieTitle_thenSucceed() {
|
||||
DocumentContext context = JsonPath.parse(jsonString);
|
||||
List<Object> revenueList = context.read("$[*]['box office']");
|
||||
Integer[] revenueArray = revenueList.toArray(new Integer[0]);
|
||||
Arrays.sort(revenueArray);
|
||||
|
||||
int highestRevenue = revenueArray[revenueArray.length - 1];
|
||||
Configuration pathConfiguration = Configuration.builder().options(Option.AS_PATH_LIST).build();
|
||||
List<String> pathList = JsonPath.using(pathConfiguration).parse(jsonString).read("$[?(@['box office'] == " + highestRevenue + ")]");
|
||||
|
||||
Map<String, String> dataRecord = context.read(pathList.get(0));
|
||||
String title = dataRecord.get("title");
|
||||
|
||||
assertEquals("Skyfall", title);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirector_whenRequestingLatestMovieTitle_thenSucceed() {
|
||||
DocumentContext context = JsonPath.parse(jsonString);
|
||||
List<Map<String, Object>> dataList = context.read("$[?(@.director == 'Sam Mendes')]");
|
||||
|
||||
List<Object> dateList = new ArrayList<>();
|
||||
for (Map<String, Object> item : dataList) {
|
||||
Object date = item.get("release date");
|
||||
dateList.add(date);
|
||||
}
|
||||
Long[] dateArray = dateList.toArray(new Long[0]);
|
||||
Arrays.sort(dateArray);
|
||||
|
||||
long latestTime = dateArray[dateArray.length - 1];
|
||||
List<Map<String, Object>> finalDataList = context.read("$[?(@['director'] == 'Sam Mendes' && @['release date'] == " + latestTime + ")]");
|
||||
String title = (String) finalDataList.get(0).get("title");
|
||||
|
||||
assertEquals("Spectre", title);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue