forked from zowe/zowe-client-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDsnGetTest.java
More file actions
290 lines (260 loc) · 13.7 KB
/
DsnGetTest.java
File metadata and controls
290 lines (260 loc) · 13.7 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*/
package zowe.client.sdk.zosfiles.dsn.methods;
import kong.unirest.core.Cookie;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedConstruction;
import org.mockito.Mockito;
import zowe.client.sdk.core.ZosConnection;
import zowe.client.sdk.core.ZosConnectionFactory;
import zowe.client.sdk.rest.GetStreamZosmfRequest;
import zowe.client.sdk.rest.Response;
import zowe.client.sdk.rest.ZosmfRequest;
import zowe.client.sdk.rest.exception.ZosmfRequestException;
import zowe.client.sdk.zosfiles.dsn.input.DsnDownloadInputData;
import zowe.client.sdk.zosfiles.dsn.model.Dataset;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.withSettings;
/**
* Class containing unit tests for DsnGet.
*
* @author Frank Giordano
* @version 6.0
*/
public class DsnGetTest {
private final ZosConnection connection = ZosConnectionFactory
.createBasicConnection("1", 443, "1", "1");
private final ZosConnection tokenConnection = ZosConnectionFactory
.createTokenConnection("1", 443, new Cookie("hello=hello"));
private GetStreamZosmfRequest mockGetRequest;
private GetStreamZosmfRequest mockGetRequestToken;
@BeforeEach
public void init() throws ZosmfRequestException {
mockGetRequest = Mockito.mock(GetStreamZosmfRequest.class);
Mockito.when(mockGetRequest.executeRequest()).thenReturn(
new Response("test data".getBytes(), 200, "success"));
doCallRealMethod().when(mockGetRequest).setUrl(any());
doCallRealMethod().when(mockGetRequest).getUrl();
mockGetRequestToken = Mockito.mock(GetStreamZosmfRequest.class,
withSettings().useConstructor(tokenConnection));
Mockito.when(mockGetRequestToken.executeRequest()).thenReturn(
new Response("test data".getBytes(), 200, "success"));
doCallRealMethod().when(mockGetRequestToken).setHeaders(anyMap());
doCallRealMethod().when(mockGetRequestToken).setStandardHeaders();
doCallRealMethod().when(mockGetRequestToken).setUrl(any());
doCallRealMethod().when(mockGetRequestToken).getHeaders();
doCallRealMethod().when(mockGetRequestToken).getUrl();
}
@Test
public void tstDsnGetSuccess() throws ZosmfRequestException, IOException {
final DsnGet dsnGet = new DsnGet(connection, mockGetRequest);
final DsnDownloadInputData downloadInputData = new DsnDownloadInputData.Builder().binary(true).build();
final InputStream inputStream = dsnGet.get("TEST.DATASET", downloadInputData);
assertEquals("test data", new String(inputStream.readAllBytes()));
assertEquals("https://1:443/zosmf/restfiles/ds/TEST.DATASET", mockGetRequest.getUrl());
}
@Test
public void tstDsnGetTokenSuccess() throws ZosmfRequestException, IOException {
final DsnGet dsnGet = new DsnGet(connection, mockGetRequestToken);
final DsnDownloadInputData downloadInputData = new DsnDownloadInputData.Builder().binary(true).build();
final InputStream inputStream = dsnGet.get("TEST.DATASET", downloadInputData);
assertEquals("{X-IBM-Data-Type=binary, Accept-Encoding=gzip, X-CSRF-ZOSMF-HEADER=true, " +
"Content-Type=application/json}", mockGetRequestToken.getHeaders().toString());
assertEquals("test data", new String(inputStream.readAllBytes()));
assertEquals("https://1:443/zosmf/restfiles/ds/TEST.DATASET", mockGetRequestToken.getUrl());
}
@Test
public void tstDsnGetTokenWithMultipleParamsSuccess() throws ZosmfRequestException, IOException {
final DsnGet dsnGet = new DsnGet(connection, mockGetRequestToken);
final DsnDownloadInputData downloadInputData = new DsnDownloadInputData.Builder()
.binary(true)
.encoding(1047L)
.returnEtag(true)
.responseTimeout("30")
.build();
final InputStream inputStream = dsnGet.get("TEST.DATASET", downloadInputData);
assertEquals("{X-IBM-Data-Type=binary, Accept-Encoding=1047, " +
"X-IBM-Return-Etag=true, X-IBM-Response-Timeout=30, " +
"X-CSRF-ZOSMF-HEADER=true, Content-Type=application/json}",
mockGetRequestToken.getHeaders().toString());
assertEquals("test data", new String(inputStream.readAllBytes()));
assertEquals("https://1:443/zosmf/restfiles/ds/TEST.DATASET", mockGetRequestToken.getUrl());
}
@Test
public void tstDsnGetTokenWithOnlyEncodingParamsSuccess() throws ZosmfRequestException, IOException {
final DsnGet dsnGet = new DsnGet(connection, mockGetRequestToken);
final DsnDownloadInputData downloadInputData = new DsnDownloadInputData.Builder()
.encoding(1047L)
.build();
final InputStream inputStream = dsnGet.get("TEST.DATASET", downloadInputData);
assertEquals("{X-IBM-Data-Type=text;fileEncoding=IBM-1047, " +
"Accept-Encoding=1047, X-CSRF-ZOSMF-HEADER=true, " +
"Content-Type=application/json}",
mockGetRequestToken.getHeaders().toString());
assertEquals("test data", new String(inputStream.readAllBytes()));
assertEquals("https://1:443/zosmf/restfiles/ds/TEST.DATASET", mockGetRequestToken.getUrl());
}
@Test
public void tstDsnGetTokenWithMultipleParamsWithoutEncodingSuccess() throws ZosmfRequestException, IOException {
final DsnGet dsnGet = new DsnGet(connection, mockGetRequestToken);
final DsnDownloadInputData downloadInputData = new DsnDownloadInputData.Builder()
.binary(true)
.returnEtag(true)
.responseTimeout("30")
.build();
final InputStream inputStream = dsnGet.get("TEST.DATASET", downloadInputData);
assertEquals("{X-IBM-Data-Type=binary, Accept-Encoding=gzip, " +
"X-IBM-Return-Etag=true, X-IBM-Response-Timeout=30, " +
"X-CSRF-ZOSMF-HEADER=true, Content-Type=application/json}",
mockGetRequestToken.getHeaders().toString());
assertEquals("test data", new String(inputStream.readAllBytes()));
assertEquals("https://1:443/zosmf/restfiles/ds/TEST.DATASET", mockGetRequestToken.getUrl());
}
@Test
public void tstDsnGetTokenWithMultipleParamsWithoutEncodingAndEtagSuccess() throws ZosmfRequestException, IOException {
final DsnGet dsnGet = new DsnGet(connection, mockGetRequestToken);
final DsnDownloadInputData downloadInputData = new DsnDownloadInputData.Builder()
.binary(true)
.responseTimeout("30")
.build();
final InputStream inputStream = dsnGet.get("TEST.DATASET", downloadInputData);
assertEquals("{X-IBM-Data-Type=binary, Accept-Encoding=gzip, " +
"X-IBM-Response-Timeout=30, X-CSRF-ZOSMF-HEADER=true, Content-Type=application/json}",
mockGetRequestToken.getHeaders().toString());
assertEquals("test data", new String(inputStream.readAllBytes()));
assertEquals("https://1:443/zosmf/restfiles/ds/TEST.DATASET", mockGetRequestToken.getUrl());
}
@Test
public void tstDsnGetTokenWithMultipleParamsWithoutEncodingAndEtagFalseSuccess() throws ZosmfRequestException, IOException {
final DsnGet dsnGet = new DsnGet(connection, mockGetRequestToken);
final DsnDownloadInputData downloadInputData = new DsnDownloadInputData.Builder()
.binary(true)
.returnEtag(false)
.responseTimeout("30")
.build();
final InputStream inputStream = dsnGet.get("TEST.DATASET", downloadInputData);
assertEquals("{X-IBM-Data-Type=binary, Accept-Encoding=gzip, " +
"X-IBM-Response-Timeout=30, X-CSRF-ZOSMF-HEADER=true, Content-Type=application/json}",
mockGetRequestToken.getHeaders().toString());
assertEquals("test data", new String(inputStream.readAllBytes()));
assertEquals("https://1:443/zosmf/restfiles/ds/TEST.DATASET", mockGetRequestToken.getUrl());
}
@Test
public void tstDsnGetTokenWithMultipleParamsWithoutEncodingAndBinaryAndEtagFalseSuccess() throws ZosmfRequestException, IOException {
final DsnGet dsnGet = new DsnGet(connection, mockGetRequestToken);
final DsnDownloadInputData downloadInputData = new DsnDownloadInputData.Builder()
.binary(false)
.returnEtag(false)
.responseTimeout("30")
.build();
final InputStream inputStream = dsnGet.get("TEST.DATASET", downloadInputData);
assertEquals("{Accept-Encoding=gzip, X-IBM-Response-Timeout=30, X-CSRF-ZOSMF-HEADER=true," +
" Content-Type=application/json}",
mockGetRequestToken.getHeaders().toString());
assertEquals("test data", new String(inputStream.readAllBytes()));
assertEquals("https://1:443/zosmf/restfiles/ds/TEST.DATASET", mockGetRequestToken.getUrl());
}
@Test
public void tstDsnGetSecondaryConstructorWithValidRequestTypeSuccess() {
final ZosConnection connection = Mockito.mock(ZosConnection.class);
final ZosmfRequest request = Mockito.mock(GetStreamZosmfRequest.class);
final DsnGet dsnGet = new DsnGet(connection, request);
assertNotNull(dsnGet);
}
@Test
public void tstDsnGetSecondaryConstructorWithNullConnectionFailure() {
final ZosmfRequest request = Mockito.mock(GetStreamZosmfRequest.class);
NullPointerException exception = assertThrows(
NullPointerException.class,
() -> new DsnGet(null, request)
);
assertEquals("connection is null", exception.getMessage());
}
@Test
public void tstDsnGetSecondaryConstructorWithNullRequestFailure() {
final ZosConnection connection = Mockito.mock(ZosConnection.class);
NullPointerException exception = assertThrows(
NullPointerException.class,
() -> new DsnGet(connection, null)
);
assertEquals("request is null", exception.getMessage());
}
@Test
public void tstDsnGetSecondaryConstructorWithInvalidRequestTypeFailure() {
final ZosConnection connection = Mockito.mock(ZosConnection.class);
final ZosmfRequest request = Mockito.mock(ZosmfRequest.class); // Not a GetZosmfRequest
IllegalStateException exception = assertThrows(
IllegalStateException.class,
() -> new DsnGet(connection, request)
);
assertEquals("GET_STREAM request type required", exception.getMessage());
}
@Test
public void tstDsnGetPrimaryConstructorWithValidConnectionSuccess() {
final ZosConnection connection = Mockito.mock(ZosConnection.class);
final DsnGet dsnGet = new DsnGet(connection);
assertNotNull(dsnGet);
}
@Test
public void tstDsnGetPrimaryConstructorWithNullConnectionFailure() {
NullPointerException exception = assertThrows(
NullPointerException.class,
() -> new DsnGet(null)
);
assertEquals("connection is null", exception.getMessage());
}
@Test
public void tstGetDsnInfoSuccess() throws Exception {
final Dataset mockDataset = new Dataset("TEST.DATA.SET", "", "", "", "",
"", "", "", "", "", "", "", "", "", "",
"", "", "", "");
final List<Dataset> mockList = Collections.singletonList(mockDataset);
try (MockedConstruction<DsnList> mocked = Mockito.mockConstruction(DsnList.class,
(mock, context) ->
Mockito.when(mock.getDatasets(Mockito.anyString(), Mockito.any()))
.thenReturn(mockList))) {
DsnGet dsnGet = new DsnGet(connection);
Dataset result = dsnGet.getDsnInfo("TEST.DATA.SET");
assertNotNull(result);
assertEquals("TEST.DATA.SET", result.getDsname());
assertEquals(1, mocked.constructed().size());
}
}
@Test
public void tstGetDsnInfoDatasetNotFoundFailure() {
try (MockedConstruction<DsnList> ignored = Mockito.mockConstruction(DsnList.class,
(mock, context) -> Mockito.when(mock.getDatasets(Mockito.anyString(), Mockito.any()))
.thenReturn(Collections.emptyList()))) {
final DsnGet dsnGet = new DsnGet(connection);
ZosmfRequestException exception = assertThrows(ZosmfRequestException.class,
() -> dsnGet.getDsnInfo("TEST.DATA.SET"));
assertEquals("dataset not found", exception.getMessage());
}
}
@Test
public void tstGetDsnInfoInvalidDatasetNameFailure() {
final DsnGet dsnGet = new DsnGet(connection);
assertThrows(IllegalArgumentException.class, () -> dsnGet.getDsnInfo("BADNAME"));
}
@Test
public void tstGetDsnInfoBlankDatasetNameFailure() {
final DsnGet dsnGet = new DsnGet(connection);
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> dsnGet.getDsnInfo(" "));
assertEquals("dataSetName not specified", ex.getMessage());
}
}