Move integration test (DataService)
[cps.git] / cps-service / src / main / java / org / onap / cps / notification / CpsDataUpdatedEventFactory.java
index 2985ed5..38f8988 100644 (file)
@@ -1,6 +1,8 @@
 /*
  * ============LICENSE_START=======================================================
- * Copyright (c) 2021 Bell Canada.
+ * Copyright (c) 2021-2022 Bell Canada.
+ * Modifications Copyright (c) 2022 Nordix Foundation
+ * Modifications Copyright (C) 2023 TechMahindra Ltd.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -25,7 +27,7 @@ import java.net.URISyntaxException;
 import java.time.OffsetDateTime;
 import java.time.format.DateTimeFormatter;
 import java.util.UUID;
-import org.onap.cps.api.CpsAdminService;
+import lombok.AllArgsConstructor;
 import org.onap.cps.api.CpsDataService;
 import org.onap.cps.event.model.Content;
 import org.onap.cps.event.model.CpsDataUpdatedEvent;
@@ -34,10 +36,12 @@ import org.onap.cps.spi.FetchDescendantsOption;
 import org.onap.cps.spi.model.Anchor;
 import org.onap.cps.spi.model.DataNode;
 import org.onap.cps.utils.DataMapUtils;
+import org.onap.cps.utils.PrefixResolver;
 import org.springframework.context.annotation.Lazy;
 import org.springframework.stereotype.Component;
 
 @Component
+@AllArgsConstructor(onConstructor = @__(@Lazy))
 public class CpsDataUpdatedEventFactory {
 
     private static final URI EVENT_SCHEMA;
@@ -56,35 +60,32 @@ public class CpsDataUpdatedEventFactory {
         }
     }
 
+    @Lazy
     private final CpsDataService cpsDataService;
-    private final CpsAdminService cpsAdminService;
 
-    public CpsDataUpdatedEventFactory(@Lazy final CpsDataService cpsDataService,
-        final CpsAdminService cpsAdminService) {
-        this.cpsDataService = cpsDataService;
-        this.cpsAdminService = cpsAdminService;
-    }
+    @Lazy
+    private final PrefixResolver prefixResolver;
 
     /**
      * Generates CPS Data Updated event. If observedTimestamp is not provided, then current timestamp is used.
      *
-     * @param dataspaceName     dataspaceName
-     * @param anchorName        anchorName
+     * @param anchor            anchor
      * @param observedTimestamp observedTimestamp
+     * @param operation         operation
      * @return CpsDataUpdatedEvent
      */
-    public CpsDataUpdatedEvent createCpsDataUpdatedEvent(final String dataspaceName, final String anchorName,
-        final OffsetDateTime observedTimestamp) {
-        final var dataNode = cpsDataService
-            .getDataNode(dataspaceName, anchorName, "/", FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
-        final var anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
-        return toCpsDataUpdatedEvent(anchor, dataNode, observedTimestamp);
+    public CpsDataUpdatedEvent createCpsDataUpdatedEvent(final Anchor anchor,
+        final OffsetDateTime observedTimestamp, final Operation operation) {
+        final var dataNode = (operation == Operation.DELETE) ? null :
+            cpsDataService.getDataNodes(anchor.getDataspaceName(), anchor.getName(),
+                "/", FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS).iterator().next();
+        return toCpsDataUpdatedEvent(anchor, dataNode, observedTimestamp, operation);
     }
 
     private CpsDataUpdatedEvent toCpsDataUpdatedEvent(final Anchor anchor, final DataNode dataNode,
-        final OffsetDateTime observedTimestamp) {
+        final OffsetDateTime observedTimestamp, final Operation operation) {
         final var cpsDataUpdatedEvent = new CpsDataUpdatedEvent();
-        cpsDataUpdatedEvent.withContent(createContent(anchor, dataNode, observedTimestamp));
+        cpsDataUpdatedEvent.withContent(createContent(anchor, dataNode, observedTimestamp, operation));
         cpsDataUpdatedEvent.withId(UUID.randomUUID().toString());
         cpsDataUpdatedEvent.withSchema(EVENT_SCHEMA);
         cpsDataUpdatedEvent.withSource(EVENT_SOURCE);
@@ -92,21 +93,25 @@ public class CpsDataUpdatedEventFactory {
         return cpsDataUpdatedEvent;
     }
 
-    private Data createData(final DataNode dataNode) {
-        final var data = new Data();
-        DataMapUtils.toDataMap(dataNode).forEach(data::setAdditionalProperty);
+    private Data createData(final DataNode dataNode, final String prefix) {
+        final Data data = new Data();
+        DataMapUtils.toDataMapWithIdentifier(dataNode, prefix).forEach(data::setAdditionalProperty);
         return data;
     }
 
     private Content createContent(final Anchor anchor, final DataNode dataNode,
-        final OffsetDateTime observedTimestamp) {
+        final OffsetDateTime observedTimestamp, final Operation operation) {
         final var content = new Content();
         content.withAnchorName(anchor.getName());
         content.withDataspaceName(anchor.getDataspaceName());
         content.withSchemaSetName(anchor.getSchemaSetName());
-        content.withData(createData(dataNode));
+        content.withOperation(Content.Operation.fromValue(operation.name()));
         content.withObservedTimestamp(
             DATE_TIME_FORMATTER.format(observedTimestamp == null ? OffsetDateTime.now() : observedTimestamp));
+        if (dataNode != null) {
+            final String prefix = prefixResolver.getPrefix(anchor, dataNode.getXpath());
+            content.withData(createData(dataNode, prefix));
+        }
         return content;
     }
 }