2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Bell Canada. All rights reserved.
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
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.onap.cps.notification
22 import org.onap.cps.api.CpsAdminService
23 import org.onap.cps.api.CpsDataService
24 import org.onap.cps.event.model.CpsDataUpdatedEvent
25 import org.onap.cps.event.model.Data
26 import org.onap.cps.spi.FetchDescendantsOption
27 import org.onap.cps.spi.model.Anchor
28 import org.onap.cps.spi.model.DataNodeBuilder
29 import org.springframework.util.StringUtils
30 import spock.lang.Specification
32 import java.time.format.DateTimeFormatter
34 class CpsDataUpdateEventFactorySpec extends Specification {
36 def mockCpsDataService = Mock(CpsDataService)
37 def mockCpsAdminService = Mock(CpsAdminService)
39 def objectUnderTest = new CpsDataUpdatedEventFactory(mockCpsDataService, mockCpsAdminService)
41 def myDataspaceName = 'my-dataspace'
42 def myAnchorName = 'my-anchorname'
43 def mySchemasetName = 'my-schemaset-name'
44 def dateTimeFormat = 'yyyy-MM-dd\'T\'HH:mm:ss.SSSZ'
46 def 'Create a CPS data updated event successfully.'() {
48 given: 'cps admin service is able to return anchor details'
49 mockCpsAdminService.getAnchor(myDataspaceName, myAnchorName) >>
50 new Anchor(myAnchorName, myDataspaceName, mySchemasetName)
51 and: 'cps data service returns the data node details'
53 def dataNode = new DataNodeBuilder().withXpath(xpath).withLeaves(['leafName': 'leafValue']).build()
54 mockCpsDataService.getDataNode(
55 myDataspaceName, myAnchorName, xpath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
57 when: 'CPS data updated event is created'
58 def cpsDataUpdatedEvent = objectUnderTest.createCpsDataUpdatedEvent(myDataspaceName, myAnchorName)
60 then: 'CPS data updated event is created with expected values'
61 with(cpsDataUpdatedEvent) {
62 type == 'org.onap.cps.data-updated-event'
63 source == new URI('urn:cps:org.onap.cps')
64 schema == CpsDataUpdatedEvent.Schema.URN_CPS_ORG_ONAP_CPS_DATA_UPDATED_EVENT_SCHEMA_1_1_0_SNAPSHOT
65 StringUtils.hasText(id)
68 with(cpsDataUpdatedEvent.content) {
69 assert isExpectedDateTimeFormat(observedTimestamp): "$observedTimestamp is not in $dateTimeFormat format"
70 anchorName == myAnchorName
71 dataspaceName == myDataspaceName
72 schemaSetName == mySchemasetName
73 data == new Data().withAdditionalProperty('leafName', 'leafValue')
77 def isExpectedDateTimeFormat(String observedTimestamp) {
79 DateTimeFormatter.ofPattern(dateTimeFormat).parse(observedTimestamp)
80 } catch (DateTimeParseException) {