Content of file JsonPointerReplaceOperationTest.java
/*
* Copyright (c) 2015, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.eclipse.parsson.tests;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import java.util.Arrays;
import jakarta.json.Json;
import jakarta.json.JsonException;
import jakarta.json.JsonObject;
import jakarta.json.JsonPointer;
import jakarta.json.JsonStructure;
import jakarta.json.JsonValue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
/**
*
* @author Alex Soto
*
*/
@RunWith(Parameterized.class)
public class JsonPointerReplaceOperationTest {
@Parameters(name = "{index}: ({0})={1}")
public static Iterable<Object[]> data() throws Exception {
return Arrays.asList(new Object[][] {
{buildSimpleReplacePatch(), buildAddress(), buildExpectedAddress(), null},
{buildComplexReplacePatch(), buildPerson(), buildExpectedPerson(), null},
{buildArrayReplacePatchInPosition(), buildPerson(), buildExpectedPersonConcreteArrayPosition(), null},
{buildArrayAddPatchInLastPosition(), buildPerson(), null, JsonException.class},
{buildNoneExistingReplacePatch(), buildAddress(), null, JsonException.class}
});
}
private JsonObject pathOperation;
private JsonStructure target;
private JsonValue expectedResult;
private Class<? extends Exception> expectedException;
public JsonPointerReplaceOperationTest(JsonObject pathOperation,
JsonStructure target, JsonValue expectedResult, Class<? extends Exception> expectedException) {
super();
this.pathOperation = pathOperation;
this.target = target;
this.expectedResult = expectedResult;
this.expectedException = expectedException;
}
@Test
public void shouldReplaceElementsToExistingJsonDocument() {
try {
JsonPointer pointer = Json.createPointer(pathOperation.getString("path"));
JsonObject modified = (JsonObject) pointer.replace(target, pathOperation.get("value"));
assertThat(modified, is(expectedResult));
assertThat(expectedException, nullValue());
} catch(Exception e) {
if(expectedException == null) {
fail(e.getMessage());
} else {
assertThat(e, instanceOf(expectedException));
assertThat(T,org.hamcrest.Matcher) in org.junit.Assert has been deprecated