Skip to content

Commit

Permalink
add ability to add profile to all trees
Browse files Browse the repository at this point in the history
Signed-off-by: Rudy Flores <[email protected]>
  • Loading branch information
rudyflores committed Nov 29, 2023
1 parent b4563fa commit f3929e9
Show file tree
Hide file tree
Showing 15 changed files with 299 additions and 288 deletions.
16 changes: 15 additions & 1 deletion packages/zowe-explorer-api/src/tree/IZoweTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

import * as vscode from "vscode";
import { imperative } from "@zowe/cli";
import { IZoweTreeNode } from "./IZoweTreeNode";
import { DataSetAllocTemplate, PersistenceSchemaEnum } from "../profiles/UserSettings";

Expand Down Expand Up @@ -63,14 +64,27 @@ export interface IZoweTree<T> extends vscode.TreeDataProvider<T> {
* Adds a session to the container
* @param sessionName
* @param type e.g. zosmf
* @param provider tree provider to add to, undefined will add for all
*/
addSession(sessionName?: string, type?: string): Promise<void>;
addSession(sessionName?: string, type?: string, provider?: IZoweTree<IZoweTreeNode>): Promise<void>;

/**
* Adds a single session to the tree
* @param profile the profile to add to the tree
*/
addSingleSession(profile: imperative.IProfileLoaded): Promise<void>;

/**
* Edit a session to the container
* @param node This parameter identifies the node that needs to be called
*/
editSession(node: IZoweTreeNode, zoweFileProvider: IZoweTree<IZoweTreeNode>): Promise<void>;

/**
* Get sessions from persistent object of provider
*/
getSessions(): string[];

/**
* Add a new session to the container
* @param zoweFileProvider The tree to which the profile should be added
Expand Down
4 changes: 4 additions & 0 deletions packages/zowe-explorer/__mocks__/mockCreators/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ export function createTreeView(selection?): vscode.TreeView<ZoweTreeProvider> {
visible: true,
onDidChangeVisibility: jest.fn(),
dispose: jest.fn(),
addSingleSession: jest.fn(),
} as unknown as vscode.TreeView<ZoweTreeProvider>;
}

Expand Down Expand Up @@ -587,6 +588,7 @@ export function createTreeProviders() {
deleteSession: jest.fn(),
removeSession: jest.fn(),
refresh: jest.fn(),
addSingleSession: jest.fn(),
} as any,
uss: {
mSessionNodes: [
Expand All @@ -596,6 +598,7 @@ export function createTreeProviders() {
deleteSession: jest.fn(),
removeSession: jest.fn(),
refresh: jest.fn(),
addSingleSession: jest.fn(),
} as any,
job: {
mSessionNodes: [
Expand All @@ -605,6 +608,7 @@ export function createTreeProviders() {
removeSession: jest.fn(),
deleteSession: jest.fn(),
refresh: jest.fn(),
addSingleSession: jest.fn(),
} as any,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -325,13 +325,19 @@ export async function addProfileDetails(profileName: string) {
await driverChrome.sleep(SHORTSLEEPTIME);
const responseTimeout = await driverChrome.findElement(By.xpath(DatasetsLocators.emptyInputBoxXpath));
responseTimeout.sendKeys(Key.ENTER);
const addToTrees = await driverChrome.findElement(By.xpath(DatasetsLocators.emptyInputBoxXpath));
addToTrees.sendKeys("No");
addToTrees.sendKeys(Key.ENTER);
}
export async function clickOnDatasetsPanel() {
await driverChrome.findElement(By.id(DatasetsLocators.datasetsPanelId)).click();
}

export async function clickOnAddSessionInDatasets() {
await driverChrome.findElement(By.id(DatasetsLocators.datasetsAddSessionId)).click();
const addToTrees = await driverChrome.findElement(By.xpath(DatasetsLocators.emptyInputBoxXpath));
addToTrees.sendKeys("No");
addToTrees.sendKeys(Key.ENTER);
}

export async function getDatasetsDefaultProfilename() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,18 @@ export async function addProfileDetailsInUss(profileName: string) {
const ussProfileName = await driverFirefox.findElement(By.xpath(UssLocators.emptyInputBoxXpath));
ussProfileName.sendKeys(profileName);
ussProfileName.sendKeys(Key.ENTER);
const addToTrees = await driverFirefox.findElement(By.xpath(DatasetsLocators.emptyInputBoxXpath));
addToTrees.sendKeys("No");
addToTrees.sendKeys(Key.ENTER);
}

export async function addProfileDetailsInJobs(profileName: string) {
const jobsProfileName = await driverFirefox.findElement(By.xpath(JobsLocators.emptyInputBoxXpath));
jobsProfileName.sendKeys(profileName);
jobsProfileName.sendKeys(Key.ENTER);
const addToTrees = await driverFirefox.findElement(By.xpath(DatasetsLocators.emptyInputBoxXpath));
addToTrees.sendKeys("No");
addToTrees.sendKeys(Key.ENTER);
}

export async function getUssDefaultProfilename() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,13 +573,19 @@ describe("Profiles Unit Tests - Function createZoweSession", () => {
hide: jest.fn(),
value: "test",
} as any);
jest.spyOn(Gui, "resolveQuickPick").mockResolvedValueOnce(new utils.FilterDescriptor("Test"));
jest.spyOn(Gui, "resolveQuickPick").mockResolvedValueOnce(new utils.FilterDescriptor("Test1"));
jest.spyOn(Gui, "resolveQuickPick").mockResolvedValueOnce(new utils.FilterDescriptor("Test2"));
jest.spyOn(Profiles.getInstance(), "getProfileInfo").mockResolvedValue({
usingTeamConfig: false,
} as any);
jest.spyOn(Gui, "showInputBox").mockResolvedValue("test");
jest.spyOn(Profiles.getInstance(), "createNewConnection").mockResolvedValue("Test");
const refreshSpy = jest.spyOn(Profiles.getInstance(), "refresh").mockImplementation();
jest.spyOn(TreeProviders, "providers", "get").mockReturnValue({
ds: { addSingleSession: jest.fn(), mSessionNodes: [...globalMocks.testUSSTree.mSessionNodes], refresh: jest.fn() } as any,
uss: { addSingleSession: jest.fn(), mSessionNodes: [...globalMocks.testUSSTree.mSessionNodes], refresh: jest.fn() } as any,
jobs: { addSingleSession: jest.fn(), mSessionNodes: [...globalMocks.testUSSTree.mSessionNodes], refresh: jest.fn() } as any,
} as any);
await expect(Profiles.getInstance().createZoweSession(globalMocks.testUSSTree)).resolves.not.toThrow();
expect(refreshSpy).toBeCalledTimes(1);
expect(spyInfo).toBeCalledWith("New profile created, test.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,12 @@ describe("Dataset Tree Unit Tests - Function addSession", () => {
mocked(vscode.window.createTreeView).mockReturnValueOnce(blockMocks.treeView);
const testTree = new DatasetTree();
testTree.mSessionNodes.push(blockMocks.datasetSessionNode);
jest.spyOn(testTree, "addSingleSession").mockImplementation();
jest.spyOn(TreeProviders, "providers", "get").mockReturnValue({
ds: { addSingleSession: jest.fn(), mSessionNodes: [blockMocks.datasetSessionNode], refresh: jest.fn() } as any,
uss: { addSingleSession: jest.fn(), mSessionNodes: [blockMocks.datasetSessionNode], refresh: jest.fn() } as any,
jobs: { addSingleSession: jest.fn(), mSessionNodes: [blockMocks.datasetSessionNode], refresh: jest.fn() } as any,
} as any);

await testTree.addSession(blockMocks.imperativeProfile.name);
expect(testTree.mSessionNodes[1].label).toBe(blockMocks.imperativeProfile.name);
Expand Down Expand Up @@ -1553,6 +1559,13 @@ describe("Dataset Tree Unit Tests - Function datasetFilterPrompt", () => {
);
favoriteSearch.contextValue = globals.DS_SESSION_CONTEXT + globals.FAV_SUFFIX;

jest.spyOn(testTree, "addSingleSession").mockImplementation();
jest.spyOn(TreeProviders, "providers", "get").mockReturnValue({
ds: { addSingleSession: jest.fn(), mSessionNodes: [blockMocks.datasetSessionNode], refresh: jest.fn() } as any,
uss: { addSingleSession: jest.fn(), mSessionNodes: [blockMocks.datasetSessionNode], refresh: jest.fn() } as any,
jobs: { addSingleSession: jest.fn(), mSessionNodes: [blockMocks.datasetSessionNode], refresh: jest.fn() } as any,
} as any);

await testTree.datasetFilterPrompt(favoriteSearch);

expect(addSessionSpy).toHaveBeenLastCalledWith(blockMocks.datasetSessionNode.label.trim());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,12 @@ describe("ZoweJobNode unit tests - Function createJobsTree", () => {
describe("ZoweJobNode unit tests - Function addSession", () => {
it("Tests that addSession adds the session to the tree", async () => {
const globalMocks = await createGlobalMocks();

jest.spyOn(TreeProviders, "providers", "get").mockReturnValue({
ds: { addSingleSession: jest.fn(), mSessionNodes: [...globalMocks.testJobsProvider.mSessionNodes], refresh: jest.fn() } as any,
uss: { addSingleSession: jest.fn(), mSessionNodes: [...globalMocks.testJobsProvider.mSessionNodes], refresh: jest.fn() } as any,
jobs: { addSingleSession: jest.fn(), mSessionNodes: [...globalMocks.testJobsProvider.mSessionNodes], refresh: jest.fn() } as any,
} as any);
await globalMocks.testJobsProvider.addSession("sestest");

expect(globalMocks.testJobsProvider.mSessionNodes[1]).toBeDefined();
expect(globalMocks.testJobsProvider.mSessionNodes[1].label).toEqual("sestest");
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ async function createGlobalMocks() {
value: jest.fn().mockReturnValue({ usingTeamConfig: false }),
});

jest.spyOn(TreeProviders, "providers", "get").mockReturnValue({
ds: { addSingleSession: jest.fn(), mSessionNodes: [...globalMocks.testTree.mSessionNodes], refresh: jest.fn() } as any,
uss: { addSingleSession: jest.fn(), mSessionNodes: [...globalMocks.testTree.mSessionNodes], refresh: jest.fn() } as any,
jobs: { addSingleSession: jest.fn(), mSessionNodes: [...globalMocks.testTree.mSessionNodes], refresh: jest.fn() } as any,
} as any);

return globalMocks;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ describe("ProfileManagement unit tests", () => {
ProfileManagement["getPromptChangeForAllTreesOptions"]()[1]
);
mocks.mockJobSessionNode.contextValue = globals.JOBS_SESSION_CONTEXT;
mocks.mockJobSessionNode.getLabel = jest.fn(() => "test");
await expect(ProfileManagement["handleHideProfiles"](mocks.mockJobSessionNode)).resolves.toEqual(undefined);
expect(commandSpy).toHaveBeenCalledWith("zowe.jobs.removeSession", mocks.mockJobSessionNode, null, false);
});
Expand Down
23 changes: 20 additions & 3 deletions packages/zowe-explorer/src/Profiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import * as nls from "vscode-nls";
import { SettingsConfig } from "./utils/SettingsConfig";
import { ZoweLogger } from "./utils/LoggerUtils";
import { TreeProviders } from "./shared/TreeProviders";
import { ProfileManagement } from "./utils/ProfileManagement";

// Set up localization
nls.config({
Expand Down Expand Up @@ -458,13 +459,23 @@ export class Profiles extends ProfilesCache {
await errorHandling(error, newprofile);
}
ZoweLogger.info(localize("createZoweSession.createNewProfile", "New profile created, {0}.", chosenProfile));
await zoweFileProvider.addSession(newprofile);
await zoweFileProvider.refresh();

if (await this.shouldAddForAllTrees(newprofile)) {
await zoweFileProvider.addSession(newprofile);
} else {
await zoweFileProvider.addSession(newprofile, undefined, zoweFileProvider);
}

zoweFileProvider.refresh();
}
}
} else if (chosenProfile) {
ZoweLogger.info(localize("createZoweSession.addProfile", "The profile {0} has been added to the {1} tree.", chosenProfile, treeType));
await zoweFileProvider.addSession(chosenProfile);
if (await this.shouldAddForAllTrees(chosenProfile)) {
await zoweFileProvider.addSession(chosenProfile);
} else {
await zoweFileProvider.addSession(chosenProfile, undefined, zoweFileProvider);
}
} else {
ZoweLogger.debug(debugMsg);
}
Expand Down Expand Up @@ -1905,4 +1916,10 @@ export class Profiles extends ProfilesCache {
newConfig.autoStore = false;
}
}

private async shouldAddForAllTrees(nodeName: string): Promise<boolean> {
const [qpAll] = ProfileManagement.getPromptChangeForAllTreesOptions();
const selection = await ProfileManagement.promptChangeForAllTrees(nodeName, true);
return selection?.label === qpAll.label;
}
}
78 changes: 77 additions & 1 deletion packages/zowe-explorer/src/abstract/ZoweTreeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { SettingsConfig } from "../utils/SettingsConfig";
import { ZoweLogger } from "../utils/LoggerUtils";
import { TreeProviders } from "../shared/TreeProviders";
import { IZoweProviders } from "../shared/init";
import { resetValidationSettings } from "../shared/actions";

// Set up localization
nls.config({
Expand Down Expand Up @@ -184,6 +185,18 @@ export class ZoweTreeProvider {
return undefined;
}

public async addSession(sessionName?: string, profileType?: string, provider?: IZoweTree<IZoweTreeNode>): Promise<void> {
ZoweLogger.trace("ZoweTreeProvider.addSession called.");
if (provider) {
await this.addSessionForProvider(sessionName, profileType, provider);
} else {
for (const key of Object.keys(TreeProviders.providers)) {
const tree = TreeProviders.providers[key];
await this.addSessionForProvider(sessionName, profileType, tree);
}
}
}

public deleteSession(node: IZoweTreeNode, hideFromAllTrees?: boolean): void {
ZoweLogger.trace("ZoweTreeProvider.deleteSession called.");
if (hideFromAllTrees) {
Expand Down Expand Up @@ -212,7 +225,7 @@ export class ZoweTreeProvider {
} else {
zoweFileProvider.deleteSession(node.getSessionNode());
this.mHistory.addSession(node.label as string);
await zoweFileProvider.addSession(node.getProfileName());
await zoweFileProvider.addSession(node.getProfileName(), undefined, zoweFileProvider);
}
this.refresh();
// Remove the edited profile from profilesForValidation
Expand Down Expand Up @@ -312,4 +325,67 @@ export class ZoweTreeProvider {
provider.removeSession(node.getLabel() as string);
provider.refresh();
}

private async addSessionForProvider(sessionName?: string, profileType?: string, treeProvider?: IZoweTree<IZoweTreeNode>): Promise<void> {
const isUsingAutomaticProfileValidation: boolean = SettingsConfig.getDirectValue(globals.SETTINGS_AUTOMATIC_PROFILE_VALIDATION);
if (sessionName) {
await this.loadProfileBySessionName(sessionName, treeProvider, isUsingAutomaticProfileValidation);
} else {
await this.loadProfileByPersistedProfile(treeProvider, profileType, isUsingAutomaticProfileValidation);
}
treeProvider.refresh();
}

private async loadProfileBySessionName(
sessionName: string,
treeProvider: IZoweTree<IZoweTreeNode>,
isUsingAutomaticProfileValidation: boolean
): Promise<void> {
const profile: imperative.IProfileLoaded = Profiles.getInstance().loadNamedProfile(sessionName.trim());
if (profile) {
await treeProvider.addSingleSession(profile);
for (const node of treeProvider.mSessionNodes) {
if (node.label !== "Favorites") {
const name = node.getProfileName();
if (name === profile.name) {
resetValidationSettings(node, isUsingAutomaticProfileValidation);
}
}
}
}
}

private async loadProfileByPersistedProfile(
treeProvider: IZoweTree<IZoweTreeNode>,
profileType: string,
isUsingAutomaticProfileValidation: boolean
): Promise<void> {
const profiles: imperative.IProfileLoaded[] = await Profiles.getInstance().fetchAllProfiles();
if (profiles) {
for (const profile of profiles) {
if (!treeProvider.mSessionNodes.find((mSessionNode) => mSessionNode.label.toString().trim() === profile.name.trim())) {
for (const session of treeProvider.getSessions()) {
if (session && session.trim() === profile.name) {
await treeProvider.addSingleSession(profile);
for (const node of treeProvider.mSessionNodes) {
if (node.label !== "Favorites") {
const name = node.getProfileName();
if (name === profile.name) {
resetValidationSettings(node, isUsingAutomaticProfileValidation);
}
}
}
}
}
}
}
}
if (treeProvider.mSessionNodes.length === 1) {
try {
await treeProvider.addSingleSession(Profiles.getInstance().getDefaultProfile(profileType));
} catch (error) {
ZoweLogger.warn(error);
}
}
}
}
Loading

0 comments on commit f3929e9

Please sign in to comment.