|
1 | 1 | import { Verifier } from '@pact-foundation/pact'
|
2 | 2 | import path from 'path'
|
3 |
| -import { PipelineConfig } from '../../pipeline-config/model/pipelineConfig' |
| 3 | +import { HealthStatus, PipelineConfig, PipelineConfigDTO } from '../../pipeline-config/model/pipelineConfig' |
4 | 4 | import { port, server } from '../../index' // the main method is automatically called due to this import
|
| 5 | +import { PipelineTransformedData } from 'src/pipeline-config/model/pipelineTransformedData' |
5 | 6 |
|
6 | 7 | jest.mock('../../env', () => ({ }))
|
7 | 8 |
|
8 | 9 | const pipelineConfigs: PipelineConfig[] = []
|
| 10 | +let nextPipelineConfigId: number |
9 | 11 |
|
10 | 12 | jest.mock('../../pipeline-config/pipelineConfigManager', () => {
|
11 | 13 | return {
|
12 | 14 | PipelineConfigManager: jest.fn().mockImplementation(() => {
|
13 | 15 | return {
|
14 |
| - getAll: jest.fn().mockResolvedValue(pipelineConfigs) |
| 16 | + getAll: jest.fn().mockResolvedValue(pipelineConfigs), |
| 17 | + |
| 18 | + get: jest.fn().mockImplementation(async (id: number) => { |
| 19 | + const result = pipelineConfigs.find((config) => config.id === id) |
| 20 | + return Promise.resolve(result) |
| 21 | + }), |
| 22 | + |
| 23 | + getByDatasourceId: jest.fn().mockImplementation(async (datasourceId: number) => { |
| 24 | + const result = pipelineConfigs.filter((config) => config.datasourceId === datasourceId) |
| 25 | + return Promise.resolve(result) |
| 26 | + }), |
| 27 | + |
| 28 | + create: jest.fn().mockImplementation(async (config: PipelineConfigDTO) => { |
| 29 | + const result: PipelineConfig = { |
| 30 | + ...config, |
| 31 | + metadata: { |
| 32 | + ...config.metadata, |
| 33 | + creationTimestamp: new Date(2022, 1) |
| 34 | + }, |
| 35 | + id: ++nextPipelineConfigId |
| 36 | + } |
| 37 | + pipelineConfigs.push(result) |
| 38 | + return await Promise.resolve(result) |
| 39 | + }), |
| 40 | + |
| 41 | + update: jest.fn(async (id: number, config: PipelineConfigDTO) => { |
| 42 | + const configToUpdate = pipelineConfigs.find((config) => config.id === id) |
| 43 | + Object.assign(configToUpdate, config) |
| 44 | + }), |
| 45 | + |
| 46 | + delete: jest.fn(async (id: number) => { |
| 47 | + const indexOfConfigToDelete = pipelineConfigs.findIndex((config) => config.id === id) |
| 48 | + if (indexOfConfigToDelete !== -1) { |
| 49 | + pipelineConfigs.splice(indexOfConfigToDelete, 1) |
| 50 | + } |
| 51 | + }) |
| 52 | + } |
| 53 | + }) |
| 54 | + } |
| 55 | +}) |
| 56 | + |
| 57 | +const pipelineTransformedData: PipelineTransformedData[] = [] |
| 58 | + |
| 59 | +jest.mock('../../pipeline-config/pipelineTransformedDataManager', () => { |
| 60 | + return { |
| 61 | + PipelineTransformedDataManager: jest.fn().mockImplementation(() => { |
| 62 | + return { |
| 63 | + getLatest: jest.fn(async (id: number) => { |
| 64 | + const result = pipelineTransformedData.find((data) => data.id === id) |
| 65 | + return Promise.resolve(result) |
| 66 | + }) |
15 | 67 | }
|
16 | 68 | })
|
17 | 69 | }
|
@@ -42,14 +94,89 @@ describe('Pact Provider Verification', () => {
|
42 | 94 | pactUrls: [
|
43 | 95 | path.resolve(process.cwd(), '..', 'pacts', 'ui-pipeline.json')
|
44 | 96 | ],
|
| 97 | + logDir: path.resolve(process.cwd(), '..', 'pacts', 'logs'), |
45 | 98 | stateHandlers: {
|
46 |
| - 'no pipelines registered': async () => { |
47 |
| - pipelineConfigs.splice(0, pipelineConfigs.length) |
48 |
| - } |
| 99 | + 'any state': setupEmptyState, |
| 100 | + 'no pipelines exist': setupEmptyState, |
| 101 | + 'some pipelines exist': setupSomePipelineConfigs, |
| 102 | + 'pipeline with id 1 exists': setupSomePipelineConfigs, |
| 103 | + 'pipeline with id 1 does not exist': setupEmptyState, |
| 104 | + 'pipelines with datasource id 2 exist': setupSomePipelineConfigs, |
| 105 | + 'pipelines with datasource id 2 do not exist': setupEmptyState, |
| 106 | + 'transformed data with id 1 exists': setupSomePipelineTransformedData, |
| 107 | + 'transformed data with id 1 does not exist': setupEmptyState |
49 | 108 | }
|
50 | 109 | })
|
51 | 110 | await verifier.verifyProvider().finally(() => {
|
52 | 111 | server?.close()
|
53 | 112 | })
|
54 | 113 | })
|
55 | 114 | })
|
| 115 | + |
| 116 | +async function setupEmptyState (): Promise<void> { |
| 117 | + clearState() |
| 118 | +} |
| 119 | + |
| 120 | +async function setupSomePipelineConfigs (): Promise<void> { |
| 121 | + clearState() |
| 122 | + addSamplePipelineConfig(++nextPipelineConfigId, 2, true) |
| 123 | + addSamplePipelineConfig(++nextPipelineConfigId, 3, false) |
| 124 | + addSamplePipelineConfig(++nextPipelineConfigId, 2, false) |
| 125 | +} |
| 126 | + |
| 127 | +async function setupSomePipelineTransformedData (): Promise<void> { |
| 128 | + clearState() |
| 129 | + addSamplePipelineTransformedData(1) |
| 130 | + addSamplePipelineTransformedData(2) |
| 131 | + addSamplePipelineTransformedData(3) |
| 132 | +} |
| 133 | + |
| 134 | +function clearState (): void { |
| 135 | + nextPipelineConfigId = 0 |
| 136 | + clearPipelineConfigs() |
| 137 | + |
| 138 | + clearPipelineTransformedData() |
| 139 | +} |
| 140 | + |
| 141 | +function clearPipelineConfigs (): void { |
| 142 | + pipelineConfigs.splice(0, pipelineConfigs.length) |
| 143 | +} |
| 144 | + |
| 145 | +function addSamplePipelineConfig (id: number, datasourceId: number, withSchema: boolean): void { |
| 146 | + const pipelineConfig: PipelineConfig = { |
| 147 | + id: id, |
| 148 | + datasourceId: datasourceId, |
| 149 | + metadata: { |
| 150 | + author: 'some author', |
| 151 | + description: 'some description', |
| 152 | + displayName: 'some display name', |
| 153 | + license: 'some license', |
| 154 | + creationTimestamp: new Date(2021, 5) |
| 155 | + }, |
| 156 | + transformation: { |
| 157 | + func: 'some function' |
| 158 | + } |
| 159 | + } |
| 160 | + if (withSchema) { |
| 161 | + pipelineConfig.schema = { } |
| 162 | + } |
| 163 | + pipelineConfigs.push(pipelineConfig) |
| 164 | +} |
| 165 | + |
| 166 | +function clearPipelineTransformedData (): void { |
| 167 | + pipelineTransformedData.splice(0, pipelineTransformedData.length) |
| 168 | +} |
| 169 | + |
| 170 | +function addSamplePipelineTransformedData (id: number): void { |
| 171 | + const data: PipelineTransformedData = { |
| 172 | + id: id, |
| 173 | + pipelineId: 42, |
| 174 | + healthStatus: HealthStatus.OK, |
| 175 | + data: { }, |
| 176 | + |
| 177 | + /* TODO without this 'createdAt' field, the UI would not be able to fully interpret the response |
| 178 | + because it expects the 'timestamp' field to be present which is derived from 'createdAt' */ |
| 179 | + createdAt: 'some creation date' |
| 180 | + } |
| 181 | + pipelineTransformedData.push(data) |
| 182 | +} |
0 commit comments