Skip to content

Commit b07f41a

Browse files
author
David Hasani
committed
add unit tests
1 parent a3659a0 commit b07f41a

File tree

3 files changed

+141
-47
lines changed

3 files changed

+141
-47
lines changed

packages/core/src/amazonqGumby/chat/controller/controller.ts

Lines changed: 14 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
validateCanCompileProject,
3333
setMaven,
3434
getValidSQLConversionCandidateProjects,
35+
validateSQLMetadataFile,
3536
} from '../../../codewhisperer/commands/startTransformByQ'
3637
import { DB, JDKVersion, transformByQState } from '../../../codewhisperer/models/model'
3738
import {
@@ -216,7 +217,6 @@ export class GumbyController {
216217

217218
// if previous transformation was already running
218219
switch (this.sessionStorage.getSession().conversationState) {
219-
// TODO: figure out if/when to set these states for SQL conversions
220220
case ConversationState.JOB_SUBMITTED:
221221
this.messenger.sendAsyncEventProgress(
222222
message.tabID,
@@ -335,7 +335,10 @@ export class GumbyController {
335335
userChoice: 'Cancel',
336336
result: MetadataResult.Pass,
337337
})
338-
this.messenger.sendJobFinishedMessage(message.tabID, CodeWhispererConstants.jobCancelledChatMessage)
338+
this.transformationFinished({
339+
message: CodeWhispererConstants.jobCancelledChatMessage,
340+
tabID: message.tabID,
341+
})
339342
break
340343
case ButtonActions.CONFIRM_SKIP_TESTS_FORM:
341344
await this.handleSkipTestsSelection(message)
@@ -529,46 +532,6 @@ export class GumbyController {
529532
await this.prepareLanguageUpgradeProjectForSubmission(message)
530533
}
531534

532-
private async validateMetadataFile(sctRulesData: any, message: any) {
533-
try {
534-
const sctRules = JSON.parse(sctRulesData)
535-
const sourceDB = sctRules['rules'][0]['locator']['sourceVendor'] as string
536-
const targetDB = sctRules['rules'][0]['locator']['targetVendor'] as string
537-
if (sourceDB.toUpperCase() !== DB.ORACLE) {
538-
this.messenger.sendJobFinishedMessage(
539-
message.tabID,
540-
CodeWhispererConstants.invalidMetadataFileUnsupportedSourceVendor(sourceDB)
541-
)
542-
return false
543-
} else if (
544-
targetDB.toUpperCase() !== DB.AURORA_POSTGRESQL &&
545-
targetDB.toUpperCase() !== DB.RDS_POSTGRESQL
546-
) {
547-
this.messenger.sendJobFinishedMessage(
548-
message.tabID,
549-
CodeWhispererConstants.invalidMetadataFileUnsupportedTargetVendor(targetDB)
550-
)
551-
return false
552-
} else if (targetDB.toUpperCase() !== transformByQState.getTargetDB()) {
553-
this.messenger.sendJobFinishedMessage(
554-
message.tabID,
555-
CodeWhispererConstants.invalidMetadataFileTargetVendorMismatch(
556-
targetDB,
557-
transformByQState.getTargetDB()!
558-
)
559-
)
560-
return false
561-
}
562-
} catch (e: any) {
563-
this.messenger.sendJobFinishedMessage(
564-
message.tabID,
565-
CodeWhispererConstants.invalidMetadataFileUnknownIssueParsing
566-
)
567-
return false
568-
}
569-
return true
570-
}
571-
572535
private async processMetadataFile(message: any) {
573536
const fileUri = await vscode.window.showOpenDialog({
574537
canSelectMany: false, // Allow only one file to be selected
@@ -577,7 +540,10 @@ export class GumbyController {
577540

578541
if (!fileUri || fileUri.length === 0) {
579542
// User canceled the dialog
580-
this.messenger.sendJobFinishedMessage(message.tabID, CodeWhispererConstants.jobCancelledChatMessage)
543+
this.transformationFinished({
544+
message: CodeWhispererConstants.jobCancelledChatMessage,
545+
tabID: message.tabID,
546+
})
581547
return
582548
}
583549

@@ -588,12 +554,15 @@ export class GumbyController {
588554
const pathToRules = zipEntries.find((entry) => entry.name === 'sct-rules.json')?.entryName
589555

590556
if (!pathToRules) {
591-
this.messenger.sendJobFinishedMessage(message.tabID, CodeWhispererConstants.invalidMetadataFileNoRulesJson)
557+
this.transformationFinished({
558+
message: CodeWhispererConstants.invalidMetadataFileNoRulesJson,
559+
tabID: message.tabID,
560+
})
592561
return
593562
}
594563

595564
const sctRulesData = nodefs.readFileSync(path.join(pathToZip, pathToRules), 'utf8')
596-
const isValidSctRulesData = await this.validateMetadataFile(sctRulesData, message)
565+
const isValidSctRulesData = await validateSQLMetadataFile(sctRulesData, message)
597566
if (!isValidSctRulesData) {
598567
return
599568
}

packages/core/src/codewhisperer/commands/startTransformByQ.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,51 @@ export async function parseBuildFile() {
274274
return undefined
275275
}
276276

277+
export async function validateSQLMetadataFile(sctRulesData: any, message: any) {
278+
try {
279+
const sctRules = JSON.parse(sctRulesData)
280+
const sourceDB = sctRules['rules'][0]['locator']['sourceVendor'] as string
281+
const targetDB = sctRules['rules'][0]['locator']['targetVendor'] as string
282+
if (sourceDB.toUpperCase() !== DB.ORACLE) {
283+
transformByQState
284+
.getChatControllers()
285+
?.transformationFinished.fire({
286+
message: CodeWhispererConstants.invalidMetadataFileUnsupportedSourceVendor(sourceDB),
287+
tabID: message.tabID,
288+
})
289+
return false
290+
} else if (targetDB.toUpperCase() !== DB.AURORA_POSTGRESQL && targetDB.toUpperCase() !== DB.RDS_POSTGRESQL) {
291+
transformByQState
292+
.getChatControllers()
293+
?.transformationFinished.fire({
294+
message: CodeWhispererConstants.invalidMetadataFileUnsupportedTargetVendor(targetDB),
295+
tabID: message.tabID,
296+
})
297+
return false
298+
} else if (targetDB.toUpperCase() !== transformByQState.getTargetDB()) {
299+
transformByQState
300+
.getChatControllers()
301+
?.transformationFinished.fire({
302+
message: CodeWhispererConstants.invalidMetadataFileTargetVendorMismatch(
303+
targetDB,
304+
transformByQState.getTargetDB()!
305+
),
306+
tabID: message.tabID,
307+
})
308+
return false
309+
}
310+
} catch (e: any) {
311+
transformByQState
312+
.getChatControllers()
313+
?.transformationFinished.fire({
314+
message: CodeWhispererConstants.invalidMetadataFileUnknownIssueParsing,
315+
tabID: message.tabID,
316+
})
317+
return false
318+
}
319+
return true
320+
}
321+
277322
export async function preTransformationUploadCode() {
278323
await vscode.commands.executeCommand('aws.amazonq.transformationHub.focus')
279324

packages/core/src/test/codewhisperer/commands/transformByQ.test.ts

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@ import assert, { fail } from 'assert'
77
import * as vscode from 'vscode'
88
import * as sinon from 'sinon'
99
import { makeTemporaryToolkitFolder } from '../../../shared/filesystemUtilities'
10-
import { transformByQState, TransformByQStoppedError } from '../../../codewhisperer/models/model'
11-
import { parseBuildFile, stopTransformByQ } from '../../../codewhisperer/commands/startTransformByQ'
10+
import { DB, transformByQState, TransformByQStoppedError } from '../../../codewhisperer/models/model'
11+
import {
12+
parseBuildFile,
13+
stopTransformByQ,
14+
validateSQLMetadataFile,
15+
} from '../../../codewhisperer/commands/startTransformByQ'
1216
import { HttpResponse } from 'aws-sdk'
1317
import * as codeWhisperer from '../../../codewhisperer/client/codewhisperer'
1418
import * as CodeWhispererConstants from '../../../codewhisperer/models/constants'
@@ -341,4 +345,80 @@ describe('transformByQ', function () {
341345
const warningMessage = await parseBuildFile()
342346
assert.strictEqual(expectedWarning, warningMessage)
343347
})
348+
349+
it(`WHEN validateMetadataFile on sct-rules.json with supported source DB and supported target DB THEN passes validation`, async function () {
350+
const sampleRules = {
351+
rules: [
352+
{
353+
name: 'Mapping rule',
354+
ruleId: '1',
355+
locator: {
356+
sourceVendor: 'ORACLE',
357+
targetVendor: 'AURORA_POSTGRESQL',
358+
},
359+
},
360+
],
361+
}
362+
const sampleRulesString = JSON.stringify(sampleRules)
363+
transformByQState.setTargetDB(DB.AURORA_POSTGRESQL)
364+
const isValidMetadata = await validateSQLMetadataFile(sampleRulesString, { tabID: 'abc123' })
365+
assert.strictEqual(isValidMetadata, true)
366+
})
367+
368+
it(`WHEN validateMetadataFile on sct-rules.json with unsupported source DB and supported target DB THEN fails validation`, async function () {
369+
const sampleRules = {
370+
rules: [
371+
{
372+
name: 'Mapping rule',
373+
ruleId: '1',
374+
locator: {
375+
sourceVendor: 'NOT_ORACLE',
376+
targetVendor: 'AURORA_POSTGRESQL',
377+
},
378+
},
379+
],
380+
}
381+
const sampleRulesString = JSON.stringify(sampleRules)
382+
transformByQState.setTargetDB(DB.AURORA_POSTGRESQL)
383+
const isValidMetadata = await validateSQLMetadataFile(sampleRulesString, { tabID: 'abc123' })
384+
assert.strictEqual(isValidMetadata, false)
385+
})
386+
387+
it(`WHEN validateMetadataFile on sct-rules.json with supported source DB and unsupported target DB THEN fails validation`, async function () {
388+
const sampleRules = {
389+
rules: [
390+
{
391+
name: 'Mapping rule',
392+
ruleId: '1',
393+
locator: {
394+
sourceVendor: 'ORACLE',
395+
targetVendor: 'NOT_AURORA_POSTGRESQL',
396+
},
397+
},
398+
],
399+
}
400+
const sampleRulesString = JSON.stringify(sampleRules)
401+
transformByQState.setTargetDB(DB.AURORA_POSTGRESQL) // even though this is set to something valid, validation should fail
402+
const isValidMetadata = await validateSQLMetadataFile(sampleRulesString, { tabID: 'abc123' })
403+
assert.strictEqual(isValidMetadata, false)
404+
})
405+
406+
it(`WHEN validateMetadataFile on sct-rules.json with supported source DB and supported target DB, but user selected a different target DB in chat form, THEN fails validation`, async function () {
407+
const sampleRules = {
408+
rules: [
409+
{
410+
name: 'Mapping rule',
411+
ruleId: '1',
412+
locator: {
413+
sourceVendor: 'ORACLE',
414+
targetVendor: 'AURORA_POSTGRESQL',
415+
},
416+
},
417+
],
418+
}
419+
const sampleRulesString = JSON.stringify(sampleRules)
420+
transformByQState.setTargetDB(DB.RDS_POSTGRESQL) // different from 'AURORA_POSTGRESQL' above, so should fail validation
421+
const isValidMetadata = await validateSQLMetadataFile(sampleRulesString, { tabID: 'abc123' })
422+
assert.strictEqual(isValidMetadata, false)
423+
})
344424
})

0 commit comments

Comments
 (0)