Send operation information in existing notification events
[cps.git] / cps-service / src / test / groovy / org / onap / cps / notification / CpsDataUpdateEventFactorySpec.groovy
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (c) 2021-2022 Bell Canada.
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.notification
22
23 import java.time.OffsetDateTime
24 import java.time.format.DateTimeFormatter
25 import org.onap.cps.utils.DateTimeUtility
26 import org.onap.cps.api.CpsAdminService
27 import org.onap.cps.api.CpsDataService
28 import org.onap.cps.event.model.Content
29 import org.onap.cps.event.model.Data
30 import org.onap.cps.spi.FetchDescendantsOption
31 import org.onap.cps.spi.model.Anchor
32 import org.onap.cps.spi.model.DataNodeBuilder
33 import org.springframework.util.StringUtils
34 import spock.lang.Specification
35
36 class CpsDataUpdateEventFactorySpec extends Specification {
37
38     def mockCpsDataService = Mock(CpsDataService)
39     def mockCpsAdminService = Mock(CpsAdminService)
40
41     def objectUnderTest = new CpsDataUpdatedEventFactory(mockCpsDataService, mockCpsAdminService)
42
43     def myDataspaceName = 'my-dataspace'
44     def myAnchorName = 'my-anchorname'
45     def mySchemasetName = 'my-schemaset-name'
46     def dateTimeFormat = 'yyyy-MM-dd\'T\'HH:mm:ss.SSSZ'
47
48     def 'Create a CPS data updated event successfully: #scenario'() {
49         given: 'cps admin service is able to return anchor details'
50             mockCpsAdminService.getAnchor(myDataspaceName, myAnchorName) >>
51                 new Anchor(myAnchorName, myDataspaceName, mySchemasetName)
52         and: 'cps data service returns the data node details'
53             def xpath = '/'
54             def dataNode = new DataNodeBuilder().withXpath(xpath).withLeaves(['leafName': 'leafValue']).build()
55             mockCpsDataService.getDataNode(
56                 myDataspaceName, myAnchorName, xpath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
57         when: 'CPS data updated event is created'
58             def cpsDataUpdatedEvent = objectUnderTest.createCpsDataUpdatedEvent(myDataspaceName,
59                 myAnchorName, DateTimeUtility.toOffsetDateTime(inputObservedTimestamp), Operation.CREATE)
60         then: 'CPS data updated event is created with correct envelope'
61             with(cpsDataUpdatedEvent) {
62                 type == 'org.onap.cps.data-updated-event'
63                 source == new URI('urn:cps:org.onap.cps')
64                 schema == new URI('urn:cps:org.onap.cps:data-updated-event-schema:v1')
65                 StringUtils.hasText(id)
66                 content != null
67             }
68         and: 'correct content'
69             with(cpsDataUpdatedEvent.content) {
70                 assert isExpectedDateTimeFormat(observedTimestamp): "$observedTimestamp is not in $dateTimeFormat format"
71                 if (inputObservedTimestamp != null)
72                     assert observedTimestamp == inputObservedTimestamp
73                 else
74                     assert OffsetDateTime.now().minusSeconds(20).isBefore(
75                         DateTimeUtility.toOffsetDateTime(observedTimestamp))
76                 assert anchorName == myAnchorName
77                 assert dataspaceName == myDataspaceName
78                 assert schemaSetName == mySchemasetName
79                 assert operation == Content.Operation.CREATE
80                 assert data == new Data().withAdditionalProperty('leafName', 'leafValue')
81             }
82         where:
83             scenario                        | inputObservedTimestamp
84             'with observed timestamp -0400' | '2021-01-01T23:00:00.345-0400'
85             'with observed timestamp +0400' | '2021-01-01T23:00:00.345+0400'
86             'missing observed timestamp'    | null
87     }
88
89     def isExpectedDateTimeFormat(String observedTimestamp) {
90         try {
91             DateTimeFormatter.ofPattern(dateTimeFormat).parse(observedTimestamp)
92         } catch (DateTimeParseException) {
93             return false
94         }
95         return true
96     }
97
98 }