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
9 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
19 package org.onap.cps.event
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
30 * Test class for CpsDataUpdatedEvent.
32 class CpsDataUpdatedEventSpec extends Specification {
34 def objectMapper = new ObjectMapper()
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')
49 'bookstore-name': 'Chapters',
54 ['authors' : ['Iain M. Banks'],
58 'title' : 'Feersum Endjinn'
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
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) {
90 source == EVENT_SOURCE
91 schema == EVENT_SCHEMA_V2
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
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
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)
123 and: 'CpsDataUpdatedEvent with the content'
124 def cpsDataUpdateEvent = new CpsDataUpdatedEvent()
126 .withSchema(EVENT_SCHEMA_V2)
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
138 def getEventAsJsonStringFromFile(String fileName) {
139 return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(
140 objectMapper.readValue(
141 this.class.getResource(fileName).getText('UTF-8'),