2 * ============LICENSE_START=======================================================
3 * Copyright (c) 2021-2022 Bell Canada.
4 * Modifications Copyright (c) 2022 Nordix Foundation
5 * Modifications Copyright (C) 2023 TechMahindra Ltd.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.cps.notification
25 import java.time.OffsetDateTime
26 import java.time.format.DateTimeFormatter
27 import org.onap.cps.utils.DateTimeUtility
28 import org.onap.cps.utils.PrefixResolver
29 import org.onap.cps.api.CpsDataService
30 import org.onap.cps.event.model.Content
31 import org.onap.cps.event.model.Data
32 import org.onap.cps.spi.FetchDescendantsOption
33 import org.onap.cps.spi.model.Anchor
34 import org.onap.cps.spi.model.DataNodeBuilder
35 import org.springframework.util.StringUtils
36 import spock.lang.Specification
38 class CpsDataUpdateEventFactorySpec extends Specification {
40 def mockCpsDataService = Mock(CpsDataService)
42 def mockPrefixResolver = Mock(PrefixResolver)
44 def objectUnderTest = new CpsDataUpdatedEventFactory(mockCpsDataService, mockPrefixResolver)
46 def dateTimeFormat = 'yyyy-MM-dd\'T\'HH:mm:ss.SSSZ'
48 def 'Create a CPS data updated event successfully: #scenario'() {
49 given: 'an anchor which has been updated'
50 def anchor = new Anchor('my-anchorname', 'my-dataspace', 'my-schemaset-name')
51 and: 'cps data service returns the data node details'
53 def dataNode = new DataNodeBuilder().withXpath(xpath).withLeaves(['leafName': 'leafValue']).build()
54 mockCpsDataService.getDataNodes(
55 'my-dataspace', 'my-anchorname', '/', FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> [dataNode]
56 when: 'CPS data updated event is created'
57 def cpsDataUpdatedEvent = objectUnderTest.createCpsDataUpdatedEvent(anchor,
58 DateTimeUtility.toOffsetDateTime(inputObservedTimestamp), Operation.CREATE)
59 then: 'CPS data updated event is created with correct envelope'
60 with(cpsDataUpdatedEvent) {
61 type == 'org.onap.cps.data-updated-event'
62 source == new URI('urn:cps:org.onap.cps')
63 schema == new URI('urn:cps:org.onap.cps:data-updated-event-schema:v1')
64 StringUtils.hasText(id)
67 and: 'correct content'
68 with(cpsDataUpdatedEvent.content) {
69 assert isExpectedDateTimeFormat(observedTimestamp): "$observedTimestamp is not in $dateTimeFormat format"
70 if (inputObservedTimestamp != null)
71 assert observedTimestamp == inputObservedTimestamp
73 assert OffsetDateTime.now().minusSeconds(20).isBefore(
74 DateTimeUtility.toOffsetDateTime(observedTimestamp))
75 assert anchorName == 'my-anchorname'
76 assert dataspaceName == 'my-dataspace'
77 assert schemaSetName == 'my-schemaset-name'
78 assert operation == Content.Operation.CREATE
79 assert data == new Data().withAdditionalProperty('xpath', ['leafName': 'leafValue'])
82 scenario | inputObservedTimestamp
83 'with observed timestamp -0400' | '2021-01-01T23:00:00.345-0400'
84 'with observed timestamp +0400' | '2021-01-01T23:00:00.345+0400'
85 'missing observed timestamp' | null
88 def 'Create a delete CPS data updated event successfully'() {
89 given: 'an anchor which has been deleted'
90 def anchor = new Anchor('my-anchorname', 'my-dataspace', 'my-schemaset-name')
91 def deletionTimestamp = '2021-01-01T23:00:00.345-0400'
92 when: 'a delete root data node event is created'
93 def cpsDataUpdatedEvent = objectUnderTest.createCpsDataUpdatedEvent(anchor,
94 DateTimeUtility.toOffsetDateTime(deletionTimestamp), Operation.DELETE)
95 then: 'CPS data updated event is created with correct envelope'
96 with(cpsDataUpdatedEvent) {
97 type == 'org.onap.cps.data-updated-event'
98 source == new URI('urn:cps:org.onap.cps')
99 schema == new URI('urn:cps:org.onap.cps:data-updated-event-schema:v1')
100 StringUtils.hasText(id)
103 and: 'correct content'
104 with(cpsDataUpdatedEvent.content) {
105 assert isExpectedDateTimeFormat(observedTimestamp): "$observedTimestamp is not in $dateTimeFormat format"
106 assert observedTimestamp == deletionTimestamp
107 assert anchorName == 'my-anchorname'
108 assert dataspaceName == 'my-dataspace'
109 assert schemaSetName == 'my-schemaset-name'
110 assert operation == Content.Operation.DELETE
115 def isExpectedDateTimeFormat(String observedTimestamp) {
117 DateTimeFormatter.ofPattern(dateTimeFormat).parse(observedTimestamp)
118 } catch (DateTimeParseException) {