"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
Object.defineProperty(exports, "__esModule", { value: true });
const cdmObjectType_1 = require("../../../Enums/cdmObjectType");
const internal_1 = require("../../../internal");
const Storage_1 = require("../../../Storage");
const CdmCollectionHelperFunctions_1 = require("./CdmCollectionHelperFunctions");
describe('Cdm/CdmCollection/CdmEntityCollection', () => {
    it('TestManifestAddEntityWithLocalizedPath', () => {
        const manifest = CdmCollectionHelperFunctions_1.generateManifest('C:\\Nothing');
        const cdmCorpus = manifest.ctx.corpus;
        const entity = new internal_1.CdmEntityDefinition(cdmCorpus.ctx, 'entityName', undefined);
        entity.explanation = 'The explanation of the entity';
        CdmCollectionHelperFunctions_1.createDocumentForEntity(cdmCorpus, entity);
        const cdmEntity = new internal_1.CdmEntityDefinition(cdmCorpus.ctx, 'cdmEntityName', undefined);
        CdmCollectionHelperFunctions_1.createDocumentForEntity(cdmCorpus, cdmEntity, 'cdm');
        const localizedEntityDeclaration = manifest.entities.push(entity);
        const cdmEntityDeclaration = manifest.entities.push(cdmEntity);
        expect(localizedEntityDeclaration.explanation)
            .toEqual('The explanation of the entity');
        expect(localizedEntityDeclaration.entityPath)
            .toEqual('entityName.cdm.json/entityName');
        expect(localizedEntityDeclaration.entityName)
            .toEqual('entityName');
        expect(cdmEntityDeclaration.entityPath)
            .toEqual('cdm:/cdmEntityName.cdm.json/cdmEntityName');
        expect(localizedEntityDeclaration.entityName)
            .toEqual('entityName');
        expect(manifest.entities.allItems.length)
            .toEqual(2);
        expect(manifest.entities.allItems[0])
            .toEqual(localizedEntityDeclaration);
        expect(manifest.entities.allItems[1])
            .toEqual(cdmEntityDeclaration);
    });
    /**
     * Tests whether the CdmEntityDeclarationDefinition can be passed directly to Manifest.Entities.Add() .
     */
    it('TestManifestCanAddEntityDeclaration', () => {
        const manifest = CdmCollectionHelperFunctions_1.generateManifest('C:\\Root\\Path');
        const entity = new internal_1.CdmEntityDefinition(manifest.ctx, 'entityName', undefined);
        CdmCollectionHelperFunctions_1.createDocumentForEntity(manifest.ctx.corpus, entity);
        const entityDeclaration = manifest.ctx.corpus.MakeObject(cdmObjectType_1.cdmObjectType.localEntityDeclarationDef, entity.entityName, false);
        entityDeclaration.entityPath = `${entity.owner.atCorpusPath}/${entity.entityName}`;
        manifest.entities.push(entityDeclaration);
        expect('local:/entityName.cdm.json/entityName')
            .toBe(entityDeclaration.entityPath);
        expect('entityName')
            .toBe(entityDeclaration.entityName);
        expect(1)
            .toBe(manifest.entities.length);
        expect(entityDeclaration)
            .toBe(manifest.entities.allItems[0]);
    });
    it('TestManifestCanAddEntityDefinition', () => {
        const manifest = CdmCollectionHelperFunctions_1.generateManifest('C:\\Root\\Path');
        const entity = new internal_1.CdmEntityDefinition(manifest.ctx, 'entityName', null);
        CdmCollectionHelperFunctions_1.createDocumentForEntity(manifest.ctx.corpus, entity);
        manifest.entities.push(entity);
        expect(1)
            .toBe(manifest.entities.length);
        expect('entityName')
            .toBe(manifest.entities.allItems[0].entityName);
    });
    /**
     * Tests whether the EntityDefinition can be passed directly to Manifest.Entities.Add().
     */
    it('TestManifestCannotAddEntityDefinitionWithoutCreatingDocument', () => {
        const cdmCorpus = new internal_1.CdmCorpusDefinition();
        cdmCorpus.storage.defaultNamespace = 'local';
        let functionWasCalled = false;
        let functionParameter1 = internal_1.cdmStatusLevel.info;
        let functionParameter2;
        cdmCorpus.setEventCallback((statusLevel, message1) => {
            functionWasCalled = true;
            functionParameter1 = statusLevel;
            functionParameter2 = message1;
        });
        cdmCorpus.storage.mount('local', new Storage_1.LocalAdapter('C:\\Root\\Path'));
        const manifest = new internal_1.CdmManifestDefinition(cdmCorpus.ctx, 'manifest');
        manifest.folderPath = '/';
        manifest.namespace = 'local';
        const entity = new internal_1.CdmEntityDefinition(manifest.ctx, 'entityName', undefined);
        const corpus = new internal_1.CdmCorpusDefinition();
        manifest.entities.push(entity);
        expect(functionWasCalled)
            .toBeTruthy();
        expect(internal_1.cdmStatusLevel.error)
            .toEqual(functionParameter1);
        expect(functionParameter2.indexOf('Expected entity to have an "Owner" document set. Cannot create entity declaration to add to manifest. '))
            .not
            .toBe(-1);
        expect(0)
            .toBe(manifest.entities.length);
    });
    it('TestManifestAddListOfEntityDeclaration', () => {
        const cdmCorpus = new internal_1.CdmCorpusDefinition();
        cdmCorpus.storage.defaultNamespace = 'local';
        cdmCorpus.storage.mount('local', new Storage_1.LocalAdapter('CdmCorpus/LocalPath'));
        const ctx = cdmCorpus.ctx;
        const cdmDocument = new internal_1.CdmDocumentDefinition(ctx, 'NameOfDocument');
        const collection = new internal_1.CdmEntityCollection(ctx, cdmDocument);
        const entityList = [];
        for (let i = 0; i < 2; i = i + 1) {
            const entity = new internal_1.CdmEntityDefinition(cdmCorpus.ctx, `entityName_${i}`, undefined);
            CdmCollectionHelperFunctions_1.createDocumentForEntity(cdmCorpus, entity);
            entityList.push(entity);
        }
        expect(collection.length)
            .toEqual(0);
        collection.concat(entityList);
        expect(collection.length)
            .toEqual(2);
        for (let i = 0; i < 2; i = i + 1) {
            expect(collection.allItems[i].entityName)
                .toEqual(`entityName_${i}`);
        }
    });
    it('TestCdmEntityCollectionRemoveEntityDeclarationDefinition', () => {
        const manifest = CdmCollectionHelperFunctions_1.generateManifest('C:\\Nothing');
        const entity = new internal_1.CdmEntityDefinition(manifest.ctx, 'entityName', undefined);
        CdmCollectionHelperFunctions_1.createDocumentForEntity(manifest.ctx.corpus, entity);
        const otherEntity = new internal_1.CdmEntityDefinition(manifest.ctx, 'otherEntityName', undefined);
        CdmCollectionHelperFunctions_1.createDocumentForEntity(manifest.ctx.corpus, otherEntity);
        manifest.entities.push(entity);
        manifest.entities.push(otherEntity);
        expect(manifest.entities.length)
            .toEqual(2);
        let removed = manifest.entities.remove(entity);
        expect(removed)
            .toBeTruthy();
        expect(manifest.entities.allItems.length)
            .toEqual(1);
        expect(manifest.entities.allItems[0].entityName)
            .toEqual(otherEntity.entityName);
        removed = manifest.entities.remove(entity);
        expect(removed)
            .toBeFalsy();
        expect(manifest.entities.allItems.length)
            .toEqual(1);
    });
});
//# sourceMappingURL=CdmEntityCollection.test.js.map