package com.github.simy4.xpath.dom.navigator;

import com.github.simy4.xpath.navigator.Node;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.w3c.dom.Document;

import javax.xml.XMLConstants;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class DomNodeTest {

    @Mock private org.w3c.dom.Node node;

    private Node nodeView;

    @BeforeEach
    void setUp() {
        when(node.getTextContent()).thenReturn("text");

        nodeView = new DomNode(node);
    }

    @Test
    void shouldReturnNodeNameForNamespaceUnawareNode() {
        when(node.getNodeName()).thenReturn("node");
        QName result = nodeView.getName();
        assertThat(result).extracting("namespaceURI", "localPart", "prefix")
                .containsExactly(XMLConstants.NULL_NS_URI, "node", XMLConstants.DEFAULT_NS_PREFIX);
    }

    @Test
    void shouldReturnNodeNameForNamespaceAwareNode() {
        when(node.getNamespaceURI()).thenReturn("http://www.example.com/my");
        when(node.getLocalName()).thenReturn("node");
        when(node.getPrefix()).thenReturn("my");
        QName result = nodeView.getName();
        assertThat(result).extracting("namespaceURI", "localPart", "prefix")
                .containsExactly("http://www.example.com/my", "node", "my");
    }

    @Test
    void shouldReturnNodeTextContent() {
        assertThat(nodeView.getText()).isEqualTo("text");
    }

    @Test
    void shouldSerializeAndDeserialize() throws IOException, ClassNotFoundException, ParserConfigurationException {
        // given
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        Document document = documentBuilderFactory.newDocumentBuilder().newDocument();
        nodeView = new DomNode(document);

        // when
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        new ObjectOutputStream(out).writeObject(nodeView);
        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
        Node deserialized = (Node) new ObjectInputStream(in).readObject();

        // then
        assertThat(deserialized).hasToString(document.toString());
    }

}
@@ -40,16 +40,75 @@
 gator;%0A%0A
+import com.github.simy4.xpath.helpers.SerializationHelper;%0A
 import c
@@ -352,467 +352,100 @@
 on;%0A
-import org.mockito.junit.jupiter.MockitoSettings;%0Aimport org.mockito.quality.Strictness;%0Aimport org.w3c.dom.Document;%0A%0Aimport javax.xml.XMLConstants;%0Aimport javax.xml.namespace.QName;%0Aimport javax.xml.parsers.DocumentBuilderFactory;%0Aimport javax.xml.parsers.ParserConfigurationException;%0A%0Aimport java.io.ByteArrayInputStream;%0Aimport java.io.ByteArrayOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.ObjectInputStream;%0Aimport java.io.ObjectOutputStream
+%0Aimport javax.xml.XMLConstants;%0Aimport javax.xml.namespace.QName;%0Aimport java.io.IOException
 ;%0A%0Ai
@@ -582,58 +582,8 @@
 ss)%0A
-@MockitoSettings(strictness = Strictness.LENIENT)%0A
 clas
@@ -608,16 +608,37 @@
    @Mock
+(serializable = true)
  private
@@ -729,65 +729,8 @@
 ) %7B%0A
-        when(node.getTextContent()).thenReturn(%22text%22);%0A%0A
     
@@ -1614,24 +1614,81 @@
 Content() %7B%0A
+        when(node.getTextContent()).thenReturn(%22text%22);%0A%0A
         asse
@@ -1837,573 +1837,186 @@
 tion
-, ParserConfigurationException %7B%0A        // given%0A        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();%0A        Document document = documentBuilderFactory.newDocumentBuilder().newDocument();%0A        nodeView = new DomNode(document);%0A%0A        // when%0A        ByteArrayOutputStream out = new ByteArrayOutputStream();%0A        new ObjectOutputStream(out).writeObject(nodeView);%0A        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());%0A        Node deserialized = (Node) new ObjectInputStream(in).readObject(
+ %7B%0A        // given%0A        when(node.getNodeName()).thenReturn(%22node%22);%0A%0A        // when%0A        Node deserializedNode = SerializationHelper.serializeAndDeserializeBack(nodeView
 );%0A%0A
@@ -2066,39 +2066,50 @@
 ized
-).hasToString(document.toString
+Node.getName()).isEqualTo(nodeView.getName
 ());

Simplify DOM node test.