aa0c7c0b39a91b7f803602e0fdf5c207d857aae3
[cps.git] / cps-service / src / test / groovy / org / onap / cps / notification / CpsDataUpdateEventFactorySpec.groovy
1 /*
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.notification
22
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
34
35 class CpsDataUpdateEventFactorySpec extends Specification {
36
37     def mockCpsDataService = Mock(CpsDataService)
38     def mockCpsAdminService = Mock(CpsAdminService)
39
40     def objectUnderTest = new CpsDataUpdatedEventFactory(mockCpsDataService, mockCpsAdminService)
41
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'
46
47     def 'Create a CPS data updated event successfully: #scenario'() {
48
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'
53             def xpath = '/'
54             def dataNode = new DataNodeBuilder().withXpath(xpath).withLeaves(['leafName': 'leafValue']).build()
55             mockCpsDataService.getDataNode(
56                 myDataspaceName, myAnchorName, xpath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
57
58         when: 'CPS data updated event is created'
59             def cpsDataUpdatedEvent = objectUnderTest.createCpsDataUpdatedEvent(myDataspaceName,
60                 myAnchorName, DateTimeUtility.toOffsetDateTime(inputObservedTimestamp))
61
62         then: 'CPS data updated event is created with correct envelope'
63
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)
69                 content != null
70             }
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
76                 else
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')
83             }
84         where:
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
89     }
90
91     def isExpectedDateTimeFormat(String observedTimestamp) {
92         try {
93             DateTimeFormatter.ofPattern(dateTimeFormat).parse(observedTimestamp)
94         } catch (DateTimeParseException) {
95             return false
96         }
97         return true
98     }
99
100 }