|  | 
|  | 1 | +package com.baeldung.updatingvaluesinjsonarray; | 
|  | 2 | + | 
|  | 3 | +import com.fasterxml.jackson.databind.ObjectMapper; | 
|  | 4 | +import com.fasterxml.jackson.databind.node.ArrayNode; | 
|  | 5 | +import com.fasterxml.jackson.databind.node.TextNode; | 
|  | 6 | +import com.google.gson.JsonArray; | 
|  | 7 | +import com.google.gson.JsonParser; | 
|  | 8 | +import com.google.gson.JsonPrimitive; | 
|  | 9 | +import org.json.JSONArray; | 
|  | 10 | +import org.junit.jupiter.api.Test; | 
|  | 11 | + | 
|  | 12 | +import static org.junit.jupiter.api.Assertions.assertEquals; | 
|  | 13 | + | 
|  | 14 | +public class UpdatingJsonArrayValuesUnitTest { | 
|  | 15 | + | 
|  | 16 | +    @Test | 
|  | 17 | +    public void givenJSONArray_whenUsingOrgJson_thenArrayCreatedAndVerified() { | 
|  | 18 | +        JSONArray jsonArray = new JSONArray().put("Apple").put("Banana").put("Cherry"); | 
|  | 19 | + | 
|  | 20 | +        assertEquals("[\"Apple\",\"Banana\",\"Cherry\"]", jsonArray.toString()); | 
|  | 21 | +    } | 
|  | 22 | + | 
|  | 23 | +    @Test | 
|  | 24 | +    public void givenJSONArray_whenUsingOrgJson_thenArrayReadAndUpdated() { | 
|  | 25 | +        JSONArray jsonArray = new JSONArray("[\"Apple\",\"Banana\",\"Cherry\"]"); | 
|  | 26 | + | 
|  | 27 | +        jsonArray.put(1, "Blueberry"); | 
|  | 28 | +        assertEquals("[\"Apple\",\"Blueberry\",\"Cherry\"]", jsonArray.toString()); | 
|  | 29 | +    } | 
|  | 30 | + | 
|  | 31 | +    @Test | 
|  | 32 | +    public void givenJsonArray_whenUsingGson_thenArrayCreatedAndVerified() { | 
|  | 33 | +        JsonArray jsonArray = new JsonArray(); | 
|  | 34 | +        jsonArray.add(new JsonPrimitive("Apple")); | 
|  | 35 | +        jsonArray.add(new JsonPrimitive("Banana")); | 
|  | 36 | +        jsonArray.add(new JsonPrimitive("Cherry")); | 
|  | 37 | + | 
|  | 38 | +        assertEquals("[\"Apple\",\"Banana\",\"Cherry\"]", jsonArray.toString()); | 
|  | 39 | +    } | 
|  | 40 | + | 
|  | 41 | +    @Test | 
|  | 42 | +    public void givenJsonArray_whenUsingGson_thenArrayReadAndUpdated() { | 
|  | 43 | +        JsonArray jsonArray = JsonParser.parseString("[\"Apple\",\"Banana\",\"Cherry\"]").getAsJsonArray(); | 
|  | 44 | + | 
|  | 45 | +        jsonArray.set(1, new JsonPrimitive("Blueberry")); | 
|  | 46 | +        assertEquals("[\"Apple\",\"Blueberry\",\"Cherry\"]", jsonArray.toString()); | 
|  | 47 | +    } | 
|  | 48 | + | 
|  | 49 | +    @Test | 
|  | 50 | +    public void givenArrayNode_whenUsingJackson_thenArrayCreatedAndVerified() throws Exception { | 
|  | 51 | +        ObjectMapper mapper = new ObjectMapper(); | 
|  | 52 | +        ArrayNode arrayNode = mapper.createArrayNode().add("Apple").add("Banana").add("Cherry"); | 
|  | 53 | + | 
|  | 54 | +        assertEquals("[\"Apple\",\"Banana\",\"Cherry\"]", arrayNode.toString()); | 
|  | 55 | +    } | 
|  | 56 | + | 
|  | 57 | +     @Test | 
|  | 58 | +    public void givenArrayNode_whenUsingJackson_thenArrayReadAndUpdated() throws Exception { | 
|  | 59 | +        ObjectMapper mapper = new ObjectMapper(); | 
|  | 60 | +        ArrayNode arrayNode = (ArrayNode) mapper.readTree("[\"Apple\",\"Banana\",\"Cherry\"]"); | 
|  | 61 | + | 
|  | 62 | +        arrayNode.set(1, "Blueberry"); | 
|  | 63 | +        assertEquals("[\"Apple\",\"Blueberry\",\"Cherry\"]", arrayNode.toString()); | 
|  | 64 | +    } | 
|  | 65 | +} | 
0 commit comments