Skip to content

Commit

Permalink
Merge pull request #2381 from zowe/api-response-fix
Browse files Browse the repository at this point in the history
Add content for api response on upload SDK methods
  • Loading branch information
awharn authored Dec 16, 2024
2 parents ac260f4 + ac0338e commit 7657a23
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/zosfiles/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ All notable changes to the Zowe z/OS files SDK package will be documented in thi

## Recent Changes

- BugFix: Corrected the `apiResponse` response value from `streamToDataSet()`,`streamToUss()`,`bufferToUss()` and `bufferToDataSet()` on the Upload SDK. [#2381](https://github.com/zowe/zowe-cli/pull/2381)
- BugFix: Corrected the `Upload.BufferToUssFile()` SDK function to properly tag uploaded files. [#2378](https://github.com/zowe/zowe-cli/pull/2378)

## `8.9.0`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { deleteFiles, getUniqueDatasetName, stripNewLines, wait, waitTime } from
import * as fs from "fs";
import { ITestEnvironment } from "../../../../../../__tests__/__src__/environment/ITestEnvironment";
import { runCliScript } from "../../../../../../__tests__/__packages__/cli-test-utils/src";
import { Readable } from "stream";

let REAL_SESSION: Session;
let testEnvironment: ITestEnvironment<ITestPropertiesSchema>;
Expand Down Expand Up @@ -309,6 +310,49 @@ describe("Upload Data Set", () => {
}
});

it("should upload a PDS file - bufferToDataSet()", async () => {
let error;
let uploadResponse;
let getResponse;
const data: Buffer = Buffer.from(testdata);

try {
uploadResponse = await Upload.bufferToDataSet(REAL_SESSION, data, dsname+"(TEST)");
getResponse = await Get.dataSet(REAL_SESSION, dsname+"(TEST)");
} catch (err) {
error = err;
Imperative.console.info("Error: " + inspect(error));
}

expect(error).toBeFalsy();

expect(uploadResponse.apiResponse).toMatchObject({"success": true, "from": "Buffer<>","to": dsname+"(TEST)"});
expect(Buffer.from(getResponse.toString().trim())).toEqual(data);
});

it("should upload a PDS file - streamToDataSet()", async () => {
let error;
let uploadResponse;
let getResponse;

const inputStream = new Readable();
inputStream.push(testdata);
inputStream.push(null);

try {
uploadResponse = await Upload.streamToDataSet(REAL_SESSION, inputStream, dsname+"(TEST)");
getResponse = await Get.dataSet(REAL_SESSION, dsname+"(TEST)");
} catch (err) {
error = err;
Imperative.console.info("Error: " + inspect(error));
}

expect(error).toBeFalsy();

expect(uploadResponse.apiResponse).toMatchObject({"success": true, "from": "Stream<>","to": dsname+"(TEST)"});
expect(getResponse.toString().trim()).toEqual(testdata);
});

it("should upload a file to a partitioned data set member using full path", async () => {
let error;
let response: IZosFilesResponse;
Expand Down Expand Up @@ -733,7 +777,7 @@ describe("Upload USS file", () => {
await deleteFiles(REAL_SESSION, ussname);
});

it("should upload a USS file", async () => {
it("should upload a USS file - bufferToUssFile()", async () => {
let error;
let uploadResponse;
let getResponse;
Expand All @@ -748,9 +792,33 @@ describe("Upload USS file", () => {
}

expect(error).toBeFalsy();

expect(uploadResponse.apiResponse).toMatchObject({"success": true, "from": "Buffer<>","to": ussname});
expect(getResponse).toEqual(Buffer.from(data.toString()));
});

it("should upload a USS file - streamToUssFile()", async () => {
let error;
let uploadResponse;
let getResponse;
const inputStream = new Readable();
inputStream.push(testdata);
inputStream.push(null);

try {
uploadResponse = await Upload.streamToUssFile(REAL_SESSION, ussname, inputStream);
getResponse = await Get.USSFile(REAL_SESSION, ussname);
} catch (err) {
error = err;
Imperative.console.info("Error: " + inspect(error));
}

expect(error).toBeFalsy();

expect(uploadResponse.apiResponse).toMatchObject({"success": true, "from": "Stream<>","to": ussname});
expect(getResponse).toEqual(Buffer.from(testdata));
});

it("should upload a USS file in binary mode", async () => {
let error;
let uploadResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { IUploadOptions } from "../../../../src/methods/upload/doc/IUploadOption
import { Upload } from "../../../../src/methods/upload/Upload";
import { List } from "../../../../src/methods/list/List";
import { Utilities } from "../../../../src/methods/utilities/Utilities";

import { inspect } from "util";
import { ZosFilesUtils } from "../../../../src/utils/ZosFilesUtils";
import { stripNewLines } from "../../../../../../__tests__/__src__/TestUtils";
import { Create } from "../../../../src/methods/create";
Expand Down Expand Up @@ -379,7 +379,7 @@ describe("z/OS Files - Upload", () => {
expect(error).toBeDefined();
expect(error).toBe(testError);
});
it("should return with proper response when upload buffer to a data set", async () => {
it("should return with proper response when upload buffer to a data set - buffer less than 10 chars", async () => {
const buffer: Buffer = Buffer.from("testing");
const endpoint = path.posix.join(ZosFilesConstants.RESOURCE, ZosFilesConstants.RES_DS_FILES, dsName);
const reqHeaders = [ZosmfHeaders.X_IBM_TEXT, ZosmfHeaders.ACCEPT_ENCODING];
Expand All @@ -392,6 +392,27 @@ describe("z/OS Files - Upload", () => {

expect(error).toBeUndefined();
expect(response).toBeDefined();
expect(response.apiResponse).toMatchObject({"from": "<Buffer 74 65 73 74 69 6e 67>", "success": true, "to": dsName});

expect(zosmfPutFullSpy).toHaveBeenCalledTimes(1);
expect(zosmfPutFullSpy).toHaveBeenCalledWith(dummySession, {resource: endpoint,
reqHeaders,
writeData: buffer});
});
it("should return with proper response when upload buffer to a data set - buffer more than 10 chars", async () => {
const buffer: Buffer = Buffer.from("bufferLargerThan10Chars");
const endpoint = path.posix.join(ZosFilesConstants.RESOURCE, ZosFilesConstants.RES_DS_FILES, dsName);
const reqHeaders = [ZosmfHeaders.X_IBM_TEXT, ZosmfHeaders.ACCEPT_ENCODING];

try {
response = await Upload.bufferToDataSet(dummySession, buffer, dsName);
} catch (err) {
error = err;
}

expect(error).toBeUndefined();
expect(response).toBeDefined();
expect(response.apiResponse).toMatchObject({"from": "<Buffer 62 75 66 66 65 72 4c 61 72 67...>", "success": true, "to": dsName});

expect(zosmfPutFullSpy).toHaveBeenCalledTimes(1);
expect(zosmfPutFullSpy).toHaveBeenCalledWith(dummySession, {resource: endpoint,
Expand Down Expand Up @@ -748,6 +769,7 @@ describe("z/OS Files - Upload", () => {

expect(error).toBeUndefined();
expect(response).toBeDefined();
expect(response.apiResponse).toMatchObject({"from": "[Readable]", "success": true, "to": dsName});

expect(zosmfPutFullSpy).toHaveBeenCalledTimes(1);
expect(zosmfPutFullSpy).toHaveBeenCalledWith(dummySession, {resource: endpoint,
Expand Down Expand Up @@ -1755,6 +1777,7 @@ describe("z/OS Files - Upload", () => {

expect(error).toBeUndefined();
expect(USSresponse).toBeDefined();
expect(USSresponse.apiResponse).toMatchObject({"from": "<Buffer 74 65 73 74 69 6e 67 0a 74 65...>", "success": true, "to": dsName});

const normalizedData = ZosFilesUtils.normalizeNewline(data);
expect(data.length).not.toBe(normalizedData.length);
Expand Down Expand Up @@ -1830,6 +1853,7 @@ describe("z/OS Files - Upload", () => {

expect(error).toBeUndefined();
expect(USSresponse).toBeDefined();
expect(USSresponse.apiResponse).toMatchObject({"from": "[Readable]", "success": true, "to": dsName});
expect(USSresponse.success).toBeTruthy();

expect(zosmfExpectFullSpy).toHaveBeenCalledTimes(1);
Expand Down
28 changes: 23 additions & 5 deletions packages/zosfiles/src/methods/upload/Upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { Utilities, Tag } from "../utilities";
import { Readable } from "stream";
import { CLIENT_PROPERTY } from "../../doc/types/ZosmfRestClientProperties";
import { TransferMode } from "../../utils/ZosFilesAttributes";

import { inspect } from "util";

export class Upload {

Expand Down Expand Up @@ -183,8 +183,13 @@ export class Upload {
}
const uploadRequest: IRestClientResponse = await ZosmfRestClient.putExpectFullResponse(session, requestOptions);

const maxBufferPreviewSize = 10;
// By default, apiResponse is empty when uploading
const apiResponse: any = {};
const apiResponse: any = {
success: true,
from: fileBuffer.length > maxBufferPreviewSize ? inspect(fileBuffer.subarray(0, maxBufferPreviewSize)).slice(0, -1) + "...>" : inspect(fileBuffer),
to: dataSetName
};

// Return Etag in apiResponse, if requested
if (options.returnEtag) {
Expand Down Expand Up @@ -242,7 +247,11 @@ export class Upload {
const uploadRequest: IRestClientResponse = await ZosmfRestClient.putExpectFullResponse(session, requestOptions);

// By default, apiResponse is empty when uploading
const apiResponse: any = {};
const apiResponse: any = {
success: true,
from: inspect(fileStream, { showHidden: false, depth: -1}),
to: dataSetName
};

// Return Etag in apiResponse, if requested
if (options.returnEtag) {
Expand Down Expand Up @@ -480,8 +489,13 @@ export class Upload {
}
const uploadRequest: IRestClientResponse = await ZosmfRestClient.putExpectFullResponse(session, requestOptions);

const maxBufferPreviewSize = 10;
// By default, apiResponse is empty when uploading
const apiResponse: any = {};
const apiResponse: any = {
success: true,
from: fileBuffer.length > maxBufferPreviewSize ? inspect(fileBuffer.subarray(0, maxBufferPreviewSize)).slice(0, -1) + "...>" : inspect(fileBuffer),
to: origUssname
};

// Return Etag in apiResponse, if requested
if (options.returnEtag) {
Expand Down Expand Up @@ -541,7 +555,11 @@ export class Upload {
}

// By default, apiResponse is empty when uploading
const apiResponse: any = {};
const apiResponse: any = {
success: true,
from: inspect(uploadStream, { showHidden: false, depth: -1}),
to: origUssname
};

// Return Etag in apiResponse, if requested
if (options.returnEtag) {
Expand Down

0 comments on commit 7657a23

Please sign in to comment.