2 * ============LICENSE_START=======================================================
3 * Copyright (c) 2021 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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.notification
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.Data
29 import org.onap.cps.spi.FetchDescendantsOption
30 import org.onap.cps.spi.model.Anchor
31 import org.onap.cps.spi.model.DataNodeBuilder
32 import org.springframework.util.StringUtils
33 import spock.lang.Specification
35 class CpsDataUpdateEventFactorySpec extends Specification {
37 def mockCpsDataService = Mock(CpsDataService)
38 def mockCpsAdminService = Mock(CpsAdminService)
40 def objectUnderTest = new CpsDataUpdatedEventFactory(mockCpsDataService, mockCpsAdminService)
42 def myDataspaceName = 'my-dataspace'
43 def myAnchorName = 'my-anchorname'
44 def mySchemasetName = 'my-schemaset-name'
45 def dateTimeFormat = 'yyyy-MM-dd\'T\'HH:mm:ss.SSSZ'
47 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'
54 def dataNode = new DataNodeBuilder().withXpath(xpath).withLeaves(['leafName': 'leafValue']).build()
55 mockCpsDataService.getDataNode(
56 myDataspaceName, myAnchorName, xpath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
58 when: 'CPS data updated event is created'
59 def cpsDataUpdatedEvent = objectUnderTest.createCpsDataUpdatedEvent(myDataspaceName,
60 myAnchorName, DateTimeUtility.toOffsetDateTime(inputObservedTimestamp))
62 then: 'CPS data updated event is created with correct envelope'
64 with(cpsDataUpdatedEvent) {
65 type == 'org.onap.cps.data-updated-event'
66 source == new URI('urn:cps:org.onap.cps')
67 schema == new URI('urn:cps:org.onap.cps:data-updated-event-schema:v1')
68 StringUtils.hasText(id)
71 and: 'correct content'
72 with(cpsDataUpdatedEvent.content) {
73 assert isExpectedDateTimeFormat(observedTimestamp): "$observedTimestamp is not in $dateTimeFormat format"
74 if (inputObservedTimestamp != null)
75 assert observedTimestamp == inputObservedTimestamp
77 assert OffsetDateTime.now().minusSeconds(20).isBefore(
78 DateTimeUtility.toOffsetDateTime(observedTimestamp))
79 assert anchorName == myAnchorName
80 assert dataspaceName == myDataspaceName
81 assert schemaSetName == mySchemasetName
82 assert data == new Data().withAdditionalProperty('leafName', 'leafValue')
85 scenario | inputObservedTimestamp
86 'with observed timestamp -0400' | '2021-01-01T23:00:00.345-0400'
87 'with observed timestamp +0400' | '2021-01-01T23:00:00.345+0400'
88 'missing observed timestamp' | null
91 def isExpectedDateTimeFormat(String observedTimestamp) {
93 DateTimeFormatter.ofPattern(dateTimeFormat).parse(observedTimestamp)
94 } catch (DateTimeParseException) {