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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
|
/*-
* ============LICENSE_START=======================================================
* SDC
* ================================================================================
* Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* 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.
* ============LICENSE_END=========================================================
* Modifications copyright (c) 2020, Nordix Foundation
* ================================================================================
*/
package org.openecomp.sdc.be.tosca;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum.VF;
import static org.openecomp.sdc.be.tosca.PropertyConvertor.PropertyType.PROPERTY;
import fj.data.Either;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import mockit.Deencapsulation;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Triple;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
import org.openecomp.sdc.be.datatypes.elements.RequirementDataDefinition;
import org.openecomp.sdc.be.datatypes.elements.ToscaArtifactDataDefinition;
import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
import org.openecomp.sdc.be.exception.ToscaExportException;
import org.openecomp.sdc.be.model.ArtifactDefinition;
import org.openecomp.sdc.be.model.AttributeDefinition;
import org.openecomp.sdc.be.model.CapabilityDefinition;
import org.openecomp.sdc.be.model.CapabilityRequirementRelationship;
import org.openecomp.sdc.be.model.Component;
import org.openecomp.sdc.be.model.ComponentInstance;
import org.openecomp.sdc.be.model.ComponentInstanceAttribute;
import org.openecomp.sdc.be.model.ComponentInstanceInput;
import org.openecomp.sdc.be.model.ComponentInstanceProperty;
import org.openecomp.sdc.be.model.ComponentParametersView;
import org.openecomp.sdc.be.model.DataTypeDefinition;
import org.openecomp.sdc.be.model.GroupDefinition;
import org.openecomp.sdc.be.model.GroupInstance;
import org.openecomp.sdc.be.model.InputDefinition;
import org.openecomp.sdc.be.model.InterfaceDefinition;
import org.openecomp.sdc.be.model.OutputDefinition;
import org.openecomp.sdc.be.model.PropertyDefinition;
import org.openecomp.sdc.be.model.RelationshipInfo;
import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
import org.openecomp.sdc.be.model.RequirementDefinition;
import org.openecomp.sdc.be.model.Resource;
import org.openecomp.sdc.be.model.Service;
import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
import org.openecomp.sdc.be.model.category.CategoryDefinition;
import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
import org.openecomp.sdc.be.tosca.exception.ToscaConversionException;
import org.openecomp.sdc.be.tosca.model.ToscaCapability;
import org.openecomp.sdc.be.tosca.model.ToscaNodeTemplate;
import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
import org.openecomp.sdc.be.tosca.model.ToscaProperty;
import org.openecomp.sdc.be.tosca.model.ToscaTemplate;
import org.openecomp.sdc.be.tosca.model.ToscaTemplateArtifact;
import org.openecomp.sdc.be.tosca.model.ToscaTemplateRequirement;
import org.openecomp.sdc.be.tosca.model.ToscaTopolgyTemplate;
import org.openecomp.sdc.be.tosca.utils.InputConverter;
import org.openecomp.sdc.be.tosca.utils.OutputConverter;
import org.openecomp.sdc.common.test.BaseConfDependent;
class ToscaExportHandlerTest extends BaseConfDependent {
private static final String COMPONENT_PROPERTY_NAME = "prop1";
private static final String COMPONENT_PROPERTY_TYPE = "string";
private static final String COMPONENT_INPUT_NAME = "input1";
private static final String COMPONENT_OUTPUT_NAME = "output1";
private static final String COMPONENT_INPUT_TYPE = "integer";
private static final String COMPONENT_OUTPUT_TYPE = "integer";
private static final String RESOURCE_NAME = "resource";
private static final String TOSCA_VERSION = "tosca_simple_yaml_1_1";
private static final String LOCAL_INTERFACE_TYPE = "Local";
public static final String UNIQUE_ID = "1L";
@InjectMocks
private ToscaExportHandler testSubject;
@Mock
private ApplicationDataTypeCache applicationDataTypeCache;
@Mock
private ToscaOperationFacade toscaOperationFacade;
@Mock
private CapabilityRequirementConverter capabilityRequirementConverter;
@Mock
private InputConverter inputConverter;
@Mock
private OutputConverter outputConverter;
@Mock
private GroupExportParser groupExportParser;
@Mock
private PropertyConvertor propertyConvertor;
@Mock
private GroupExportParserImpl groupExportParserImpl;
@Mock
private InterfaceLifecycleOperation interfaceLifecycleOperation;
@Mock
private InterfacesOperationsConverter interfacesOperationsConverter;
@Mock
private PolicyExportParser policyExportParser;
@Mock
private AttributeConverter attributeConverter;
@BeforeAll
public static void setupBeforeClass() {
componentName = "catalog-be";
confPath = "src/test/resources/config";
setUp();
}
@BeforeEach
void setUpMock() {
MockitoAnnotations.openMocks(this);
doReturn(new ToscaProperty()).when(propertyConvertor).convertProperty(any(), any(), eq(PROPERTY));
doReturn(new HashMap<String, Object>()).when(interfacesOperationsConverter).getInterfacesMap(any(), isNull(), anyMap(), anyMap(), anyBoolean());
}
private Resource getNewResource() {
Resource resource = new Resource();
resource.setUniqueId("resourceUniqueId");
List<CategoryDefinition> categories = new ArrayList<>();
CategoryDefinition category = new CategoryDefinition();
List<SubCategoryDefinition> subcategories = new ArrayList<>();
SubCategoryDefinition subcategory = new SubCategoryDefinition();
List<DataTypeDefinition> dataTypes = new ArrayList<>();
DataTypeDefinition dataType = new DataTypeDefinition();
dataType.setName("dataTypeName");
dataType.setDerivedFromName("tosca.datatypes.Root");
PropertyDataDefinition propData = new PropertyDataDefinitionBuilder()
.setName("property")
.setType("type")
.build();
List<PropertyDataDefinition> propDataList = Arrays.asList(propData);
dataType.setPropertiesData(propDataList);
List<PropertyDefinition> propList = propDataList.stream().map(PropertyDefinition::new)
.collect(Collectors.toList());
dataType.setProperties(propList);
dataTypes.add(dataType);
subcategory.setName("name");
subcategories.add(subcategory);
category.setName("name");
category.setSubcategories(subcategories);
categories.add(category);
final List<PropertyDefinition> properties = new ArrayList<>();
properties.add(new PropertyDefinition());
resource.setProperties(properties);
Map<String, InterfaceDefinition> proxyInterfaces = new HashMap<>();
proxyInterfaces.put("Local", new InterfaceDefinition("Local", "desc", new HashMap<>()));
resource.setInterfaces(proxyInterfaces);
resource.setCategories(categories);
resource.setVersion("version");
resource.setVendorName("vendorName");
resource.setVendorRelease("vendorRelease");
resource.setResourceVendorModelNumber("resourceVendorModelNumber");
resource.setDataTypes(dataTypes);
return resource;
}
private Service getNewService() {
Service service = new Service();
List<CategoryDefinition> categories = new ArrayList<>();
CategoryDefinition category = new CategoryDefinition();
List<SubCategoryDefinition> subcategories = new ArrayList<>();
SubCategoryDefinition subcategory = new SubCategoryDefinition();
subcategory.setName("name");
subcategories.add(subcategory);
category.setName("name");
category.setSubcategories(subcategories);
categories.add(category);
service.setCategories(categories);
service.setComponentType(ComponentTypeEnum.SERVICE);
service.setServiceType("serviceType");
service.setServiceRole("serviceRole");
service.setEnvironmentContext("environmentContext");
return service;
}
@Test
void testExportComponent() {
Component component = getNewResource();
Either<ToscaRepresentation, ToscaError> result;
when(applicationDataTypeCache.getAll(null)).thenReturn(Either.left(new HashMap<>()));
when(capabilityRequirementConverter.convertRequirements(anyMap(), any(Resource.class),
any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType()));
when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes(any()))
.thenReturn(Either.left(Collections.emptyMap()));
// default test when component is Resource
result = testSubject.exportComponent(component);
assertNotNull(result);
component = getNewService();
when(capabilityRequirementConverter.convertRequirements(anyMap(), any(Service.class),
any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType()));
when(applicationDataTypeCache.getAll(null)).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
// default test when component is Service
result = testSubject.exportComponent(component);
assertNotNull(result);
}
@Test
void testExportComponentInterface() {
Resource component = getNewResource();
Either<ToscaRepresentation, ToscaError> result;
component.setInterfaces(new HashMap<>());
when(applicationDataTypeCache.getAll(null)).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes(any()))
.thenReturn(Either.left(Collections.emptyMap()));
// default test when convertInterfaceNodeType is right
result = testSubject.exportComponentInterface(component, false);
assertNotNull(result);
when(applicationDataTypeCache.getAll(null)).thenReturn(Either.left(new HashMap<>()));
when(capabilityRequirementConverter.convertRequirements(anyMap(), any(Resource.class),
any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType()));
// default test when convertInterfaceNodeType is left
result = testSubject.exportComponentInterface(component, false);
assertNotNull(result);
}
@Test
void testExportDataType() {
DataTypeDefinition dataTypeDefinition = new DataTypeDefinition();
dataTypeDefinition.setUniqueId("uniqueId");
Either<ToscaRepresentation, ToscaError> result;
when(applicationDataTypeCache.getAll(null)).thenReturn(Either.left(new HashMap<>()));
result = testSubject.exportDataType(dataTypeDefinition);
assertNotNull(result);
}
@Test
void testConvertInterfaceNodeTypeProperties() {
Resource component = getNewResource();
component.setInterfaces(new HashMap<>());
InputDefinition input = new InputDefinition();
input.setName(COMPONENT_INPUT_NAME);
input.setType(COMPONENT_INPUT_TYPE);
component.setInputs(Collections.singletonList(input));
PropertyDefinition property = new PropertyDefinition();
property.setName(COMPONENT_PROPERTY_NAME);
property.setType(COMPONENT_PROPERTY_TYPE);
component.setProperties(Collections.singletonList(property));
component.setName(RESOURCE_NAME);
component.setToscaResourceName(RESOURCE_NAME);
when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes(any()))
.thenReturn(Either.left(Collections.emptyMap()));
when(applicationDataTypeCache.getAll(null)).thenReturn(Either.left(new HashMap<>()));
// when convertRequirements is called, make it return the same value as 3rd (index=2) argument.
when(capabilityRequirementConverter.convertRequirements(anyMap(), any(Resource.class),
any(ToscaNodeType.class))).thenAnswer(i -> Either.left(i.getArgument(2)));
final var result = testSubject.convertInterfaceNodeType(new HashMap<String, Component>(), component,
new ToscaTemplate(TOSCA_VERSION), new HashMap<String, ToscaNodeType>(), false);
assertNotNull(result);
assertTrue(result.isLeft());
Map<String, ToscaNodeType> nodeTypeMap = result.left().value().getNode_types();
assertNotNull(nodeTypeMap);
assertEquals(1, nodeTypeMap.size());
ToscaNodeType toscaNodeType = nodeTypeMap.values().iterator().next();
assertNotNull(toscaNodeType);
Map<String, ToscaProperty> propertyMap = toscaNodeType.getProperties();
// Check if inputs and properties in component are merged properly
assertNotNull(propertyMap);
assertEquals(1, propertyMap.size());
assertFalse(propertyMap.containsKey(COMPONENT_INPUT_NAME));
assertTrue(propertyMap.containsKey(COMPONENT_PROPERTY_NAME));
}
@Test
void testGetDependencies() {
Component component = new Resource();
Either<ToscaTemplate, ToscaError> result;
// default test
result = testSubject.getDependencies(component);
assertNotNull(result);
}
@Test
void testSetImports() {
Resource resource = new Resource();
resource.setResourceType(ResourceTypeEnum.PNF);
resource.setName("TestResourceName");
Map<String, ArtifactDefinition> artifactList = new HashMap<>();
ArtifactDefinition artifact = new ArtifactDefinition();
artifact.setArtifactName("name.name2");
artifactList.put("assettoscatemplate", artifact);
resource.setArtifacts(artifactList);
resource.setToscaArtifacts(artifactList);
ToscaTemplate toscaTemplate = new ToscaTemplate("");
ComponentInstance ci = new ComponentInstance();
ci.setComponentUid("name");
ci.setOriginType(OriginTypeEnum.PNF);
ci.setSourceModelUid("modelName");
List<ComponentInstance> componentInstanceList = new LinkedList<>();
componentInstanceList.add(ci);
resource.setComponentInstances(componentInstanceList);
when(toscaOperationFacade.getToscaFullElement("name")).thenReturn(Either.left(resource));
Either<ImmutablePair<ToscaTemplate, Map<String, Component>>, ToscaError> result;
result = Deencapsulation.invoke(testSubject, "fillImports", resource, toscaTemplate);
verify(toscaOperationFacade, times(1)).getToscaFullElement("name");
assertTrue(result.isLeft());
ToscaTemplate toscaTemplateRes = result.left().value().left;
assertEquals(8, toscaTemplateRes.getImports().size());
assertNotNull(toscaTemplateRes.getImports().get(6).get("resource-TestResourceName-interface"));
assertNotNull(toscaTemplateRes.getImports().get(7).get("resource-TestResourceName"));
assertEquals(1, toscaTemplateRes.getDependencies().size());
assertEquals("name.name2", toscaTemplateRes.getDependencies().get(0).getLeft());
}
@Test
void testConvertToscaTemplate() throws Exception {
final Component component = getNewResource();
final ToscaTemplate toscaNode = new ToscaTemplate("");
Either<ToscaTemplate, ToscaError> result;
final List<ComponentInstance> resourceInstances = new ArrayList<>();
final ComponentInstance instance = new ComponentInstance();
instance.setOriginType(OriginTypeEnum.SERVICE);
instance.setSourceModelUid("targetModelUid");
resourceInstances.add(instance);
component.setComponentInstances(resourceInstances);
when(applicationDataTypeCache.getAll(null)).thenReturn(Either.left(new HashMap<>()));
when(capabilityRequirementConverter.getOriginComponent(anyMap(), any(ComponentInstance.class))).thenReturn(Either.right(false));
final Map<String, ToscaProperty> map = new HashMap<>();
map.put("mock", new ToscaProperty());
doReturn(map).when(outputConverter).convert(any(), any());
// default test
result = Deencapsulation.invoke(testSubject, "convertToscaTemplate", component, toscaNode);
assertNotNull(result);
}
@Test
void testConvertToscaTemplateWhenComponentContainsGroup() throws ToscaConversionException {
Component component = getNewResource();
ToscaTemplate toscaNode = new ToscaTemplate("");
component.setComponentInstances(new ArrayList<>());
List<GroupDefinition> groups = new ArrayList<>();
GroupDefinition group = new GroupDefinition();
List<String> artifacts = new ArrayList<>();
artifacts.add("artifact");
group.setType("org.openecomp.groups.VfModule");
group.setArtifacts(artifacts);
groups.add(group);
component.setGroups(groups);
final var input = new InputDefinition();
input.setName(COMPONENT_INPUT_NAME);
input.setType(COMPONENT_INPUT_TYPE);
component.setInputs(Collections.singletonList(input));
final var output = new OutputDefinition();
output.setName(COMPONENT_OUTPUT_NAME);
output.setType(COMPONENT_OUTPUT_TYPE);
component.setOutputs(Collections.singletonList(output));
Map<String, String[]> substitutionMappingMap = new HashMap<>();
String[] array = {"value1", "value2"};
substitutionMappingMap.put("key", array);
when(capabilityRequirementConverter.convertSubstitutionMappingCapabilities(anyMap(),
any(Component.class))).thenReturn(Either.left(substitutionMappingMap));
when(capabilityRequirementConverter.convertSubstitutionMappingRequirements(any(Component.class), anyMap()))
.thenReturn(Either.left(Collections.emptyMap()));
when(applicationDataTypeCache.getAll(null)).thenReturn(Either.left(new HashMap<>()));
when(inputConverter.convertInputs(anyList(), anyMap())).thenReturn(new HashMap<>());
when(groupExportParser.getGroups(component)).thenReturn(null);
final Map<String, ToscaProperty> map = new HashMap<>();
map.put("mock", new ToscaProperty());
doReturn(map).when(outputConverter).convert(any(), any());
// test component contains group
final var result = Deencapsulation.invoke(testSubject, "convertToscaTemplate", component, toscaNode);
assertNotNull(result);
}
@Test
void testConvertToscaTemplateWhenComponentIsService() throws Exception {
Component component = getNewService();
ToscaTemplate toscaNode = new ToscaTemplate("");
Either<ToscaTemplate, ToscaError> result;
component.setComponentInstances(new ArrayList<>());
List<GroupDefinition> groups = new ArrayList<>();
GroupDefinition group = new GroupDefinition();
List<String> artifacts = new ArrayList<>();
artifacts.add("artifact");
group.setType("org.openecomp.groups.VfModule");
group.setArtifacts(artifacts);
groups.add(group);
component.setGroups(groups);
Map<String, String[]> substitutionMappingMap = new HashMap<>();
String[] array = {"value1", "value2"};
substitutionMappingMap.put("key", array);
when(capabilityRequirementConverter.convertSubstitutionMappingCapabilities(anyMap(), any(Component.class)))
.thenReturn(Either.left(substitutionMappingMap));
when(capabilityRequirementConverter
.convertSubstitutionMappingRequirements(any(Component.class), anyMap()))
.thenReturn(Either.left(Collections.emptyMap()));
when(applicationDataTypeCache.getAll(null)).thenReturn(Either.left(new HashMap<>()));
when(inputConverter.convertInputs(anyList(), anyMap())).thenReturn(new HashMap<>());
final Map<String, ToscaProperty> map = new HashMap<>();
map.put("mock", new ToscaProperty());
doReturn(map).when(outputConverter).convert(any(), any());
// test component contains group
result = Deencapsulation.invoke(testSubject, "convertToscaTemplate", component, toscaNode);
assertNotNull(result);
}
@Test
void testConvertMetadata_1() {
Component component = getNewResource();
boolean isInstance = true;
ComponentInstance componentInstance = new ComponentInstance();
componentInstance.setOriginType(OriginTypeEnum.ServiceProxy);
componentInstance.setSourceModelInvariant("targetModelInvariant");
// default test
Map<String, String> result = Deencapsulation.invoke(testSubject, "convertMetadata", component, isInstance, componentInstance);
assertNotNull(result);
}
@Test
void testFillImports() {
Component component = getNewService();
ToscaTemplate toscaTemplate = new ToscaTemplate("");
Either<ImmutablePair<ToscaTemplate, Map<String, Component>>, ToscaError> result;
ComponentInstance instance = new ComponentInstance();
List<ComponentInstance> resourceInstances = new ArrayList<>();
instance.setComponentUid("name");
resourceInstances.add(instance);
component.setComponentInstances(resourceInstances);
Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
ArtifactDefinition artifact = new ArtifactDefinition();
artifact.setArtifactName("name.name2");
toscaArtifacts.put("assettoscatemplate", artifact);
component.setToscaArtifacts(toscaArtifacts);
when(toscaOperationFacade.getToscaFullElement(any(String.class)))
.thenReturn(Either.left(component));
Resource baseType = getNewResource();
Map<String, ArtifactDefinition> baseTypeToscaArtifacts = new HashMap<>();
ArtifactDefinition baseTypeArtifact = new ArtifactDefinition();
baseTypeArtifact.setArtifactName("typeA");
baseTypeToscaArtifacts.put("assettoscatemplate", baseTypeArtifact);
baseType.setToscaArtifacts(baseTypeToscaArtifacts);
component.setDerivedFromGenericType("org.typeA");
component.setDerivedFromGenericVersion("1.0");
when(toscaOperationFacade.getByToscaResourceNameAndVersion("org.typeA", "1.0", null)).thenReturn(Either.left(baseType));
// default test
result = Deencapsulation.invoke(testSubject, "fillImports", component, toscaTemplate);
assertNotNull(result);
}
@Test
void testCreateDependency() {
Map<String, Component> componentCache = new HashMap<>();
List<Map<String, Map<String, String>>> imports = new ArrayList<>();
List<Triple<String, String, Component>> dependecies = new ArrayList<>();
ComponentInstance ci = new ComponentInstance();
Component component = getNewResource();
Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
ArtifactDefinition artifact = new ArtifactDefinition();
artifact.setArtifactName("name.name2");
toscaArtifacts.put("assettoscatemplate", artifact);
component.setToscaArtifacts(toscaArtifacts);
ci.setComponentUid("name");
ci.setOriginType(OriginTypeEnum.ServiceProxy);
ci.setSourceModelUid("modelName");
when(toscaOperationFacade.getToscaFullElement("name")).thenReturn(Either.left(component));
when(toscaOperationFacade.getToscaFullElement("modelName")).thenReturn(Either.left(new Service()));
// default test
Deencapsulation.invoke(testSubject, "createDependency", componentCache, imports, dependecies, ci);
assertFalse(componentCache.isEmpty());
}
@Test
void testGetInterfaceFilename() {
String artifactName = "artifact.name";
String result;
// default test
result = ToscaExportHandler.getInterfaceFilename(artifactName);
assertNotNull(result);
}
@Test
void testConvertNodeType() {
Component component = new Resource();
ToscaTemplate toscaNode = new ToscaTemplate("");
Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
Either<ToscaTemplate, ToscaError> result;
when(applicationDataTypeCache.getAll(null)).thenReturn(Either.right(JanusGraphOperationStatus.ALREADY_EXIST));
when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes(any()))
.thenReturn(Either.left(Collections.emptyMap()));
// default test
result = Deencapsulation
.invoke(testSubject, "convertNodeType", new HashMap<>(), component, toscaNode, nodeTypes);
assertNotNull(result);
}
@Test
void testConvertInterfaceNodeType() {
Component component = getNewResource();
ToscaTemplate toscaNode = new ToscaTemplate("");
Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
Either<ToscaTemplate, ToscaError> result;
List<InputDefinition> inputs = new ArrayList<>();
inputs.add(new InputDefinition());
component.setInputs(inputs);
when(applicationDataTypeCache.getAll(null)).thenReturn(Either.left(new HashMap<>()));
when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes(any()))
.thenReturn(Either.left(Collections.emptyMap()));
when(capabilityRequirementConverter.convertRequirements(anyMap(), any(Resource.class),
any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType()));
// default test
result = Deencapsulation.invoke(testSubject, "convertInterfaceNodeType", new HashMap<>(), component, toscaNode
, nodeTypes, false);
assertNotNull(result);
}
@Test
void testConvertReqCapAndTypeName() {
Component component = new Resource();
ToscaTemplate toscaNode = new ToscaTemplate("");
Map<String, ToscaNodeType> nodeTypes = new HashMap<>();
ToscaNodeType toscaNodeType = new ToscaNodeType();
Map<String, DataTypeDefinition> dataTypes = new HashMap<>();
Either<ToscaTemplate, ToscaError> result;
when(
capabilityRequirementConverter
.convertCapabilities(anyMap(), any(Resource.class), anyMap()))
.thenReturn(new HashMap<>());
when(capabilityRequirementConverter.convertRequirements(anyMap(), any(Resource.class),
any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType()));
// default test
result = Deencapsulation
.invoke(testSubject, "convertReqCapAndTypeName", new HashMap<>(), component, toscaNode, nodeTypes,
toscaNodeType, dataTypes);
assertNotNull(result);
component = new Service();
when(capabilityRequirementConverter.convertRequirements(anyMap(), any(Service.class),
any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType()));
// test when component is service
result = Deencapsulation
.invoke(testSubject, "convertReqCapAndTypeName", new HashMap<>(), component, toscaNode, nodeTypes,
toscaNodeType, dataTypes);
assertNotNull(result);
}
@Test
void testConvertNodeTemplatesWhenComponentIsService() {
final Component component = getNewService();
final List<ComponentInstance> componentInstances = new ArrayList<>();
final Map<String, List<ComponentInstanceProperty>> componentInstancesProperties = new HashMap<>();
final Map<String, Component> componentCache = new HashMap<>();
final Map<String, DataTypeDefinition> dataTypes = new HashMap<>();
final ToscaTopolgyTemplate topologyTemplate = new ToscaTopolgyTemplate();
final Either<Map<String, ToscaNodeTemplate>, ToscaError> result;
final Map<String, List<ComponentInstanceInput>> componentInstancesInputs = new HashMap<>();
final List<ComponentInstanceInput> inputs = new ArrayList<>();
final ComponentInstanceInput componentInstanceInput = new ComponentInstanceInput();
componentInstanceInput.setUniqueId("uuid");
inputs.add(componentInstanceInput);
componentInstancesInputs.put("uuid", inputs);
final List<RequirementCapabilityRelDef> resourceInstancesRelations = new ArrayList<>();
final RequirementCapabilityRelDef reldef = new RequirementCapabilityRelDef();
reldef.setFromNode("node");
resourceInstancesRelations.add(reldef);
component.setComponentInstancesRelations(resourceInstancesRelations);
final ComponentInstance instance = new ComponentInstance();
instance.setUniqueId("id");
instance.setComponentUid("uid");
instance.setOriginType(OriginTypeEnum.ServiceProxy);
final List<GroupInstance> groupInstances = new ArrayList<>();
final GroupInstance groupInst = new GroupInstance();
final List<String> artifacts = new ArrayList<>();
artifacts.add("artifact");
groupInst.setArtifacts(artifacts);
groupInst.setType("type");
groupInstances.add(groupInst);
instance.setGroupInstances(groupInstances);
final List<PropertyDefinition> properties = new ArrayList<>();
properties.add(new PropertyDefinition());
instance.setProperties(properties);
instance.setUniqueId("uuid");
instance.setDescription("desc");
instance.setSourceModelUid("sourceModelUid");
componentInstances.add(instance);
component.setComponentInstances(componentInstances);
component.setComponentInstancesInputs(componentInstancesInputs);
component.setInvariantUUID("uuid");
component.setUUID("uuid");
component.setDescription("desc");
component.setUniqueId("uid");
componentCache.put("uid", component);
final List<ComponentInstanceProperty> componentInstanceProperties = new ArrayList<>();
componentInstanceProperties.add(new ComponentInstanceProperty());
componentInstancesProperties.put("uuid", componentInstanceProperties);
component.setComponentInstancesProperties(componentInstancesProperties);
final Map<String, List<ComponentInstanceAttribute>> componentInstancesAttributes = new HashMap<>();
final List<ComponentInstanceAttribute> componentInstanceAttributes = new ArrayList<>();
final ComponentInstanceAttribute componentInstanceAttribute = new ComponentInstanceAttribute();
componentInstanceAttribute.setDefaultValue("def value");
componentInstanceAttributes.add(componentInstanceAttribute);
componentInstancesAttributes.put("uuid", componentInstanceAttributes);
component.setComponentInstancesAttributes(componentInstancesAttributes);
ComponentInstanceProperty cip = new ComponentInstanceProperty();
cip.setInstanceUniqueId("id");
List<ComponentInstanceProperty> list = new ArrayList<>();
list.add(cip);
componentInstancesProperties.put("id", list);
component.setComponentInstancesProperties(componentInstancesProperties);
when(capabilityRequirementConverter.getOriginComponent(anyMap(), any(ComponentInstance.class))).thenReturn(Either.left(component));
when(capabilityRequirementConverter
.convertComponentInstanceCapabilities(any(ComponentInstance.class), anyMap(), any(ToscaNodeTemplate.class)))
.thenReturn(Either.left(new ToscaNodeTemplate()));
when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes(any())).thenReturn(Either.left(Collections.emptyMap()));
when(applicationDataTypeCache.getAll(null)).thenReturn(Either.left(new HashMap<>()));
when(capabilityRequirementConverter.convertRequirements(anyMap(), any(Resource.class), any(ToscaNodeType.class)))
.thenReturn(Either.left(new ToscaNodeType()));
when(toscaOperationFacade.getToscaFullElement("uid")).thenReturn(Either.left(component));
when(toscaOperationFacade.getToscaFullElement("sourceModelUid")).thenReturn(Either.left(component));
when(toscaOperationFacade.getLatestByName("serviceProxy", null)).thenReturn(Either.left(new Resource()));
when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class))).thenReturn(Either.left(getNewResource()));
final Map<String, String[]> substitutionMappingMap = new HashMap<>();
final String[] array = {"value1", "value2"};
substitutionMappingMap.put("key", array);
when(capabilityRequirementConverter.convertSubstitutionMappingCapabilities(anyMap(), any(Component.class)))
.thenReturn(Either.left(substitutionMappingMap));
when(capabilityRequirementConverter
.convertSubstitutionMappingRequirements(any(Component.class), anyMap()))
.thenReturn(Either.left(Collections.emptyMap()));
// default test
final Either<ToscaRepresentation, ToscaError> toscaRepresentationToscaErrorEither = testSubject.exportComponent(component);
assertNotNull(toscaRepresentationToscaErrorEither);
}
@Test
void testConvertNodeTemplatesWhenComponentIsResource() {
final Resource component = getNewResource();
component.setResourceType(VF);
final List<ComponentInstance> componentInstances = new ArrayList<>();
final Map<String, List<ComponentInstanceProperty>> componentInstancesProperties = new HashMap<>();
final Map<String, Component> componentCache = new HashMap<>();
final Map<String, DataTypeDefinition> dataTypes = new HashMap<>();
final ToscaTopolgyTemplate topologyTemplate = new ToscaTopolgyTemplate();
final Either<Map<String, ToscaNodeTemplate>, ToscaError> result;
final Map<String, List<ComponentInstanceInput>> componentInstancesInputs = new HashMap<>();
final List<ComponentInstanceInput> inputs = new ArrayList<>();
final ComponentInstanceInput componentInstanceInput = new ComponentInstanceInput();
componentInstanceInput.setUniqueId("uuid");
inputs.add(componentInstanceInput);
componentInstancesInputs.put("uuid", inputs);
final List<RequirementCapabilityRelDef> resourceInstancesRelations = new ArrayList<>();
final RequirementCapabilityRelDef reldef = new RequirementCapabilityRelDef();
reldef.setFromNode("node");
resourceInstancesRelations.add(reldef);
component.setComponentInstancesRelations(resourceInstancesRelations);
final ComponentInstance instance = new ComponentInstance();
instance.setUniqueId("id");
instance.setComponentUid("uid");
instance.setOriginType(OriginTypeEnum.VFC);
final List<GroupInstance> groupInstances = new ArrayList<>();
final GroupInstance groupInst = new GroupInstance();
final List<String> artifacts = new ArrayList<>();
artifacts.add("artifact");
groupInst.setArtifacts(artifacts);
groupInst.setType("type");
groupInstances.add(groupInst);
instance.setGroupInstances(groupInstances);
final List<PropertyDefinition> properties = new ArrayList<>();
properties.add(new PropertyDefinition());
instance.setProperties(properties);
component.setProperties(properties);
instance.setUniqueId("uuid");
instance.setDescription("desc");
instance.setSourceModelUid("sourceModelUid");
final Map<String, ArtifactDefinition> artifactList = new HashMap<>();
final ArtifactDefinition artifact = new ArtifactDefinition();
artifact.setArtifactName("name.name2");
artifactList.put("assettoscatemplate", artifact);
instance.setArtifacts(artifactList);
Map<String, ToscaArtifactDataDefinition> toscaArtifactDataDefinitionMap = new HashMap<>();
toscaArtifactDataDefinitionMap.put("assettoscatemplate", new ToscaArtifactDataDefinition());
instance.setToscaArtifacts(toscaArtifactDataDefinitionMap);
componentInstances.add(instance);
component.setComponentInstances(componentInstances);
component.setComponentInstancesInputs(componentInstancesInputs);
component.setInvariantUUID("uuid");
component.setUUID("uuid");
component.setDescription("desc");
component.setUniqueId("uid");
componentCache.put("uid", component);
final List<ComponentInstanceProperty> componentInstanceProperties = new ArrayList<>();
componentInstanceProperties.add(new ComponentInstanceProperty());
componentInstancesProperties.put("uuid", componentInstanceProperties);
component.setComponentInstancesProperties(componentInstancesProperties);
final Map<String, List<ComponentInstanceAttribute>> componentInstancesAttributes = new HashMap<>();
final List<ComponentInstanceAttribute> componentInstanceAttributes = new ArrayList<>();
final ComponentInstanceAttribute componentInstanceAttribute = new ComponentInstanceAttribute();
componentInstanceAttribute.setDefaultValue("def value");
componentInstanceAttributes.add(componentInstanceAttribute);
componentInstancesAttributes.put("uuid", componentInstanceAttributes);
component.setComponentInstancesAttributes(componentInstancesAttributes);
component.setArtifacts(artifactList);
component.setToscaArtifacts(artifactList);
final List<AttributeDefinition> attributes = new ArrayList<>();
final var attribute = new AttributeDefinition();
attribute.setName("mock");
attributes.add(attribute);
component.setAttributes(attributes);
List<ComponentInstanceInput> componentInstanceInputs = new ArrayList<>();
componentInstanceInputs.add(new ComponentInstanceInput());
componentInstancesInputs.put("id", componentInstanceInputs);
component.setComponentInstancesInputs(componentInstancesInputs);
when(capabilityRequirementConverter.getOriginComponent(anyMap(), any(ComponentInstance.class))).thenReturn(Either.left(component));
when(capabilityRequirementConverter
.convertComponentInstanceCapabilities(any(ComponentInstance.class), anyMap(), any(ToscaNodeTemplate.class)))
.thenReturn(Either.left(new ToscaNodeTemplate()));
when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes(any())).thenReturn(Either.left(Collections.emptyMap()));
when(applicationDataTypeCache.getAll(null)).thenReturn(Either.left(new HashMap<>()));
when(capabilityRequirementConverter.convertRequirements(anyMap(), any(Resource.class), any(ToscaNodeType.class)))
.thenReturn(Either.left(new ToscaNodeType()));
when(toscaOperationFacade.getToscaFullElement("uid")).thenReturn(Either.left(component));
when(toscaOperationFacade.getToscaFullElement("sourceModelUid")).thenReturn(Either.left(component));
when(toscaOperationFacade.getLatestByName("serviceProxy", null)).thenReturn(Either.left(new Resource()));
Resource newResource = getNewResource();
InputDefinition input = new InputDefinition();
input.setName(COMPONENT_INPUT_NAME);
input.setType(COMPONENT_INPUT_TYPE);
newResource.setInputs(Collections.singletonList(input));
when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class))).thenReturn(Either.left(newResource));
final Map<String, String[]> substitutionMappingMap = new HashMap<>();
final String[] array = {"value1", "value2"};
substitutionMappingMap.put("key", array);
when(capabilityRequirementConverter.convertSubstitutionMappingCapabilities(anyMap(), any(Component.class)))
.thenReturn(Either.left(substitutionMappingMap));
when(capabilityRequirementConverter
.convertSubstitutionMappingRequirements(any(Component.class), anyMap()))
.thenReturn(Either.left(Collections.emptyMap()));
// default test
final Either<ToscaRepresentation, ToscaError> toscaRepresentationToscaErrorEither = testSubject.exportComponent(newResource);
assertNotNull(toscaRepresentationToscaErrorEither);
}
@Test
void testConvertNodeTemplatesWhenConvertComponentInstanceCapabilitiesIsRight() {
Component component = getNewResource();
List<ComponentInstance> componentInstances = new ArrayList<>();
Map<String, List<ComponentInstanceProperty>> componentInstancesProperties = new HashMap<>();
Map<String, List<ComponentInstanceProperty>> componentInstancesInterfaces = new HashMap<>();
Map<String, Component> componentCache = new HashMap<>();
Map<String, DataTypeDefinition> dataTypes = new HashMap<>();
ToscaTopolgyTemplate topologyTemplate = new ToscaTopolgyTemplate();
Either<Map<String, ToscaNodeTemplate>, ToscaError> result;
Map<String, List<ComponentInstanceInput>> componentInstancesInputs = new HashMap<>();
List<ComponentInstanceInput> inputs = new ArrayList<>();
inputs.add(new ComponentInstanceInput());
componentInstancesInputs.put("key", inputs);
List<RequirementCapabilityRelDef> resourceInstancesRelations = new ArrayList<>();
RequirementCapabilityRelDef reldef = new RequirementCapabilityRelDef();
reldef.setFromNode("node");
resourceInstancesRelations.add(reldef);
component.setComponentInstancesRelations(resourceInstancesRelations);
ComponentInstance instance = new ComponentInstance();
instance.setUniqueId("id");
instance.setComponentUid("uid");
instance.setOriginType(OriginTypeEnum.ServiceProxy);
componentInstances.add(instance);
component.setComponentInstances(componentInstances);
component.setComponentInstancesInputs(componentInstancesInputs);
component.setInvariantUUID("uuid");
component.setUUID("uuid");
component.setDescription("desc");
componentCache.put("uid", component);
when(capabilityRequirementConverter.getOriginComponent(anyMap(), any(ComponentInstance.class))).thenReturn(Either.left(component));
when(capabilityRequirementConverter
.convertComponentInstanceCapabilities(any(ComponentInstance.class), anyMap(), any(ToscaNodeTemplate.class)))
.thenReturn(Either.right(ToscaError.GENERAL_ERROR));
when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes(any())).thenReturn(Either.left(Collections.emptyMap()));
when(applicationDataTypeCache.getAll(null)).thenReturn(Either.left(new HashMap<>()));
when(capabilityRequirementConverter.convertRequirements(anyMap(), any(Resource.class),
any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType()));
// default test
final Either<ToscaRepresentation, ToscaError> toscaRepresentationToscaErrorEither = testSubject.exportComponent(component);
assertNotNull(toscaRepresentationToscaErrorEither);
}
@Test
void testConvetNodeTemplateWhenGetOriginComponentIsRight() {
Component component = getNewResource();
List<ComponentInstance> componentInstances = new ArrayList<>();
Map<String, List<ComponentInstanceProperty>> componentInstancesProperties = new HashMap<>();
Map<String, List<ComponentInstanceProperty>> componentInstancesInterfaces = new HashMap<>();
Map<String, Component> componentCache = new HashMap<>();
Map<String, DataTypeDefinition> dataTypes = new HashMap<>();
ToscaTopolgyTemplate topologyTemplate = new ToscaTopolgyTemplate();
Either<Map<String, ToscaNodeTemplate>, ToscaError> result;
Map<String, List<ComponentInstanceInput>> componentInstancesInputs = new HashMap<>();
List<ComponentInstanceInput> inputs = new ArrayList<>();
inputs.add(new ComponentInstanceInput());
componentInstancesInputs.put("key", inputs);
List<RequirementCapabilityRelDef> resourceInstancesRelations = new ArrayList<>();
RequirementCapabilityRelDef reldef = new RequirementCapabilityRelDef();
reldef.setFromNode("id");
resourceInstancesRelations.add(reldef);
component.setComponentInstancesRelations(resourceInstancesRelations);
ComponentInstance instance = new ComponentInstance();
instance.setUniqueId("id");
instance.setComponentUid("uid");
instance.setOriginType(OriginTypeEnum.ServiceProxy);
componentInstances.add(instance);
component.setComponentInstances(componentInstances);
component.setComponentInstancesInputs(componentInstancesInputs);
component.setInvariantUUID("uuid");
component.setUUID("uuid");
component.setDescription("desc");
final List<AttributeDefinition> attributes = new ArrayList<>();
final var attribute = new AttributeDefinition();
attribute.setName("mock");
attributes.add(attribute);
component.setAttributes(attributes);
componentCache.put("uid", component);
when(capabilityRequirementConverter.getOriginComponent(anyMap(), any(ComponentInstance.class))).thenReturn(Either.right(false));
when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes(any())).thenReturn(Either.left(Collections.emptyMap()));
when(applicationDataTypeCache.getAll(null)).thenReturn(Either.left(new HashMap<>()));
when(capabilityRequirementConverter.convertRequirements(anyMap(), any(Resource.class),
any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType()));
// default test
final Either<ToscaRepresentation, ToscaError> toscaRepresentationToscaErrorEither = testSubject.exportComponent(component);
assertNotNull(toscaRepresentationToscaErrorEither);
}
@Test
void testConvertNodeTemplatesWhenConvertComponentInstanceRequirmentsIsRight() {
Resource component = getNewResource();
component.setResourceType(VF);
List<ComponentInstance> componentInstances = new ArrayList<>();
Map<String, List<ComponentInstanceProperty>> componentInstancesProperties = new HashMap<>();
Map<String, List<ComponentInstanceProperty>> componentInstancesInterfaces = new HashMap<>();
Map<String, Component> componentCache = new HashMap<>();
Map<String, DataTypeDefinition> dataTypes = new HashMap<>();
ToscaTopolgyTemplate topologyTemplate = new ToscaTopolgyTemplate();
Map<String, List<ComponentInstanceInput>> componentInstancesInputs = new HashMap<>();
List<ComponentInstanceInput> inputs = new ArrayList<>();
inputs.add(new ComponentInstanceInput());
componentInstancesInputs.put("key", inputs);
List<RequirementCapabilityRelDef> resourceInstancesRelations = new ArrayList<>();
RequirementCapabilityRelDef reldef = new RequirementCapabilityRelDef();
reldef.setFromNode("id");
reldef.setToNode("node");
List<CapabilityRequirementRelationship> relationships = new ArrayList<>();
CapabilityRequirementRelationship relationship = new CapabilityRequirementRelationship();
relationship.setRelation(new RelationshipInfo());
relationships.add(relationship);
reldef.setRelationships(relationships);
resourceInstancesRelations.add(reldef);
component.setComponentInstancesRelations(resourceInstancesRelations);
Map<String, ArtifactDefinition> artifactList = new HashMap<>();
ArtifactDefinition artifact = new ArtifactDefinition();
artifact.setArtifactName("name.name2");
artifactList.put("assettoscatemplate", artifact);
component.setArtifacts(artifactList);
component.setToscaArtifacts(artifactList);
ComponentInstance instance = new ComponentInstance();
instance.setUniqueId("id");
instance.setComponentUid("id");
instance.setOriginType(OriginTypeEnum.VF);
componentInstances.add(instance);
component.setComponentInstances(componentInstances);
component.setComponentInstancesInputs(componentInstancesInputs);
component.setComponentInstances(componentInstances);
doReturn(Either.left(component)).when(toscaOperationFacade).getToscaFullElement("id");
when(capabilityRequirementConverter.getOriginComponent(anyMap(), any(ComponentInstance.class))).thenReturn(Either.left(component));
when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes(any())).thenReturn(Either.left(Collections.emptyMap()));
when(applicationDataTypeCache.getAll(null)).thenReturn(Either.left(new HashMap<>()));
when(capabilityRequirementConverter.convertRequirements(anyMap(), any(Resource.class),
any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType()));
when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class)))
.thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
// default test
final Either<ToscaRepresentation, ToscaError> result = testSubject.exportComponent(component);
assertNotNull(result);
}
@Test
void testCreateNodeType() {
Component component = new Resource();
List<String> array = new ArrayList<>();
array.add("value");
((Resource) component).setDerivedFrom(array);
ToscaNodeType result;
// test when component is resource
result = Deencapsulation.invoke(testSubject, "createNodeType", component);
assertNotNull(result);
component = new Service();
// test when component is service
result = Deencapsulation.invoke(testSubject, "createNodeType", component);
assertNotNull(result);
}
@Test
void testCreateProxyInterfaceTypesComponentNotFound() {
Component container = new Service();
Either<Map<String, ToscaNodeType>, ToscaError> result;
List<ComponentInstance> componentInstances = new ArrayList<>();
ComponentInstance instance = new ComponentInstance();
instance.setOriginType(OriginTypeEnum.ServiceProxy);
instance.setSourceModelUid("targetModelUid");
instance.setToscaComponentName("toscaComponentName");
componentInstances.add(instance);
container.setComponentInstances(componentInstances);
when(toscaOperationFacade.getToscaElement(any(String.class),
any(ComponentParametersView.class)))
.thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
result = Deencapsulation.invoke(testSubject, "createProxyInterfaceTypes", container);
assertTrue(result.isRight());
}
@Test
void testCreateProxyInterfaceTypesWhenInterfaceLifecycleFetchFailed() {
Component container = new Service();
Either<Map<String, ToscaNodeType>, ToscaError> result;
List<ComponentInstance> componentInstances = new ArrayList<>();
ComponentInstance instance = new ComponentInstance();
instance.setOriginType(OriginTypeEnum.ServiceProxy);
instance.setSourceModelUid("targetModelUid");
instance.setToscaComponentName("toscaComponentName");
componentInstances.add(instance);
container.setComponentInstances(componentInstances);
when(toscaOperationFacade.getToscaElement(any(String.class),
any(ComponentParametersView.class)))
.thenReturn(Either.left(new Resource()));
when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes(any()))
.thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
result = Deencapsulation.invoke(testSubject, "createProxyInterfaceTypes", container);
assertTrue(result.isRight());
}
@Test
void testCreateProxyInterfaceTypesPositive() {
Component container = new Service();
Either<Map<String, ToscaNodeType>, ToscaError> result;
List<ComponentInstance> componentInstances = new ArrayList<>();
ComponentInstance instance = new ComponentInstance();
instance.setOriginType(OriginTypeEnum.ServiceProxy);
instance.setSourceModelUid("targetModelUid");
instance.setToscaComponentName("toscaComponentName");
componentInstances.add(instance);
container.setComponentInstances(componentInstances);
when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes(any())).thenReturn(Either.left(Collections.emptyMap()));
Component proxyResource = new Resource();
Map<String, InterfaceDefinition> proxyInterfaces = new HashMap<>();
proxyInterfaces.put("Local", new InterfaceDefinition("Local", "desc", new HashMap<>()));
proxyResource.setInterfaces(proxyInterfaces);
when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class))).thenReturn(Either.left(proxyResource));
when(interfacesOperationsConverter.addInterfaceTypeElement(any(Component.class), anyList()))
.thenReturn(Collections.singletonMap(LOCAL_INTERFACE_TYPE, new HashMap<>()));
result = Deencapsulation.invoke(testSubject, "createProxyInterfaceTypes", container);
assertNotNull(result);
assertTrue(result.isLeft());
assertEquals(1, result.left().value().size());
}
@Test
void testCreateProxyNodeTypes() {
Map<String, Component> componentCache = new HashMap<>();
Component container = new Resource();
Either<Map<String, ToscaNodeType>, ToscaError> result;
List<ComponentInstance> componentInstances = new ArrayList<>();
ComponentInstance instance = new ComponentInstance();
instance.setOriginType(OriginTypeEnum.ServiceProxy);
instance.setSourceModelUid("targetModelUid");
componentInstances.add(instance);
container.setComponentInstances(componentInstances);
when(toscaOperationFacade.getLatestByName("serviceProxy", null))
.thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
// test when getLatestByName return is right
result = Deencapsulation.invoke(testSubject, "createProxyNodeTypes", componentCache, container);
assertNotNull(result);
}
@Test
void testCreateServiceSubstitutionNodeTypes() {
Map<String, Component> componentCache = new HashMap<>();
Component referencedService = getNewService();
referencedService.setInvariantUUID("uuid");
referencedService.setUUID("uuid");
referencedService.setUniqueId("targetModelUid");
referencedService.setDescription("desc");
componentCache.put("targetModelUid", referencedService);
Component containerService = new Service();
List<ComponentInstance> componentInstances = new ArrayList<>();
ComponentInstance instance = new ComponentInstance();
instance.setOriginType(OriginTypeEnum.ServiceSubstitution);
instance.setSourceModelUid("targetModelUid");
componentInstances.add(instance);
containerService.setComponentInstances(componentInstances);
when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes(any()))
.thenReturn(Either.left(Collections.emptyMap()));
when(applicationDataTypeCache.getAll(null)).thenReturn(Either.left(new HashMap<>()));
when(capabilityRequirementConverter.convertRequirements(anyMap(), any(Service.class),
any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType()));
ToscaTemplate toscaNode = new ToscaTemplate("1_1");
Deencapsulation.invoke(testSubject, "createServiceSubstitutionNodeTypes", componentCache, containerService, toscaNode);
assertNotNull(toscaNode.getNode_types());
}
@Test
void testCreateProxyNodeTypesWhenGetLatestByNameReturnValue() {
Map<String, Component> componentCache = new HashMap<>();
Component container = new Resource();
Either<Map<String, ToscaNodeType>, ToscaError> result;
List<ComponentInstance> componentInstances = new ArrayList<>();
ComponentInstance instance = new ComponentInstance();
instance.setOriginType(OriginTypeEnum.ServiceProxy);
instance.setSourceModelUid("targetModelUid");
componentInstances.add(instance);
container.setComponentInstances(componentInstances);
when(toscaOperationFacade.getLatestByName("serviceProxy", null)).thenReturn(Either.left(new Resource()));
ComponentParametersView parameterView = new ComponentParametersView();
parameterView.disableAll();
parameterView.setIgnoreCategories(false);
when(toscaOperationFacade.getToscaElement(any(String.class),
any(ComponentParametersView.class)))
.thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
when(applicationDataTypeCache.getAll(null)).thenReturn(Either.left(new HashMap<>()));
// test when getLatestByName is left
result = Deencapsulation.invoke(testSubject, "createProxyNodeTypes", componentCache, container);
assertNotNull(result);
}
@Test
void testCreateProxyNodeType() {
Map<String, Component> componentCache = new HashMap<>();
Component origComponent = new Resource();
Component proxyComponent = new Resource();
ComponentInstance instance = new ComponentInstance();
ToscaNodeType result;
when(applicationDataTypeCache.getAll(null)).thenReturn(Either.left(new HashMap<>()));
// default test
result = Deencapsulation.invoke(testSubject, "createProxyNodeType", componentCache, origComponent,
proxyComponent, instance);
assertNotNull(result);
}
@Test
void testConvertComponentInstanceRequirements() {
Component component = new Resource();
ComponentInstance componentInstance = new ComponentInstance();
List<RequirementCapabilityRelDef> relations = new ArrayList<>();
ToscaNodeTemplate nodeTypeTemplate = new ToscaNodeTemplate();
Component originComponent = new Resource();
Map<String, Component> componentCache = new HashMap<>();
Either<ToscaNodeTemplate, ToscaError> result;
// default test
result = Deencapsulation.invoke(testSubject, "convertComponentInstanceRequirements", component,
componentInstance, relations, nodeTypeTemplate, originComponent, componentCache);
assertNotNull(result);
RequirementCapabilityRelDef reldef = new RequirementCapabilityRelDef();
reldef.setFromNode("name");
reldef.setToNode("name1");
List<CapabilityRequirementRelationship> relationships = new ArrayList<>();
CapabilityRequirementRelationship cap = new CapabilityRequirementRelationship();
cap.setRelation(new RelationshipInfo());
relationships.add(cap);
reldef.setRelationships(relationships);
relations.add(reldef);
componentInstance.setUniqueId("name");
List<ComponentInstance> instances = new ArrayList<>();
instances.add(componentInstance);
component.setComponentInstances(instances);
// test when filteredRElations ins't empty
result = Deencapsulation.invoke(testSubject, "convertComponentInstanceRequirements", component,
componentInstance, relations, nodeTypeTemplate, originComponent, componentCache);
assertNotNull(result);
}
@Test
void buildRequirementFailure() {
final Component fromOriginComponent = new Resource();
final ComponentInstance fromInstance = new ComponentInstance();
final String fromInstanceUid = "fromInstanceUid";
fromInstance.setUniqueId(fromInstanceUid);
fromInstance.setComponentUid("componentUid");
final RequirementCapabilityRelDef relationshipDefinition = new RequirementCapabilityRelDef();
relationshipDefinition.setToNode("wrongNodeUid");
final List<CapabilityRequirementRelationship> relationshipList = new ArrayList<>();
final CapabilityRequirementRelationship relationship = new CapabilityRequirementRelationship();
relationship.setRequirement(new RequirementDataDefinition());
relationshipList.add(relationship);
relationshipDefinition.setRelationships(relationshipList);
final List<ComponentInstance> instancesList = new ArrayList<>();
instancesList.add(fromInstance);
String expectedError = String
.format("Failed to find a relation from the node %s to the node %s", fromInstance.getName(),
relationshipDefinition.getToNode());
assertThrows(ToscaExportException.class, () ->
Deencapsulation.invoke(testSubject, "buildRequirement", fromInstance, fromOriginComponent,
instancesList, relationshipDefinition, new HashMap<>()), expectedError);
try {
Deencapsulation.invoke(testSubject, "buildRequirement", fromInstance, fromOriginComponent,
instancesList, relationshipDefinition, new HashMap<>());
} catch (Exception e) {
assertTrue(e instanceof ToscaExportException);
assertEquals(expectedError, e.getMessage());
}
final RelationshipInfo relation = new RelationshipInfo();
final String requirementUid = "Uid";
relation.setRequirementUid(requirementUid);
final String requirementName = "requirementName";
relation.setRequirement(requirementName);
final String capabilityName = "capabilityName";
relation.setCapability(capabilityName);
final String capabilityOwnerId = "capabilityOwnerId";
relation.setCapabilityOwnerId(capabilityOwnerId);
relationship.setRelation(relation);
final Map<String, List<RequirementDefinition>> requirementMap = new HashMap<>();
final RequirementDefinition requirementDefinition = new RequirementDefinition();
requirementMap.put(requirementUid, Collections.singletonList(requirementDefinition));
fromOriginComponent.setRequirements(requirementMap);
relationshipDefinition.setToNode(fromInstanceUid);
expectedError = String
.format("Failed to find a requirement with uniqueId %s on a component with uniqueId %s",
relation.getRequirementUid(), fromOriginComponent.getUniqueId());
assertThrows(ToscaExportException.class, () ->
Deencapsulation.invoke(testSubject, "buildRequirement", fromInstance, fromOriginComponent,
instancesList, relationshipDefinition, new HashMap<>()), expectedError);
requirementDefinition.setName(requirementName);
when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class)))
.thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
expectedError = String.format(
"Failed to build substituted name for the requirement %s. "
+ "Failed to get an origin component with uniqueId %s",
requirementName, fromInstance.getActualComponentUid());
assertThrows(ToscaExportException.class, () -> Deencapsulation
.invoke(testSubject, "buildRequirement", fromInstance, fromOriginComponent, instancesList,
relationshipDefinition, new HashMap<>()), expectedError);
final Component toOriginComponent = new Resource();
final Map<String, List<CapabilityDefinition>> capabilityMap = new HashMap<>();
final CapabilityDefinition capabilityDefinition = new CapabilityDefinition();
capabilityDefinition.setName(capabilityName);
capabilityDefinition.setOwnerId(capabilityOwnerId);
capabilityDefinition.setType("aType");
final String capabilityPreviousName = "capabilityPreviousName";
capabilityDefinition.setPreviousName(capabilityPreviousName);
capabilityMap.put(capabilityName, Collections.singletonList(capabilityDefinition));
toOriginComponent.setCapabilities(capabilityMap);
when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class)))
.thenReturn(Either.left(toOriginComponent));
requirementDefinition.setCapability(capabilityName);
relation.setCapability("wrong");
final String requirementPreviousName = "requirementPreviousName";
requirementDefinition.setPreviousName(requirementPreviousName);
requirementDefinition.setPath(new ArrayList<>());
expectedError = String
.format("Failed to find a capability with name %s on a component with uniqueId %s",
relation.getCapability(), fromOriginComponent.getUniqueId());
assertThrows(ToscaExportException.class, () -> Deencapsulation
.invoke(testSubject, "buildRequirement", fromInstance, fromOriginComponent, instancesList,
relationshipDefinition, new HashMap<>()),
expectedError);
}
@Test
void testBuildRequirement() {
final ComponentInstance fromInstance = new ComponentInstance();
fromInstance.setUniqueId("name");
fromInstance.setComponentUid("string");
final List<ComponentInstance> instancesList = new ArrayList<>();
final Map<String, Component> componentCache = new HashMap<>();
final List<CapabilityRequirementRelationship> relationshipList = new ArrayList<>();
final CapabilityRequirementRelationship relationship = new CapabilityRequirementRelationship();
relationship.setRequirement(new RequirementDataDefinition());
final RelationshipInfo relation = new RelationshipInfo();
final String requirementUid = "Uid";
relation.setRequirementUid(requirementUid);
final String requirementName = "requirementName";
relation.setRequirement(requirementName);
final String capabilityName = "capabilityName";
relation.setCapability(capabilityName);
final String capabilityOwnerId = "capabilityOwnerId";
relation.setCapabilityOwnerId(capabilityOwnerId);
relationship.setRelation(relation);
relationshipList.add(relationship);
final RequirementCapabilityRelDef relationshipDefinition = new RequirementCapabilityRelDef();
relationshipDefinition.setRelationships(relationshipList);
relationshipDefinition.setToNode("name");
instancesList.add(fromInstance);
final RequirementDefinition requirementDefinition = new RequirementDefinition();
requirementDefinition.setName(requirementName);
requirementDefinition.setCapability(capabilityName);
final String requirementPreviousName = "requirementPreviousName";
requirementDefinition.setPreviousName(requirementPreviousName);
requirementDefinition.setPath(new ArrayList<>());
final Map<String, List<RequirementDefinition>> requirementMap = new HashMap<>();
requirementMap.put(requirementUid, Collections.singletonList(requirementDefinition));
final Component fromOriginComponent = new Resource();
fromOriginComponent.setRequirements(requirementMap);
final Map<String, List<CapabilityDefinition>> capabilityMap = new HashMap<>();
final CapabilityDefinition capabilityDefinition = new CapabilityDefinition();
capabilityDefinition.setName(capabilityName);
capabilityDefinition.setOwnerId(capabilityOwnerId);
final String capabilityPreviousName = "capabilityPreviousName";
capabilityDefinition.setPreviousName(capabilityPreviousName);
capabilityMap.put(capabilityName, Collections.singletonList(capabilityDefinition));
final Component toOriginComponent = new Resource();
toOriginComponent.setCapabilities(capabilityMap);
when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class)))
.thenReturn(Either.left(toOriginComponent));
final String builtCapabilityName = "builtCapabilityName";
when(
capabilityRequirementConverter
.buildSubstitutedName(anyMap(), eq(toOriginComponent), anyList(), eq(capabilityName), eq(
capabilityPreviousName), any()))
.thenReturn(Either.left(builtCapabilityName));
final String builtRequirementName = "builtRequirementName";
when(
capabilityRequirementConverter
.buildSubstitutedName(anyMap(), eq(fromOriginComponent), anyList(), eq(requirementName), eq(
requirementPreviousName), any()))
.thenReturn(Either.left(builtRequirementName));
final Map<String, ToscaTemplateRequirement> actualRequirementMap =
Deencapsulation.invoke(testSubject, "buildRequirement", fromInstance, fromOriginComponent,
instancesList, relationshipDefinition, componentCache);
assertNotNull(actualRequirementMap);
assertFalse(actualRequirementMap.isEmpty());
assertTrue(actualRequirementMap.containsKey(builtRequirementName));
final ToscaTemplateRequirement actualToscaTemplateRequirement = actualRequirementMap.get(builtRequirementName);
assertNotNull(actualToscaTemplateRequirement);
assertEquals(builtCapabilityName, actualToscaTemplateRequirement.getCapability());
//to toOriginComponent not found
when(toscaOperationFacade.getToscaElement(any(String.class), any(ComponentParametersView.class)))
.thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
assertThrows(ToscaExportException.class, () -> Deencapsulation.invoke(testSubject, "buildRequirement", fromInstance, fromOriginComponent,
instancesList, relationshipDefinition, componentCache));
}
@Test
void testAddRequirmentsWithBuildAndAddRequirements() {
ComponentInstance fromInstance = new ComponentInstance();
Component fromOriginComponent = new Resource();
List<ComponentInstance> instancesList = new ArrayList<>();
RequirementCapabilityRelDef rel = new RequirementCapabilityRelDef();
List<Map<String, ToscaTemplateRequirement>> toscaRequirements = new ArrayList<>();
Map<String, Component> componentCache = new HashMap<>();
List<CapabilityRequirementRelationship> relationships = new ArrayList<>();
CapabilityRequirementRelationship cap = new CapabilityRequirementRelationship();
cap.setRequirement(new RequirementDataDefinition());
RelationshipInfo relation = new RelationshipInfo();
relation.setRequirementUid("Uid");
relation.setRequirement("requirment");
relation.setCapability("cap");
relation.setCapabilityOwnerId("id");
cap.setRelation(relation);
relationships.add(cap);
rel.setRelationships(relationships);
rel.setToNode("name");
fromInstance.setUniqueId("name");
fromInstance.setComponentUid("string");
instancesList.add(fromInstance);
Map<String, List<RequirementDefinition>> requirements = new HashMap<>();
List<RequirementDefinition> defs = new ArrayList<>();
RequirementDefinition def = new RequirementDefinition();
def.setName("requirment");
def.setCapability("cap");
defs.add(def);
requirements.put("key", defs);
fromOriginComponent.setRequirements(requirements);
Map<String, List<CapabilityDefinition>> capabilities = new HashMap<>();
List<CapabilityDefinition> caps = new ArrayList<>();
CapabilityDefinition capdef = new CapabilityDefinition();
capdef.setOwnerId("id");
capdef.setName("cap");
capdef.setPreviousName("before cap");
capdef.setType("type");
caps.add(capdef);
capabilities.put("cap", caps);
fromOriginComponent.setCapabilities(capabilities);
when(toscaOperationFacade.getToscaElement(any(String.class),
any(ComponentParametersView.class))).thenReturn(Either.left(fromOriginComponent));
when(
capabilityRequirementConverter
.buildSubstitutedName(anyMap(), any(Component.class), anyList(), anyString(), anyString(), any()))
.thenReturn(Either.right(false));
final String expectedErrorMsg =
String.format("Failed to build a substituted capability name for the capability "
+ "with name %s on a component with uniqueId %s",
cap.getRequirement(), fromOriginComponent.getUniqueId());
assertThrows(ToscaExportException.class, () ->
Deencapsulation.invoke(testSubject, "buildRequirement", fromInstance, fromOriginComponent, instancesList,
rel, componentCache), expectedErrorMsg);
}
@Test
void testBuildAndAddRequirement() {
Component fromOriginComponent = new Resource();
Component toOriginComponent = new Resource();
CapabilityDefinition capability = new CapabilityDefinition();
RequirementDefinition requirement = new RequirementDefinition();
RelationshipInfo reqAndRelationshipPair = new RelationshipInfo();
ComponentInstance toInstance = new ComponentInstance();
Map<String, Component> componentCache = new HashMap<>();
final CapabilityRequirementRelationship capabilityRequirementRelationship = new CapabilityRequirementRelationship();
capabilityRequirementRelationship.setRelation(reqAndRelationshipPair);
capability.setPath(new ArrayList<>());
capability.setPreviousName("before cap");
reqAndRelationshipPair.setCapability("cap");
requirement.setPath(new ArrayList<>());
requirement.setPreviousName("before req");
reqAndRelationshipPair.setRequirement("req");
when(
capabilityRequirementConverter
.buildSubstitutedName(anyMap(), eq(toOriginComponent), anyList(), eq("cap"), anyString(), any()))
.thenReturn(Either.left("buildCapNameRes"));
when(
capabilityRequirementConverter
.buildSubstitutedName(anyMap(), eq(fromOriginComponent), anyList(), eq("req"), anyString(), any()))
.thenReturn(Either.left("buildReqNameRes"));
// default test
final Map<String, ToscaTemplateRequirement> requirementMap =
Deencapsulation.invoke(testSubject, "buildRequirement", fromOriginComponent, toOriginComponent,
capability, requirement, capabilityRequirementRelationship, toInstance, componentCache);
assertNotNull(requirementMap);
assertFalse(requirementMap.isEmpty());
assertTrue(requirementMap.containsKey("buildReqNameRes"));
final ToscaTemplateRequirement actualToscaTemplateRequirement = requirementMap.get("buildReqNameRes");
assertNotNull(actualToscaTemplateRequirement);
assertEquals("buildCapNameRes", actualToscaTemplateRequirement.getCapability());
}
@Test
void testBuildRequirementBuildSubstitutedNameReturnsValueTwice() {
final Component fromOriginComponent = new Resource();
final Component toOriginComponent = new Resource();
final CapabilityDefinition capability = new CapabilityDefinition();
final RequirementDefinition requirement = new RequirementDefinition();
final RelationshipInfo relationship = new RelationshipInfo();
final CapabilityRequirementRelationship capabilityRequirementRelationship = new CapabilityRequirementRelationship();
capabilityRequirementRelationship.setRelation(relationship);
ComponentInstance toInstance = new ComponentInstance();
Map<String, Component> componentCache = new HashMap<>();
capability.setPath(new ArrayList<>());
relationship.setCapability("cap");
requirement.setPath(new ArrayList<>());
relationship.setRequirement("req");
final String builtCapabilityOrRequirementName = "builtCapabilityOrRequirementName";
when(capabilityRequirementConverter.buildSubstitutedName(anyMap(), eq(fromOriginComponent), anyList(), eq("cap"), any(), any()))
.thenReturn(Either.left(builtCapabilityOrRequirementName));
when(capabilityRequirementConverter.buildSubstitutedName(anyMap(), eq(toOriginComponent), anyList(), eq("req"), any(), any()))
.thenReturn(Either.left(builtCapabilityOrRequirementName));
final Map<String, ToscaTemplateRequirement> requirementMap = Deencapsulation
.invoke(testSubject, "buildRequirement", fromOriginComponent, toOriginComponent, capability, requirement,
capabilityRequirementRelationship, toInstance, componentCache);
assertNotNull(requirementMap);
assertFalse(requirementMap.isEmpty());
assertTrue(requirementMap.containsKey(builtCapabilityOrRequirementName));
final ToscaTemplateRequirement actualToscaTemplateRequirement = requirementMap.get(builtCapabilityOrRequirementName);
assertNotNull(actualToscaTemplateRequirement);
assertEquals(builtCapabilityOrRequirementName, actualToscaTemplateRequirement.getCapability());
}
@Test
void testIsRequirementBelongToRelation() {
Component originComponent = new Resource();
RelationshipInfo reqAndRelationshipPair = new RelationshipInfo();
RequirementDefinition requirement = new RequirementDefinition();
String fromInstanceId = "";
boolean result;
requirement.setName("name");
reqAndRelationshipPair.setRequirement("name1");
// test return false
result = Deencapsulation.invoke(testSubject, "isRequirementBelongToRelation", originComponent,
reqAndRelationshipPair, requirement, fromInstanceId);
assertFalse(result);
}
@Test
void testIsRequirementBelongToRelationWithNonAtomicComponent() {
Component originComponent = new Service();
RelationshipInfo reqAndRelationshipPair = new RelationshipInfo();
RequirementDefinition requirement = new RequirementDefinition();
requirement.setUniqueId(UNIQUE_ID);
reqAndRelationshipPair.setRequirementUid(UNIQUE_ID);
String fromInstanceId = "";
boolean result;
// default test return true
result = Deencapsulation.invoke(testSubject, "isRequirementBelongToRelation", originComponent,
reqAndRelationshipPair, requirement, fromInstanceId);
assertTrue(result);
}
@Test
void testIsRequirementBelongToOwner() {
RelationshipInfo reqAndRelationshipPair = new RelationshipInfo();
RequirementDefinition requirement = new RequirementDefinition();
String fromInstanceId = "";
Component originComponent = new Resource();
boolean result;
requirement.setOwnerId("owner1");
reqAndRelationshipPair.setRequirementOwnerId("owner");
// default test
result = Deencapsulation.invoke(testSubject, "isRequirementBelongToOwner", reqAndRelationshipPair, requirement,
fromInstanceId, originComponent);
assertFalse(result);
}
@Test
void testIsCvfc() {
Component component = new Service();
boolean result;
result = Deencapsulation.invoke(testSubject, "isCvfc", component);
assertFalse(result);
}
@Test
void testConvertCapabilities() {
final Component component = new Resource();
final Map<String, Component> componentCache = new HashMap<>();
final Either<Map<String, String[]>, ToscaError> expectedResult = Either.right(ToscaError.NODE_TYPE_CAPABILITY_ERROR);
when(capabilityRequirementConverter.convertSubstitutionMappingCapabilities(componentCache, component))
.thenReturn(expectedResult);
// default test return isRight
final Either<Map<String, String[]>, ToscaError> actualResult =
Deencapsulation.invoke(testSubject, "convertSubstitutionMappingCapabilities", component, componentCache);
assertNotNull(actualResult);
assertEquals(expectedResult, actualResult);
}
@Test
void testConvertCapabilities_1() {
Component component = new Resource();
ToscaNodeType nodeType = new ToscaNodeType();
Map<String, DataTypeDefinition> dataTypes = new HashMap<>();
Either<ToscaNodeType, ToscaError> result;
Map<String, ToscaCapability> capabilities = new HashMap<>();
capabilities.put("key", new ToscaCapability());
// default test
result = Deencapsulation
.invoke(testSubject, "convertCapabilities", new HashMap<>(), component, nodeType, dataTypes);
assertNotNull(result);
}
@Test
void testConvertToNodeTemplateArtifacts() {
Map<String, ToscaArtifactDataDefinition> container = new HashMap<>();
ToscaArtifactDataDefinition art = new ToscaArtifactDataDefinition();
art.setFile("test_file");
art.setType("test_type");
Map<String, ToscaTemplateArtifact> result;
container.put("test_art", art);
result = Deencapsulation.invoke(testSubject, "convertToNodeTemplateArtifacts", container);
assertNotNull(result);
assertTrue(MapUtils.isNotEmpty(result));
assertEquals("test_file", result.get("test_art").getFile());
assertEquals("test_type", result.get("test_art").getType());
}
private Component getTestComponent() {
Component component = new Service();
component.setUniqueId("serviceUniqueId");
component.setNormalizedName("normalizedServiceComponentName");
InterfaceDefinition addedInterface = new InterfaceDefinition();
addedInterface.setType("com.some.service.or.other.serviceName");
final String interfaceType = "normalizedServiceComponentName-interface";
component.setInterfaces(new HashMap<>());
component.getInterfaces().put(interfaceType, addedInterface);
return component;
}
private PropertyDefinition createMockProperty(String propertyName, String defaultValue) {
PropertyDefinition propertyDefinition = new PropertyDefinition();
propertyDefinition.setName(propertyName);
propertyDefinition.setType("string");
propertyDefinition.setDefaultValue(defaultValue);
return propertyDefinition;
}
private InputDefinition createMockInput(String inputName, String defaultValue) {
InputDefinition inputDefinition = new InputDefinition();
inputDefinition.setName(inputName);
inputDefinition.setType("string");
inputDefinition.setDefaultValue(defaultValue);
return inputDefinition;
}
}
|