Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ActionFlowApi } from "./action-flow-api";
import { fileService, FileService } from "../../../core/utils/file-service";
import { logger } from "../../../core/utils/logger";
import { FileConstants } from "../../../core/utils/file.constants";
import { resolve } from "node:path";

export class ActionFlowService {
public static readonly METADATA_FILE_NAME = "metadata.json";
Expand All @@ -31,7 +32,8 @@ export class ActionFlowService {
}

const fileName = "action-flows_export_" + uuidv4() + ".zip";
zip.writeZip(fileName, () => fs.chmodSync(fileName, FileConstants.DEFAULT_FILE_PERMISSIONS));
const fullFilePath = resolve(process.cwd(), fileName);
zip.writeZip(fullFilePath, () => fs.chmodSync(fullFilePath, FileConstants.DEFAULT_FILE_PERMISSIONS));
logger.info(FileService.fileDownloadedMessage + fileName);
}

Expand Down
4 changes: 2 additions & 2 deletions src/commands/asset-registry/asset-registry.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Context } from "../../core/command/cli-context";
import { fileService, FileService } from "../../core/utils/file-service";
import { FatalError, logger } from "../../core/utils/logger";
import { v4 as uuidv4 } from "uuid";
import * as fs from "fs";

export class AssetRegistryService {
private api: AssetRegistryApi;
Expand Down Expand Up @@ -92,7 +91,8 @@ export class AssetRegistryService {
}

if (hasFile) {
return this.parseJson(fs.readFileSync(opts.file!, "utf-8"), `-f ${opts.file}`);

return this.parseJson(fileService.readFile(opts.file), `-f ${opts.file}`);
}

if (hasNodeKey && hasConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Context } from "../../core/command/cli-context";
import { fileService, FileService } from "../../core/utils/file-service";
import { logger } from "../../core/utils/logger";
import { SinglePackageExportApi } from "./api/single-package-export-api";
import { resolve } from "node:path";

export class SinglePackageExportService {

Expand All @@ -16,7 +17,7 @@ export class SinglePackageExportService {

if (zip) {
const fileName = `${packageKey}.zip`;
fileService.writeBufferToFileWithGivenName(packageData, fileName);
fileService.writeBufferToFileWithGivenName(packageData, resolve(process.cwd(), fileName));
logger.info(FileService.fileDownloadedMessage + fileName);
return;
}
Expand Down
6 changes: 3 additions & 3 deletions src/commands/configuration-management/variable.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,19 @@ export class VariableService {
}

private async getVersionedVariablesByKeyVersionPairs(keysByVersion: string[], keysByVersionFile: string): Promise<VariableManifestTransport[]> {
const variablesExportRequest: PackageKeyAndVersionPair[] = await this.buildKeyVersionPairs(keysByVersion, keysByVersionFile);
const variablesExportRequest: PackageKeyAndVersionPair[] = this.buildKeyVersionPairs(keysByVersion, keysByVersionFile);

const variableManifests = await this.variableApi.findVariablesWithValuesByPackageKeysAndVersion(variablesExportRequest);
return fixConnectionVariables(variableManifests);
}

private async buildKeyVersionPairs(keysByVersion: string[], keysByVersionFile: string): Promise<PackageKeyAndVersionPair[]> {
private buildKeyVersionPairs(keysByVersion: string[], keysByVersionFile: string): PackageKeyAndVersionPair[] {
let variablesExportRequest: PackageKeyAndVersionPair[] = [];

if (keysByVersion.length !== 0) {
variablesExportRequest = this.buildKeyAndVersionPairsFromArrayInput(keysByVersion);
} else if (keysByVersion.length === 0 && keysByVersionFile !== "") {
variablesExportRequest = await fileService.readFileToJson(keysByVersionFile);
variablesExportRequest = fileService.readFileToJson(keysByVersionFile);
} else {
throw new FatalError("Please provide keysByVersion mappings or file path!");
}
Expand Down
6 changes: 4 additions & 2 deletions src/commands/t2tc/t2tc-package.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { StudioService } from "./studio.service";
import { GitService } from "../../core/git-profile/git/git.service";
import * as fs from "fs";
import { FileConstants } from "../../core/utils/file.constants";
import { resolve } from "node:path";

export class T2tcPackageService {

Expand Down Expand Up @@ -142,7 +143,7 @@ export class T2tcPackageService {
await this.studioService.processImportedPackages(configs, existingStudioPackages, studioManifests);

if (gitBranch) {
fs.rmSync(temporaryGitFolder, { recursive: true });
fs.rmSync(temporaryGitFolder, { recursive: true, force: true });
}
fs.rmSync(sourceToBeImported);

Expand Down Expand Up @@ -248,7 +249,8 @@ export class T2tcPackageService {
} else {
const fileDownloadedMessage = "File downloaded successfully. New filename: ";
const filename = `export_${uuidv4()}.zip`;
exportedZip.writeZip(filename, () => fs.chmodSync(filename, FileConstants.DEFAULT_FILE_PERMISSIONS));
const fullFilePath = resolve(process.cwd(), filename);
exportedZip.writeZip(fullFilePath, () => fs.chmodSync(fullFilePath, FileConstants.DEFAULT_FILE_PERMISSIONS));
logger.info(fileDownloadedMessage + filename);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/utils/file-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class FileService {
this.restrictFilePermissions(targetPath);
}

public async readFileToJson<T>(fileName: string): Promise<T> {
public readFileToJson(fileName: string): any {
const fileContent = this.readFile(fileName);

return JSON.parse(fileContent);
Expand Down
6 changes: 3 additions & 3 deletions tests/commands/action-flows/analyze-action-flows.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as path from "path";
import { mockedAxiosInstance } from "../../utls/http-requests-mock";
import { loggingTestTransport, mockWriteFileSync } from "../../jest.setup";
import { loggingTestTransport } from "../../jest.setup";
import { FileService } from "../../../src/core/utils/file-service";
import { ActionFlowCommandService } from "../../../src/commands/action-flows/action-flow/action-flow-command.service";
import { testContext } from "../../utls/test-context";
import { getJsonFromDownloadedFile, getJsonFromFile } from "../../utls/fs-utils";

describe("Analyze action-flows", () => {

Expand Down Expand Up @@ -62,9 +63,8 @@ describe("Analyze action-flows", () => {

expect(loggingTestTransport.logMessages.length).toBe(1);
expect(loggingTestTransport.logMessages[0].message).toContain(FileService.fileDownloadedMessage);
const expectedFileName = loggingTestTransport.logMessages[0].message.split(FileService.fileDownloadedMessage)[1];

expect(mockWriteFileSync).toHaveBeenCalledWith(path.resolve(process.cwd(), expectedFileName), JSON.stringify(mockAnalyzeResponse, null, 4), { encoding: "utf-8", mode: 0o600 });
expect(getJsonFromDownloadedFile()).toEqual(mockAnalyzeResponse);
expect(mockedAxiosInstance.get).toHaveBeenCalledWith(`https://myTeam.celonis.cloud/ems-automation/api/root/${packageId}/export/assets/analyze`, expect.anything());
});
});
27 changes: 5 additions & 22 deletions tests/commands/action-flows/export-action-flows.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import * as AdmZip from "adm-zip";
import * as fs from "fs";
import { parse, stringify } from "yaml";
import { mockAxiosGet } from "../../utls/http-requests-mock";
import { ActionFlowCommandService } from "../../../src/commands/action-flows/action-flow/action-flow-command.service";
import { loggingTestTransport, mockWriteSync } from "../../jest.setup";
import { FileService } from "../../../src/core/utils/file-service";
import { mockExistsSyncOnce, mockReadFileSync } from "../../utls/fs-mock-utils";
import { loggingTestTransport } from "../../jest.setup";
import { ActionFlowService } from "../../../src/commands/action-flows/action-flow/action-flow.service";
import { testContext } from "../../utls/test-context";
import { getDownloadedFileName, writeJsonTempFile } from "../../utls/fs-utils";

describe("Export action-flows", () => {

Expand Down Expand Up @@ -69,10 +67,6 @@ describe("Export action-flows", () => {
},
};

beforeEach(() => {
(fs.openSync as jest.Mock).mockReturnValue(100);
});

it("Should call export API and return the zip response", async () => {
const zipExport = new AdmZip();
zipExport.addFile(actionFlowFileName, Buffer.from(stringify(actionFlowConfig)));
Expand All @@ -82,12 +76,7 @@ describe("Export action-flows", () => {
await new ActionFlowCommandService(testContext).exportActionFlows(packageId, null);

expect(loggingTestTransport.logMessages.length).toBe(1);
const expectedZipFileName = loggingTestTransport.logMessages[0].message.split(FileService.fileDownloadedMessage)[1];
expect(fs.openSync).toHaveBeenCalledWith(expectedZipFileName, expect.anything(), expect.anything());
expect(mockWriteSync).toHaveBeenCalled();

const fileBuffer = mockWriteSync.mock.calls[0][1];
const receivedZip = new AdmZip(fileBuffer);
const receivedZip = new AdmZip(getDownloadedFileName());

expect(receivedZip.getEntries().length).toBe(1);
const receivedZipEntry = receivedZip.getEntries()[0];
Expand Down Expand Up @@ -131,21 +120,15 @@ describe("Export action-flows", () => {
"actionFlowsTeamId": "555",
};

mockExistsSyncOnce();
mockReadFileSync(stringify(metadata));
writeJsonTempFile("metadata.json", metadata);
const zipExport = new AdmZip();
zipExport.addFile(actionFlowFileName, Buffer.from(stringify(actionFlowConfig)));

mockAxiosGet(`https://myTeam.celonis.cloud/ems-automation/api/root/${packageId}/export/assets`, zipExport.toBuffer());
await new ActionFlowCommandService(testContext).exportActionFlows(packageId, ActionFlowService.METADATA_FILE_NAME);

expect(loggingTestTransport.logMessages.length).toBe(1);
const expectedZipFileName = loggingTestTransport.logMessages[0].message.split(FileService.fileDownloadedMessage)[1];
expect(fs.openSync).toHaveBeenCalledWith(expectedZipFileName, expect.anything(), expect.anything());
expect(mockWriteSync).toHaveBeenCalled();

const fileBuffer = mockWriteSync.mock.calls[0][1];
const receivedZip = new AdmZip(fileBuffer);
const receivedZip = new AdmZip(getDownloadedFileName());

expect(receivedZip.getEntries().length).toBe(2);
expect(receivedZip.getEntries().filter(entry => entry.name === ActionFlowService.METADATA_FILE_NAME).length).toBe(1);
Expand Down
16 changes: 7 additions & 9 deletions tests/commands/action-flows/import-action-flows.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import * as path from "path";
import * as AdmZip from "adm-zip";
import { mockedAxiosInstance } from "../../utls/http-requests-mock";
import { mockCreateReadStream } from "../../utls/fs-mock-utils";
import { ActionFlowCommandService } from "../../../src/commands/action-flows/action-flow/action-flow-command.service";
import { loggingTestTransport, mockWriteFileSync } from "../../jest.setup";
import { loggingTestTransport } from "../../jest.setup";
import { FileService } from "../../../src/core/utils/file-service";
import { testContext } from "../../utls/test-context";
import { getJsonFromDownloadedFile, zipToTempFolder } from "../../utls/fs-utils";

describe("Import action-flows", () => {

Expand All @@ -28,9 +27,9 @@ describe("Import action-flows", () => {
const resp = { data: mockImportResponse };
(mockedAxiosInstance.post as jest.Mock).mockResolvedValue(resp);
const zip = new AdmZip();
mockCreateReadStream(zip.toBuffer());
const zipPath = zipToTempFolder(zip);

await new ActionFlowCommandService(testContext).importActionFlows(packageId, "tmp", true, false);
await new ActionFlowCommandService(testContext).importActionFlows(packageId, zipPath, true, false);

expect(loggingTestTransport.logMessages.length).toBe(1);
expect(loggingTestTransport.logMessages[0].message).toContain(JSON.stringify(mockImportResponse, null, 4));
Expand All @@ -42,15 +41,14 @@ describe("Import action-flows", () => {
const resp = { data: mockImportResponse };
(mockedAxiosInstance.post as jest.Mock).mockResolvedValue(resp);
const zip = new AdmZip();
mockCreateReadStream(zip.toBuffer());
const zipPath = zipToTempFolder(zip);

await new ActionFlowCommandService(testContext).importActionFlows(packageId, "tmp", true, true);
await new ActionFlowCommandService(testContext).importActionFlows(packageId, zipPath, true, true);

expect(loggingTestTransport.logMessages.length).toBe(1);
expect(loggingTestTransport.logMessages[0].message).toContain(FileService.fileDownloadedMessage);
const expectedFileName = loggingTestTransport.logMessages[0].message.split(FileService.fileDownloadedMessage)[1];

expect(mockWriteFileSync).toHaveBeenCalledWith(path.resolve(process.cwd(), expectedFileName), JSON.stringify(mockImportResponse, null, 4), { encoding: "utf-8", mode: 0o600 });
expect(getJsonFromDownloadedFile()).toEqual(mockImportResponse);
expect(mockedAxiosInstance.post).toHaveBeenCalledWith(`https://myTeam.celonis.cloud/ems-automation/api/root/${packageId}/import/assets`, expect.anything(), expect.anything());
});
});
14 changes: 3 additions & 11 deletions tests/commands/asset-registry/asset-registry-examples.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { mockAxiosGet } from "../../utls/http-requests-mock";
import { AssetRegistryService } from "../../../src/commands/asset-registry/asset-registry.service";
import { testContext } from "../../utls/test-context";
import { loggingTestTransport, mockWriteFileSync } from "../../jest.setup";
import { FileService } from "../../../src/core/utils/file-service";
import * as path from "path";
import { loggingTestTransport } from "../../jest.setup";
import { getJsonFromDownloadedFile } from "../../utls/fs-utils";

describe("Asset registry examples", () => {
const examplesResponse = [
Expand All @@ -27,14 +26,7 @@ describe("Asset registry examples", () => {

await new AssetRegistryService(testContext).getExamples("BOARD_V2", true);

const expectedFileName = loggingTestTransport.logMessages[0].message.split(FileService.fileDownloadedMessage)[1];
expect(mockWriteFileSync).toHaveBeenCalledWith(
path.resolve(process.cwd(), expectedFileName),
expect.any(String),
{ encoding: "utf-8", mode: 0o600 }
);

const written = JSON.parse(mockWriteFileSync.mock.calls[0][1]);
const written = getJsonFromDownloadedFile();
expect(written.length).toBe(2);
expect(written[0].name).toBe("Simple View");
});
Expand Down
14 changes: 3 additions & 11 deletions tests/commands/asset-registry/asset-registry-get.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { AssetRegistryDescriptor } from "../../../src/commands/asset-registry/as
import { mockAxiosGet } from "../../utls/http-requests-mock";
import { AssetRegistryService } from "../../../src/commands/asset-registry/asset-registry.service";
import { testContext } from "../../utls/test-context";
import { loggingTestTransport, mockWriteFileSync } from "../../jest.setup";
import { FileService } from "../../../src/core/utils/file-service";
import * as path from "path";
import { loggingTestTransport } from "../../jest.setup";
import { getJsonFromDownloadedFile } from "../../utls/fs-utils";

describe("Asset registry get", () => {
const boardDescriptor: AssetRegistryDescriptor = {
Expand Down Expand Up @@ -46,14 +45,7 @@ describe("Asset registry get", () => {

await new AssetRegistryService(testContext).getType("BOARD_V2", true);

const expectedFileName = loggingTestTransport.logMessages[0].message.split(FileService.fileDownloadedMessage)[1];
expect(mockWriteFileSync).toHaveBeenCalledWith(
path.resolve(process.cwd(), expectedFileName),
expect.any(String),
{ encoding: "utf-8", mode: 0o600 }
);

const written = JSON.parse(mockWriteFileSync.mock.calls[0][1]) as AssetRegistryDescriptor;
const written = getJsonFromDownloadedFile() as AssetRegistryDescriptor;
expect(written.assetType).toBe("BOARD_V2");
expect(written.displayName).toBe("View");
expect(written.service.basePath).toBe("/blueprint/api");
Expand Down
14 changes: 3 additions & 11 deletions tests/commands/asset-registry/asset-registry-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { AssetRegistryMetadata } from "../../../src/commands/asset-registry/asse
import { mockAxiosGet } from "../../utls/http-requests-mock";
import { AssetRegistryService } from "../../../src/commands/asset-registry/asset-registry.service";
import { testContext } from "../../utls/test-context";
import { loggingTestTransport, mockWriteFileSync } from "../../jest.setup";
import { FileService } from "../../../src/core/utils/file-service";
import * as path from "path";
import { loggingTestTransport } from "../../jest.setup";
import { getJsonFromDownloadedFile } from "../../utls/fs-utils";

describe("Asset registry list", () => {
const metadata: AssetRegistryMetadata = {
Expand Down Expand Up @@ -85,14 +84,7 @@ describe("Asset registry list", () => {

await new AssetRegistryService(testContext).listTypes(true);

const expectedFileName = loggingTestTransport.logMessages[0].message.split(FileService.fileDownloadedMessage)[1];
expect(mockWriteFileSync).toHaveBeenCalledWith(
path.resolve(process.cwd(), expectedFileName),
expect.any(String),
{ encoding: "utf-8", mode: 0o600 }
);

const written = JSON.parse(mockWriteFileSync.mock.calls[0][1]) as AssetRegistryMetadata;
const written = getJsonFromDownloadedFile() as AssetRegistryMetadata;
expect(Object.keys(written.types).length).toBe(2);
expect(written.types["BOARD_V2"].assetType).toBe("BOARD_V2");
expect(written.types["SEMANTIC_MODEL"].assetType).toBe("SEMANTIC_MODEL");
Expand Down
14 changes: 3 additions & 11 deletions tests/commands/asset-registry/asset-registry-schema.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { mockAxiosGet } from "../../utls/http-requests-mock";
import { AssetRegistryService } from "../../../src/commands/asset-registry/asset-registry.service";
import { testContext } from "../../utls/test-context";
import { loggingTestTransport, mockWriteFileSync } from "../../jest.setup";
import { FileService } from "../../../src/core/utils/file-service";
import * as path from "path";
import { loggingTestTransport } from "../../jest.setup";
import { getJsonFromDownloadedFile } from "../../utls/fs-utils";

describe("Asset registry schema", () => {
const schemaResponse = {
Expand Down Expand Up @@ -33,14 +32,7 @@ describe("Asset registry schema", () => {

await new AssetRegistryService(testContext).getSchema("BOARD_V2", true);

const expectedFileName = loggingTestTransport.logMessages[0].message.split(FileService.fileDownloadedMessage)[1];
expect(mockWriteFileSync).toHaveBeenCalledWith(
path.resolve(process.cwd(), expectedFileName),
expect.any(String),
{ encoding: "utf-8", mode: 0o600 }
);

const written = JSON.parse(mockWriteFileSync.mock.calls[0][1]);
const written = getJsonFromDownloadedFile();
expect(written.$schema).toBe("http://json-schema.org/draft-07/schema#");
expect(written.title).toBe("Board");
});
Expand Down
Loading
Loading