/* * Copyright (c) 2024 John Ahlroos * * Creative Commons Attribution-NoDerivatives 4.0 International Public License * * By exercising the Licensed Rights (defined below), You accept and agree * to be bound by the terms and conditions of this Creative Commons * Attribution-NoDerivatives 4.0 International Public License ("Public * License"). To the extent this Public License may be interpreted as a * contract, You are granted the Licensed Rights in consideration of Your * acceptance of these terms and conditions, and the Licensor grants You * such rights in consideration of benefits the Licensor receives from * making the Licensed Material available under these terms and * conditions. * * Refer to LICENSE file or https://creativecommons.org/licenses/by-nd/4.0 for full license. * */ package com.devsoap.json; import org.junit.jupiter.api.Test; import static com.devsoap.json.JsonAssert.assertThat; public class FieldTraversalAssertionTest { private static final String JSON = """ { "level": 0, "root": { "level": 1, "child": { "level": 2, "child": { "level": 3, "last": 42 } } } } """; @Test public void root() { assertThat(JSON).field("level").isEqualTo(0); assertThat(JSON).field("root").isNotNull(); } @Test public void fieldTraversal() { assertThat(JSON) .field("root") .field("child") .field("level") .isEqualTo(2); } @Test public void pathTraversal() { assertThat(JSON) .path("root", "child", "level") .isEqualTo(2); } @Test public void firstFieldMatching() { assertThat(JSON) .firstField("child") .field("level") .isEqualTo(2); assertThat(JSON) .firstField("last") .isEqualTo(42); } @Test public void lastFieldMatching() { assertThat(JSON) .lastField("child") .field("level") .isEqualTo(3); assertThat(JSON) .lastField("level") .isEqualTo(3); } @Test public void nthFieldMatching() { assertThat(JSON) .nthField("child", 1) .field("level") .isEqualTo(3); for (var level=0; level<=3; level++) { assertThat(JSON) .nthField("level", level) .isEqualTo(level); } } }