summaryrefslogtreecommitdiff
path: root/src/test/java/org/zwobble/mammoth/tests/docx/CommentXmlTests.java
blob: 04648668e9340447d1ccf24e0a11a08e92ec98d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package org.zwobble.mammoth.tests.docx;

import org.junit.Test;
import org.zwobble.mammoth.internal.documents.Comment;
import org.zwobble.mammoth.internal.docx.CommentXmlReader;
import org.zwobble.mammoth.internal.results.InternalResult;
import org.zwobble.mammoth.internal.xml.XmlNode;

import java.util.List;
import java.util.Optional;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.core.Is.isA;
import static org.zwobble.mammoth.internal.util.Lists.list;
import static org.zwobble.mammoth.internal.util.Maps.map;
import static org.zwobble.mammoth.internal.xml.XmlNodes.element;
import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals;
import static org.zwobble.mammoth.tests.ResultMatchers.isInternalSuccess;
import static org.zwobble.mammoth.tests.documents.DocumentElementMakers.paragraph;
import static org.zwobble.mammoth.tests.docx.BodyXmlReaderMakers.bodyReader;

public class CommentXmlTests {
    @Test
    public void idAndBodyOfCommentIsRead() {
        List<XmlNode> body = list(element("w:p"));
        CommentXmlReader reader = new CommentXmlReader(bodyReader());
        InternalResult<List<Comment>> result = reader.readElement(element("w:comments", list(
            element("w:comment", map("w:id", "1"), body)
        )));
        assertThat(
            result,
            isInternalSuccess(contains(
                allOf(
                    isA(Comment.class),
                    hasProperty("commentId", equalTo("1")),
                    hasProperty("body", deepEquals(list(paragraph())))
                )
            ))
        );
    }

    @Test
    public void whenOptionalAttributesOfCommentAreMissingThenTheyAreReadAsNone() {
        CommentXmlReader reader = new CommentXmlReader(bodyReader());
        InternalResult<List<Comment>> result = reader.readElement(element("w:comments", list(
            element("w:comment", map("w:id", "1"))
        )));
        assertThat(
            result,
            isInternalSuccess(contains(
                allOf(
                    isA(Comment.class),
                    hasProperty("authorName", equalTo(Optional.empty())),
                    hasProperty("authorInitials", equalTo(Optional.empty()))
                )
            ))
        );
    }

    @Test
    public void whenOptionalAttributesOfCommentAreBlankThenTheyAreReadAsNone() {
        CommentXmlReader reader = new CommentXmlReader(bodyReader());
        InternalResult<List<Comment>> result = reader.readElement(element("w:comments", list(
            element("w:comment", map("w:id", "1", "w:author", " ", "w:initials", " "))
        )));
        assertThat(
            result,
            isInternalSuccess(contains(
                allOf(
                    isA(Comment.class),
                    hasProperty("authorName", equalTo(Optional.empty())),
                    hasProperty("authorInitials", equalTo(Optional.empty()))
                )
            ))
        );
    }

    @Test
    public void whenOptionalAttributesOfCommentAreNotBlankThenTheyAreRead() {
        CommentXmlReader reader = new CommentXmlReader(bodyReader());
        InternalResult<List<Comment>> result = reader.readElement(element("w:comments", list(
            element("w:comment", map("w:id", "1", "w:author", "The Piemaker", "w:initials", "TP"))
        )));
        assertThat(
            result,
            isInternalSuccess(contains(
                allOf(
                    isA(Comment.class),
                    hasProperty("authorName", equalTo(Optional.of("The Piemaker"))),
                    hasProperty("authorInitials", equalTo(Optional.of("TP")))
                )
            ))
        );
    }
}