81b2bf2c952cd6e65a64335b9bca09964bc7aad2
[cps.git] / cps-service / src / test / groovy / org / onap / cps / events / CpsDataUpdateEventsServiceSpec.groovy
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2024 TechMahindra Ltd.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.events
22
23 import static org.onap.cps.events.model.Data.Operation.CREATE
24 import static org.onap.cps.events.model.Data.Operation.DELETE
25 import static org.onap.cps.events.model.Data.Operation.UPDATE
26
27 import com.fasterxml.jackson.databind.ObjectMapper
28 import io.cloudevents.CloudEvent
29 import io.cloudevents.core.CloudEventUtils
30 import io.cloudevents.jackson.PojoCloudEventDataMapper
31 import org.onap.cps.events.model.CpsDataUpdatedEvent
32 import org.onap.cps.events.model.Data
33 import org.onap.cps.spi.model.Anchor
34 import org.onap.cps.utils.JsonObjectMapper
35 import org.springframework.test.context.ContextConfiguration
36 import spock.lang.Specification
37
38 import java.time.OffsetDateTime
39
40 @ContextConfiguration(classes = [ObjectMapper, JsonObjectMapper])
41 class CpsDataUpdateEventsServiceSpec extends Specification {
42     def mockEventsPublisher = Mock(EventsPublisher)
43     def notificationsEnabled = true
44     def objectMapper = new ObjectMapper();
45
46     def objectUnderTest = new CpsDataUpdateEventsService(mockEventsPublisher)
47
48     def 'Create and Publish cps update event where events are #scenario'() {
49         given: 'an anchor, operation and observed timestamp'
50             def anchor = new Anchor('anchor01', 'dataspace01', 'schema01');
51             def operation = operationInRequest
52             def observedTimestamp = OffsetDateTime.now()
53         and: 'notificationsEnabled is #notificationsEnabled and it will be true as default'
54             objectUnderTest.notificationsEnabled = true
55         when: 'service is called to publish data update event'
56             objectUnderTest.topicName = "cps-core-event"
57             objectUnderTest.publishCpsDataUpdateEvent(anchor, xpath, operation, observedTimestamp)
58         then: 'the event contains the required attributes'
59             1 * mockEventsPublisher.publishCloudEvent('cps-core-event', 'dataspace01:anchor01', _) >> {
60             args ->
61                 {
62                     def cpsDataUpdatedEvent = (args[2] as CloudEvent)
63                     assert cpsDataUpdatedEvent.getExtension('correlationid') == 'dataspace01:anchor01'
64                     assert cpsDataUpdatedEvent.type == 'org.onap.cps.events.model.CpsDataUpdatedEvent'
65                     assert cpsDataUpdatedEvent.source.toString() == 'CPS'
66                     def actualEventOperation = CloudEventUtils.mapData(cpsDataUpdatedEvent, PojoCloudEventDataMapper.from(objectMapper, CpsDataUpdatedEvent.class)).getValue().data.operation
67                     assert actualEventOperation == expectedOperation
68                 }
69             }
70         where: 'the following values are used'
71         scenario                                   | xpath        | operationInRequest  || expectedOperation
72         'empty xpath'                              | ''           | CREATE              || CREATE
73         'root xpath and create operation'          | '/'          | CREATE              || CREATE
74         'root xpath and update operation'          | '/'          | UPDATE              || UPDATE
75         'root xpath and delete operation'          | '/'          | DELETE              || DELETE
76         'not root xpath and update operation'      | 'test'       | UPDATE              || UPDATE
77         'root node xpath and create operation'     | '/test'      | CREATE              || CREATE
78         'non root node xpath and update operation' | '/test/path' | CREATE              || UPDATE
79         'non root node xpath and delete operation' | '/test/path' | DELETE              || UPDATE
80     }
81
82     def 'publish cps update event when notification service is disabled'() {
83         given: 'an anchor, operation and observed timestamp'
84             def anchor = new Anchor('anchor01', 'dataspace01', 'schema01');
85             def operation = CREATE
86             def observedTimestamp = OffsetDateTime.now()
87         and: 'notificationsEnabled is flase'
88             objectUnderTest.notificationsEnabled = false
89         when: 'service is called to publish data update event'
90             objectUnderTest.topicName = "cps-core-event"
91             objectUnderTest.publishCpsDataUpdateEvent(anchor, '/', operation, observedTimestamp)
92         then: 'the event contains the required attributes'
93             0 * mockEventsPublisher.publishCloudEvent('cps-core-event', 'dataspace01:anchor01', _)
94     }
95 }