"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/Projection', () => {
    /**
     * The path between TestDataPath and TestName.
     */
    const testsSubpath = 'Persistence/CdmFolder/Projection';
    /**
     * Basic test to load persisted Projection based entities
     */
    it('TestLoadProjection', () => __awaiter(void 0, void 0, void 0, function* () {
        const corpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, 'TestLoadProjection');
        const manifest = yield corpus.fetchObjectAsync('local:/default.manifest.cdm.json');
        const expected = 'TestSource';
        let actual;
        let actualType = internal_1.cdmObjectType.error;
        // TestEntityStringReference.cdm.json
        const entTestEntityStringReference = yield corpus.fetchObjectAsync('local:/TestEntityStringReference.cdm.json/TestEntityStringReference', manifest);
        expect(entTestEntityStringReference)
            .toBeTruthy();
        actual = entTestEntityStringReference.extendsEntity.namedReference;
        actualType = entTestEntityStringReference.extendsEntity.objectType;
        expect(actual)
            .toEqual(expected);
        expect(actualType)
            .toEqual(internal_1.cdmObjectType.entityRef);
        // TestEntityEntityReference.cdm.json
        const entTestEntityEntityReference = yield corpus.fetchObjectAsync('local:/TestEntityEntityReference.cdm.json/TestEntityEntityReference', manifest);
        expect(entTestEntityEntityReference)
            .toBeTruthy();
        actual = entTestEntityEntityReference.extendsEntity.namedReference;
        actualType = entTestEntityEntityReference.extendsEntity.objectType;
        expect(actual)
            .toEqual(expected);
        expect(actualType)
            .toEqual(internal_1.cdmObjectType.entityRef);
        // TestEntityProjection.cdm.json
        const entTestEntityProjection = yield corpus.fetchObjectAsync('local:/TestEntityProjection.cdm.json/TestEntityProjection', manifest);
        expect(entTestEntityProjection)
            .toBeTruthy();
        actual = entTestEntityProjection.extendsEntity.explicitReference.source.namedReference;
        actualType = entTestEntityProjection.extendsEntity.explicitReference.objectType;
        expect(actual)
            .toEqual(expected);
        expect(actualType)
            .toEqual(internal_1.cdmObjectType.projectionDef);
        // TestEntityNestedProjection.cdm.json
        const entTestEntityNestedProjection = yield corpus.fetchObjectAsync('local:/TestEntityNestedProjection.cdm.json/TestEntityNestedProjection', manifest);
        expect(entTestEntityNestedProjection)
            .toBeTruthy();
        actual = entTestEntityNestedProjection.extendsEntity.explicitReference.source.explicitReference.source.explicitReference.source.namedReference;
        actualType = (entTestEntityNestedProjection.extendsEntity.explicitReference.source.explicitReference.source.explicitReference).objectType;
        expect(actual)
            .toEqual(expected);
        expect(actualType)
            .toEqual(internal_1.cdmObjectType.projectionDef);
        // TestEntityAttributeStringReference.cdm.json
        const entTestEntityAttributeStringReference = yield corpus.fetchObjectAsync('local:/TestEntityAttributeStringReference.cdm.json/TestEntityAttributeStringReference', manifest);
        expect(entTestEntityAttributeStringReference)
            .toBeTruthy();
        actual = entTestEntityAttributeStringReference.attributes.allItems[0].entity.namedReference;
        actualType = entTestEntityAttributeStringReference.attributes.allItems[0].entity.objectType;
        expect(actual)
            .toEqual(expected);
        expect(actualType)
            .toEqual(internal_1.cdmObjectType.entityRef);
        // TestEntityAttributeEntityReference.cdm.json
        const entTestEntityAttributeEntityReference = yield corpus.fetchObjectAsync('local:/TestEntityAttributeEntityReference.cdm.json/TestEntityAttributeEntityReference', manifest);
        expect(entTestEntityAttributeEntityReference)
            .toBeTruthy();
        actual = entTestEntityAttributeEntityReference.attributes.allItems[0].entity.namedReference;
        actualType = entTestEntityAttributeEntityReference.attributes.allItems[0].entity.objectType;
        expect(actual)
            .toEqual(expected);
        expect(actualType)
            .toEqual(internal_1.cdmObjectType.entityRef);
        // TestEntityAttributeProjection.cdm.json
        const entTestEntityAttributeProjection = yield corpus.fetchObjectAsync('local:/TestEntityAttributeProjection.cdm.json/TestEntityAttributeProjection', manifest);
        expect(entTestEntityAttributeProjection)
            .toBeTruthy();
        actual = entTestEntityAttributeProjection.attributes.allItems[0].entity.explicitReference.source.namedReference;
        actualType = entTestEntityAttributeProjection.attributes.allItems[0].entity.explicitReference.objectType;
        expect(actual)
            .toEqual(expected);
        expect(actualType)
            .toEqual(internal_1.cdmObjectType.projectionDef);
        // TestEntityAttributeNestedProjection.cdm.json
        const entTestEntityAttributeNestedProjection = yield corpus.fetchObjectAsync('local:/TestEntityAttributeNestedProjection.cdm.json/TestEntityAttributeNestedProjection', manifest);
        expect(entTestEntityAttributeNestedProjection)
            .toBeTruthy();
        actual = entTestEntityAttributeNestedProjection.attributes.allItems[0].entity.explicitReference.source.explicitReference.source.explicitReference.source.namedReference;
        actualType = entTestEntityAttributeNestedProjection.attributes.allItems[0].entity.explicitReference.source.explicitReference.source.explicitReference.objectType;
        expect(actual)
            .toEqual(expected);
        expect(actualType)
            .toEqual(internal_1.cdmObjectType.projectionDef);
        // TestOperationCollection.cdm.json
        const entTestOperationCollection = yield corpus.fetchObjectAsync('local:/TestOperationCollection.cdm.json/TestOperationCollection', manifest);
        expect(entTestOperationCollection)
            .toBeTruthy();
        const actualOperationCount = entTestOperationCollection.extendsEntity.explicitReference.operations.length;
        expect(actualOperationCount)
            .toEqual(9);
        const operations = entTestOperationCollection.extendsEntity.explicitReference.operations;
        expect(operations.allItems[0].type)
            .toEqual(internal_1.cdmOperationType.addCountAttribute);
        expect(operations.allItems[1].type)
            .toEqual(internal_1.cdmOperationType.addSupportingAttribute);
        expect(operations.allItems[2].type)
            .toEqual(internal_1.cdmOperationType.addTypeAttribute);
        expect(operations.allItems[3].type)
            .toEqual(internal_1.cdmOperationType.excludeAttributes);
        expect(operations.allItems[4].type)
            .toEqual(internal_1.cdmOperationType.arrayExpansion);
        expect(operations.allItems[5].type)
            .toEqual(internal_1.cdmOperationType.combineAttributes);
        expect(operations.allItems[6].type)
            .toEqual(internal_1.cdmOperationType.renameAttributes);
        expect(operations.allItems[7].type)
            .toEqual(internal_1.cdmOperationType.replaceAsForeignKey);
        expect(operations.allItems[8].type)
            .toEqual(internal_1.cdmOperationType.includeAttributes);
        // TestEntityTrait.cdm.json
        const entTestEntityTrait = yield corpus.fetchObjectAsync('local:/TestEntityTrait.cdm.json/TestEntityTrait', manifest);
        expect(entTestEntityTrait)
            .toBeTruthy();
        expect(entTestEntityTrait.attributes.allItems[0].name)
            .toEqual('TestAttribute');
        expect(entTestEntityTrait.attributes.allItems[0].dataType.namedReference)
            .toEqual('testDataType');
        // TestEntityExtendsTrait.cdm.json
        const entTestEntityExtendsTrait = yield corpus.fetchObjectAsync('local:/TestEntityExtendsTrait.cdm.json/TestEntityExtendsTrait', manifest);
        expect(entTestEntityExtendsTrait)
            .toBeTruthy();
        expect(entTestEntityExtendsTrait.attributes.allItems[0].name)
            .toEqual('TestExtendsTraitAttribute');
        expect(entTestEntityExtendsTrait.attributes.allItems[0].dataType.namedReference)
            .toEqual('testDerivedDataType');
        // TestProjectionTrait.cdm.json
        const entTestProjectionTrait = yield corpus.fetchObjectAsync('local:/TestProjectionTrait.cdm.json/TestProjectionTrait', manifest);
        expect(entTestProjectionTrait)
            .toBeTruthy();
        expect(entTestProjectionTrait.attributes.allItems[0].name)
            .toEqual('TestProjectionAttribute');
        expect(entTestProjectionTrait.attributes.allItems[0].dataType.namedReference)
            .toEqual('testDataType');
        // TestProjectionExtendsTrait.cdm.json
        const entTestProjectionExtendsTrait = yield corpus.fetchObjectAsync('local:/TestProjectionExtendsTrait.cdm.json/TestProjectionExtendsTrait', manifest);
        expect(entTestProjectionExtendsTrait)
            .toBeTruthy();
        expect(entTestProjectionExtendsTrait.attributes.allItems[0].name)
            .toEqual('TestProjectionAttributeB');
        expect(entTestProjectionExtendsTrait.attributes.allItems[0].dataType.namedReference)
            .toEqual('testExtendsDataTypeB');
    }));
    /**
     * Basic test to save persisted Projections based entities
     */
    it('TestSaveProjection', () => __awaiter(void 0, void 0, void 0, function* () {
        const corpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, 'TestSaveProjection');
        const manifest = yield corpus.fetchObjectAsync('local:/default.manifest.cdm.json');
        const entitySales = yield corpus.fetchObjectAsync('local:/Sales.cdm.json/Sales', manifest);
        expect(entitySales)
            .toBeTruthy();
        const actualRoot = corpus.storage.fetchRootFolder("output");
        expect(actualRoot)
            .toBeTruthy();
        actualRoot.documents.push(entitySales.inDocument, 'Persisted_Sales.cdm.json');
        yield actualRoot.documents.allItems[0].saveAsAsync('output:/Persisted_Sales.cdm.json');
        const entityActual = yield corpus.fetchObjectAsync('output:/Persisted_Sales.cdm.json/Sales', manifest);
        expect(entityActual)
            .toBeTruthy();
        const entityContentActual = Persistence_1.PersistenceLayer.toData(entityActual, null, null, "CdmFolder");
        expect(entityContentActual)
            .toBeTruthy();
        expect(entityContentActual.hasAttributes)
            .toBeTruthy();
        expect(entityContentActual.hasAttributes.length === 1)
            .toBeTruthy();
        expect(JSON.stringify(entityContentActual.hasAttributes[0]).indexOf('"entityReference"'))
            .toBe(-1);
    }));
});
//# sourceMappingURL=ProjectionPersistence.test.js.map