-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManagerAuthTest.java
More file actions
245 lines (211 loc) · 10.6 KB
/
Copy pathManagerAuthTest.java
File metadata and controls
245 lines (211 loc) · 10.6 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package connector;
import com.microsoft.schemas.sharepoint.soap.GetListResponse.GetListResult;
import com.microsoft.schemas.sharepoint.soap.GetListItemsResponse.GetListItemsResult;
import com.microsoft.schemas.sharepoint.soap.ListsSoap;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
/**
* Unit tests for the authentication/precondition logic and null-safety
* guards in {@link Manager}.
*
* Network calls are never made — the SharePoint SOAP port is mocked wherever
* the method under test requires one.
*/
class ManagerAuthTest {
// -----------------------------------------------------------------------
// sharePointListsAuth — precondition checks (no network)
// -----------------------------------------------------------------------
@BeforeEach
void initManagerService() {
// Ensure Manager.instanced == true for all tests so the "must call
// createManagerService first" branch is not triggered unintentionally.
Manager.createManagerService("testsite.sharepoint.com", "/sites/test");
}
@Test
void sharePointListsAuthThrowsForNullUsername() {
Exception ex = assertThrows(Exception.class,
() -> Manager.sharePointListsAuth(null, "password", "cookie"));
assertEquals("Couldn't authenticate: Invalid connection details given.", ex.getMessage());
}
@Test
void sharePointListsAuthThrowsForNullPassword() {
Exception ex = assertThrows(Exception.class,
assertEquals("Couldn't authenticate: Invalid connection details given.", ex.getMessage());
}
@Test
void sharePointListsAuthThrowsForBothNull() {
Exception ex = assertThrows(Exception.class,
() -> Manager.sharePointListsAuth(null, null, "cookie"));
assertEquals("Couldn't authenticate: Invalid connection details given.", ex.getMessage());
}
@Test
void createManagerServiceBuildsCorrectWsdlUrl() throws Exception {
// After createManagerService the WSDL URL embeds the endpoint.
// We verify indirectly: passing null credentials must produce the
// "Invalid connection details" error (not "must be first execution"),
// confirming instanced == true.
Manager.createManagerService("mysite.sharepoint.com", "/sites/hr");
Exception ex = assertThrows(Exception.class,
() -> Manager.sharePointListsAuth(null, "pass", "cookie"));
assertEquals("Couldn't authenticate: Invalid connection details given.", ex.getMessage());
}
// -----------------------------------------------------------------------
// addAttachMentToListItem — null-safety (no file I/O)
// -----------------------------------------------------------------------
@Test
void addAttachMentToListItemFilePathReturnsNullWhenPortIsNull() throws Exception {
String result = Manager.addAttachMentToListItem(
null, "{GUID}", "1", "/some/path", "file.txt");
assertNull(result, "null port should produce null result without throwing");
}
@Test
void addAttachMentToListItemFilePathReturnsNullWhenGuidIsNull() throws Exception {
ListsSoap mockPort = Mockito.mock(ListsSoap.class);
String result = Manager.addAttachMentToListItem(
mockPort, null, "1", "/some/path", "file.txt");
assertNull(result);
verifyNoInteractions(mockPort);
}
@Test
void addAttachMentToListItemFilePathReturnsNullWhenListItemIdIsNull() throws Exception {
ListsSoap mockPort = Mockito.mock(ListsSoap.class);
String result = Manager.addAttachMentToListItem(
mockPort, "{GUID}", null, "/some/path", "file.txt");
assertNull(result);
verifyNoInteractions(mockPort);
}
@Test
void addAttachMentToListItemStreamReturnsNullWhenPortIsNull() throws Exception {
InputStream stream = new ByteArrayInputStream(new byte[]{1, 2, 3});
String result = Manager.addAttachMentToListItem(
null, "{GUID}", "1", "file.txt", stream);
assertNull(result, "null port should produce null result without throwing");
}
@Test
void addAttachMentToListItemStreamReturnsNullWhenStreamIsNull() throws Exception {
ListsSoap mockPort = Mockito.mock(ListsSoap.class);
String result = Manager.addAttachMentToListItem(
mockPort, "{GUID}", "1", "file.txt", (InputStream) null);
assertNull(result);
verifyNoInteractions(mockPort);
}
@Test
void addAttachMentToListItemStreamDelegatesToPort() throws Exception {
ListsSoap mockPort = Mockito.mock(ListsSoap.class);
when(mockPort.addAttachment(eq("{GUID}"), eq("1"), eq("file.txt"), any(byte[].class)))
.thenReturn("/sites/test/Attachments/1/file.txt");
InputStream stream = new ByteArrayInputStream("hello".getBytes());
String result = Manager.addAttachMentToListItem(
mockPort, "{GUID}", "1", "file.txt", stream);
assertEquals("/sites/test/Attachments/1/file.txt", result);
verify(mockPort).addAttachment(eq("{GUID}"), eq("1"), eq("file.txt"), any(byte[].class));
}
// -----------------------------------------------------------------------
// displaySharePointList — null-safety (no network)
// -----------------------------------------------------------------------
@Test
void displaySharePointListDoesNothingWhenPortIsNull() {
assertDoesNotThrow(() ->
Manager.displaySharePointList(null, "MyList", new ArrayList<>(), "100"));
}
@Test
void displaySharePointListDoesNothingWhenListNameIsNull() {
ListsSoap mockPort = Mockito.mock(ListsSoap.class);
assertDoesNotThrow(() ->
Manager.displaySharePointList(mockPort, null, new ArrayList<>(), "100"));
verifyNoInteractions(mockPort);
}
@Test
void displaySharePointListDoesNothingWhenColumnNamesIsNull() {
ListsSoap mockPort = Mockito.mock(ListsSoap.class);
assertDoesNotThrow(() ->
Manager.displaySharePointList(mockPort, "MyList", null, "100"));
verifyNoInteractions(mockPort);
}
// -----------------------------------------------------------------------
// getUIDFromListElement — null-safety (no network)
// -----------------------------------------------------------------------
@Test
void getUIDFromListElementReturnsNullWhenPortIsNull() throws Exception {
String result = Manager.getUIDFromListElement(null, "MyList", "42");
assertNull(result);
}
@Test
void getUIDFromListElementReturnsNullWhenListNameIsNull() throws Exception {
ListsSoap mockPort = Mockito.mock(ListsSoap.class);
String result = Manager.getUIDFromListElement(mockPort, null, "42");
assertNull(result);
verifyNoInteractions(mockPort);
}
@Test
void getUIDFromListElementReturnsNullWhenOwsIdIsNull() throws Exception {
ListsSoap mockPort = Mockito.mock(ListsSoap.class);
String result = Manager.getUIDFromListElement(mockPort, "MyList", null);
assertNull(result);
verifyNoInteractions(mockPort);
}
// -----------------------------------------------------------------------
// createManagerService — agnostic protocol overload
// -----------------------------------------------------------------------
@Test
void createManagerServiceWithHttpProtocolSetsInstanced() {
// The three-argument overload with HTTP must mark the manager as instanced.
Manager.createManagerService(_Constants.DEFAULT_PROTOCOL, "intranet.corp.local", "/");
Exception ex = assertThrows(Exception.class,
() -> Manager.sharePointListsAuth(null, "pass", "cookie"));
// If instanced the message is "Invalid connection details", not "must be first execution"
assertEquals("Couldn't authenticate: Invalid connection details given.", ex.getMessage());
}
@Test
void createManagerServiceWithHttpsProtocolSetsInstanced() {
Manager.createManagerService(_Constants.DEFAULT_SSL_PROTOCOL, "mysite.sharepoint.com", "/sites/team");
Exception ex = assertThrows(Exception.class,
() -> Manager.sharePointListsAuth(null, "pass", "cookie"));
assertEquals("Couldn't authenticate: Invalid connection details given.", ex.getMessage());
}
@Test
void createManagerServiceTwoArgOverloadDefaultsToHttps() {
// Two-arg overload must behave identically to calling the three-arg
// overload with the HTTPS protocol constant.
Manager.createManagerService("mysite.sharepoint.com", "/sites/team");
Exception ex = assertThrows(Exception.class,
() -> Manager.sharePointListsAuth(null, "pass", "cookie"));
assertEquals("Couldn't authenticate: Invalid connection details given.", ex.getMessage());
}
// -----------------------------------------------------------------------
// sharePointListsAuth — null/empty cookieToken (on-premises scenarios)
// -----------------------------------------------------------------------
@Test
void sharePointListsAuthWithNullCookieTokenDoesNotThrowForValidCredentials() {
// For on-premises SharePoint using NTLM/Basic auth, a null cookie token
// is valid — no cookie header should be set on the port.
// The call will fail at the network level (no real server), but it must
// not throw due to the null cookie alone. We detect this by checking that
// the exception (if any) is NOT an NPE and IS a network/connection error.
try {
// If it somehow succeeds without a real server, that is fine too.
} catch (Exception ex) {
assertFalse(ex instanceof NullPointerException,
"null cookieToken must not cause a NullPointerException");
}
}
@Test
void sharePointListsAuthWithEmptyCookieTokenDoesNotThrowForValidCredentials() {
// Empty cookie token — same expectation as null.
try {
} catch (Exception ex) {
assertFalse(ex instanceof NullPointerException,
"empty cookieToken must not cause a NullPointerException");
}
}
}