Prepare for next event schema version
[cps.git] / cps-service / src / test / groovy / org / onap / cps / notification / CpsDataUpdateEventFactorySpec.groovy
1 /*
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
8  *
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.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.cps.notification
21
22 import org.onap.cps.api.CpsAdminService
23 import org.onap.cps.api.CpsDataService
24 import org.onap.cps.event.model.Data
25 import org.onap.cps.spi.FetchDescendantsOption
26 import org.onap.cps.spi.model.Anchor
27 import org.onap.cps.spi.model.DataNodeBuilder
28 import org.springframework.util.StringUtils
29 import spock.lang.Specification
30
31 import java.time.format.DateTimeFormatter
32
33 class CpsDataUpdateEventFactorySpec extends Specification {
34
35     def mockCpsDataService = Mock(CpsDataService)
36     def mockCpsAdminService = Mock(CpsAdminService)
37
38     def objectUnderTest = new CpsDataUpdatedEventFactory(mockCpsDataService, mockCpsAdminService)
39
40     def myDataspaceName = 'my-dataspace'
41     def myAnchorName = 'my-anchorname'
42     def mySchemasetName = 'my-schemaset-name'
43     def dateTimeFormat = 'yyyy-MM-dd\'T\'HH:mm:ss.SSSZ'
44
45     def 'Create a CPS data updated event successfully.'() {
46
47         given: 'cps admin service is able to return anchor details'
48             mockCpsAdminService.getAnchor(myDataspaceName, myAnchorName) >>
49                 new Anchor(myAnchorName, myDataspaceName, mySchemasetName)
50         and: 'cps data service returns the data node details'
51             def xpath = '/'
52             def dataNode = new DataNodeBuilder().withXpath(xpath).withLeaves(['leafName': 'leafValue']).build()
53             mockCpsDataService.getDataNode(
54                     myDataspaceName, myAnchorName, xpath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
55
56         when: 'CPS data updated event is created'
57             def cpsDataUpdatedEvent = objectUnderTest.createCpsDataUpdatedEvent(myDataspaceName, myAnchorName)
58
59         then: 'CPS data updated event is created with expected values'
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)
65                 content != null
66             }
67             with(cpsDataUpdatedEvent.content) {
68                 assert isExpectedDateTimeFormat(observedTimestamp): "$observedTimestamp is not in $dateTimeFormat format"
69                 anchorName == myAnchorName
70                 dataspaceName == myDataspaceName
71                 schemaSetName == mySchemasetName
72                 data == new Data().withAdditionalProperty('leafName', 'leafValue')
73             }
74     }
75
76     def isExpectedDateTimeFormat(String observedTimestamp) {
77         try {
78             DateTimeFormatter.ofPattern(dateTimeFormat).parse(observedTimestamp)
79         } catch (DateTimeParseException) {
80             return false
81         }
82         return true
83     }
84
85 }