diff --git a/document-warehouse/create-document-schema.js b/document-warehouse/create-document-schema.js index dac74009db..38b542cb83 100644 --- a/document-warehouse/create-document-schema.js +++ b/document-warehouse/create-document-schema.js @@ -41,7 +41,7 @@ async function main( // Property Definition const propertyDefinition = {}; - propertyDefinition.name = 'schema property 1'; // Must be unique within a document schema (case insensitive) + propertyDefinition.name = 'testPropertyDefinitionName'; // Must be unique within a document schema (case insensitive) propertyDefinition.displayName = 'searchable text'; propertyDefinition.isSearchable = true; propertyDefinition.textTypeOptions = {}; @@ -49,7 +49,7 @@ async function main( // Document Schema const documentSchema = {}; documentSchema.displayName = 'My Test Schema'; - documentSchema.propertyDefinitions = []; + documentSchema.propertyDefinitions = [propertyDefinition]; request.documentSchema = documentSchema; @@ -62,7 +62,8 @@ async function main( // Print out response response.then( - result => console.log(`${JSON.stringify(result)}`), + result => + console.log(`Document Schema Created: ${JSON.stringify(result)}`), error => console.log(`${error}`) ); } diff --git a/document-warehouse/create-document.js b/document-warehouse/create-document.js new file mode 100644 index 0000000000..03cfab66cd --- /dev/null +++ b/document-warehouse/create-document.js @@ -0,0 +1,103 @@ +/** Copyright 2023 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; +async function main( + projectNumber = 'YOUR_PROJECT_NUMBER', + location = 'YOUR_PROJECT_LOCATION', + userId = 'user:xxx@example.com' +) { + // [START contentwarehouse_quickstart] + + /** + * TODO(developer): Uncomment these variables before running the sample. + * const projectNumber = 'YOUR_PROJECT_NUMBER'; + * const location = 'YOUR_PROJECT_LOCATION'; // Format is 'us' or 'eu' + * const userId = 'user:xxx@example.com'; // Format is "user:xxx@example.com" + */ + + // Import from google cloud + const {DocumentSchemaServiceClient, DocumentServiceClient} = + require('@google-cloud/contentwarehouse').v1; + + // Create service client + const schemaClient = new DocumentSchemaServiceClient(); + const serviceClient = new DocumentServiceClient(); + + // Get Document Schema + async function quickstart() { + // Initialize request argument(s) + const schemaRequest = {}; + const documentRequest = {}; + + // The full resource name of the location, e.g.: + // projects/{project_number}/locations/{location} + const parent = `projects/${projectNumber}/locations/${location}`; + schemaRequest.parent = parent; + documentRequest.parent = parent; + + // Property Definition + const propertyDefinition = {}; + propertyDefinition.name = 'testPropertyDefinitionName'; // Must be unique within a document schema (case insensitive) + propertyDefinition.displayName = 'searchable text'; + propertyDefinition.isSearchable = true; + propertyDefinition.textTypeOptions = {}; + + // Document Schema + const documentSchemaRequest = {}; + documentSchemaRequest.displayName = 'My Test Schema'; + documentSchemaRequest.propertyDefinitions = [propertyDefinition]; + + schemaRequest.documentSchema = documentSchemaRequest; + + // Create Document Schema + const documentSchema = + await schemaClient.createDocumentSchema(schemaRequest); + + // Property Value Definition + const documentProperty = {}; + documentProperty.name = propertyDefinition.name; + documentProperty.textValues = {values: ['GOOG']}; + + // Document Definition + const document = {}; + document.displayName = 'My Test Document'; + document.documentSchemaName = documentSchema[0].name; + document.plainText = "This is a sample of a document's text."; + document.properties = [documentProperty]; + + documentRequest.document = document; + + // Metadata Definition + documentRequest.requestMetadata = {userInfo: {id: userId}}; + + // Make Request + const response = serviceClient.createDocument(documentRequest); + + // Print out response + response.then( + result => console.log(`Document Created: ${JSON.stringify(result)}`), + error => console.log(`${error}`) + ); + } + + // [END contentwarehouse_quickstart] + await quickstart(); +} + +main(...process.argv.slice(2)).catch(err => { + console.error(err); + process.exitCode = 1; +}); diff --git a/document-warehouse/test/document-schema.test.js b/document-warehouse/test/document-schema.test.js index f8abbf7367..d9defa6aed 100644 --- a/document-warehouse/test/document-schema.test.js +++ b/document-warehouse/test/document-schema.test.js @@ -23,12 +23,13 @@ const {ProjectsClient} = require('@google-cloud/resource-manager').v3; const iamClient = new PoliciesClient(); const projectClient = new ProjectsClient(); +const confirmationCreate = 'Document Schema Created'; const confirmationDeleted = 'Document Schema Deleted'; const confirmationGet = 'Schema Found'; const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); -describe('Create and delete document schema', () => { +describe('Document schema tests', () => { let projectNumber; let documentSchema; let documentSchemaId; @@ -57,10 +58,10 @@ describe('Create and delete document schema', () => { const output = execSync( `node create-document-schema.js ${projectNumber} ${location}` ); - documentSchema = JSON.parse(output)[0]; + documentSchema = JSON.parse(output.slice(confirmationCreate.length + 2))[0]; getDocumentSchemaId(); - assert.notEqual(documentSchema, null); + assert(output.startsWith(confirmationCreate)); }); it('should get created document schema', async () => { diff --git a/document-warehouse/test/document.test.js b/document-warehouse/test/document.test.js new file mode 100644 index 0000000000..7c02953bbc --- /dev/null +++ b/document-warehouse/test/document.test.js @@ -0,0 +1,56 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +'use strict'; + +const {describe, it} = require('mocha'); +const assert = require('assert'); +const cp = require('child_process'); + +const {PoliciesClient} = require('@google-cloud/iam').v2; +const {ProjectsClient} = require('@google-cloud/resource-manager').v3; +const iamClient = new PoliciesClient(); +const projectClient = new ProjectsClient(); + +const confirmationCreate = 'Document Created'; + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +describe('Document tests', () => { + let projectNumber; + const location = 'us'; + const userId = + 'user:kokoro-system-test@long-door-651.iam.gserviceaccount.com'; + + async function getProjectNumber() { + const projectId = await iamClient.getProjectId(); + const request = {name: `projects/${projectId}`}; + const project = await projectClient.getProject(request); + const resources = project[0].name.toString().split('/'); + const projectNumber = resources[resources.length - 1]; + return projectNumber; + } + + before(async () => { + projectNumber = await getProjectNumber(); + }); + + it('should create a document', async () => { + const output = execSync( + `node create-document.js ${projectNumber} ${location} ${userId}` + ); + + assert(output.startsWith(confirmationCreate)); + }); +});