Skip to content

Commit

Permalink
updated copy pds into new target data set functionality
Browse files Browse the repository at this point in the history
Signed-off-by: Pujal <[email protected]>
  • Loading branch information
pujal0909 committed Dec 20, 2024
1 parent 48fb141 commit dcdb848
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -444,17 +444,23 @@ describe("Copy", () => {
});
describe("Partitioned > Partitioned", () => {
let copyPDSSpy = jest.spyOn(Copy, "copyPDS");
let createSpy: jest.SpyInstance;
let dataSetExistsSpy: jest.SpyInstance;
beforeEach(() => {
copyPDSSpy.mockClear();
copyPDSSpy = jest.spyOn(Copy, "copyPDS").mockResolvedValue({
success:true,
commandResponse: ZosFilesMessages.datasetCopiedSuccessfully.message,
});
isPDSSpy = jest.spyOn(Copy as any, "isPDS").mockResolvedValue(true);
createSpy = jest.spyOn(Create, "dataSetLike");
dataSetExistsSpy = jest.spyOn(Copy, "dataSetExists");
});
afterAll(() => {
copyPDSSpy.mockRestore();
isPDSSpy.mockRestore();
createSpy.mockRestore();
dataSetExistsSpy.mockRestore();
});
it("should call copyPDS to copy members of source PDS to target PDS", async () => {
const response = await Copy.dataSet(
Expand All @@ -475,6 +481,36 @@ describe("Copy", () => {
commandResponse: ZosFilesMessages.datasetCopiedSuccessfully.message
});
});
it("should call Create.dataSetLike and create a new data set if the target data set inputted does not exist", async() => {
const response = await Copy.dataSet(
dummySession,
{dsn: toDataSetName},
{"from-dataset": {
dsn:fromDataSetName
}}
);
dataSetExistsSpy.mockResolvedValue(false);
expect(createSpy).toHaveBeenCalledWith(dummySession, toDataSetName, fromDataSetName);
expect(response).toEqual({
success: true,
commandResponse: ZosFilesMessages.datasetCopiedSuccessfully.message
});
});
it("should not create a new data set if the target data set inputted exists", async() => {
const response = await Copy.dataSet(
dummySession,
{dsn: toDataSetName},
{"from-dataset": {
dsn:fromDataSetName
}}
);
dataSetExistsSpy.mockResolvedValue(true);
expect(createSpy).not.toHaveBeenCalled();
expect(response).toEqual({
success: true,
commandResponse: ZosFilesMessages.datasetCopiedSuccessfully.message
});
});
});
});
describe("Failure Scenarios", () => {
Expand Down Expand Up @@ -564,6 +600,7 @@ describe("Copy", () => {
const readStream = jest.spyOn(IO, "createReadStream");
const rmSync = jest.spyOn(fs, "rmSync");
const listDatasetSpy = jest.spyOn(List, "dataSet");
const targetDataSetExistsSpy = jest.spyOn(Copy, "dataSetExists");

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note test

Unused variable targetDataSetExistsSpy.

const dsPO = {
dsname: fromDataSetName,
Expand Down Expand Up @@ -613,6 +650,47 @@ describe("Copy", () => {
expect(response).toEqual(false);
expect(listDatasetSpy).toHaveBeenCalledWith(dummySession, dsPS.dsname, { attributes: true });
});
it("should return true if the data set exists", async () => {
let caughtError;
let response;
listDatasetSpy.mockImplementation(async (): Promise<any> => {
return {
apiResponse: {
returnedRows: 1,
items: [dsPO]
}
};
});
try {
response = await Copy.dataSetExists(dummySession, dsPO.dsname);
}
catch(e) {
caughtError = e;

Check warning

Code scanning / CodeQL

Useless assignment to local variable Warning test

The value assigned to caughtError here is unused.
}
expect(response).toEqual(true);
expect(listDatasetSpy).toHaveBeenCalledWith(dummySession, dsPO.dsname, { attributes: true , start: dsPO.dsname});
});

it("should return false if the data set does not exist", async () => {
let caughtError;
let response;
listDatasetSpy.mockImplementation(async (): Promise<any> => {
return {
apiResponse: {
returnedRows: 0,
items: []
}
};
});
try {
response = await Copy.dataSetExists(dummySession, dsPO.dsname);
}
catch(e) {
caughtError = e;

Check warning

Code scanning / CodeQL

Useless assignment to local variable Warning test

The value assigned to caughtError here is unused.
}
expect(response).toEqual(false);
expect(listDatasetSpy).toHaveBeenCalledWith(dummySession, dsPO.dsname, { attributes: true , start: dsPO.dsname});
});

it("should successfully copy members from source to target PDS", async () => {
let caughtError;
Expand All @@ -635,7 +713,6 @@ describe("Copy", () => {
uploadSpy.mockResolvedValue(undefined);
rmSync.mockImplementation(jest.fn());


try{
response = await Copy.copyPDS(dummySession, fromDataSetName, toDataSetName);
}
Expand Down
17 changes: 17 additions & 0 deletions packages/zosfiles/src/methods/copy/Copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export class Copy {
ImperativeExpect.toBeDefinedAndNonBlank(toDataSetName, "toDataSetName");

const sourceIsPds = await this.isPDS(session, options["from-dataset"].dsn);
const targetDataSetExists = await this.dataSetExists(session, toDataSetName);
if(!targetDataSetExists) {
await Create.dataSetLike(session, toDataSetName, options["from-dataset"].dsn);

Check warning on line 63 in packages/zosfiles/src/methods/copy/Copy.ts

View check run for this annotation

Codecov / codecov/patch

packages/zosfiles/src/methods/copy/Copy.ts#L63

Added line #L63 was not covered by tests
}
const targetIsPds = await this.isPDS(session, toDataSetName);
if (sourceIsPds && targetIsPds) {
return await this.copyPDS(session, options["from-dataset"].dsn, toDataSetName);
Expand Down Expand Up @@ -121,6 +125,19 @@ export class Copy {
}
}

public static async dataSetExists(
session: AbstractSession,
dataSetName: string
): Promise<boolean> {
let dsnameIndex;
const dataSetList = await List.dataSet(session, dataSetName, {attributes: true, start: dataSetName});
if(dataSetList.apiResponse != null && dataSetList.apiResponse.returnedRows != null && dataSetList.apiResponse.items != null) {
dsnameIndex = dataSetList.apiResponse.returnedRows === 0 ? -1 :
dataSetList.apiResponse.items.findIndex((ds: any) => ds.dsname.toUpperCase() === dataSetName.toUpperCase());
}
return dsnameIndex !== -1;
}

/**
* Copy the members of a Partitioned dataset into another Partitioned dataset
*
Expand Down

0 comments on commit dcdb848

Please sign in to comment.