DCAE-D be initial commit
[sdc/dcae-d/dt-be-main.git] / dcaedt_be / src / test / java / org / onap / sdc / dcae / ves / VesStructureLoaderTest.java
1 package org.onap.sdc.dcae.ves;
2
3 import org.junit.Test;
4 import org.onap.sdc.dcae.VesStructureLoaderMock;
5
6 import java.util.*;
7
8 import static org.junit.Assert.assertEquals;
9 import static org.junit.Assert.assertTrue;
10
11 public class VesStructureLoaderTest {
12
13         // the file names of test schema files
14         private final String UNRESOLVABLE_REFERENCES = "CommonEventFormat_vUnresolvable.json";
15         private final String VALID_VERSION_4_1 = "CommonEventFormat_v4.1.json";
16         private final String VALID_VERSION_5_3 = "CommonEventFormat_v5.3.json";
17         private final String INVALID_JSON = "CommonEventFormat_vInvalidJson.json";
18         private final String UNSUPPORTED_FILENAME = "unsupportedFilename.json";
19         private final String INVALID_SCHEMA_STRUCTURE = "CommonEventFormat_vInvalidSchemaStructure.json";
20         private final String INVALID_TYPE = "CommonEventFormat_vInvalidType.json";
21         private final String INVALID_REQUIRED_ENTRY = "CommonEventFormat_vInvalidRequiredEntry.json";
22         private final String NO_EVENT_PROPERTY = "CommonEventFormat_vNoEventProperty.json";
23         private final String NO_COMMON_EVENT_HEADER = "CommonEventFormat_v4.1WithoutCommonEventHeader.json";
24
25         // schema directory test paths
26         private final String EMPTY_SCHEMA_DIR = System.getProperty("user.dir") + "/src/test/resources/ves-schema/empty";
27         private final String NONE_EXISTING_DIR = EMPTY_SCHEMA_DIR + "/null";
28
29         private final String ERROR_TEXT = "Error: parsing VES schema file ";
30
31         // files loaded from default path, only valid files are kept, errors logged for invalid files (initError);
32         @Test
33         public void defaultInit() {
34                 VesStructureLoaderMock loader = new VesStructureLoaderMock();
35                 Set<String> expectedAvailableVersions = new HashSet<>();
36                 expectedAvailableVersions.add(loader.getVersionFromFileName(VALID_VERSION_4_1));
37                 expectedAvailableVersions.add(loader.getVersionFromFileName(VALID_VERSION_5_3));
38                 expectedAvailableVersions.add(loader.getVersionFromFileName(NO_COMMON_EVENT_HEADER));
39                 assertEquals(expectedAvailableVersions, loader.getAvailableVersionsList());
40                 List<String> expectedLoggedErrors = Arrays
41                                 .asList(getExpectedInvalidJsonError(), getExpectedInvalidRequiredEntryError(), getExpectedInvalidStructureError(), getExpectedInvalidTypeError(), getExpectedNoEventDefinitionError(), getExpectedUnresolvableError());
42                 assertTrue(loader.getInitErrors().containsAll(expectedLoggedErrors));
43                 assertEquals(expectedLoggedErrors.size(), loader.getInitErrors().size());
44         }
45
46         @Test
47         public void initWithEmptyDir() {
48                 VesStructureLoaderMock loader = new VesStructureLoaderMock(true, EMPTY_SCHEMA_DIR);
49                 assertTrue(loader.getAvailableVersionsList().isEmpty());
50                 assertEquals("No VES schema files found", loader.getInitErrors().get(0));
51         }
52
53         @Test
54         public void initWithNoneExistingDir() {
55                 VesStructureLoaderMock loader = new VesStructureLoaderMock(true, NONE_EXISTING_DIR);
56                 assertTrue(loader.getAvailableVersionsList().isEmpty());
57                 assertEquals("No VES schema files found", loader.getInitErrors().get(0));
58         }
59
60         @Test
61         public void complexDataTypeLoaderOutputTest() {
62                 VesStructureLoaderMock loader = new VesStructureLoaderMock();
63                 VesDataTypeDefinition loaded = loader.getEventListenerDefinitionByVersion("5.3").get("stateChangeFields");
64                 assertEquals(buildStateChangeFieldsDefinition(), loaded);
65         }
66
67         private String getExpectedInvalidJsonError() {
68                 return ERROR_TEXT + INVALID_JSON + " failed due to java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at path $";
69         }
70
71         private String getExpectedUnresolvableError() {
72                 return ERROR_TEXT + UNRESOLVABLE_REFERENCES + " failed due to the following definitions containing unresolvable references: [\"otherFields\",\"stateChangeFields\",\"syslogFields\",\"thresholdCrossingAlertFields\"]";
73         }
74
75         private String getExpectedInvalidStructureError() {
76                 return ERROR_TEXT + INVALID_SCHEMA_STRUCTURE + " failed due to java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 8 column 20 path $.definitions..properties[0]";
77         }
78
79         private String getExpectedInvalidTypeError() {
80                 return ERROR_TEXT + INVALID_TYPE + " failed due to invalid type declaration: invalid";
81         }
82
83         private String getExpectedInvalidRequiredEntryError() {
84                 return ERROR_TEXT + INVALID_REQUIRED_ENTRY + " failed due to invalid required entry: codecIdentifier(invalid)";
85         }
86
87         private String getExpectedNoEventDefinitionError() {
88                 return ERROR_TEXT + NO_EVENT_PROPERTY + " failed due to schema not containing property: event";
89         }
90
91         private VesDataTypeDefinition buildFieldDefinition() {
92                 Map<String, VesDataTypeDefinition> propsMap = new HashMap<>();
93                 VesDataTypeDefinition prop = buildVesDataType(null, VesSimpleTypesEnum.STRING.getType(), new ArrayList<>(), null, null);
94                 propsMap.put("name", prop);
95                 propsMap.put("value", prop);
96                 return buildVesDataType("name value pair", VesSimpleTypesEnum.OBJECT.getType(), Arrays.asList("name", "value"), propsMap, null);
97         }
98
99         private VesDataTypeDefinition buildStateChangeFieldsDefinition() {
100
101                 VesDataItemsDefinition items = new VesDataItemsDefinition();
102                 items.add(buildFieldDefinition());
103                 VesDataTypeDefinition prop = buildVesDataType("additional stateChange fields if needed", VesSimpleTypesEnum.ARRAY.getType(), new ArrayList<>(), null, null);
104                 prop.setItems(items);
105                 Map<String, VesDataTypeDefinition> propsMap = new HashMap<>();
106                 propsMap.put("additionalFields", prop);
107                 prop = buildVesDataType("new state of the entity", VesSimpleTypesEnum.STRING.getType(), new ArrayList<>(), null, Arrays.asList("inService", "maintenance", "outOfService"));
108                 propsMap.put("newState", prop);
109                 prop = buildVesDataType("previous state of the entity", VesSimpleTypesEnum.STRING.getType(), new ArrayList<>(), null, Arrays.asList("inService", "maintenance", "outOfService"));
110                 propsMap.put("oldState", prop);
111                 prop = buildVesDataType("version of the stateChangeFields block", VesSimpleTypesEnum.NUMBER.getType(), new ArrayList<>(), null, null);
112                 propsMap.put("stateChangeFieldsVersion", prop);
113                 prop = buildVesDataType("card or port name of the entity that changed state", VesSimpleTypesEnum.STRING.getType(), new ArrayList<>(), null, null);
114                 propsMap.put("stateInterface", prop);
115                 VesDataTypeDefinition def = buildVesDataType("stateChange fields", VesSimpleTypesEnum.OBJECT.getType(), Arrays.asList("newState", "oldState", "stateChangeFieldsVersion", "stateInterface"), propsMap, null);
116                 return def;
117         }
118
119         private VesDataTypeDefinition buildVesDataType(String description, String type, List<String> required, Map<String, VesDataTypeDefinition> properties, List<String> enums) {
120                 VesDataTypeDefinition def = new VesDataTypeDefinition();
121                 def.setDescription(description);
122                 def.setType(type);
123                 def.setRequired(required);
124                 def.setEnums(enums);
125                 def.setProperties(properties);
126                 return def;
127         }
128
129 }