|
| 1 | +import { NodeTransport } from "../../../src/commands/configuration-management/interfaces/node.interfaces"; |
| 2 | +import { mockAxiosGet } from "../../utls/http-requests-mock"; |
| 3 | +import { NodeService } from "../../../src/commands/configuration-management/node.service"; |
| 4 | +import { testContext } from "../../utls/test-context"; |
| 5 | +import { loggingTestTransport, mockWriteFileSync } from "../../jest.setup"; |
| 6 | +import { FileService } from "../../../src/core/utils/file-service"; |
| 7 | +import * as path from "path"; |
| 8 | + |
| 9 | +describe("Node find", () => { |
| 10 | + const node: NodeTransport = { |
| 11 | + id: "node-id", |
| 12 | + key: "node-key", |
| 13 | + name: "Node Name", |
| 14 | + packageNodeKey: "package-node-key", |
| 15 | + parentNodeKey: "parent-node-key", |
| 16 | + packageNodeId: "package-node-id", |
| 17 | + type: "VIEW", |
| 18 | + invalidContent: false, |
| 19 | + creationDate: new Date().toISOString(), |
| 20 | + changeDate: new Date().toISOString(), |
| 21 | + createdBy: "user-id", |
| 22 | + updatedBy: "user-id", |
| 23 | + schemaVersion: 1, |
| 24 | + flavor: "STUDIO", |
| 25 | + }; |
| 26 | + |
| 27 | + it("Should find node without configuration", async () => { |
| 28 | + const packageKey = "package-key"; |
| 29 | + const nodeKey = "node-key"; |
| 30 | + mockAxiosGet(`https://myTeam.celonis.cloud/pacman/api/core/staging/packages/${packageKey}/nodes/${nodeKey}?withConfiguration=false`, node); |
| 31 | + |
| 32 | + await new NodeService(testContext).findNode(packageKey, nodeKey, false, false); |
| 33 | + |
| 34 | + expect(loggingTestTransport.logMessages.length).toBe(11); |
| 35 | + expect(loggingTestTransport.logMessages[0].message).toContain(`ID: ${node.id}`); |
| 36 | + expect(loggingTestTransport.logMessages[1].message).toContain(`Key: ${node.key}`); |
| 37 | + expect(loggingTestTransport.logMessages[2].message).toContain(`Name: ${node.name}`); |
| 38 | + expect(loggingTestTransport.logMessages[3].message).toContain(`Type: ${node.type}`); |
| 39 | + expect(loggingTestTransport.logMessages[4].message).toContain(`Package Node Key: ${node.packageNodeKey}`); |
| 40 | + expect(loggingTestTransport.logMessages[5].message).toContain(`Parent Node Key: ${node.parentNodeKey}`); |
| 41 | + expect(loggingTestTransport.logMessages[6].message).toContain(`Created By: ${node.createdBy}`); |
| 42 | + expect(loggingTestTransport.logMessages[7].message).toContain(`Updated By: ${node.updatedBy}`); |
| 43 | + expect(loggingTestTransport.logMessages[8].message).toContain(`Creation Date: ${new Date(node.creationDate).toISOString()}`); |
| 44 | + expect(loggingTestTransport.logMessages[9].message).toContain(`Change Date: ${new Date(node.changeDate).toISOString()}`); |
| 45 | + expect(loggingTestTransport.logMessages[10].message).toContain(`Flavor: ${node.flavor}`); |
| 46 | + }); |
| 47 | + |
| 48 | + it("Should find node with configuration", async () => { |
| 49 | + const packageKey = "package-key"; |
| 50 | + const nodeKey = "node-key"; |
| 51 | + const nodeWithConfig: NodeTransport = { |
| 52 | + ...node, |
| 53 | + configuration: { |
| 54 | + someKey: "someValue", |
| 55 | + anotherKey: 123, |
| 56 | + }, |
| 57 | + }; |
| 58 | + |
| 59 | + mockAxiosGet(`https://myTeam.celonis.cloud/pacman/api/core/staging/packages/${packageKey}/nodes/${nodeKey}?withConfiguration=true`, nodeWithConfig); |
| 60 | + |
| 61 | + await new NodeService(testContext).findNode(packageKey, nodeKey, true, false); |
| 62 | + |
| 63 | + expect(loggingTestTransport.logMessages.length).toBe(12); |
| 64 | + expect(loggingTestTransport.logMessages[0].message).toContain(`ID: ${nodeWithConfig.id}`); |
| 65 | + expect(loggingTestTransport.logMessages[10].message).toContain(`Configuration: ${JSON.stringify(nodeWithConfig.configuration, null, 2)}`); |
| 66 | + expect(loggingTestTransport.logMessages[11].message).toContain(`Flavor: ${nodeWithConfig.flavor}`); |
| 67 | + }); |
| 68 | + |
| 69 | + it("Should find node without parent node key", async () => { |
| 70 | + const packageKey = "package-key"; |
| 71 | + const nodeKey = "node-key"; |
| 72 | + const nodeWithoutParent: NodeTransport = { |
| 73 | + ...node, |
| 74 | + parentNodeKey: undefined, |
| 75 | + }; |
| 76 | + |
| 77 | + mockAxiosGet(`https://myTeam.celonis.cloud/pacman/api/core/staging/packages/${packageKey}/nodes/${nodeKey}?withConfiguration=false`, nodeWithoutParent); |
| 78 | + |
| 79 | + await new NodeService(testContext).findNode(packageKey, nodeKey, false, false); |
| 80 | + |
| 81 | + expect(loggingTestTransport.logMessages.length).toBe(10); |
| 82 | + // Verify that parent node key is not logged |
| 83 | + const parentNodeKeyMessage = loggingTestTransport.logMessages.find(log => log.message.includes("Parent Node Key")); |
| 84 | + expect(parentNodeKeyMessage).toBeUndefined(); |
| 85 | + }); |
| 86 | + |
| 87 | + it("Should find node and return as JSON", async () => { |
| 88 | + const packageKey = "package-key"; |
| 89 | + const nodeKey = "node-key"; |
| 90 | + mockAxiosGet(`https://myTeam.celonis.cloud/pacman/api/core/staging/packages/${packageKey}/nodes/${nodeKey}?withConfiguration=false`, node); |
| 91 | + |
| 92 | + await new NodeService(testContext).findNode(packageKey, nodeKey, false, true); |
| 93 | + |
| 94 | + const expectedFileName = loggingTestTransport.logMessages[0].message.split(FileService.fileDownloadedMessage)[1]; |
| 95 | + |
| 96 | + expect(mockWriteFileSync).toHaveBeenCalledWith(path.resolve(process.cwd(), expectedFileName), expect.any(String), {encoding: "utf-8"}); |
| 97 | + |
| 98 | + const nodeTransport = JSON.parse(mockWriteFileSync.mock.calls[0][1]) as NodeTransport; |
| 99 | + |
| 100 | + expect(nodeTransport).toEqual(node); |
| 101 | + }); |
| 102 | + |
| 103 | + it("Should find node with configuration and return as JSON", async () => { |
| 104 | + const packageKey = "package-key"; |
| 105 | + const nodeKey = "node-key"; |
| 106 | + const nodeWithConfig: NodeTransport = { |
| 107 | + ...node, |
| 108 | + configuration: { |
| 109 | + someKey: "someValue", |
| 110 | + nested: { |
| 111 | + value: true, |
| 112 | + }, |
| 113 | + }, |
| 114 | + }; |
| 115 | + |
| 116 | + mockAxiosGet(`https://myTeam.celonis.cloud/pacman/api/core/staging/packages/${packageKey}/nodes/${nodeKey}?withConfiguration=true`, nodeWithConfig); |
| 117 | + |
| 118 | + await new NodeService(testContext).findNode(packageKey, nodeKey, true, true); |
| 119 | + |
| 120 | + const expectedFileName = loggingTestTransport.logMessages[0].message.split(FileService.fileDownloadedMessage)[1]; |
| 121 | + |
| 122 | + expect(mockWriteFileSync).toHaveBeenCalledWith(path.resolve(process.cwd(), expectedFileName), expect.any(String), {encoding: "utf-8"}); |
| 123 | + |
| 124 | + const nodeTransport = JSON.parse(mockWriteFileSync.mock.calls[0][1]) as NodeTransport; |
| 125 | + |
| 126 | + expect(nodeTransport).toEqual(nodeWithConfig); |
| 127 | + expect(nodeTransport.configuration).toEqual(nodeWithConfig.configuration); |
| 128 | + }); |
| 129 | + |
| 130 | + it("Should find node with invalid configuration", async () => { |
| 131 | + const packageKey = "package-key"; |
| 132 | + const nodeKey = "node-key"; |
| 133 | + const invalidConfigMessage = "Invalid JSON: Unexpected token at position 10"; |
| 134 | + const nodeWithInvalidConfig: NodeTransport = { |
| 135 | + ...node, |
| 136 | + invalidContent: true, |
| 137 | + invalidConfiguration: invalidConfigMessage, |
| 138 | + }; |
| 139 | + |
| 140 | + mockAxiosGet(`https://myTeam.celonis.cloud/pacman/api/core/staging/packages/${packageKey}/nodes/${nodeKey}?withConfiguration=false`, nodeWithInvalidConfig); |
| 141 | + |
| 142 | + await new NodeService(testContext).findNode(packageKey, nodeKey, false, false); |
| 143 | + |
| 144 | + expect(loggingTestTransport.logMessages.length).toBe(12); |
| 145 | + expect(loggingTestTransport.logMessages[0].message).toContain(`ID: ${nodeWithInvalidConfig.id}`); |
| 146 | + expect(loggingTestTransport.logMessages[10].message).toContain(`Invalid Configuration: ${invalidConfigMessage}`); |
| 147 | + expect(loggingTestTransport.logMessages[11].message).toContain(`Flavor: ${nodeWithInvalidConfig.flavor}`); |
| 148 | + }); |
| 149 | + |
| 150 | + it("Should find node with invalid configuration and return as JSON", async () => { |
| 151 | + const packageKey = "package-key"; |
| 152 | + const nodeKey = "node-key"; |
| 153 | + const invalidConfigMessage = "Syntax error in configuration"; |
| 154 | + const nodeWithInvalidConfig: NodeTransport = { |
| 155 | + ...node, |
| 156 | + invalidContent: true, |
| 157 | + invalidConfiguration: invalidConfigMessage, |
| 158 | + }; |
| 159 | + |
| 160 | + mockAxiosGet(`https://myTeam.celonis.cloud/pacman/api/core/staging/packages/${packageKey}/nodes/${nodeKey}?withConfiguration=false`, nodeWithInvalidConfig); |
| 161 | + |
| 162 | + await new NodeService(testContext).findNode(packageKey, nodeKey, false, true); |
| 163 | + |
| 164 | + const expectedFileName = loggingTestTransport.logMessages[0].message.split(FileService.fileDownloadedMessage)[1]; |
| 165 | + |
| 166 | + expect(mockWriteFileSync).toHaveBeenCalledWith(path.resolve(process.cwd(), expectedFileName), expect.any(String), {encoding: "utf-8"}); |
| 167 | + |
| 168 | + const nodeTransport = JSON.parse(mockWriteFileSync.mock.calls[0][1]) as NodeTransport; |
| 169 | + |
| 170 | + expect(nodeTransport).toEqual(nodeWithInvalidConfig); |
| 171 | + expect(nodeTransport.invalidConfiguration).toEqual(invalidConfigMessage); |
| 172 | + expect(nodeTransport.invalidContent).toBe(true); |
| 173 | + }); |
| 174 | +}); |
| 175 | + |
0 commit comments