summaryrefslogtreecommitdiffstats
path: root/dcaedt_be/src/test/java/org/onap/sdc/dcae/VesStructureLoaderMock.java
blob: 34db9fa0fa6f88a2d57c26e2d107be2d04ec0bc9 (plain)
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
package org.onap.sdc.dcae;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.onap.sdc.dcae.ves.EventListenerDefinition;
import org.onap.sdc.dcae.ves.VesDataItemsDefinition;
import org.onap.sdc.dcae.ves.VesDataTypeDefinition;
import org.onap.sdc.dcae.ves.VesJsonDeserializer;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.lang.reflect.Type;
import java.util.*;
import java.util.stream.Collectors;

public class VesStructureLoaderMock {

	private Map<String, EventListenerDefinition> eventListeners = new HashMap<>();
	private Type type = new TypeToken<VesDataItemsDefinition>() {
	}.getType();
	private Gson gson = new GsonBuilder().registerTypeAdapter(type, new VesJsonDeserializer()).create();
	private final String schemaNamePrefix = "CommonEventFormat_v";
	private final String schemaNameSuffix = ".json";
	private List<String> initErrors;

	public VesStructureLoaderMock() {
		this(true);
	}

	public VesStructureLoaderMock(boolean validateAndResolve) {
		this(validateAndResolve, System.getProperty("user.dir") + "/src/test/resources/ves-schema");
	}

	public VesStructureLoaderMock(boolean validateAndResolve, String path) {
		initErrors = init(validateAndResolve, path);
	}

	public List<String> init(boolean validateAndResolve, String pathToSchemaDir) {

		List<String> parseErrors = new ArrayList<>();
		File dir = new File(pathToSchemaDir);
		File[] files = dir.listFiles(new FilenameFilter() {
			@Override public boolean accept(File dir, String name) {
				return name.startsWith(schemaNamePrefix) && name.endsWith(schemaNameSuffix);
			}
		});
		if (ArrayUtils.isEmpty(files)) {
			parseErrors.add("No VES schema files found");
		} else {
			for (File f : files) {
				String error = parseJsonFileAndSaveToMap(f, validateAndResolve);
				if (StringUtils.isNotBlank(error)) {
					parseErrors.add("Error: parsing VES schema file " + f.getName() + " failed due to " + error);
				}
			}
		}
		return parseErrors;

	}

	public Map<String, VesDataTypeDefinition> getEventListenerDefinitionByVersion(String version) {
		return eventListeners.get(version).getProperties().get(EventListenerDefinition.EVENT_ROOT).getProperties();
	}

	public Set<String> getAvailableVersionsList() {
		return eventListeners.keySet();
	}

	public Map<String, Set<String>> getAvailableVersionsAndEventTypes() {
		return eventListeners.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> getEventListenerDefinitionByVersion(e.getKey()).keySet()));
	}

	public Set<String> getEventTypeListByVersion(String version) {
		return getEventListenerDefinitionByVersion(version).keySet();
	}

	public String getVersionFromFileName(String fileName) {
		return fileName.replace(schemaNamePrefix, "").replace(schemaNameSuffix, "");
	}

	private String parseJsonFileAndSaveToMap(File file, boolean validateAndResolve) {
		String validationError = null;
		try {
			EventListenerDefinition eventListener = gson.fromJson(new FileReader(file), EventListenerDefinition.class);
			if (validateAndResolve)
				validationError = getValidatorMessage(eventListener);
			if (StringUtils.isEmpty(validationError))
				eventListeners.put(getVersionFromFileName(file.getName()), eventListener);
		} catch (FileNotFoundException | JsonIOException | JsonSyntaxException e) {
			validationError = e.getMessage();
		}
		return validationError;
	}

	public Map<String, EventListenerDefinition> getEventListeners() {
		return eventListeners;
	}

	public List<String> getInitErrors() {
		return initErrors;
	}

	private String getValidatorMessage(EventListenerDefinition eventListenerDefinition) {
		String validationError = eventListenerDefinition.validate();
		if (StringUtils.isBlank(validationError))
			validationError = eventListenerDefinition.resolveRefTypes();
		return validationError;
	}
}