545b183378e4b0c9d845ee2ceb3ecde5ad2fa93d
[cps.git] / cps-events / src / test / groovy / org / onap / cps / event / CpsDataUpdatedEventSpec.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  * ============LICENSE_END=========================================================
17 */
18
19 package org.onap.cps.event
20
21 import com.fasterxml.jackson.databind.ObjectMapper
22 import com.fasterxml.jackson.databind.node.ObjectNode
23 import org.onap.cps.event.model.Content
24 import org.onap.cps.event.model.CpsDataUpdatedEvent
25 import org.onap.cps.event.model.Data
26 import spock.lang.Specification
27
28 /**
29  * Test class for CpsDataUpdatedEvent.
30  */
31 class CpsDataUpdatedEventSpec extends Specification {
32
33     def objectMapper = new ObjectMapper()
34
35     final DATASPACE_NAME = 'my-dataspace'
36     final BOOKSTORE_SCHEMA_SET = 'bookstore-schemaset'
37     final ANCHOR_NAME = 'chapters'
38     final EVENT_TIMESTAMP = '2020-12-01T00:00:00.000+0000'
39     final EVENT_ID = '77b8f114-4562-4069-8234-6d059ff742ac'
40     final EVENT_SOURCE = new URI('urn:cps:org.onap.cps')
41     final EVENT_TYPE = 'org.onap.cps.data-updated-event'
42     final EVENT_SCHEMA = new URI('urn:cps:org.onap.cps:data-updated-event-schema:v1')
43
44     final DATA = [
45             'test:bookstore': [
46                     'bookstore-name': 'Chapters',
47                     'categories'    : [
48                             ['code' : '01',
49                              'name' : 'SciFi',
50                              'books': [
51                                      ['authors' : ['Iain M. Banks'],
52                                       'lang'    : 'en',
53                                       'price'   : 895,
54                                       'pub_year': '1994',
55                                       'title'   : 'Feersum Endjinn'
56                                      ]
57                              ]
58                             ]
59                     ]
60             ]
61     ]
62
63     def 'Conversion from JSON String to CpsDataUpdatedEvent POJO.'() {
64         when: 'event JSON String is converted to CpsDataUpdatedEvent'
65             def notificationMessage = getEventAsJsonStringFromFile()
66             def cpsDataUpdatedEvent = objectMapper.readValue(notificationMessage, CpsDataUpdatedEvent.class)
67         then: 'CpsDataUpdatedEvent POJO has the excepted values'
68             cpsDataUpdatedEvent.id == EVENT_ID
69             cpsDataUpdatedEvent.source == EVENT_SOURCE
70             cpsDataUpdatedEvent.schema == EVENT_SCHEMA
71             cpsDataUpdatedEvent.type == EVENT_TYPE
72             def content = cpsDataUpdatedEvent.content
73             content.observedTimestamp == EVENT_TIMESTAMP
74             content.dataspaceName == DATASPACE_NAME
75             content.schemaSetName == BOOKSTORE_SCHEMA_SET
76             content.anchorName == ANCHOR_NAME
77             content.data.getAdditionalProperties() == DATA
78     }
79
80     def 'Conversion CpsDataUpdatedEvent POJO to JSON String.'() {
81         given: 'Event content with the Data'
82             def data = new Data()
83             data.withAdditionalProperty('test:bookstore', DATA.'test:bookstore')
84             def content = new Content()
85             content.withAnchorName(ANCHOR_NAME)
86                     .withDataspaceName(DATASPACE_NAME)
87                     .withSchemaSetName(BOOKSTORE_SCHEMA_SET)
88                     .withObservedTimestamp(EVENT_TIMESTAMP)
89                     .withData(data)
90         and: 'CpsDataUpdatedEvent with the content'
91             def cpsDataUpdateEvent = new CpsDataUpdatedEvent()
92             cpsDataUpdateEvent
93                     .withSchema(EVENT_SCHEMA)
94                     .withId(EVENT_ID)
95                     .withSource(EVENT_SOURCE)
96                     .withType(EVENT_TYPE)
97                     .withContent(content)
98         when: 'CpsDataUpdatedEvent is converted to JSON string'
99             def actualMessage = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(cpsDataUpdateEvent)
100         then: 'the created JSON String is same as the expected JSON String'
101             def expectedMessage = getEventAsJsonStringFromFile()
102             assert actualMessage == expectedMessage
103     }
104
105     def getEventAsJsonStringFromFile() {
106         return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(
107                 objectMapper.readValue(
108                         this.class.getResource('/bookstore-chapters.json').getText('UTF-8'),
109                         ObjectNode.class)
110         )
111     }
112
113 }