"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.CdmFolder.DataPartition', () => {
    /// <summary>
    /// The path between TestDataPath and TestName.
    /// </summary>
    const testsSubpath = 'Persistence/CdmFolder/DataPartition';
    const doesWriteTestDebuggingFiles = false;
    /**
     * Testing for Manifest instance with local entity declaration having data partitions.
     */
    it('TestLoadLocalEntityWithDataPartition', () => {
        const readFile = testHelper_1.testHelper.getInputFileContent(testsSubpath, 'TestLoadLocalEntityWithDataPartition', 'entities.manifest.cdm.json');
        const cdmManifest = Persistence_1.CdmFolder.ManifestPersistence.fromObject(new internal_1.resolveContext(new internal_1.CdmCorpusDefinition(), undefined), 'entities', 'testNamespace', '/', JSON.parse(readFile));
        expect(cdmManifest.entities.length)
            .toBe(1);
        expect(cdmManifest.entities.allItems[0].getObjectType())
            .toBe(internal_1.cdmObjectType.localEntityDeclarationDef);
        const entity = cdmManifest.entities.allItems[0];
        expect(entity.dataPartitions.length)
            .toBe(2);
        const relativePartition = entity.dataPartitions.allItems[0];
        expect(relativePartition.name)
            .toBe('Sample data partition');
        expect(relativePartition.location)
            .toBe('test/location');
        expect(relativePartition.lastFileModifiedTime.toUTCString())
            .toBe('Mon, 15 Sep 2008 23:53:23 GMT');
        expect(relativePartition.exhibitsTraits.length)
            .toBe(1);
        expect(relativePartition.specializedSchema)
            .toBe('teststring');
        const testList = relativePartition.arguments.get('test');
        expect(testList.length)
            .toEqual(3);
        expect(testList[0])
            .toEqual('something');
        expect(testList[1])
            .toEqual('somethingelse');
        expect(testList[2])
            .toEqual('anotherthing');
        const keyList = relativePartition.arguments.get('KEY');
        expect(keyList.length)
            .toEqual(1);
        expect(keyList[0])
            .toEqual('VALUE');
        expect(relativePartition.arguments.has('wrong'))
            .toBeFalsy();
        const absolutePartition = entity.dataPartitions.allItems[1];
        expect(absolutePartition.location)
            .toBe('local:/some/test/location');
    });
    /**
     * Manifest.DataPartitions.Arguments can be read in multiple forms,
     * but should always be serialized as {name: 'theName', value: 'theValue'}.
     */
    it('TestDataPartitionArgumentsAreSerializedAppropriately', () => {
        const readFile = testHelper_1.testHelper.getInputFileContent(testsSubpath, 'TestDataPartitionArgumentsAreSerializedAppropriately', 'entities.manifest.cdm.json');
        const cdmManifest = Persistence_1.CdmFolder.ManifestPersistence.fromObject(new internal_1.resolveContext(new internal_1.CdmCorpusDefinition(), undefined), 'entities', 'testNamespace', '/', JSON.parse(readFile));
        const obtainedCdmFolder = Persistence_1.CdmFolder.ManifestPersistence.toData(cdmManifest, undefined, undefined);
        if (doesWriteTestDebuggingFiles) {
            testHelper_1.testHelper.writeActualOutputFileContent(testsSubpath, 'TestDataPartitionArgumentsAreSerializedAppropriately', 'savedManifest.manifest.cdm.json', JSON.stringify(obtainedCdmFolder));
        }
        const expectedOutput = testHelper_1.testHelper.getExpectedOutputFileContent(testsSubpath, 'TestDataPartitionArgumentsAreSerializedAppropriately', 'savedManifest.manifest.cdm.json');
        const expectedManifest = JSON.parse(expectedOutput);
        expect(testHelper_1.testHelper.compareObjectsContent(obtainedCdmFolder, expectedManifest, true))
            .toBeTruthy();
    });
    /**
     * Testing programatically creating manifest with partitions and persisting
     */
    it('TestProgrammaticallyCreatePartitions', () => __awaiter(void 0, void 0, void 0, function* () {
        const corpus = new internal_1.CdmCorpusDefinition();
        const manifest = corpus.MakeObject(internal_1.cdmObjectType.manifestDef, 'manifest');
        const entity = manifest.entities.push('entity');
        const relativePartition = corpus.MakeObject(internal_1.cdmObjectType.dataPartitionDef, 'relative partition');
        relativePartition.location = 'relative/path';
        relativePartition.arguments.set('test1', ['argument1']);
        relativePartition.arguments.set('test2', ['argument2', 'argument3']);
        const absolutePartition = corpus.MakeObject(internal_1.cdmObjectType.dataPartitionDef, 'absolute partition');
        absolutePartition.location = 'local:/absolute/path';
        // add an empty arguments list to test empty list should not be displayed in ToData json.
        absolutePartition.arguments.set('test', []);
        entity.dataPartitions.push(relativePartition);
        entity.dataPartitions.push(absolutePartition);
        const manifestData = Persistence_1.CdmFolder.ManifestPersistence.toData(manifest, new internal_1.resolveOptions(), new internal_1.copyOptions());
        expect(manifestData.entities.length)
            .toBe(1);
        const entityData = manifestData.entities[0];
        const partitionsList = entityData.dataPartitions;
        expect(partitionsList.length)
            .toBe(2);
        const relativePartitionData = partitionsList[0];
        const absolutePartitionData = partitionsList[1];
        expect(relativePartitionData.location)
            .toBe(relativePartition.location);
        const argumentsList = relativePartitionData.arguments;
        expect(argumentsList.length)
            .toBe(3);
        expect(argumentsList[0].name)
            .toBe('test1');
        expect(argumentsList[0].value)
            .toBe('argument1');
        expect(argumentsList[1].name)
            .toBe('test2');
        expect(argumentsList[1].value)
            .toBe('argument2');
        expect(argumentsList[2].name)
            .toBe('test2');
        expect(argumentsList[2].value)
            .toBe('argument3');
        expect(absolutePartitionData.location)
            .toBe(absolutePartition.location);
        // test if empty argument list is set to null
        expect(absolutePartitionData.arguments)
            .toBeUndefined();
    }));
});
//# sourceMappingURL=DataPartition.test.js.map