Upgrade event schema to support delete operation
[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.Shared
27 import spock.lang.Specification
28
29 /**
30  * Test class for CpsDataUpdatedEvent.
31  */
32 class CpsDataUpdatedEventSpec extends Specification {
33
34     def objectMapper = new ObjectMapper()
35
36     final DATASPACE_NAME = 'my-dataspace'
37     final BOOKSTORE_SCHEMA_SET = 'bookstore-schemaset'
38     final ANCHOR_NAME = 'chapters'
39     final EVENT_TIMESTAMP = '2020-12-01T00:00:00.000+0000'
40     final EVENT_ID = '77b8f114-4562-4069-8234-6d059ff742ac'
41     final EVENT_SOURCE = new URI('urn:cps:org.onap.cps')
42     final EVENT_TYPE = 'org.onap.cps.data-updated-event'
43     final EVENT_SCHEMA_V1 = new URI('urn:cps:org.onap.cps:data-updated-event-schema:v1')
44     final EVENT_SCHEMA_V2 = new URI('urn:cps:org.onap.cps:data-updated-event-schema:v2')
45
46     @Shared
47     final DATA = [
48             'test:bookstore': [
49                     'bookstore-name': 'Chapters',
50                     'categories'    : [
51                             ['code' : '01',
52                              'name' : 'SciFi',
53                              'books': [
54                                      ['authors' : ['Iain M. Banks'],
55                                       'lang'    : 'en',
56                                       'price'   : 895,
57                                       'pub_year': '1994',
58                                       'title'   : 'Feersum Endjinn'
59                                      ]
60                              ]
61                             ]
62                     ]
63             ]
64     ]
65
66     def 'Conversion from Event V1 JSON String to CpsDataUpdatedEvent POJO.'() {
67         when: 'event V1 JSON String is converted to CpsDataUpdatedEvent'
68             def notificationMessage = getEventAsJsonStringFromFile('/event-v1.json')
69             def cpsDataUpdatedEvent = objectMapper.readValue(notificationMessage, CpsDataUpdatedEvent.class)
70         then: 'CpsDataUpdatedEvent POJO has the excepted values'
71             cpsDataUpdatedEvent.id == EVENT_ID
72             cpsDataUpdatedEvent.source == EVENT_SOURCE
73             cpsDataUpdatedEvent.schema == EVENT_SCHEMA_V1
74             cpsDataUpdatedEvent.type == EVENT_TYPE
75             def content = cpsDataUpdatedEvent.content
76             content.observedTimestamp == EVENT_TIMESTAMP
77             content.dataspaceName == DATASPACE_NAME
78             content.schemaSetName == BOOKSTORE_SCHEMA_SET
79             content.anchorName == ANCHOR_NAME
80             content.data.getAdditionalProperties() == DATA
81     }
82
83     def 'Conversion from Event V2 JSON String to CpsDataUpdatedEvent POJO'() {
84         when: 'event V2 JSON String is converted to CpsDataUpdatedEvent'
85             def notificationMessage = getEventAsJsonStringFromFile(inputEventJson)
86             def cpsDataUpdatedEvent = objectMapper.readValue(notificationMessage, CpsDataUpdatedEvent.class)
87         then: 'CpsDataUpdatedEvent POJO has the excepted values'
88             with(cpsDataUpdatedEvent) {
89                 id == EVENT_ID
90                 source == EVENT_SOURCE
91                 schema == EVENT_SCHEMA_V2
92                 type == EVENT_TYPE
93             }
94             with(cpsDataUpdatedEvent.content) {
95                 observedTimestamp == EVENT_TIMESTAMP
96                 dataspaceName == DATASPACE_NAME
97                 schemaSetName == BOOKSTORE_SCHEMA_SET
98                 anchorName == ANCHOR_NAME
99                 operation == expectedOperation
100                 if (expectedData != null)
101                     data.getAdditionalProperties() == expectedData
102                 else
103                     data == null
104             }
105         where:
106             scenario                        | inputEventJson                              || expectedData | expectedOperation
107             'create operation'              | '/event-v2-create-operation.json'           || DATA         | Content.Operation.CREATE
108             'delete operation'              | '/event-v2-delete-operation.json'           || null         | Content.Operation.DELETE
109             'create with additional fields' | '/event-v2-with-additional-properties.json' || DATA         | Content.Operation.CREATE
110     }
111
112     def 'Conversion from CpsDataUpdatedEvent POJO to Event V2 JSON String.'() {
113         given: 'Event V2 content with the Data'
114             def data = new Data()
115             data.withAdditionalProperty('test:bookstore', DATA.'test:bookstore')
116             def content = new Content()
117             content.withAnchorName(ANCHOR_NAME)
118                     .withDataspaceName(DATASPACE_NAME)
119                     .withSchemaSetName(BOOKSTORE_SCHEMA_SET)
120                     .withObservedTimestamp(EVENT_TIMESTAMP)
121                     .withOperation(Content.Operation.CREATE)
122                     .withData(data)
123         and: 'CpsDataUpdatedEvent with the content'
124             def cpsDataUpdateEvent = new CpsDataUpdatedEvent()
125             cpsDataUpdateEvent
126                     .withSchema(EVENT_SCHEMA_V2)
127                     .withId(EVENT_ID)
128                     .withSource(EVENT_SOURCE)
129                     .withType(EVENT_TYPE)
130                     .withContent(content)
131         when: 'CpsDataUpdatedEvent is converted to Event V2 JSON string'
132             def actualMessage = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(cpsDataUpdateEvent)
133         then: 'the created JSON String is same as the expected JSON String'
134             def expectedMessage = getEventAsJsonStringFromFile('/event-v2-create-operation.json')
135             assert actualMessage == expectedMessage
136     }
137
138     def getEventAsJsonStringFromFile(String fileName) {
139         return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(
140                 objectMapper.readValue(
141                         this.class.getResource(fileName).getText('UTF-8'),
142                         ObjectNode.class)
143         )
144     }
145 }