From d02143c0c616a32dcc095877fea5c87c05e4adab Mon Sep 17 00:00:00 2001 From: Bogumil Zebek Date: Fri, 18 Sep 2020 09:11:26 +0200 Subject: Fix an unexpected error in tests - Fix an error reported by DMaapEventPublisher test when pk is not available. Issue-ID: DCAEGEN2-2374 Change-Id: I69c0f89b292c8454f3eebaffe4884928d241f235 Signed-off-by: Zebek Bogumil --- .../java/org/onap/dcae/common/model/VesEvent.java | 112 +++++++++++++-------- 1 file changed, 69 insertions(+), 43 deletions(-) (limited to 'src/main/java/org/onap/dcae/common/model/VesEvent.java') diff --git a/src/main/java/org/onap/dcae/common/model/VesEvent.java b/src/main/java/org/onap/dcae/common/model/VesEvent.java index 6c9a8ee2..8e2db80e 100644 --- a/src/main/java/org/onap/dcae/common/model/VesEvent.java +++ b/src/main/java/org/onap/dcae/common/model/VesEvent.java @@ -32,14 +32,15 @@ import org.json.JSONObject; */ public class VesEvent { - private static final String EVENT_LITERAL = "event"; + public static final String VES_UNIQUE_ID = "VESuniqueId"; private static final String COMMON_EVENT_HEADER = "commonEventHeader"; - private static final String VES_UNIQUE_ID = "VESuniqueId"; private static final String DOMAIN = "domain"; private static final String STND_DEFINED_NAMESPACE = "stndDefinedNamespace"; private static final String STND_DEFINED_DOMAIN = "stndDefined"; private static final String STND_DEFINED_FIELDS = "stndDefinedFields"; private static final String SCHEMA_REFERENCE = "schemaReference"; + private static final String EVENT = "event"; + private static final String PARTITION_KEY = "sourceName"; private final JSONObject event; @@ -47,6 +48,45 @@ public class VesEvent { this.event = event; } + public JsonNode asJsonNode() throws JsonProcessingException { + ObjectMapper objectMapper = new ObjectMapper(); + return objectMapper.readTree(event.toString()); + } + + /** + * Returns VES event in form of JSON object. + * + * @return event in form of json Object + */ + public JSONObject asJsonObject() { + return new JSONObject(event.toString()); + } + + /** + * Returns Domain name from VES event. + * + * @return domain + */ + public String getDomain() { + return getEventHeader().getString(DOMAIN); + } + + /** + * Returns event primary key. + * @return a primary key + */ + public String getPK() { + return event.getJSONObject(EVENT).getJSONObject(COMMON_EVENT_HEADER).get(PARTITION_KEY).toString(); + } + + /** + * Returns schema reference. + * @return a schema reference. + */ + public String getSchemaReference() { + return getStndDefinedFields().getString(SCHEMA_REFERENCE); + } + /** * Returns stream ID from VES event. * @@ -63,21 +103,40 @@ public class VesEvent { } /** - * Returns Domain name from VES event. + * Returns unique ID of VES event. * - * @return domain + * @return unique ID */ - public String getDomain() { - return getEventHeader().getString(DOMAIN); + public Object getUniqueId() { + return event.get(VES_UNIQUE_ID); } - public String getSchemaReference() { - return getStndDefinedFields().getString(SCHEMA_REFERENCE); + /** + * Checks if type of event is same as given in paramaters. + * + * @param type name that will be compared with event type + * @return true or false depending if type given in parameter is same as VES event type + */ + public boolean hasType(String type) { + return this.event.has(type); + } + + /** + * Remove Json element from event by key. + * @param key + */ + public void removeElement(String key) { + this.event.remove(key); + } + + @Override + public String toString() { + return event.toString(); } private JSONObject getStndDefinedFields() { return event - .getJSONObject(EVENT_LITERAL) + .getJSONObject(EVENT) .getJSONObject(STND_DEFINED_FIELDS); } @@ -97,44 +156,11 @@ public class VesEvent { private JSONObject getEventHeader() { return event - .getJSONObject(EVENT_LITERAL) + .getJSONObject(EVENT) .getJSONObject(COMMON_EVENT_HEADER); } private boolean isStdDefinedDomain(String domain) { return domain.equals(STND_DEFINED_DOMAIN); } - - /** - * Returns unique ID of VES event. - * - * @return unique ID - */ - public Object getUniqueId() { - return event.get(VES_UNIQUE_ID); - } - - /** - * Returns VES event in form of JSON object. - * - * @return event in form of json Object - */ - public JSONObject asJsonObject() { - return new JSONObject(event.toString()); - } - - public JsonNode asJsonNode() throws JsonProcessingException { - ObjectMapper objectMapper = new ObjectMapper(); - return objectMapper.readTree(event.toString()); - } - - /** - * Checks if type of event is same as given in paramaters. - * - * @param type name that will be compared with event type - * @return true or false depending if type given in parameter is same as VES event type - */ - public boolean hasType(String type) { - return this.event.has(type); - } } -- cgit 1.2.3-korg