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