"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const internal_1 = require("../../../internal");
const Persistence_1 = require("../../../Persistence");
const testHelper_1 = require("../../testHelper");
describe('Persistence.ModelJson.ModelJson', () => {
    /**
     * Test path between TestDataPath and TestName.
     */
    const testsSubpath = 'Persistence/ModelJson/DataPartition';
    it('TestModelJsonDataPartitionLocationConsistency', () => __awaiter(void 0, void 0, void 0, function* () {
        const cdmCorpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, 'TestModelJsonDataPartitionLocationConsistency');
        const manifestRead = yield cdmCorpus.fetchObjectAsync('default.manifest.cdm.json', cdmCorpus.storage.fetchRootFolder('local'));
        expect(manifestRead.entities.allItems[0].dataPartitions.allItems[0].location)
            .toEqual('EpisodeOfCare/partition-data.csv');
        const convertedToModelJson = yield Persistence_1.ModelJson.ManifestPersistence.toData(manifestRead, undefined, undefined);
        const location = convertedToModelJson.entities[0]['partitions'][0].location;
        expect(location.indexOf('\\Persistence\\ModelJson\\DataPartition\\TestModelJsonDataPartitionLocationConsistency\\Input\\EpisodeOfCare\\partition-data.csv'))
            .toBeGreaterThan(-1);
        const cdmCorpus2 = testHelper_1.testHelper.getLocalCorpus(testsSubpath, 'TestModelJsonDataPartitionLocationConsistency');
        const manifestAfterConvertion = yield Persistence_1.ModelJson.ManifestPersistence.fromObject(cdmCorpus2.ctx, convertedToModelJson, cdmCorpus2.storage.fetchRootFolder('local'));
        expect(manifestAfterConvertion.entities.allItems[0].dataPartitions.allItems[0].location)
            .toEqual('EpisodeOfCare/partition-data.csv');
        const cdmCorpus3 = testHelper_1.testHelper.getLocalCorpus(testsSubpath, 'TestModelJsonDataPartitionLocationConsistency');
        const readFile = testHelper_1.testHelper.getInputFileContent(testsSubpath, 'TestModelJsonDataPartitionLocationConsistency', 'model.json');
        const namespaceFolder = cdmCorpus3.storage.fetchRootFolder('local');
        let modelJsonAsString = readFile;
        modelJsonAsString = modelJsonAsString.replace(/\r\n/g, '\n');
        modelJsonAsString = modelJsonAsString.replace(/\s/g, '');
        modelJsonAsString = modelJsonAsString.replace('C:\\\\cdm\\\\testData\\\\Persistence\\\\ModelJson\\\\DataPartition\\\\TestModelJsonDataPartitionLocationConsistency\\\\Input\\\\EpisodeOfCare\\\\partition-data.csv', location.replace(new RegExp('\\\\', 'g'), '\\\\'));
        modelJsonAsString = modelJsonAsString.replace(new RegExp('/', 'g'), '\\\\');
        const manifestReadFromModelJson = yield Persistence_1.ModelJson.ManifestPersistence.fromObject(cdmCorpus3.ctx, JSON.parse(modelJsonAsString), namespaceFolder);
        expect(manifestReadFromModelJson.entities.allItems[0].dataPartitions.allItems[0].location)
            .toEqual('EpisodeOfCare/partition-data.csv');
    }));
    /**
     * Tests that the trait is.partition.format.CSV is merged with the fileFormatSettings property during load.
     */
    it('testLoadingCsvPartitionTraits', () => __awaiter(void 0, void 0, void 0, function* () {
        var cdmCorpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, 'TestLoadingCsvPartitionTraits');
        var manifest = yield cdmCorpus.fetchObjectAsync('model.json');
        var dataPartition = manifest.entities.allItems[0].dataPartitions.allItems[0];
        // Ensure that the fileFormatSettings and the is.partition.format.CSV trait got merged into one trait.
        expect(dataPartition.exhibitsTraits.allItems.length)
            .toEqual(1);
        var csvTrait = dataPartition.exhibitsTraits.allItems[0];
        expect(csvTrait.arguments.allItems[0].value)
            .toEqual('true');
        expect(csvTrait.arguments.allItems[1].value)
            .toEqual('CsvStyle.QuoteAlways');
        expect(csvTrait.arguments.allItems[2].value)
            .toEqual(',');
        expect(csvTrait.arguments.allItems[3].value)
            .toEqual('QuoteStyle.Csv');
        expect(csvTrait.arguments.allItems[4].value)
            .toEqual('UTF-8');
        expect(csvTrait.arguments.allItems[5].value)
            .toEqual('\n');
    }));
    /**
     * Tests that the trait is.partition.format.CSV is merged with the fileFormatSettings property during load.
     * Given that the trait is does not have arguments present on fileFormatSettings.
     */
    it('testLoadingCsvPartitionTraitsFromFileFormatSettings', () => __awaiter(void 0, void 0, void 0, function* () {
        var cdmCorpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, 'TestLoadingCsvPartitionTraitsFromFileFormatSettings');
        var manifest = yield cdmCorpus.fetchObjectAsync('model.json');
        var dataPartition = manifest.entities.allItems[0].dataPartitions.allItems[0];
        // Ensure that the fileFormatSettings and the is.partition.format.CSV trait got merged into one trait.
        expect(dataPartition.exhibitsTraits.allItems.length)
            .toEqual(1);
        var csvTrait = dataPartition.exhibitsTraits.allItems[0];
        expect(csvTrait.arguments.allItems[0].value)
            .toEqual('\n');
        expect(csvTrait.arguments.allItems[1].value)
            .toEqual('false');
        expect(csvTrait.arguments.allItems[2].value)
            .toEqual('CsvStyle.QuoteAfterDelimiter');
        expect(csvTrait.arguments.allItems[3].value)
            .toEqual(';');
        expect(csvTrait.arguments.allItems[4].value)
            .toEqual('QuoteStyle.None');
        expect(csvTrait.arguments.allItems[5].value)
            .toEqual('ASCII');
    }));
    /**
     * Tests that the trait is.partition.format.CSV is saved when contains arguments not supported by fileFormatSettings.
     */
    it('testLoadingAndSavingCsvPartitionTraits', () => __awaiter(void 0, void 0, void 0, function* () {
        var cdmCorpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, 'TestLoadingCsvPartitionTraitsFromFileFormatSettings');
        var manifest = yield cdmCorpus.fetchObjectAsync('model.json');
        // If the data partition trait is.partition.format.CSV being saved has arguments that are not supported by fileFormatSettings
        // the trait should also be persisted.
        var manifestData = yield Persistence_1.ModelJson.ManifestPersistence.toData(manifest, new internal_1.resolveOptions(manifest.inDocument), new internal_1.copyOptions());
        var localEntity = manifestData.entities[0];
        expect(localEntity.partitions[0]['cdm:traits'].length)
            .toEqual(1);
        // Remove the argument that is not supported by fileFormatSettings and check if the trait is removed after that.
        var csvTrait = manifest.entities.allItems[0].dataPartitions.allItems[0].exhibitsTraits.allItems[0];
        csvTrait.arguments.remove(csvTrait.arguments.item('newline'));
        manifestData = yield Persistence_1.ModelJson.ManifestPersistence.toData(manifest, new internal_1.resolveOptions(manifest.inDocument), new internal_1.copyOptions());
        localEntity = manifestData.entities[0];
        expect(localEntity.partitions[0]['cdm:traits'])
            .toBeUndefined();
    }));
});
//# sourceMappingURL=dataPartition.test.js.map