Skip to content

Commit 0ace686

Browse files
committed
update tests
1 parent ee5d938 commit 0ace686

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { ApiCommunication, Topic } from './parsedApiConfigurationTypes';
2+
3+
export function normalizeCommunication(input: Partial<ApiCommunication> & { [key: string]: any }): ApiCommunication {
4+
return {
5+
attributes: {
6+
componentCode: input.componentCode ?? input.attributes?.componentCode ?? '',
7+
stateMachineCode: input.stateMachineCode ?? input.attributes?.stateMachineCode,
8+
eventType: input.eventType ?? input.attributes?.eventType,
9+
event: input.event ?? input.attributes?.event,
10+
eventCode: input.eventCode ?? input.attributes?.eventCode,
11+
},
12+
topic: normalizeTopic(input.topic)
13+
};
14+
}
15+
16+
function normalizeTopic(topic: Topic[] | Topic | undefined): [Topic] {
17+
if (!topic) {
18+
return [{ value: '' }];
19+
}
20+
if (Array.isArray(topic)) {
21+
return topic.map((t) => ({ value: t.value ?? '' })) as [Topic];
22+
}
23+
return [{ value: topic.value ?? '' }];
24+
}

src/configuration/apiConfiguration.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
StateMachine,
77
} from './parsedApiConfigurationTypes';
88

9+
import { normalizeCommunication } from './apiCommunicationUtils';
10+
911
export interface PublisherDetails {
1012
eventCode: number;
1113
routingKey: string;
@@ -125,7 +127,7 @@ export class DefaultApiConfiguration implements ApiConfiguration {
125127
containsStateMachine(componentName: string, stateMachineName: string): boolean {
126128
const result = this.findComponent(component => component.name === componentName);
127129
if (result) {
128-
return result.stateMachines[0].stateMachine.find(stm => stm.attributes.name === stateMachineName) != null;
130+
return result.stateMachines.stateMachine.find(stm => stm.name === stateMachineName) != null;
129131
}
130132
return false;
131133
}
@@ -158,7 +160,11 @@ export class DefaultApiConfiguration implements ApiConfiguration {
158160
stateMachineCode: number,
159161
messageType: string
160162
): ApiCommunication | undefined {
161-
return this._config.deployment.clientAPICommunication[0].publish.find(
163+
const raw = this._config.deployment.clientAPICommunication.publish;
164+
const publishArrayRaw = Array.isArray(raw) ? raw : [raw];
165+
const publishArray = publishArrayRaw.map(normalizeCommunication);
166+
167+
return publishArray.find(
162168
pub =>
163169
Number(pub.attributes.componentCode) === componentCode &&
164170
Number(pub.attributes.stateMachineCode) === stateMachineCode &&
@@ -183,18 +189,24 @@ export class DefaultApiConfiguration implements ApiConfiguration {
183189
stateMachineCode: number,
184190
type: SubscriberEventType
185191
): ApiCommunication | undefined {
186-
return this._config.deployment.clientAPICommunication[0].subscribe.find(
187-
pub =>
188-
Number(pub.attributes.componentCode) === componentCode &&
189-
Number(pub.attributes.stateMachineCode) === stateMachineCode &&
190-
pub.attributes.eventType === SubscriberEventType[type].toUpperCase()
192+
const raw = this._config.deployment.clientAPICommunication.subscribe;
193+
const subscribeArrayRaw = Array.isArray(raw) ? raw : [raw];
194+
const subscribeArray = subscribeArrayRaw.map(normalizeCommunication);
195+
196+
return subscribeArray.find(
197+
sub =>
198+
Number(sub.attributes.componentCode) === componentCode &&
199+
Number(sub.attributes.stateMachineCode) === stateMachineCode &&
200+
sub.attributes.eventType === SubscriberEventType[type].toUpperCase()
191201
);
192202
}
193203

194204
getSnapshotTopic(componentCode: number): string {
195-
const snapshot = this._config.deployment.clientAPICommunication[0].snapshot.find(
196-
pub => Number(pub.attributes.componentCode) === componentCode
197-
);
205+
const raw = this._config.deployment.clientAPICommunication.snapshot;
206+
const snapshotArrayRaw = Array.isArray(raw) ? raw : [raw];
207+
const snapshotArray = snapshotArrayRaw.map(normalizeCommunication);
208+
209+
const snapshot = snapshotArray.find(pub => Number(pub.attributes.componentCode) === componentCode);
198210

199211
if (!snapshot) {
200212
throw new Error(`Snapshot topic not found - component code: ${componentCode}`);

0 commit comments

Comments
 (0)