summaryrefslogtreecommitdiffstats
path: root/cps-service/src/test
diff options
context:
space:
mode:
Diffstat (limited to 'cps-service/src/test')
-rw-r--r--cps-service/src/test/groovy/org/onap/cps/api/impl/CpsDataServiceImplSpec.groovy29
-rw-r--r--cps-service/src/test/groovy/org/onap/cps/notification/CpsDataUpdateEventFactorySpec.groovy52
-rw-r--r--cps-service/src/test/groovy/org/onap/cps/notification/NotificationServiceSpec.groovy34
3 files changed, 70 insertions, 45 deletions
diff --git a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsDataServiceImplSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsDataServiceImplSpec.groovy
index 56b0e2d53b..785788be90 100644
--- a/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsDataServiceImplSpec.groovy
+++ b/cps-service/src/test/groovy/org/onap/cps/api/impl/CpsDataServiceImplSpec.groovy
@@ -24,7 +24,6 @@ package org.onap.cps.api.impl
import org.onap.cps.TestUtils
import org.onap.cps.api.CpsAdminService
-import org.onap.cps.api.CpsModuleService
import org.onap.cps.notification.NotificationService
import org.onap.cps.notification.Operation
import org.onap.cps.spi.CpsDataPersistenceService
@@ -47,9 +46,14 @@ class CpsDataServiceImplSpec extends Specification {
def objectUnderTest = new CpsDataServiceImpl(mockCpsDataPersistenceService, mockCpsAdminService,
mockYangTextSchemaSourceSetCache, mockNotificationService)
+ def setup() {
+ mockCpsAdminService.getAnchor(dataspaceName, anchorName) >> anchor
+ }
+
def dataspaceName = 'some dataspace'
def anchorName = 'some anchor'
def schemaSetName = 'some schema set'
+ def anchor = Anchor.builder().name(anchorName).schemaSetName(schemaSetName).build()
def observedTimestamp = OffsetDateTime.now()
def 'Saving json data.'() {
@@ -62,7 +66,7 @@ class CpsDataServiceImplSpec extends Specification {
1 * mockCpsDataPersistenceService.storeDataNode(dataspaceName, anchorName,
{ dataNode -> dataNode.xpath == '/test-tree' })
and: 'data updated event is sent to notification service'
- 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, '/', Operation.CREATE)
+ 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/', Operation.CREATE)
}
def 'Saving child data fragment under existing node.'() {
@@ -75,7 +79,7 @@ class CpsDataServiceImplSpec extends Specification {
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(dataspaceName, anchorName, observedTimestamp, '/test-tree', Operation.CREATE)
+ 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/test-tree', Operation.CREATE)
}
def 'Saving list element data fragment under existing node.'() {
@@ -95,7 +99,7 @@ class CpsDataServiceImplSpec extends Specification {
}
)
and: 'data updated event is sent to notification service'
- 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, '/test-tree', Operation.UPDATE)
+ 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/test-tree', Operation.UPDATE)
}
def 'Saving empty list element data fragment.'() {
@@ -127,7 +131,7 @@ class CpsDataServiceImplSpec extends Specification {
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(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE)
+ 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, parentNodeXpath, Operation.UPDATE)
where: 'following parameters were used'
scenario | parentNodeXpath | jsonData || expectedNodeXpath | leaves
'top level node' | '/' | '{"test-tree": {"branch": []}}' || '/test-tree' | Collections.emptyMap()
@@ -160,7 +164,7 @@ class CpsDataServiceImplSpec extends Specification {
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(dataspaceName, anchorName, observedTimestamp, '/bookstore', Operation.UPDATE)
+ 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/bookstore', Operation.UPDATE)
}
def 'Replace data node: #scenario.'() {
@@ -172,7 +176,7 @@ class CpsDataServiceImplSpec extends Specification {
1 * mockCpsDataPersistenceService.replaceDataNodeTree(dataspaceName, anchorName,
{ dataNode -> dataNode.xpath == expectedNodeXpath })
and: 'data updated event is sent to notification service'
- 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, parentNodeXpath, Operation.UPDATE)
+ 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, parentNodeXpath, Operation.UPDATE)
where: 'following parameters were used'
scenario | parentNodeXpath | jsonData || expectedNodeXpath
'top level node' | '/' | '{"test-tree": {"branch": []}}' || '/test-tree'
@@ -196,7 +200,7 @@ class CpsDataServiceImplSpec extends Specification {
}
)
and: 'data updated event is sent to notification service'
- 1 * mockNotificationService.processDataUpdatedEvent(dataspaceName, anchorName, observedTimestamp, '/test-tree', Operation.UPDATE)
+ 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/test-tree', Operation.UPDATE)
}
def 'Replace whole list content with empty list element.'() {
@@ -217,7 +221,7 @@ class CpsDataServiceImplSpec extends Specification {
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(dataspaceName, anchorName, observedTimestamp, '/test-tree/branch', Operation.DELETE)
+ 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/test-tree/branch', Operation.DELETE)
}
def 'Delete data node under anchor and dataspace.'() {
@@ -228,7 +232,7 @@ class CpsDataServiceImplSpec extends Specification {
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(dataspaceName, anchorName, observedTimestamp, '/data-node', Operation.DELETE)
+ 1 * mockNotificationService.processDataUpdatedEvent(anchor, observedTimestamp, '/data-node', Operation.DELETE)
}
def 'Delete all data nodes for a given anchor and dataspace.'() {
@@ -238,11 +242,12 @@ class CpsDataServiceImplSpec extends Specification {
objectUnderTest.deleteDataNodes(dataspaceName, anchorName, observedTimestamp)
then: '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) {
- def anchor = Anchor.builder().name(anchorName).schemaSetName(schemaSetName).build()
- mockCpsAdminService.getAnchor(dataspaceName, anchorName) >> anchor
def mockYangTextSchemaSourceSet = Mock(YangTextSchemaSourceSet)
mockYangTextSchemaSourceSetCache.get(dataspaceName, schemaSetName) >> mockYangTextSchemaSourceSet
def yangResourceNameToContent = TestUtils.getYangResourcesAsMap(yangResources)
diff --git a/cps-service/src/test/groovy/org/onap/cps/notification/CpsDataUpdateEventFactorySpec.groovy b/cps-service/src/test/groovy/org/onap/cps/notification/CpsDataUpdateEventFactorySpec.groovy
index 67ed3d90fa..5b13fa516a 100644
--- a/cps-service/src/test/groovy/org/onap/cps/notification/CpsDataUpdateEventFactorySpec.groovy
+++ b/cps-service/src/test/groovy/org/onap/cps/notification/CpsDataUpdateEventFactorySpec.groovy
@@ -36,27 +36,22 @@ import spock.lang.Specification
class CpsDataUpdateEventFactorySpec extends Specification {
def mockCpsDataService = Mock(CpsDataService)
- def mockCpsAdminService = Mock(CpsAdminService)
- def objectUnderTest = new CpsDataUpdatedEventFactory(mockCpsDataService, mockCpsAdminService)
+ def objectUnderTest = new CpsDataUpdatedEventFactory(mockCpsDataService)
- def myDataspaceName = 'my-dataspace'
- def myAnchorName = 'my-anchorname'
- def mySchemasetName = 'my-schemaset-name'
def dateTimeFormat = 'yyyy-MM-dd\'T\'HH:mm:ss.SSSZ'
def 'Create a CPS data updated event successfully: #scenario'() {
- given: 'cps admin service is able to return anchor details'
- mockCpsAdminService.getAnchor(myDataspaceName, myAnchorName) >>
- new Anchor(myAnchorName, myDataspaceName, mySchemasetName)
+ given: 'an anchor which has been updated'
+ def anchor = new Anchor('my-anchorname', 'my-dataspace', 'my-schemaset-name')
and: 'cps data service returns the data node details'
def xpath = '/'
def dataNode = new DataNodeBuilder().withXpath(xpath).withLeaves(['leafName': 'leafValue']).build()
mockCpsDataService.getDataNode(
- myDataspaceName, myAnchorName, xpath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
+ 'my-dataspace', 'my-anchorname', xpath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
when: 'CPS data updated event is created'
- def cpsDataUpdatedEvent = objectUnderTest.createCpsDataUpdatedEvent(myDataspaceName,
- myAnchorName, DateTimeUtility.toOffsetDateTime(inputObservedTimestamp), Operation.CREATE)
+ def cpsDataUpdatedEvent = objectUnderTest.createCpsDataUpdatedEvent(anchor,
+ DateTimeUtility.toOffsetDateTime(inputObservedTimestamp), Operation.CREATE)
then: 'CPS data updated event is created with correct envelope'
with(cpsDataUpdatedEvent) {
type == 'org.onap.cps.data-updated-event'
@@ -72,10 +67,10 @@ class CpsDataUpdateEventFactorySpec extends Specification {
assert observedTimestamp == inputObservedTimestamp
else
assert OffsetDateTime.now().minusSeconds(20).isBefore(
- DateTimeUtility.toOffsetDateTime(observedTimestamp))
- assert anchorName == myAnchorName
- assert dataspaceName == myDataspaceName
- assert schemaSetName == mySchemasetName
+ DateTimeUtility.toOffsetDateTime(observedTimestamp))
+ assert anchorName == 'my-anchorname'
+ assert dataspaceName == 'my-dataspace'
+ assert schemaSetName == 'my-schemaset-name'
assert operation == Content.Operation.CREATE
assert data == new Data().withAdditionalProperty('leafName', 'leafValue')
}
@@ -86,6 +81,33 @@ class CpsDataUpdateEventFactorySpec extends Specification {
'missing observed timestamp' | null
}
+ def 'Create a delete CPS data updated event successfully'() {
+ given: 'an anchor which has been deleted'
+ def anchor = new Anchor('my-anchorname', 'my-dataspace', 'my-schemaset-name')
+ def deletionTimestamp = '2021-01-01T23:00:00.345-0400'
+ when: 'a delete root data node event is created'
+ def cpsDataUpdatedEvent = objectUnderTest.createCpsDataUpdatedEvent(anchor,
+ DateTimeUtility.toOffsetDateTime(deletionTimestamp), Operation.DELETE)
+ then: 'CPS data updated event is created with correct envelope'
+ with(cpsDataUpdatedEvent) {
+ type == 'org.onap.cps.data-updated-event'
+ source == new URI('urn:cps:org.onap.cps')
+ schema == new URI('urn:cps:org.onap.cps:data-updated-event-schema:v1')
+ StringUtils.hasText(id)
+ content != null
+ }
+ and: 'correct content'
+ with(cpsDataUpdatedEvent.content) {
+ assert isExpectedDateTimeFormat(observedTimestamp): "$observedTimestamp is not in $dateTimeFormat format"
+ assert observedTimestamp == deletionTimestamp
+ assert anchorName == 'my-anchorname'
+ assert dataspaceName == 'my-dataspace'
+ assert schemaSetName == 'my-schemaset-name'
+ assert operation == Content.Operation.DELETE
+ assert data == null
+ }
+ }
+
def isExpectedDateTimeFormat(String observedTimestamp) {
try {
DateTimeFormatter.ofPattern(dateTimeFormat).parse(observedTimestamp)
diff --git a/cps-service/src/test/groovy/org/onap/cps/notification/NotificationServiceSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/notification/NotificationServiceSpec.groovy
index 306e187a00..c20bdeea7c 100644
--- a/cps-service/src/test/groovy/org/onap/cps/notification/NotificationServiceSpec.groovy
+++ b/cps-service/src/test/groovy/org/onap/cps/notification/NotificationServiceSpec.groovy
@@ -23,6 +23,7 @@ package org.onap.cps.notification
import java.time.OffsetDateTime
import org.onap.cps.config.AsyncConfig
import org.onap.cps.event.model.CpsDataUpdatedEvent
+import org.onap.cps.spi.model.Anchor
import org.spockframework.spring.SpringBean
import org.spockframework.spring.SpringSpy
import org.springframework.beans.factory.annotation.Autowired
@@ -53,15 +54,14 @@ class NotificationServiceSpec extends Specification {
NotificationService objectUnderTest
@Shared
- def myDataspacePublishedName = 'my-dataspace-published'
- def myAnchorName = 'my-anchorname'
+ def anchor = new Anchor('my-anchorname', 'my-dataspace-published', 'my-schemaset-name')
def myObservedTimestamp = OffsetDateTime.now()
def 'Skip sending notification when disabled.'() {
given: 'notification is disabled'
spyNotificationProperties.isEnabled() >> false
when: 'dataUpdatedEvent is received'
- objectUnderTest.processDataUpdatedEvent(myDataspacePublishedName, myAnchorName, myObservedTimestamp, '/', Operation.CREATE)
+ objectUnderTest.processDataUpdatedEvent(anchor, myObservedTimestamp, '/', Operation.CREATE)
then: 'the notification is not sent'
0 * mockNotificationPublisher.sendNotification(_)
}
@@ -69,13 +69,14 @@ class NotificationServiceSpec extends Specification {
def 'Send notification when enabled: #scenario.'() {
given: 'notification is enabled'
spyNotificationProperties.isEnabled() >> true
+ and: 'an anchor is in dataspace where #scenario'
+ def anchor = new Anchor('my-anchorname', dataspaceName, 'my-schemaset-name')
and: 'event factory can create event successfully'
def cpsDataUpdatedEvent = new CpsDataUpdatedEvent()
- mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(dataspaceName, myAnchorName, myObservedTimestamp,
- Operation.CREATE) >>
+ mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor, myObservedTimestamp, Operation.CREATE) >>
cpsDataUpdatedEvent
when: 'dataUpdatedEvent is received'
- def future = objectUnderTest.processDataUpdatedEvent(dataspaceName, myAnchorName, myObservedTimestamp,
+ def future = objectUnderTest.processDataUpdatedEvent(anchor, myObservedTimestamp,
'/', Operation.CREATE)
and: 'wait for async processing to complete'
future.get(10, TimeUnit.SECONDS)
@@ -86,7 +87,7 @@ class NotificationServiceSpec extends Specification {
where:
scenario | dataspaceName || expectedSendNotificationCount
'dataspace name does not match filter' | 'does-not-match-pattern' || 0
- 'dataspace name matches filter' | myDataspacePublishedName || 1
+ 'dataspace name matches filter' | 'my-dataspace-published' || 1
}
def 'Send UPDATE operation when non-root data nodes are changed.'() {
@@ -94,10 +95,10 @@ class NotificationServiceSpec extends Specification {
spyNotificationProperties.isEnabled() >> true
and: 'event factory creates event if operation is UPDATE'
def cpsDataUpdatedEvent = new CpsDataUpdatedEvent()
- mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(myDataspacePublishedName, myAnchorName, myObservedTimestamp,
+ mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor, myObservedTimestamp,
Operation.UPDATE) >> cpsDataUpdatedEvent
when: 'dataUpdatedEvent is received for non-root xpath'
- def future = objectUnderTest.processDataUpdatedEvent(myDataspacePublishedName, myAnchorName, myObservedTimestamp, '/non-root-node',
+ def future = objectUnderTest.processDataUpdatedEvent(anchor, myObservedTimestamp, '/non-root-node',
operation)
and: 'wait for async processing to complete'
future.get(10, TimeUnit.SECONDS)
@@ -114,11 +115,10 @@ class NotificationServiceSpec extends Specification {
spyNotificationProperties.isEnabled() >> true
and: 'event factory creates event if operation is #operation'
def cpsDataUpdatedEvent = new CpsDataUpdatedEvent()
- mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(myDataspacePublishedName, myAnchorName, myObservedTimestamp,
- operation) >> cpsDataUpdatedEvent
+ mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor, myObservedTimestamp, operation) >>
+ cpsDataUpdatedEvent
when: 'dataUpdatedEvent is received for root xpath'
- def future = objectUnderTest.processDataUpdatedEvent(myDataspacePublishedName, myAnchorName, myObservedTimestamp, '/',
- operation)
+ def future = objectUnderTest.processDataUpdatedEvent(anchor, myObservedTimestamp, '/', operation)
and: 'wait for async processing to complete'
future.get(10, TimeUnit.SECONDS)
then: 'async process completed successfully'
@@ -134,19 +134,17 @@ class NotificationServiceSpec extends Specification {
given: 'notification is enabled'
spyNotificationProperties.isEnabled() >> true
and: 'event factory can not create event successfully'
- mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(myDataspacePublishedName, myAnchorName,
- myObservedTimestamp, Operation.CREATE) >>
+ mockCpsDataUpdatedEventFactory.createCpsDataUpdatedEvent(anchor, myObservedTimestamp, Operation.CREATE) >>
{ throw new Exception("Could not create event") }
when: 'event is sent for processing'
- def future = objectUnderTest.processDataUpdatedEvent(myDataspacePublishedName, myAnchorName,
- myObservedTimestamp, '/', Operation.CREATE)
+ def future = objectUnderTest.processDataUpdatedEvent(anchor, myObservedTimestamp, '/', Operation.CREATE)
and: 'wait for async processing to complete'
future.get(10, TimeUnit.SECONDS)
then: 'async process completed successfully'
future.isDone()
and: 'error is handled and not thrown to caller'
notThrown Exception
- 1 * spyNotificationErrorHandler.onException(_, _, _, _)
+ 1 * spyNotificationErrorHandler.onException(_, _, _, '/', Operation.CREATE)
}
}