/*
 * Copyright 2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.developerb.nmxmlp;

import org.junit.Test;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class MissingNodeTest extends AbstractNXTest {

    private final String xml = "<root><person firstName='Nasse' lastName='Nøff' age='10' /></root>";

    @Test
    public void optionalButExistingAttribute() {
        NX.Cursor root = parse(xml);
        NX.Cursor person = root.to("person");

        Integer age = person.optionalAttr("age").text(Integer::parseInt);

        assertThat(age)
                .as("Extracted age")
                .isEqualTo(10);
    }

    @Test
    public void attributeOnMissingOptionalNode() {
        NX.Cursor root = parse(xml);
        NX.Cursor pirate = root.toOptional("pirate"); // Does not exist

        Integer numberOfLegs = pirate.attr("legs").text(Integer::parseInt);
        assertNull(numberOfLegs);
    }

    @Test
    public void optionalAttributeOnMissingOptionalNode() {
        NX.Cursor root = parse(xml);
        NX.Cursor pirate = root.toOptional("pirate"); // Does not exist

        Integer numberOfLegs = pirate.optionalAttr("legs").text(Integer::parseInt);
        assertNull(numberOfLegs);
    }

    @Test
    public void toMultipleLevelsMissing() {
        NX.Cursor root = parse(xml);
        NX.Cursor pirateLegs = root.toOptional("pirate", "legs"); // Does not exist

        assertNull(pirateLegs.text());
    }

    @Test
    public void updatingTextOfMissingNodesDoesNothing() {
        NX.Cursor root = parse(xml);
        String before = root.dumpXml();

        NX.Cursor pirate = root.toOptional("pirate"); // Does not exist
        pirate.text("who cares");

        assertEquals(before, root.dumpXml());
    }

    @Test
    public void extractCollectionUnderMissingNode() {
        NX.Cursor root = parse(xml);
        NX.Cursor pirate = root.toOptional("pirate");
        List<Object> birds = pirate.extractCollection("birds", (NX.Cursor cursor) -> null);

        assertThat(birds)
                .as("Collection extracted from a missing node")
                .isEmpty();
    }

    @Test
    public void extractCollectionUnderMissingNodeRegisteredExtractor() {
        NX.Cursor root = parse(xml);
        NX.Cursor pirate = root.toOptional("pirate");
        List<Integer> birds = pirate.extractCollection("birds", Integer.class);

        assertThat(birds)
                .as("Collection extracted from a missing node")
                .isEmpty();
    }

    @Test
    public void extractFromMissingNode() {
        NX.Cursor root = parse(xml);
        NX.Cursor pirate = root.toOptional("pirate");

        Integer age = pirate.to("age").extract(Integer.class);
        assertNull(age);
    }
}

@@ -685,16 +685,66 @@
 il.List;
+%0Aimport java.util.concurrent.atomic.AtomicInteger;
 %0A%0Aimport
@@ -3471,10 +3471,351 @@
 ;%0A    %7D%0A
+%0A    @Test%0A    public void iterateUnderMissingNode() %7B%0A        NX.Cursor root = parse(xml);%0A        NX.Cursor pirate = root.toOptional(%22pirate%22);%0A%0A        AtomicInteger counter = new AtomicInteger(0);%0A        pirate.iterateCollection(%22birds%22, (NX.Cursor cursor) -%3E counter.incrementAndGet());%0A%0A        assertEquals(0, counter.get());%0A    %7D%0A%0A
 %7D%0A

Test - Iterate under missing node