"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 AttributeGroupPersistence_1 = require("../../../../Persistence/CdmFolder/AttributeGroupPersistence");
const EntityPersistence_1 = require("../../../../Persistence/CdmFolder/EntityPersistence");
const Storage_1 = require("../../../../Storage");
const testHelper_1 = require("../../../testHelper");
describe('Persistence.CdmFolder.Entity', () => {
    /// <summary>
    /// The path between TestDataPath and TestName.
    /// </summary>
    const testsSubpath = 'Persistence/CdmFolder/Entity';
    const schemaDocsRoot = testHelper_1.testHelper.schemaDocumentsPath;
    /**
     * Testing that trait with multiple properties are maintained even when one of the properties is null
     */
    it('TestEntityProperties', () => __awaiter(void 0, void 0, void 0, function* () {
        const corpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, 'TestEntityProperties');
        const obj = yield corpus.fetchObjectAsync('local:/entA.cdm.json/Entity A');
        const att = obj.attributes.allItems[0];
        let result = att.appliedTraits.allItems.find((x) => x.namedReference === 'is.constrained');
        expect(result).not
            .toBeUndefined();
        expect(att.maximumLength)
            .toBe(30);
        expect(att.maximumValue)
            .toBeUndefined();
        expect(att.minimumValue)
            .toBeUndefined();
        // removing the only argument should remove the trait
        att.maximumLength = undefined;
        result = att.appliedTraits.allItems.find((x) => x.namedReference === 'is.constrained');
        expect(att.maximumLength)
            .toBeUndefined();
        expect(result)
            .toBeUndefined();
    }));
    /**
     * Testing special case where "this.attributes" attributes do not inherit the InDocument field because these attributes
     * are created during resolution (no inDocument propagation during resolution). This error appears when running copyData
     * with stringRefs = true in certain cases
     */
    it('TestFromAndToDataWithElevatedTraits', () => __awaiter(void 0, void 0, void 0, function* () {
        const corpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, 'TestFromAndToDataWithElevatedTraits');
        // need to set schema docs to the cdm namespace instead of using resources
        corpus.storage.mount('cdm', new Storage_1.LocalAdapter(schemaDocsRoot));
        corpus.setEventCallback((level, msg) => {
            expect(msg.indexOf('unable to resolve an entity'))
                .toBe(-1);
        }, internal_1.cdmStatusLevel.warning);
        const entity = yield corpus.fetchObjectAsync('local:/Account.cdm.json/Account');
        const resEntity = yield entity.createResolvedEntityAsync(`${entity.entityName}_`);
        EntityPersistence_1.EntityPersistence.toData(resEntity, new internal_1.resolveOptions(resEntity.inDocument), { stringRefs: true });
    }));
    /**
     * Testing that loading entities with missing references logs warnings when the resolve option shallowValidation = true.
     */
    it('TestLoadingEntityWithShallowValidation', () => __awaiter(void 0, void 0, void 0, function* () {
        const corpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, 'TestLoadingEntityWithShallowValidation');
        corpus.storage.mount('cdm', new Storage_1.LocalAdapter(schemaDocsRoot));
        corpus.setEventCallback((level, msg) => {
            // When messages regarding references not being resolved or loaded are logged, check that they are warnings and not errors.
            if (msg.indexOf('Unable to resolve the reference') != -1 || msg.indexOf('Could not read') != -1) {
                expect(level).toEqual(internal_1.cdmStatusLevel.warning);
            }
        }, internal_1.cdmStatusLevel.warning);
        // Load entity with shallowValidation = true.
        yield corpus.fetchObjectAsync('local:/Entity.cdm.json/Entity', undefined, true);
        // Load resolved entity with shallowValidation = true.
        yield corpus.fetchObjectAsync('local:/ResolvedEntity.cdm.json/ResolvedEntity', undefined, true);
    }));
    /**
     * Testing that loading entities with missing references logs error when the resolve option shallowValidation = false.
     */
    it('TestLoadingEntityWithoutShallowValidation', () => __awaiter(void 0, void 0, void 0, function* () {
        const corpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, 'TestLoadingEntityWithShallowValidation');
        corpus.storage.mount('cdm', new Storage_1.LocalAdapter(schemaDocsRoot));
        corpus.setEventCallback((level, msg) => {
            // When messages regarding references not being resolved or loaded are logged, check that they are errors.
            if (msg.indexOf('Unable to resolve the reference') !== -1 || msg.indexOf('Could not read') != -1) {
                expect(level)
                    .toEqual(internal_1.cdmStatusLevel.error);
            }
        }, internal_1.cdmStatusLevel.warning);
        // Load entity with shallowValidation = false.
        yield corpus.fetchObjectAsync('local:/Entity.cdm.json/Entity');
        // Load resolved entity with shallowValidation = false.
        yield corpus.fetchObjectAsync('local:/ResolvedEntity.cdm.json/ResolvedEntity');
    }));
    /**
     * Testing that an empty list in an Attribute Group still exists after persisting
     */
    it('TestPersistAttributeGroupDefinition', () => {
        const corpus = new internal_1.CdmCorpusDefinition();
        const attGroup = new internal_1.CdmAttributeGroupDefinition(corpus.ctx, 'attGroup');
        const persistedGroup = AttributeGroupPersistence_1.AttributeGroupPersistence.toData(attGroup, new internal_1.resolveOptions(attGroup.inDocument), new internal_1.copyOptions());
        expect(persistedGroup.members).not
            .toBeUndefined();
    });
});
//# sourceMappingURL=Entity.test.js.map