Skip to content

Commit e2cc1f8

Browse files
author
Gerald Baulig
committed
fix(test): fix test configs
1 parent c2b378d commit e2cc1f8

File tree

6 files changed

+180
-86
lines changed

6 files changed

+180
-86
lines changed

cfg/config.json

+87-23
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"unit_codes",
2727
"templates",
2828
"settings",
29-
"currencys"
29+
"currencies"
3030
],
3131
"customQueries": [
3232
{
@@ -309,28 +309,92 @@
309309
"resourcesServiceNamePrefix": "io.restorecommerce.",
310310
"resourcesServiceConfigPrefix": "restore-",
311311
"resourcesDeletedMessage": "io.restorecommerce.resourcebase.Deleted",
312-
"resources": {
313-
"unit_code": "unit_codes",
314-
"command": "commands",
315-
"customer": "customers",
316-
"address": "addresss",
317-
"contact_point_type": "contact_point_types",
318-
"country": "countrys",
319-
"contact_point": "contact_points",
320-
"credential": "credentials",
321-
"locale": "locales",
322-
"location": "locations",
323-
"notification": "notifications",
324-
"notification_channel": "notification_channels",
325-
"organization": "organizations",
326-
"shop": "shops",
327-
"tax": "taxs",
328-
"tax_type": "tax_types",
329-
"timezone": "timezones",
330-
"template": "templates",
331-
"setting": "settings",
332-
"currency": "currencys"
333-
}
312+
"resources": [
313+
{
314+
"resourceName": "address",
315+
"collectionName": "addresss"
316+
},
317+
{
318+
"resourceName": "command",
319+
"collectionName": "commands"
320+
},
321+
{
322+
"resourceName": "contact_point",
323+
"collectionName": "contact_points"
324+
},
325+
{
326+
"resourceName": "contact_point_type",
327+
"collectionName": "contact_point_types"
328+
},
329+
{
330+
"resourceName": "country",
331+
"collectionName": "countrys"
332+
},
333+
{
334+
"resourceName": "credential",
335+
"collectionName": "credentials"
336+
},
337+
{
338+
"resourceName": "currency",
339+
"collectionName": "currencies"
340+
},
341+
{
342+
"resourceName": "customer",
343+
"collectionName": "customers"
344+
},
345+
{
346+
"resourceName": "local",
347+
"collectionName": "locals"
348+
},
349+
{
350+
"resourceName": "location",
351+
"collectionName": "locations"
352+
},
353+
{
354+
"resourceName": "notification",
355+
"collectionName": "notifications"
356+
},
357+
{
358+
"resourceName": "notification_channel",
359+
"collectionName": "notification_channels"
360+
},
361+
{
362+
"resourceName": "organization",
363+
"collectionName": "organizations"
364+
},
365+
{
366+
"resourceName": "shop",
367+
"collectionName": "shops"
368+
},
369+
{
370+
"resourceName": "tax",
371+
"collectionName": "taxs"
372+
},
373+
{
374+
"resourceName": "tax_type",
375+
"collectionName": "tax_types"
376+
},
377+
{
378+
"resourceName": "tax_type",
379+
"collectionName": "tax_types"
380+
},
381+
{
382+
"resourceName": "template",
383+
"collectionName": "templates"
384+
},
385+
{
386+
"resourceName": "timezone",
387+
"collectionName": "timezones"
388+
},
389+
{
390+
"resourceName": "setting",
391+
"collectionName": "settings"
392+
},
393+
{
394+
"resourceName": "unit_code",
395+
"collectionName": "unit_codes"
396+
}
397+
]
334398
}
335399
},
336400
"command-interface": {

cfg/config_test.json

+16-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"logger": {
33
"console": {
44
"handleExceptions": false,
5-
"level": "crit",
5+
"level": "error",
66
"colorize": true,
77
"prettyPrint": true
88
}
@@ -15,9 +15,9 @@
1515
"database": "restore-resource-srv-test",
1616
"autoCreate": true,
1717
"collections": [
18-
"command",
19-
"organization",
20-
"contact_point"
18+
"commands",
19+
"organizations",
20+
"contact_points"
2121
],
2222
"customQueries": [
2323
{
@@ -47,9 +47,18 @@
4747
"resourcesServiceNamePrefix": "io.restorecommerce.",
4848
"resourcesServiceConfigPrefix": "restore-",
4949
"resources": [
50-
"command",
51-
"organization",
52-
"contact_point"
50+
{
51+
"resourceName": "command",
52+
"collectionName": "commands"
53+
},
54+
{
55+
"resourceName": "contact_point",
56+
"collectionName": "contact_points"
57+
},
58+
{
59+
"resourceName": "organization",
60+
"collectionName": "organizations"
61+
}
5362
]
5463
}
5564
},

src/service.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as _ from 'lodash-es';
2-
import { RawServiceImplementation } from 'nice-grpc';
32
import { Logger } from '@restorecommerce/logger';
43
import { ResourcesAPIBase, ServiceBase } from '@restorecommerce/resource-base-interface';
54
import { Operation, PolicySetRQResponse, ResolvedSubject } from '@restorecommerce/acs-client';

src/worker.ts

+22-18
Original file line numberDiff line numberDiff line change
@@ -204,33 +204,33 @@ export class Worker {
204204
if (!resources) {
205205
throw new Error('config field resources does not exist');
206206
}
207-
207+
208208
// Generate a config for each resource
209209
const kafkaCfg = cfg.get('events:kafka');
210210
// const grpcConfig = cfg.get('server:transports:0');
211211
const eventTypes = ['Created', 'Read', 'Modified', 'Deleted'];
212-
for (let resourceCfg of Object.values<any>(resources)) {
212+
for (const resourceCfg of Object.values<any>(resources)) {
213213
const resourcesDeletedMessage = resourceCfg.resourcesDeletedMessage;
214214
const resourcesServiceNamePrefix = resourceCfg.resourcesServiceNamePrefix;
215-
for (let [resource, collection] of Object.entries<string>(resourceCfg.resources)) {
216-
const resourceObjectName = resource.split('_').map(
215+
for (const { resourceName, collectionName } of resourceCfg.resources) {
216+
const resourceObjectName = resourceName.split('_').map(
217217
(name: string) => name.charAt(0).toUpperCase() + name.slice(1)
218218
).join('');
219219

220220
for (let event of eventTypes) {
221221
if (event?.toLocaleLowerCase() === 'deleted') {
222-
kafkaCfg[`${resource}${event}`] = {
222+
kafkaCfg[`${resourceName}${event}`] = {
223223
messageObject: resourcesDeletedMessage
224224
};
225225
}
226226
else {
227-
kafkaCfg[`${resource}${event}`] = {
228-
messageObject: `${resourcesServiceNamePrefix}${resource}.${resourceObjectName}`
227+
kafkaCfg[`${resourceName}${event}`] = {
228+
messageObject: `${resourcesServiceNamePrefix}${resourceName}.${resourceObjectName}`
229229
};
230230
}
231231

232-
const topicName = `${resourcesServiceNamePrefix}${collection}.resource`;
233-
const topicLabel = `${resource}.resource`;
232+
const topicName = `${resourcesServiceNamePrefix}${collectionName}.resource`;
233+
const topicLabel = `${resourceName}.resource`;
234234
kafkaCfg.topics[topicLabel] = {
235235
topic: topicName,
236236
};
@@ -279,33 +279,37 @@ export class Worker {
279279
const redisClientSubject: RedisClientType = createClient(redisConfig);
280280
await redisClientSubject.on('error', (err) => logger.error('Redis Client Error', err));
281281
await redisClientSubject.connect();
282-
for (let resourceCfg of Object.values<any>(resources)) {
282+
for (const resourceCfg of Object.values<any>(resources)) {
283283
const resourcesServiceConfigPrefix = resourceCfg.resourcesServiceConfigPrefix;
284284
const resourcesServiceNamePrefix = resourceCfg.resourcesServiceNamePrefix;
285+
const collectionNames = cfg.get('database:arango:collections') as string[];
285286

286-
for (let [resourceName, collectionName] of Object.entries<string>(resourceCfg.resources)) {
287-
let resourceFieldConfig: any = {};
287+
for (const { resourceName, collectionName } of resourceCfg.resources) {
288+
if (!collectionNames?.includes(collectionName)) {
289+
logger.warn('No collection initialized for resourse', { resourceName, collectionName });
290+
}
291+
const resourceFieldConfig: any = {};
288292
if (fieldGeneratorConfig && (resourceName in fieldGeneratorConfig)) {
289-
resourceFieldConfig['strategies'] = fieldGeneratorConfig[resourceName];
293+
resourceFieldConfig.strategies = fieldGeneratorConfig[resourceName];
290294
logger.info('Setting up field generators on Redis...');
291-
resourceFieldConfig['redisClient'] = redisClient;
295+
resourceFieldConfig.redisClient = redisClient;
292296
}
293297
// bufferFields handler
294298
if (bufferHandlerConfig && (collectionName in bufferHandlerConfig)) {
295-
resourceFieldConfig['bufferFields'] = bufferHandlerConfig[collectionName];
299+
resourceFieldConfig.bufferFields = bufferHandlerConfig[collectionName];
296300
}
297301
// dateTimeStampFields handler
298302
if (cfg.get('fieldHandlers:timeStampFields')) {
299-
resourceFieldConfig['timeStampFields'] = [];
303+
resourceFieldConfig.timeStampFields = [];
300304
for (let timeStampFiledConfig of cfg.get('fieldHandlers:timeStampFields')) {
301305
if (timeStampFiledConfig.entities.includes(collectionName)) {
302-
resourceFieldConfig['timeStampFields'].push(...timeStampFiledConfig.fields);
306+
resourceFieldConfig.timeStampFields.push(...timeStampFiledConfig.fields);
303307
}
304308
}
305309
}
306310
// requiredFields handler
307311
if (requiredFieldsConfig && (collectionName in requiredFieldsConfig)) {
308-
resourceFieldConfig['requiredFields'] = requiredFieldsConfig;
312+
resourceFieldConfig.requiredFields = requiredFieldsConfig;
309313
}
310314
logger.info(`Setting up ${resourceName} resource service`);
311315

test/resource_srv.spec.ts

+25-19
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,22 @@ const listOfOrganizations = [
6666

6767
// get client connection object
6868
async function getClientResourceServices() {
69-
const options: any = { microservice: {} };
70-
options.microservice = {
71-
service: {},
72-
mapClients: new Map()
69+
const options: any = {
70+
microservice: {
71+
service: {},
72+
mapClients: new Map()
73+
}
7374
};
7475
const resources = cfg.get('resources');
7576
const clientConfig = cfg.get('client');
76-
for (let resource in resources) {
77-
const resourceCfg = resources[resource];
78-
const resourceNames = resourceCfg.resources;
77+
for (const resourceCfg of Object.values<any>(resources)) {
7978
// const protosPrefix = resourceCfg.resourcesProtoPathPrefix;
8079
const servicePrefix = resourceCfg.resourcesServiceNamePrefix;
8180

82-
logger.silly('microservice clients', resourceNames);
81+
logger.silly('microservice clients', resourceCfg.resources);
8382

84-
for (let resource of resourceNames) {
85-
if (resource === 'command') {
83+
for (const { resourceName, collectionName } of resourceCfg.resources) {
84+
if (resourceName === 'command') {
8685
// if resource is command create a commandInterface client
8786
const serviceName = 'io.restorecommerce.commandinterface.Service';
8887
const cisConfig = cfg.get('client:commandinterface');
@@ -93,20 +92,27 @@ async function getClientResourceServices() {
9392
);
9493
// const client = new GrpcClient(cfg.get('client:commandinterface'), logger);
9594
options.microservice.service[serviceName] = client;
96-
options.microservice.mapClients.set(resource, serviceName);
95+
options.microservice.mapClients.set(resourceName, serviceName);
9796
continue;
9897
}
99-
const serviceName = `${servicePrefix}${resource}.Service`;
98+
const serviceName = `${servicePrefix}${resourceName}-srv`;
10099
const defaultConfig = clientConfig['default-resource-srv'];
101100
try {
102-
let serviceDefinition = ServiceDefinitionList.filter((obj) => obj.fullName.split('.')[2] === resource)[0];
103-
const client = createClient({ ...defaultConfig, logger }, serviceDefinition, createChannel(defaultConfig.address));
101+
let serviceDefinition = ServiceDefinitionList.filter((obj) => obj.fullName.split('.')[2] === resourceName)[0];
102+
const client = createClient(
103+
{ ...defaultConfig, logger },
104+
serviceDefinition,
105+
createChannel(defaultConfig.address)
106+
);
104107
options.microservice.service[serviceName] = client;
105-
options.microservice.mapClients.set(resource, serviceName);
108+
options.microservice.mapClients.set(resourceName, serviceName);
106109
logger.verbose('connected to microservice', serviceName);
107110
} catch (err) {
108-
logger.error('microservice connecting to service',
109-
serviceName, err);
111+
logger.error(
112+
'microservice connecting to service',
113+
serviceName,
114+
err
115+
);
110116
}
111117
}
112118
}
@@ -142,10 +148,10 @@ describe('resource-srv testing', () => {
142148
// List of serviceMappedValues
143149
const serviceMapping = await getClientResourceServices();
144150
// get the Organization service
145-
let orgMapValue = serviceMapping.microservice.mapClients.get('organization');
151+
const orgMapValue = serviceMapping.microservice.mapClients.get('organization');
146152
organizationService = serviceMapping.microservice.service[orgMapValue];
147153
// get contact_point service
148-
let contacPointMapValue = serviceMapping.microservice.mapClients.get('contact_point');
154+
const contacPointMapValue = serviceMapping.microservice.mapClients.get('contact_point');
149155
contactPointsService = serviceMapping.microservice.service[contacPointMapValue];
150156

151157
// create events for restoring

0 commit comments

Comments
 (0)