Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/commands/t2tc/t2tc-package.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,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
2 changes: 1 addition & 1 deletion src/core/utils/file-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class FileService {
});
}

public async readFileToJson<T>(fileName: string): Promise<T> {
public readFileToJson<T>(fileName: string): Promise<T> {
Comment thread
ksalihu marked this conversation as resolved.
Outdated
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());
});
});
26 changes: 5 additions & 21 deletions tests/commands/action-flows/export-action-flows.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
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 { loggingTestTransport } from "../../jest.setup";
import { FileService } from "../../../src/core/utils/file-service";
import { mockExistsSyncOnce, mockReadFileSync } from "../../utls/fs-mock-utils";
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 +68,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 +77,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 +121,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
12 changes: 5 additions & 7 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 } from "../../utls/fs-utils";

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

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

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

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());
zip.writeZip("tmp.zip");

await new ActionFlowCommandService(testContext).importActionFlows(packageId, "tmp", 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { AgentSkillsResponse } from "../../../src/commands/asset-registry/asset-
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 skills list", () => {
const skillsResponse: AgentSkillsResponse = {
Expand Down Expand Up @@ -46,14 +45,7 @@ describe("Asset registry skills list", () => {

await new AssetRegistryService(testContext).listSkills(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 AgentSkillsResponse;
const written = getJsonFromDownloadedFile() as AgentSkillsResponse;
expect(written.skills.length).toBe(2);
expect(written.skills[0].name).toBe("skill-one");
expect(written.skills[1].path).toBe("asset/BOARD_V2/board-authoring");
Expand Down
27 changes: 6 additions & 21 deletions tests/commands/asset-registry/asset-registry-validate.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import { mockAxiosPost, mockedPostRequestBodyByUrl } 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 * as fs from "fs";

jest.mock("fs", () => ({
...jest.requireActual("fs"),
readFileSync: jest.fn(),
}));
import { loggingTestTransport } from "../../jest.setup";
import { getJsonFromDownloadedFile, writeJsonTempFile, writeTempFile } from "../../utls/fs-utils";

describe("Asset registry validate", () => {
const validateResponse = {
Expand Down Expand Up @@ -53,14 +46,7 @@ describe("Asset registry validate", () => {
json: true,
});

const msg = loggingTestTransport.logMessages[0].message;
const expectedFileName = msg.split(FileService.fileDownloadedMessage)[1];
expect(mockWriteFileSync).toHaveBeenCalledWith(
path.resolve(process.cwd(), expectedFileName),
expect.any(String),
expect.objectContaining({ encoding: "utf-8" })
);
const written = JSON.parse(mockWriteFileSync.mock.calls[0][1]);
const written = getJsonFromDownloadedFile();
expect(written.valid).toBe(false);
});

Expand Down Expand Up @@ -144,7 +130,7 @@ describe("Asset registry validate", () => {
};

it("Should validate with -f file and print result", async () => {
(fs.readFileSync as jest.Mock).mockReturnValue(JSON.stringify(fullRequest));
writeJsonTempFile("request.json", fullRequest);
mockAxiosPost(mockUrl, validateResponse);

await new AssetRegistryService(testContext).validate({
Expand All @@ -153,12 +139,11 @@ describe("Asset registry validate", () => {
json: false,
});

expect(fs.readFileSync).toHaveBeenCalledWith("request.json", "utf-8");
expect(loggingTestTransport.logMessages[0].message).toContain("INVALID_ENUM_VALUE");
});

it("Should send the file body as-is (no envelope wrapping)", async () => {
(fs.readFileSync as jest.Mock).mockReturnValue(JSON.stringify(fullRequest));
writeJsonTempFile("request.json", fullRequest);
mockAxiosPost(mockUrl, validateResponse);

await new AssetRegistryService(testContext).validate({
Expand All @@ -173,7 +158,7 @@ describe("Asset registry validate", () => {
});

it("Should throw when -f file contains invalid JSON", async () => {
(fs.readFileSync as jest.Mock).mockReturnValue("not-json{");
writeTempFile("bad.json", "not-json{");

await expect(
new AssetRegistryService(testContext).validate({
Expand Down
Loading
Loading