java-tutorials/json/src/test/java/com/baeldung/jsonpointer/JsonPointerCrudUnitTest.java
cmlavila 5eea4fc6a6 BAEL-2037 - Code for mini-article (#4923)
* BAEL-2037 - Code for mini-article

* BAEL-2037 - Fixes after editors review

* Fixing dependencies

* Change File Reader

* Formatting

* Fix imports

* Rename test class
2018-08-14 00:02:06 +01:00

59 lines
1.5 KiB
Java

package com.baeldung.jsonpointer;
import static org.junit.Assert.*;
import org.junit.Test;
public class JsonPointerCrudUnitTest {
@Test
public void testJsonPointerCrudForAddress() {
JsonPointerCrud jsonPointerCrud = new JsonPointerCrud(JsonPointerCrudUnitTest.class.getResourceAsStream("/address.json"));
assertFalse(jsonPointerCrud.check("city"));
// insert a value
jsonPointerCrud.insert("city", "Rio de Janeiro");
assertTrue(jsonPointerCrud.check("city"));
// fetch full json
String fullJSON = jsonPointerCrud.fetchFullJSON();
assertTrue(fullJSON.contains("name"));
assertTrue(fullJSON.contains("city"));
// fetch value
String cityName = jsonPointerCrud.fetchValueFromKey("city");
assertEquals(cityName, "Rio de Janeiro");
// update value
jsonPointerCrud.update("city", "Sao Paulo");
// fetch value
cityName = jsonPointerCrud.fetchValueFromKey("city");
assertEquals(cityName, "Sao Paulo");
// delete
jsonPointerCrud.delete("city");
assertFalse(jsonPointerCrud.check("city"));
}
@Test
public void testJsonPointerCrudForBooks() {
JsonPointerCrud jsonPointerCrud = new JsonPointerCrud(JsonPointerCrudUnitTest.class.getResourceAsStream("/books.json"));
// fetch value
String book = jsonPointerCrud.fetchListValues("books/1");
assertEquals(book, "{\"title\":\"Title 2\",\"author\":\"John Doe\"}");
}
}