Add code for data format webtool
[dcaegen2/platform/cli.git] / dcaedftool / src / app / df-jsoninput.component.spec.ts
1 // org.onap.dcae\r
2 // ============LICENSE_START====================================================\r
3 // Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.\r
4 // =============================================================================\r
5 // Licensed under the Apache License, Version 2.0 (the "License");\r
6 // you may not use this file except in compliance with the License.\r
7 // You may obtain a copy of the License at\r
8 //\r
9 //     http://www.apache.org/licenses/LICENSE-2.0\r
10 //\r
11 // Unless required by applicable law or agreed to in writing, software\r
12 // distributed under the License is distributed on an "AS IS" BASIS,\r
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14 // See the License for the specific language governing permissions and\r
15 // limitations under the License.\r
16 // ============LICENSE_END======================================================\r
17 //\r
18 // ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
19 import { NgModule } from '@angular/core';\r
20 import { BrowserModule } from '@angular/platform-browser';\r
21 import {HttpModule} from '@angular/http';  // for future\r
22 import {FormsModule, ReactiveFormsModule} from '@angular/forms'; // for future\r
23 import {BrowserAnimationsModule} from '@angular/platform-browser/animations'; // for future\r
24 import { MaterialModule } from '@angular/material';\r
25 import { FlexLayoutModule } from '@angular/flex-layout';\r
26 import { RouterModule, Routes } from '@angular/router';\r
27 import { APP_BASE_HREF } from '@angular/common';\r
28 \r
29 import {ValidateMetaSchemaService} from './validate-metaschema.service';\r
30 import {ValidateJSONService} from './validate-json.service';\r
31 import {MetaSchemaService} from './metaschema.service';\r
32 import {DFJSONInputComponent} from './df-jsoninput.component';\r
33 \r
34 \r
35 \r
36 import { async, ComponentFixture, TestBed } from '@angular/core/testing';\r
37 import { By } from '@angular/platform-browser';\r
38 import { DebugElement } from '@angular/core';\r
39 \r
40 describe('DFSJSONInputComponent Tests', function () {\r
41   let de: DebugElement;\r
42   let comp: DFJSONInputComponent;\r
43   let fixture: ComponentFixture<DFJSONInputComponent>;\r
44 \r
45   beforeEach(async(() => {\r
46     TestBed.configureTestingModule({\r
47       imports:      [  BrowserModule,\r
48         BrowserAnimationsModule,\r
49         FormsModule,\r
50         HttpModule,\r
51         ReactiveFormsModule,\r
52         FlexLayoutModule,\r
53         MaterialModule,\r
54 \r
55         ],\r
56       providers: [{provide: APP_BASE_HREF, useValue: '/'},\r
57       ValidateMetaSchemaService,\r
58       ValidateJSONService,\r
59       MetaSchemaService\r
60                   ],\r
61       declarations: [\r
62                       DFJSONInputComponent,\r
63 \r
64                    ]\r
65     })\r
66     .compileComponents();\r
67   }));\r
68 \r
69   beforeEach(() => {\r
70     fixture = TestBed.createComponent(DFJSONInputComponent);\r
71     comp = fixture.componentInstance;\r
72     //de = fixture.debugElement.query(By.css('h1'));\r
73   });\r
74 \r
75   it('should create component', () => expect(comp).toBeDefined() );\r
76 \r
77  it('should have no metaschema display as default', () => {\r
78       expect(comp.metaButton).toMatch('Display MetaSchema');\r
79   });\r
80   it('should  metaschema display after toggle', () => {\r
81        comp.toggleMetaSchema();\r
82        expect(comp.metaButton).toMatch('Hide MetaSchema');\r
83    });\r
84    it('should validate valid schema', () => {\r
85      let ev = {target: {name: "test", value: '{"self": {  "name": "CUDA Simple JSON Example","version": "1.0.0","description": "An example of unnested JSON schema for CUDA Input and output"},"dataformatversion": "1.0.0",  "jsonschema": {  "$schema": "http://json-schema.org/draft-04/schema#","type": "object",  "properties": {"raw-text": {"type": "string"  }  },  "required": ["raw-text"],  "additionalProperties": false }}'}};\r
86      comp.doDFSchemaChange(ev);\r
87      expect(comp.schemaMsg).toMatch("Valid Data Format Schema") ;\r
88     });\r
89     it('should not validate an invalid schema', () => {\r
90       let ev = {target: {name: "test", value: '{"self": {  "name": "CUDA Simple JSON Example","version": "1.0.0","description": "An example of unnested JSON schema for CUDA Input and output"},  "jsonschema": {  "$schema": "http://json-schema.org/draft-04/schema#","type": "object",  "properties": {"raw-text": {"type": "string"  }  },  "required": ["raw-text"],  "additionalProperties": false  }}'}};\r
91       comp.doDFSchemaChange(ev);\r
92       expect(comp.schemaMsg).toMatch("Invalid Data Format Schema") ;\r
93      });\r
94      it('schema must have JSON schema ', () => {\r
95        let ev = {target: {name: "test", value: '{"self": {  "name": "CUDA Simple JSON Example","version": "1.0.0","description": "An example of unnested JSON schema for CUDA Input and output"},  "json1schema": {  "$schema": "http://json-schema.org/draft-04/schema#","type": "object",  "properties": {"raw-text": {"type": "string"  }  },  "required": ["raw-text"],  "additionalProperties": false  }}'}};\r
96        comp.doDFSchemaChange(ev);\r
97        expect(comp.schemaMsg).toMatch("Invalid Schema - must specify jsonschema") ;\r
98       });\r
99       it('JSON schema must be 04', () => {\r
100         let ev = {target: {name: "test", value: '{"self": {  "name": "CUDA Simple JSON Example","version": "1.0.0","description": "An example of unnested JSON schema for CUDA Input and output"},"dataformatversion": "1.0.0",  "jsonschema": {  "$schema": "http://json-schema.org/draft-05/schema#","type": "object",  "properties": {"raw-text": {"type": "string"  }  },  "required": ["raw-text"],  "additionalProperties": false }}'}};\r
101         comp.doDFSchemaChange(ev);\r
102         expect(comp.schemaMsg).toMatch(/Invalid JSON Schema Data Format -  jsonschema\$schema version must be 04/) ;\r
103        });\r
104      it('should fail if JSON input and no schema', () => {\r
105        let ev = {target: {name: "test", value: '{}'}};\r
106        comp.doDFJSONChange(ev);\r
107        expect(comp.jsonMsg).toMatch("Enter a Valid Schema") ;\r
108       });\r
109       it('should validate if JSON input and match valid schema', () => {\r
110         let ev = {target: {name: "test", value: '{"self": {  "name": "CUDA Simple JSON Example","version": "1.0.0","description": "An example of unnested JSON schema for CUDA Input and output"},"dataformatversion": "1.0.0",  "jsonschema": {  "$schema": "http://json-schema.org/draft-04/schema#","type": "object",  "properties": {"raw-text": {"type": "string"  }  },  "required": ["raw-text"],  "additionalProperties": false }}'}};\r
111         comp.doDFSchemaChange(ev);\r
112         let jev = {target: {name: "test", value: '{"raw-text": "test"}'}};\r
113         comp.doDFJSONChange(jev);\r
114         expect(comp.jsonMsg).toMatch("JSON Input Validated") ;\r
115        });\r
116        it('should not validate if JSON input does not match valid schema', () => {\r
117          let ev = {target: {name: "test", value: '{"self": {  "name": "CUDA Simple JSON Example","version": "1.0.0","description": "An example of unnested JSON schema for CUDA Input and output"},"dataformatversion": "1.0.0",  "jsonschema": {  "$schema": "http://json-schema.org/draft-04/schema#","type": "object",  "properties": {"raw-text": {"type": "string"  }  },  "required": ["raw-text"],  "additionalProperties": false }}'}};\r
118          comp.doDFSchemaChange(ev);\r
119          let jev = {target: {name: "test", value: '{"badraw-text": "test"}'}};\r
120          comp.doDFJSONChange(jev);\r
121          expect(comp.jsonMsg).toMatch("JSON Input does not match Schema") ;\r
122         });\r
123         it('should recover if schema becomes valid', () => {\r
124           let ev = {target: {name: "test", value: '{"self": {  "name": "CUDA Simple JSON Example","version": "1.0.0","description": "An example of unnested JSON schema for CUDA Input and output"},  "jsonschema": {  "$schema": "http://json-schema.org/draft-04/schema#","type": "object",  "properties": {"raw-text": {"type": "string"  }  },  "required": ["raw-text"],  "additionalProperties": false  }}'}};\r
125           comp.doDFSchemaChange(ev);\r
126           expect(comp.schemaMsg).toMatch("Invalid Data Format Schema") ;\r
127           let jev = {target: {name: "test", value: '{"raw-text": "test"}'}};\r
128           comp.doDFJSONChange(jev);\r
129           let ev2 = {target: {name: "test", value: '{"self": {  "name": "CUDA Simple JSON Example","version": "1.0.0","description": "An example of unnested JSON schema for CUDA Input and output"},"dataformatversion": "1.0.0",  "jsonschema": {  "$schema": "http://json-schema.org/draft-04/schema#","type": "object",  "properties": {"raw-text": {"type": "string"  }  },  "required": ["raw-text"],  "additionalProperties": false }}'}};\r
130           comp.doDFSchemaChange(ev2);\r
131           expect(comp.jsonMsg).toMatch("JSON Input Validated") ;\r
132           expect(comp.schemaMsg).toMatch("Valid Data Format Schema") ;\r
133          });\r
134          it('should fail if schema becomes invalid', () => {\r
135            let ev = {target: {name: "test", value: '{"self": {  "name": "CUDA Simple JSON Example","version": "1.0.0","description": "An example of unnested JSON schema for CUDA Input and output"},"dataformatversion": "1.0.0",  "jsonschema": {  "$schema": "http://json-schema.org/draft-04/schema#","type": "object",  "properties": {"raw-text": {"type": "string"  }  },  "required": ["raw-text"],  "additionalProperties": false }}'}};\r
136            comp.doDFSchemaChange(ev);\r
137            let jev = {target: {name: "test", value: '{"raw-text": "test"}'}};\r
138            comp.doDFJSONChange(jev);\r
139            ev = {target: {name: "test", value: '{"self": {  "name": "CUDA Simple JSON Example","version": "1.0.0","description": "An example of unnested JSON schema for CUDA Input and output"},"dataformatversion": "1.0.0",  "json1schema": {  "$schema": "http://json-schema.org/draft-04/schema#","type": "object",  "properties": {"raw-text": {"type": "string"  }  },  "required": ["raw-text"],  "additionalProperties": false }}'}};\r
140            comp.doDFSchemaChange(ev);\r
141            expect(comp.jsonMsg).toMatch("Enter a valid Schema") ;\r
142           });\r
143           it('should  validate if JSON input is fixed to match valid schema', () => {\r
144             let ev = {target: {name: "test", value: '{"self": {  "name": "CUDA Simple JSON Example","version": "1.0.0","description": "An example of unnested JSON schema for CUDA Input and output"},"dataformatversion": "1.0.0",  "jsonschema": {  "$schema": "http://json-schema.org/draft-04/schema#","type": "object",  "properties": {"raw-text": {"type": "string"  }  },  "required": ["raw-text"],  "additionalProperties": false }}'}};\r
145             comp.doDFSchemaChange(ev);\r
146             let jev = {target: {name: "test", value: '{"badraw-text": "test"}'}};\r
147             comp.doDFJSONChange(jev);\r
148             expect(comp.jsonMsg).toMatch("JSON Input does not match Schema") ;\r
149             jev = {target: {name: "test", value: '{"raw-text": "test"}'}};\r
150             comp.doDFJSONChange(jev);\r
151             expect(comp.jsonMsg).toMatch("JSON Input Validated") ;\r
152            });\r
153 });\r