@@ -6,6 +6,9 @@ import * as JsonSchema from '../src/languageservice/jsonSchema07';
66import fs = require( 'fs' ) ;
77import url = require( 'url' ) ;
88import path = require( 'path' ) ;
9+ import { SchemaModification , MODIFICATION_ACTIONS , SchemaDeletions } from '../src/languageservice/apis/schemaModification' ;
10+ import { KUBERNETES_SCHEMA_URL } from '../src/languageservice/utils/kubernetesResolver' ;
11+ import { XHRResponse , xhr } from 'request-light' ;
912
1013const fixtureDocuments = {
1114 'http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json' : 'deploymentTemplate.json' ,
@@ -41,6 +44,13 @@ const workspaceContext = {
4144 url . resolve ( resource , relativePath )
4245} ;
4346
47+ const schemaRequestServiceForURL = ( uri : string ) : Thenable < string > => {
48+ const headers = { 'Accept-Encoding' : 'gzip, deflate' } ;
49+ return xhr ( { url : uri , followRedirects : 5 , headers } ) . then ( response =>
50+ response . responseText , ( error : XHRResponse ) =>
51+ Promise . reject ( error . responseText || error . toString ( ) ) ) ;
52+ } ;
53+
4454suite ( 'JSON Schema' , ( ) => {
4555 test ( 'Resolving $refs' , function ( testDone ) {
4656 const service = new SchemaService . YAMLSchemaService ( requestServiceMock , workspaceContext ) ;
@@ -319,5 +329,158 @@ suite('JSON Schema', () => {
319329 assert . notEqual ( schema , undefined ) ;
320330 testDone ( ) ;
321331 } ) ;
332+ test ( 'Modifying schema' , async ( ) => {
333+ const service = new SchemaService . JSONSchemaService ( requestServiceMock , workspaceContext ) ;
334+ service . setSchemaContributions ( {
335+ schemas : {
336+ 'https://myschemastore/main/schema1.json' : {
337+ type : 'object' ,
338+ properties : {
339+ apiVersion : {
340+ type : 'string' ,
341+ enum : [
342+ 'v1'
343+ ]
344+ } ,
345+ kind : {
346+ type : 'string' ,
347+ enum : [
348+ 'Pod'
349+ ]
350+ }
351+ }
352+ }
353+ }
354+ } ) ;
355+
356+ const schemaModification = new SchemaModification ( ) ;
357+ await schemaModification . addContent ( service , {
358+ action : MODIFICATION_ACTIONS . add ,
359+ path : 'properties/apiVersion' ,
360+ key : 'enum' ,
361+ content : [
362+ 'v2' ,
363+ 'v3' ,
364+ ] ,
365+ schema : 'https://myschemastore/main/schema1.json'
366+ } ) ;
367+
368+ const fs = await service . getResolvedSchema ( 'https://myschemastore/main/schema1.json' ) ;
369+ assert . deepEqual ( fs . schema . properties [ 'apiVersion' ] , {
370+ type : 'string' ,
371+ enum : [ 'v2' , 'v3' ]
372+ } ) ;
373+ assert . deepEqual ( fs . schema . properties [ 'kind' ] , {
374+ type : 'string' ,
375+ enum : [ 'Pod' ]
376+ } ) ;
377+ } ) ;
378+
379+ test ( 'Deleting schema' , async ( ) => {
380+ const service = new SchemaService . JSONSchemaService ( requestServiceMock , workspaceContext ) ;
381+ service . setSchemaContributions ( {
382+ schemas : {
383+ 'https://myschemastore/main/schema1.json' : {
384+ type : 'object' ,
385+ properties : {
386+ apiVersion : {
387+ type : 'string' ,
388+ enum : [
389+ 'v1'
390+ ]
391+ } ,
392+ kind : {
393+ type : 'string' ,
394+ enum : [
395+ 'Pod'
396+ ]
397+ }
398+ }
399+ }
400+ }
401+ } ) ;
402+
403+ const schemaModification = new SchemaModification ( ) ;
404+ await schemaModification . deleteContent ( service , {
405+ action : MODIFICATION_ACTIONS . delete ,
406+ path : 'properties' ,
407+ key : 'apiVersion' ,
408+ schema : 'https://myschemastore/main/schema1.json'
409+ } as SchemaDeletions ) ;
410+
411+ const fs = await service . getResolvedSchema ( 'https://myschemastore/main/schema1.json' ) ;
412+ assert . notDeepEqual ( fs . schema . properties [ 'apiVersion' ] , {
413+ type : 'string' ,
414+ enum : [ 'v2' , 'v3' ]
415+ } ) ;
416+ assert . equal ( fs . schema . properties [ 'apiVersion' ] , undefined ) ;
417+ assert . deepEqual ( fs . schema . properties [ 'kind' ] , {
418+ type : 'string' ,
419+ enum : [ 'Pod' ]
420+ } ) ;
421+ } ) ;
422+
423+ test ( 'Modifying schema works with kubernetes resolution' , async ( ) => {
424+ const service = new SchemaService . JSONSchemaService ( schemaRequestServiceForURL , workspaceContext ) ;
425+ service . registerExternalSchema ( KUBERNETES_SCHEMA_URL ) ;
426+
427+ const schemaModification = new SchemaModification ( ) ;
428+ await schemaModification . addContent ( service , {
429+ action : MODIFICATION_ACTIONS . add ,
430+ path : 'oneOf/0/properties/kind' ,
431+ key : 'enum' ,
432+ content : [
433+ 'v2' ,
434+ 'v3' ,
435+ ] ,
436+ schema : KUBERNETES_SCHEMA_URL
437+ } ) ;
438+
439+ const fs = await service . getResolvedSchema ( KUBERNETES_SCHEMA_URL ) ;
440+ assert . deepEqual ( fs . schema . oneOf [ 0 ] . properties [ 'kind' ] [ 'enum' ] , [ 'v2' , 'v3' ] ) ;
441+ } ) ;
442+
443+ test ( 'Deleting schema works with Kubernetes resolution' , async ( ) => {
444+ const service = new SchemaService . JSONSchemaService ( schemaRequestServiceForURL , workspaceContext ) ;
445+ service . registerExternalSchema ( KUBERNETES_SCHEMA_URL ) ;
446+
447+ const schemaModification = new SchemaModification ( ) ;
448+ await schemaModification . deleteContent ( service , {
449+ action : MODIFICATION_ACTIONS . delete ,
450+ path : 'oneOf/0/properties/kind' ,
451+ key : 'enum' ,
452+ schema : KUBERNETES_SCHEMA_URL
453+ } ) ;
454+
455+ const fs = await service . getResolvedSchema ( KUBERNETES_SCHEMA_URL ) ;
456+ assert . equal ( fs . schema . oneOf [ 0 ] . properties [ 'kind' ] [ 'enum' ] , undefined ) ;
457+ } ) ;
458+
459+ test ( 'Adding a brand new schema' , async ( ) => {
460+ const service = new SchemaService . JSONSchemaService ( schemaRequestServiceForURL , workspaceContext ) ;
461+ service . saveSchema ( 'hello_world' , {
462+ enum : [
463+ 'test1' ,
464+ 'test2'
465+ ]
466+ } ) ;
467+
468+ const hello_world_schema = await service . getResolvedSchema ( 'hello_world' ) ;
469+ assert . deepEqual ( hello_world_schema . schema . enum , [ 'test1' , 'test2' ] ) ;
470+ } ) ;
471+
472+ test ( 'Deleting an existing schema' , async ( ) => {
473+ const service = new SchemaService . JSONSchemaService ( schemaRequestServiceForURL , workspaceContext ) ;
474+ service . saveSchema ( 'hello_world' , {
475+ enum : [
476+ 'test1' ,
477+ 'test2'
478+ ]
479+ } ) ;
480+
481+ await service . deleteSchema ( 'hello_world' ) ;
482+
483+ const hello_world_schema = await service . getResolvedSchema ( 'hello_world' ) ;
484+ assert . equal ( hello_world_schema , null ) ;
322485 } ) ;
323486} ) ;
0 commit comments