Skip to content

Commit

Permalink
[Data Mapper] Fixes for namespaces in yml (#957)
Browse files Browse the repository at this point in the history
* Fix for namespaces never generating

* Fix for tests

* minor update to for values
  • Loading branch information
refortie authored Sep 26, 2022
1 parent 2b4c3b0 commit 26797f9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
14 changes: 6 additions & 8 deletions libs/data-mapper/src/lib/__test__/MapDefinition.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ describe('Map definition conversions', () => {
describe('convertToMapDefinition', () => {
const schema: Schema = simpleMockSchema;
const extendedSchema = convertSchemaToSchemaExtended(schema);
const headerString =
'$version: 1.0\n$input: XML\n$output: XML\n$sourceSchema: CBRSourceSchema.xsd\n$targetSchema: CBRSourceSchema.xsd\n$sourceNamespaces:\n ns0: http://CBR.CBRSourceSchema\n xs: http://www.w3.org/2001/XMLSchema\n$targetNamespaces:\n ns0: http://CBR.CBRSourceSchema\n xs: http://www.w3.org/2001/XMLSchema\n';

it('Test no connections', () => {
const connections: ConnectionDictionary = {};

const mapDefinition = convertToMapDefinition(connections, extendedSchema, extendedSchema);
expect(mapDefinition).toEqual(
'$version: 1.0\n$input: XML\n$output: XML\n$sourceSchema: CBRSourceSchema.xsd\n$targetSchema: CBRSourceSchema.xsd\n'
);
expect(mapDefinition).toEqual(headerString);
});

it('Test 1 connection', () => {
Expand All @@ -36,9 +36,7 @@ describe('Map definition conversions', () => {
};

const mapDefinition = convertToMapDefinition(connections, extendedSchema, extendedSchema);
expect(mapDefinition).toEqual(
'$version: 1.0\n$input: XML\n$output: XML\n$sourceSchema: CBRSourceSchema.xsd\n$targetSchema: CBRSourceSchema.xsd\nns0:CBRInputRecord:\n Identity:\n UserID: /ns0:CBRInputRecord/Identity/UserID'
);
expect(mapDefinition).toEqual(`${headerString}ns0:CBRInputRecord:\n Identity:\n UserID: /ns0:CBRInputRecord/Identity/UserID`);
});

it('Test deep connection', () => {
Expand All @@ -53,7 +51,7 @@ describe('Map definition conversions', () => {

const mapDefinition = convertToMapDefinition(connections, extendedSchema, extendedSchema);
expect(mapDefinition).toEqual(
'$version: 1.0\n$input: XML\n$output: XML\n$sourceSchema: CBRSourceSchema.xsd\n$targetSchema: CBRSourceSchema.xsd\nns0:CBRInputRecord:\n Identity:\n Name:\n FirstName: /ns0:CBRInputRecord/Identity/Name/FirstName'
`${headerString}ns0:CBRInputRecord:\n Identity:\n Name:\n FirstName: /ns0:CBRInputRecord/Identity/Name/FirstName`
);
});

Expand All @@ -75,7 +73,7 @@ describe('Map definition conversions', () => {

const mapDefinition = convertToMapDefinition(connections, extendedSchema, extendedSchema);
expect(mapDefinition).toEqual(
'$version: 1.0\n$input: XML\n$output: XML\n$sourceSchema: CBRSourceSchema.xsd\n$targetSchema: CBRSourceSchema.xsd\nns0:CBRInputRecord:\n Identity:\n Name:\n LastName: /ns0:CBRInputRecord/Identity/Name/LastName\n FirstName: /ns0:CBRInputRecord/Identity/Name/FirstName'
`${headerString}ns0:CBRInputRecord:\n Identity:\n Name:\n LastName: /ns0:CBRInputRecord/Identity/Name/LastName\n FirstName: /ns0:CBRInputRecord/Identity/Name/FirstName`
);
});

Expand Down
3 changes: 2 additions & 1 deletion libs/data-mapper/src/lib/models/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export interface Schema {
name: string;
type: SchemaType;
targetNamespace: string;
namespaces?: Map<string, string>;
namespaces?: NamespaceDictionary;
schemaTreeRoot: SchemaNode;
}

Expand Down Expand Up @@ -101,3 +101,4 @@ export enum SchemaTypes {
}

export type SchemaNodeDictionary = { [key: string]: SchemaNodeExtended };
export type NamespaceDictionary = { [key: string]: string };
14 changes: 7 additions & 7 deletions libs/data-mapper/src/lib/utils/DataMap.Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { InvalidFormatException, InvalidFormatExceptionCode } from '../exceptions/MapDefinitionExceptions';
import type { ConnectionDictionary, LoopConnection } from '../models/Connection';
import type { DataMap, MapNode } from '../models/DataMap';
import type { SchemaExtended, SchemaNodeExtended } from '../models/Schema';
import type { NamespaceDictionary, SchemaExtended, SchemaNodeExtended } from '../models/Schema';
import { inputPrefix, outputPrefix } from '../utils/ReactFlow.Util';
import yaml from 'js-yaml';

Expand All @@ -36,26 +36,26 @@ const generateMapDefinitionHeader = (sourceSchema: SchemaExtended, targetSchema:
let mapDefinitionHeader = `${reservedMapDefinitionKeys.version}: ${mapDefinitionVersion}${yamlFormats.newLine}`;
mapDefinitionHeader += `${reservedMapDefinitionKeys.sourceFormat}: ${sourceSchema.type}${yamlFormats.newLine}`;
mapDefinitionHeader += `${reservedMapDefinitionKeys.targetFormat}: ${targetSchema.type}${yamlFormats.newLine}`;
mapDefinitionHeader += `${reservedMapDefinitionKeys.sourceSchemaName}: ${targetSchema.name}${yamlFormats.newLine}`;
mapDefinitionHeader += `${reservedMapDefinitionKeys.sourceSchemaName}: ${sourceSchema.name}${yamlFormats.newLine}`;
mapDefinitionHeader += `${reservedMapDefinitionKeys.targetSchemaName}: ${targetSchema.name}${yamlFormats.newLine}`;

if (sourceSchema.namespaces && sourceSchema.namespaces.size > 0) {
if (sourceSchema.namespaces && Object.keys(sourceSchema.namespaces).length > 0) {
mapDefinitionHeader += `${reservedMapDefinitionKeys.sourceNamespaces}:${yamlFormats.newLine}`;
mapDefinitionHeader += generateNamespaceEntries(sourceSchema.namespaces);
}

if (targetSchema.namespaces && targetSchema.namespaces.size > 0) {
if (targetSchema.namespaces && Object.keys(targetSchema.namespaces).length > 0) {
mapDefinitionHeader += `${reservedMapDefinitionKeys.targetNamespaces}:${yamlFormats.newLine}`;
mapDefinitionHeader += generateNamespaceEntries(targetSchema.namespaces);
}

return mapDefinitionHeader;
};

const generateNamespaceEntries = (namespaces: Map<string, string>): string => {
const generateNamespaceEntries = (namespaces: NamespaceDictionary): string => {
let results = '';
namespaces.forEach((value, key) => {
results += `${key}: ${value}${yamlFormats.newLine}`;
Object.entries(namespaces).forEach(([key, value]) => {
results += `${yamlFormats.indentGap}${key}: ${value}${yamlFormats.newLine}`;
});

return results;
Expand Down

0 comments on commit 26797f9

Please sign in to comment.