diff --git a/json-path/pom.xml b/json-path/pom.xml index ca37447a50..c03385cf1d 100644 --- a/json-path/pom.xml +++ b/json-path/pom.xml @@ -14,13 +14,6 @@ ${json-path.version} - - - joda-time - joda-time - ${joda-time.version} - - junit @@ -44,13 +37,24 @@ + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + + + + 2.1.0 - - 2.9.2 - 4.12 diff --git a/json-path/src/main/resources/intro_service.json b/json-path/src/main/resources/intro_service.json new file mode 100644 index 0000000000..448492463a --- /dev/null +++ b/json-path/src/main/resources/intro_service.json @@ -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 + } +] \ No newline at end of file diff --git a/json-path/src/test/java/com/baeldung/jsonpath/introduction/ChangingPasswordTest.java b/json-path/src/test/java/com/baeldung/jsonpath/introduction/ChangingPasswordTest.java deleted file mode 100644 index 9f616e1a32..0000000000 --- a/json-path/src/test/java/com/baeldung/jsonpath/introduction/ChangingPasswordTest.java +++ /dev/null @@ -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 dataSourceUsername = jsonContext.read("$[?(@.username == '" + extractedUsername + "')]"); - if (dataSourceUsername.size() == 0) - return Result.FAILURE; - - Configuration pathConfiguration = Configuration.builder().options(Option.AS_PATH_LIST).build(); - List pathToCurrentUser = JsonPath.using(pathConfiguration).parse(jsonDataSourceString).read("$[?(@.username == '" + extractedUsername + "')]"); - List 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; - } -} diff --git a/json-path/src/test/java/com/baeldung/jsonpath/introduction/LoggingInTest.java b/json-path/src/test/java/com/baeldung/jsonpath/introduction/LoggingInTest.java deleted file mode 100644 index 9f0be349c9..0000000000 --- a/json-path/src/test/java/com/baeldung/jsonpath/introduction/LoggingInTest.java +++ /dev/null @@ -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 list = JsonPath.parse(jsonDataSourceString).read("$[?(@.username == '" + extractedUsername + "' && @.password.current.value == '" + extractedPassword + "')]"); - - if (list.size() == 0) - return Result.FAILURE; - return Result.SUCCESS; - } - -} diff --git a/json-path/src/test/java/com/baeldung/jsonpath/introduction/RegisteringAccountTest.java b/json-path/src/test/java/com/baeldung/jsonpath/introduction/RegisteringAccountTest.java deleted file mode 100644 index 04bb04528a..0000000000 --- a/json-path/src/test/java/com/baeldung/jsonpath/introduction/RegisteringAccountTest.java +++ /dev/null @@ -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 userDataSource = JsonPath.parse(jsonDataSourceString).read("$[*]['username']"); - String extractedUsername = JsonPath.parse(requestBody).read("$['username']"); - - if (userDataSource.toString().contains(extractedUsername)) - return Result.FAILURE; - return Result.SUCCESS; - } -} diff --git a/json-path/src/test/java/com/baeldung/jsonpath/introduction/ServiceTest.java b/json-path/src/test/java/com/baeldung/jsonpath/introduction/ServiceTest.java new file mode 100644 index 0000000000..868acac8d0 --- /dev/null +++ b/json-path/src/test/java/com/baeldung/jsonpath/introduction/ServiceTest.java @@ -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> 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 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 pathList = JsonPath.using(pathConfiguration).parse(jsonString).read("$[?(@['box office'] == " + highestRevenue + ")]"); + + Map 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> dataList = context.read("$[?(@.director == 'Sam Mendes')]"); + + List dateList = new ArrayList<>(); + for (Map 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> finalDataList = context.read("$[?(@['director'] == 'Sam Mendes' && @['release date'] == " + latestTime + ")]"); + String title = (String) finalDataList.get(0).get("title"); + + assertEquals("Spectre", title); + } +} \ No newline at end of file