"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 testHelper_1 = require("../../testHelper");
const projectionTestUtils_1 = require("../../Utilities/projectionTestUtils");
/**
 *  Class to handle AlterTraits operations
 */
describe('Cdm/Projection/TestProjectionAddArtifactAttribute', () => {
    /**
     * The path between TestDataPath and TestName.
     */
    const testsSubpath = 'Cdm/Projection/ProjectionAddArtifactAttributeTest';
    /**
     * All possible combinations of the different resolution directives
     */
    const resOptsCombinations = [
        [],
        ['referenceOnly'],
        ['normalized'],
        ['structured'],
        ['referenceOnly', 'normalized'],
        ['referenceOnly', 'structured'],
        ['normalized', 'structured'],
        ['referenceOnly', 'normalized', 'structured']
    ];
    /**
     * Test AddArtifactAttribute to add an entity attribute on an entity attribute.
     */
    it('TestAddEntAttrOnEntAttrProj', () => __awaiter(void 0, void 0, void 0, function* () {
        const testName = 'TestAddEntAttrOnEntAttrProj';
        const entityName = 'NewPerson';
        const corpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, testName);
        corpus.setEventCallback((statusLevel, message) => {
            if (message.indexOf('CdmOperationAddArtifactAttribute | Operation AddArtifactAttribute is not supported on an entity attribute yet.') === -1) {
                throw new Error(`Some unexpected failure - ${message}!`);
            }
        }, internal_1.cdmStatusLevel.warning);
        const entity = yield corpus.fetchObjectAsync(`local:/${entityName}.cdm.json/${entityName}`);
        const resolvedEntity = yield projectionTestUtils_1.projectionTestUtils.getResolvedEntity(corpus, entity, []);
    }));
    /**
     * Test AddArtifactAttribute to add an attribute group on an entity attribute.
     */
    it('TestAddAttrGrpOnEntAttrProj', () => __awaiter(void 0, void 0, void 0, function* () {
        const testName = 'TestAddAttrGrpOnEntAttrProj';
        const entityName = 'NewPerson';
        const corpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, testName);
        corpus.setEventCallback((statusLevel, message) => {
            if (message.indexOf('CdmOperationAddArtifactAttribute | Operation AddArtifactAttribute is not supported on an attribute group yet.') === -1) {
                throw new Error(`Some unexpected failure - ${message}!`);
            }
        }, internal_1.cdmStatusLevel.warning);
        const entity = yield corpus.fetchObjectAsync(`local:/${entityName}.cdm.json/${entityName}`);
        const resolvedEntity = yield projectionTestUtils_1.projectionTestUtils.getResolvedEntity(corpus, entity, []);
    }));
    /**
     * Test AddArtifactAttribute to add a type attribute on a type attribute.
     */
    it('TestAddTypeAttrOnTypeAttrProj', () => __awaiter(void 0, void 0, void 0, function* () {
        const testName = 'TestAddTypeAttrOnTypeAttrProj';
        const entityName = 'Person';
        const corpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, testName);
        for (const resOpt of resOptsCombinations) {
            yield projectionTestUtils_1.projectionTestUtils.loadEntityForResolutionOptionAndSave(corpus, testName, testsSubpath, entityName, resOpt);
        }
        const entity = yield corpus.fetchObjectAsync(`local:/${entityName}.cdm.json/${entityName}`);
        const resolvedEntity = yield projectionTestUtils_1.projectionTestUtils.getResolvedEntity(corpus, entity, []);
        expect(resolvedEntity.attributes.length)
            .toEqual(2);
        expect(resolvedEntity.attributes.allItems[0].name)
            .toEqual('newTerm');
        expect(resolvedEntity.attributes.allItems[1].name)
            .toEqual('FavoriteTerm');
    }));
    /**
     * Test AddArtifactAttribute operation nested with ExcludeAttributes/RenameAttributes.
     */
    it('TestCombineOpsNestedProj', () => __awaiter(void 0, void 0, void 0, function* () {
        const testName = 'TestCombineOpsNestedProj';
        const entityName = 'NewPerson';
        const corpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, testName);
        for (const resOpt of resOptsCombinations) {
            yield projectionTestUtils_1.projectionTestUtils.loadEntityForResolutionOptionAndSave(corpus, testName, testsSubpath, entityName, resOpt);
        }
        const entity = yield corpus.fetchObjectAsync(`local:/${entityName}.cdm.json/${entityName}`);
        const resolvedEntity = yield projectionTestUtils_1.projectionTestUtils.getResolvedEntity(corpus, entity, []);
        // Original set of attributes: [ { "name", "age", "address", "phoneNumber", "email" }, "FavoriteTerm" ]
        // Entity Attribuite:
        // Exclude attributes: ["age", "phoneNumber", "name"]
        // Add attribute: ["newName"]
        // Rename attribute ["newName" -> "renaming-{m}" ]
        // Rename attribute ["renaming-newName" -> "renamingAgain-{m}" ]
        // Add attribute: ["newName_1"]
        // Type Attribute:
        // Add attribute: ["newName"]
        // Rename attribute ["newName" -> "renamed-{m}" ]
        // Add attribute: ["newTerm" (InsertAtTop:true)]
        expect(resolvedEntity.attributes.length)
            .toEqual(7);
        expect(resolvedEntity.attributes.allItems[0].name)
            .toEqual('address');
        expect(resolvedEntity.attributes.allItems[1].name)
            .toEqual('email');
        expect(resolvedEntity.attributes.allItems[2].name)
            .toEqual('renamingAgain-renaming-newName');
        expect(resolvedEntity.attributes.allItems[3].name)
            .toEqual('newName_1');
        expect(resolvedEntity.attributes.allItems[4].name)
            .toEqual('newTerm');
        expect(resolvedEntity.attributes.allItems[5].name)
            .toEqual('FavoriteTerm');
        expect(resolvedEntity.attributes.allItems[6].name)
            .toEqual('renamed-newName');
    }));
    /**
     * Test AddArtifactAttribute operation with a 'structured' condition.
     */
    it('TestConditionalProj', () => __awaiter(void 0, void 0, void 0, function* () {
        const testName = 'TestConditionalProj';
        const entityName = 'NewPerson';
        const corpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, testName);
        for (const resOpt of resOptsCombinations) {
            yield projectionTestUtils_1.projectionTestUtils.loadEntityForResolutionOptionAndSave(corpus, testName, testsSubpath, entityName, resOpt);
        }
        const entity = yield corpus.fetchObjectAsync(`local:/${entityName}.cdm.json/${entityName}`);
        const resolvedEntity = yield projectionTestUtils_1.projectionTestUtils.getResolvedEntity(corpus, entity, ['referenceOnly']);
        // Original set of attributes: ['name', 'age', 'address', 'phoneNumber', 'email']
        // Condition not met, keep attributes in flat list
        expect(resolvedEntity.attributes.length)
            .toEqual(5);
        expect(resolvedEntity.attributes.allItems[0].name)
            .toEqual('name');
        expect(resolvedEntity.attributes.allItems[1].name)
            .toEqual('age');
        expect(resolvedEntity.attributes.allItems[2].name)
            .toEqual('address');
        expect(resolvedEntity.attributes.allItems[3].name)
            .toEqual('phoneNumber');
        expect(resolvedEntity.attributes.allItems[4].name)
            .toEqual('email');
        const resolvedEntity2 = yield projectionTestUtils_1.projectionTestUtils.getResolvedEntity(corpus, entity, ['structured']);
        // Original set of attributes: ['name', 'age', 'address', 'phoneNumber', 'email']
        // Condition met, keep attributes in flat list and add the new attribute "newName" all attributes at the end
        expect(resolvedEntity2.attributes.length)
            .toEqual(6);
        expect(resolvedEntity2.attributes.allItems[0].name)
            .toEqual('name');
        expect(resolvedEntity2.attributes.allItems[1].name)
            .toEqual('age');
        expect(resolvedEntity2.attributes.allItems[2].name)
            .toEqual('address');
        expect(resolvedEntity2.attributes.allItems[3].name)
            .toEqual('phoneNumber');
        expect(resolvedEntity2.attributes.allItems[4].name)
            .toEqual('email');
        expect(resolvedEntity2.attributes.allItems[5].name)
            .toEqual('newName');
    }));
    /**
     * Test for creating a projection with an AddArtifactAttribute operation and a condition using the object model.
     */
    it('TestConditionalProjUsingObjectModel', () => __awaiter(void 0, void 0, void 0, function* () {
        const testName = 'TestConditionalProjUsingObjectModel';
        const corpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, testName);
        const localRoot = corpus.storage.fetchRootFolder('local');
        // Create an entity.
        const entity = projectionTestUtils_1.projectionTestUtils.createEntity(corpus, localRoot);
        // Create a projection with a condition that states the operation should only execute when the resolution directive is 'structured'.
        const projection = projectionTestUtils_1.projectionTestUtils.createProjection(corpus, localRoot);
        projection.condition = 'structured==true';
        // Create an AddArtifactAttribute operation
        const addArtifactAttributeOp = corpus.MakeObject(internal_1.cdmObjectType.operationAddArtifactAttributeDef);
        addArtifactAttributeOp.newAttribute = corpus.MakeObject(internal_1.cdmObjectType.typeAttributeDef, 'newName');
        addArtifactAttributeOp.newAttribute.dataType = corpus.MakeRef(internal_1.cdmObjectType.dataTypeRef, 'string', true);
        projection.operations.push(addArtifactAttributeOp);
        // Create an entity reference to hold this projection.
        const projectionEntityRef = corpus.MakeObject(internal_1.cdmObjectType.entityRef, undefined);
        projectionEntityRef.explicitReference = projection;
        // Create an entity attribute that contains this projection and add this to the entity.
        const entityAttribute = corpus.MakeObject(internal_1.cdmObjectType.entityAttributeDef, 'TestEntityAttribute');
        entityAttribute.entity = projectionEntityRef;
        entity.attributes.push(entityAttribute);
        // Create resolution options with the 'referenceOnly' directive.
        const resOpt = new internal_1.resolveOptions(entity.inDocument);
        resOpt.directives = new internal_1.AttributeResolutionDirectiveSet(new Set(['referenceOnly']));
        // Resolve the entity with 'referenceOnly'
        const resolvedEntityWithReferenceOnly = yield entity.createResolvedEntityAsync(`Resolved_${entity.entityName}.cdm.json`, resOpt, localRoot);
        // Original set of attributes: ['id', 'name', 'value', 'date']
        // Condition not met, keep attributes in flat list
        expect(resolvedEntityWithReferenceOnly.attributes.length)
            .toEqual(4);
        expect(resolvedEntityWithReferenceOnly.attributes.allItems[0].name)
            .toEqual('id');
        expect(resolvedEntityWithReferenceOnly.attributes.allItems[1].name)
            .toEqual('name');
        expect(resolvedEntityWithReferenceOnly.attributes.allItems[2].name)
            .toEqual('value');
        expect(resolvedEntityWithReferenceOnly.attributes.allItems[3].name)
            .toEqual('date');
        // Now resolve the entity with the 'structured' directive
        resOpt.directives = new internal_1.AttributeResolutionDirectiveSet(new Set(['structured']));
        const resolvedEntityWithStructured = yield entity.createResolvedEntityAsync(`Resolved_${entity.entityName}.cdm.json`, resOpt, localRoot);
        // Verify correctness of the resolved attributes after running the AddAttributeGroup operation
        // Condition met, new traits are added
        expect(resolvedEntityWithStructured.attributes.length)
            .toEqual(5);
        expect(resolvedEntityWithStructured.attributes.allItems[0].name)
            .toEqual('id');
        expect(resolvedEntityWithStructured.attributes.allItems[1].name)
            .toEqual('name');
        expect(resolvedEntityWithStructured.attributes.allItems[2].name)
            .toEqual('value');
        expect(resolvedEntityWithStructured.attributes.allItems[3].name)
            .toEqual('date');
        expect(resolvedEntityWithStructured.attributes.allItems[4].name)
            .toEqual('newName');
    }));
    /**
     * Test AddArtifactAttribute operation on an entity definition.
     */
    it('TestExtendsEntityProj', () => __awaiter(void 0, void 0, void 0, function* () {
        const testName = 'TestExtendsEntityProj';
        const entityName = 'Child';
        const corpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, testName);
        for (const resOpt of resOptsCombinations) {
            yield projectionTestUtils_1.projectionTestUtils.loadEntityForResolutionOptionAndSave(corpus, testName, testsSubpath, entityName, resOpt);
        }
        const entity = yield corpus.fetchObjectAsync(`local:/${entityName}.cdm.json/${entityName}`);
        const resolvedEntity = yield projectionTestUtils_1.projectionTestUtils.getResolvedEntity(corpus, entity, []);
        // Original set of attributes: ['name', 'age', 'address', 'phoneNumber', 'email']
        expect(resolvedEntity.attributes.length)
            .toEqual(6);
        expect(resolvedEntity.attributes.allItems[0].name)
            .toEqual('name');
        expect(resolvedEntity.attributes.allItems[1].name)
            .toEqual('age');
        expect(resolvedEntity.attributes.allItems[2].name)
            .toEqual('address');
        expect(resolvedEntity.attributes.allItems[3].name)
            .toEqual('phoneNumber');
        expect(resolvedEntity.attributes.allItems[4].name)
            .toEqual('email');
        expect(resolvedEntity.attributes.allItems[5].name)
            .toEqual('newName');
    }));
    /**
     * Multiple AddArtifactAttribute operations on the same projection.
     */
    it('TestMultipleOpProj', () => __awaiter(void 0, void 0, void 0, function* () {
        const testName = 'TestMultipleOpProj';
        const entityName = 'NewPerson';
        const corpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, testName);
        for (const resOpt of resOptsCombinations) {
            yield projectionTestUtils_1.projectionTestUtils.loadEntityForResolutionOptionAndSave(corpus, testName, testsSubpath, entityName, resOpt);
        }
        const entity = yield corpus.fetchObjectAsync(`local:/${entityName}.cdm.json/${entityName}`);
        const resolvedEntity = yield projectionTestUtils_1.projectionTestUtils.getResolvedEntity(corpus, entity, []);
        // Original set of attributes: ["name", "age", "address", "phoneNumber", "email"]
        // Add attribute: ["newName", "newName_1", "newName"]
        // 2 "newName" will be merged
        expect(resolvedEntity.attributes.length)
            .toEqual(7);
        expect(resolvedEntity.attributes.allItems[0].name)
            .toEqual('name');
        expect(resolvedEntity.attributes.allItems[1].name)
            .toEqual('age');
        expect(resolvedEntity.attributes.allItems[2].name)
            .toEqual('address');
        expect(resolvedEntity.attributes.allItems[3].name)
            .toEqual('phoneNumber');
        expect(resolvedEntity.attributes.allItems[4].name)
            .toEqual('email');
        expect(resolvedEntity.attributes.allItems[5].name)
            .toEqual('newName');
        expect(resolvedEntity.attributes.allItems[6].name)
            .toEqual('newName_1');
    }));
    /**
     * Test insertAtTop field in AddArtifactAttribute operation.
     */
    it('TestInsertAtTop', () => __awaiter(void 0, void 0, void 0, function* () {
        const testName = 'TestInsertAtTop';
        const entityName = 'NewPerson';
        const corpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, testName);
        for (const resOpt of resOptsCombinations) {
            yield projectionTestUtils_1.projectionTestUtils.loadEntityForResolutionOptionAndSave(corpus, testName, testsSubpath, entityName, resOpt);
        }
        const entity = yield corpus.fetchObjectAsync(`local:/${entityName}.cdm.json/${entityName}`);
        const resolvedEntity = yield projectionTestUtils_1.projectionTestUtils.getResolvedEntity(corpus, entity, []);
        // Original set of attributes: ["name", "age", "address", "phoneNumber", "email"]
        // Add attribute: ["newName" (InsertAtTop:false), "newName_1" (InsertAtTop:true)]
        expect(resolvedEntity.attributes.length)
            .toEqual(7);
        expect(resolvedEntity.attributes.allItems[0].name)
            .toEqual('newName_1');
        expect(resolvedEntity.attributes.allItems[1].name)
            .toEqual('name');
        expect(resolvedEntity.attributes.allItems[2].name)
            .toEqual('age');
        expect(resolvedEntity.attributes.allItems[3].name)
            .toEqual('address');
        expect(resolvedEntity.attributes.allItems[4].name)
            .toEqual('phoneNumber');
        expect(resolvedEntity.attributes.allItems[5].name)
            .toEqual('email');
        expect(resolvedEntity.attributes.allItems[6].name)
            .toEqual('newName');
    }));
    /**
     * Nested projections with AddArtifactAttribute.
     */
    it('TestNestedProj', () => __awaiter(void 0, void 0, void 0, function* () {
        const testName = 'TestNestedProj';
        const entityName = 'NewPerson';
        const corpus = testHelper_1.testHelper.getLocalCorpus(testsSubpath, testName);
        for (const resOpt of resOptsCombinations) {
            yield projectionTestUtils_1.projectionTestUtils.loadEntityForResolutionOptionAndSave(corpus, testName, testsSubpath, entityName, resOpt);
        }
        const entity = yield corpus.fetchObjectAsync(`local:/${entityName}.cdm.json/${entityName}`);
        const resolvedEntity = yield projectionTestUtils_1.projectionTestUtils.getResolvedEntity(corpus, entity, []);
        // Create resolution options with the 'referenceOnly' directive.
        const resolvedEntityWithReferenceOnly = yield projectionTestUtils_1.projectionTestUtils.getResolvedEntity(corpus, entity, ['referenceOnly']);
        // Original set of attributes: ["name", "age", "address", "phoneNumber", "email"]
        expect(resolvedEntity.attributes.length)
            .toEqual(7);
        expect(resolvedEntity.attributes.allItems[0].name)
            .toEqual('name');
        expect(resolvedEntity.attributes.allItems[1].name)
            .toEqual('age');
        expect(resolvedEntity.attributes.allItems[2].name)
            .toEqual('address');
        expect(resolvedEntity.attributes.allItems[3].name)
            .toEqual('phoneNumber');
        expect(resolvedEntity.attributes.allItems[4].name)
            .toEqual('email');
        expect(resolvedEntity.attributes.allItems[5].name)
            .toEqual('newName_inner');
        expect(resolvedEntity.attributes.allItems[6].name)
            .toEqual('newName_outer');
    }));
});
//# sourceMappingURL=ProjectionAddArtifactAttribute.test.js.map