diff options
Diffstat (limited to 'cps-service/src/test')
-rw-r--r-- | cps-service/src/test/groovy/org/onap/cps/events/CpsDataUpdateEventsServiceSpec.groovy | 67 | ||||
-rw-r--r-- | cps-service/src/test/groovy/org/onap/cps/impl/CpsNotificationServiceImplSpec.groovy | 40 |
2 files changed, 64 insertions, 43 deletions
diff --git a/cps-service/src/test/groovy/org/onap/cps/events/CpsDataUpdateEventsServiceSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/events/CpsDataUpdateEventsServiceSpec.groovy index 6d9ff12060..e5160a0be7 100644 --- a/cps-service/src/test/groovy/org/onap/cps/events/CpsDataUpdateEventsServiceSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/events/CpsDataUpdateEventsServiceSpec.groovy @@ -21,23 +21,23 @@ package org.onap.cps.events -import static org.onap.cps.events.model.Data.Operation.CREATE -import static org.onap.cps.events.model.Data.Operation.DELETE -import static org.onap.cps.events.model.Data.Operation.UPDATE - -import org.onap.cps.api.CpsNotificationService import com.fasterxml.jackson.databind.ObjectMapper import io.cloudevents.CloudEvent import io.cloudevents.core.CloudEventUtils import io.cloudevents.jackson.PojoCloudEventDataMapper -import org.onap.cps.events.model.CpsDataUpdatedEvent +import org.onap.cps.api.CpsNotificationService import org.onap.cps.api.model.Anchor +import org.onap.cps.events.model.CpsDataUpdatedEvent import org.onap.cps.utils.JsonObjectMapper import org.springframework.test.context.ContextConfiguration import spock.lang.Specification import java.time.OffsetDateTime +import static org.onap.cps.events.model.Data.Operation.CREATE +import static org.onap.cps.events.model.Data.Operation.DELETE +import static org.onap.cps.events.model.Data.Operation.UPDATE + @ContextConfiguration(classes = [ObjectMapper, JsonObjectMapper]) class CpsDataUpdateEventsServiceSpec extends Specification { def mockEventsPublisher = Mock(EventsPublisher) @@ -48,9 +48,10 @@ class CpsDataUpdateEventsServiceSpec extends Specification { def setup() { mockCpsNotificationService.isNotificationEnabled('dataspace01', 'anchor01') >> true + objectUnderTest.topicName = 'cps-core-event' } - def 'Create and Publish cps update event where events are #scenario'() { + def 'Create and Publish cps update event where events are #scenario.'() { given: 'an anchor, operation and observed timestamp' def anchor = new Anchor('anchor01', 'dataspace01', 'schema01'); def operation = operationInRequest @@ -60,7 +61,6 @@ class CpsDataUpdateEventsServiceSpec extends Specification { and: 'cpsChangeEventNotificationsEnabled is also true' objectUnderTest.cpsChangeEventNotificationsEnabled = true when: 'service is called to publish data update event' - objectUnderTest.topicName = "cps-core-event" objectUnderTest.publishCpsDataUpdateEvent(anchor, xpath, operation, observedTimestamp) then: 'the event contains the required attributes' 1 * mockEventsPublisher.publishCloudEvent('cps-core-event', 'dataspace01:anchor01', _) >> { @@ -86,42 +86,39 @@ class CpsDataUpdateEventsServiceSpec extends Specification { 'non root node xpath and delete operation' | '/test/path' | DELETE || UPDATE } - def 'publish cps update event when #scenario'() { - given: 'an anchor, operation and observed timestamp' - def anchor = new Anchor('anchor01', 'dataspace01', 'schema01'); - def operation = CREATE - def observedTimestamp = OffsetDateTime.now() - and: 'notificationsEnabled is #notificationsEnabled' - objectUnderTest.notificationsEnabled = notificationsEnabled - and: 'cpsChangeEventNotificationsEnabled is #cpsChangeEventNotificationsEnabled' - objectUnderTest.cpsChangeEventNotificationsEnabled = cpsChangeEventNotificationsEnabled - when: 'service is called to publish data update event' - objectUnderTest.topicName = "cps-core-event" - objectUnderTest.publishCpsDataUpdateEvent(anchor, '/', operation, observedTimestamp) - then: 'the event contains the required attributes' - expectedCallToPublisher * mockEventsPublisher.publishCloudEvent('cps-core-event', 'dataspace01:anchor01', _) - where: 'below scenarios are present' - scenario | notificationsEnabled | cpsChangeEventNotificationsEnabled || expectedCallToPublisher - 'both notifications enabled' | true | true || 1 - 'both notifications disabled' | false | false || 0 - 'only CPS change event notification enabled' | false | true || 0 - 'only overall notification enabled' | true | false || 0 - - } - - def 'publish cps update event when no timestamp provided'() { + def 'Publish cps update event when no timestamp provided.'() { given: 'an anchor, operation and null timestamp' def anchor = new Anchor('anchor01', 'dataspace01', 'schema01'); - def operation = CREATE def observedTimestamp = null and: 'notificationsEnabled is true' objectUnderTest.notificationsEnabled = true and: 'cpsChangeEventNotificationsEnabled is true' objectUnderTest.cpsChangeEventNotificationsEnabled = true when: 'service is called to publish data update event' - objectUnderTest.topicName = "cps-core-event" - objectUnderTest.publishCpsDataUpdateEvent(anchor, '/', operation, observedTimestamp) + objectUnderTest.publishCpsDataUpdateEvent(anchor, '/', CREATE, observedTimestamp) then: 'the event is published' 1 * mockEventsPublisher.publishCloudEvent('cps-core-event', 'dataspace01:anchor01', _) } + + def 'Enabling and disabling publish cps update events.'() { + given: 'a different anchor' + def anchor = new Anchor('anchor02', 'some dataspace', 'some schema'); + and: 'notificationsEnabled is #notificationsEnabled' + objectUnderTest.notificationsEnabled = notificationsEnabled + and: 'cpsChangeEventNotificationsEnabled is #cpsChangeEventNotificationsEnabled' + objectUnderTest.cpsChangeEventNotificationsEnabled = cpsChangeEventNotificationsEnabled + and: 'notification service enabled is: #cpsNotificationServiceisNotificationEnabled' + mockCpsNotificationService.isNotificationEnabled(_, 'anchor02') >> cpsNotificationServiceisNotificationEnabled + when: 'service is called to publish data update event' + objectUnderTest.publishCpsDataUpdateEvent(anchor, '/', CREATE, null) + then: 'the event is only published when all related flags are true' + expectedCallsToPublisher * mockEventsPublisher.publishCloudEvent(*_) + where: 'the following flags are used' + notificationsEnabled | cpsChangeEventNotificationsEnabled | cpsNotificationServiceisNotificationEnabled || expectedCallsToPublisher + false | true | true || 0 + true | false | true || 0 + true | true | false || 0 + true | true | true || 1 + } + } diff --git a/cps-service/src/test/groovy/org/onap/cps/impl/CpsNotificationServiceImplSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/impl/CpsNotificationServiceImplSpec.groovy index 0f563272d1..b7f06456c9 100644 --- a/cps-service/src/test/groovy/org/onap/cps/impl/CpsNotificationServiceImplSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/impl/CpsNotificationServiceImplSpec.groovy @@ -46,13 +46,15 @@ class CpsNotificationServiceImplSpec extends Specification { def anchorName = 'cps-notification-subscriptions' def schemaSetName = 'cps-notification-subscriptions' def anchor = new Anchor(anchorName, dataspaceName, schemaSetName) + def someDataNode = new DataNodeBuilder().withXpath('/xpath-1').build() def mockCpsDataPersistenceService = Mock(CpsDataPersistenceService) def mockCpsAnchorService = Mock(CpsAnchorService) def mockYangTextSchemaSourceSetCache = Mock(YangTextSchemaSourceSetCache) def mockTimedYangTextSchemaSourceSetBuilder = Mock(TimedYangTextSchemaSourceSetBuilder) def yangParser = new YangParser(new YangParserHelper(), mockYangTextSchemaSourceSetCache, mockTimedYangTextSchemaSourceSetBuilder) - def mockPrefixResolver = Mock(PrefixResolver) + def mockPrefixResolver = Mock(PrefixResolver) + def objectUnderTest = new CpsNotificationServiceImpl(mockCpsAnchorService, mockCpsDataPersistenceService, yangParser, mockPrefixResolver) def 'add notification subscription for list of dataspaces'() { @@ -112,8 +114,7 @@ class CpsNotificationServiceImplSpec extends Specification { def 'is notification enabled for given anchor'() { given: 'data nodes available for given anchor' - mockCpsDataPersistenceService.getDataNodes(dataspaceName, anchorName, "/dataspaces", FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> - [new DataNodeBuilder().withXpath('/xpath-1').build()] + mockCpsDataPersistenceService.getDataNodes(dataspaceName, anchorName, "/dataspaces", FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> [someDataNode] when: 'is notification enabled is called' boolean isNotificationEnabled = objectUnderTest.isNotificationEnabled(dataspaceName, anchorName) then: 'the notification is enabled' @@ -130,26 +131,49 @@ class CpsNotificationServiceImplSpec extends Specification { assert !isNotificationEnabled } + def 'is notification enabled for given anchor because all anchors are enabled'() { + given: 'data nodes not available for given anchor' + mockCpsDataPersistenceService.getDataNodes(dataspaceName, anchorName, "/dataspaces/dataspace[@name='ds01']/anchors/anchor[@name='anchor-01']", FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> + { throw new DataNodeNotFoundException(dataspaceName, anchorName) } + and: 'data nodes not available for any specific anchor' + mockCpsDataPersistenceService.getDataNodes(dataspaceName, anchorName, "/dataspaces/dataspace[@name='ds01']/anchors", FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> + { throw new DataNodeNotFoundException(dataspaceName, anchorName) } + when: 'is notification enabled is called' + boolean isNotificationEnabled = objectUnderTest.isNotificationEnabled('ds01', 'anchor-01') + then: 'the notification is enabled' + assert isNotificationEnabled + } + def 'is notification enabled for all anchors in a dataspace'() { given: 'data nodes available for given dataspace' mockCpsDataPersistenceService.getDataNodes(dataspaceName, anchorName, "/dataspaces/dataspace[@name='ds01']", FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> - [new DataNodeBuilder().withXpath('/xpath-1').build()] + [someDataNode] and: 'data nodes not available for any specific anchor' mockCpsDataPersistenceService.getDataNodes(dataspaceName, anchorName, "/dataspaces/dataspace[@name='ds01']/anchors", FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> - { throw new DataNodeNotFoundException(dataspaceName, anchorName) } + { throw new DataNodeNotFoundException(dataspaceName, anchorName) } when: 'is notification enabled is called' boolean isNotificationEnabled = objectUnderTest.notificationEnabledForAllAnchors('ds01') then: 'the notification is enabled' assert isNotificationEnabled } - def 'is notification disabled for all anchors in a dataspace'() { + def 'is notification disabled for a dataspace'() { + given: 'No data nodes available for given dataspace' + mockCpsDataPersistenceService.getDataNodes(dataspaceName, anchorName, "/dataspaces/dataspace[@name='ds01']", FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> + { throw new DataNodeNotFoundException(dataspaceName, anchorName) } + when: 'is notification enabled is called' + boolean isNotificationEnabled = objectUnderTest.notificationEnabledForAllAnchors('ds01') + then: 'the notification is disabled' + assert !isNotificationEnabled + } + + def 'is notification disabled for some anchors in a dataspace'() { given: 'data nodes available for given dataspace' mockCpsDataPersistenceService.getDataNodes(dataspaceName, anchorName, "/dataspaces/dataspace[@name='ds01']", FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> - [new DataNodeBuilder().withXpath('/xpath-1').build()] + [someDataNode] and: 'data nodes also available for any specific anchor' mockCpsDataPersistenceService.getDataNodes(dataspaceName, anchorName, "/dataspaces/dataspace[@name='ds01']/anchors", FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> - [new DataNodeBuilder().withXpath('/xpath-1').build()] + [someDataNode] when: 'is notification enabled is called' boolean isNotificationEnabled = objectUnderTest.notificationEnabledForAllAnchors('ds01') then: 'the notification is disabled' |