"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 Storage_1 = require("../../Storage");
const adlsModelJsonTestHelper_1 = require("../adlsModelJsonTestHelper");
const adlsTestHelper_1 = require("../adlsTestHelper");
const testHelper_1 = require("../testHelper");
describe('Samples.ReadLocalSaveAdlsTests', () => {
    const testsSubpath = 'Samples';
    const testName = 'TestReadLocalSaveAdls';
    let rootRelativePath;
    const sampleIt = (process.env['SAMPLE_RUNTESTS'] === '1' && process.env['ADLS_RUNTESTS'] === '1') ? it : it.skip;
    sampleIt('TestReadLocalSaveAdls', () => __awaiter(void 0, void 0, void 0, function* () {
        testHelper_1.testHelper.deleteFilesFromActualOutput(testHelper_1.testHelper.getActualOutputFolderPath(testsSubpath, testName));
        rootRelativePath = `Samples/TestReadLocalSaveAdls/${process.env['USERNAME']}_${process.env['COMPUTERNAME']}_Typescript`;
        // Modify partition.location in model.json and save it into actual output
        adlsModelJsonTestHelper_1.adlsModelJsonTestHelper.updateInputAndExpectedAndSaveToActualSubFolder(testsSubpath, testName, rootRelativePath, 'model.json', 'OrdersProductsCustomersLinked', new Date().toString());
        const cdmCorpus = setupCdmCorpus();
        yield readLocalSaveAdls(cdmCorpus);
        // Check the model.json file in ADLS and delete it.
        const actualContent = yield cdmCorpus.storage.fetchAdapter('adls')
            .readAsync('model.json');
        adlsModelJsonTestHelper_1.adlsModelJsonTestHelper.saveModelJsonToActualOutput(testsSubpath, testName, 'model.json', actualContent);
        const expectedContent = adlsModelJsonTestHelper_1.adlsModelJsonTestHelper.getExpectedFileContent(testsSubpath, testName, 'model.json');
        testHelper_1.testHelper.assertSameObjectWasSerialized(expectedContent, actualContent);
    }), 100000);
    function setupCdmCorpus() {
        // ------------------------------------------------------------------------------------------------------------
        // Instantiate corpus and set up the default namespace to be ADLS
        const cdmCorpus = new internal_1.CdmCorpusDefinition();
        cdmCorpus.storage.defaultNamespace = 'local';
        // ------------------------------------------------------------------------------------------------------------
        // Set up adapters for managing access to local FS, remote and ADLS locations
        // Fake cdm, normaly use the CDM Standards adapter
        // Mount it as the 'cdm' device, not the default so must use "cdm:/folder" to get there
        cdmCorpus.storage.mount('cdm', new Storage_1.LocalAdapter(testHelper_1.testHelper.sampleSchemaFolderPath));
        cdmCorpus.storage.mount('local', new Storage_1.LocalAdapter(adlsModelJsonTestHelper_1.adlsModelJsonTestHelper.getActualSubFolderPath(testsSubpath, testName, adlsModelJsonTestHelper_1.adlsModelJsonTestHelper.inputFolderName)));
        // Example how to mount to the ADLS - make sure the hostname and root entered here are also changed
        // in the model.json file we load in the next section
        cdmCorpus.storage.mount('adls', adlsTestHelper_1.adlsTestHelper.createAdapterWithClientId(rootRelativePath));
        return cdmCorpus;
    }
    function readLocalSaveAdls(cdmCorpus) {
        return __awaiter(this, void 0, void 0, function* () {
            // ------------------------------------------------------------------------------------------------------------
            // Load a model.json file from local FS
            const manifest = yield cdmCorpus.fetchObjectAsync('local:/model.json');
            // ------------------------------------------------------------------------------------------------------------
            // Explore entities and partitions defined in the model
            console.log('Listing entity declarations:');
            for (const decl of manifest.entities.allItems) {
                console.log(' ' + decl.entityName);
                if (decl.objectType === internal_1.cdmObjectType.localEntityDeclarationDef) {
                    for (const dataPart of decl.dataPartitions.allItems) {
                        console.log(' ' + dataPart.location);
                    }
                }
            }
            // ------------------------------------------------------------------------------------------------------------
            // Make changes to the model
            // Create a new document where the new entity's definition will be stored
            const newEntityDoc = cdmCorpus.MakeObject(internal_1.cdmObjectType.documentDef, 'NewEntity.cdm.json', false);
            newEntityDoc.imports.push('cdm:/foundations.cdm.json');
            cdmCorpus.storage.fetchRootFolder('local').documents.push(newEntityDoc);
            const newEntity = newEntityDoc.definitions.push(internal_1.cdmObjectType.entityDef, 'NewEntity');
            // Define new string attribute and add it to the entity definition
            const newAttribute = cdmCorpus.MakeObject(internal_1.cdmObjectType.typeAttributeDef, 'NewAttribute', false);
            newAttribute.dataFormat = internal_1.cdmDataFormat.string;
            newEntity.attributes.push(newAttribute);
            // Create a local declaration of the entity and point it to the new entity document
            const newEntityDecl = manifest.entities.push(newEntity);
            // Define a partition and add it to the local declaration
            const newPartition = cdmCorpus.MakeObject(internal_1.cdmObjectType.dataPartitionDef, 'NewPartition', false);
            newPartition.location = 'adls:/NewPartition.csv';
            newEntityDecl.dataPartitions.push(newPartition);
            // ------------------------------------------------------------------------------------------------------------
            // Save the file to ADLSg2 - we instruct that by changing the folio's namespace to 'adls'
            const adlsFolder = cdmCorpus.storage.fetchRootFolder('adls');
            adlsFolder.documents.push(manifest);
            yield manifest.saveAsAsync('model.json', true);
        });
    }
});
//# sourceMappingURL=ReadLocalSaveAdls.test.js.map