-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListsRequestTest.java
More file actions
157 lines (126 loc) · 5.47 KB
/
Copy pathListsRequestTest.java
File metadata and controls
157 lines (126 loc) · 5.47 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package connector;
import org.junit.jupiter.api.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import java.util.HashMap;
import static org.junit.jupiter.api.Assertions.*;
/**
* Unit tests for {@link ListsRequest}.
*
* Covers CAML batch XML structure construction, the three supported request
* types, field element generation, and all invalid-input paths.
*/
class ListsRequestTest {
// -----------------------------------------------------------------------
// Constructor — valid request types
// -----------------------------------------------------------------------
@Test
void newRequestCreatesNonNullDocumentAndMethod() throws Exception {
ListsRequest req = new ListsRequest("New");
assertNotNull(req.getRootDocument());
assertNotNull(req.getRootDocContent());
}
@Test
void newRequestCreatesBatchRootElement() throws Exception {
ListsRequest req = new ListsRequest("New");
Document doc = req.getRootDocument();
NodeList batches = doc.getElementsByTagName("Batch");
assertEquals(1, batches.getLength(), "Exactly one <Batch> element expected");
Element batch = (Element) batches.item(0);
assertEquals("1", batch.getAttribute("ListVersion"));
assertEquals("Continue", batch.getAttribute("OnError"));
}
@Test
void newRequestSetsMethodCmdNew() throws Exception {
ListsRequest req = new ListsRequest("New");
Element method = req.getRootDocContent();
assertEquals("New", method.getAttribute("Cmd"));
assertEquals("1", method.getAttribute("ID"));
}
@Test
void updateRequestSetsMethodCmdUpdate() throws Exception {
ListsRequest req = new ListsRequest("Update");
assertEquals("Update", req.getRootDocContent().getAttribute("Cmd"));
}
@Test
void deleteRequestSetsMethodCmdDelete() throws Exception {
ListsRequest req = new ListsRequest("Delete");
assertEquals("Delete", req.getRootDocContent().getAttribute("Cmd"));
}
// -----------------------------------------------------------------------
// Constructor — invalid input
// -----------------------------------------------------------------------
@Test
void unsupportedTypeThrowsWithDescriptiveMessage() {
Exception ex = assertThrows(Exception.class, () -> new ListsRequest("Read"));
assertEquals("Unsupported request type", ex.getMessage());
}
@Test
void emptyStringTypeThrowsUnsupportedRequestType() {
Exception ex = assertThrows(Exception.class, () -> new ListsRequest(""));
assertEquals("Unsupported request type", ex.getMessage());
}
@Test
void nullTypeThrowsWithDescriptiveMessage() {
Exception ex = assertThrows(Exception.class, () -> new ListsRequest(null));
assertEquals("Null parameters", ex.getMessage());
}
// -----------------------------------------------------------------------
// createListItem — happy path
// -----------------------------------------------------------------------
@Test
void createListItemReturnsTrueForValidFields() throws Exception {
ListsRequest req = new ListsRequest("New");
HashMap<String, String> fields = new HashMap<>();
fields.put("Title", "Hello World");
assertTrue(req.createListItem(fields));
}
@Test
void createListItemAddsOneFieldElementPerEntry() throws Exception {
ListsRequest req = new ListsRequest("New");
HashMap<String, String> fields = new HashMap<>();
fields.put("Title", "Test Title");
fields.put("Description", "Test Desc");
req.createListItem(fields);
NodeList fieldNodes = req.getRootDocument().getElementsByTagName("Field");
assertEquals(2, fieldNodes.getLength());
}
@Test
void createListItemSetsNameAttributeAndTextContent() throws Exception {
ListsRequest req = new ListsRequest("New");
HashMap<String, String> fields = new HashMap<>();
fields.put("Title", "My Value");
req.createListItem(fields);
NodeList fieldNodes = req.getRootDocument().getElementsByTagName("Field");
assertEquals(1, fieldNodes.getLength());
Element field = (Element) fieldNodes.item(0);
assertEquals("Title", field.getAttribute("Name"));
assertEquals("My Value", field.getTextContent());
}
@Test
void createListItemIsIdempotentAndAccumulatesFields() throws Exception {
ListsRequest req = new ListsRequest("New");
HashMap<String, String> first = new HashMap<>();
first.put("Title", "First");
HashMap<String, String> second = new HashMap<>();
second.put("Body", "Second");
req.createListItem(first);
req.createListItem(second);
NodeList fieldNodes = req.getRootDocument().getElementsByTagName("Field");
assertEquals(2, fieldNodes.getLength());
}
// -----------------------------------------------------------------------
// createListItem — invalid input
// -----------------------------------------------------------------------
@Test
void createListItemReturnsFalseForNullFields() throws Exception {
ListsRequest req = new ListsRequest("New");
assertFalse(req.createListItem(null));
}
@Test
void createListItemReturnsFalseForEmptyFields() throws Exception {
ListsRequest req = new ListsRequest("New");
assertFalse(req.createListItem(new HashMap<>()));
}
}