Skip to content

Commit

Permalink
Merge pull request #3507 from GoogleCloudPlatform/create-doc
Browse files Browse the repository at this point in the history
feat: Create document sample
  • Loading branch information
holtskinner authored Sep 20, 2023
2 parents f655518 + 2270f0f commit af7e69d
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 6 deletions.
7 changes: 4 additions & 3 deletions document-warehouse/create-document-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ 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 = {};

// Document Schema
const documentSchema = {};
documentSchema.displayName = 'My Test Schema';
documentSchema.propertyDefinitions = [];
documentSchema.propertyDefinitions = [propertyDefinition];

request.documentSchema = documentSchema;

Expand All @@ -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}`)
);
}
Expand Down
103 changes: 103 additions & 0 deletions document-warehouse/create-document.js
Original file line number Diff line number Diff line change
@@ -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;
});
7 changes: 4 additions & 3 deletions document-warehouse/test/document-schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 () => {
Expand Down
56 changes: 56 additions & 0 deletions document-warehouse/test/document.test.js
Original file line number Diff line number Diff line change
@@ -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));
});
});

0 comments on commit af7e69d

Please sign in to comment.