/
ontology-visualizer.component.spec.ts
190 lines (184 loc) · 9.32 KB
/
ontology-visualizer.component.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { waitForAsync, ComponentFixture, TestBed } from '@angular/core/testing';
import { MatRadioModule } from '@angular/material/radio';
import { MatSlideToggleModule } from '@angular/material/slide-toggle';
import { MatSliderModule } from '@angular/material/slider';
import { RouterTestingModule } from '@angular/router/testing';
import {
IHasProperty,
KnoraApiConnection,
ReadOntology,
ResourceClassDefinition,
ResourcePropertyDefinition
} from '@dasch-swiss/dsp-js';
import { AppInitService } from 'src/app/app-init.service';
import { DspApiConfigToken, DspApiConnectionToken } from 'src/app/main/declarations/dsp-api-tokens';
import { ErrorComponent } from 'src/app/main/error/error.component';
import { TestConfig } from 'test.config';
import { OntologyVisualizerComponent } from './ontology-visualizer.component';
import { VisualizerComponent } from './visualizer/visualizer.component';
describe('OntologyVisualizerComponent', () => {
let testHostComponent: OntologyVisualizerComponent;
let testHostFixture: ComponentFixture<OntologyVisualizerComponent>;
// test ontology
const testOntology = new ReadOntology();
testOntology.id = 'http://www.knora.org/ontology/0000/testontology';
// a test resource class
const testResourceClass1 = new ResourceClassDefinition();
testResourceClass1.id = 'http://www.knora.org/ontology/0000/testontology/v2#testResource1';
testResourceClass1.canBeInstantiated = true;
testResourceClass1.comment = 'first test resource class';
testResourceClass1.label = 'Test Resource 1';
testResourceClass1.propertiesList = [];
testResourceClass1.subClassOf = ['http://api.knora.org/ontology/knora-api/v2#Resource', 'http://purl.org/ontology/bibo/testResource'];
const textValueProperty: IHasProperty = {
propertyIndex: 'http://www.knora.org/ontology/0000/testontology/v2#hasText',
guiOrder: 1,
isInherited: false,
cardinality: 1
};
testResourceClass1.propertiesList.push(textValueProperty);
const textValuePropertyDefinition = new ResourcePropertyDefinition();
textValuePropertyDefinition.objectType = 'http://api.knora.org/ontology/knora-api/v2#TextValue';
textValuePropertyDefinition.label = 'testText';
textValuePropertyDefinition.id = 'http://www.knora.org/ontology/0000/testontology/v2#hasText';
textValuePropertyDefinition.subjectType = 'http://www.knora.org/ontology/0000/testontology/v2#testResource1';
testOntology.properties['http://www.knora.org/ontology/0000/testontology/v2#hasText'] = textValuePropertyDefinition;
const circularValueProperty: IHasProperty = {
propertyIndex: 'http://www.knora.org/ontology/0000/testontology/v2#hasCopy',
guiOrder: 1,
isInherited: false,
cardinality: 1
};
testResourceClass1.propertiesList.push(circularValueProperty);
const circularValuePropertyDefinition = new ResourcePropertyDefinition();
circularValuePropertyDefinition.objectType = 'http://www.knora.org/ontology/0000/testontology/v2#testResource1';
circularValuePropertyDefinition.label = 'textHasCopy';
circularValuePropertyDefinition.id = 'http://www.knora.org/ontology/0000/testontology/v2#hasCopy';
circularValuePropertyDefinition.subjectType = 'http://www.knora.org/ontology/0000/testontology/v2#testResource1';
testOntology.properties['http://www.knora.org/ontology/0000/testontology/v2#hasCopy'] = circularValuePropertyDefinition;
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [
OntologyVisualizerComponent,
VisualizerComponent,
ErrorComponent
],
imports: [
HttpClientTestingModule,
MatRadioModule,
MatSliderModule,
MatSlideToggleModule,
RouterTestingModule
],
providers: [
AppInitService,
{
provide: DspApiConfigToken,
useValue: TestConfig.ApiConfig
},
{
provide: DspApiConnectionToken,
useValue: new KnoraApiConnection(TestConfig.ApiConfig)
}
]
})
.compileComponents();
}));
beforeEach(() => {
testHostFixture = TestBed.createComponent(OntologyVisualizerComponent);
testHostComponent = testHostFixture.componentInstance;
testHostComponent.ontoClasses = [testResourceClass1];
testHostComponent.ontology = testOntology;
testHostFixture.detectChanges();
});
it('should check the component input (ontology and ontoClasses)', waitForAsync( () => {
expect(testHostComponent.ontoClasses.length).toEqual(1);
expect(testHostComponent.ontoClasses[0].propertiesList.length).toEqual(2);
expect(testHostComponent.ontology.id).toMatch('http://www.knora.org/ontology/0000/testontology');
}));
it('should get ontology prefix, resource label, and combination of both from class IRI', waitForAsync( () => {
const resLabelInfo = testHostComponent.createLabelFromIRI('http://www.knora.org/ontology/0000/testontology/v2#testResource1');
expect(resLabelInfo.ontoName).toEqual('testontology');
expect(resLabelInfo.type).toEqual('testResource1');
expect(resLabelInfo.newLabel).toEqual(resLabelInfo.ontoName + ':' + resLabelInfo.type);
}));
it('should convert resource classes defined in ontology to nodes', waitForAsync( () => {
expect(testHostComponent.nodes.length).toEqual(4);
const nodeForTestResource1 = testHostComponent.nodes[0];
expect(nodeForTestResource1['id']).toEqual('http://www.knora.org/ontology/0000/testontology/v2#testResource1');
expect(nodeForTestResource1['label']).toEqual('testontology:testResource1');
expect(nodeForTestResource1['class']).toEqual('native');
expect(nodeForTestResource1['group']).toEqual('resource');
}));
it('should convert super classes defined for resources to nodes as external resource', waitForAsync( () => {
const nodeForKnoraResource = {
'id': 'http://api.knora.org/ontology/knora-api/v2#Resource',
'label': 'knora-api:Resource',
'group': 'resource',
'class': 'external'
};
expect(testHostComponent.nodes).toContain(nodeForKnoraResource);
const nodeForbiboResource = {
'id': 'http://purl.org/ontology/bibo/testResource',
'label': 'http://purl.org/ontology/bibo/testResource',
'group': 'resource',
'class': 'external'
};
expect(testHostComponent.nodes).toContain(nodeForbiboResource);
}));
it('should check if a resource class is in nodes list with its IRI', waitForAsync( () => {
expect(testHostComponent.isInNodes('http://www.knora.org/ontology/0000/testontology/v2#testResource1')).toBeTruthy();
}));
it('should create links defining subclass relations', waitForAsync( () => {
expect(testHostComponent.links.length).toEqual(4);
const subClassofKnoraAPiResource = {
'source': 'http://www.knora.org/ontology/0000/testontology/v2#testResource1',
'target': 'http://api.knora.org/ontology/knora-api/v2#Resource',
'label': 'subClassOf',
'rotation': 0,
'curvature': 0
};
const subClassofBiboResource = {
'source': 'http://www.knora.org/ontology/0000/testontology/v2#testResource1',
'target': 'http://purl.org/ontology/bibo/testResource',
'label': 'subClassOf',
'rotation': 0,
'curvature': 0
};
expect(testHostComponent.links).toContain(subClassofBiboResource);
expect(testHostComponent.links).toContain(subClassofKnoraAPiResource);
}));
it('should convert literal object type to a node', waitForAsync( () => {
const targetnodeID = testHostComponent.addObjectTypeToNodes('http://api.knora.org/ontology/knora-api/v2#TextValue',
'http://www.knora.org/ontology/0000/testontology/v2#hasText', 'Test Resource 1');
expect(targetnodeID).toEqual('Test Resource 1_hasText');
const textValueNode = {
'id': 'Test Resource 1_hasText',
'label': 'knora-api:TextValue',
'group': 'literal',
'class': 'TextValue'
};
expect(testHostComponent.nodes).toContain(textValueNode);
}));
it('should convert property to link', waitForAsync( () => {
const textValueLink = {
'source': 'http://www.knora.org/ontology/0000/testontology/v2#testResource1',
'target': 'Test Resource 1_hasText',
'label': 'testText',
'rotation': 0,
'curvature': 0
};
expect(testHostComponent.links).toContain(textValueLink);
}));
it('should convert property to a circular link', waitForAsync( () => {
const circularLink = {
'source': 'http://www.knora.org/ontology/0000/testontology/v2#testResource1',
'target': 'http://www.knora.org/ontology/0000/testontology/v2#testResource1',
'label': 'textHasCopy',
'rotation': 1,
'curvature': 0.5
};
expect(testHostComponent.links).toContain(circularLink);
}));
});