aecc3f7ee085c2960c927e47ea4a3dc87d23e410
[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.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
31
32 import java.time.format.DateTimeFormatter
33
34 class CpsDataUpdateEventFactorySpec extends Specification {
35
36     def mockCpsDataService = Mock(CpsDataService)
37     def mockCpsAdminService = Mock(CpsAdminService)
38
39     def objectUnderTest = new CpsDataUpdatedEventFactory(mockCpsDataService, mockCpsAdminService)
40
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'
45
46     def 'Create a CPS data updated event successfully.'() {
47
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'
52             def xpath = '/'
53             def dataNode = new DataNodeBuilder().withXpath(xpath).withLeaves(['leafName': 'leafValue']).build()
54             mockCpsDataService.getDataNode(
55                     myDataspaceName, myAnchorName, xpath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
56
57         when: 'CPS data updated event is created'
58             def cpsDataUpdatedEvent = objectUnderTest.createCpsDataUpdatedEvent(myDataspaceName, myAnchorName)
59
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)
66                 content != null
67             }
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')
74             }
75     }
76
77     def isExpectedDateTimeFormat(String observedTimestamp) {
78         try {
79             DateTimeFormatter.ofPattern(dateTimeFormat).parse(observedTimestamp)
80         } catch (DateTimeParseException) {
81             return false
82         }
83         return true
84     }
85
86 }