-
Notifications
You must be signed in to change notification settings - Fork 15
/
executeStepWithDependencies.test.ts
238 lines (216 loc) · 6.95 KB
/
executeStepWithDependencies.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import {
createDirectRelationship,
Entity,
IntegrationInstanceConfig,
IntegrationInvocationConfig,
IntegrationStep,
Relationship,
RelationshipClass,
} from '@jupiterone/integration-sdk-core';
import { v4 as uuid } from 'uuid';
import { executeStepWithDependencies } from '../executeStepWithDependencies';
function getMockInvocationConfig(
config?: Partial<IntegrationInvocationConfig>,
): IntegrationInvocationConfig {
return {
integrationSteps: [],
...config,
};
}
function getMockIntegrationStep(
config?: Partial<IntegrationStep>,
): IntegrationStep {
return {
id: 'id',
name: 'name',
entities: [],
relationships: [],
executionHandler: () => undefined,
...config,
};
}
function getMockInstanceConfig(
config?: Partial<IntegrationInstanceConfig>,
): IntegrationInstanceConfig {
return {
...config,
};
}
function getMockEntity(e?: Partial<Entity>): Entity {
return {
_class: 'Record',
_type: 'entity-type',
_key: uuid(),
...e,
};
}
describe('executeStepWithDependencies', () => {
describe('invalid step', () => {
test('should fail if step `id` property does not exist in dependency graph', async () => {
await expect(
executeStepWithDependencies({
stepId: 'invalid-id',
invocationConfig: getMockInvocationConfig({
integrationSteps: [getMockIntegrationStep({ id: 'valid-id' })],
}),
instanceConfig: getMockInstanceConfig(),
}),
).rejects.toThrow('Node does not exist: invalid-id');
});
});
describe('invalid invocationConfig', () => {
test('should fail if `step.dependencyGraphId` property is present', async () => {
await expect(
executeStepWithDependencies({
stepId: 'step-1',
invocationConfig: getMockInvocationConfig({
integrationSteps: [
getMockIntegrationStep({
id: 'step-1',
dependencyGraphId: 'last',
}),
],
}),
instanceConfig: getMockInstanceConfig(),
}),
).rejects.toThrow(
'executeStepWithDependencies does not currently support steps with a dependencyGraphId',
);
});
test('should fail if invocationConfig contains circular dependency', async () => {
await expect(
executeStepWithDependencies({
stepId: 'step-1',
invocationConfig: getMockInvocationConfig({
integrationSteps: [
getMockIntegrationStep({ id: 'step-1', dependsOn: ['step-2'] }),
getMockIntegrationStep({ id: 'step-2', dependsOn: ['step-1'] }),
],
}),
instanceConfig: getMockInstanceConfig(),
}),
).rejects.toThrow('Dependency Cycle Found: step-1 -> step-2 -> step-1');
});
});
describe('success', () => {
test('should return entities, relationships, and data', async () => {
const e1 = getMockEntity();
const e2 = getMockEntity();
const r1 = createDirectRelationship({
from: e1,
_class: RelationshipClass.HAS,
to: e2,
});
const d1 = { key: 'value' };
const stepOne = getMockIntegrationStep({
id: 'step-1',
executionHandler: async ({ jobState }) => {
await jobState.addEntity(e1);
await jobState.addEntity(e2);
await jobState.addRelationship(r1);
await jobState.setData(Object.keys(d1)[0], Object.values(d1)[0]);
},
});
await expect(
executeStepWithDependencies({
stepId: stepOne.id,
invocationConfig: getMockInvocationConfig({
integrationSteps: [stepOne],
}),
instanceConfig: getMockInstanceConfig(),
}),
).resolves.toMatchObject({
collectedEntities: [e1, e2],
collectedRelationships: [r1],
collectedData: d1,
});
});
test('should not expose entities or relationships from previous steps', async () => {
const entityType = 'entity-type';
const e1 = getMockEntity({ _type: entityType });
const e2 = getMockEntity({ _type: entityType });
const relationshipType = 'relationship-type';
const r1 = createDirectRelationship({
from: e1,
_class: RelationshipClass.HAS,
to: e2,
properties: { _type: relationshipType },
});
const stepOne = getMockIntegrationStep({
id: 'step-1',
executionHandler: async ({ jobState }) => {
await jobState.addEntity(e1);
await jobState.addEntity(e2);
await jobState.addRelationship(r1);
},
});
let stepTwoExecutionHandlerDidExecute = false;
const stepTwo = getMockIntegrationStep({
id: 'step-2',
dependsOn: ['step-1'],
executionHandler: async ({ jobState }) => {
const entities: Entity[] = [];
await jobState.iterateEntities({ _type: entityType }, (e) => {
entities.push(e);
});
expect(entities).toMatchObject([e1, e2]);
const relationships: Relationship[] = [];
await jobState.iterateRelationships(
{ _type: relationshipType },
(r) => {
relationships.push(r);
},
);
expect(relationships).toMatchObject([r1]);
stepTwoExecutionHandlerDidExecute = true;
},
});
await expect(
executeStepWithDependencies({
stepId: stepTwo.id,
invocationConfig: getMockInvocationConfig({
integrationSteps: [stepOne, stepTwo],
}),
instanceConfig: getMockInstanceConfig(),
}),
).resolves.toMatchObject({
collectedEntities: [],
collectedRelationships: [],
});
expect(stepTwoExecutionHandlerDidExecute).toBe(true);
});
test('should allow step to access data from previous step', async () => {
const setDataKey = 'set-data-key';
const setDataValue = 'set-data-value';
const stepOne = getMockIntegrationStep({
id: 'step-1',
executionHandler: async ({ jobState }) => {
await jobState.setData(setDataKey, setDataValue);
},
});
let stepTwoExecutionHandlerDidExecute = false;
const stepTwo = getMockIntegrationStep({
id: 'step-2',
dependsOn: ['step-1'],
executionHandler: async ({ jobState }) => {
const getDataValue = await jobState.getData(setDataKey);
expect(getDataValue).toBe(setDataValue);
stepTwoExecutionHandlerDidExecute = true;
},
});
await expect(
executeStepWithDependencies({
stepId: stepTwo.id,
invocationConfig: getMockInvocationConfig({
integrationSteps: [stepOne, stepTwo],
}),
instanceConfig: getMockInstanceConfig(),
}),
).resolves.toMatchObject({
collectedEntities: [],
collectedRelationships: [],
});
expect(stepTwoExecutionHandlerDidExecute).toBe(true);
});
});
});