CpsValidator.validateNameCharacters(dataspaceName, anchorName);
final DataNode dataNode = buildDataNode(dataspaceName, anchorName, ROOT_NODE_XPATH, jsonData);
cpsDataPersistenceService.storeDataNode(dataspaceName, anchorName, dataNode);
- processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, ROOT_NODE_XPATH, CREATE);
+ processDataUpdatedEventAsync(dataspaceName, anchorName, ROOT_NODE_XPATH, CREATE, observedTimestamp);
}
@Override
CpsValidator.validateNameCharacters(dataspaceName, anchorName);
final DataNode dataNode = buildDataNode(dataspaceName, anchorName, parentNodeXpath, jsonData);
cpsDataPersistenceService.addChildDataNode(dataspaceName, anchorName, parentNodeXpath, dataNode);
- processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, CREATE);
+ processDataUpdatedEventAsync(dataspaceName, anchorName, parentNodeXpath, CREATE, observedTimestamp);
}
@Override
buildDataNodes(dataspaceName, anchorName, parentNodeXpath, jsonData);
cpsDataPersistenceService.addListElements(dataspaceName, anchorName, parentNodeXpath,
listElementDataNodeCollection);
- processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, UPDATE);
+ processDataUpdatedEventAsync(dataspaceName, anchorName, parentNodeXpath, UPDATE, observedTimestamp);
}
@Override
final DataNode dataNode = buildDataNode(dataspaceName, anchorName, parentNodeXpath, jsonData);
cpsDataPersistenceService
.updateDataLeaves(dataspaceName, anchorName, dataNode.getXpath(), dataNode.getLeaves());
- processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, UPDATE);
+ processDataUpdatedEventAsync(dataspaceName, anchorName, parentNodeXpath, UPDATE, observedTimestamp);
}
@Override
for (final DataNode dataNodeUpdate : dataNodeUpdates) {
processDataNodeUpdate(dataspaceName, anchorName, dataNodeUpdate);
}
- processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, UPDATE);
+ processDataUpdatedEventAsync(dataspaceName, anchorName, parentNodeXpath, UPDATE, observedTimestamp);
}
@Override
CpsValidator.validateNameCharacters(dataspaceName, anchorName);
final DataNode dataNode = buildDataNode(dataspaceName, anchorName, parentNodeXpath, jsonData);
cpsDataPersistenceService.replaceDataNodeTree(dataspaceName, anchorName, dataNode);
- processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, UPDATE);
+ processDataUpdatedEventAsync(dataspaceName, anchorName, parentNodeXpath, UPDATE, observedTimestamp);
}
@Override
final Collection<DataNode> dataNodes, final OffsetDateTime observedTimestamp) {
CpsValidator.validateNameCharacters(dataspaceName, anchorName);
cpsDataPersistenceService.replaceListContent(dataspaceName, anchorName, parentNodeXpath, dataNodes);
- processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, UPDATE);
+ processDataUpdatedEventAsync(dataspaceName, anchorName, parentNodeXpath, UPDATE, observedTimestamp);
}
@Override
final OffsetDateTime observedTimestamp) {
CpsValidator.validateNameCharacters(dataspaceName, anchorName);
cpsDataPersistenceService.deleteDataNode(dataspaceName, anchorName, dataNodeXpath);
- processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, dataNodeXpath, DELETE);
+ processDataUpdatedEventAsync(dataspaceName, anchorName, dataNodeXpath, DELETE, observedTimestamp);
}
@Override
public void deleteDataNodes(final String dataspaceName, final String anchorName,
final OffsetDateTime observedTimestamp) {
CpsValidator.validateNameCharacters(dataspaceName, anchorName);
- final Anchor anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
+ processDataUpdatedEventAsync(dataspaceName, anchorName, ROOT_NODE_XPATH, DELETE, observedTimestamp);
cpsDataPersistenceService.deleteDataNodes(dataspaceName, anchorName);
- processDataUpdatedEventAsync(anchor, ROOT_NODE_XPATH, DELETE, observedTimestamp);
}
@Override
final OffsetDateTime observedTimestamp) {
CpsValidator.validateNameCharacters(dataspaceName, anchorName);
cpsDataPersistenceService.deleteListDataNode(dataspaceName, anchorName, listNodeXpath);
- processDataUpdatedEventAsync(dataspaceName, anchorName, observedTimestamp, listNodeXpath, DELETE);
+ processDataUpdatedEventAsync(dataspaceName, anchorName, listNodeXpath, DELETE, observedTimestamp);
}
private DataNode buildDataNode(final String dataspaceName, final String anchorName,
}
- private void processDataUpdatedEventAsync(final String dataspaceName, final String anchorName,
- final OffsetDateTime observedTimestamp, final String xpath,
- final Operation operation) {
- final Anchor anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
- this.processDataUpdatedEventAsync(anchor, xpath, operation, observedTimestamp);
- }
-
- private void processDataUpdatedEventAsync(final Anchor anchor,
- final String xpath,
- final Operation operation,
- final OffsetDateTime observedTimestamp) {
+ private void processDataUpdatedEventAsync(final String dataspaceName, final String anchorName, final String xpath,
+ final Operation operation, final OffsetDateTime observedTimestamp) {
try {
- notificationService.processDataUpdatedEvent(anchor, observedTimestamp, xpath,
- operation);
+ notificationService.processDataUpdatedEvent(dataspaceName, anchorName, xpath, operation, observedTimestamp);
} catch (final Exception exception) {
//If async message can't be queued for notification service, the initial request should not failed.
log.error("Failed to send message to notification service", exception);
import javax.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
+import org.onap.cps.api.CpsAdminService;
import org.onap.cps.spi.model.Anchor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
private final NotificationPublisher notificationPublisher;
private final CpsDataUpdatedEventFactory cpsDataUpdatedEventFactory;
private final NotificationErrorHandler notificationErrorHandler;
+ private final CpsAdminService cpsAdminService;
private List<Pattern> dataspacePatterns;
@PostConstruct
/**
* Process Data Updated Event and publishes the notification.
*
- * @param anchor anchor
- * @param observedTimestamp observedTimestamp
+ * @param dataspaceName dataspaceName
+ * @param anchorName anchorName
* @param xpath xpath of changed data node
* @param operation operation
+ * @param observedTimestamp observedTimestamp
* @return future
*/
@Async("notificationExecutor")
- public Future<Void> processDataUpdatedEvent(final Anchor anchor, final OffsetDateTime observedTimestamp,
- final String xpath, final Operation operation) {
+ public Future<Void> processDataUpdatedEvent(final String dataspaceName, final String anchorName,
+ final String xpath, final Operation operation, final OffsetDateTime observedTimestamp) {
+
+ final Anchor anchor = cpsAdminService.getAnchor(dataspaceName, anchorName);
log.debug("process data updated event for anchor '{}'", anchor);
try {
- if (shouldSendNotification(anchor.getDataspaceName())) {
+ if (shouldSendNotification(dataspaceName)) {
final var cpsDataUpdatedEvent =
- cpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor,
- observedTimestamp, getRootNodeOperation(xpath, operation));
+ cpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor,
+ observedTimestamp, getRootNodeOperation(xpath, operation));
log.debug("data updated event to be published {}", cpsDataUpdatedEvent);
notificationPublisher.sendNotification(cpsDataUpdatedEvent);
}
CPS operation should not fail if sending event fails for any reason.
*/
notificationErrorHandler.onException("Failed to process cps-data-updated-event.",
- exception, anchor, xpath, operation);
+ exception, anchor, xpath, operation);
}
return CompletableFuture.completedFuture(null);
}
1 * mockCpsDataPersistenceService.storeDataNode(dataspaceName, anchorName,
{ dataNode -> dataNode.xpath == '/test-tree' })
and: 'data updated event is sent to notification service'
- 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/', Operation.CREATE)
+ 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, '/', Operation.CREATE, observedTimestamp)
}
def 'Saving json data with invalid #scenario.'() {
then: 'a data validation exception is thrown'
thrown(DataValidationException)
and: 'the persistence service method is not invoked'
- 0 * mockCpsDataPersistenceService.storeDataNode(_, _, _)
+ 0 * mockCpsDataPersistenceService.storeDataNode(*_)
and: 'data updated event is not sent to notification service'
- 0 * mockNotificationService.processDataUpdatedEvent(_, _, _, _)
+ 0 * mockNotificationService.processDataUpdatedEvent(*_)
where: 'the following parameters are used'
scenario | dataspaceName | anchorName
'dataspace name' | 'dataspace names with spaces' | 'anchorName'
1 * mockCpsDataPersistenceService.addChildDataNode(dataspaceName, anchorName, '/test-tree',
{ dataNode -> dataNode.xpath == '/test-tree/branch[@name=\'New\']' })
and: 'data updated event is sent to notification service'
- 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/test-tree', Operation.CREATE)
+ 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, '/test-tree', Operation.CREATE, observedTimestamp)
}
def 'Saving child data fragment under existing node with invalid #scenario.'() {
then: 'a data validation exception is thrown'
thrown(DataValidationException)
and: 'the persistence service method is not invoked'
- 0 * mockCpsDataPersistenceService.addChildDataNode(_, _, _,_)
+ 0 * mockCpsDataPersistenceService.addChildDataNode(*_)
and: 'data updated event is not sent to notification service'
- 0 * mockNotificationService.processDataUpdatedEvent(_, _, _, _)
+ 0 * mockNotificationService.processDataUpdatedEvent(*_)
where: 'the following parameters are used'
scenario | dataspaceName | anchorName
'dataspace name' | 'dataspace names with spaces' | 'anchorName'
}
)
and: 'data updated event is sent to notification service'
- 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/test-tree', Operation.UPDATE)
+ 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, '/test-tree', Operation.UPDATE, observedTimestamp)
}
def 'Saving empty list element data fragment.'() {
then: 'a data validation exception is thrown'
thrown(DataValidationException)
and: 'add list elements persistence method is not invoked'
- 0 * mockCpsDataPersistenceService.addListElements(_, _, _, _)
+ 0 * mockCpsDataPersistenceService.addListElements(*_)
where: 'the following parameters are used'
scenario | dataspaceName | anchorName
'dataspace name' | 'dataspace names with spaces' | 'anchorName'
then: 'a data validation exception is thrown'
thrown(DataValidationException)
and: 'get data node persistence service is not invoked'
- 0 * mockCpsDataPersistenceService.getDataNode(_, _, _, _)
+ 0 * mockCpsDataPersistenceService.getDataNode(*_)
where: 'the following parameters are used'
scenario | dataspaceName | anchorName
'dataspace name' | 'dataspace names with spaces' | 'anchorName'
then: 'the persistence service method is invoked with correct parameters'
1 * mockCpsDataPersistenceService.updateDataLeaves(dataspaceName, anchorName, expectedNodeXpath, leaves)
and: 'data updated event is sent to notification service'
- 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, parentNodeXpath, Operation.UPDATE)
+ 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, parentNodeXpath, Operation.UPDATE, observedTimestamp)
where: 'following parameters were used'
scenario | parentNodeXpath | jsonData || expectedNodeXpath | leaves
'top level node' | '/' | '{"test-tree": {"branch": []}}' || '/test-tree' | Collections.emptyMap()
then: 'a data validation exception is thrown'
thrown(DataValidationException)
and: 'the persistence service method is not invoked'
- 0 * mockCpsDataPersistenceService.updateDataLeaves(_, _, _, _)
+ 0 * mockCpsDataPersistenceService.updateDataLeaves(*_)
and: 'data updated event is not sent to notification service'
- 0 * mockNotificationService.processDataUpdatedEvent(_, _, _, _)
+ 0 * mockNotificationService.processDataUpdatedEvent(*_)
where: 'the following parameters are used'
scenario | dataspaceName | anchorName
'dataspace name' | 'dataspace names with spaces' | 'anchorName'
1 * mockCpsDataPersistenceService.updateDataLeaves(dataspaceName, anchorName,
"/bookstore/categories[@code='01']", ['name':'Romance', 'code': '01'])
and: 'the data updated event is sent to the notification service'
- 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/bookstore', Operation.UPDATE)
+ 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, '/bookstore', Operation.UPDATE, observedTimestamp)
}
def 'Update Bookstore node leaves with invalid #scenario' () {
then: 'a data validation exception is thrown'
thrown(DataValidationException)
and: 'the persistence service method is not invoked'
- 0 * mockCpsDataPersistenceService.updateDataLeaves(_, _, _, _)
+ 0 * mockCpsDataPersistenceService.updateDataLeaves(*_)
and: 'the data updated event is not sent to the notification service'
- 0 * mockNotificationService.processDataUpdatedEvent(_, _, _, _)
+ 0 * mockNotificationService.processDataUpdatedEvent(*_)
where: 'the following parameters are used'
scenario | dataspaceName | anchorName
'dataspace name' | 'dataspace names with spaces' | 'anchorName'
1 * mockCpsDataPersistenceService.replaceDataNodeTree(dataspaceName, anchorName,
{ dataNode -> dataNode.xpath == expectedNodeXpath })
and: 'data updated event is sent to notification service'
- 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, parentNodeXpath, Operation.UPDATE)
+ 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, parentNodeXpath, Operation.UPDATE, observedTimestamp)
where: 'following parameters were used'
scenario | parentNodeXpath | jsonData || expectedNodeXpath
'top level node' | '/' | '{"test-tree": {"branch": []}}' || '/test-tree'
then: 'a data validation exception is thrown'
thrown(DataValidationException)
and: 'the persistence service method is not invoked'
- 0 * mockCpsDataPersistenceService.replaceDataNodeTree(_, _,_)
+ 0 * mockCpsDataPersistenceService.replaceDataNodeTree(*_)
and: 'data updated event is not sent to notification service'
- 0 * mockNotificationService.processDataUpdatedEvent(_, _, _, _)
+ 0 * mockNotificationService.processDataUpdatedEvent(*_)
where: 'the following parameters are used'
scenario | dataspaceName | anchorName
'dataspace name' | 'dataspace names with spaces' | 'anchorName'
}
)
and: 'data updated event is sent to notification service'
- 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/test-tree', Operation.UPDATE)
+ 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, '/test-tree', Operation.UPDATE, observedTimestamp)
}
def 'Replace whole list content with empty list element.'() {
then: 'a data validation exception is thrown'
thrown(DataValidationException)
and: 'the persistence service method is not invoked'
- 0 * mockCpsDataPersistenceService.replaceListContent(_, _,_)
+ 0 * mockCpsDataPersistenceService.replaceListContent(*_)
and: 'data updated event is not sent to notification service'
- 0 * mockNotificationService.processDataUpdatedEvent(_, _, _, _)
+ 0 * mockNotificationService.processDataUpdatedEvent(*_)
where: 'the following parameters are used'
scenario | dataspaceName | anchorName
'dataspace name' | 'dataspace names with spaces' | 'anchorName'
then: 'the persistence service method is invoked with correct parameters'
1 * mockCpsDataPersistenceService.deleteListDataNode(dataspaceName, anchorName, '/test-tree/branch')
and: 'data updated event is sent to notification service'
- 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/test-tree/branch', Operation.DELETE)
+ 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, '/test-tree/branch', Operation.DELETE, observedTimestamp)
}
then: 'a data validation exception is thrown'
thrown(DataValidationException)
and: 'the persistence service method is not invoked'
- 0 * mockCpsDataPersistenceService.deleteListDataNode(_, _, _)
+ 0 * mockCpsDataPersistenceService.deleteListDataNode(*_)
and: 'data updated event is not sent to notification service'
- 0 * mockNotificationService.processDataUpdatedEvent(_, _, _, _)
+ 0 * mockNotificationService.processDataUpdatedEvent(*_)
where: 'the following parameters are used'
scenario | dataspaceName | anchorName
'dataspace name' | 'dataspace names with spaces' | 'anchorName'
then: 'the persistence service method is invoked with the correct parameters'
1 * mockCpsDataPersistenceService.deleteDataNode(dataspaceName, anchorName, '/data-node')
and: 'data updated event is sent to notification service'
- 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/data-node', Operation.DELETE)
+ 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, '/data-node', Operation.DELETE, observedTimestamp)
}
def 'Delete data node with an invalid #scenario.'() {
then: 'a data validation exception is thrown'
thrown(DataValidationException)
and: 'the persistence service method is not invoked'
- 0 * mockCpsDataPersistenceService.deleteDataNode(_, _, _)
+ 0 * mockCpsDataPersistenceService.deleteDataNode(*_)
and: 'data updated event is not sent to notification service'
- 0 * mockNotificationService.processDataUpdatedEvent(_, _, _, _)
+ 0 * mockNotificationService.processDataUpdatedEvent(*_)
where: 'the following parameters are used'
scenario | dataspaceName | anchorName
'dataspace name' | 'dataspace names with spaces' | 'anchorName'
setupSchemaSetMocks('test-tree.yang')
when: 'delete data node method is invoked with correct parameters'
objectUnderTest.deleteDataNodes(dataspaceName, anchorName, observedTimestamp)
- then: 'the persistence service method is invoked with the correct parameters'
+ then: 'data updated event is sent to notification service before the delete'
+ 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, '/', Operation.DELETE, observedTimestamp)
+ and: 'the persistence service method is invoked with the correct parameters'
1 * mockCpsDataPersistenceService.deleteDataNodes(dataspaceName, anchorName)
- and: 'data updated event is sent to notification service'
- 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/', Operation.DELETE)
-
}
def setupSchemaSetMocks(String... yangResources) {
/*
* ============LICENSE_START=======================================================
* Copyright (c) 2021-2022 Bell Canada.
+ * Modifications Copyright (C) 2022 Nordix Foundation
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
package org.onap.cps.notification
-import java.time.OffsetDateTime
+import org.onap.cps.api.CpsAdminService
import org.onap.cps.config.AsyncConfig
import org.onap.cps.event.model.CpsDataUpdatedEvent
import org.onap.cps.spi.model.Anchor
import spock.lang.Shared
import spock.lang.Specification
+import java.time.OffsetDateTime
import java.util.concurrent.TimeUnit
@SpringBootTest
NotificationErrorHandler spyNotificationErrorHandler
@SpringSpy
NotificationProperties spyNotificationProperties
+ @SpringBean
+ CpsAdminService mockCpsAdminService = Mock()
@Autowired
NotificationService objectUnderTest
+ @Shared
+ def dataspaceName = 'my-dataspace-published'
+ @Shared
+ def anchorName = 'my-anchorname'
@Shared
def anchor = new Anchor('my-anchorname', 'my-dataspace-published', 'my-schemaset-name')
def myObservedTimestamp = OffsetDateTime.now()
+ def setup() {
+ mockCpsAdminService.getAnchor(dataspaceName, anchorName) >> anchor
+ }
+
def 'Skip sending notification when disabled.'() {
given: 'notification is disabled'
spyNotificationProperties.isEnabled() >> false
when: 'dataUpdatedEvent is received'
- objectUnderTest.processDataUpdatedEvent(anchor, myObservedTimestamp, '/', Operation.CREATE)
+ objectUnderTest.processDataUpdatedEvent(dataspaceName, anchorName, '/', Operation.CREATE, myObservedTimestamp)
then: 'the notification is not sent'
0 * mockNotificationPublisher.sendNotification(_)
}
mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor, myObservedTimestamp, Operation.CREATE) >>
cpsDataUpdatedEvent
when: 'dataUpdatedEvent is received'
- def future = objectUnderTest.processDataUpdatedEvent(anchor, myObservedTimestamp,
- '/', Operation.CREATE)
+ def future = objectUnderTest.processDataUpdatedEvent(dataspaceName, anchorName,
+ '/', Operation.CREATE, myObservedTimestamp)
and: 'wait for async processing to complete'
future.get(10, TimeUnit.SECONDS)
then: 'async process completed successfully'
mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor, myObservedTimestamp, expectedOperationInEvent) >>
cpsDataUpdatedEvent
when: 'dataUpdatedEvent is received for #xpath'
- def future = objectUnderTest.processDataUpdatedEvent(anchor, myObservedTimestamp, xpath, operation)
+ def future = objectUnderTest.processDataUpdatedEvent(dataspaceName, anchorName, xpath, operation, myObservedTimestamp)
and: 'wait for async processing to complete'
future.get(10, TimeUnit.SECONDS)
then: 'async process completed successfully'
mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor, myObservedTimestamp, Operation.CREATE) >>
{ throw new Exception("Could not create event") }
when: 'event is sent for processing'
- def future = objectUnderTest.processDataUpdatedEvent(anchor, myObservedTimestamp, '/', Operation.CREATE)
+ def future = objectUnderTest.processDataUpdatedEvent(dataspaceName, anchorName, '/', Operation.CREATE, myObservedTimestamp)
and: 'wait for async processing to complete'
future.get(10, TimeUnit.SECONDS)
then: 'async process completed successfully'